diff --git a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantStateLogic.php b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantStateLogic.php index 6dca08a6..fe0b947b 100644 --- a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantStateLogic.php +++ b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantStateLogic.php @@ -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)); } diff --git a/app/Classes/Modules/Segments/Services/UpdatesConstantValueByState.php b/app/Classes/Modules/Segments/Services/UpdatesConstantValueByState.php index ae8728fa..fd283cb1 100644 --- a/app/Classes/Modules/Segments/Services/UpdatesConstantValueByState.php +++ b/app/Classes/Modules/Segments/Services/UpdatesConstantValueByState.php @@ -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; } } \ No newline at end of file diff --git a/app/Http/Resources/StateResource.php b/app/Http/Resources/StateResource.php index b9d71dae..eb3238ed 100644 --- a/app/Http/Resources/StateResource.php +++ b/app/Http/Resources/StateResource.php @@ -1,8 +1,9 @@ get()->first()->value; + return [ 'id' => $this->id, 'name' => $this->name, 'country' => $this->country, + 'stateCharges' => array_key_exists($this->id, $segmentConstant) ? $segmentConstant[$this->id] : null, ]; } } diff --git a/resources/assets/vue/components/settings/elements/StatesChargesComponent.vue b/resources/assets/vue/components/settings/elements/StatesChargesComponent.vue index dbbd14c8..d9b13f07 100644 --- a/resources/assets/vue/components/settings/elements/StatesChargesComponent.vue +++ b/resources/assets/vue/components/settings/elements/StatesChargesComponent.vue @@ -27,7 +27,7 @@
-
MYR 0.00
+
MYR {{ data.stateCharges === null ? 0.00 : data.stateCharges.center }}
@@ -39,7 +39,7 @@
-
MYR 0.00
+
MYR {{ data.stateCharges === null ? 0.00 : data.stateCharges.outstation }}
diff --git a/resources/assets/vue/components/settings/forms/EditStateChargesFormComponent.vue b/resources/assets/vue/components/settings/forms/EditStateChargesFormComponent.vue index 0df2f606..6634a93d 100644 --- a/resources/assets/vue/components/settings/forms/EditStateChargesFormComponent.vue +++ b/resources/assets/vue/components/settings/forms/EditStateChargesFormComponent.vue @@ -6,16 +6,16 @@

{{data.name}}

- +
- +
- +
- +
@@ -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 } } } },