From eb905e7176bdc9d823ccc354f0ae2a76253e5582 Mon Sep 17 00:00:00 2001 From: omair saleh Date: Sat, 5 Mar 2022 17:59:02 +0800 Subject: [PATCH] fix active customer list --- app/Models/CompanyModule.php | 11 +++++++++++ routes/web.php | 26 +++++++++++--------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/Models/CompanyModule.php b/app/Models/CompanyModule.php index b0058389..180d8286 100644 --- a/app/Models/CompanyModule.php +++ b/app/Models/CompanyModule.php @@ -21,6 +21,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use PhpParser\Node\Expr\AssignOp\Mod; +use Staudenmeir\EloquentHasManyDeep\HasManyDeep; +use Staudenmeir\EloquentHasManyDeep\HasRelationships; /** * Class CompanyModule @@ -32,6 +34,7 @@ use PhpParser\Node\Expr\AssignOp\Mod; */ class CompanyModule extends AbstractModel implements Addressable, Documentable, Contactable, ContainerOwner, Packable { + use HasRelationships; use SoftDeletes; protected $table = 'company_modules'; @@ -70,6 +73,14 @@ class CompanyModule extends AbstractModel implements Addressable, Documentable, return $this->morphMany(PackingList::class, 'owner'); } + /** + * @return hasManyDeep + */ + public function orderPackingLists(): hasManyDeep + { + return $this->hasManyDeep(PackingList::class, [Order::class], ['company_module_id', ['owner_type', 'owner_id']], ['id', null]); + } + /** * @return belongsToMany */ diff --git a/routes/web.php b/routes/web.php index 405697ba..741782f4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -204,22 +204,18 @@ Route::get('/settings', function () { return view('pages.settings'); })->name('settings'); -Route::get('/customers/active/{active_start}/{active_end}/{?inactive_start}/{?inactive_end}', function ($active_start, $active_end, $inactive_start, $inactive_end) { - $companies = CompanyModule::where('type', \App\Classes\ValueObjects\Constants\BusinessType::IMPORTER)->whereHas('orders', function($query) use($active_start, $active_end, $inactive_start, $inactive_end){ - return $query->whereHas('packingLists', function($query) use($active_start, $active_end){ - return $query->where('type', \App\Classes\ValueObjects\Constants\PackingListType::WAREHOUSE_RECEIVE_LIST)->whereHas('transports', function($query) use($active_start, $active_end){ - return $query->whereHas('schedules', function($query) use($active_start, $active_end){ - return $query->where('eta', '>=', \Carbon\Carbon::parse($active_start))->where('eta', '<=', \Carbon\Carbon::parse($active_end)->addDay()); - }); - }); - })->whereDoesntHave('packingLists', function($query) use($inactive_start, $inactive_end){ - return $query->where('type', \App\Classes\ValueObjects\Constants\PackingListType::WAREHOUSE_RECEIVE_LIST)->whereHas('transports', function($query) use($inactive_start, $inactive_end){ - return $query->whereDoesntHave('schedules', function($query) use($inactive_start, $inactive_end){ - return $query->where('eta', '>=', \Carbon\Carbon::parse($inactive_start))->where('eta', '<=', \Carbon\Carbon::parse($inactive_end)->addDay()); - }); - }); +Route::get('/customers/active/{active_start}/{active_end}/{inactive_start?}/{inactive_end?}', function ($active_start, $active_end, $inactive_start=null, $inactive_end=null) { + $activeCompanies = CompanyModule::where('type', \App\Classes\ValueObjects\Constants\BusinessType::IMPORTER)->whereHas('orderPackingLists', function($query) use($inactive_start, $inactive_end) { + return $query->where('packing_lists.type', \App\Classes\ValueObjects\Constants\PackingListType::WAREHOUSE_RECEIVE_LIST)->whereHas('transports', function ($query) use ($inactive_start, $inactive_end) { + return $query->where('drop_date', '>=', \Carbon\Carbon::parse($inactive_start))->where('drop_date', '<=', \Carbon\Carbon::parse($inactive_end)->addDay()); }); - })->get(); + })->pluck('id'); + + $companies = CompanyModule::where('type', \App\Classes\ValueObjects\Constants\BusinessType::IMPORTER)->whereHas('orderPackingLists', function($query) use($active_start, $active_end) { + return $query->where('packing_lists.type', \App\Classes\ValueObjects\Constants\PackingListType::WAREHOUSE_RECEIVE_LIST)->whereHas('transports', function ($query) use ($active_start, $active_end) { + return $query->whereDate('drop_date', '>=', \Carbon\Carbon::parse($active_start))->whereDate('drop_date', '<=', \Carbon\Carbon::parse($active_end)->addDay()); + }); + })->whereNotIn('id', $activeCompanies)->get(); foreach ($companies as $key => $company){ $connection = $company->inviters()->withPivot('invitee_reference')->first();