diff --git a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantPostcodeLogic.php b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantPostcodeLogic.php index ac6dc26e..38c21b62 100644 --- a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantPostcodeLogic.php +++ b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantPostcodeLogic.php @@ -5,17 +5,8 @@ namespace App\Classes\Modules\Segments\ControllersLogic; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use App\Http\Resources\ConstantResource; -use App\Classes\ValueObjects\Constants\SegmentConstants; -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\Standards\Rules\CanUpdateConstant; -use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject; -use App\Classes\Modules\Segments\Services\AddItemToConstantValueArray; -use App\Classes\Modules\Segments\Services\RemoveItemFromConstantValueArray; -use App\Classes\Exceptions\ResourceNotFoundException; -use App\Classes\Modules\Segments\Services\CreatesConstant; +use App\Classes\Modules\Segments\Processors\UpdatePostcodeSegmentProcessor; class UpdateConstantPostcodeLogic extends AbstractControllerLogic { @@ -30,54 +21,18 @@ class UpdateConstantPostcodeLogic extends AbstractControllerLogic ]; } - /** @var CanUpdateConstant */ - private $canUpdateConstant; - - /** @var UpdatesConstant */ - private $updatesConstant; - - /** @var FetchesSegment */ - private $fetchesSegment; - - /** @var FetchesConstant */ - private $fetchesConstant; - - /** @var AddItemToConstantValueArray */ - private $addItemToConstantValueArray; - - /** @var RemoveItemFromConstantValueArray */ - private $removeItemFromConstantValueArray; - - /** @var CreatesConstant */ - private $createsConstant; + /** @var UpdatePostcodeSegmentProcessor */ + private $updatePostcodeSegmentProcessor; /** * UpdateConstantLogic constructor. - * @param CanUpdateConstant $canUpdateConstant - * @param UpdatesConstant $updatesConstant - * @param FetchesSegment $fetchesSegment - * @param FetchesConstant $fetchesConstant - * @param AddItemToConstantValueArray $addItemToConstantValueArray - * @param CreatesConstant $createsConstant - * @param RemoveItemFromConstantValueArray $removeItemFromConstantValueArray + * @param UpdatePostcodeSegmentProcessor $updatePostcodeSegmentProcessor */ public function __construct( - CanUpdateConstant $canUpdateConstant, - UpdatesConstant $updatesConstant, - FetchesSegment $fetchesSegment, - FetchesConstant $fetchesConstant, - AddItemToConstantValueArray $addItemToConstantValueArray, - RemoveItemFromConstantValueArray $removeItemFromConstantValueArray, - CreatesConstant $createsConstant + UpdatePostcodeSegmentProcessor $updatePostcodeSegmentProcessor, ) { - $this->canUpdateConstant = $canUpdateConstant; - $this->updatesConstant = $updatesConstant; - $this->fetchesSegment = $fetchesSegment; - $this->fetchesConstant = $fetchesConstant; - $this->addItemToConstantValueArray = $addItemToConstantValueArray; - $this->removeItemFromConstantValueArray = $removeItemFromConstantValueArray; - $this->createsConstant = $createsConstant; + $this->updatePostcodeSegmentProcessor = $updatePostcodeSegmentProcessor; } /** * @param Request $request @@ -88,42 +43,7 @@ class UpdateConstantPostcodeLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { - $segment = $this->fetchesSegment->execute(['id' => $request->route('id')]); - - try { - $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference')]); - - $constantValue = $this->addItemToConstantValueArray->execute($constant->value, $request->input('postcode')); - - $object = new ConstantObject($request->input('reference'), $constantValue); - - } catch (ResourceNotFoundException $exception){ - - $constantObject = [$request->input('postcode')]; - - $object = new ConstantObject( - $request->input('reference'), - $constantObject - ); - - $constant = $this->createsConstant->execute($segment, $object); - } - - try { - // check if postcode exist in the opposite constant, and delete it - $oppositeConstant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference') == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE]); - - $oppositeConstantValue = $this->removeItemFromConstantValueArray->execute($oppositeConstant->value, $request->input('postcode')); - - $oppositeObject = new ConstantObject($request->input('reference') == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE, $oppositeConstantValue); - - $this->updatesConstant->execute($oppositeConstant, $oppositeObject); - - } catch (ResourceNotFoundException $exception){ - // if opposite constant does not exist, do nothing - } - - $constant = $this->updatesConstant->execute($constant, $object); + $constant = $this->updatePostcodeSegmentProcessor->execute($request->route('id'), $request->input('reference'), $request->input('postcode')); return $this->resourceResponse(new ConstantResource($constant)); } diff --git a/app/Classes/Modules/Segments/Processors/UpdatePostcodeSegmentProcessor.php b/app/Classes/Modules/Segments/Processors/UpdatePostcodeSegmentProcessor.php new file mode 100644 index 00000000..f2fad395 --- /dev/null +++ b/app/Classes/Modules/Segments/Processors/UpdatePostcodeSegmentProcessor.php @@ -0,0 +1,122 @@ +canUpdateConstant = $canUpdateConstant; + $this->updatesConstant = $updatesConstant; + $this->fetchesSegment = $fetchesSegment; + $this->fetchesConstant = $fetchesConstant; + $this->addItemToConstantValueArray = $addItemToConstantValueArray; + $this->removeItemFromConstantValueArray = $removeItemFromConstantValueArray; + $this->createsConstant = $createsConstant; + } + + + /** + * @param string $name + * @param int $companyModuleId + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function execute($segment_id, $reference, $postcode){ + + $segment = $this->fetchesSegment->execute(['id' => $segment_id]); + + try { + $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $reference]); + + $constantValue = $this->addItemToConstantValueArray->execute($constant->value, $postcode); + + $object = new ConstantObject($reference, $constantValue); + + } catch (ResourceNotFoundException $exception){ + + $constantObject = [$postcode]; + + $object = new ConstantObject( + $reference, + $constantObject + ); + + $constant = $this->createsConstant->execute($segment, $object); + } + + try { + // check if postcode exist in the opposite constant, and delete it + $oppositeConstant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $reference == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE]); + + $oppositeConstantValue = $this->removeItemFromConstantValueArray->execute($oppositeConstant->value, $postcode); + + $oppositeObject = new ConstantObject($reference == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE, $oppositeConstantValue); + + $this->updatesConstant->execute($oppositeConstant, $oppositeObject); + + } catch (ResourceNotFoundException $exception){ + // if opposite constant does not exist, do nothing + } + + $constant = $this->updatesConstant->execute($constant, $object); + + return $constant; + + } + +} diff --git a/database/seeders/DummyDataSeeder.php b/database/seeders/DummyDataSeeder.php index e32eaa2c..815ad6b9 100644 --- a/database/seeders/DummyDataSeeder.php +++ b/database/seeders/DummyDataSeeder.php @@ -46,19 +46,23 @@ use App\Classes\ValueObjects\Constants\DocumentType; use App\Classes\ValueObjects\Constants\PackageType; use App\Classes\ValueObjects\Constants\PackingListType; use App\Classes\ValueObjects\Constants\RoleTypes; +use App\Classes\ValueObjects\Constants\SegmentConstants; use App\Classes\ValueObjects\Constants\TransportType; +use App\Classes\Modules\Segments\Processors\UpdatePostcodeSegmentProcessor; use App\Models\Address; use App\Models\Company; use App\Models\CompanyModule; use App\Models\Container; use App\Models\Document; use App\Models\PackingList; +use App\Models\SegmentConstant; use App\Models\Transport; use App\Models\User; use Carbon\Carbon; use Illuminate\Database\Seeder; use Faker\Generator as Faker; +use Illuminate\Support\Facades\Artisan; class DummyDataSeeder extends Seeder { @@ -583,6 +587,32 @@ class DummyDataSeeder extends Seeder // 3. overweight cbm } + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // + // Generate and Approve Dummmy Invoice // + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // + ini_set('memory_limit', '-1'); + $randomCompanyModule = CompanyModule::latest('id')->take(5)->get(); + + $centerPostcodeConstantObject = SegmentConstant::where('reference', SegmentConstants::CENTER_POSTCODE)->get(); + $centerPostcodeConstant = $centerPostcodeConstantObject->isEmpty() ? [] : (array)$centerPostcodeConstantObject->first()->value; + + foreach($randomCompanyModule as $companyModule) { + foreach($companyModule->addresses as $address) { + $postcode = $address->postcode; + $postcodeArea = in_array($postcode, $centerPostcodeConstant) ? 'CENTER_POSTCODE' : null; + + if (!$postcodeArea) { + $random = rand(0, 1); + $postcodeArray = ['OUTSTATION_POSTCODE', 'CENTER_POSTCODE']; + (App()->make(UpdatePostcodeSegmentProcessor::class))->execute('1', $postcodeArray[$random],$postcode); + $this->command->info($postcodeArray[$random]); + } + } + } + + Artisan::call('invoice:generate'); + Artisan::call('approve:invoice'); + } }