From 92a38a96a03f55fab59dc2261caefb110bbfc421 Mon Sep 17 00:00:00 2001 From: Martin Kiragu Date: Fri, 26 May 2023 19:02:46 +0300 Subject: [PATCH] updates on the customers endpoint --- .../ControllersLogic/ListCompaniesLogic.php | 23 ++++++--- .../Reports/MonthlyReportController.php | 51 ++++++++++++++++++- app/Library/helper.php | 13 +++++ app/Models/Company.php | 24 ++++++++- app/Transformers/CompanyTransformer.php | 10 ++-- app/Transformers/DocumentTransformer.php | 13 ++++- .../views/pages/admin/customers.blade.php | 23 +++++---- 7 files changed, 131 insertions(+), 26 deletions(-) diff --git a/app/Classes/Modules/Companies/ControllersLogic/ListCompaniesLogic.php b/app/Classes/Modules/Companies/ControllersLogic/ListCompaniesLogic.php index 80b15c1c..dd0e497b 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/ListCompaniesLogic.php +++ b/app/Classes/Modules/Companies/ControllersLogic/ListCompaniesLogic.php @@ -3,13 +3,16 @@ namespace App\Classes\Modules\Companies\ControllersLogic; +use App\Classes\Exceptions\AccessForbiddenException; +use App\Classes\Exceptions\MalformedRequestException; +use App\Classes\Exceptions\RequestValidationException; use App\Classes\General\Abstracts\AbstractControllerLogic; use App\Classes\Modules\Companies\Services\ListsCompanies; use App\Classes\Modules\Companies\Standards\Rules\CanListCompanies; -use App\Http\Resources\CompanyResource; -use ErrorException; +use App\Transformers\CompanyTransformer; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Spatie\Fractalistic\ArraySerializer; class ListCompaniesLogic extends AbstractControllerLogic { @@ -45,16 +48,24 @@ class ListCompaniesLogic extends AbstractControllerLogic /** * @param Request $request * @return JsonResponse - * @throws \App\Classes\Exceptions\AccessForbiddenException - * @throws \App\Classes\Exceptions\MalformedRequestException - * @throws \App\Classes\Exceptions\RequestValidationException + * @throws AccessForbiddenException + * @throws MalformedRequestException + * @throws RequestValidationException */ public function logic(Request $request) : JsonResponse { $this->canListCompanies->passes(); $query = $this->listsCompanies->execute($this->listsCompanies->deserializeFilters($request->input('filters'))); - return $this->collectionResponse(CompanyResource::collection($query)); + // return $this->collectionResponse(CompanyResource::collection($query)); + + return fractal() + ->parseIncludes(['owner']) + ->collection($query, new CompanyTransformer(), "data") + ->serializeWith(new ArraySerializer()) + ->respond(200, [], JSON_PRETTY_PRINT); + + } diff --git a/app/Http/Controllers/Reports/MonthlyReportController.php b/app/Http/Controllers/Reports/MonthlyReportController.php index 1787ec97..531f97e7 100644 --- a/app/Http/Controllers/Reports/MonthlyReportController.php +++ b/app/Http/Controllers/Reports/MonthlyReportController.php @@ -307,7 +307,7 @@ class MonthlyReportController return $query->where('packing_lists.type', PackingListType::WAREHOUSE_RECEIVE_LIST)->whereHas('transports', function ($query) use ($active_start, $active_end) { return $query->whereDate('drop_date', '>=', Carbon::parse($active_start))->whereDate('drop_date', '<=', Carbon::parse($active_end)->addDay()); }); - })->whereNotIn('id', $activeCompanies)->get();*/ + })->whereNotIn('id', $activeCompanies)->get(); $companies = getActiveCompanies($inactive_start, $inactive_end, $active_start, $active_end); @@ -317,7 +317,7 @@ class MonthlyReportController foreach ($companies as $key => $company) { $connection = $company->inviters()->withPivot('invitee_reference')->first(); $marking = $connection ? $connection->pivot->invitee_reference : ''; - $packingList = $company->orderPackingLists()->where('packing_lists.type', \App\Classes\ValueObjects\Constants\PackingListType::SHIPPING_PACKING_LIST)->get(); + $packingList = $company->orderPackingLists()->where('packing_lists.type', PackingListType::SHIPPING_PACKING_LIST)->get(); $totalCbm = $packingList->flatMap(function ($packingList) { return $packingList->packages; })->sum(function ($package) { @@ -341,6 +341,53 @@ class MonthlyReportController $counter++; } + */ + + $companies = getActiveCompanies($inactive_start, $inactive_end, $active_start, $active_end); + + $customerActivityData = []; + + $packingListType = PackingListType::SHIPPING_PACKING_LIST; + + $companies->load([ + 'inviters' => function ($query) { + $query->withPivot('invitee_reference')->first(); + }, + 'orderPackingLists' => function ($query) use ($packingListType) { + $query->where('packing_lists.type', $packingListType)->with('packages'); + }, + ]); + + $counter = 1; + + foreach ($companies as $company) { + $connection = $company->inviters->first(); + $marking = $connection ? $connection->pivot->invitee_reference : ''; + $packingList = $company->orderPackingLists; + + $totalCbm = $packingList->flatMap->packages->sum(function ($package) { + return (((float)$package->width / 100) * ((float)$package->length / 100) * ((float)$package->height / 100)) * $package->quantity; + }); + + if ($minCbm !== null && $totalCbm < $minCbm) { + continue; + } + + if ($minOrders !== null && $packingList->count() < $minOrders) { + continue; + } + + $customerActivityData[] = [ + 'key' => $counter, + 'marking' => $marking, + 'packingListCount' => $packingList->count(), + 'totalCbm' => $totalCbm, + ]; + + $counter++; + } + + return (new ApiResponseObject('fetch service report Successful', '', HttpStatus::OK_WITH_MESSAGE, ['data' => [ diff --git a/app/Library/helper.php b/app/Library/helper.php index 0cb295cc..5cdf19fd 100644 --- a/app/Library/helper.php +++ b/app/Library/helper.php @@ -69,3 +69,16 @@ if (!function_exists('format_number')) { } } + /** + * check if a customer have confirmed orders + * @param Company $company + * @return bool + */ +if (!function_exists('checkIfHasConfirmedOrders')) +{ + function checkIfHasConfirmedOrders(Company $company): bool + { + return $company->hasConfirmedOrder(); + } +} + diff --git a/app/Models/Company.php b/app/Models/Company.php index bf5dabd0..4b9254c3 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -111,12 +111,32 @@ class Company extends AbstractModel implements Documentable, Contactable $orders = []; $this->CompanyModules->each(function ($companyModule) { - $orders[] = $companyModule->orders->sortByDesc('id'); - }); + $orders[] = $companyModule->orders->sortByDesc('id'); + }); return collect($orders); } + /** + * Has Confirmed orders + * @return bool + */ + public function hasConfirmedOrder(): bool + { + $importerModule = $this->companyModules()->where('type', BusinessType::IMPORTER)->first(); + + if (!$importerModule) { + return false; // Return false if no relation found + } + + $hasConfirmedOrder = $importerModule->orders() + ->where('status', '!=', 0) + ->exists(); + + return (bool)$hasConfirmedOrder; + + } + /** * @return MorphMany diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index c65af2ab..a955cebd 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -16,8 +16,6 @@ class CompanyTransformer extends TransformerAbstract * List of all default includes */ protected array $defaultIncludes = [ - 'contact', - 'employee', 'company_module' ]; @@ -29,13 +27,13 @@ class CompanyTransformer extends TransformerAbstract 'segments', 'last_order', - 'order_count', 'identification', ]; public function transform(Company $company): array { - + $companyModule = $company->companyModules()->first(); + $hasConfirmedOrders = checkIfHasConfirmedOrders($company); return [ 'id' => $company->id, 'hash_id' => Crypt::encryptString($company->id), @@ -45,7 +43,9 @@ class CompanyTransformer extends TransformerAbstract 'type' => $company->type, 'business_type' => (int)$company->business_type, 'status' => $company->status, - 'created_at' => $company->created_at->format('d-m-Y') + 'created_at' => $company->created_at->format('d-m-Y'), + 'order_count' => $companyModule->orders()->count(), + 'has_confirmed_order' => $hasConfirmedOrders, ]; } diff --git a/app/Transformers/DocumentTransformer.php b/app/Transformers/DocumentTransformer.php index c89b335a..c805e721 100644 --- a/app/Transformers/DocumentTransformer.php +++ b/app/Transformers/DocumentTransformer.php @@ -4,8 +4,10 @@ namespace App\Transformers; use App\Models\Document; use Carbon\Carbon; +use League\Fractal\Manager; use League\Fractal\Resource\Collection; use League\Fractal\Resource\Item; +use League\Fractal\Serializer\ArraySerializer; use League\Fractal\TransformerAbstract; class DocumentTransformer extends TransformerAbstract @@ -43,10 +45,19 @@ class DocumentTransformer extends TransformerAbstract return $this->collection($files, new FileTransformer(true)); } + /** + * include owner data + * @param Document $document + * @return Item + */ public function includeOwner(Document $document): Item { $owner = $document->owner; - return $this->item($owner, new CompanyTransformer()); + + $transformer = new CompanyTransformer(); + $transformer->setDefaultIncludes([ 'contact', 'employee', 'company_module']); + + return $this->item($owner, $transformer); } } diff --git a/resources/views/pages/admin/customers.blade.php b/resources/views/pages/admin/customers.blade.php index d11f50dc..9641d323 100644 --- a/resources/views/pages/admin/customers.blade.php +++ b/resources/views/pages/admin/customers.blade.php @@ -16,7 +16,9 @@
- + @@ -30,15 +32,16 @@
Customers Without Confirmed Orders
-
-
- - - -
-
+ +