diff --git a/app/Classes/Modules/Accounts/Processors/GenerateEmailVerificationAttemptProcessor.php b/app/Classes/Modules/Accounts/Processors/GenerateEmailVerificationAttemptProcessor.php index 23853f27..4056234c 100644 --- a/app/Classes/Modules/Accounts/Processors/GenerateEmailVerificationAttemptProcessor.php +++ b/app/Classes/Modules/Accounts/Processors/GenerateEmailVerificationAttemptProcessor.php @@ -56,4 +56,4 @@ class GenerateEmailVerificationAttemptProcessor } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Orders/ControllersLogic/ShippingCostLogic.php b/app/Classes/Modules/Orders/ControllersLogic/ShippingCostLogic.php new file mode 100644 index 00000000..b406e565 --- /dev/null +++ b/app/Classes/Modules/Orders/ControllersLogic/ShippingCostLogic.php @@ -0,0 +1,103 @@ + 'Order Shipping Cost', + 'message' => 'Order shipping cost calculation done' + ]; + } + + /** @var CalculateCBM */ + private $calculateCBM; + + /** @var CalculatesBaseRate */ + private $calculateBaseRate; + + /** @var CalculatesSegmentRate */ + private $calculateSegmentRate; + + /** @var CalculatesStateRate */ + private $calculateStateRate; + + /** @var CalculatesWarehouse */ + private $calculateWarehouseRate; + + /** @var FetchesOrder */ + private $fetchesOrder; + + /** @var ShippingCostTransactionProcessor */ + private $shippingCostTransactionProcessor; + + /** + * ShippingCostLogic constructor. + * @param CalculateCBM $calculateCBM + * @param FetchesOrder $fetchesOrder + */ + public function __construct(FetchesOrder $fetchesOrder, CalculatesCBM $calculateCBM, CalculatesBaseRate $calculateBaseRate, CalculatesSegmentRate $calculateSegmentRate, CalculatesStateRate $calculateStateRate, CalculatesWarehouseRate $calculateWarehouseRate, CreateShippingCostTransactionProcessor $shippingCostTransactionProcessor) + { + $this->fetchesOrder = $fetchesOrder; + $this->calculateCBM = $calculateCBM; + $this->calculatesBaseRate = $calculateBaseRate; + $this->calculatesSegmentRate = $calculateSegmentRate; + $this->calculatesStateRate = $calculateStateRate; + $this->calculateWarehouseRate = $calculateWarehouseRate; + $this->shippingCostTransactionProcessor = $shippingCostTransactionProcessor; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $order = $this->fetchesOrder->getRepository()->where(['id' => $request->route('id')])->first(); + + $baseRate = $this->calculatesBaseRate->execute($order); + $segmentRate = $this->calculatesSegmentRate->execute($order, SegmentConstants::STANDARD_SEGMENT) + $this->calculatesSegmentRate->execute($order, SegmentConstants::CUSTOM_SEGMENT); + $stateRate = $this->calculatesStateRate->execute($order, false) + $this->calculatesStateRate->execute($order, true); + $warehouseRate = $this->calculateWarehouseRate->execute($order); + + $totalShippingCost = 0; + $totalCBM = 0; + $quantity = 0; + foreach($order->parcels as $parcel){ + $cbm = $this->calculateCBM->execute($order, $parcel); + + if($cbm > 0){ + $totalCBM += $cbm; + $quantity ++; + $totalShippingCost += (($segmentRate + $stateRate + $warehouseRate) * $cbm); + } + } + + $this->shippingCostTransactionProcessor->execute($order, $quantity, round($totalCBM,2), round($totalShippingCost,2)); + return $this->resourceResponse(new OrderResource($order)); + } + +} diff --git a/app/Classes/Modules/Orders/Services/CalculatesBaseRate.php b/app/Classes/Modules/Orders/Services/CalculatesBaseRate.php new file mode 100644 index 00000000..14a354f5 --- /dev/null +++ b/app/Classes/Modules/Orders/Services/CalculatesBaseRate.php @@ -0,0 +1,18 @@ +length * $parcel->width * $parcel->height; + } + +} diff --git a/app/Classes/Modules/Orders/Services/CalculatesSegmentRate.php b/app/Classes/Modules/Orders/Services/CalculatesSegmentRate.php new file mode 100644 index 00000000..402d2aab --- /dev/null +++ b/app/Classes/Modules/Orders/Services/CalculatesSegmentRate.php @@ -0,0 +1,20 @@ +code = $code; + $this->reference = $reference; $this->name = $name; $this->quantity = $quantity; $this->price = $price; @@ -36,9 +36,9 @@ class TransactionDetailObject implements DataTransferObject /** * @return string */ - public function getCode(): ?string + public function getReference(): ?string { - return $this->code; + return $this->reference; } /** @@ -70,4 +70,4 @@ class TransactionDetailObject implements DataTransferObject return $this->getQuantity() * $this->getPrice(); } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Transactions/Processors/CreateShippingCostTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateShippingCostTransactionProcessor.php new file mode 100644 index 00000000..5591731c --- /dev/null +++ b/app/Classes/Modules/Transactions/Processors/CreateShippingCostTransactionProcessor.php @@ -0,0 +1,66 @@ +generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsShippingCostTransaction = $createsShippingCostTransaction; + } + + + /** + * @param Order $order + * @return void + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function execute(Order $order, int $quantity, float $totalCBM, float $totalShippingCost) + { + $billNumber = $this->generatesTransactionBillNumber->execute('SHP-'); + + $transactionObject = new TransactionObject( + $billNumber, + TransactionType::SHIPPING_COST, + 1, + $order->companyModule->company_id, + 1, + PaymentMethodType::BA, + $totalShippingCost, + $totalShippingCost, + 1, + 1, + 0, + 0, + 0, + null, + ApprovalStatus::PENDING_SUBMISSION + ); + + $transactionDetailObject = new TransactionDetailObject( + TransactionType::SHIPPING_COST, + 'Shipping Cost', + $quantity, + $totalShippingCost/$totalCBM + ); + + $shippingCostTransaction = $this->createsShippingCostTransaction->execute($order, $transactionObject, $transactionDetailObject); + + } +} diff --git a/app/Classes/Modules/Transactions/Services/CreatesShippingCostTransaction.php b/app/Classes/Modules/Transactions/Services/CreatesShippingCostTransaction.php new file mode 100644 index 00000000..e071d174 --- /dev/null +++ b/app/Classes/Modules/Transactions/Services/CreatesShippingCostTransaction.php @@ -0,0 +1,52 @@ +bill_no = $object->getBillNo(); + $model->type = $object->getTransactionType(); + $model->issuer = $object->getIssuer(); + $model->receiver = $object->getReceiver(); + $model->recipient_bank_account_id = $object->getRecipientBankAccountId(); + $model->payment_method = $object->getPaymentMethod(); + $model->amount = $object->getAmount(); + $model->original_amount = $object->getOriginalAmount(); + $model->currency_id = $object->getCurrencyId(); + $model->original_currency_id = $object->getOriginalCurrencyId(); + $model->currency_rate = $object->getCurrencyRate(); + $model->tax = $object->getTax(); + $model->service_charge = $object->getServiceCharge(); + $model->expires_on = $object->getExpiresOn(); + $model->status = $object->getStatus(); + + $transaction = $this->handler($order->transactions(), $model); + + + $model = new TransactionDetail(); + $model->reference = $transactionDetailObject->getReference(); + $model->name = $transactionDetailObject->getName(); + $model->quantity = $transactionDetailObject->getQuantity(); + $model->price = $transactionDetailObject->getPrice(); + $model->amount = $transactionDetailObject->getAmount(); + + return $this->handler($transaction->transactionDetails(), $model); + + + } +} diff --git a/app/Classes/ValueObjects/Constants/TransactionType.php b/app/Classes/ValueObjects/Constants/TransactionType.php index 905c12b1..8a5f1075 100644 --- a/app/Classes/ValueObjects/Constants/TransactionType.php +++ b/app/Classes/ValueObjects/Constants/TransactionType.php @@ -22,4 +22,6 @@ final class TransactionType { public const SUPPLIER_DELIVER = 8; + public const SHIPPING_COST = 9; + } diff --git a/app/Http/Controllers/Orders/ShippingCostController.php b/app/Http/Controllers/Orders/ShippingCostController.php new file mode 100644 index 00000000..c86f8735 --- /dev/null +++ b/app/Http/Controllers/Orders/ShippingCostController.php @@ -0,0 +1,20 @@ +execute($request); + } +} diff --git a/app/Models/Order.php b/app/Models/Order.php index 9d3099a5..bdd7d652 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -88,4 +88,12 @@ class Order extends AbstractModel implements Addressable, Packable { return $this->addresses()->where('status','=',ApprovalStatus::APPROVED); } + + /** + * @return morphMany + */ + public function transactions(): morphMany + { + return $this->morphMany(Transaction::class, 'owner'); + } } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 0e39c289..232964d9 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -16,12 +16,17 @@ class Transaction extends AbstractModel implements Documentable { protected $table = 'transactions'; - /** - * @return BelongsTo - */ - public function booking(): BelongsTo + public function owner(): morphTo { - return $this->BelongsTo( Booking::class, 'booking_id', 'id'); + return $this->morphTo(); + } + + /** + * @return MorphMany + */ + public function order(): morphMany + { + return $this->morphMany(Order::class, 'owner'); } /** diff --git a/app/Models/TransactionDetail.php b/app/Models/TransactionDetail.php index 8e0a1613..c70f3989 100644 --- a/app/Models/TransactionDetail.php +++ b/app/Models/TransactionDetail.php @@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; class TransactionDetail extends AbstractModel { - protected $table = 'transaction_detail'; + protected $table = 'transaction_details'; /** * @return BelongsTo diff --git a/database/migrations/2021_10_16_223035_create_transactions_table.php b/database/migrations/2021_10_16_223035_create_transactions_table.php new file mode 100644 index 00000000..a40b43ea --- /dev/null +++ b/database/migrations/2021_10_16_223035_create_transactions_table.php @@ -0,0 +1,63 @@ +id(); + $table->morphs('owner'); + $table->string('type')->default(TransactionType::PAYMENT); + $table->foreignId('issuer')->unsigned(); + $table->foreignId('receiver')->unsigned(); + $table->foreignId('recipient_bank_account_id')->unsigned(); + $table->string('payment_method')->nullable(); + $table->string('payment_reference')->nullable(); + $table->string('bill_no')->unique(); + $table->decimal('amount', 25, 5)->default(0.00); + $table->decimal('original_amount', 25, 5)->default(0.00); + $table->foreignId('currency_id')->unsigned(); + $table->foreignId('original_currency_id')->unsigned(); + $table->decimal('currency_rate', 14, 5)->default(0.00); + $table->decimal('tax', 14, 5)->default(0.00); + $table->decimal('service_charge', 14, 5)->default(0.00); + $table->timestamp('expires_on')->nullable(); + $table->integer('status')->default(ApprovalStatus::PENDING_SUBMISSION); + $table->softDeletes(); + $table->timestamps(); + + $table->foreign('currency_id')->references('id')->on('currencies'); + $table->foreign('issuer')->references('id')->on('companies'); + $table->foreign('receiver')->references('id')->on('companies'); + $table->foreign('original_currency_id')->references('id')->on('currencies'); + + }); + + //In-case the model name lengthy + Schema::table('transactions', function (Blueprint $table) { + $table->string('owner_type', 250)->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('transactions'); + } +} diff --git a/database/migrations/2021_10_16_223129_create_transaction_details_table.php b/database/migrations/2021_10_16_223129_create_transaction_details_table.php new file mode 100644 index 00000000..d08dd7f1 --- /dev/null +++ b/database/migrations/2021_10_16_223129_create_transaction_details_table.php @@ -0,0 +1,41 @@ +id(); + $table->foreignId('transaction_id')->unsigned(); + $table->string('reference'); + $table->string('name'); + $table->integer('quantity')->default(0); + $table->decimal('price', 25, 5)->default(0.00); + $table->decimal('amount', 25, 5)->default(0.00); + $table->softDeletes(); + $table->timestamps(); + + $table->foreign('transaction_id')->references('id')->on('transactions'); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('transaction_details'); + } +} diff --git a/routes/order.php b/routes/order.php index bcefa2ed..2da8880f 100644 --- a/routes/order.php +++ b/routes/order.php @@ -15,4 +15,6 @@ Route::group(['prefix' => 'order', 'as' => 'order.', 'namespace' => 'Orders'], f Route::post('/create-warehouse-packing-list', 'CreateWarehousePackingListController@create')->name('create.warehouse.packing_list'); Route::post('/create-shipping-packing-list', 'CreateShippingPackingListController@create')->name('create.shipping.packing_list'); // Route::post('/create', 'CreateOrderController@create')->name('create'); + + Route::get('/{id}/shipping-cost', 'ShippingCostController@calculate')->name('shipping.cost'); });