/
zohosubscription
/
app
/
Console
/
Commands
/
Upload File
HOME
<?php namespace App\Console\Commands; use App\Http\Controllers\Xero\AuthorizationController; use Carbon\Carbon; use GuzzleHttp\Client; use Illuminate\Console\Command; 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\Phone; class SyncVendors extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'sync:vendors'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { Log::channel("contacts_log")->info("*****Vendors Scheduler started."); $criteria = "sort_column=last_modified_time&sort_order=D&per_page=20"; $zoho_vendors = search_zoho_records("vendors",$criteria); if (isset($zoho_vendors->code) && $zoho_vendors->code == 0 && isset($zoho_vendors->contacts)){ $vendors = $zoho_vendors->contacts; foreach ($vendors as $vendor){ if (Carbon::parse($vendor->last_modified_time)->gt(Carbon::now()->subMinutes(35))) { $zoho_contact = get_zoho_single_record("contacts", $vendor->contact_id); if (isset($zoho_contact->code) && $zoho_contact->code == 0 && 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 ($store == "" && 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") { continue; } $this->contact_zoho_to_xero($contact_zoho, $store); } } } } Log::channel("contacts_log")->info("*****Vendors Scheduler completed."); } 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); Log::channel("contacts_log")->info("*****Vendors Created."); } else { $contact_updated = $apiInstance->updateContact(constant("XERO_" . $store . "_TENANT_ID"), $xero_contact_id, $contacts); $xero_id = $contact_updated->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); //Log::channel("contacts_log")->info("*****Vendors Updated."); } } catch (\Exception $e){ Log::info($e); } } }