/
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\Item; use XeroAPI\XeroPHP\Models\Accounting\Items; use XeroAPI\XeroPHP\Models\Accounting\Purchase; class ItemCreated 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("items_log")->info("*****Item JOB started for ".$this->id); if ($this->source == "zoho"){ $this->zoho_to_xero(); } Log::channel("items_log")->info("****Item JOB Completed for ".$this->id); } public function zoho_to_xero(){ try { $item_res = get_zoho_single_record("items", $this->id); if (isset($item_res->code) && $item_res->code == 0 && isset($item_res->message) && $item_res->message="success" && isset($item_res->item)) { $item_zoho = $item_res->item; $store = ""; if(isset($item_zoho->sku) && isset($item_zoho->sku) && !empty($item_zoho->sku)){ if (substr( $item_zoho->sku, 0, 2 ) === "AU"){ $store = "AU"; } elseif (substr( $item_zoho->sku, 0, 2 ) === "NZ"){ $store = "NZ"; } } else{ Log::channel("items_log")->info("Job failed due to: SKU not found. ID:".$this->id); return; } if ($store != "AU" && $store != "NZ"){ Log::channel("items_log")->info("Job failed due to: Invalid SKU: ".$item_zoho->sku.", 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_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; } } } /// Create XERO Account $items = new Items(); $item_1 = new Item(); $item_1->setCode($item_zoho->sku); $item_1->setName($item_zoho->name); $item_1->setDescription($item_zoho->description); $item_1->setPurchaseDescription($item_zoho->purchase_description); if (isset($item_zoho->sales_rate) && !empty($item_zoho->sales_rate)){ $sales_details = new Purchase(); $sales_details->setUnitPrice($item_zoho->sales_rate); $sales_details->setAccountCode(200); if (isset($item_zoho->tax_type) && $item_zoho->tax_type == "tax" && isset($item_zoho->tax_percentage)){ if ($store == "AU") { $sales_details->setTaxType(AU_TAX_TYPE); } elseif ($store == "NZ") { $sales_details->setTaxType(NZ_TAX_TYPE); } } $item_1->setSalesDetails($sales_details); } if (isset($item_zoho->purchase_rate) && !empty($item_zoho->purchase_rate)){ $sales_details = new Purchase(); $sales_details->setUnitPrice($item_zoho->purchase_rate); $sales_details->setCogsAccountCode(310); $item_1->setPurchaseDetails($sales_details); } if(!is_null($item_zoho->sales_rate) && is_null($item_zoho->purchase_rate)){ $item_1->setIsPurchased(false); } $item_1->setInventoryAssetAccountCode(630); $items_arr[] = $item_1; $items->setItems($items_arr); if ($xero_item_id == "") { $contact_created = $apiInstance->createItems(constant("XERO_" . $store . "_TENANT_ID"), $items); $xero_id = $contact_created->getItems()[0]->getItemId(); $zoho_data['id'] = $this->id; $zoho_data['custom_fields'][0]['label'] = "XeroID"; $zoho_data['custom_fields'][0]['value'] = $xero_id; $response = update_zoho_record("items",$this->id,$zoho_data); Log::channel("items_log")->info("Item Successfully created in Xero with ID : ".$this->id); } else { $item_1 = $apiInstance->getItem(constant("XERO_" . $store . "_TENANT_ID"),$xero_item_id); if (count($item_1->getItems()) > 0) { $item_1 = $item_1->getItems()[0]; $zoho_modified_time = Carbon::parse($item_zoho->last_modified_time); $xero_modified_time = Carbon::parse($item_1->getUpdatedDateUtc()); if ($zoho_modified_time->diffInMinutes($xero_modified_time) > 3) { $item_updated = $apiInstance->updateItem(constant("XERO_" . $store . "_TENANT_ID"), $xero_item_id, $items); Log::channel("items_log")->info("Item Successfully updated in Xero with ID : ".$this->id); } } } }elseif(isset($item_res->message)){ Log::info("Item Invalid ID or Due to some thing else record not found. Desc:".$item_res->message); }else{ Log::channel("items_log")->info("Item Invalid ID or Due to some thing else record not found".$this->id); } } catch (\Exception $e){ Log::channel("items_log")->info("Job Failed due to:".$e); } } }