From 04215916f5cacc5071e4edbf68b84af86eb77eb5 Mon Sep 17 00:00:00 2001 From: omair saleh Date: Tue, 12 Oct 2021 12:25:26 +0800 Subject: [PATCH] add report from to filters --- .../Reports/MonthlyReportController.php | 33 +++++++++---------- app/Models/Container.php | 20 +++++------ 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Reports/MonthlyReportController.php b/app/Http/Controllers/Reports/MonthlyReportController.php index cb48a5d7..2406d197 100644 --- a/app/Http/Controllers/Reports/MonthlyReportController.php +++ b/app/Http/Controllers/Reports/MonthlyReportController.php @@ -5,9 +5,7 @@ namespace App\Http\Controllers\Reports; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\BusinessType; use App\Classes\ValueObjects\Constants\HttpStatus; -use App\Classes\ValueObjects\Constants\PackingListType; use App\Classes\ValueObjects\Response\ApiResponseObject; -use App\Models\Company; use App\Models\CompanyModule; use App\Models\Container; use App\Models\Order; @@ -19,13 +17,19 @@ use Illuminate\Http\Request; class MonthlyReportController { public function salesReport(Request $request): JsonResponse { - $today = Carbon::now()->subMonth(); - $start_day = 20; - $end_day = 30; + $from = $request->input('from') ? Carbon::parse($request->input('from')) : Carbon::now()->startOfMonth(); + $to = $request->input('to') ? Carbon::parse($request->input('to')) : Carbon::now()->endOfMonth(); - $containers = Container::whereDay('loading_date', '>=', $start_day)->whereDay('loading_date', '<=', $end_day)->whereMonth('loading_date', $today->format('m'))->whereYear('loading_date', $today->format('Y'))->orderBy('loading_date')->get(); + $containers = Container::whereBetween('loading_date', [$from, $to])->orderBy('loading_date')->get(); + $customers = $containers->flatMap(function ($container) { + return $container->companyModules; + }); + + $orders = $containers->flatMap(function ($container) { + return $container->orders; + })->unique(); $packages = $containers->flatMap(function ($container) { return $container->packages; @@ -35,19 +39,12 @@ class MonthlyReportController return (( (float) $package->width / 100) * ( (float) $package->length / 100) * ( (float) $package->height / 100)) * $package->quantity; }); - $customers = $packages->unique(function ($package) { - return $package->companyModule; - }); - $orders = $packages->unique(function ($package) { - return $package->order; - }); - - $days = Carbon::now()->addDay()->diffInDaysFiltered(function (Carbon $date) { + $days = Carbon::parse('20-09-2021')->addDay()->diffInDaysFiltered(function (Carbon $date) { return $date->isWeekday(); - }, Carbon::now()->endOfMonth()); + }, Carbon::parse('20-09-2021')->endOfMonth()); return (new ApiResponseObject('fetch monthly report Successful', '', @@ -55,15 +52,15 @@ class MonthlyReportController 'containers' => count($containers), 'activated_orders' => count($orders), 'active_customers' => count($customers), - 'new_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->whereDay('created_at', '>=', $start_day)->whereDay('created_at', '<=', $end_day)->whereMonth('created_at', $today->format('m'))->whereYear('created_at', $today->format('Y'))->count(), - 'new_converted_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->whereDay('created_at', '>=', $start_day)->whereDay('created_at', '<=', $end_day)->whereMonth('created_at', $today->format('m'))->whereYear('created_at', $today->format('Y'))->whereHas('orders', function($order){ + 'new_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->whereBetween('created_at', [$from, $to])->count(), + 'new_converted_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->whereBetween('created_at', [$from, $to])->whereHas('orders', function($order){ return $order->whereHas('packingLists'); })->count(), 'total_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->count(), 'total_cbm' => $totalCbm, 'daily_cbm' => (800 - $totalCbm) / $days, 'packages' => $packages->sum('quantity'), - 'total_orders' => Order::whereDay('created_at', '>=', $start_day)->whereDay('created_at', '<=', $end_day)->whereMonth('created_at', $today->format('m'))->whereYear('created_at', $today->format('Y'))->count() + 'total_orders' => Order::whereBetween('created_at', [$from, $to])->count() ]]))->handler(); } diff --git a/app/Models/Container.php b/app/Models/Container.php index 158d0845..93aaeee5 100644 --- a/app/Models/Container.php +++ b/app/Models/Container.php @@ -4,9 +4,9 @@ namespace App\Models; use App\Classes\General\Interfaces\Transportable; use Illuminate\Database\Eloquent\Relations\BelongsToMany; -use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Staudenmeir\EloquentHasManyDeep\HasManyDeep; use Staudenmeir\EloquentHasManyDeep\HasRelationships; class Container extends AbstractModel implements Transportable @@ -26,27 +26,27 @@ class Container extends AbstractModel implements Transportable } /** - * @return HasManyThrough + * @return hasManyDeep */ - public function packages(): hasManyThrough + public function packages(): hasManyDeep { - return $this->hasManyDeep(Package::class, [ContainerPackingList::class, PackingList::class], ['container_id', 'id', 'packing_list_id'], ['id', 'packing_list_id', 'id']); + return $this->hasManyDeepFromRelations($this->packingLists(), (new PackingList())->packages()); } /** - * @return HasManyThrough + * @return hasManyDeep */ - public function companyModules(): hasManyThrough + public function companyModules(): hasManyDeep { - return $this->hasManyDeep(CompanyModule::class, [ContainerPackingList::class, PackingList::class, Order::class], ['container_id', 'id', 'id', 'id'], ['id', 'packing_list_id', null, 'company_module_id']); + return $this->hasManyDeep(CompanyModule::class, [ContainerPackingList::class, PackingList::class, Order::class], ['container_id', 'id', 'id', 'id'], ['id', 'packing_list_id', 'owner_id', 'company_module_id']); } /** - * @return HasManyThrough + * @return hasManyDeep */ - public function orders(): hasManyThrough + public function orders(): hasManyDeep { - return $this->hasManyDeep(Order::class, [ContainerPackingList::class, PackingList::class], ['container_id', 'id', 'id'], ['id', 'packing_list_id', null]); + return $this->hasManyDeep(Order::class, [ContainerPackingList::class, PackingList::class], ['container_id', 'id', 'id'], ['id', 'packing_list_id', 'owner_id']); } /**