mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/portal.git
synced 2026-08-21 21:44:09 +00:00
Merge branch 'cbm_calc' into 'master'
Shipping Cost Calculations See merge request CIEFWorldwideSdnBhd/shipping-portal!70
This commit is contained in:
+1
-1
@@ -56,4 +56,4 @@ class GenerateEmailVerificationAttemptProcessor
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Orders\ControllersLogic;
|
||||
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
||||
use App\Classes\Modules\Orders\Services\CalculatesCBM;
|
||||
use App\Classes\Modules\Orders\Services\CalculatesBaseRate;
|
||||
use App\Classes\Modules\Orders\Services\CalculatesSegmentRate;
|
||||
use App\Classes\Modules\Orders\Services\CalculatesStateRate;
|
||||
use App\Classes\Modules\Orders\Services\CalculatesWarehouseRate;
|
||||
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
||||
use App\Classes\Modules\Transactions\Processors\CreateShippingCostTransactionProcessor;
|
||||
|
||||
use App\Classes\Modules\Orders\Services\FetchesOrder;
|
||||
use App\Http\Resources\OrderResource;
|
||||
use App\Models\Order;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ShippingCostLogic extends AbstractControllerLogic
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function notification():array {
|
||||
return [
|
||||
'title' => '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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Orders\Services;
|
||||
|
||||
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Models\Order;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CalculatesBaseRate
|
||||
{
|
||||
|
||||
public function execute(Order $order){
|
||||
return 300;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Orders\Services;
|
||||
|
||||
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Models\Order;
|
||||
use App\Models\Package;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CalculatesCBM
|
||||
{
|
||||
|
||||
public function execute(Order $order, Package $parcel){
|
||||
return $parcel->length * $parcel->width * $parcel->height;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Orders\Services;
|
||||
|
||||
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
||||
use App\Models\Order;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CalculatesSegmentRate
|
||||
{
|
||||
|
||||
public function execute(Order $order, int $segmentRates){
|
||||
return ($segmentRates == SegmentConstants::STANDARD_SEGMENT) ? 300 : (
|
||||
(($segmentRates == SegmentConstants::CUSTOM_SEGMENT) ? -20 : 0)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Orders\Services;
|
||||
|
||||
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Models\Order;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CalculatesStateRate
|
||||
{
|
||||
|
||||
public function execute(Order $order, $isOutskirt=false){
|
||||
return ($isOutskirt) ? 20 : 40;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Orders\Services;
|
||||
|
||||
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Models\Order;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CalculatesWarehouseRate
|
||||
{
|
||||
|
||||
public function execute(Order $order){
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use App\Classes\General\Interfaces\DataTransferObject;
|
||||
class TransactionDetailObject implements DataTransferObject
|
||||
{
|
||||
/** @var string|null */
|
||||
private $code;
|
||||
private $reference;
|
||||
|
||||
/** @var string|null */
|
||||
private $name;
|
||||
@@ -20,14 +20,14 @@ class TransactionDetailObject implements DataTransferObject
|
||||
|
||||
/**
|
||||
* TransactionDetailObject constructor.
|
||||
* @param string $code
|
||||
* @param string $reference
|
||||
* @param string $name
|
||||
* @param int $quantity
|
||||
* @param float $price
|
||||
*/
|
||||
public function __construct(?string $code, ?string $name , ?int $quantity, ?float $price)
|
||||
public function __construct(?string $reference, ?string $name , ?int $quantity, ?float $price)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace App\Classes\Modules\Transactions\Processors;
|
||||
|
||||
use App\Classes\Modules\Transactions\Services\CreatesShippingCostTransaction;
|
||||
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
|
||||
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
|
||||
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionDetailObject;
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Classes\ValueObjects\Constants\PaymentMethodType;
|
||||
use App\Models\Order;
|
||||
|
||||
class CreateShippingCostTransactionProcessor
|
||||
{
|
||||
/** @var CreatesShippingCostTransaction */
|
||||
private $createsShippingCostTransaction;
|
||||
|
||||
/** @var GeneratesTransactionBillNumber */
|
||||
private $generatesTransactionBillNumber;
|
||||
|
||||
public function __construct(CreatesShippingCostTransaction $createsShippingCostTransaction, GeneratesTransactionBillNumber $generatesTransactionBillNumber)
|
||||
{
|
||||
$this->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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Transactions\Services;
|
||||
|
||||
use App\Classes\General\Eloquent\AbstractUpdateRecord;
|
||||
use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord;
|
||||
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
|
||||
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionDetailObject;
|
||||
use App\Models\Order;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\TransactionDetail;
|
||||
|
||||
class CreatesShippingCostTransaction extends AbstractUpdateRelationshipRecord
|
||||
{
|
||||
/**
|
||||
* @param TransactionObject $object
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function execute(Order $order, TransactionObject $object, TransactionDetailObject $transactionDetailObject) {
|
||||
$model = new Transaction();
|
||||
$model->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);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -22,4 +22,6 @@ final class TransactionType {
|
||||
|
||||
public const SUPPLIER_DELIVER = 8;
|
||||
|
||||
public const SHIPPING_COST = 9;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Orders;
|
||||
|
||||
use App\Classes\Modules\Orders\ControllersLogic\ShippingCostLogic;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ShippingCostController
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param ShippingCostLogic $logic
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function calculate(Request $request, ShippingCostLogic $logic): JsonResponse
|
||||
{
|
||||
return $logic->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class TransactionDetail extends AbstractModel
|
||||
{
|
||||
protected $table = 'transaction_detail';
|
||||
protected $table = 'transaction_details';
|
||||
|
||||
/**
|
||||
* @return BelongsTo
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
|
||||
class CreateTransactionsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('transactions', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateTransactionDetailsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('transaction_details', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user