Files
portal/app/Classes/Modules/Transactions/ControllersLogic/ShippingEstimationCalculatorLogic.php
T
2022-05-20 08:14:14 +07:00

78 lines
3.1 KiB
PHP

<?php
namespace App\Classes\Modules\Transactions\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\SegmentConstants\Services\FetchesSegmentConstant;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Models\District;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\General\Helper;
class ShippingEstimationCalculatorLogic extends AbstractControllerLogic
{
/** @var FetchesSegmentConstant */
private $fetchesSegmentConstant;
/**
* @return array
*/
protected function notification():array {
return [
'title' => '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_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;
// 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;
// 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;
$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;
}
$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
]);
}
}