408 lines
17 KiB
PHP
408 lines
17 KiB
PHP
<?php
|
|
if (!defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
/*
|
|
* InvoicePlane
|
|
*
|
|
* @author InvoicePlane Developers & Contributors
|
|
* @copyright Copyright (c) 2012 - 2017 InvoicePlane.com
|
|
* @license https://invoiceplane.com/license.txt
|
|
* @link https://invoiceplane.com
|
|
*/
|
|
|
|
/**
|
|
* Class Dashboard
|
|
*/
|
|
class Dashboard extends Admin_Controller
|
|
{
|
|
public function index()
|
|
{
|
|
|
|
$this->load->model('invoices/mdl_items'); // added code for item vaues
|
|
$this->load->model('reports/mdl_reports');
|
|
|
|
$this->layout->set(
|
|
array(
|
|
'results' => $this->mdl_reports->sales_dashboard(),
|
|
'items' => $this->mdl_items->get()->result(), // added for geting value of item
|
|
'resu' => $this->mdl_items->today(),
|
|
'result' => $this->mdl_items->month(),
|
|
'res' => $this->mdl_items->year(),
|
|
|
|
)
|
|
);
|
|
|
|
$this->layout->buffer('content', 'dashboard/index');
|
|
$this->layout->render();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function zohobooks_api_fetch_all(){
|
|
|
|
//echo 'scuess';//die();
|
|
if (isset($_POST['sync']))
|
|
{
|
|
$con = new mysqli($this->db->hostname,$this->db->username,$this->db->password,$this->db->database);
|
|
|
|
$ch=curl_init();
|
|
|
|
$organization_id = '776847408';
|
|
$client_id = '1000.0WMDLCXAZV5DR60IKE9P49YTYGVL6C';
|
|
$refresh_token = '1000.0fcf314cc7b7b598df6e519461bbe9b8.f77688a2254137532ae33ad5e152a064';
|
|
$client_secret = '99da3a05cfa13cf91eb6476a3c44cfa3e21b6106a8';
|
|
$redirect_uri = 'https://resicoindustries.com/RIA';
|
|
|
|
$date_today = date('Y-m-d');
|
|
// $date_yesterday = date('Y-m-d',strtotime('-1 day',strtotime($date_today)));
|
|
$date_today = '2022-01-31';
|
|
$date_yesterday = date('Y-m-d',strtotime('-7 day',strtotime($date_today)));
|
|
$date_yesterday = '2022-01-01';
|
|
$log = $date_yesterday+'---'+$date_today;
|
|
log_message('error', $log);
|
|
log_message('error', 'zoho url beforefirst');
|
|
$url_org ="https://books.zoho.com/api/v3/invoices?";
|
|
|
|
log_message('error', 'zoho url after');
|
|
|
|
$fields=array(
|
|
|
|
"usestate" => "true",
|
|
"organization_id" =>"776847408",
|
|
"date_start" => $date_yesterday,
|
|
"date_end" => $date_today
|
|
|
|
);
|
|
|
|
$data_all = http_build_query($fields);
|
|
|
|
$accesstoken = '1000.095b66fc95fc81874c1cfc7d15b0904e.10bc9956d9b9ec14cce1350ee0cf8254';
|
|
|
|
//code => 57 -->expired
|
|
//code => 0 -->not expired
|
|
$headers=array(
|
|
'Authorization:Zoho-oauthtoken '.$accesstoken,
|
|
'Content-Type:application/json'
|
|
);
|
|
|
|
$data_access = $url_org.$data_all;
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $data_access);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
$body = '{}';
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
log_message('error', 'result before');
|
|
|
|
$result=curl_exec($ch);
|
|
|
|
log_message('error', $result);
|
|
log_message('error', 'res before');
|
|
|
|
$res = json_decode($result);
|
|
// print_r($res);
|
|
log_message('error','res after');
|
|
|
|
log_message('error', $res->code);
|
|
if($res->code=="57")
|
|
{
|
|
log_message('error', 'res->code');
|
|
//echo 'new token';//die();
|
|
$url_getauthtoken = 'https://accounts.zoho.com/oauth/v2/token?refresh_token='.$refresh_token.'&client_id='.$client_id.'&client_secret='.$client_secret.'&redirect_uri='.$redirect_uri.'&grant_type=refresh_token';
|
|
curl_setopt($ch, CURLOPT_URL, $url_getauthtoken);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
$body = '{}';
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$res_token=curl_exec($ch);
|
|
log_message('error', 'res_token after');
|
|
|
|
$ress = json_decode($res_token);
|
|
log_message('error', 'ress after');
|
|
|
|
$newaccesstoken = $ress->access_token;
|
|
|
|
|
|
$headers_new=array(
|
|
'Authorization:Zoho-oauthtoken '.$newaccesstoken,
|
|
'Content-Type:application/json'
|
|
);
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url_org.$data_access);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_new);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
$body = '{}';
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$result=curl_exec($ch);
|
|
|
|
$res = json_decode($result,true);
|
|
|
|
|
|
// print_r($res['invoices']);
|
|
//print_r(count($res['invoices']));//die();
|
|
$inv_array = $res['invoices'];
|
|
|
|
foreach($inv_array as $inv){
|
|
|
|
$invoice_id = $inv['invoice_id'];
|
|
|
|
|
|
if($invoice_id!==""){
|
|
|
|
$url2 = 'https://books.zoho.com/api/v3/invoices/'.$invoice_id.'?organization_id='.$organization_id;
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url2);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_new);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
$body2 = '{}';
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$body2);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$result2=curl_exec($ch);
|
|
|
|
$resall = json_decode($result2,true);
|
|
$res2[] = $resall['invoice'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//echo count($res2);
|
|
|
|
|
|
$insert_count = 0;
|
|
$update_count = 0;
|
|
foreach($res2 as $rs)
|
|
{
|
|
|
|
|
|
$gst_no = $rs['gst_no'];
|
|
$sku = $rs['line_items'][0]['sku'];
|
|
$invoice_number = $rs['invoice_number'];
|
|
$invoice_date = $rs['date'];
|
|
$due_date = $rs['due_date'];
|
|
$item_price = $rs['line_items'][0]['rate'];
|
|
$item_order = $rs['line_items'][0]['item_order'];
|
|
$item_product_unit = $rs['line_items'][0]['unit'];
|
|
$item_name = $rs['line_items'][0]['name'];
|
|
$item_description = $rs['line_items'][0]['description'];
|
|
$item_quantity = $rs['line_items'][0]['quantity'];
|
|
// $batch_no = $rs['custom_fields'][5]['value'];
|
|
$batch_no = "";
|
|
$invoice_time=$rs['created_time'];
|
|
$time_array = explode("T", $invoice_time);
|
|
$time_array = explode("+",$time_array[1]);
|
|
$invoice_time = $time_array[0];
|
|
$status = $rs['status'];
|
|
|
|
$vehicle_number = $rs['custom_field_hash']['cf_vehicle_number'];
|
|
$transport_mode =$rs['custom_field_hash']['cf_transport_mode'];
|
|
$purchase_order_date = $rs['custom_field_hash']['cf_purchase_order_date'];
|
|
$driver = $rs['custom_field_hash']['cf_driver'];
|
|
$place_of_supply = $rs['custom_field_hash']['cf_place_of_supply'];
|
|
|
|
|
|
$cgst = $rs['taxes'][0]['tax_name'];
|
|
$sgst = $rs['taxes'][1]['tax_name'];
|
|
$cgst = $cgst[4];
|
|
$sgst = $sgst[4];
|
|
$cgst = $cgst.'.000';
|
|
$getcgsttax = "SELECT * FROM ip_tax_rates WHERE tax_rate_percent='$cgst'";
|
|
$res_cgsttax = $con->query($getcgsttax);
|
|
$rw_cgsttax = $res_cgsttax->fetch_assoc();
|
|
$cgsttaxid = $rw_cgsttax['tax_rate_id'];
|
|
|
|
$sgst = $sgst.'.000';
|
|
$getsgsttax = "SELECT * FROM ip_tax_rates WHERE tax_rate_percent='$sgst'";
|
|
$res_sgsttax = $con->query($getsgsttax);
|
|
$rw_sgsttax = $res_sgsttax->fetch_assoc();
|
|
$sgsttaxid = $rw_sgsttax['tax_rate_id'];
|
|
|
|
|
|
|
|
$item_subtotal = $rs['line_items'][0]['item_total'];
|
|
$item_tax_total = $rs['tax_total'];
|
|
$item_total = $rs['total'];
|
|
$item_sgst_amt = $rs['taxes'][1]['tax_amount'];
|
|
$item_cgst_amt = $rs['taxes'][0]['tax_amount'];
|
|
|
|
$item_discount = 0.00;
|
|
$item_igst_amt = 0.00;
|
|
|
|
|
|
if($batch_no=="")
|
|
{
|
|
|
|
$batch_no = "";
|
|
}
|
|
|
|
$gst_sql = "SELECT * FROM ip_clients WHERE client_tax_code='$gst_no'";
|
|
$res_gst = $con->query($gst_sql);
|
|
// $r_gst[] = $res_gst->fetch_assoc()['client_id'];
|
|
$client_id = $res_gst->fetch_assoc()['client_id'];
|
|
|
|
|
|
$sku_sql = "SELECT * FROM ip_products WHERE sku='$sku'";
|
|
$res_sku = $con->query($sku_sql);
|
|
// $r_sku[] = $res_sku->fetch_assoc()['product_id'];
|
|
$product_id = $res_sku->fetch_assoc()['product_id'];
|
|
|
|
$sql_invoice_chk = "SELECT * FROM ip_invoices WHERE invoice_number='$invoice_number' AND invoice_date_created='$invoice_date'";
|
|
$res_invoice = $con->query($sql_invoice_chk);
|
|
|
|
|
|
if($res_invoice->num_rows<=0)
|
|
{
|
|
|
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
for ($i = 0; $i < 16; $i++)
|
|
{
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
|
|
|
|
|
|
$url_key = $randomString;
|
|
|
|
log_message('error', 'ip_invoices insert before');
|
|
|
|
$sql_invoice_insert = "INSERT INTO ip_invoices(user_id,client_id,invoice_group_id,invoice_status_id,invoice_date_created,invoice_time_created,invoice_date_due,invoice_number,invoice_url_key,receivedstatus,credit_status)VALUES(1,'$client_id',6,2,'$invoice_date','$invoice_time','$due_date','$invoice_number','$url_key','ST066',0)";
|
|
$result_invoice_insert = $con->query($sql_invoice_insert);
|
|
|
|
$invoice_insert_id=$con->insert_id;
|
|
|
|
log_message('error', 'ip_invoices insert after');
|
|
|
|
log_message('error', 'ip_invoice_items insert before');
|
|
|
|
$sql_invoiceitem_insert = "INSERT INTO ip_invoice_items(invoice_id,item_product_id,item_date_added,item_name,item_description,item_quantity,item_price,item_order,item_product_unit,Sgst_item_tax_rate_id,Cgst_item_tax_rate_id)
|
|
VALUES('$invoice_insert_id','$product_id','$invoice_date','$item_name','$item_description','$item_quantity','$item_price','$item_order','$item_product_unit','$sgsttaxid','$cgsttaxid')";
|
|
$result_invoiceitem_insert = $con->query($sql_invoiceitem_insert);
|
|
|
|
$item_insert_id = $con->insert_id;
|
|
|
|
log_message('error', ' ip_invoice_items insert after');
|
|
//tax amount -- ip_invoice_item_amounts - table
|
|
|
|
log_message('error', 'ip_invoice_item_amounts insert before');
|
|
|
|
$sql_tax_amount_insert = "INSERT INTO ip_invoice_item_amounts(item_id,item_subtotal,item_tax_total,item_total,item_sgst_amt,item_cgst_amt,item_discount,item_igst_amt)VALUES('$item_insert_id','$item_subtotal','$item_tax_total','$item_total','$item_sgst_amt','$item_cgst_amt','$item_discount','$item_igst_amt')";
|
|
$result_tax_amount_insert = $con->query($sql_tax_amount_insert);
|
|
|
|
log_message('error', 'ip_invoice_item_amounts insert after');
|
|
|
|
|
|
|
|
log_message('error', 'ip_invoice_custom insert before');
|
|
//custom fields insert
|
|
$sql_invoice_custom_insert = "INSERT INTO ip_invoice_custom(invoice_id,invoice_custom_fieldid,invoice_custom_fieldvalue)VALUES('$invoice_insert_id',62,'$batch_no')";
|
|
$result_invoice_custom_insert = $con->query($sql_invoice_custom_insert);
|
|
|
|
log_message('error', 'ip_invoice_custom insert after');
|
|
|
|
$custom_vehicle = "INSERT INTO ip_invoice_custom(invoice_id,invoice_custom_fieldid,invoice_custom_fieldvalue)VALUES('$invoice_insert_id',8,'$vehicle_number')";
|
|
$res_vehicle = $con->query($custom_vehicle);
|
|
|
|
$custom_transport_mode = "INSERT INTO ip_invoice_custom(invoice_id,invoice_custom_fieldid,invoice_custom_fieldvalue)VALUES('$invoice_insert_id',7,'$transport_mode')";
|
|
$res_transport_mode= $con->query($custom_transport_mode);
|
|
|
|
$custom_purchase_order_date= "INSERT INTO ip_invoice_custom(invoice_id,invoice_custom_fieldid,invoice_custom_fieldvalue)VALUES('$invoice_insert_id',15,'$purchase_order_date')";
|
|
$res_purchase_order_date = $con->query($custom_purchase_order_date);
|
|
|
|
log_message('error', 'ip_invoice_custom insert before');
|
|
|
|
$custom_driver = "INSERT INTO ip_invoice_custom(invoice_id,invoice_custom_fieldid,invoice_custom_fieldvalue)VALUES('$invoice_insert_id',58,'$driver')";
|
|
$res_driver = $con->query($custom_driver);
|
|
log_message('error', 'ip_invoice_custom insert after');
|
|
|
|
log_message('error', 'ip_invoice_custom insert before');
|
|
$custom_place = "INSERT INTO ip_invoice_custom(invoice_id,invoice_custom_fieldid,invoice_custom_fieldvalue)VALUES('$invoice_insert_id',10,'$place_of_supply')";
|
|
$res_place = $con->query($custom_place);
|
|
$insert_count = $insert_count + 1;
|
|
//echo "success";
|
|
log_message('error', 'ip_invoice_custom insert after');
|
|
}
|
|
else// update invoice status, especially for cancelled invoices
|
|
{
|
|
|
|
if($status == "void")
|
|
{
|
|
$sql_invoice_update = "UPDATE ip_invoices SET invoice_status_id = 5 WHERE invoice_number='$invoice_number' AND invoice_date_created='$invoice_date'";
|
|
$sql_invoice_update = $con->query($sql_invoice_update);
|
|
// echo "update-".$sql_invoice_update;
|
|
$update_count = $update_count + 1;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
echo json_encode(array('new_invoices' => $insert_count,'cancel_status_updated' => $update_count,'from_date' => $date_yesterday,'to_date'=> $date_today));
|
|
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
|
|
// print_r($res);
|
|
|
|
$invoice_id = $res->invoices[0]->invoice_id;
|
|
|
|
if($invoice_id!==""){
|
|
|
|
$url2 = 'https://books.zoho.com/api/v3/invoices/'.$invoice_id.'?organization_id='.$organization_id;
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url2);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
$body2 = '{}';
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$body2);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$result2=curl_exec($ch);
|
|
|
|
$res2 = json_decode($result2);
|
|
|
|
//print_r($res2->invoice);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
//end
|
|
// END zoho API
|
|
|
|
|
|
|
|
|
|
}
|