From d8b41cbe55da118e2c7c17a2a28566a19478ac35 Mon Sep 17 00:00:00 2001 From: omair saleh Date: Tue, 20 Sep 2022 06:43:54 +0800 Subject: [PATCH] billing fix up --- .../Billplzs/Services/CreatesBillplzBill.php | 4 +- .../CreatePaymentTransactionLogic.php | 2 +- .../CreateShippingInvoiceTransactionLogic.php | 65 +++++++++++++++++-- .../UpdateShippingInvoiceTransactionLogic.php | 10 ++- .../Constants/TransactionDetailType.php | 2 + app/Models/Transaction.php | 3 + .../elements/CustomerProfileComponent.vue | 2 +- .../forms/DetachSegmentFormComponent.vue | 8 ++- .../forms/InvoiceItemFormComponent.vue | 4 +- 9 files changed, 82 insertions(+), 18 deletions(-) diff --git a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php index 5b331fca..1dad646c 100644 --- a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php +++ b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php @@ -23,7 +23,7 @@ class CreatesBillplzBill try{ // config('billplz.maybank') $response = Http::withBasicAuth(config('billplz.api_key').':', '')->post(config('billplz.base_url').'/api/v3/bills', [ - 'collection_id' => $wallet ? config('billplz.wallet_collection_id') : config('billplz.collection_id'), + 'collection_id' => config('billplz.collection_id'), 'name' => $name, 'email' => $email, 'description' => $description, @@ -35,7 +35,7 @@ class CreatesBillplzBill 'reference_2_label' => 'Bill Number', 'reference_2' => $billNumber ]); - +dd($response); if($response->successful()){ $data = $response->json(); diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreatePaymentTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreatePaymentTransactionLogic.php index 9535c04f..0d3b3a98 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreatePaymentTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreatePaymentTransactionLogic.php @@ -90,7 +90,7 @@ class CreatePaymentTransactionLogic extends AbstractControllerLogic $request->input('bank_code'), true ); - +dd($billPlzBill); $payment_reference = $billPlzBill->id; } else { diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateShippingInvoiceTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateShippingInvoiceTransactionLogic.php index a8274aea..4cd52eb6 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateShippingInvoiceTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateShippingInvoiceTransactionLogic.php @@ -16,6 +16,7 @@ use App\Classes\Modules\Transactions\DataTransferObjects\TransactionDetailObject use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; use App\Classes\ValueObjects\Constants\OrderRoleTypes; +use App\Classes\ValueObjects\Constants\PackingListType; use App\Classes\ValueObjects\Constants\TransactionType; use App\Classes\ValueObjects\Constants\PaymentMethodType; use App\Classes\ValueObjects\Constants\ApprovalStatus; @@ -92,6 +93,7 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic public function logic(Request $request) : JsonResponse { $packing_list = $this->fetchesPackingList->execute(['id' => $request->input('packing_list_id')]); + $packing_list = PackingList::where('reference', $packing_list->reference)->where('type', PackingListType::SHIPPING_PACKING_LIST)->first(); $cbm = $packing_list->packages->where('type', '!=', PackageType::OVER_WEIGHT)->sum(function($package) { return ($package->width / 100) * ($package->height / 100) *($package->length / 100) * ($package->quantity); @@ -102,6 +104,8 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic }); $order = $packing_list->owner; + $companyModule = $order->companyModule; + $connection = $companyModule->connections()->first(); $address = $order->addresses()->first(); $base_price_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::BASE_PRICE]); @@ -110,13 +114,22 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic $center_postcode_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::CENTER_POSTCODE]); $outstation_postcode_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::OUTSTATION_POSTCODE]); + $noMinimumCharge = $connection->segments()->where('segments.id', 4)->first(); + $base_price = 0; $warehouse_rate = 0; $state_rate = 0; + $minimum_cbm = 0.3; + $segment_price = 0; $state_select = ''; + $segment_price = $connection->segments()->whereHas('constants')->get()->map(function($segment){ + return (float) $segment->constants()->first()->value[0]; + })->sort()->first(); + $segment_price = $segment_price ? $segment_price : 0; + $packing_list_drop_date = Carbon::parse(PackingList::where('reference', $packing_list->reference)->where('type', 1)->first()->transports->first()->drop_date)->format('d/m/Y'); - $base_price = $this->getConstantByKey($base_price_constant, $packing_list_drop_date); + $base_price = $this->getConstantByKey($base_price_constant, Carbon::parse($packing_list_drop_date)->format('Y-m-d')); $warehouseId = $order->orderRoles()->where('role_id', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->company_module_id; $selected_warehouse_rate = $this->getConstantByKey($warehouse_rate_constant, $warehouseId); @@ -132,8 +145,40 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic $state_rate_constant = (array)$state_rate_constant; $state_rate = $state_select == '' ? 0 : $state_rate_constant[$state_select]; - $price_cbm = $base_price + $warehouse_rate + $state_rate; - $total_cbm = $price_cbm * ($cbm + $over_weight_cbm); + $container = $packing_list->containers()->first(); + $containerPackingLists = collect(); + $hasMinimumCharge = null; + + foreach ($container->packingLists as $packingList){ + if($packingList->owner->companyModule->id !== $companyModule->id) continue; + $containerPackingLists->push($packingList); + + if(!$hasMinimumCharge) { + $hasMinimumCharge = $packingList->transactions()->whereHas('transactionDetails', function ($query){ + $query->where('reference', 'MIN_CBM_CHARGES'); + })->first(); + } + + } + + $totalContainerCbm = $containerPackingLists->sum(function($packingList){ + return $packingList->packages->where('type', '!=', PackageType::OVER_WEIGHT)->sum(function($package) { + return ($package->width / 100) * ($package->height / 100) *($package->length / 100) * ($package->quantity); + }); + }); + + $minimum_charge = round($minimum_cbm - $totalContainerCbm, '2'); + $minimum_charge = $minimum_charge < 0 ? 0 : $minimum_charge; + $minimum_charge = $noMinimumCharge ? 0 : $minimum_charge; + + + if($hasMinimumCharge) { + $minimum_charge = 0; + } + + $price_cbm = $base_price + $segment_price + $warehouse_rate + $state_rate; + + $total_cbm = $price_cbm * ($cbm + $minimum_charge + $over_weight_cbm); $billNumber = $this->generatesTransactionBillNumber->execute('SHIP-'); @@ -166,7 +211,7 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic ); $this->createsTransactionDetail->execute($invoice_transaction, $object_detail); - + if ($over_weight_cbm > 0) { $object_detail = new TransactionDetailObject( 'OVER_WEIGHT_CHARGES', @@ -178,6 +223,17 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic $this->createsTransactionDetail->execute($invoice_transaction, $object_detail); } + if ($minimum_charge > 0) { + $object_detail = new TransactionDetailObject( + 'MIN_CBM_CHARGES', + TransactionDetailType::MIN_CBM_CHARGES, + $minimum_charge, + $price_cbm + ); + + $this->createsTransactionDetail->execute($invoice_transaction, $object_detail); + } + return $this->response([]); } @@ -185,6 +241,7 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic if ($segmentConstantObject) { $base_rate = (array) $segmentConstantObject->value; $base_rate = array_key_exists($key, $base_rate) === true ? $base_rate[$key] : 0; + return $base_rate; } } diff --git a/app/Classes/Modules/Transactions/ControllersLogic/UpdateShippingInvoiceTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/UpdateShippingInvoiceTransactionLogic.php index f60130ac..a9bc8f99 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/UpdateShippingInvoiceTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/UpdateShippingInvoiceTransactionLogic.php @@ -85,7 +85,7 @@ class UpdateShippingInvoiceTransactionLogic extends AbstractControllerLogic { $invoice_transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]); - $old_transaction_details = $invoice_transaction->transactionDetails->whereNotIn('reference', ['SHIPPING_FEE', 'OVER_WEIGHT_CHARGES'])->pluck('id')->toArray(); + $old_transaction_details = $invoice_transaction->transactionDetails->whereNotIn('reference', ['SHIPPING_FEE', 'OVER_WEIGHT_CHARGES', 'MIN_CBM_CHARGES'])->pluck('id')->toArray(); $new_transaction_details = collect($request->input('transaction_details')); @@ -104,7 +104,7 @@ class UpdateShippingInvoiceTransactionLogic extends AbstractControllerLogic foreach($new_transaction_details as $transaction_detail){ $transaction_detail = (object) $transaction_detail; - if(!in_array($transaction_detail->reference, ['SHIPPING_FEE', 'OVER_WHEIGHT_CHARGES'])){ + if(!in_array($transaction_detail->reference, ['SHIPPING_FEE', 'OVER_WEIGHT_CHARGES', 'MIN_CBM_CHARGES'])){ $object_detail = new TransactionDetailObject( 'CUSTOM_CHARGES', isset($transaction_detail->name) ? $transaction_detail->name : TransactionDetailType::CUSTOM_CHARGES, @@ -117,8 +117,6 @@ class UpdateShippingInvoiceTransactionLogic extends AbstractControllerLogic }else{ $new_transaction_detail = $this->createsTransactionDetail->execute($invoice_transaction, $object_detail); } - - $total_cbm += $new_transaction_detail->amount; } } @@ -129,8 +127,8 @@ class UpdateShippingInvoiceTransactionLogic extends AbstractControllerLogic $invoice_transaction->issuer, 1, PaymentMethodType::CASH, - $total_cbm, - $total_cbm, + $invoice_transaction->transactionDetails()->sum('amount'), + $invoice_transaction->transactionDetails()->sum('amount'), 1, 1, 0, diff --git a/app/Classes/ValueObjects/Constants/TransactionDetailType.php b/app/Classes/ValueObjects/Constants/TransactionDetailType.php index aa9e835c..c84409fb 100644 --- a/app/Classes/ValueObjects/Constants/TransactionDetailType.php +++ b/app/Classes/ValueObjects/Constants/TransactionDetailType.php @@ -8,5 +8,7 @@ final class TransactionDetailType { public const OVER_WEIGHT_CHARGES = 'Overweight Charges'; + public const MIN_CBM_CHARGES = 'Overweight Charges'; + public const CUSTOM_CHARGES = 'Custom charges'; } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index a0055fb1..8bca16ed 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -13,9 +13,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\SoftDeletes; class Transaction extends AbstractModel implements Documentable, Transactionable, Remarkable { + use SoftDeletes; + protected $table = 'transactions'; public function owner(): morphTo diff --git a/resources/assets/vue/components/companies/elements/CustomerProfileComponent.vue b/resources/assets/vue/components/companies/elements/CustomerProfileComponent.vue index 8b0b6625..c8338065 100644 --- a/resources/assets/vue/components/companies/elements/CustomerProfileComponent.vue +++ b/resources/assets/vue/components/companies/elements/CustomerProfileComponent.vue @@ -46,7 +46,7 @@ - + diff --git a/resources/assets/vue/components/companies/forms/DetachSegmentFormComponent.vue b/resources/assets/vue/components/companies/forms/DetachSegmentFormComponent.vue index 7e87816d..7bd5bd74 100644 --- a/resources/assets/vue/components/companies/forms/DetachSegmentFormComponent.vue +++ b/resources/assets/vue/components/companies/forms/DetachSegmentFormComponent.vue @@ -12,7 +12,7 @@

Are you Sure?

-
Are you sure you want to detach custom from {{this.item.name}} segment?
+
Are you sure you want to detach customer from {{this.item.name}} segment?
@@ -20,7 +20,7 @@
Cancel
-
Remove Segment
+
Remove Segment
@@ -36,6 +36,10 @@ company_id: { required: true, type: Number + }, + connection: { + required: true, + type: Number } }, diff --git a/resources/assets/vue/components/paymentsBilling/forms/InvoiceItemFormComponent.vue b/resources/assets/vue/components/paymentsBilling/forms/InvoiceItemFormComponent.vue index 47cdfe37..b32e2b49 100644 --- a/resources/assets/vue/components/paymentsBilling/forms/InvoiceItemFormComponent.vue +++ b/resources/assets/vue/components/paymentsBilling/forms/InvoiceItemFormComponent.vue @@ -21,12 +21,12 @@

Total

{{currency}} {{(Math.round((productTotal + Number.EPSILON) * 100) / 100).toFixed(2)}}

-
+
-
+