mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/portal.git
synced 2026-08-19 04:23:59 +00:00
213 lines
8.2 KiB
PHP
213 lines
8.2 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Classes\Modules\Transactions\ControllersLogic;
|
|
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
|
|
use App\Classes\Modules\SegmentConstants\Services\FetchesSegmentConstant;
|
|
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
|
|
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
|
|
use App\Classes\Modules\Transactions\Services\CreatesTransactionDetail;
|
|
use App\Classes\Modules\Documents\Services\CreatesDocument;
|
|
use App\Classes\Modules\Documents\Services\CreatesFiles;
|
|
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
|
|
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionDetailObject;
|
|
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
|
|
|
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Classes\ValueObjects\Constants\PaymentMethodType;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\DocumentType;
|
|
use App\Classes\ValueObjects\Constants\PackageType;
|
|
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
|
use App\Classes\ValueObjects\Constants\TransactionDetailType;
|
|
|
|
use App\Models\Document;
|
|
use App\Models\Transaction;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Meneses\LaravelMpdf\Facades\LaravelMpdf;
|
|
|
|
class CreateShippingInvoiceTransactionLogic extends AbstractControllerLogic
|
|
{
|
|
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification():array {
|
|
return [
|
|
'title' => 'Create Supplier Transactions',
|
|
'message' => 'You have successfully created currency supplier transactions'
|
|
];
|
|
}
|
|
|
|
/** @var FetchesPackingList */
|
|
private $fetchesPackingList;
|
|
|
|
/** @var FetchesSegmentConstant */
|
|
private $fetchesSegmentConstant;
|
|
|
|
/** @var GeneratesTransactionBillNumber */
|
|
private $generatesTransactionBillNumber;
|
|
|
|
/** @var CreatesTransaction */
|
|
private $createsTransaction;
|
|
|
|
/** @var CreatesTransactionDetail */
|
|
private $createsTransactionDetail;
|
|
|
|
/** @var CreatesDocument */
|
|
private $createsDocument;
|
|
|
|
/** @var CreatesFiles */
|
|
private $createsFile;
|
|
|
|
public function __construct(
|
|
FetchesPackingList $fetchesPackingList,
|
|
FetchesSegmentConstant $fetchesSegmentConstant,
|
|
GeneratesTransactionBillNumber $generatesTransactionBillNumber,
|
|
CreatesTransaction $createsTransaction,
|
|
CreatesTransactionDetail $createsTransactionDetail,
|
|
CreatesDocument $createsDocument,
|
|
CreatesFiles $createsFile
|
|
)
|
|
{
|
|
|
|
$this->fetchesPackingList = $fetchesPackingList;
|
|
$this->fetchesSegmentConstant = $fetchesSegmentConstant;
|
|
$this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
|
|
$this->createsTransaction = $createsTransaction;
|
|
$this->createsTransactionDetail = $createsTransactionDetail;
|
|
$this->createsDocument = $createsDocument;
|
|
$this->createsFile = $createsFile;
|
|
}
|
|
|
|
public function logic(Request $request) : JsonResponse
|
|
{
|
|
$packing_list = $this->fetchesPackingList->execute(['id' => $request->input('packing_list_id')]);
|
|
|
|
$cbm = $packing_list->packages->where('type', '!=', PackageType::OVER_WEIGHT)->sum(function($package) {
|
|
return ($package->width / 100) * ($package->height / 100) *($package->length / 100) * ($package->quantity);
|
|
});
|
|
|
|
$over_weight_cbm = $packing_list->packages->where('type', PackageType::OVER_WEIGHT)->sum(function($package) {
|
|
return ($package->width / 100) * ($package->height / 100) *($package->length / 100) * ($package->quantity);
|
|
});
|
|
|
|
$order = $packing_list->owner;
|
|
$address = $order->addresses()->first();
|
|
|
|
$base_price_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::BASE_PRICE]);
|
|
$warehouse_rate_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::WAREHOUSE_RATE]);
|
|
$state_rate_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::STATE_RATE]);
|
|
$center_postcode_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::CENTER_POSTCODE]);
|
|
$outstation_postcode_constant = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => SegmentConstants::OUTSTATION_POSTCODE]);
|
|
|
|
$base_price = 0;
|
|
$warehouse_rate = 0;
|
|
$state_rate = 0;
|
|
$state_select = '';
|
|
|
|
$base_price = $this->getConstantByKey($base_price_constant, date('Y-m-d'));
|
|
dd($base_price);
|
|
|
|
$warehouseId = $order->orderRoles()->where('role_id', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->company_module_id;
|
|
$warehouse_rate = $this->getConstantByKey($warehouse_rate_constant, $warehouseId)->amount;
|
|
|
|
$stateId = $address->state_id;
|
|
$state_rate_constant = $this->getConstantByKey($state_rate_constant, $stateId);
|
|
|
|
// get $state_rate
|
|
$postcode = $address->postcode;
|
|
$this->checkPostcodeExistInConstant($center_postcode_constant, $postcode) === true ? $state_select = 'center' : '' ;
|
|
$this->checkPostcodeExistInConstant($outstation_postcode_constant, $postcode) === true ? $state_select = 'outstation' : '' ;
|
|
$state_rate_constant = (array)$state_rate_constant;
|
|
$state_rate = $state_rate_constant[$state_select];
|
|
|
|
$price_cbm = $base_price + $warehouse_rate + $state_rate;
|
|
$total_cbm = $price_cbm * ($cbm + $over_weight_cbm);
|
|
|
|
$billNumber = $this->generatesTransactionBillNumber->execute('SHIP-');
|
|
|
|
$object = new TransactionObject(
|
|
$billNumber,
|
|
TransactionType::SHIPPING_INVOICE,
|
|
1,
|
|
$order->company_module_id,
|
|
1,
|
|
PaymentMethodType::CASH,
|
|
$total_cbm,
|
|
$total_cbm,
|
|
1,
|
|
1,
|
|
0,
|
|
0,
|
|
0,
|
|
null,
|
|
ApprovalStatus::PENDING_SUBMISSION
|
|
);
|
|
|
|
/** @var Transaction $invoice_transaction */
|
|
$invoice_transaction = $this->createsTransaction->execute($packing_list, $object);
|
|
|
|
$object_detail = new TransactionDetailObject(
|
|
'SHIPPING_FEE',
|
|
TransactionDetailType::SHIPPING_FEE,
|
|
$cbm,
|
|
$price_cbm
|
|
);
|
|
|
|
$this->createsTransactionDetail->execute($invoice_transaction, $object_detail);
|
|
|
|
if ($over_weight_cbm > 0) {
|
|
$object_detail = new TransactionDetailObject(
|
|
'OVER_WEIGHT_CHARGES',
|
|
TransactionDetailType::OVER_WEIGHT_CHARGES,
|
|
$over_weight_cbm,
|
|
$price_cbm
|
|
);
|
|
|
|
$this->createsTransactionDetail->execute($invoice_transaction, $object_detail);
|
|
}
|
|
|
|
$transaction_invoice_pdf = LaravelMpdf::loadView('pages.pdfs.shipping_invoice', ['invoice_transaction' => $invoice_transaction]);
|
|
|
|
$document_object = new DocumentObject(
|
|
DocumentType::SHIPPING_INVOICE,
|
|
[chunk_split('data:application/pdf;base64,'.base64_encode($transaction_invoice_pdf->output()))],
|
|
'',
|
|
ApprovalStatus::COMPLETED,
|
|
'shipping_invoice'
|
|
);
|
|
|
|
/** @var Document $document */
|
|
$document = $this->createsDocument->execute($invoice_transaction, $document_object);
|
|
|
|
$this->createsFile->execute($document, $document_object);
|
|
|
|
return $this->response([]);
|
|
}
|
|
|
|
function getConstantByKey($segmentConstantObject, $key) {
|
|
if ($segmentConstantObject) {
|
|
$base_rate = (array) $segmentConstantObject->value;
|
|
$base_rate = array_key_exists($key, $base_rate) === true ? $base_rate[$key] : 0;
|
|
return $base_rate;
|
|
}
|
|
}
|
|
|
|
function checkPostcodeExistInConstant($segmentConstantObject, $postcode) {
|
|
if ($segmentConstantObject) {
|
|
$segmentConstantObject = $segmentConstantObject->value;
|
|
return in_array($postcode, $segmentConstantObject);
|
|
}
|
|
}
|
|
|
|
}
|