From 475846ab652a074374e1c368936d9245c636e96c Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Fri, 30 Sep 2022 04:29:27 +0800 Subject: [PATCH] conditionally load wallet in company resource --- ...eatePurchaseOrderFor1688OrderProcessor.php | 19 +++++++++++++++++-- app/Http/Resources/CompanyResource.php | 2 +- app/Http/Resources/WalletResource.php | 4 ++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/Classes/Modules/Bookings/Processors/CreatePurchaseOrderFor1688OrderProcessor.php b/app/Classes/Modules/Bookings/Processors/CreatePurchaseOrderFor1688OrderProcessor.php index d089d7c7..80c406e6 100644 --- a/app/Classes/Modules/Bookings/Processors/CreatePurchaseOrderFor1688OrderProcessor.php +++ b/app/Classes/Modules/Bookings/Processors/CreatePurchaseOrderFor1688OrderProcessor.php @@ -27,16 +27,26 @@ class CreatePurchaseOrderFor1688OrderProcessor /** @var Convert1688PurchaseOrderToProductList */ private $convert1688PurchaseOrderToProductList; + /** @var UpdatesTransactionStatus */ + private $updatesTransactionStatus; + + /** @var CreateInvoiceTransactionProcessor */ + private $createInvoiceTransactionProcessor; + /** * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber * @param CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor * @param Convert1688PurchaseOrderToProductList $convert1688PurchaseOrderToProductList + * @param UpdatesTransactionStatus $updatesTransactionStatus + * @param CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor */ - public function __construct(GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor, Convert1688PurchaseOrderToProductList $convert1688PurchaseOrderToProductList) + public function __construct(GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor, Convert1688PurchaseOrderToProductList $convert1688PurchaseOrderToProductList, UpdatesTransactionStatus $updatesTransactionStatus, CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor) { $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createPurchaseOrderTransactionProcessor = $createPurchaseOrderTransactionProcessor; $this->convert1688PurchaseOrderToProductList = $convert1688PurchaseOrderToProductList; + $this->updatesTransactionStatus = $updatesTransactionStatus; + $this->createInvoiceTransactionProcessor = $createInvoiceTransactionProcessor; } @@ -63,7 +73,12 @@ class CreatePurchaseOrderFor1688OrderProcessor $total, $total, $booking->fix_currency_id, $booking->fix_currency_id, 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, $products); - $this->createPurchaseOrderTransactionProcessor->execute($booking, $object); + $purchaseOrder = $this->createPurchaseOrderTransactionProcessor->execute($booking, $object); + + if($purchaseOrder->amount === $booking->fix_amount){ + $this->updatesTransactionStatus->execute($purchaseOrder, ApprovalStatus::APPROVED); + $this->createInvoiceTransactionProcessor->execute($booking); + } } diff --git a/app/Http/Resources/CompanyResource.php b/app/Http/Resources/CompanyResource.php index 7070b87f..cb357684 100644 --- a/app/Http/Resources/CompanyResource.php +++ b/app/Http/Resources/CompanyResource.php @@ -61,7 +61,7 @@ class CompanyResource extends JsonResource ], 'segments' => SegmentResource::collection($this->segments), 'services' => (new FetchesCompanyServices())->getServices($this->servicesConfigurations()), - 'wallet' => $this->whenLoaded('wallets', new WalletResource($this->wallets()->first()), ''), + 'wallet' => $this->whenLoaded('wallets', new WalletResource($this->wallets()->with('transactions')->first()), new WalletResource($this->wallets()->first())), 'created_at' => $this->created_at->format('d-m-Y'), $this->mergeWhen($this->business_type === BusinessType::CURRENCY_VENDOR, [ 'currencies' => $segment ? CurrencyResource::collection(Currency::whereIn('id', $segment->detail->currencies)->get()) : [], diff --git a/app/Http/Resources/WalletResource.php b/app/Http/Resources/WalletResource.php index 143af1c4..4fa484ad 100644 --- a/app/Http/Resources/WalletResource.php +++ b/app/Http/Resources/WalletResource.php @@ -21,8 +21,8 @@ class WalletResource extends JsonResource 'currency_id' => $this->currency_id, 'amount' => (double) $this->amount, 'company_id' => (int) $this->owner->id, - 'transactions' => WalletTransactionResource::collection($this->transactions()->whereIn('status', [2, 3])->orderBy('id', 'DESC')->get()), - 'top_up_records' => WalletTransactionResource::collection($this->transactions()->whereNotIn('status', [0])->where('type', TransactionType::TOP_UP)->orderBy('id', 'DESC')->get()) + 'transactions' => $this->whenLoaded('transactions', WalletTransactionResource::collection($this->transactions()->whereIn('status', [2, 3])->orderBy('id', 'DESC')->get()), []), + 'top_up_records' => $this->whenLoaded('transactions', WalletTransactionResource::collection($this->transactions()->whereNotIn('status', [0])->where('type', TransactionType::TOP_UP)->orderBy('id', 'DESC')->get()), []), ]; } }