From 0821db2899a33e165fb3055edfbb9b09835c22f7 Mon Sep 17 00:00:00 2001 From: "marjuqi@ad" Date: Thu, 19 May 2022 06:23:19 +0700 Subject: [PATCH] refactor functions: getConstantByKey, checkPostcodeExistInConstant --- app/Classes/General/Helper.php | 27 ++++++++++++++++++ .../CreateShippingInvoiceTransactionLogic.php | 28 ++++--------------- .../ShippingEstimationCalculatorLogic.php | 26 ++++------------- 3 files changed, 38 insertions(+), 43 deletions(-) diff --git a/app/Classes/General/Helper.php b/app/Classes/General/Helper.php index cad68208..7473ec80 100644 --- a/app/Classes/General/Helper.php +++ b/app/Classes/General/Helper.php @@ -24,4 +24,31 @@ class Helper return array_slice(get_class_methods($className), 1); } + /** + * @param object $segmentConstantObject + * @param string $key + * @return integer + */ + static function getConstantByKey($segmentConstantObject, $key) { + if ($segmentConstantObject) { + $base_rate = (array) $segmentConstantObject->value; + $base_rate = array_key_exists($key, $base_rate) === true ? $base_rate[$key] : 0; + return $base_rate; + } + } + + /** + * @param object $segmentConstantObject + * @param string $postcode + * @return boolean + */ + static function checkPostcodeExistInConstant($segmentConstantObject, $postcode) { + $segmentConstantObject = $segmentConstantObject->value; + if (!empty($segmentConstantObject)) { + return in_array($postcode, $segmentConstantObject); + } else { + return false; + } + } + } \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateShippingInvoiceTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateShippingInvoiceTransactionLogic.php index c7cc985d..6424ba57 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateShippingInvoiceTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateShippingInvoiceTransactionLogic.php @@ -32,6 +32,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Meneses\LaravelMpdf\Facades\LaravelMpdf; +use App\Classes\General\Helper; class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic { @@ -114,19 +115,19 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic $state_rate = 0; $state_select = ''; - $base_price = $this->getConstantByKey($base_price_constant, date('Y-m-d')); + $base_price = Helper::getConstantByKey($base_price_constant, date('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); + $selected_warehouse_rate = Helper::getConstantByKey($warehouse_rate_constant, $warehouseId); $warehouse_rate = is_object($selected_warehouse_rate) ? $selected_warehouse_rate->amount : 0; $stateId = $address->state_id; - $state_rate_constant = $this->getConstantByKey($state_rate_constant, $stateId); + $state_rate_constant = Helper::getConstantByKey($state_rate_constant, $stateId); // get $state_rate $postcode = $address->postcode; - $this->checkPostcodeExistInConstant($center_postcode_constant, $postcode) === true ? $state_select = 'center' : '' ; - $this->checkPostcodeExistInConstant($outstation_postcode_constant, $postcode) === true ? $state_select = 'outstation' : '' ; + Helper::checkPostcodeExistInConstant($center_postcode_constant, $postcode) === true ? $state_select = 'center' : '' ; + Helper::checkPostcodeExistInConstant($outstation_postcode_constant, $postcode) === true ? $state_select = 'outstation' : '' ; $state_rate_constant = (array)$state_rate_constant; $state_rate = $state_select == '' ? 0 : $state_rate_constant[$state_select]; @@ -194,21 +195,4 @@ class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic return $this->response([]); } - function getConstantByKey($segmentConstantObject, $key) { - if ($segmentConstantObject) { - $base_rate = (array) $segmentConstantObject->value; - $base_rate = array_key_exists($key, $base_rate) === true ? $base_rate[$key] : 0; - return $base_rate; - } - } - - function checkPostcodeExistInConstant($segmentConstantObject, $postcode) { - $segmentConstantObject = $segmentConstantObject->value; - if (!empty($segmentConstantObject)) { - return in_array($postcode, $segmentConstantObject); - } else { - return false; - } - } - } diff --git a/app/Classes/Modules/Transactions/ControllersLogic/ShippingEstimationCalculatorLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/ShippingEstimationCalculatorLogic.php index aad6e295..825bc81e 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/ShippingEstimationCalculatorLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/ShippingEstimationCalculatorLogic.php @@ -10,6 +10,7 @@ use App\Models\District; use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use App\Classes\General\Helper; class ShippingEstimationCalculatorLogic extends AbstractControllerLogic { @@ -49,14 +50,14 @@ class ShippingEstimationCalculatorLogic extends AbstractControllerLogic $state_rate_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::STATE_RATE]); $outstation_postcode_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::OUTSTATION_POSTCODE]); - $base_price = $this->getConstantByKey($base_price_constant, date('Y-m-d')); - $selected_warehouse_rate = $this->getConstantByKey($warehouse_rate_constant, $request->input('warehouse_id')); + $base_price = Helper::getConstantByKey($base_price_constant, date('Y-m-d')); + $selected_warehouse_rate = Helper::getConstantByKey($warehouse_rate_constant, $request->input('warehouse_id')); $warehouse_rate = is_object($selected_warehouse_rate) ? $selected_warehouse_rate->amount : 0; $district = District::where('postcode','LIKE','%'.$request->input('postcode').'%')->first(); - $state_rate_constant = $this->getConstantByKey($state_rate_constant, $district->state_id); + $state_rate_constant = Helper::getConstantByKey($state_rate_constant, $district->state_id); - $this->checkPostcodeExistInConstant($outstation_postcode_constant, $request->input('postcode')) === true ? $state_select = 'outstation' : '' ; + Helper::checkPostcodeExistInConstant($outstation_postcode_constant, $request->input('postcode')) === true ? $state_select = 'outstation' : '' ; $state_rate_constant = (array)$state_rate_constant; $state_rate = $state_select == '' ? 0 : $state_rate_constant[$state_select]; @@ -69,21 +70,4 @@ class ShippingEstimationCalculatorLogic extends AbstractControllerLogic ]); } - function getConstantByKey($segmentConstantObject, $key) { - if ($segmentConstantObject) { - $base_rate = (array) $segmentConstantObject->value; - $base_rate = array_key_exists($key, $base_rate) === true ? $base_rate[$key] : 0; - return $base_rate; - } - } - - function checkPostcodeExistInConstant($segmentConstantObject, $postcode) { - $segmentConstantObject = $segmentConstantObject->value; - if (!empty($segmentConstantObject)) { - return in_array($postcode, $segmentConstantObject); - } else { - return false; - } - } - }