/
var
/
www
/
html
/
plugin-techloyce
/
app
/
Jobs
/
Upload File
HOME
<?php namespace App\Jobs; use App\Http\Controllers\Xero\AuthorizationController; use Carbon\Carbon; use GuzzleHttp\Client; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use XeroAPI\XeroPHP\Api\AccountingApi; use XeroAPI\XeroPHP\Configuration; use XeroAPI\XeroPHP\Models\Accounting\Address; use XeroAPI\XeroPHP\Models\Accounting\Contact; use XeroAPI\XeroPHP\Models\Accounting\Contacts; use XeroAPI\XeroPHP\Models\Accounting\Invoice; use XeroAPI\XeroPHP\Models\Accounting\Invoices; use XeroAPI\XeroPHP\Models\Accounting\LineItem; use XeroAPI\XeroPHP\Models\Accounting\Phone; class InvoiceCreated implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $id; protected $source; protected $tenant; protected $event_type; /** * Create a new job instance. * * @return void */ public function __construct($id,$source,$tenant,$event_type) { $this->id = $id; $this->source = $source; $this->tenant = $tenant; $this->event_type = $event_type; } /** * Execute the job. * * @return void */ public function handle() { Log::channel("invoices_log")->info("*****Invoice JOB started for ".$this->id); if ($this->source == "zoho"){ $this->zoho_to_xero(); } Log::channel("invoices_log")->info("****Invoice JOB Completed for ".$this->id); } public function zoho_to_xero(){ try { $invoice_res = get_zoho_single_record("invoices", $this->id); if (isset($invoice_res->code) && $invoice_res->code == 0 && isset($invoice_res->message) && $invoice_res->message="success" && isset($invoice_res->invoice)) { $invoice_zoho = $invoice_res->invoice; $customer_id = $invoice_zoho->customer_id; $zoho_contact = get_zoho_single_record("contacts",$customer_id); if (isset($zoho_contact->code) && $zoho_contact->code == 0 && isset($zoho_contact->message) && $zoho_contact->message="success" && isset($zoho_contact->contact)) { $contact_zoho = $zoho_contact->contact; $store = ""; if(isset($contact_zoho->billing_address) && isset($contact_zoho->billing_address->country) && !empty($contact_zoho->billing_address->country)){ if($contact_zoho->billing_address->country == "Australia" || $contact_zoho->billing_address->country == "AU"){ $store = "AU"; }elseif($contact_zoho->billing_address->country == "New Zealand" || $contact_zoho->billing_address->country == "NZ"){ $store = "NZ"; } } if(isset($contact_zoho->shipping_address) && isset($contact_zoho->shipping_address->country) && !empty($contact_zoho->shipping_address->country)){ if($contact_zoho->shipping_address->country == "Australia" || $contact_zoho->shipping_address->country == "AU"){ $store = "AU"; }elseif($contact_zoho->shipping_address->country == "New Zealand" || $contact_zoho->shipping_address->country == "NZ"){ $store = "NZ"; } } if ($store != "AU" && $store != "NZ"){ Log::channel("contacts_log")->info("Job failed due to: Invalid Country. ID:".$this->id); return; } $xer_authorization = new AuthorizationController(); $config = Configuration::getDefaultConfiguration()->setAccessToken((string)$xer_authorization->get_access_token($store)); $config->setHost("https://api.xero.com/api.xro/2.0"); $apiInstance = new AccountingApi( new Client(), $config ); $xero_line_item_arr = array(); foreach ($invoice_zoho->line_items as $line_item) { $item_description = $line_item->description; if ($item_description == '') { $item_description = "no description"; } $item_quantity = $line_item->quantity; $item_rate = $line_item->rate; $item_sku = $line_item->sku; $tax_name = $line_item->tax_name; $account_code = null; if ($tax_name == "AU GST") { $account_code = AU_TAX_TYPE; } else if ($tax_name == "NZ GST") { $account_code = NZ_TAX_TYPE; } ///////////////////////// xero lineItems ////////////////////// $xero_line_item = new LineItem(); $xero_line_item->setItemCode($item_sku); $xero_line_item->setDescription($item_description); $xero_line_item->setQuantity($item_quantity); $xero_line_item->setUnitAmount($item_rate); if ($account_code) $xero_line_item->setTaxType($account_code); $xero_line_item_arr[] = $xero_line_item; } /// Create XERO Account $invoices = new Invoices(); $invoice_1 = new Invoice(); $invoice_1->setType("ACCREC"); $xero_contact = $this->contact_zoho_to_xero($contact_zoho,$store); $invoice_1->setContact($xero_contact); $invoice_1->setDate($invoice_zoho->date); $invoice_1->setReference($invoice_zoho->reference_number); if (isset($invoice_zoho->invoice_number) && !empty($invoice_zoho->invoice_number)) { $bill_number = $invoice_zoho->invoice_number; } else { $bill_number = $invoice_zoho->salesorder_number; } $invoice_1->setInvoiceNumber($bill_number); $invoice_1->setDueDate($invoice_zoho->due_date); $invoice_1->setStatus("SUBMITTED"); $invoice_1->setLineItems($xero_line_item_arr); $invoices_arr[] = $invoice_1; $invoices->setInvoices($invoices_arr); $xero_invoice_id = ""; if(isset($invoice_zoho->custom_fields) && count($invoice_zoho->custom_fields) > 0){ $custom_filed = $invoice_zoho->custom_fields; foreach ($custom_filed as $value) { if($value->label == "XeroID"){ $xero_invoice_id = $value->value; break; } } } if ($xero_invoice_id == "") { $invoice_created = $apiInstance->createInvoices(constant("XERO_" . $store . "_TENANT_ID"), $invoices); $xero_id = $invoice_created->getInvoices()[0]->getInvoiceId(); $zoho_data['id'] = $this->id; $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $xero_id; $response = update_zoho_record("invoices", $this->id, $zoho_data); Log::channel("invoices_log")->info("Invoice Successfully created in Xero with ID : " . $this->id); } else { $invoice_1 = $apiInstance->getInvoice(constant("XERO_" . $store . "_TENANT_ID"), $xero_invoice_id); if (count($invoice_1->getInvoices()) > 0) { $invoice_1 = $invoice_1->getInvoices()[0]; $zoho_modified_time = Carbon::parse($invoice_zoho->last_modified_time); $xero_modified_time = Carbon::parse($invoice_1->getUpdatedDateUtc()); if ($zoho_modified_time->diffInMinutes($xero_modified_time) > 3) { $invoice_updated = $apiInstance->updateInvoice(constant("XERO_" . $store . "_TENANT_ID"), $xero_invoice_id, $invoices); Log::channel("invoices_log")->info("Invoice Successfully updated in Xero with ID : " . $this->id); } } } }else{ Log::channel("invoices_log")->info("Invoice Customer ID or Due to some thing else record not found. Desc:".$zoho_contact->message); } }elseif(isset($invoice_res->message)){ Log::channel("invoices_log")->info("Invoice Invalid ID or Due to some thing else record not found. Desc:".$invoice_res->message); }else{ Log::channel("invoices_log")->info("Invoice Invalid ID or Due to some thing else record not found".$this->id); } } catch (\Exception $e){ Log::channel("invoices_log")->info("Job Failed due to:".$e); } } private function contact_zoho_to_xero($contact_zoho,$store){ try { $xer_authorization = new AuthorizationController(); $config = Configuration::getDefaultConfiguration()->setAccessToken((string)$xer_authorization->get_access_token($store)); $config->setHost("https://api.xero.com/api.xro/2.0"); $apiInstance = new AccountingApi( new Client(), $config ); $xero_contact_id = ""; if(isset($contact_zoho->custom_fields) && count($contact_zoho->custom_fields) > 0){ $custom_filed = $contact_zoho->custom_fields; foreach ($custom_filed as $value) { if($value->label == "XeroID"){ $xero_contact_id = $value->value; break; } } } /// Create XERO Account $contacts = new Contacts(); $contact_1 = new Contact(); $contact_1->setName($contact_zoho->contact_name); $contact_1->setFirstName($contact_zoho->first_name); $contact_1->setLastName($contact_zoho->last_name); $contact_1->setEmailAddress($contact_zoho->email); $address_1 = new Address(); $address_1->setAddressType("STREET"); $address_1->setAttentionTo($contact_zoho->billing_address->attention); $address_1->setAddressLine1($contact_zoho->billing_address->address); $address_1->setAddressLine2($contact_zoho->billing_address->street2); $address_1->setCity($contact_zoho->billing_address->city); $address_1->setRegion($contact_zoho->billing_address->state); $address_1->setPostalCode($contact_zoho->billing_address->zip); $address_1->setCountry($contact_zoho->billing_address->country); $address_2 = new Address(); $address_2->setAddressType("POBOX"); $address_2->setAttentionTo($contact_zoho->shipping_address->attention); $address_2->setAddressLine1($contact_zoho->shipping_address->address); $address_2->setAddressLine2($contact_zoho->shipping_address->street2); $address_2->setCity($contact_zoho->shipping_address->city); $address_2->setRegion($contact_zoho->shipping_address->state); $address_2->setPostalCode($contact_zoho->shipping_address->zip); $address_2->setCountry($contact_zoho->shipping_address->country); $contact_1->setAddresses(array($address_1,$address_2)); $contact_phone = $contact_zoho->phone; if (empty($contact_phone)){ $contact_phone = $contact_zoho->billing_address->phone; if (empty($contact_phone)) { $contact_phone = $contact_zoho->shipping_address->phone; } } if (!empty($contact_phone)) { $phones_arr = array(); $phone_1 = new Phone(); $phone_1->setPhoneType("DEFAULT"); $phone_1->setPhoneNumber($contact_phone); $phones_arr[] = $phone_1; if($contact_zoho->mobile && !empty($contact_zoho->mobile)){ $phone_2 = new Phone(); $phone_2->setPhoneType("MOBILE"); $phone_2->setPhoneNumber($contact_zoho->mobile); $phones_arr[] = $phone_2; } $contact_1->setPhones($phones_arr); } if($contact_zoho->contact_type =="customer"){ $contact_1->setIsSupplier(false); $contact_1->setIsCustomer(true); } else if($contact_zoho->contact_type =="vendor"){ $contact_1->setIsSupplier(true); $contact_1->setIsCustomer(false); } $contact_arr[] = $contact_1; $contacts->setContacts($contact_arr); if ($xero_contact_id == "") { $contact_created = $apiInstance->createContacts(constant("XERO_" . $store . "_TENANT_ID"), $contacts); $xero_id = $contact_created->getContacts()[0]->getContactId(); $zoho_data['id'] = $contact_zoho->contact_id; $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $xero_id; update_zoho_record("contacts",$contact_zoho->contact_id,$zoho_data); return $contact_created->getContacts()[0]; } else { $contact_1 = $apiInstance->getContact(constant("XERO_" . $store . "_TENANT_ID"),$xero_contact_id); if (count($contact_1->getContacts()) > 0) { $contact_1 = $contact_1->getContacts()[0]; return $contact_1; } } } catch (\Exception $e){ return null; } } }