Merge pull request #1099 from openclassify/mtop

added bestseller twig function
This commit is contained in:
Fatih Alp 2021-06-28 18:10:32 +03:00 committed by GitHub
commit 7439ffbb5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 571 additions and 464 deletions

View File

@ -0,0 +1,26 @@
<?php
use Anomaly\Streams\Platform\Database\Migration\Migration;
class VisiosoftModuleAdvsAddTotalSalesField extends Migration
{
protected $delete = false;
protected $stream = [
'slug' => 'advs',
];
protected $fields = [
'total_sales' => [
'type' => 'anomaly.field_type.integer',
'config' => [
'min' => 0,
'default_value' => 0,
],
],
];
protected $assignments = [
'total_sales'
];
}

View File

@ -22,6 +22,7 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface
'detail_url', 'detail_url',
'currency_price', 'currency_price',
'category1', 'category1',
'currency_standard_price',
'category2', 'category2',
'thumbnail', 'thumbnail',
]; ];
@ -40,6 +41,14 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface
return app(Currency::class)->format($this->price, $this->currency); return app(Currency::class)->format($this->price, $this->currency);
} }
public function getCurrencyStandardPriceAttribute()
{
if ($this->standard_price > $this->price) {
return app(Currency::class)->format($this->standard_price, $this->currency);
}
return null;
}
public function getCategory1Attribute() public function getCategory1Attribute()
{ {
return $this->hasMany('Visiosoft\CatsModule\Category\CategoryModel', 'id', 'cat1')->first(); return $this->hasMany('Visiosoft\CatsModule\Category\CategoryModel', 'id', 'cat1')->first();
@ -410,6 +419,13 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface
} }
} }
public function currentAds() {
return $this->whereDate('finish_at', '>=', date("Y-m-d H:i:s"))
->where('status', '=', 'approved')
->where('slug', '!=', '')
->orderBy('publish_at', 'desc');
}
public function inStock() public function inStock()
{ {
return $this->is_get_adv && $this->stock; return $this->is_get_adv && $this->stock;

View File

@ -379,16 +379,21 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface
*/ */
public function latestAds() public function latestAds()
{ {
$latest_advs = $this->model $latest_advs = $this->model->currentAds()
->whereDate('finish_at', '>=', date("Y-m-d H:i:s")) ->limit(setting_value('visiosoft.module.advs::latest-limit'))
->where('status', '=', 'approved') ->get();
->where('slug', '!=', '') return $this->model->getLocationNames($latest_advs);
->orderBy('publish_at', 'desc') }
->limit(setting_value('visiosoft.module.advs::latest-limit'))->get();
$ads = $this->model->getLocationNames($latest_advs); public function bestsellerAds($catId = null, $limit = 10)
{
return $ads; return $this->model->currentAds()->orderBy('total_sales', 'desc')
->where(function ($query) use ($catId) {
if ($catId) {
$query->where('cat1', $catId);
}
})
->limit($limit)->get();
} }
public function getByCat($catID, $level = 1, $limit = 20) public function getByCat($catID, $level = 1, $limit = 20)

View File

@ -35,6 +35,8 @@ interface AdvRepositoryInterface extends EntryRepositoryInterface
public function latestAds(); public function latestAds();
public function bestsellerAds($catId= null, $limit = 10);
public function getByCat($catID, $level = 1, $limit = 20); public function getByCat($catID, $level = 1, $limit = 20);
public function getAdsCountByCategory($catID, $level = 1); public function getAdsCountByCategory($catID, $level = 1);

View File

@ -9,6 +9,7 @@ use Visiosoft\AdvsModule\Adv\Command\getPopular;
use Visiosoft\AdvsModule\Adv\Command\GetUserAds; use Visiosoft\AdvsModule\Adv\Command\GetUserAds;
use Visiosoft\AdvsModule\Adv\Command\isActive; use Visiosoft\AdvsModule\Adv\Command\isActive;
use Visiosoft\AdvsModule\Adv\Command\LatestAds; use Visiosoft\AdvsModule\Adv\Command\LatestAds;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
use Visiosoft\AdvsModule\Support\Command\Currency; use Visiosoft\AdvsModule\Support\Command\Currency;
class AdvsModulePlugin extends Plugin class AdvsModulePlugin extends Plugin
@ -49,6 +50,12 @@ class AdvsModulePlugin extends Plugin
return $latestAds; return $latestAds;
} }
), ),
new \Twig_SimpleFunction(
'bestsellerAds',
function ($catId = null, $limit = 10) {
return app(AdvRepositoryInterface::class)->bestsellerAds($catId, $limit);
}
),
new \Twig_SimpleFunction( new \Twig_SimpleFunction(
'appendRequestURL', 'appendRequestURL',
function ($request, $url, $new_parameters, $removeParams = []) { function ($request, $url, $new_parameters, $removeParams = []) {

View File

@ -15,6 +15,7 @@ use Visiosoft\AdvsModule\Adv\Form\AdvFormBuilder;
use Visiosoft\AdvsModule\Http\Middleware\redirectDiffrentLang; use Visiosoft\AdvsModule\Http\Middleware\redirectDiffrentLang;
use Visiosoft\AdvsModule\Http\Middleware\SetLang; use Visiosoft\AdvsModule\Http\Middleware\SetLang;
use Visiosoft\AdvsModule\Listener\AddAdvsSettingsScript; use Visiosoft\AdvsModule\Listener\AddAdvsSettingsScript;
use Visiosoft\AdvsModule\Listener\AddTotalSales;
use Visiosoft\AdvsModule\Option\Contract\OptionRepositoryInterface; use Visiosoft\AdvsModule\Option\Contract\OptionRepositoryInterface;
use Visiosoft\AdvsModule\Option\OptionRepository; use Visiosoft\AdvsModule\Option\OptionRepository;
use Visiosoft\AdvsModule\OptionConfiguration\Contract\OptionConfigurationRepositoryInterface; use Visiosoft\AdvsModule\OptionConfiguration\Contract\OptionConfigurationRepositoryInterface;
@ -33,6 +34,7 @@ use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\CatsModule\Category\CategoryRepository; use Visiosoft\CatsModule\Category\CategoryRepository;
use Visiosoft\LocationModule\Country\Contract\CountryRepositoryInterface; use Visiosoft\LocationModule\Country\Contract\CountryRepositoryInterface;
use Visiosoft\LocationModule\Country\CountryRepository; use Visiosoft\LocationModule\Country\CountryRepository;
use Visiosoft\OrdersModule\Orderdetail\Event\CreatedOrderDetail;
class AdvsModuleServiceProvider extends AddonServiceProvider class AdvsModuleServiceProvider extends AddonServiceProvider
{ {
@ -245,7 +247,9 @@ class AdvsModuleServiceProvider extends AddonServiceProvider
protected $listeners = [ protected $listeners = [
TableIsQuerying::class => [ TableIsQuerying::class => [
AddAdvsSettingsScript::class, AddAdvsSettingsScript::class,
], ], CreatedOrderDetail::class => [
AddTotalSales::class,
]
]; ];
protected $bindings = [ protected $bindings = [

View File

@ -0,0 +1,24 @@
<?php namespace Visiosoft\AdvsModule\Listener;
use Visiosoft\AdvsModule\Adv\AdvModel;
use Visiosoft\OrdersModule\Orderdetail\Event\CreatedOrderDetail;
class AddTotalSales
{
private $advModel;
public function __construct(
AdvModel $advModel
)
{
$this->advModel = $advModel;
}
public function handle(CreatedOrderDetail $event)
{
$item = $event->getOrderItem();
$adv = $this->advModel->find($event->getOrderItem()->item_id);
$total = $adv->total_sales + $item->piece;
$adv->total_sales = $total;
$adv->save();
}
}

View File

@ -0,0 +1,21 @@
<?php namespace Visiosoft\AdvsModule\OptionHandler;
use Anomaly\CheckboxesFieldType\CheckboxesFieldType;
use Visiosoft\AdvsModule\Adv\AdvModel;
class AdvsOptions
{
private $advModel;
public function __construct(AdvModel $advModel)
{
$this->advModel = $advModel;
}
public function handle(CheckboxesFieldType $fieldType)
{
$categories = $this->advModel->currentAds()->get();
$options = $categories->pluck('name', 'id')->all();
$fieldType->setOptions($options);
}
}

View File

@ -61,9 +61,11 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
) )
->leftJoin((DB::raw($dBName . ' c2')), DB::raw('c2.parent_category_id'), '=', DB::raw('c1.id')) ->leftJoin((DB::raw($dBName . ' c2')), DB::raw('c2.parent_category_id'), '=', DB::raw('c1.id'))
->leftJoin((DB::raw($dBNamet . ' t1')), DB::raw('c1.id'), '=', DB::raw('t1.entry_id')) ->leftJoin((DB::raw($dBNamet . ' t1')), DB::raw('c1.id'), '=', DB::raw('t1.entry_id'))
->leftJoin((DB::raw($dBNamet . ' t2')), DB::raw('c2.id'), '=', DB::raw('t2.entry_id')) ->leftJoin((DB::raw($dBNamet . ' t2')), function ($join) {
$join->on(DB::raw('c2.id'), '=', DB::raw('t2.entry_id'))
->where(DB::raw('t2.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale')));
})
->where(DB::raw('t1.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale'))) ->where(DB::raw('t1.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale')))
->where(DB::raw('t2.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale')))
->where(DB::raw("c1.deleted_at"), NULL) ->where(DB::raw("c1.deleted_at"), NULL)
->where(DB::raw("c2.deleted_at"), NULL) ->where(DB::raw("c2.deleted_at"), NULL)
->whereNull(DB::raw("c1.parent_category_id")) ->whereNull(DB::raw("c1.parent_category_id"))