diff --git a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantLogic.php b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantLogic.php index b7186064..71d0c542 100644 --- a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantLogic.php +++ b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantLogic.php @@ -15,6 +15,7 @@ use App\Http\Resources\ConstantResource; use App\Models\SegmentConstant; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use App\Classes\Modules\Segments\Services\UpdatesConstantValueByState; class UpdateConstantLogic extends AbstractControllerLogic { @@ -47,6 +48,8 @@ class UpdateConstantLogic extends AbstractControllerLogic /** @var CreatesConstant */ private $createsConstant; + /** @var UpdatesConstantValueByState */ + private $updatesConstantValueByState; /** * UpdateConstantLogic constructor. @@ -56,8 +59,17 @@ class UpdateConstantLogic extends AbstractControllerLogic * @param FetchesConstant $fetchesConstant * @param CanCreateConstant $canCreateConstant * @param CreatesConstant $createsConstant + * @param UpdatesConstantValueByState $updatesConstantValueByState */ - public function __construct(CanUpdateConstant $canUpdateConstant, UpdatesConstant $updatesConstant, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant, CanCreateConstant $canCreateConstant, CreatesConstant $createsConstant) + public function __construct( + CanUpdateConstant $canUpdateConstant, + UpdatesConstant $updatesConstant, + FetchesSegment $fetchesSegment, + FetchesConstant $fetchesConstant, + CanCreateConstant $canCreateConstant, + CreatesConstant $createsConstant, + UpdatesConstantValueByState $updatesConstantValueByState + ) { $this->canUpdateConstant = $canUpdateConstant; $this->updatesConstant = $updatesConstant; @@ -65,6 +77,7 @@ class UpdateConstantLogic extends AbstractControllerLogic $this->fetchesConstant = $fetchesConstant; $this->canCreateConstant = $canCreateConstant; $this->createsConstant = $createsConstant; + $this->updatesConstantValueByState = $updatesConstantValueByState; } /** * @param Request $request @@ -76,27 +89,33 @@ class UpdateConstantLogic extends AbstractControllerLogic public function logic(Request $request) : JsonResponse { - $object = new ConstantObject($request->input('reference'), $request->input('value')); - $segment = $this->fetchesSegment->execute(['id' => $request->route('id')]); try { - $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $object->getReference()]); - $this->canUpdateConstant->passes($object); + $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference')]); - /** @var SegmentConstant $constant */ - $constant = $this->updatesConstant->execute($constant, $object); + $constantValue = $this->updatesConstantValueByState->execute($constant->value, $request->input('id'), $request->input('rate')); + + $object = new ConstantObject($request->input('reference'), $constantValue); + + $this->canUpdateConstant->passes($object); } catch (ResourceNotFoundException $exception){ - $this->canCreateConstant->passes($object); + $constantObject = []; + + $constantObject[$request->input('id')] = $request->input('rate'); + + $object = new ConstantObject( + $request->input('reference'), + $constantObject + ); - /** @var SegmentConstant $constant */ $constant = $this->createsConstant->execute($segment, $object); - } - + + $constant = $this->updatesConstant->execute($constant, $object); return $this->resourceResponse(new ConstantResource($constant)); } diff --git a/app/Http/Resources/CompanyModuleResource.php b/app/Http/Resources/CompanyModuleResource.php index 7af3ae8d..51c651f7 100644 --- a/app/Http/Resources/CompanyModuleResource.php +++ b/app/Http/Resources/CompanyModuleResource.php @@ -32,6 +32,9 @@ class CompanyModuleResource extends JsonResource $connection = $this->inviters()->withPivot('invitee_reference')->first(); $marking = $connection ? $connection->pivot->invitee_reference:''; + $segmentConstantObject = SegmentConstant::where('reference', SegmentConstants::WAREHOUSE_RATE)->get(); + $segmentConstant = $segmentConstantObject->isEmpty() ? [] : (array)$segmentConstantObject->first()->value; + return [ 'id' => $this->id, 'type' => $this->type, @@ -40,7 +43,8 @@ class CompanyModuleResource extends JsonResource 'address' => new AddressResource($defaultAddress), 'company' => new CompanyResource($this->whenLoaded('company')), 'remarks' => RemarkResource::collection($this->remarks), - 'marking' => $marking + 'marking' => $marking, + 'warehouseCharges' => array_key_exists($this->id, $segmentConstant) ? $segmentConstant[$this->id] : null, ]; } diff --git a/app/Http/Resources/StateResource.php b/app/Http/Resources/StateResource.php index eb3238ed..9ccee3d3 100644 --- a/app/Http/Resources/StateResource.php +++ b/app/Http/Resources/StateResource.php @@ -16,7 +16,8 @@ class StateResource extends JsonResource public function toArray($request) { - $segmentConstant = (array) SegmentConstant::where('reference', SegmentConstants::STATE_RATE)->get()->first()->value; + $segmentConstantObject = SegmentConstant::where('reference', SegmentConstants::STATE_RATE)->get(); + $segmentConstant = $segmentConstantObject->isEmpty() ? [] : (array)$segmentConstantObject->first()->value; return [ 'id' => $this->id, diff --git a/resources/assets/vue/components/settings/elements/StatesChargesComponent.vue b/resources/assets/vue/components/settings/elements/StatesChargesComponent.vue index d9b13f07..62191046 100644 --- a/resources/assets/vue/components/settings/elements/StatesChargesComponent.vue +++ b/resources/assets/vue/components/settings/elements/StatesChargesComponent.vue @@ -27,7 +27,7 @@