subMonth(); $containers = Container::whereMonth('loading_date', $today->format('m'))->whereYear('loading_date', $today->format('Y'))->orderBy('loading_date')->get(); $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' => count($orders), 'active_customers' => count($customers), '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(); } public function serviceReport(Request $request): JsonResponse { return (new ApiResponseObject('fetch service report Successful', '', HttpStatus::OK_WITH_MESSAGE, ['data' => [ 'received' => Package::whereHas('packingList', function($query){ return $query->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST); })->sum('quantity'), 'pending_shipment' => Package::whereDoesntHave('shippingSchedules', function($schedule){ return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED); })->sum('quantity'), 'in_transit' => Package::whereHas('shippingSchedules', function($schedule){ return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED); })->whereDoesntHave('shippingTransports', function($transport){ return $transport->where('transport_arrangements.status', ApprovalStatus::COMPLETED); })->sum('quantity'), 'pending_delivery' => Package::whereHas('shippingTransports', function($transport){ return $transport->where('transport_arrangements.status', ApprovalStatus::COMPLETED); })->whereDoesntHave('deliverySchedules', function($schedule){ return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED); })->sum('quantity'), 'delivered' => Package::whereHas('shippingSchedules', function($schedule){ return $schedule->where('eta', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED); })->whereHas('deliverySchedules', function($schedule){ return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED); })->sum('quantity'), ]]))->handler(); } }