add report from to filters

This commit is contained in:
omair saleh
2021-10-12 12:25:26 +08:00
parent 25f9db99c7
commit 04215916f5
2 changed files with 25 additions and 28 deletions
@@ -5,9 +5,7 @@ namespace App\Http\Controllers\Reports;
use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\BusinessType; use App\Classes\ValueObjects\Constants\BusinessType;
use App\Classes\ValueObjects\Constants\HttpStatus; use App\Classes\ValueObjects\Constants\HttpStatus;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Response\ApiResponseObject; use App\Classes\ValueObjects\Response\ApiResponseObject;
use App\Models\Company;
use App\Models\CompanyModule; use App\Models\CompanyModule;
use App\Models\Container; use App\Models\Container;
use App\Models\Order; use App\Models\Order;
@@ -19,13 +17,19 @@ use Illuminate\Http\Request;
class MonthlyReportController class MonthlyReportController
{ {
public function salesReport(Request $request): JsonResponse { public function salesReport(Request $request): JsonResponse {
$today = Carbon::now()->subMonth();
$start_day = 20; $from = $request->input('from') ? Carbon::parse($request->input('from')) : Carbon::now()->startOfMonth();
$end_day = 30; $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) { $packages = $containers->flatMap(function ($container) {
return $container->packages; return $container->packages;
@@ -35,19 +39,12 @@ class MonthlyReportController
return (( (float) $package->width / 100) * ( (float) $package->length / 100) * ( (float) $package->height / 100)) * $package->quantity; 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) { $days = Carbon::parse('20-09-2021')->addDay()->diffInDaysFiltered(function (Carbon $date) {
return $package->order;
});
$days = Carbon::now()->addDay()->diffInDaysFiltered(function (Carbon $date) {
return $date->isWeekday(); return $date->isWeekday();
}, Carbon::now()->endOfMonth()); }, Carbon::parse('20-09-2021')->endOfMonth());
return (new ApiResponseObject('fetch monthly report Successful', return (new ApiResponseObject('fetch monthly report Successful',
'', '',
@@ -55,15 +52,15 @@ class MonthlyReportController
'containers' => count($containers), 'containers' => count($containers),
'activated_orders' => count($orders), 'activated_orders' => count($orders),
'active_customers' => count($customers), '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_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->whereBetween('created_at', [$from, $to])->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_converted_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->whereBetween('created_at', [$from, $to])->whereHas('orders', function($order){
return $order->whereHas('packingLists'); return $order->whereHas('packingLists');
})->count(), })->count(),
'total_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->count(), 'total_customers' => CompanyModule::where('type', BusinessType::IMPORTER)->count(),
'total_cbm' => $totalCbm, 'total_cbm' => $totalCbm,
'daily_cbm' => (800 - $totalCbm) / $days, 'daily_cbm' => (800 - $totalCbm) / $days,
'packages' => $packages->sum('quantity'), '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(); ]]))->handler();
} }
+10 -10
View File
@@ -4,9 +4,9 @@ namespace App\Models;
use App\Classes\General\Interfaces\Transportable; use App\Classes\General\Interfaces\Transportable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphMany;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships; use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class Container extends AbstractModel implements Transportable 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']);
} }
/** /**