/
zoho-bitbuck
/
zoho
/
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\Item; use XeroAPI\XeroPHP\Models\Accounting\Items; use XeroAPI\XeroPHP\Models\Accounting\LineItem; use XeroAPI\XeroPHP\Models\Accounting\Phone; use XeroAPI\XeroPHP\Models\Accounting\Purchase; use XeroAPI\XeroPHP\Models\Accounting\Payment; use XeroAPI\XeroPHP\Models\Accounting\Payments; 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('JOb source: '.$this->source); if ($this->source == "zoho"){ Log::channel("invoices_log")->info("*****Invoice JOB started for ".$this->id); $this->zoho_to_xero(); Log::channel("invoices_log")->info("****Invoice JOB Completed for ".$this->id); }/**/ if ($this->source == "xero"){ Log::channel("invoices_log")->info("*****Xero Invoice Webhook Triggered with ID " . $this->id . " Source: " . strtoupper($this->source)); $this->xero_to_zoho(); Log::channel("invoices_log")->info("****Xero Invoice Webhook Triggered Completed for ".$this->id); } } public function zoho_to_xero(){ $invoice_res = get_zoho_subscriptions_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_subscriptions_single_record("customers",$customer_id); if (isset($zoho_contact->code) && $zoho_contact->code == 0 && isset($zoho_contact->message) && $zoho_contact->message="success" && isset($zoho_contact->customer)) { $contact_zoho = $zoho_contact->customer; $xer_authorization = new AuthorizationController(); $config = Configuration::getDefaultConfiguration()->setAccessToken((string)$xer_authorization->get_access_token_zoho_subscriptions()); $config->setHost("https://api.xero.com/api.xro/2.0"); $apiInstance = new AccountingApi( new Client(), $config ); /**/ $xero_line_item_arr = array(); foreach ($invoice_zoho->invoice_items as $line_item) { $item_code = isset($line_item->code)?$line_item->code:''; $pricing_scheme = ''; if(isset($item_code) && $item_code!=''){ $item_res = $this->item_zoho_to_xero($line_item,$apiInstance); $item_res = json_decode($item_res); $account_code = isset($item_res->account_code)?$item_res->account_code:''; $xero_item_id = isset($item_res->xero_item_id)?$item_res->xero_item_id:''; $pricing_scheme = isset($item_res->pricing_scheme)?$item_res->pricing_scheme:''; $item_zoho = isset($item_res->item_zoho)?$item_res->item_zoho:''; if($pricing_scheme != 'tier'){ if($xero_item_id==''){ continue; } } } $item_description = $line_item->description; if ($item_description == '') { $item_description = "no description"; } $item_quantity = $line_item->quantity; $item_rate = $line_item->price; $tax_name = $line_item->tax_name; $tax_code = $line_item->tax_type; if ($tax_code == "tax") { //Xero accept this for Sales(200).... $tax_code = 'OUTPUT'; } ///////////////////////// xero lineItems ////////////////////// $xero_line_item = new LineItem(); $xero_line_item->setQuantity($item_quantity); if($pricing_scheme == 'tier'){ //$xero_line_item->setItemCode($item_code); $unit_amt = $item_rate/$item_quantity; //setLineAmount $xero_line_item->setLineAmount($item_rate); $xero_line_item->setUnitAmount($unit_amt); //dd($line_item,$item_zoho->price_brackets); $price_bracket_str = ''; foreach($item_zoho->price_brackets as $price_bracket){ if(isset($price_bracket->end_quantity) && $price_bracket->end_quantity!=''){ $end_quantity = $price_bracket->end_quantity; }else{ $end_quantity = '∞'; } $price_bracket_str .= ' Quantity '.$price_bracket->start_quantity.' To '.$end_quantity.' Price is '. $price_bracket->price; } $unit_amt = number_format((float)$unit_amt, 2, '.', ''); $item_description .= ' ('.$price_bracket_str.' and Avg Unit Amount is '.$unit_amt.')'; }else{ $xero_line_item->setItemCode($item_code); $xero_line_item->setUnitAmount($item_rate); } $xero_line_item->setDescription($item_description); if ($tax_code) { $xero_line_item->setTaxType($tax_code); } if(isset($account_code) && $account_code!=''){ $xero_line_item->setAccountCode($account_code); }else{ $xero_line_item->setAccountCode('200'); $account_code = '200'; } if(isset($line_item->discount_amount) && $line_item->discount_amount > 0){ //$xero_line_item->setDiscountRate($invoice_zoho->discount_percent); $xero_line_item->setDiscountAmount($line_item->discount_amount); } $xero_line_item_arr[] = $xero_line_item; } //creating contact... $xero_contact = $this->contact_zoho_to_xero($contact_zoho); //$xero_contact = json_decode($xero_contact); /// Create XERO Account $invoices = new Invoices(); $invoice_1 = new Invoice(); $invoice_1->setType("ACCREC"); $invoice_1->setContact($xero_contact);//$xero_contact->ContactID $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); /**/ if($invoice_zoho->status=='sent'){ $invoice_1->setStatus('AUTHORISED'); }elseif($invoice_zoho->status=='paid'){ $invoice_1->setStatus('AUTHORISED'); //$invoice_1->setAmountPaid($invoice_zoho->payment_made); //$invoice_1->setAmountDue($invoice_zoho->balance); }elseif($invoice_zoho->status=='partially_paid'){ $invoice_1->setStatus('AUTHORISED'); //$invoice_1->setAmountPaid($invoice_zoho->payment_made); //$invoice_1->setAmountDue($invoice_zoho->balance); }elseif($invoice_zoho->status=='overdue'){ $invoice_1->setStatus('AUTHORISED'); //$invoice_1->setAmountPaid($invoice_zoho->payment_made); //$invoice_1->setAmountDue($invoice_zoho->balance); } $invoice_1->setLineItems($xero_line_item_arr); $invoice_1->setAmountPaid($invoice_zoho->payment_made); $invoice_1->setAmountDue($invoice_zoho->balance); $invoice_1->setTotal($invoice_zoho->total); $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 == "") { try{ $invoice_created = $apiInstance->createInvoices(XERO_TENANT_ID, $invoices); $invoice = $invoice_created->getInvoices()[0]; $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; Log::channel("invoices_log")->info("Invoice Successfully created in Xero with ID : " . $xero_id); $response = update_zoho_subscriptions_record("invoices", $this->id, $zoho_data); Log::channel("invoices_log")->info("*****Invoice Payment JOB started for ".$xero_id); $this->invoice_payments($invoice,$invoice_zoho,$apiInstance,$xero_id); Log::channel("invoices_log")->info("****Invoice Payment JOB Completed for ".$xero_id); }catch (\Exception $e){ Log::channel("invoices_log")->info("Invoice Created Failed due to: ".$e->getResponseBody()); } } else { $invoice_1 = $apiInstance->getInvoice(XERO_TENANT_ID,$xero_invoice_id); $invoice = $invoice_1->getInvoices()[0]; if (count($invoice_1->getInvoices()) > 0) { $invoice_1 = $invoice_1->getInvoices()[0]; $zoho_modified_time = Carbon::parse($invoice_zoho->updated_time); $xero_modified_time = Carbon::parse($invoice_1->getUpdatedDateUtc()); if ($zoho_modified_time->diffInMinutes($xero_modified_time) > 1) { try{ $invoice_updated = $apiInstance->updateInvoice(XERO_TENANT_ID, $xero_invoice_id, $invoices); Log::channel("invoices_log")->info("Invoice Successfully updated in Xero with ID : " . $this->id); Log::channel("invoices_log")->info("*****Invoice Payment JOB started for ".$xero_invoice_id); $this->invoice_payments($invoice,$invoice_zoho,$apiInstance,$xero_invoice_id); Log::channel("invoices_log")->info("****Invoice Payment JOB Completed for ".$xero_invoice_id); }catch (\Exception $e){ Log::channel("invoices_log")->info("Invoice Update Failed due to: ".$e->getResponseBody()); //Log::channel("invoices_log")->info("Invoice Update Failed data : ".print_r(json_encode($e))); } } } } }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); } } public function xero_to_zoho(){ $xero_invoice_id = $this->id; $source = $this->source; $invoice_res = search_zoho_subscriptions_records("invoices", 'cf_xeroid='.$xero_invoice_id); //dd($invoice_res); if (isset($invoice_res->code) && $invoice_res->code == 0 && isset($invoice_res->message) && $invoice_res->message="success" && isset($invoice_res->invoices)) { if(isset($invoice_res->invoices[0])){ $invoice_zoho = $invoice_res->invoices[0]; $customer_id = $invoice_zoho->customer_id; $xer_authorization = new AuthorizationController(); $config = Configuration::getDefaultConfiguration()->setAccessToken((string)$xer_authorization->get_access_token_zoho_subscriptions()); $config->setHost("https://api.xero.com/api.xro/2.0"); $apiInstance = new AccountingApi( new Client(), $config ); if ($xero_invoice_id != "") { $invoice_1 = $apiInstance->getInvoice(XERO_TENANT_ID,$xero_invoice_id); if (count($invoice_1->getInvoices()) > 0){ $invoice = $invoice_1->getInvoices()[0]; $zoho_modified_time = Carbon::parse($invoice_zoho->updated_time); $xero_modified_time = Carbon::parse($invoice->getUpdatedDateUtc()); if ($zoho_modified_time->diffInMinutes($xero_modified_time) > 1) { $XeroPayments = $invoice->getPayments(); $XeroPaymentsStatus = $invoice->getStatus(); if(isset($XeroPayments) && $XeroPayments!=''){ foreach($XeroPayments as $payment){ $zoho_data['customer_id'] = $customer_id; $zoho_data['payment_mode'] = 'banktransfer'; $zoho_data['amount'] = $payment->getAmount(); $zoho_data['reference_number'] = $payment->getReference(); $zoho_data_invoices = array(); $zoho_data_invoices['invoice_id'] = $invoice_zoho->invoice_id; $zoho_data_invoices['invoice_number'] = $invoice_zoho->number; $zoho_data_invoices['invoice_amount'] = $invoice_zoho->total; //$zoho_data_invoices['balance_amount'] = $invoice_zoho->amount_due; $zoho_data_invoices['amount_applied'] = $payment->getAmount(); $zoho_data['invoices'] = array($zoho_data_invoices); $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $payment->getPaymentId(); Log::channel("invoices_log")->info("Xero Invoice Payment ID : " . $payment->getPaymentId()); Log::channel("invoices_log")->info("Xero Invoice Payment Amount : " . $payment->getAmount()); /**/ $response = insert_zoho_subscriptions_records("payments", $zoho_data); if (isset($response->code) && $response->code == 0 && isset($response->message) && $response->message="success" && isset($response->payment)) { Log::channel("invoices_log")->info("Xero Invoice Payment Successfully created in Zoho with ID : " . $response->payment->payment_id); }else{ Log::channel("invoices_log")->info($response->message); } } }else{ Log::channel("invoices_log")->info("No Payment Due in Xero!"); } } } } }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); } }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".$source_id); } } private function clean_me($str){ // strip out all whitespace $str = preg_replace('/\s*/', '', $str); // convert the string to all lowercase $str = strtolower($str); return $str; } private function invoice_payments($invoice,$invoice_zoho,$apiInstance,$xero_invoice_id){ if($invoice_zoho->status=='sent'){ $Status = 'SUBMITTED'; Log::channel("invoices_log")->info("Invoice Status :".$invoice_zoho->status.""); }elseif($invoice_zoho->status=='paid' || $invoice_zoho->status=='partially_paid' || $invoice_zoho->status=='overdue'){ Log::channel("invoices_log")->info("Invoice Status :".$invoice_zoho->status.""); if(isset($invoice_zoho->payments)){ Log::channel("invoices_log")->info("Invoice Total :".$invoice_zoho->total.""); Log::channel("invoices_log")->info("Invoice Payment Made :".$invoice_zoho->payment_made.""); Log::channel("invoices_log")->info("Invoice Balance :".$invoice_zoho->balance.""); Log::channel("invoices_log")->info("Invoice Payments :".json_encode($invoice_zoho->payments).""); foreach($invoice_zoho->payments as $payment){ $id = $payment->payment_id; $payments_res = get_zoho_subscriptions_single_record("payments", $id); $payment_id = ''; $payment_xero_id = ''; if(isset($payments_res->custom_fields) && count($payments_res->custom_fields) > 0){ $custom_filed = $payments_res->custom_fields; foreach ($custom_filed as $value) { if($value->label == "XeroID"){ $payment_xero_id = $value->value; break; } } } if($payment_xero_id==""){ $amount = $payment->amount; $payment_id = $id; Log::channel("invoices_log")->info("Payment Amount :".$payment->amount.""); Log::channel("invoices_log")->info("Payment Id :".$payment_id.""); } } } if((isset($amount) && $amount!='') && (isset($payment_id) && $payment_id!='')){ $Status = 'AUTHORISED'; $payments = new Payments(); $newPayment = new Payment(); $newPayment->setInvoice($invoice); $newPayment->setDate(Carbon::now()->format("Y-m-d")); $newPayment->setAmount($amount); $newPayment->setStatus($Status); //$newPayment->setCode('101'); 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 == "Xero Bank Accounts"){ $xero_bank_account = $value->value; break; } } } $xero_bank_account = $this->clean_me($xero_bank_account); Log::channel("invoices_log")->info("Invoice Payment Send To :".$xero_bank_account.""); switch ($xero_bank_account) { case $this->clean_me("30KFT Business"): $account_id = "F2FBA6A7-8935-4CBC-96E2-99213B5C25A1"; break; case $this->clean_me("Direct Debit - AUD"): $account_id = "bd9f4b03-5073-483e-953c-be7dd7db2d6e"; break; case $this->clean_me("Rental Deposit"): $account_id = "f0dfa376-e62d-4e2e-8160-8b1c496b1d93"; break; case $this->clean_me("VirginAust Velocity Flyer Card"): $account_id = "428298f4-d4ef-4866-9702-0d3b5b617a8a"; break; case $this->clean_me("30KFT Saving"): $account_id = "c38b8f5d-cb62-4510-b63c-d8c927c4dfa5"; break; default: $account_id = ''; } Log::channel("invoices_log")->info("Invoice Payment Send To Account ID :".$account_id.""); $accounts = $apiInstance->getAccount(XERO_TENANT_ID, $account_id); $newPayment->setAccount($accounts->getAccounts()[0]); $payment_arr[] = $newPayment; $payments->setPayments($payment_arr); try{ $payment_created = $apiInstance->createPayments(XERO_TENANT_ID, $payments); $xero_id = $payment_created->getPayments()[0]->getPaymentId(); Log::channel("invoices_log")->info("Payment Successfully created in Xero with ID : " . $xero_id); $zoho_data['id'] = $payment_id; $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $xero_id; if(isset($payment_id) && $payment_id!=''){ $response = update_zoho_subscriptions_record("payments", $payment_id, $zoho_data); } Log::channel("invoices_log")->info("Payment ID for Xero :".$xero_id.""); Log::channel("invoices_log")->info("Payment Added for Xero Invoice :".$xero_invoice_id.""); }catch (\Exception $e){ Log::channel("invoices_log")->info("Payment created Failed due to: ".$e->getResponseBody()); } }else{ Log::channel("invoices_log")->info("No Payment Created!"); } } } private function contact_zoho_to_xero($contact_zoho){ $xer_authorization = new AuthorizationController(); $config = Configuration::getDefaultConfiguration()->setAccessToken((string)$xer_authorization->get_access_token_zoho_subscriptions()); $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->display_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->street); $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->street); $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); } $contact_1->setIsCustomer(true); /*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 == "") { try { $contact_created = $apiInstance->createContacts(XERO_TENANT_ID, $contacts); if ($contact_created->getContacts()[0]->getHasValidationErrors()) { $msg = $contact_created->getContacts()[0]->getValidationErrors()[0]["message"]; Log::channel("invoices_log")->info($msg); if(strpos($msg, 'The contact name must be unique across all active contacts') !== false){ try { $where = 'EmailAddress=="'.$contact_created->getContacts()[0]->getEmailAddress().'"'; // string | Filter by an any element $already_contact = $apiInstance->getContacts(XERO_TENANT_ID, $if_modified_since = '', $where, $order ='', $i_ds ='', 1, $include_archived = false); $xero_id = $already_contact->getContacts()[0]->getContactId(); } catch (\Exception $e) { Log::channel("invoices_log")->info("Contact created Failed due to: ".$e->getResponseBody()); //echo 'Exception when calling AccountingApi->getContacts: ', $e->getMessage(), PHP_EOL; //echo 'Exception when calling AccountingApi->getContacts: ', $e->getResponseBody(), PHP_EOL; } //dd($contact_created->getContacts()[0]->getEmailAddress()); } }else{ $xero_id = $contact_created->getContacts()[0]->getContactId(); } //$xero_id = $contact_created->getContacts()[0]->getContactId(); //$zoho_data['id'] = $contact_zoho->customer_id; $zoho_data = array(); $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = (string) $xero_id; $upd_response = update_zoho_subscriptions_record("customers",$contact_zoho->customer_id,$zoho_data); if(isset($already_contact)){ $contact_1 = $already_contact->getContacts()[0]; }else{ $contact_1 = $contact_created->getContacts()[0]; } return $contact_1; } catch (\Exception $e){ Log::channel("invoices_log")->info("Contact created Failed due to: ".$e->getResponseBody()); } }else { try { $contact_1 = $apiInstance->getContact(XERO_TENANT_ID,$xero_contact_id); if (count($contact_1->getContacts()) > 0) { $contact_1 = $contact_1->getContacts()[0]; return $contact_1; } } catch (\Exception $e){ Log::channel("invoices_log")->info("Get Contact Failed due to: ".$e->getResponseBody()); } } } private function item_zoho_to_xero($line_item,$apiInstance){ $item_res = get_zoho_subscriptions_single_record("plans", $line_item->code); /**/ if (isset($item_res->code) && $item_res->code == 0 && isset($item_res->message) && $item_res->message = "success" && isset($item_res->plan)) { $item_zoho = $item_res->plan; // $account_code = '200'; if(isset($item_zoho->custom_fields) && count($item_zoho->custom_fields) > 0){ $custom_filed = $item_zoho->custom_fields; foreach ($custom_filed as $value) { if($value->label == "GL Code"){ $account_code = $value->value; break; } } } if($item_zoho->pricing_scheme=='tier'){ return json_encode(array('pricing_scheme'=>$item_zoho->pricing_scheme,'item_zoho'=>$item_zoho,'account_code'=>$account_code)); } /// Create XERO ITEM $items = new Items(); $item_1 = new Item(); $item_1->setCode($item_zoho->plan_code); $item_1->setName(substr($item_zoho->name,0,50)); $item_1->setDescription($item_zoho->description); if (isset($item_zoho->recurring_price) && !empty($item_zoho->recurring_price)) { $sales_details = new Purchase(); $sales_details->setUnitPrice($item_zoho->recurring_price); if (isset($item_zoho->tax_type) && isset($item_zoho->tax_percentage)) { $sales_details->setTaxType($item_zoho->tax_type); } if(isset($account_code) && $account_code!=''){ $sales_details->setAccountCode($account_code); }else{ $sales_details->setAccountCode('200'); $account_code = '200'; } $item_1->setSalesDetails($sales_details); } $item_1->setIsPurchased(false); $item_1->setIsSold(true); $items_arr[] = $item_1; $items->setItems($items_arr); $xero_item_id = ""; if(isset($item_zoho->custom_fields) && count($item_zoho->custom_fields) > 0){ $custom_filed = $item_zoho->custom_fields; foreach ($custom_filed as $value) { if($value->label == "XeroID"){ $xero_item_id = $value->value; break; } } } if ($xero_item_id == "") { try { $item_created = $apiInstance->createItems(XERO_TENANT_ID, $items); $xero_item_id = $item_created->getItems()[0]->getItemId(); $zoho_data['code'] = $line_item->code; $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $xero_item_id; update_zoho_subscriptions_record("plans", $line_item->code, $zoho_data); } catch (\Exception $e){ Log::channel("invoices_log")->info("Item created Failed due to: ".$e->getResponseBody()); } }else{ try { $xero_item = $apiInstance->getItem(XERO_TENANT_ID,$xero_item_id); $xero_item_id = $xero_item->getItems()[0]->getItemId(); $xero_item_id = $xero_item_id; }catch (\Exception $e){ if($e->getCode()==404){ try{ $item_created = $apiInstance->createItems(XERO_TENANT_ID, $items); $xero_item_id = $item_created->getItems()[0]->getItemId(); $zoho_data['code'] = $line_item->code; $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $xero_item_id; update_zoho_subscriptions_record("plans", $line_item->code, $zoho_data); }catch (\Exception $e){ Log::channel("invoices_log")->info("Item created Failed due to: ".$e->getResponseBody()); } } } } }else{ $item_res = get_zoho_subscriptions_single_record("addons", $line_item->code); //xero_item_id = ''; $item_zoho = $item_res->addon; // $account_code = '200'; if(isset($item_zoho->custom_fields) && count($item_zoho->custom_fields) > 0){ $custom_filed = $item_zoho->custom_fields; foreach ($custom_filed as $value) { if($value->label == "GL Code"){ $account_code = $value->value; break; } } } if($item_zoho->pricing_scheme=='tier'){ return json_encode(array('pricing_scheme'=>$item_zoho->pricing_scheme,'item_zoho'=>$item_zoho,'account_code'=>$account_code)); } /// Create XERO ITEM $items = new Items(); $item_1 = new Item(); $item_1->setCode($item_zoho->addon_code); $item_1->setName(substr($item_zoho->name,0,50)); $item_1->setDescription($item_zoho->description); if (isset($item_zoho->price_brackets[0]->price) && !empty($item_zoho->price_brackets[0]->price)) { $sales_details = new Purchase(); $sales_details->setUnitPrice($item_zoho->price_brackets[0]->price); if (isset($item_zoho->tax_type) && isset($item_zoho->tax_percentage)) { $sales_details->setTaxType($item_zoho->tax_type); } if(isset($account_code) && $account_code!=''){ $sales_details->setAccountCode($account_code); }else{ $sales_details->setAccountCode('200'); $account_code = '200'; } $item_1->setSalesDetails($sales_details); } $item_1->setIsPurchased(false); $item_1->setIsSold(true); $items_arr[] = $item_1; $items->setItems($items_arr); $xero_item_id = ""; if(isset($item_zoho->custom_fields) && count($item_zoho->custom_fields) > 0){ $custom_filed = $item_zoho->custom_fields; foreach ($custom_filed as $value) { if($value->label == "XeroID"){ $xero_item_id = $value->value; break; } } } if ($xero_item_id == "") { try { $item_created = $apiInstance->createItems(XERO_TENANT_ID, $items); $xero_item_id = $item_created->getItems()[0]->getItemId(); $zoho_data['code'] = $line_item->code; $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $xero_item_id; update_zoho_subscriptions_record("addons", $line_item->code, $zoho_data); } catch (\Exception $e){ Log::channel("invoices_log")->info("Item created Failed due to: ".$e->getResponseBody()); } }else{ try { $xero_item = $apiInstance->getItem(XERO_TENANT_ID,$xero_item_id); $xero_item_id = $xero_item->getItems()[0]->getItemId(); $xero_item_id = $xero_item_id; }catch (\Exception $e){ if($e->getCode()==404){ try { $item_created = $apiInstance->createItems(XERO_TENANT_ID, $items); $xero_item_id = $item_created->getItems()[0]->getItemId(); $zoho_data['code'] = $line_item->code; $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $xero_item_id; update_zoho_subscriptions_record("addons", $line_item->code, $zoho_data); }catch (\Exception $e){ Log::channel("invoices_log")->info("Item created Failed due to: ".$e->getResponseBody()); } } } } } return json_encode(array('xero_item_id'=>$xero_item_id,'account_code'=>$account_code)); } }