mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
160 lines
6.2 KiB
PHP
160 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\PerfexCRM\Processors;
|
|
|
|
// use App\Classes\Modules\PerfexCRM\DataTransferObjects\ContactObject;
|
|
use App\Classes\Modules\PerfexCRM\Services\CreatesPerfexCRMInvoice;
|
|
use App\Classes\Modules\PerfexCRM\Services\CreatesPerfexCRMInvoicePayment;
|
|
use App\Classes\Modules\PerfexCRM\Services\ConvertsPerfexCRMLeadToCustomer;
|
|
use App\Classes\Modules\PerfexCRM\DataTransferObjects\InvoicePerfexCRMObject;
|
|
use App\Classes\Modules\PerfexCRM\DataTransferObjects\InvoicePaymentPerfexCRMObject;
|
|
use App\Classes\Modules\PerfexCRM\DataTransferObjects\InvoiceSingleItemPerfexCRMObject;
|
|
use Carbon\Carbon;
|
|
|
|
class CreatePerfexCRMInvoiceProcessor
|
|
{
|
|
|
|
/** @var CreatesPerfexCRMInvoice */
|
|
private $createsPerfexCRMInvoice;
|
|
|
|
/** @var CreatesPerfexCRMInvoicePayment */
|
|
private $createsPerfexCRMInvoicePayment;
|
|
|
|
/** @var ConvertsPerfexCRMLeadToCustomer */
|
|
private $convertsPerfexCRMLeadToCustomer;
|
|
|
|
/**
|
|
* CreatePerfexCRMInvoiceProcessor constructor.
|
|
* @param CreatesPerfexCRMInvoice $createsPerfexCRMInvoice
|
|
*/
|
|
public function __construct(CreatesPerfexCRMInvoice $createsPerfexCRMInvoice,
|
|
CreatesPerfexCRMInvoicePayment $createsPerfexCRMInvoicePayment,
|
|
ConvertsPerfexCRMLeadToCustomer $convertsPerfexCRMLeadToCustomer)
|
|
{
|
|
$this->createsPerfexCRMInvoice = $createsPerfexCRMInvoice;
|
|
$this->createsPerfexCRMInvoicePayment = $createsPerfexCRMInvoicePayment;
|
|
$this->convertsPerfexCRMLeadToCustomer = $convertsPerfexCRMLeadToCustomer;
|
|
}
|
|
|
|
/**
|
|
* @param $transaction
|
|
* @param $purchaseOrder
|
|
* @param $supplier
|
|
* @return null|object
|
|
* @throws \App\Classes\Exceptions\MalformedRequestException
|
|
*/
|
|
public function execute($transaction, $purchaseOrder, $supplier) {
|
|
$clientId = "";
|
|
$number = $transaction->bill_no;
|
|
$prefix = "INV-";
|
|
if (substr($number, 0, strlen($prefix)) == $prefix) {
|
|
$number = substr($number, strlen($prefix));
|
|
}
|
|
|
|
$date = Carbon::parse($transaction->booking->created_at)->format('Y-m-d');
|
|
$dueDate = Carbon::parse($transaction->booking->created_at)->format('Y-m-d');
|
|
$currency = 1; //cief TODO: To look into Malaysia and Chinese currency
|
|
$subTotal = 0.00;
|
|
$total = 0.00;
|
|
|
|
$billingStreet = "";
|
|
$addresses = $supplier->addresses()->where('billing', '=', true)->first();
|
|
|
|
$billingStreet = $billingStreet.$addresses->street_one;
|
|
$billingStreet = $billingStreet.$addresses->street_two.',';
|
|
$billingStreet = $billingStreet.$addresses->district()->first()->name.',';
|
|
$billingStreet = $billingStreet.$addresses->postcode;
|
|
$billingStreet = $billingStreet.$addresses->state()->first()->name.',';
|
|
$billingStreet = $billingStreet.$addresses->country()->first()->name;
|
|
|
|
$projectId = "";
|
|
$allowedPaymentModes = [];
|
|
$invoiceItems = [];
|
|
|
|
$email = $supplier->employees()->first()->email;
|
|
$email = 'dillon37@yahoo.com'; //cief TODO: To be updated to user actual email address
|
|
$result = $this->convertsPerfexCRMLeadToCustomer->execute($email);
|
|
if(isset($result->payload)){
|
|
$clientId = $result->payload['client_id'];
|
|
}
|
|
|
|
//newitems
|
|
foreach ($purchaseOrder->transactionDetails as $key => $transaction_detail){
|
|
$order = $key + 1;
|
|
$stockCode = $transaction_detail->product_code;
|
|
$description = $transaction_detail->product_name;
|
|
$quantity = $transaction_detail->quantity;
|
|
$unitPrice = 0.00;
|
|
if($transaction->booking()->first()->fix_currency_id !== 1)
|
|
$unitPrice = (1/$transaction->currency_rate) * $transaction_detail->price;
|
|
else
|
|
$unitPrice = $transaction_detail->price;
|
|
|
|
//$totalAmount = 0.00;
|
|
if($transaction->booking()->first()->fix_currency_id !== 1){
|
|
|
|
//$totalAmount = (float)number_format( (1/$transaction->currency_rate) * $transaction_detail->price, 2,'.','')*$transaction_detail->quantity;
|
|
$subTotal += (1/$transaction->currency_rate) * $transaction_detail->price * $transaction_detail->quantity;
|
|
}
|
|
else
|
|
{
|
|
//$totalAmount = (float)number_format($transaction_detail->price, 2,'.','')*$transaction_detail->quantity;
|
|
$subTotal += $transaction_detail->price * $transaction_detail->quantity;
|
|
}
|
|
|
|
//string $description, string $longDescription, int $qty, int $rate, int $order, string $unit
|
|
$invoiceSingleItem = new InvoiceSingleItemPerfexCRMObject(
|
|
$description,
|
|
"",
|
|
$quantity,
|
|
$unitPrice,
|
|
$order,
|
|
""
|
|
);
|
|
array_push($invoiceItems, $invoiceSingleItem);
|
|
}
|
|
|
|
if($transaction->booking()->first()->fix_currency_id !== 1){
|
|
$total = ((1/$transaction->currency_rate) * $transaction->amount) + $transaction->service_charge + $transaction->tax;
|
|
}
|
|
else{
|
|
$total = $transaction->amount + $transaction->service_charge + $transaction->tax;
|
|
}
|
|
|
|
//cief TODO: To be updated, temporarily hardcoded allow payment modes
|
|
array_push($allowedPaymentModes, 1, 2);
|
|
|
|
$invoicePerfexCRMObject = new InvoicePerfexCRMObject(
|
|
$clientId,
|
|
$number,
|
|
$date,
|
|
$dueDate,
|
|
$currency,
|
|
$subTotal,
|
|
$total,
|
|
$billingStreet,
|
|
$projectId,
|
|
$allowedPaymentModes,
|
|
$invoiceItems
|
|
);
|
|
|
|
$result = $this->createsPerfexCRMInvoice->execute($invoicePerfexCRMObject);
|
|
|
|
//If there is a checking whether an payment to an invoice need to be generated, do it here
|
|
if(true && $result->payload['id']){
|
|
$invoicePaymentPerfexCRMObject = new InvoicePaymentPerfexCRMObject(
|
|
$result->payload['id'],
|
|
$total,
|
|
$date,
|
|
1,
|
|
"",
|
|
""
|
|
);
|
|
$this->createsPerfexCRMInvoicePayment->execute($invoicePaymentPerfexCRMObject);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
}
|