mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
70 lines
3.2 KiB
PHP
70 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\PerfexCRM\Services;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use App\Classes\Exceptions\MalformedRequestException;
|
|
use App\Classes\Exceptions\ConnectionErrorException;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Classes\Modules\PerfexCRM\DataTransferObjects\InvoicePerfexCRMObject;
|
|
|
|
class CreatesPerfexCRMInvoice
|
|
{
|
|
/**
|
|
* @param InvoicePerfexCRMObject $invoicePerfexCRMObject
|
|
* @return null|object
|
|
* @throws MalformedRequestException
|
|
*/
|
|
public function execute(InvoicePerfexCRMObject $invoicePerfexCRMObject) {
|
|
try{
|
|
$subtotal = floor($invoicePerfexCRMObject->getSubTotal() * 100) / 100;
|
|
$total = floor($invoicePerfexCRMObject->getTotal() * 100) / 100;
|
|
|
|
$data = [
|
|
'clientid' => $invoicePerfexCRMObject->getClientId(),
|
|
'number' => $invoicePerfexCRMObject->getNumber(),
|
|
'date' => $invoicePerfexCRMObject->getDate(),
|
|
'duedate' => $invoicePerfexCRMObject->getDueDate(),
|
|
'currency' => $invoicePerfexCRMObject->getCurrency(),
|
|
'subtotal' => number_format($subtotal, 2, '.', ''),
|
|
'total' => number_format($total, 2, '.', ''),
|
|
'billing_street' => $invoicePerfexCRMObject->getBillingStreet(),
|
|
'project_id' => $invoicePerfexCRMObject->getProjectId(),
|
|
'allowed_payment_modes[0]' => 1,
|
|
'allowed_payment_modes[1]' => 2,
|
|
];
|
|
|
|
for($count=0; $count < count($invoicePerfexCRMObject->getInvoiceItems()); $count++) {
|
|
$oneItem = [
|
|
"newitems[".$count."][description]" => $invoicePerfexCRMObject->getInvoiceItems()[$count]->description,
|
|
"newitems[".$count."][long_description]" => $invoicePerfexCRMObject->getInvoiceItems()[$count]->longDescription,
|
|
"newitems[".$count."][qty]" => $invoicePerfexCRMObject->getInvoiceItems()[$count]->qty,
|
|
"newitems[".$count."][rate]" => $invoicePerfexCRMObject->getInvoiceItems()[$count]->rate,
|
|
"newitems[".$count."][order]" => $invoicePerfexCRMObject->getInvoiceItems()[$count]->order,
|
|
"newitems[".$count."][unit]" => $invoicePerfexCRMObject->getInvoiceItems()[$count]->unit,
|
|
];
|
|
$data = array_merge($data, $oneItem);
|
|
}
|
|
|
|
$response = Http::asForm()->withHeaders([
|
|
'authtoken' => config('perfexcrm.api_key')])
|
|
->post(config('perfexcrm.base_url').'/api/invoices',$data);
|
|
|
|
if($response->successful()){
|
|
$data = $response->json();
|
|
return (object) $data;
|
|
}else{
|
|
Log::error('CreatesPerfexCRMInvoice: '.$response);
|
|
return null;
|
|
}
|
|
}
|
|
catch(\Illuminate\Http\Client\ConnectionException $exception){
|
|
$error = 'Failed to connect to CRM';
|
|
throw new ConnectionErrorException($error, $exception->getMessage(), "", $exception->getTraceAsString());
|
|
}
|
|
catch(\Exception $exception){
|
|
throw new MalformedRequestException('Unable to get correct response from Perfex CRM server: ' . $exception->getMessage());
|
|
}
|
|
}
|
|
}
|