update monthly report

This commit is contained in:
omair saleh
2021-10-05 14:24:43 +08:00
parent ebbe2c7863
commit 9340d4360e
3 changed files with 72 additions and 43 deletions
@@ -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();
}
}
+9 -1
View File
@@ -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]);
}
/**
@@ -81,15 +81,15 @@
<div class="col-xs-height col-top">
<div class="p-l-20 p-t-50 p-b-40 p-r-20">
<h3 class="no-margin p-b-5">{{report.active_customers}}</h3>
<span class="small hint-text pull-left">{{ Math.ceil((((report.new_customers / report.active_customers) * 100) * 100) / 100).toFixed(2)}}%</span>
<span class="pull-right small text-primary">New Customers: {{report.new_customers}}</span>
<span class="small hint-text pull-left">{{ Math.ceil((((report.active_customers / report.total_customers) * 100) * 100) / 100).toFixed(2)}}%</span>
<span class="pull-right small text-primary">Total Customers: {{report.total_customers}}</span>
</div>
</div>
</div>
<div class="row-xs-height">
<div class="col-xs-height col-bottom">
<div class="progress progress-small m-b-0">
<div class="progress-bar progress-bar-primary" :style="{'width': Math.ceil((((report.new_customers / report.active_customers) * 100) * 100) / 100).toFixed(2)+'%'}"></div>
<div class="progress-bar progress-bar-primary" :style="{'width': Math.ceil((((report.active_customers / report.total_customers) * 100) * 100) / 100).toFixed(2)+'%'}"></div>
</div>
</div>
</div>
@@ -97,6 +97,37 @@
</div>
</div>
<div class="col">
<div class="card no-border bg-master-lightest widget-loader-bar m-b-10">
<div class="container-xs-height full-height">
<div class="row-xs-height">
<div class="col-xs-height col-top">
<div class="card-header top-left top-right">
<div class="card-title">
<span class="font-montserrat fs-11 all-caps">New Customers</span>
</div>
</div>
</div>
</div>
<div class="row-xs-height">
<div class="col-xs-height col-top">
<div class="p-l-20 p-t-50 p-b-40 p-r-20">
<h3 class="no-margin p-b-5">{{report.new_customers}}</h3>
<span class="small hint-text pull-left">{{ Math.ceil((((report.new_converted_customers / report.new_customers) * 100) * 100) / 100).toFixed(2)}}%</span>
<span class="pull-right small text-primary">Placed Order: {{report.new_converted_customers}}</span>
</div>
</div>
</div>
<div class="row-xs-height">
<div class="col-xs-height col-bottom">
<div class="progress progress-small m-b-0">
<div class="progress-bar progress-bar-primary" :style="{'width': Math.ceil((((report.new_converted_customers / report.new_customers) * 100) * 100) / 100).toFixed(2)+'%'}"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col hide">
<div class="card no-border bg-master-lightest widget-loader-bar m-b-10">
<div class="container-xs-height full-height">
<div class="row-xs-height">