/
var
/
www
/
html
/
plugin-techloyce
/
Modules
/
Accountings
/
Services
/
Upload File
HOME
<?php namespace Modules\Accountings\Services; use Carbon\Carbon; use GuzzleHttp\Client; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; use Modules\Accountings\Contracts\SubscriptionFlowContract; use Modules\Accountings\Entities\Accounting; class SubscriptionFlowService implements SubscriptionFlowContract { public $request; public $response; public function __construct() { $this->response = ['status' => false]; } public function authorize(Request $request) { $this->request = $request; Session::put("ClientID", $request->sourceAdditionalData['ClientID'] ?? null); Session::put("ClientSecret", $request->sourceAdditionalData['ClientSecret'] ?? null); Session::put("BaseUri", $request->sourceAdditionalData['BaseUri'] ?? null); $this->response['status'] = true; return $this; } public function authorize_callback(Request $request) { $client_id = Session::get("ClientID"); $client_secret = Session::get("ClientSecret"); $base_uri = Session::get("BaseUri"); try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/oauth/token'; $params['form_params'] = array('client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'client_credentials'); $response = $client->post($URI, $params); $response = json_decode($response->getBody()); $this->response['access_token'] = $response->access_token; $this->response['expires_in'] = $response->expires_in; $this->response['extra_information'] = array( "ClientID" => Session::get("ClientID"), "ClientSecret" => Session::get("ClientSecret"), "BaseUri" => Session::get("BaseUri"), ); $this->response['status'] = true; } catch (\Exception $e) { $this->response['status'] = false; } return $this; } public function get_access_token($auth) { if ($auth->integration_source == "SubscriptionFlow") { $client_id = $auth->source_extra_information['ClientID']; $client_secret = $auth->source_extra_information['ClientSecret']; $base_uri = $auth->source_extra_information['BaseUri']; $expires_in = $auth->source_expires_in; $access_token = $auth->source_access_token; if (Carbon::parse($auth->source_token_updated_at)->addSeconds($expires_in)->subMinute()->lte(Carbon::now())) { try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/oauth/token'; $params['form_params'] = array('client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'client_credentials'); $response = $client->post($URI, $params); $response = json_decode($response->getBody()); $auth->source_access_token = $response->access_token; $auth->source_expires_in = $response->expires_in; $auth->source_token_updated_at = Carbon::now()->format("Y-m-d H:i:s"); $auth->save(); $data['status'] = true; $data['access_token'] = $auth->source_access_token; $data['base_uri'] = $base_uri; } catch (\Exception $e) { $data['status'] = false; $data['message'] = "Authentication Failed!"; } } else { $data['status'] = true; $data['access_token'] = $access_token; $data['base_uri'] = $base_uri; } } elseif ($auth->integration_destination == "SubscriptionFlow") { $client_id = $auth->destination_extra_information['ClientID']; $client_secret = $auth->destination_extra_information['ClientSecret']; $base_uri = $auth->destination_extra_information['BaseUri']; $expires_in = $auth->destination_expires_in; $access_token = $auth->destination_access_token; if (Carbon::parse($auth->destination_token_updated_at)->addSeconds($expires_in)->subMinute()->lte(Carbon::now())) { try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/oauth/token'; $params['form_params'] = array('client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'client_credentials'); $response = $client->post($URI, $params); $response = json_decode($response->getBody()); $auth->destination_access_token = $response->access_token; $auth->destination_expires_in = $response->expires_in; $auth->destination_token_updated_at = Carbon::now()->format("Y-m-d H:i:s"); $auth->save(); $data['status'] = true; $data['access_token'] = $auth->destination_access_token; $data['base_uri'] = $base_uri; } catch (\Exception $e) { $data['status'] = false; $data['message'] = "Authentication Failed!"; } } else { $data['status'] = true; $data['access_token'] = $access_token; $data['base_uri'] = $base_uri; } } return $data; } public function get_customer($id, $auth) { $data = $this->get_access_token($auth); if ($data['status']) { $base_uri = $data['base_uri']; $access_token = $data['access_token']; try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/api/v1/customers/' . $id; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response = $client->get($URI, $params); $response = json_decode($response->getBody()); if (isset($response->data->id)) { $customer_record = $response->data->attributes; //Emails $URI = $base_uri . '/api/v1/customers/' . $id . "/link/email_addresses"; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response_emails = $client->get($URI, $params); $response_emails = json_decode($response_emails->getBody()); $email = ""; if (isset($response_emails->data)) { foreach ($response_emails->data as $email_data) { if ($email_data->primary == 1) { $email = $email_data->email; break; } } } //Contacts $URI = $base_uri . '/api/v1/customers/' . $id . "/link/contacts"; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response_contacts = $client->get($URI, $params); $response_contacts = json_decode($response_contacts->getBody()); $first_name = ""; $last_name = ""; if (isset($response_contacts->data)) { foreach ($response_contacts->data as $contacts) { if ($contacts->is_primary == 1) { $first_name = $contacts->first_name; $last_name = $contacts->last_name; break; } } } $customer['id'] = $customer_record->id; $customer['name'] = $customer_record->name; $customer['contact_first_name'] = $first_name; $customer['contact_last_name'] = $last_name; $customer['name'] = $customer_record->name; $customer['email'] = $email; $customer['phone_number'] = $customer_record->phone_number; $customer['customer_number'] = $customer_record->customer_number; $customer['billing_address_1'] = $customer_record->billing_address_1; $customer['billing_address_2'] = $customer_record->billing_address_2; $customer['billing_city'] = $customer_record->billing_city; $customer['billing_state'] = $customer_record->billing_state; $customer['billing_postal_code'] = $customer_record->billing_postal_code; $customer['billing_country'] = $customer_record->billing_country; $customer['shipping_address_1'] = $customer_record->shipping_address_1; $customer['shipping_address_2'] = $customer_record->shipping_address_2; $customer['shipping_city'] = $customer_record->shipping_city; $customer['shipping_state'] = $customer_record->shipping_state; $customer['shipping_postal_code'] = $customer_record->shipping_postal_code; $customer['shipping_country'] = $customer_record->shipping_country; $customer['notes'] = $customer_record->notes; if (isset($customer_record->accounting_resource_id)) $customer['resource_id'] = $customer_record->accounting_resource_id; $this->response['status'] = true; $this->response['data'] = $customer; } else { $this->response['status'] = false; } } catch (\Exception $e) { $this->response['status'] = false; } } else { $this->response['status'] = false; $this->response['message'] = "Authentication Failed!"; } return $this; } public function update_customer_resource_id($id, $auth, $customer_data) { $data = $this->get_access_token($auth); if ($data['status'] && isset($customer_data['data']['resource_id']) && $customer_data['data']['resource_id'] != "00000000-0000-0000-0000-000000000000") { $base_uri = $data['base_uri']; $access_token = $data['access_token']; try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/api/v1/customers/' . $id; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array("accounting_resource_id" => $customer_data['data']['resource_id']); $response = $client->put($URI, $params); $response = json_decode($response->getBody()); } catch (\Exception $e) { $this->response['status'] = false; } } else { $this->response['status'] = false; $this->response['message'] = "Authentication Failed!"; } return $this; } public function get_invoice($id, $auth) { $data = $this->get_access_token($auth); if ($data['status']) { $base_uri = $data['base_uri']; $access_token = $data['access_token']; try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/api/v1/invoices/' . $id; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response = $client->get($URI, $params); $response = json_decode($response->getBody()); if (isset($response->data->id) && $response->data->attributes->name != "TBD") { $invoice_record = $response->data->attributes; $invoice['id'] = $invoice_record->id; $invoice['number'] = $invoice_record->name; $invoice['date'] = $invoice_record->invoice_date; $invoice['due_date'] = $invoice_record->due_date; $invoice['terms'] = $invoice_record->terms; $invoice['status'] = $invoice_record->status; $invoice['discount'] = $invoice_record->discount_value; $invoice['tax_amount'] = $invoice_record->tax_amount; $invoice['total_amount'] = $invoice_record->total_amount; $invoice['received_amount'] = $invoice_record->received_payment; $invoice['opening_balance'] = $invoice_record->opening_balance; $invoice['closing_balance'] = $invoice_record->closing_balance; if (isset($invoice_record->accounting_resource_id)) $invoice['resource_id'] = $invoice_record->accounting_resource_id; $customer = null; if (isset($response->data->relationships->customer_id->id)) { $customer = $this->get_customer($response->data->relationships->customer_id->id, $auth); if ($this->response['status']) $customer = $this->response['data']; } $invoice['customer'] = $customer; //Invoice Plans $URI = $base_uri . '/api/v1/invoices/' . $id . "/link/plans"; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response_plans = $client->get($URI, $params); $response_plans = json_decode($response_plans->getBody()); $line_items = array(); $item_counter = 0; if (isset($response_plans->data)) { foreach ($response_plans->data as $plan) { $URI = $base_uri . '/api/v1/orderplans/' . $plan->id . "/link/invoice_prices"; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response_line_items = $client->get($URI, $params); $response_line_items = json_decode($response_line_items->getBody()); if (isset($response_line_items->data)) { foreach ($response_line_items->data as $line) { if (isset($line->plan_price_id) && !empty($line->plan_price_id)) { $URI = $base_uri . '/api/v1/plan-prices/' . $line->plan_price_id; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response_charge = $client->get($URI, $params); $response_charge = json_decode($response_charge->getBody()); if (isset($response_charge->data->id)) { $plan_price = $response_charge->data->attributes; $line_items[$item_counter]['name'] = $plan_price->name; $line_items[$item_counter]['description'] = $plan_price->description; } } $line_items[$item_counter]['product_name'] = $plan->product_name; $line_items[$item_counter]['product_description'] = $plan->product_description; $line_items[$item_counter]['plan_name'] = $plan->plan_name; $line_items[$item_counter]['plan_description'] = $plan->plan_description; $line_items[$item_counter]['price'] = $line->list_price; $line_items[$item_counter]['quantity'] = $line->quantity; $line_items[$item_counter]['service_from'] = $line->service_from; $line_items[$item_counter]['service_to'] = $line->service_to; $line_items[$item_counter]['discount'] = $line->discount; $line_items[$item_counter]['tax'] = $line->tax; $line_items[$item_counter]['account_code'] = $line->accounting_account_code; $item_counter++; } } } } $invoice['line_items'] = $line_items; $this->response['status'] = true; $this->response['data'] = $invoice; } else { $this->response['status'] = false; $this->response['message'] = "Project Invoice cannot be created"; } } catch (\Exception $e) { $this->response['status'] = false; $this->response['message'] = $e->getMessage(); } } else { $this->response['status'] = false; $this->response['message'] = "Authentication Failed!"; } return $this; } public function update_invoice_resource_id($id, $auth, $invoice_data) { $data = $this->get_access_token($auth); if ($data['status'] && isset($invoice_data['data']['resource_id']) && $invoice_data['data']['resource_id'] != "00000000-0000-0000-0000-000000000000") { $base_uri = $data['base_uri']; $access_token = $data['access_token']; try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/api/v1/invoices/' . $id; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array("accounting_resource_id" => $invoice_data['data']['resource_id']); $response = $client->put($URI, $params); $response = json_decode($response->getBody()); } catch (\Exception $e) { $this->response['status'] = false; } } else { $this->response['status'] = false; $this->response['message'] = "Authentication Failed!"; } return $this; } public function get_invoice_payments($id, $auth) { $data = $this->get_access_token($auth); if ($data['status']) { $base_uri = $data['base_uri']; $access_token = $data['access_token']; try { //Invoice Transactions $client = new Client(); $URI = $base_uri . '/api/v1/invoices/' . $id . "/link/transactions"; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response_invoices = $client->get($URI, $params); $response_invoices = json_decode($response_invoices->getBody()); $transactions = array(); $item_counter = 0; if (isset($response_invoices->data)) { foreach ($response_invoices->data as $key => $transaction) { if (isset($transaction->status) && $transaction->status == "Paid" && $transaction->transaction_category == "Payment" && isset($transaction->id) && isset($transaction->pivot_amount) && $transaction->pivot_amount > 0) { $transactions[$item_counter]['id'] = $transaction->id; $transactions[$item_counter]['title'] = $transaction->name; $transactions[$item_counter]['number'] = $transaction->number; $transactions[$item_counter]['date'] = $transaction->date; $transactions[$item_counter]['type'] = $transaction->type; $transactions[$item_counter]['transaction_category'] = $transaction->transaction_category; $transactions[$item_counter]['status'] = $transaction->status; $transactions[$item_counter]['source'] = $transaction->cash_or_card; $transactions[$item_counter]['amount'] = $transaction->pivot_amount; $transactions[$item_counter]['reference'] = $transaction->reference; $transactions[$item_counter]['description'] = $transaction->description; $transactions[$item_counter]['transaction_id'] = $transaction->transaction_id; $transactions[$item_counter]['account_code'] = $transaction->accounting_account_code; if (isset($transaction->accounting_resource_id)) $transactions[$item_counter]['resource_id'] = $transaction->accounting_resource_id; $item_counter++; } } } $this->response['status'] = true; $this->response['data'] = $transactions; } catch (\Exception $e) { $this->response['status'] = false; $this->response['message'] = $e->getMessage(); } } else { $this->response['status'] = false; $this->response['message'] = "Authentication Failed!"; } return $this; } public function get_transaction($id, $auth) { $data = $this->get_access_token($auth); if ($data['status']) { $base_uri = $data['base_uri']; $access_token = $data['access_token']; try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/api/v1/transactions/' . $id; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response = $client->get($URI, $params); $response = json_decode($response->getBody()); $transaction_record = $response->data->attributes; $transaction['id'] = $transaction_record->id; $transaction['title'] = $transaction_record->name; $transaction['number'] = $transaction_record->number; $transaction['date'] = $transaction_record->date; $transaction['type'] = $transaction_record->type; $transaction['transaction_category'] = $transaction_record->transaction_category; $transaction['status'] = $transaction_record->status; $transaction['source'] = $transaction_record->cash_or_card; $transaction['amount'] = $transaction_record->amount; $transaction['reference'] = $transaction_record->reference; $transaction['description'] = $transaction_record->description; $transaction['transaction_id'] = $transaction_record->transaction_id; if (isset($transaction_record->accounting_resource_id)) $transaction['resource_id'] = $transaction_record->accounting_resource_id; $customer = null; $customer = null; if (isset($response->data->relationships->customer_id->id)) { $customer = $this->get_customer($response->data->relationships->customer_id->id, $auth); if ($this->response['status']) $customer = $this->response['data']; } $transaction['customer'] = $customer; //Invoice Plans $URI = $base_uri . '/api/v1/transactions/' . $id . "/link/invoices"; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array(); $response_invoices = $client->get($URI, $params); $response_invoices = json_decode($response_invoices->getBody()); $allocations = array(); $item_counter = 0; if (isset($response_invoices->data)) { foreach ($response_invoices->data as $key => $invoice) { if (isset($invoice->id) && isset($invoice->pivot_amount) && $invoice->pivot_amount > 0) { $allocations[$item_counter]['invoice_id'] = $invoice->id; $allocations[$item_counter]['amount'] = $invoice->pivot_amount; $invoice_obj = $this->get_invoice($invoice->id, $auth); if ($this->response['status']) { $invoice_obj = $this->response['data']; $allocations[$item_counter]['invoice'] = $invoice_obj; } $item_counter++; } } } $transaction['invoice_allocations'] = $allocations; $this->response['status'] = true; $this->response['data'] = $transaction; } catch (\Exception $e) { $this->response['status'] = false; $this->response['message'] = $e->getMessage(); } } else { $this->response['status'] = false; $this->response['message'] = "Authentication Failed!"; } return $this; } public function update_transaction_resource_id($id, $auth, $xero_id) { $data = $this->get_access_token($auth); if ($data['status'] && !empty($xero_id) && $xero_id != "00000000-0000-0000-0000-000000000000") { $base_uri = $data['base_uri']; $access_token = $data['access_token']; try { /// Authorization Process $client = new Client(); $URI = $base_uri . '/api/v1/transactions/' . $id; $params['headers'] = array("Authorization" => "Bearer " . $access_token); $params['form_params'] = array("accounting_resource_id" => $xero_id); $response = $client->put($URI, $params); $response = json_decode($response->getBody()); } catch (\Exception $e) { $this->response['status'] = false; } } else { $this->response['status'] = false; $this->response['message'] = "Authentication Failed!"; } return $this; } }