diff --git a/app/Classes/Jobs/CreatePerfexCRMInvoice.php b/app/Classes/Jobs/CreatePerfexCRMInvoice.php new file mode 100644 index 00000000..ace24e26 --- /dev/null +++ b/app/Classes/Jobs/CreatePerfexCRMInvoice.php @@ -0,0 +1,33 @@ +transaction = $transaction; + } + + public function handle() + { + (App()->make(CreatePerfexCRMInvoiceProcessor::class))->execute($this->transaction); + } +} diff --git a/app/Classes/Jobs/CreatePerfexCRMSingleTask.php b/app/Classes/Jobs/CreatePerfexCRMSingleTask.php index 586efe0f..a66fee7b 100644 --- a/app/Classes/Jobs/CreatePerfexCRMSingleTask.php +++ b/app/Classes/Jobs/CreatePerfexCRMSingleTask.php @@ -4,12 +4,12 @@ namespace App\Classes\Jobs; use App\Classes\Modules\PerfexCRM\Processors\CreatePerfexCRMTaskProcessor; use App\Classes\Modules\PerfexCRM\Services\FetchesPerfexCRMLead; +use App\Classes\Modules\PerfexCRM\DataTransferObjects\CreateTaskPerfexCRMObject; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -use App\Classes\Modules\PerfexCRM\DataTransferObjects\CreateTaskPerfexCRMObject; class CreatePerfexCRMSingleTask implements ShouldQueue { diff --git a/app/Classes/Jobs/UpdatePerfexCRMInvoice.php b/app/Classes/Jobs/UpdatePerfexCRMInvoice.php new file mode 100644 index 00000000..8b806b4f --- /dev/null +++ b/app/Classes/Jobs/UpdatePerfexCRMInvoice.php @@ -0,0 +1,76 @@ +updatePerfexCRMInvoiceObject = $updatePerfexCRMInvoiceObject; + } + + public function handle() + { + //get the client id + $customer = (App()->make(FetchesPerfexCRMCustomer::class))->execute($this->updatePerfexCRMInvoiceObject->getEmail()); + $transaction = $this->updatePerfexCRMInvoiceObject->getTransaction(); + + //get the invoice id + $invoiceId = 0; + $invoiceStatus = 0; + $invoice = (App()->make(FetchesPerfexCRMInvoice::class))->execute($customer->userid,"INV-", $transaction->owner->bill_no); + + if(is_null($invoice)){ + $result = (App()->make(CreatePerfexCRMInvoiceProcessor::class))->execute($transaction->owner); + $invoiceId = $result->payload['id']; + } + else{ + $invoiceId = $invoice->id; + $invoiceStatus = $invoice->status; + } + + if($invoiceId != 0 && $invoiceStatus != PerfexCRMInvoiceStatus::PAID){ + + $date = Carbon::parse($transaction->created_at)->format('Y-m-d'); + + $invoicePaymentPerfexCRMObject = new InvoicePaymentPerfexCRMObject( + $invoiceId, + "$transaction->amount", + $date, + 1, + "", + "" + ); + (App()->make(CreatesPerfexCRMInvoicePayment::class))->execute($invoicePaymentPerfexCRMObject); + } + } +} diff --git a/app/Classes/Modules/PerfexCRM/DataTransferObjects/InvoicePerfexCRMObject.php b/app/Classes/Modules/PerfexCRM/DataTransferObjects/InvoicePerfexCRMObject.php index 7e204ff8..f1f21f11 100644 --- a/app/Classes/Modules/PerfexCRM/DataTransferObjects/InvoicePerfexCRMObject.php +++ b/app/Classes/Modules/PerfexCRM/DataTransferObjects/InvoicePerfexCRMObject.php @@ -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; } diff --git a/app/Classes/Modules/PerfexCRM/DataTransferObjects/InvoiceSingleItemPerfexCRMObject.php b/app/Classes/Modules/PerfexCRM/DataTransferObjects/InvoiceSingleItemPerfexCRMObject.php index 4f92895d..7b9428db 100644 --- a/app/Classes/Modules/PerfexCRM/DataTransferObjects/InvoiceSingleItemPerfexCRMObject.php +++ b/app/Classes/Modules/PerfexCRM/DataTransferObjects/InvoiceSingleItemPerfexCRMObject.php @@ -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; } diff --git a/app/Classes/Modules/PerfexCRM/DataTransferObjects/UpdatePerfexCRMInvoiceObject.php b/app/Classes/Modules/PerfexCRM/DataTransferObjects/UpdatePerfexCRMInvoiceObject.php new file mode 100644 index 00000000..a3f4fb0b --- /dev/null +++ b/app/Classes/Modules/PerfexCRM/DataTransferObjects/UpdatePerfexCRMInvoiceObject.php @@ -0,0 +1,38 @@ +email = $email; + $this->transaction = $transaction; + } + + /** + * @return string + */ + public function getEmail(): string + { + return $this->email; + } + + /** + * @return Transaction + */ + public function getTransaction(): Transaction + { + return $this->transaction; + } +} diff --git a/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMInvoiceProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMInvoiceProcessor.php index 7a893bde..a780f303 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMInvoiceProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMInvoiceProcessor.php @@ -2,7 +2,7 @@ namespace App\Classes\Modules\PerfexCRM\Processors; -// use App\Classes\Modules\PerfexCRM\DataTransferObjects\ContactObject; +use App\Models\Transaction; use App\Classes\Modules\PerfexCRM\Services\CreatesPerfexCRMInvoice; use App\Classes\Modules\PerfexCRM\Services\CreatesPerfexCRMInvoicePayment; use App\Classes\Modules\PerfexCRM\Services\ConvertsPerfexCRMLeadToCustomer; @@ -37,28 +37,32 @@ class CreatePerfexCRMInvoiceProcessor } /** - * @param $transaction - * @param $purchaseOrder - * @param $supplier + * @param Transaction $transaction * @return null|object * @throws \App\Classes\Exceptions\MalformedRequestException */ - public function execute($transaction, $purchaseOrder, $supplier) { + public function execute(Transaction $transaction) { $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)); } - $date = Carbon::parse($transaction->booking->created_at)->format('Y-m-d'); - $dueDate = Carbon::parse($transaction->booking->created_at)->format('Y-m-d'); + $date = Carbon::parse($transaction->created_at)->format('Y-m-d'); + $dueDate = Carbon::parse($transaction->created_at)->format('Y-m-d'); $currency = 1; //cief TODO: To look into Malaysia and Chinese currency $subTotal = 0.00; $total = 0.00; + $companyModule = $transaction->owner->owner->companyModule; $billingStreet = ""; - $addresses = $supplier->addresses()->where('billing', '=', true)->first(); + $addresses = $companyModule + ->addresses() + ->where('status', '=', \App\Classes\ValueObjects\Constants\ApprovalStatus::APPROVED) + ->where('type', '=', \App\Classes\ValueObjects\Constants\AddressType::BILLING)->first(); $billingStreet = $billingStreet.$addresses->street_one; $billingStreet = $billingStreet.$addresses->street_two.','; @@ -71,36 +75,23 @@ class CreatePerfexCRMInvoiceProcessor $allowedPaymentModes = []; $invoiceItems = []; - $email = $supplier->employees()->first()->email; - $email = 'dillon37@yahoo.com'; //cief TODO: To be updated to user actual email address + $employee = $companyModule->employees()->first(); + $email = $employee->email; + $result = $this->convertsPerfexCRMLeadToCustomer->execute($email); if(isset($result->payload)){ $clientId = $result->payload['client_id']; } //newitems - foreach ($purchaseOrder->transactionDetails as $key => $transaction_detail){ + foreach ($transaction->transactionDetails as $key => $transaction_detail){ $order = $key + 1; - $stockCode = $transaction_detail->product_code; - $description = $transaction_detail->product_name; + $description = $transaction_detail->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; + $unitPrice = $transaction_detail->price; - //$totalAmount = 0.00; - if($transaction->booking()->first()->fix_currency_id !== 1){ + $subTotal = $transaction_detail->amount; - //$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( @@ -114,16 +105,11 @@ class CreatePerfexCRMInvoiceProcessor 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; - } + $total = $transaction->amount; - //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( $clientId, $number, @@ -140,20 +126,7 @@ 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); - } - - return true; + return $result; } } diff --git a/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessor.php index d011ffb1..2d074624 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessor.php @@ -8,13 +8,15 @@ use App\Classes\Modules\PerfexCRM\Services\Init; use App\Classes\Modules\Companies\Services\FetchesCompany; use App\Classes\Modules\PerfexCRM\DataTransferObjects\UpdatePerfexCRMObject; use App\Classes\Modules\PerfexCRM\DataTransferObjects\InitialPerfexCRMObject; +use App\Classes\Modules\PerfexCRM\DataTransferObjects\UpdatePerfexCRMInvoiceObject; use App\Classes\General\Eloquent\AbstractUpdateRecord; use App\Classes\ValueObjects\Constants\PerfexCRMMilestones; use App\Classes\ValueObjects\Constants\PerfexCRMTasks; -use App\Models\Transaction; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\Jobs\InitializePerfexCRM; +use App\Classes\Jobs\UpdatePerfexCRMInvoice; use App\Classes\Jobs\UpdatePerfexCRM; +use App\Models\Transaction; class TransactionToPerfexCRMProcessor { @@ -83,7 +85,14 @@ class TransactionToPerfexCRMProcessor //$this->initializePerfexCRMProcessor->execute($initialPerfexCRMObject); InitializePerfexCRM::dispatch($initialPerfexCRMObject); + + $updatePerfexCRMInvoiceOject = new UpdatePerfexCRMInvoiceObject( + $contactEmail, + $model, + ); + UpdatePerfexCRMInvoice::dispatch($updatePerfexCRMInvoiceOject); } + return true; } } diff --git a/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoice.php b/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoice.php index 196e3130..b595a86f 100644 --- a/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoice.php +++ b/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoice.php @@ -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, diff --git a/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoicePayment.php b/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoicePayment.php index 035cc1b8..f3e92529 100644 --- a/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoicePayment.php +++ b/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoicePayment.php @@ -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(), diff --git a/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMCustomer.php b/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMCustomer.php new file mode 100644 index 00000000..9f3928f6 --- /dev/null +++ b/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMCustomer.php @@ -0,0 +1,34 @@ + 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()); + } + } +} diff --git a/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMInvoice.php b/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMInvoice.php new file mode 100644 index 00000000..cd7aff30 --- /dev/null +++ b/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMInvoice.php @@ -0,0 +1,36 @@ + 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()); + } + } +} diff --git a/app/Classes/Modules/Transactions/Processors/ApproveShippingInvoiceTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/ApproveShippingInvoiceTransactionProcessor.php index 6fe29c44..e5b7bad0 100644 --- a/app/Classes/Modules/Transactions/Processors/ApproveShippingInvoiceTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/ApproveShippingInvoiceTransactionProcessor.php @@ -16,6 +16,8 @@ use App\Classes\Modules\Documents\Services\CreatesFiles; use App\Classes\Modules\Documents\Services\CreatesDocument; use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; +use App\Classes\Modules\PerfexCRM\Processors\CreatePerfexCRMInvoiceProcessor; +use App\Classes\Jobs\CreatePerfexCRMInvoice; class ApproveShippingInvoiceTransactionProcessor { @@ -29,16 +31,21 @@ class ApproveShippingInvoiceTransactionProcessor /** @var CreatesFiles */ private $createsFiles; + /** @var CreatePerfexCRMInvoiceProcessor */ + private $createPerfexCRMInvoiceProcessor; + /** * @param UpdatesTransactionStatus $updatesTransactionStatus * @param CreatesDocument $createsDocument * @param CreatesFiles $createsFiles + * @param CreatePerfexCRMInvoiceProcessor $createPerfexCRMInvoiceProcessor */ - public function __construct(UpdatesTransactionStatus $updatesTransactionStatus, CreatesDocument $createsDocument, CreatesFiles $createsFiles) + public function __construct(UpdatesTransactionStatus $updatesTransactionStatus, CreatesDocument $createsDocument, CreatesFiles $createsFiles, CreatePerfexCRMInvoiceProcessor $createPerfexCRMInvoiceProcessor) { $this->updatesTransactionStatus = $updatesTransactionStatus; $this->createsDocument = $createsDocument; $this->createsFiles = $createsFiles; + $this->createPerfexCRMInvoiceProcessor = $createPerfexCRMInvoiceProcessor; } @@ -72,6 +79,12 @@ class ApproveShippingInvoiceTransactionProcessor if(app()->environment(['production'])) { $user->notify(new InvoiceIssuedEmail($user, $packingList)); } + + // update perfex crm + if(config('perfexcrm.is_enabled') == 'true'){ + CreatePerfexCRMInvoice::dispatch($invoice_transaction); + } + return; } diff --git a/app/Classes/ValueObjects/Constants/PerfexCRMInvoiceStatus.php b/app/Classes/ValueObjects/Constants/PerfexCRMInvoiceStatus.php new file mode 100644 index 00000000..2e692129 --- /dev/null +++ b/app/Classes/ValueObjects/Constants/PerfexCRMInvoiceStatus.php @@ -0,0 +1,14 @@ +