update state charges

This commit is contained in:
edmondlang
2022-04-19 13:28:56 +08:00
parent 1814352780
commit 7b88fd5c3c
5 changed files with 65 additions and 38 deletions
@@ -11,9 +11,11 @@ use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Segments\Services\FetchesConstant;
use App\Classes\Modules\Segments\Services\UpdatesConstant;
use App\Classes\Modules\Segments\Services\CreatesConstant;
use App\Classes\Modules\Segments\Standards\Rules\CanUpdateConstant;
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Classes\Modules\Segments\Services\UpdatesConstantValueByState;
use App\Classes\Exceptions\ResourceNotFoundException;
class UpdateConstantStateLogic extends AbstractControllerLogic
{
@@ -37,6 +39,9 @@ class UpdateConstantStateLogic extends AbstractControllerLogic
/** @var FetchesSegment */
private $fetchesSegment;
/** @var CreatesConstant */
private $createsConstant;
/** @var FetchesConstant */
private $fetchesConstant;
@@ -51,14 +56,23 @@ class UpdateConstantStateLogic extends AbstractControllerLogic
* @param FetchesSegment $fetchesSegment
* @param FetchesConstant $fetchesConstant
* @param UpdatesConstantValueByState $updatesConstantValueByState
* @param CreatesConstant $createsConstant
*/
public function __construct(CanUpdateConstant $canUpdateConstant, UpdatesConstant $updatesConstant, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant,UpdatesConstantValueByState $updatesConstantValueByState)
public function __construct(
CanUpdateConstant $canUpdateConstant,
UpdatesConstant $updatesConstant,
FetchesSegment $fetchesSegment,
FetchesConstant $fetchesConstant,
UpdatesConstantValueByState $updatesConstantValueByState,
CreatesConstant $createsConstant
)
{
$this->canUpdateConstant = $canUpdateConstant;
$this->updatesConstant = $updatesConstant;
$this->fetchesSegment = $fetchesSegment;
$this->fetchesConstant = $fetchesConstant;
$this->updatesConstantValueByState = $updatesConstantValueByState;
$this->createsConstant = $createsConstant;
}
/**
* @param Request $request
@@ -69,18 +83,35 @@ class UpdateConstantStateLogic extends AbstractControllerLogic
*/
public function logic(Request $request) : JsonResponse
{
$segment = $this->fetchesSegment->execute(['id' => $request->route('id')]);
$constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => SegmentConstants::STATE_RATE]);
$constantValue = $this->updatesConstantValueByState->execute($constant->value, $request->input('status_id'), $request->input('rate'));
try {
$object = new ConstantObject(SegmentConstants::STATE_RATE, (array) $constantValue);
$this->canUpdateConstant->passes($object);
$constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => SegmentConstants::STATE_RATE]);
$constantValue = $this->updatesConstantValueByState->execute($constant->value, $request->input('id'), $request->input('rate'));
$object = new ConstantObject(SegmentConstants::STATE_RATE, $constantValue);
$this->canUpdateConstant->passes($object);
} catch (ResourceNotFoundException $exception){
$constantObject = [];
$constantObject[$request->input('id')] = $request->input('rate');
$object = new ConstantObject(
SegmentConstants::STATE_RATE,
$constantObject
);
$constant = $this->createsConstant->execute($segment, $object);
}
$constant = $this->updatesConstant->execute($constant, $object);
return $this->resourceResponse(new ConstantResource($constant));
}
@@ -8,25 +8,16 @@ class UpdatesConstantValueByState
/**
* @param $json
* @param int $status_id
* @param int $rate
* @return string
* @param Array $rate
* @return Array
*/
public function execute($json, int $status_id, int $rate) {
public function execute($json, int $state_id, Array $rate) {
$newArray = $json;
foreach($json->config as $key => $object){
if($object->status_id == $status_id){
$newArray->config[$key] = (object) [
'rate' => $rate,
'status_id' => $object->status_id,
'outstation_rate' => $object->outstation_rate,
];
}else{
$newArray->config[$key] = $object;
}
}
return $newArray;
$objectArray = (array) $json;
$objectArray[$state_id] = $rate;
return $objectArray;
}
}
+6 -1
View File
@@ -1,8 +1,9 @@
<?php
namespace App\Http\Resources;
use App\Models\SegmentConstant;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Classes\ValueObjects\Constants\SegmentConstants;
class StateResource extends JsonResource
{
@@ -14,10 +15,14 @@ class StateResource extends JsonResource
*/
public function toArray($request)
{
$segmentConstant = (array) SegmentConstant::where('reference', SegmentConstants::STATE_RATE)->get()->first()->value;
return [
'id' => $this->id,
'name' => $this->name,
'country' => $this->country,
'stateCharges' => array_key_exists($this->id, $segmentConstant) ? $segmentConstant[$this->id] : null,
];
}
}
@@ -27,7 +27,7 @@
</div>
<div class="row">
<div class="col">
<div class="font-heading all-caps">MYR 0.00</div>
<div class="font-heading all-caps">MYR {{ data.stateCharges === null ? 0.00 : data.stateCharges.center }}</div>
</div>
</div>
</div>
@@ -39,7 +39,7 @@
</div>
<div class="row">
<div class="col">
<div class="font-heading all-caps">MYR 0.00</div>
<div class="font-heading all-caps">MYR {{ data.stateCharges === null ? 0.00 : data.stateCharges.outstation }}</div>
</div>
</div>
</div>
@@ -6,16 +6,16 @@
<h2 class="text-center bold">{{data.name}}</h2>
</div>
</div>
<validation-wrapper-component class="m-b-15" :validator="$v.parameters.rate">
<validation-wrapper-component class="m-b-15" :validator="$v.parameters.rate.center">
<label class="text-primary">City Center Charges (MYR)</label>
<div class="controls">
<input type="text" class="form-control fs-12" v-model.trim="parameters.rate" v-money="money">
<input type="text" class="form-control fs-12" v-model.trim="parameters.rate.center" v-money="money">
</div>
</validation-wrapper-component>
<validation-wrapper-component class="m-b-15" :validator="$v.parameters.value.outstation_rate">
<validation-wrapper-component class="m-b-15" :validator="$v.parameters.rate.outstation">
<label class="text-primary">Outskirt Charges (MYR)</label>
<div class="controls">
<input type="text" class="form-control fs-12" v-model.trim="parameters.value.outstation_rate" v-money="money">
<input type="text" class="form-control fs-12" v-model.trim="parameters.rate.outstation" v-money="money">
</div>
</validation-wrapper-component>
<div class="row m-t-15">
@@ -45,22 +45,22 @@
},
data() {
return {
cityCenterCharges: '',
cityCenterCharges: '',
outskirtCharges : '',
parameters: {
status_id: this.data.id,
rate: 0,
value: {
outstation_rate: 0
id: this.data.id,
rate: {
center: this.data.stateCharges == null ? 0 : this.data.stateCharges.center,
outstation: this.data.stateCharges == null ? 0 : this.data.stateCharges.outstation,
}
}
};
},
validations: {
parameters: {
rate: { required },
value: {
outstation_rate: { required }
rate: {
center: { required },
outstation: { required }
}
}
},