Files
shipping-portal/app/Http/Controllers/Reports/MonthlyReportController.php
T
2021-10-11 14:37:13 +08:00

95 lines
5.0 KiB
PHP

<?php
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;
use App\Models\Package;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class MonthlyReportController
{
public function salesReport(Request $request): JsonResponse {
$today = Carbon::now()->subMonth();
$start_day = 6;
$end_day = 12;
$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();
$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();
$days = Carbon::now()->addDay()->diffInDaysFiltered(function (Carbon $date) {
return $date->isWeekday();
}, Carbon::now()->endOfMonth());
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)->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){
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()
]]))->handler();
}
public function serviceReport(Request $request): JsonResponse {
return (new ApiResponseObject('fetch service report Successful',
'',
HttpStatus::OK_WITH_MESSAGE, ['data' => [
'received' => (float) Package::warehouseReceiveList()->sum('quantity'),
'pending_shipment' => (float) Package::shippingList()->whereDoesntHave('shippingSchedules', function($schedule){
return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
})->sum('quantity'),
'in_transit' => (float) Package::shippingList()->Shipping()->whereHas('shippingSchedules', function($schedule){
return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
})->whereDoesntHave('container', function($container){
return $container->where('containers.status', ApprovalStatus::COMPLETED);
})->sum('quantity'),
'pending_delivery' => (float) Package::shippingList()->whereHas('container', function($container){
return $container->where('containers.status', ApprovalStatus::COMPLETED);
})->whereDoesntHave('deliverySchedules', function($schedule){
return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
})->sum('quantity'),
'delivered' => (float) Package::shippingList()->whereHas('deliverySchedules', function($schedule){
return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
})->sum('quantity'),
]]))->handler();
}
}