diff --git a/app/Http/Controllers/Reports/MonthlyReportController.php b/app/Http/Controllers/Reports/MonthlyReportController.php index 96836f97..b38edcf0 100644 --- a/app/Http/Controllers/Reports/MonthlyReportController.php +++ b/app/Http/Controllers/Reports/MonthlyReportController.php @@ -2,9 +2,12 @@ namespace App\Http\Controllers\Reports; +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; use Carbon\Carbon; @@ -14,53 +17,40 @@ use Illuminate\Http\Request; class MonthlyReportController { public function report(Request $request): JsonResponse { - $month = Carbon::now()->subMonth(); - $containers = Container::whereMonth('loading_date', $month->format('m'))->whereYear('loading_date', '=', '2021')->orderBy('loading_date')->get(); + $today = Carbon::now()->subMonth(); + $containers = Container::whereMonth('loading_date', $today->format('m'))->whereYear('loading_date', $today->format('Y'))->orderBy('loading_date')->get(); - $total = 0; - $customers = collect(); - foreach ($containers as $container){ - foreach($container->packingLists as $packingList){ - $cbm = $packingList->packages->sum(function ($package){ - return (( (float) $package->width / 100) * ( (float) $package->length / 100) * ( (float) $package->height / 100)) * $package->quantity; - }); - - $total += $cbm; - $marking = $packingList->owner->companyModule->inviters()->withPivot('invitee_reference')->first()->pivot->invitee_reference; - if(!$customers->has($marking)){ - $customers->put($marking, collect([ - 'cbm' => $cbm, - 'orders' => 1, - 'packages' => $packingList->packages->sum('quantity') - ])); - - continue; - } - - $customers[$marking]['cbm'] += $cbm; - $customers[$marking]['orders'] += 1; - $customers[$marking]['packages'] += $packingList->packages->sum('quantity'); - } - } - - $activeOrders = $customers->sum(function($customer){ - return $customer['orders']; + $packages = $containers->flatMap(function ($container) { + return $container->packages; }); + + $totalCbm = $packages->sum(function($package){ + return (( (float) $package->width / 100) * ( (float) $package->length / 100) * ( (float) $package->height / 100)) * $package->quantity; + }); + + $customers = $containers->flatMap(function($container){ + return $container->companyModules; + })->unique(); + + $orders = $containers->flatMap(function($container){ + return $container->orders; + })->unique(); + return (new ApiResponseObject('fetch monthly report Successful', '', HttpStatus::OK_WITH_MESSAGE, ['data' => [ 'containers' => count($containers), - 'activated_orders' => $activeOrders, + 'activated_orders' => count($orders), 'active_customers' => count($customers), - 'new_customers' => Company::whereMonth('created_at', $month->format('m'))->count(), - 'total_cbm' => $customers->sum(function($customer){ - return $customer['cbm']; - }), - 'packages' => $customers->sum(function($customer){ - return $customer['packages']; - }), - 'total_orders' => Order::whereMonth('created_at', $month->format('m'))->count() + 'new_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->whereMonth('created_at', $today->format('m'))->whereYear('created_at', $today->format('Y'))->count(), + 'new_converted_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->whereMonth('created_at', $today->format('m'))->whereYear('created_at', $today->format('Y'))->whereHas('orders', function($order){ + return $order->whereHas('packingLists'); + })->count(), + 'total_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->count(), + 'total_cbm' => $totalCbm, + 'packages' => $packages->sum('quantity'), + 'total_orders' => Order::whereMonth('created_at', $today->format('m'))->whereYear('created_at', $today->format('Y'))->count() ]]))->handler(); } } diff --git a/app/Models/Container.php b/app/Models/Container.php index a103790b..158d0845 100644 --- a/app/Models/Container.php +++ b/app/Models/Container.php @@ -38,7 +38,15 @@ class Container extends AbstractModel implements Transportable */ public function companyModules(): hasManyThrough { - return $this->hasManyDeep(CompanyModule::class, [ContainerPackingList::class, PackingList::class, Order::class], ['container_id', 'id', 'company_module'], ['id', 'packing_list_id', 'id']); + 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 HasManyThrough + */ + public function orders(): hasManyThrough + { + return $this->hasManyDeep(Order::class, [ContainerPackingList::class, PackingList::class], ['container_id', 'id', 'id'], ['id', 'packing_list_id', null]); } /** diff --git a/resources/assets/vue/components/reports/sections/MonthlyReportSectionComponent.vue b/resources/assets/vue/components/reports/sections/MonthlyReportSectionComponent.vue index 6af42e93..48e966d8 100644 --- a/resources/assets/vue/components/reports/sections/MonthlyReportSectionComponent.vue +++ b/resources/assets/vue/components/reports/sections/MonthlyReportSectionComponent.vue @@ -81,15 +81,15 @@

{{report.active_customers}}

- {{ Math.ceil((((report.new_customers / report.active_customers) * 100) * 100) / 100).toFixed(2)}}% - New Customers: {{report.new_customers}} + {{ Math.ceil((((report.active_customers / report.total_customers) * 100) * 100) / 100).toFixed(2)}}% + Total Customers: {{report.total_customers}}
-
+
@@ -97,6 +97,37 @@
+
+
+
+
+
+
+ New Customers +
+
+
+
+
+
+
+

{{report.new_customers}}

+ {{ Math.ceil((((report.new_converted_customers / report.new_customers) * 100) * 100) / 100).toFixed(2)}}% + Placed Order: {{report.new_converted_customers}} +
+
+
+
+
+
+
+
+
+
+
+
+
+