From af3ff46a20b5eb693bd933b2e2b4e0c4f13f0d08 Mon Sep 17 00:00:00 2001 From: ahmedsophyudden Date: Mon, 28 Feb 2022 22:40:16 +0800 Subject: [PATCH] Grab service charge from setting for calculate supplier transaction --- .../Eloquent/Filters/ServiceCharge.php | 21 +++++ .../CreateSegmentConstantLogic.php | 70 +++++++++++++++ .../FetchSegmentConstantLogic.php | 51 +++++++++++ .../UpdateSegmentConstantLogic.php | 88 +++++++++++++++++++ .../CreateSupplierTransactionLogic.php | 15 +++- .../CreateSegmentConstantController.php | 19 ++++ .../FetchSegmentConstantController.php | 20 +++++ .../UpdateSegmentConstantController.php | 20 +++++ app/Http/Resources/CompanyResource.php | 1 + app/Models/SegmentConstant.php | 4 + routes/api.php | 2 + routes/segment_constant.php | 17 ++-- 12 files changed, 317 insertions(+), 11 deletions(-) create mode 100644 app/Classes/General/Eloquent/Filters/ServiceCharge.php create mode 100644 app/Classes/Modules/SegmentConstants/ControllersLogic/CreateSegmentConstantLogic.php create mode 100644 app/Classes/Modules/SegmentConstants/ControllersLogic/FetchSegmentConstantLogic.php create mode 100644 app/Classes/Modules/SegmentConstants/ControllersLogic/UpdateSegmentConstantLogic.php create mode 100644 app/Http/Controllers/SegmentConstants/CreateSegmentConstantController.php create mode 100644 app/Http/Controllers/SegmentConstants/FetchSegmentConstantController.php create mode 100644 app/Http/Controllers/SegmentConstants/UpdateSegmentConstantController.php diff --git a/app/Classes/General/Eloquent/Filters/ServiceCharge.php b/app/Classes/General/Eloquent/Filters/ServiceCharge.php new file mode 100644 index 00000000..de8cdded --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/ServiceCharge.php @@ -0,0 +1,21 @@ +where('reference', SegmentConstants::SERVICE_CHARGE)->where('detail->id', $value); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/SegmentConstants/ControllersLogic/CreateSegmentConstantLogic.php b/app/Classes/Modules/SegmentConstants/ControllersLogic/CreateSegmentConstantLogic.php new file mode 100644 index 00000000..12b26ad6 --- /dev/null +++ b/app/Classes/Modules/SegmentConstants/ControllersLogic/CreateSegmentConstantLogic.php @@ -0,0 +1,70 @@ + 'Created Segment Constant', + 'message' => 'You have successfully created a new Segment Constant' + ]; + } + + /** @var CanCreateConstant */ + private $canCreateConstant; + + /** @var CreatesConstant */ + private $createsConstant; + + /** @var FetchesSegment */ + private $fetchesSegment; + + + /** + * CreateSegmentLogic constructor. + * @param CanCreateConstant $canCreateConstant + * @param CreatesConstant $createsConstant + */ + public function __construct(CanCreateConstant $canCreateConstant, CreatesConstant $createsConstant, FetchesSegment $fetchesSegment) + { + $this->canCreateConstant = $canCreateConstant; + $this->createsConstant = $createsConstant; + $this->fetchesSegment = $fetchesSegment; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function logic(Request $request) : JsonResponse + { + $constant_object = new ConstantObject($request->input('name'), $request->input('reference'), $request->input('detail')); + + $segment = $this->fetchesSegment->execute(['id' => $request->input('segment_id')]); + + $this->canCreateConstant->passes($constant_object); + $constant = $this->createsConstant->execute($segment, $constant_object); + + return $this->resourceResponse(new ConstantResource($constant)); + + } +} \ No newline at end of file diff --git a/app/Classes/Modules/SegmentConstants/ControllersLogic/FetchSegmentConstantLogic.php b/app/Classes/Modules/SegmentConstants/ControllersLogic/FetchSegmentConstantLogic.php new file mode 100644 index 00000000..43c10039 --- /dev/null +++ b/app/Classes/Modules/SegmentConstants/ControllersLogic/FetchSegmentConstantLogic.php @@ -0,0 +1,51 @@ + 'Retrieved Segment Constant', + 'message' => 'You have successfully retrieved a segment constant' + ]; + } + + /** @var FetchesConstant */ + private $fetchesConstant; + + /** + * FetchSegmentLogic constructor. + * @param FetchesConstant $fetchesConstant + */ + public function __construct(FetchesConstant $fetchesConstant) + { + $this->fetchesConstant = $fetchesConstant; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function logic(Request $request) : JsonResponse + { + $query = $this->fetchesConstant->execute(['id' => $request->route('id')]); + + return $this->resourceResponse(new ConstantResource($query)); + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/SegmentConstants/ControllersLogic/UpdateSegmentConstantLogic.php b/app/Classes/Modules/SegmentConstants/ControllersLogic/UpdateSegmentConstantLogic.php new file mode 100644 index 00000000..49897e95 --- /dev/null +++ b/app/Classes/Modules/SegmentConstants/ControllersLogic/UpdateSegmentConstantLogic.php @@ -0,0 +1,88 @@ + 'Updated Segment Constant', + 'message' => 'You have successfully updated the Segment Constant' + ]; + } + + /** @var CanUpdateConstant */ + private $canUpdateConstant; + + /** @var UpdatesConstant */ + private $updatesConstant; + + /** @var FetchesConstant */ + private $fetchesConstant; + + + /** + * UpdateSegmentLogic constructor. + * @param CanUpdateConstant $canUpdateConstant + * @param UpdatesConstant $updatesConstant + * @param FetchesConstant $fetchesConstant + */ + public function __construct( + CanUpdateConstant $canUpdateConstant, + UpdatesConstant $updatesConstant, + FetchesConstant $fetchesConstant + ) + { + $this->canUpdateConstant = $canUpdateConstant; + $this->updatesConstant = $updatesConstant; + $this->fetchesConstant = $fetchesConstant; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + try { + DB::beginTransaction(); + + $constant_query = $this->fetchesConstant->execute(['id' => $request->route('id')]); + + $constant_object = new ConstantObject( + $request->input('name', $constant_query->name), + $request->input('reference', $constant_query->reference), + $request->input('detail', (array) $constant_query->detail)); + $this->canUpdateConstant->passes($constant_object); + $constant_query = $this->updatesConstant->execute($constant_query, $constant_object); + + DB::commit(); + + return $this->resourceResponse(new ConstantResource($constant_query)); + + } catch (\Exception $exception){ + throw new ErrorException($exception->getMessage(), $exception->getCode()); + } + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php index b524b3e9..9cb671a8 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php @@ -3,12 +3,14 @@ namespace App\Classes\Modules\Transactions\ControllersLogic; +use DB; use Carbon\Carbon; use App\Models\Document; use Barryvdh\DomPDF\PDF; use App\Models\Transaction; use Illuminate\Support\Str; use Illuminate\Http\Request; +use App\Models\SegmentConstant; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Storage; use Meneses\LaravelMpdf\Facades\LaravelMpdf; @@ -16,16 +18,18 @@ use App\Classes\ValueObjects\Constants\DocumentType; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\TransactionType; use App\Classes\Modules\Documents\Services\CreatesFiles; +use App\Classes\ValueObjects\Constants\SegmentConstants; use App\Classes\ValueObjects\Constants\PaymentMethodType; use App\Classes\General\Abstracts\AbstractControllerLogic; use App\Classes\Modules\Companies\Services\FetchesCompany; +use App\Classes\Modules\Segments\Services\FetchesConstant; use Meneses\LaravelLaravelMpdf\Facades\LaravelLaravelMpdf; use App\Classes\Modules\Documents\Services\CreatesDocument; use App\Classes\Modules\Transactions\Services\CreatesTransaction; + use App\Classes\Modules\Transactions\Services\FetchesTransaction; use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; - use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; use App\Classes\Modules\Transactions\Services\CalculatesTransactionTransferFee; @@ -79,6 +83,7 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic * CreateSupplierTransactionLogic constructor. * @param FetchesCompany $fetchesCompany * @param FetchesTransaction $fetchesTransaction + * @param FetchesConstant $fetchesConstant * @param UpdatesTransactionStatus $updatesTransactionStatus * @param CreatesTransaction $createsTransaction * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber @@ -86,11 +91,12 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic * @param CalculatesTransactionTransferFee $calculatesTransactionTransferFee * @param PDF $pdf */ - public function __construct(FetchesCompany $fetchesCompany, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, CreatesTransaction $createsTransaction, CreatesDocument $createsDocument, CreatesFiles $createsFile, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CalculatesTransactionServiceCharge $calculatesTransactionServiceCharge, CalculatesTransactionTransferFee $calculatesTransactionTransferFee, PDF $pdf) + public function __construct(FetchesCompany $fetchesCompany, FetchesTransaction $fetchesTransaction, FetchesConstant $fetchesConstant, UpdatesTransactionStatus $updatesTransactionStatus, CreatesTransaction $createsTransaction, CreatesDocument $createsDocument, CreatesFiles $createsFile, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CalculatesTransactionServiceCharge $calculatesTransactionServiceCharge, CalculatesTransactionTransferFee $calculatesTransactionTransferFee, PDF $pdf) { $this->fetchesCompany = $fetchesCompany; $this->fetchesTransaction = $fetchesTransaction; + $this->fetchesConstant = $fetchesConstant; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->createsTransaction = $createsTransaction; $this->createsDocument = $createsDocument; @@ -119,7 +125,10 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic $this->updatesTransactionStatus->execute($payment, ApprovalStatus::COMPLETED); $billNumber = $this->generatesTransactionBillNumber->execute('SPLR-'); - $serviceCharge = $this->calculatesTransactionServiceCharge->execute($payment->original_amount, $rate); + // $constant = $this->fetchesConstant->execute(['service_charge' => $supplier->id]); + $constant = SegmentConstant::where('reference', SegmentConstants::SERVICE_CHARGE)->where('detail->id', $supplier->id)->first(); + $serviceCharge = $this->calculatesTransactionServiceCharge->execute($payment->original_amount, $rate, $constant ? $constant->detail->service_charge->amount : 0.75); + $object = new TransactionObject($billNumber, TransactionType::BILL, $supplier->id, 1, $supplier->banks()->where('default', true)->first()->id, PaymentMethodType::CASH, $payment->original_amount * (1 / $rate), $payment->original_amount, 1, $payment->original_currency_id, diff --git a/app/Http/Controllers/SegmentConstants/CreateSegmentConstantController.php b/app/Http/Controllers/SegmentConstants/CreateSegmentConstantController.php new file mode 100644 index 00000000..7520e1d1 --- /dev/null +++ b/app/Http/Controllers/SegmentConstants/CreateSegmentConstantController.php @@ -0,0 +1,19 @@ +execute($request); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/SegmentConstants/FetchSegmentConstantController.php b/app/Http/Controllers/SegmentConstants/FetchSegmentConstantController.php new file mode 100644 index 00000000..67532d3f --- /dev/null +++ b/app/Http/Controllers/SegmentConstants/FetchSegmentConstantController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/SegmentConstants/UpdateSegmentConstantController.php b/app/Http/Controllers/SegmentConstants/UpdateSegmentConstantController.php new file mode 100644 index 00000000..c34914dc --- /dev/null +++ b/app/Http/Controllers/SegmentConstants/UpdateSegmentConstantController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/app/Http/Resources/CompanyResource.php b/app/Http/Resources/CompanyResource.php index 54ab047d..50a2da9c 100644 --- a/app/Http/Resources/CompanyResource.php +++ b/app/Http/Resources/CompanyResource.php @@ -55,6 +55,7 @@ class CompanyResource extends JsonResource 'default' => new BankResource($this->banks->where('type', BankAccountType::EXTERNAL)->where('default', true)->first()) ], 'segments' => SegmentResource::collection($this->segments), + 'service_charges' => SegmentConstant::where('reference', SegmentConstants::SERVICE_CHARGE)->where('detail->id', $this->id)->first(), 'services' => (new FetchesCompanyServices())->getServices($this->servicesConfigurations()), 'currencies' => $this->when($this->business_type === BusinessType::CURRENCY_VENDOR, function(){ $segment = SegmentConstant::where('reference', SegmentConstants::SUPPLIER_CURRENCIES)->where('detail->id', $this->id)->first(); diff --git a/app/Models/SegmentConstant.php b/app/Models/SegmentConstant.php index 08c8b2af..b7fda276 100644 --- a/app/Models/SegmentConstant.php +++ b/app/Models/SegmentConstant.php @@ -20,6 +20,10 @@ class SegmentConstant extends AbstractModel protected $table = 'segment_constants'; protected $dates = ['deleted_at']; + + // protected $casts = [ + // 'detail' => 'array', + // ]; public function getDetailAttribute($value) { diff --git a/routes/api.php b/routes/api.php index cf5838f3..10c0fb80 100644 --- a/routes/api.php +++ b/routes/api.php @@ -35,6 +35,8 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function require __DIR__ . '/segment.php'; + require __DIR__ . '/segment_constant.php'; + require __DIR__ . '/currency.php'; require __DIR__ . '/bank.php'; diff --git a/routes/segment_constant.php b/routes/segment_constant.php index e49e9e97..e393e3f3 100644 --- a/routes/segment_constant.php +++ b/routes/segment_constant.php @@ -2,19 +2,20 @@ use Illuminate\Support\Facades\Route; -Route::group(['namespace' => 'StandardSegmentConstants'], function () { - Route::group(['middleware' => 'valid.token'], function () { - Route::group(['prefix' => 'standard-segment-constant'], function () { - Route::get('/list', 'ListStandardSegmentConstantsController@list')->name('standard_segment.update'); - Route::put('/update/{id}', 'UpdateStandardSegmentConstantController@update')->name('standard_segment_constant.update'); - }); - }); -}); +// Route::group(['namespace' => 'StandardSegmentConstants'], function () { +// Route::group(['middleware' => 'valid.token'], function () { +// Route::group(['prefix' => 'standard-segment-constant'], function () { +// Route::get('/list', 'ListStandardSegmentConstantsController@list')->name('standard_segment.update'); +// Route::put('/update/{id}', 'UpdateStandardSegmentConstantController@update')->name('standard_segment_constant.update'); +// }); +// }); +// }); Route::group(['namespace' => 'SegmentConstants'], function () { Route::group(['middleware' => 'valid.token'], function () { Route::group(['prefix' => 'segment-constant'], function () { + Route::get('/{id}/show', 'FetchSegmentConstantController@fetch')->name('segment_constant.show'); Route::post('/create', 'CreateSegmentConstantController@create')->name('segment_constant.create'); Route::put('/update/{id}', 'UpdateSegmentConstantController@update')->name('segment_constant.update'); });