refactor codes in ShippingEstimationCalculatorLogic.php

This commit is contained in:
marjuqi@ad
2022-05-20 09:06:20 +07:00
parent 6de35b13f1
commit 7771b903ae
@@ -45,24 +45,20 @@ class ShippingEstimationCalculatorLogic extends AbstractControllerLogic
$cbm = (float) ($width * $length * $height);
// calculate base price
$base_rates = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::BASE_PRICE]);
$base_prices = (array) $base_rates->value;
$base_price = array_key_exists(date('Y-m-d'), $base_prices) === true ? $base_prices[date('Y-m-d')] : 0;
$base_prices = $this->getSegmentConstantData(SegmentConstants::BASE_PRICE);
$base_price = $this->getConstantDataUsingKey($base_prices, date('Y-m-d'));
// calculate warehouse rate
$warehouse_rates = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::WAREHOUSE_RATE]);
$warehouse_prices = (array) $warehouse_rates->value;
$warehouse_rate = array_key_exists($request->input('warehouse_id'), $warehouse_prices) === true ? $warehouse_prices[$request->input('warehouse_id')]->amount : 0;
$warehouse_prices = $this->getSegmentConstantData(SegmentConstants::WAREHOUSE_RATE);
$warehouse_rate = $this->getConstantDataUsingKey($warehouse_prices, $request->input('warehouse_id'), 'amount');
// calculate applied state rate
$state_rates = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::STATE_RATE]);
$state_prices = (array) $state_rates->value;
$outstation_postcodes = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::OUTSTATION_POSTCODE]);
$outstation_postcodes = (array) $outstation_postcodes->value;
$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 = array_key_exists($district->state_id, $state_prices) === true ? $state_prices[$district->state_id]->outstation : 0;
$state_rate = $this->getConstantDataUsingKey($state_prices, $district->state_id, 'outstation');
}
$price_cbm = $base_price + $warehouse_rate + $state_rate;
@@ -74,4 +70,23 @@ class ShippingEstimationCalculatorLogic extends AbstractControllerLogic
]);
}
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;
}
}