CRM create invoice

This commit is contained in:
Dillon
2023-03-05 20:26:20 +08:00
parent 8b6f297977
commit 914b961d66
10 changed files with 165 additions and 29 deletions
@@ -0,0 +1,42 @@
<?php
namespace App\Classes\Jobs;
use App\Classes\Modules\PerfexCRM\Processors\CreatePerfexCRMInvoiceProcessor;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class CreatePerfexCRMInvoice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** @var $transaction*/
private $transaction;
/** @var $purchaseOrder*/
private $purchaseOrder;
/** @var $supplier*/
private $supplier;
/**
* CreatePerfexCRMInvoice constructor.
* @param $transaction
* @param $purchaseOrder
* @param $supplier
*/
public function __construct($transaction, $purchaseOrder, $supplier)
{
$this->transaction = $transaction;
$this->purchaseOrder = $purchaseOrder;
$this->supplier = $supplier;
}
public function handle()
{
(App()->make(CreatePerfexCRMInvoiceProcessor::class))->execute($this->transaction, $this->purchaseOrder, $this->supplier);
}
}
@@ -7,7 +7,7 @@ use App\Classes\General\Interfaces\DataTransferObject;
class InvoicePerfexCRMObject implements DataTransferObject
{
/** @var int */
/** @var string */
private $clientId;
/** @var string */
@@ -56,9 +56,9 @@ class InvoicePerfexCRMObject implements DataTransferObject
}
/**
* @return int
* @return string
*/
public function getClientId(): int
public function getClientId(): string
{
return $this->clientId;
}
@@ -14,7 +14,7 @@ class InvoiceSingleItemPerfexCRMObject implements DataTransferObject
/** @var string */
public $longDescription;
/** @var int */
/** @var float */
public $qty;
/** @var float */
@@ -26,7 +26,7 @@ class InvoiceSingleItemPerfexCRMObject implements DataTransferObject
/** @var string */
public $unit;
public function __construct(string $description, string $longDescription, int $qty, float $rate, int $order, string $unit)
public function __construct(string $description, string $longDescription, float $qty, float $rate, int $order, string $unit)
{
$this->description = $description;
$this->longDescription = $longDescription;
@@ -53,9 +53,9 @@ class InvoiceSingleItemPerfexCRMObject implements DataTransferObject
}
/**
* @return int
* @return float
*/
public function getQty(): int
public function getQty(): float
{
return $this->qty;
}
@@ -44,12 +44,16 @@ class CreatePerfexCRMInvoiceProcessor
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function execute($transaction, $purchaseOrder, $supplier) {
$clientId = "";
$number = $transaction->bill_no;
$prefix = "INV-";
//This will remove the prefix if prefix already exist in the string
if (substr($number, 0, strlen($prefix)) == $prefix) {
$number = substr($number, strlen($prefix));
}
$number = 'EXC-'.$number;
$date = Carbon::parse($transaction->booking->created_at)->format('Y-m-d');
$dueDate = Carbon::parse($transaction->booking->created_at)->format('Y-m-d');
@@ -72,7 +76,7 @@ class CreatePerfexCRMInvoiceProcessor
$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'];
@@ -94,12 +98,12 @@ class CreatePerfexCRMInvoiceProcessor
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;
$subTotal += number_format((1/$transaction->currency_rate) * $transaction_detail->price, 2,'.','') * $transaction_detail->quantity;
}
else
{
//$totalAmount = (float)number_format($transaction_detail->price, 2,'.','')*$transaction_detail->quantity;
$subTotal += $transaction_detail->price * $transaction_detail->quantity;
$subTotal += number_format($transaction_detail->price, 2,'.','') * $transaction_detail->quantity;
}
//string $description, string $longDescription, int $qty, int $rate, int $order, string $unit
@@ -121,7 +125,8 @@ class CreatePerfexCRMInvoiceProcessor
$total = $transaction->amount + $transaction->service_charge + $transaction->tax;
}
//cief TODO: To be updated, temporarily hardcoded allow payment modes
//This setting is similar to Setup > Leads > Sources, Setup > Leads > Statuses
//Can be found at Finance > Payment Modes
array_push($allowedPaymentModes, 1, 2);
$invoicePerfexCRMObject = new InvoicePerfexCRMObject(
@@ -140,18 +145,23 @@ class CreatePerfexCRMInvoiceProcessor
$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);
if(!is_null($result))
{
if($result->payload['id']){
$invoicePaymentPerfexCRMObject = new InvoicePaymentPerfexCRMObject(
$result->payload['id'],
$total,
$date,
1,
"",
""
);
$this->createsPerfexCRMInvoicePayment->execute($invoicePaymentPerfexCRMObject);
}
}
//else: logs will record the following:
//"status":false,"error":{"number":"The Invoice number is already in use"},"message":"<p>The Invoice number is already in use<\/p>"}
return true;
}
@@ -22,8 +22,8 @@ class CreatesPerfexCRMInvoice
'date' => $invoicePerfexCRMObject->getDate(),
'duedate' => $invoicePerfexCRMObject->getDueDate(),
'currency' => $invoicePerfexCRMObject->getCurrency(),
'subtotal' => $invoicePerfexCRMObject->getSubTotal(),
'total' => $invoicePerfexCRMObject->getTotal(),
'subtotal' => number_format($invoicePerfexCRMObject->getSubTotal(), 2, '.', ''),
'total' => number_format($invoicePerfexCRMObject->getTotal(), 2, '.', ''),
'billing_street' => $invoicePerfexCRMObject->getBillingStreet(),
'project_id' => $invoicePerfexCRMObject->getProjectId(),
'allowed_payment_modes[0]' => 1,
@@ -18,7 +18,7 @@ class CreatesPerfexCRMInvoicePayment
try{
$data = [
'invoiceid' => $invoicePaymentPerfexCRMObject->getInvoiceId(),
'amount' => $invoicePaymentPerfexCRMObject->getAmount(),
'amount' => number_format($invoicePaymentPerfexCRMObject->getAmount(), 2, '.', ''),
'date' => $invoicePaymentPerfexCRMObject->getDate(),
'paymentmode' => $invoicePaymentPerfexCRMObject->getPaymentMode(),
'transactionid' => $invoicePaymentPerfexCRMObject->getTransactionId(),
@@ -0,0 +1,34 @@
<?php
namespace App\Classes\Modules\PerfexCRM\Services;
use Illuminate\Support\Facades\Http;
use App\Classes\Exceptions\MalformedRequestException;
use Illuminate\Support\Facades\Log;
class FetchesPerfexCRMCustomer
{
/**
* @param string $email
* @return null|object
* @throws MalformedRequestException
*/
public function execute(string $email) {
try{
$response = Http::withHeaders([
'authtoken' => config('perfexcrm.api_key'),])
->get(config('perfexcrm.base_url').'/api/customers/byemail/'.$email);
if($response->successful()){
$data = $response->json();
return (object) $data;
}else{
Log::error($response);
return null;
}
}catch(\Exception $exception){
throw new MalformedRequestException('Unable to get correct response from Perfex CRM server: ' . $exception->getMessage());
}
}
}
@@ -0,0 +1,36 @@
<?php
namespace App\Classes\Modules\PerfexCRM\Services;
use Illuminate\Support\Facades\Http;
use App\Classes\Exceptions\MalformedRequestException;
use Illuminate\Support\Facades\Log;
class FetchesPerfexCRMInvoice
{
/**
* @param string $clientId
* @param string $invoicePrefix
* @param string $invoiceNumber
* @return null|object
* @throws MalformedRequestException
*/
public function execute(string $clientId, string $invoicePrefix, string $invoiceNumber) {
try{
$response = Http::withHeaders([
'authtoken' => config('perfexcrm.api_key'),])
->get(config('perfexcrm.base_url').'/api/invoices/customsearch/'.$clientId.'/'.$invoicePrefix.'/'.$invoiceNumber);
if($response->successful()){
$data = $response->json();
return (object) $data;
}else{
Log::error($response);
return null;
}
}catch(\Exception $exception){
throw new MalformedRequestException('Unable to get correct response from Perfex CRM server: ' . $exception->getMessage());
}
}
}
@@ -19,6 +19,7 @@ use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\Jobs\CreatePerfexCRMInvoice;
use App\Models\Booking;
use App\Models\SegmentConstant;
@@ -199,10 +200,9 @@ class CreateInvoiceTransactionProcessor
$this->invoiceDocumentProcessor->execute($supplier_deliver_order_transaction, $purchaseOrder, $supplier, DocumentType::SUPPLIER_DELIVER_ORDER);
// update perfex crm
// if(config('perfexcrm.is_enabled') == 'true'){
// {
// $this->createPerfexCRMInvoiceProcessor->execute($invoice_transaction, $purchaseOrder, $supplier);
// }
if(config('perfexcrm.is_enabled') == 'true'){
CreatePerfexCRMInvoice::dispatch($invoice_transaction, $purchaseOrder, $supplier);
}
$this->updatesBookingStatus->execute($booking, ApprovalStatus::COMPLETED);
}
@@ -0,0 +1,14 @@
<?php
namespace App\Classes\ValueObjects\Constants;
final class PerfexCRMInvoiceStatus {
public const UNPAID = 1;
public const PAID = 2;
public const PARTIALLY_PAID = 3;
public const OVERDUE = 4;
}