diff --git a/app/Classes/Modules/Transactions/ControllersLogic/ShippingEstimationCalculatorLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/ShippingEstimationCalculatorLogic.php new file mode 100644 index 00000000..753704a0 --- /dev/null +++ b/app/Classes/Modules/Transactions/ControllersLogic/ShippingEstimationCalculatorLogic.php @@ -0,0 +1,92 @@ + 'Shipping estimation', + 'message' => 'You have successfully calculate the Shipping Estimation' + ]; + } + + public function __construct( + FetchesSegmentConstant $fetchesSegmentConstant + ){ + $this->fetchesSegmentConstant = $fetchesSegmentConstant; + } + + public function logic(Request $request) : JsonResponse + { + $base_price = $warehouse_rate = $state_rate = 0; + + $width = $request->input('width') / 100; // metre + $length = $request->input('length') / 100; // metre + $height = $request->input('height') / 100; // metre + + $cbm = (float) ($width * $length * $height); + + // calculate base price + $base_prices = $this->getSegmentConstantData(SegmentConstants::BASE_PRICE); + $base_price = $this->getConstantDataUsingKey($base_prices, date('Y-m-d')); + + // calculate warehouse rate + $warehouse_prices = $this->getSegmentConstantData(SegmentConstants::WAREHOUSE_RATE); + $warehouse_rate = $this->getConstantDataUsingKey($warehouse_prices, $request->input('warehouse_id'), 'amount'); + + // calculate applied state rate + $state_prices = $this->getSegmentConstantData(SegmentConstants::STATE_RATE); + $outstation_postcodes = $this->getSegmentConstantData(SegmentConstants::OUTSTATION_POSTCODE); + + $district = District::where('postcode','LIKE','%'.$request->input('postcode').'%')->first(); + if(array_search($request->input('postcode'), $outstation_postcodes)!==false){ + $state_rate = $this->getConstantDataUsingKey($state_prices, $district->state_id, 'outstation'); + } + + $price_cbm = $base_price + $warehouse_rate + $state_rate; + $total_price_cbm = $price_cbm * $cbm; + + return $this->response([ + 'cbm' => $cbm, 'base_price' => $base_price, 'warehouse_rate' => $warehouse_rate, + 'state_rate' => $state_rate, 'total_price_cbm' => $total_price_cbm + ]); + } + + private function getConstantDataUsingKey($data, $search_key, $field='') + { + if(array_key_exists($search_key, $data) !== true) { + return 0; + } else { + if($field!=''){ + return $data[$search_key]->$field; + } else { + return $data[$search_key]; + } + } + } + + private function getSegmentConstantData($reference) + { + $data = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => $reference]); + return (array) $data->value; + } + +} diff --git a/app/Http/Controllers/Transactions/ShippingEstimationCalculatorController.php b/app/Http/Controllers/Transactions/ShippingEstimationCalculatorController.php new file mode 100644 index 00000000..d83a49b6 --- /dev/null +++ b/app/Http/Controllers/Transactions/ShippingEstimationCalculatorController.php @@ -0,0 +1,20 @@ +execute($request); + } +} diff --git a/routes/transaction.php b/routes/transaction.php index c3e1b2f2..e5a64bc2 100644 --- a/routes/transaction.php +++ b/routes/transaction.php @@ -14,6 +14,8 @@ Route::group(['prefix' => 'transactions', 'namespace' => 'Transactions', 'as' => route::post('/shipping-invoice/create', 'CreateShippingInvoiceTransactionController@create')->name('supplier.create'); + route::post('/shipping-invoice/calculator', 'ShippingEstimationCalculatorController@calculate')->name('shipping.estimation.calculator'); + // Route::group(['prefix' => '{id}/payment', 'as' => 'payment.'], function () { // Route::post('quotation', 'FetchBookingPaymentQuotationController@fetch')->name('quotation'); // Route::post('create', 'CreateBookingPaymentController@create')->name('create');