FIX_ZOHO API issues : ps
This commit is contained in:
parent
7b1fe83e77
commit
3789823c23
@ -135,7 +135,7 @@ $routes->get('purchaseorder/Savencrlist', 'Purchaseorder::Savencrlist');
|
||||
$routes->get('purchaseorder/Nonconfirmativelist','Purchaseorder::Nonconfirmativelist');
|
||||
|
||||
//Zohobooks sync
|
||||
$routes->get('user/zohobooks_api_fetch_all','User::zohobooks_api_fetch_all');
|
||||
$routes->match(['get', 'post'],'user/zohobooks_api_fetch_all','User::zohobooks_api_fetch_all');
|
||||
|
||||
// Application Payslip Routes
|
||||
$routes->match(['get', 'post', 'put', 'delete'],'monthlyListings', 'Monthlypay::monthlyListing');
|
||||
|
||||
@ -1301,18 +1301,20 @@ class User extends BaseController
|
||||
$tokendata['token'] = $token;
|
||||
$Zohobook_model->customUpdate($tokendata, 1, 'zohobook_accesstoken');
|
||||
}
|
||||
// $date_today = '2024-06-15';
|
||||
// $date_yesterday = '2024-06-15';
|
||||
$result = getheringInvoiceDetails($date_today, $date_yesterday, $token);//refer zohobook helper
|
||||
// print_r($result);die;
|
||||
$headers_new = $result['headers_new'];
|
||||
$res = json_decode($result['data'], true);
|
||||
if(isset($res['invoices']) && !empty($res['invoices'])){
|
||||
$inv_array = $res['invoices'];
|
||||
// echo json_encode($inv_array);die;
|
||||
}else{
|
||||
}else{
|
||||
$inv_array = array();
|
||||
log_message('error', $res['message']);
|
||||
echo "An Error Occur ==> " .$res['message']." \n <br> ";
|
||||
throw new \Exception($res['message']);
|
||||
$errorMessage = isset($res['message']) ? $res['message'] : 'No invoices found for the given date range.';
|
||||
log_message('error', $errorMessage); // Log the error message from the API response or a custom message
|
||||
echo "An Error Occurred ==> " . $errorMessage . " \n <br> ";
|
||||
throw new \Exception($errorMessage); // Throw an exception with the error message
|
||||
}
|
||||
|
||||
if(!empty($inv_array)){
|
||||
@ -1392,10 +1394,7 @@ class User extends BaseController
|
||||
$ip_invoice_custom_b['invoice_custom_fieldid'] = 62;
|
||||
$ip_invoice_custom_b['invoice_custom_fieldvalue'] = $batch_no;
|
||||
$result_invoice_custom_insert = $Zohobook_model->customInsert($ip_invoice_custom_b, 'ip_invoice_custom');
|
||||
if ($result_invoice_custom_insert)
|
||||
log_message('error', 'Data insert success DB : ip_invoice_custom ');
|
||||
else
|
||||
log_message('error', 'Data insert failed DB : ip_invoice_custom ');
|
||||
log_message('error', $result_invoice_custom_insert ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom ');
|
||||
}
|
||||
// vehicle_number
|
||||
if($vehicle_number){
|
||||
@ -1403,10 +1402,7 @@ class User extends BaseController
|
||||
$ip_invoice_custom_v['invoice_custom_fieldid'] = 8;
|
||||
$ip_invoice_custom_v['invoice_custom_fieldvalue'] = $vehicle_number;
|
||||
$res_vehicle = $Zohobook_model->customInsert($ip_invoice_custom_v, 'ip_invoice_custom');
|
||||
if ($res_vehicle)
|
||||
log_message('error', 'Data insert success DB : ip_invoice_custom ');
|
||||
else
|
||||
log_message('error', 'Data insert failed DB : ip_invoice_custom ');
|
||||
log_message('error', $res_vehicle ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom ');
|
||||
}
|
||||
// transport_mode
|
||||
if($transport_mode){
|
||||
@ -1414,10 +1410,7 @@ class User extends BaseController
|
||||
$ip_invoice_custom_t['invoice_custom_fieldid'] = 7;
|
||||
$ip_invoice_custom_t['invoice_custom_fieldvalue'] = $transport_mode;
|
||||
$res_transport_mode = $Zohobook_model->customInsert($ip_invoice_custom_t, 'ip_invoice_custom');
|
||||
if ($res_transport_mode)
|
||||
log_message('error', 'Data insert success DB : ip_invoice_custom ');
|
||||
else
|
||||
log_message('error', 'Data insert failed DB : ip_invoice_custom ');
|
||||
log_message('error', $res_transport_mode ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom ');
|
||||
}
|
||||
// purchase_order_date
|
||||
if($purchase_order_date){
|
||||
@ -1425,10 +1418,7 @@ class User extends BaseController
|
||||
$ip_invoice_custom_p['invoice_custom_fieldid'] = 15;
|
||||
$ip_invoice_custom_p['invoice_custom_fieldvalue'] = $purchase_order_date;
|
||||
$res_purchase_order_date = $Zohobook_model->customInsert($ip_invoice_custom_p, 'ip_invoice_custom');
|
||||
if ($res_purchase_order_date)
|
||||
log_message('error', 'Data insert success DB : ip_invoice_custom ');
|
||||
else
|
||||
log_message('error', 'Data insert failed DB : ip_invoice_custom ');
|
||||
log_message('error', $res_purchase_order_date ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom ');
|
||||
}
|
||||
// driver
|
||||
if($driver){
|
||||
@ -1436,10 +1426,7 @@ class User extends BaseController
|
||||
$ip_invoice_custom_d['invoice_custom_fieldid'] = 58;
|
||||
$ip_invoice_custom_d['invoice_custom_fieldvalue'] = $driver;
|
||||
$res_driver = $Zohobook_model->customInsert($ip_invoice_custom_d, 'ip_invoice_custom');
|
||||
if ($res_driver)
|
||||
log_message('error', 'Data insert success DB : ip_invoice_custom ');
|
||||
else
|
||||
log_message('error', 'Data insert failed DB : ip_invoice_custom ');
|
||||
log_message('error', $res_driver ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom ');
|
||||
}
|
||||
// place_of_supply
|
||||
if($place_of_supply){
|
||||
@ -1447,106 +1434,129 @@ class User extends BaseController
|
||||
$ip_invoice_custom_d['invoice_custom_fieldid'] = 10;
|
||||
$ip_invoice_custom_d['invoice_custom_fieldvalue'] = $place_of_supply;
|
||||
$res_place = $Zohobook_model->customInsert($ip_invoice_custom_d, 'ip_invoice_custom');
|
||||
if ($res_place)
|
||||
log_message('error', 'Data insert success DB : ip_invoice_custom ');
|
||||
else
|
||||
log_message('error', 'Data insert failed DB : ip_invoice_custom ');
|
||||
log_message('error', $res_place ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom ');
|
||||
}
|
||||
//end invoice level custom fields
|
||||
//if line items available
|
||||
// echo count($rs['line_items']);
|
||||
if (isset($rs['line_items']) && count($rs['line_items']) > 0) {
|
||||
log_message('error', 'Processing line items' . count($rs['line_items']));
|
||||
for ($i = 0; $i < count($rs['line_items']); $i++) { //start insert line items
|
||||
$sku = $rs['line_items'][$i]['sku'];
|
||||
$item_price = $rs['line_items'][$i]['rate'];
|
||||
$item_order = $rs['line_items'][$i]['item_order'];
|
||||
$item_product_unit = $rs['line_items'][$i]['unit'];
|
||||
$item_name = $rs['line_items'][$i]['name'];
|
||||
$item_description = $rs['line_items'][$i]['description'];
|
||||
$item_quantity = $rs['line_items'][$i]['quantity'];
|
||||
$cgst = $rs['line_items'][$i]['line_item_taxes'][1]['tax_name'];
|
||||
$sgst = $rs['line_items'][$i]['line_item_taxes'][0]['tax_name'];
|
||||
$cgst = $cgst[4];
|
||||
$sgst = $sgst[4];
|
||||
$cgst = $cgst . '.000';
|
||||
$getcgsttax = $Zohobook_model->ip_tax_rates($cgst);
|
||||
if ($getcgsttax) {
|
||||
$cgsttaxid = $getcgsttax[0]['tax_rate_id'];
|
||||
log_message('error', 'Data fetch success DB : ip_tax_rates cgst' . $cgst);
|
||||
} else {
|
||||
log_message('error', 'No data => Data fetch failed DB : ip_tax_rates ' . $cgst);
|
||||
$cgst_data['tax_rate_percent'] = $cgst;
|
||||
$getcgsttax = $Zohobook_model->customInsert($cgst_data, 'ip_tax_rates');
|
||||
$cgsttaxid = $getcgsttax[0]['tax_rate_id'];
|
||||
log_message('error', 'New Data Created : DB : ip_tax_rates , tax_rate_id : ' . $cgsttaxid);
|
||||
// echo('<br> inx' . $i . '--' . count($rs['line_items']));
|
||||
|
||||
for ($i = 0; $i < count($rs['line_items']); $i++) {
|
||||
log_message('error', 'Processing item index: ' . $i);
|
||||
$lineItem = $rs['line_items'][$i];
|
||||
log_message('error', 'inx' . $i . '--' . $lineItem['line_item_id']);
|
||||
// echo('<br> inx' . $i . '--' . $lineItem['line_item_id']);
|
||||
|
||||
$sku = $lineItem['sku'];
|
||||
$item_price = $lineItem['rate'];
|
||||
$item_order = $lineItem['item_order'];
|
||||
$item_product_unit = $lineItem['unit'];
|
||||
$item_name = $lineItem['name'];
|
||||
$item_description = $lineItem['description'];
|
||||
$item_quantity = $lineItem['quantity'];
|
||||
$cgst = $lineItem['line_item_taxes'][1]['tax_name'];
|
||||
$sgst = $lineItem['line_item_taxes'][0]['tax_name'];
|
||||
$cgst = $cgst[4];
|
||||
$sgst = $sgst[4];
|
||||
$cgst = $cgst . '.000';
|
||||
|
||||
// Fetch CGST Tax
|
||||
$getcgsttax = $Zohobook_model->ip_tax_rates($cgst);
|
||||
if ($getcgsttax) {
|
||||
$cgsttaxid = $getcgsttax[0]['tax_rate_id'];
|
||||
log_message('error', 'CGST Tax fetched: ' . $cgst);
|
||||
} else {
|
||||
$cgst_data['tax_rate_percent'] = $cgst;
|
||||
$getcgsttax = $Zohobook_model->customInsert($cgst_data, 'ip_tax_rates');
|
||||
$cgsttaxid = $getcgsttax[0]['tax_rate_id'];
|
||||
log_message('error', 'New CGST Tax created: ' . $cgsttaxid);
|
||||
}
|
||||
|
||||
$sgst = $sgst . '.000';
|
||||
|
||||
// Fetch SGST Tax
|
||||
$getsgsttax = $Zohobook_model->ip_tax_rates($sgst);
|
||||
if ($getsgsttax) {
|
||||
$sgsttaxid = $getsgsttax[0]['tax_rate_id'];
|
||||
log_message('error', 'SGST Tax fetched: ' . $sgst);
|
||||
} else {
|
||||
$sgst_data['tax_rate_percent'] = $sgst;
|
||||
$getsgsttax = $Zohobook_model->customInsert($sgst_data, 'ip_tax_rates');
|
||||
$sgsttaxid = $getsgsttax[0]['tax_rate_id'];
|
||||
log_message('error', 'New SGST Tax created: ' . $sgsttaxid);
|
||||
}
|
||||
|
||||
$item_subtotal = $lineItem['item_total'];
|
||||
$item_sgst_amt = $lineItem['line_item_taxes'][0]['tax_amount'];
|
||||
$item_cgst_amt = $lineItem['line_item_taxes'][1]['tax_amount'];
|
||||
$item_tax_total = $item_sgst_amt + $item_cgst_amt;
|
||||
$item_total = $item_subtotal + $item_tax_total;
|
||||
$item_discount = 0.00;
|
||||
$item_igst_amt = 0.00;
|
||||
|
||||
// Fetch Product ID
|
||||
$sku_sql = $Zohobook_model->ip_products($sku);
|
||||
if ($sku_sql) {
|
||||
$product_id = $sku_sql[0]['product_id'];
|
||||
log_message('error', 'Product ID fetched: ' . $product_id);
|
||||
} else {
|
||||
$sku_data['product_name'] = $item_name;
|
||||
$sku_data['product_description'] = $item_description;
|
||||
$sku_data['sku'] = $sku;
|
||||
$sku_sql = $Zohobook_model->customInsert($sku_data, 'ip_products');
|
||||
$product_id = $sku_sql[0]['product_id'];
|
||||
log_message('error', 'New Product created: ' . $product_id);
|
||||
}
|
||||
|
||||
$ip_invoice_items = [
|
||||
'invoice_id' => $invoice_insert_id,
|
||||
'item_product_id' => $product_id,
|
||||
'item_date_added' => $invoice_date,
|
||||
'item_name' => $item_name,
|
||||
'item_description' => $item_description,
|
||||
'item_quantity' => $item_quantity,
|
||||
'item_price' => $item_price,
|
||||
'item_order' => $item_order,
|
||||
'item_product_unit' => $item_product_unit,
|
||||
'Sgst_item_tax_rate_id' => $sgsttaxid,
|
||||
'Cgst_item_tax_rate_id' => $cgsttaxid
|
||||
];
|
||||
|
||||
// Insert Invoice Item
|
||||
$item_insert_id = $Zohobook_model->customInsert($ip_invoice_items, 'ip_invoice_items');
|
||||
|
||||
if ($item_insert_id) {
|
||||
log_message('error', 'Invoice item inserted: ' . $item_insert_id);
|
||||
} else {
|
||||
log_message('error', 'Failed to insert invoice item');
|
||||
continue;
|
||||
}
|
||||
|
||||
$ip_invoice_item_amounts = [
|
||||
'item_id' => $item_insert_id,
|
||||
'item_subtotal' => $item_subtotal,
|
||||
'item_tax_total' => $item_tax_total,
|
||||
'item_total' => $item_total,
|
||||
'item_sgst_amt' => $item_sgst_amt,
|
||||
'item_cgst_amt' => $item_cgst_amt,
|
||||
'item_discount' => $item_discount,
|
||||
'item_igst_amt' => $item_igst_amt
|
||||
];
|
||||
|
||||
// Insert Invoice Item Amount
|
||||
$result_tax_amount_insert = $Zohobook_model->customInsert($ip_invoice_item_amounts, 'ip_invoice_item_amounts');
|
||||
if ($result_tax_amount_insert) {
|
||||
log_message('error', 'Invoice item amount inserted');
|
||||
} else {
|
||||
log_message('error', 'Failed to insert invoice item amount');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$sgst = $sgst . '.000';
|
||||
$getsgsttax = $Zohobook_model->ip_tax_rates($sgst);
|
||||
if ($getsgsttax) {
|
||||
$sgsttaxid = $getsgsttax[0]['tax_rate_id'];
|
||||
log_message('error', 'Data fetch success DB : ip_tax_rates sgst' . $sgst);
|
||||
} else {
|
||||
log_message('error', 'No data => Data fetch failed DB : ip_tax_rates ' . $sgst);
|
||||
$sgst_data['tax_rate_percent'] = $sgst;
|
||||
$getsgsttax = $Zohobook_model->customInsert($sgst_data, 'ip_tax_rates');
|
||||
$sgsttaxid = $getsgsttax[0]['tax_rate_id'];
|
||||
log_message('error', 'New Data Created : DB : ip_tax_rates , tax_rate_id : ' . $sgsttaxid);
|
||||
}
|
||||
$item_subtotal = $rs['line_items'][$i]['item_total'];
|
||||
$item_sgst_amt = $rs['line_items'][$i]['line_item_taxes'][0]['tax_amount'];
|
||||
$item_cgst_amt = $rs['line_items'][$i]['line_item_taxes'][1]['tax_amount'];
|
||||
$item_tax_total = ($item_sgst_amt + $item_cgst_amt);
|
||||
$item_total = ($item_subtotal + $item_tax_total);
|
||||
$item_discount = 0.00;
|
||||
$item_igst_amt = 0.00;
|
||||
// if($batch_no==""){ $batch_no = "";}
|
||||
$sku_sql = $Zohobook_model->ip_products($sku);
|
||||
if ($sku_sql) {
|
||||
$product_id = $sku_sql[0]['product_id'];
|
||||
log_message('error', 'Data fetch success DB : ip_products , product_id : ' . $product_id);
|
||||
} else {
|
||||
log_message('error', 'No data => Data fetch failed DB : ip_products ' . $sku);
|
||||
$sku_data['product_name'] = $item_name;
|
||||
$sku_data['product_description'] = $item_description;
|
||||
$sku_data['sku'] = $sku;
|
||||
$sku_sql = $Zohobook_model->customInsert($sku_data, 'ip_products');
|
||||
$product_id = $sku_sql[0]['sku'];
|
||||
log_message('error', 'sku' . $sku);
|
||||
log_message('error', 'New Data Created : DB : product_id , product_id : ' . $product_id);
|
||||
}
|
||||
//#1
|
||||
$ip_invoice_items['invoice_id'] = $invoice_insert_id;
|
||||
$ip_invoice_items['item_product_id'] = $product_id;
|
||||
$ip_invoice_items['item_date_added'] = $invoice_date;
|
||||
$ip_invoice_items['item_name'] = $item_name;
|
||||
$ip_invoice_items['item_description'] = $item_description;
|
||||
$ip_invoice_items['item_quantity'] = $item_quantity;
|
||||
$ip_invoice_items['item_price'] = $item_price;
|
||||
$ip_invoice_items['item_order'] = $item_order;
|
||||
$ip_invoice_items['item_product_unit'] = $item_product_unit;
|
||||
$ip_invoice_items['Sgst_item_tax_rate_id'] = $sgsttaxid;
|
||||
$ip_invoice_items['Cgst_item_tax_rate_id'] = $cgsttaxid;
|
||||
$item_insert_id = $Zohobook_model->customInsert($ip_invoice_items, 'ip_invoice_items');
|
||||
if ($item_insert_id)
|
||||
log_message('error', 'Data insert success DB : ip_invoice_items => Sgst_item_tax_rate_id :' . $sgsttaxid);
|
||||
else
|
||||
log_message('error', 'Data insert failed DB : ip_invoice_items => Sgst_item_tax_rate_id :' . $sgsttaxid);
|
||||
//tax amount -- ip_invoice_item_amounts - table
|
||||
$ip_invoice_item_amounts['item_id'] = $item_insert_id;
|
||||
$ip_invoice_item_amounts['item_subtotal'] = $item_subtotal;
|
||||
$ip_invoice_item_amounts['item_tax_total'] = $item_tax_total;
|
||||
$ip_invoice_item_amounts['item_total'] = $item_total;
|
||||
$ip_invoice_item_amounts['item_sgst_amt'] = $item_sgst_amt;
|
||||
$ip_invoice_item_amounts['item_cgst_amt'] = $item_cgst_amt;
|
||||
$ip_invoice_item_amounts['item_discount'] = $item_discount;
|
||||
$ip_invoice_item_amounts['item_igst_amt'] = $item_igst_amt;
|
||||
$result_tax_amount_insert = $Zohobook_model->customInsert($ip_invoice_item_amounts, 'ip_invoice_item_amounts');
|
||||
if ($result_tax_amount_insert)
|
||||
log_message('error', 'Data insert success DB : ip_invoice_item_amounts ');
|
||||
else
|
||||
log_message('error', 'Data insert failed DB : ip_invoice_item_amounts ');
|
||||
} //end line items insert
|
||||
|
||||
} else {
|
||||
// echo "fails";
|
||||
log_message('error', 'Line Items not found in API result');
|
||||
}
|
||||
} else {
|
||||
@ -1570,12 +1580,16 @@ class User extends BaseController
|
||||
}
|
||||
}
|
||||
}
|
||||
echo 'new invoice inserted count- ' . $insert_count . '/' . 'status updated count - ' . $update_count;
|
||||
// echo 'new invoice inserted count- ' . $insert_count . '/' . 'status updated count - ' . $update_count;
|
||||
$response = [
|
||||
'status' => true,
|
||||
'message' => "Successfully data inserted. Total inserted: " . $insert_count . ", Total Updated: " . $update_count
|
||||
];
|
||||
return json_encode($response);
|
||||
}else{
|
||||
$err = 'invoice details not founded ( from '.$date_yesterday.' into '.$date_today.' )';
|
||||
log_message('error', $err);
|
||||
echo $err;
|
||||
throw new \Exception($err);
|
||||
$err = 'invoice details not founded ( from '.$date_yesterday.' into '.$date_today.' )';
|
||||
log_message('error', $err);
|
||||
throw new \Exception($err);
|
||||
}
|
||||
// curl_close($ch);
|
||||
// }
|
||||
@ -1584,6 +1598,12 @@ class User extends BaseController
|
||||
// $data['last_query'] = $this->db->getLastQuery();
|
||||
$Zohobook_model->customInsert($data, 'zohobook_api_err');
|
||||
log_message('error', "Data insertion failed : " . $e->getMessage());
|
||||
|
||||
$response = [
|
||||
'status' => false,
|
||||
'message' => "An error occurred: " . $e->getMessage()
|
||||
];
|
||||
return json_encode($response);
|
||||
}
|
||||
}
|
||||
// END zoho API
|
||||
|
||||
@ -43,7 +43,7 @@ if (! function_exists('getheringInvoiceDetails')) {
|
||||
// Zoho API credentials
|
||||
$organization_id = '776847408';
|
||||
$url_org = "https://books.zoho.com/api/v3/invoices?";
|
||||
// $url_org = "https://zohoapis.com/api/v3/invoices?";
|
||||
// $url_org = "https://zohoapis.com/books/v3/invoices?";
|
||||
|
||||
// Construct query parameters
|
||||
$fields = array(
|
||||
@ -64,7 +64,9 @@ if (! function_exists('getheringInvoiceDetails')) {
|
||||
$data_access = $url_org.$data_all;
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_URL, $url_org.$data_access);
|
||||
curl_setopt($ch, CURLOPT_URL, $data_access);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
@ -90,6 +92,7 @@ if (! function_exists('getheringInvoiceSubDetails')) {
|
||||
// Construct URL for invoice sub-details
|
||||
$url2 = 'https://books.zoho.com/api/v3/invoices/'.$invoice_id.'?organization_id='.$organization_id;
|
||||
// $url2 = 'https://zohoapis.com/api/v3/invoices/'.$invoice_id.'?organization_id='.$organization_id;
|
||||
// $url2 = 'https://zohoapis.com/books/v3/invoices/'.$invoice_id.'?organization_id='.$organization_id;
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_URL, $url2);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
@ -95,12 +95,12 @@ function synczohobooks(){
|
||||
data:{sync},
|
||||
|
||||
success:function(data){
|
||||
alert("Sync Completed");
|
||||
|
||||
console.log(data);
|
||||
alert("Sync Completed");
|
||||
},failure:function(data){alert("Failed");},
|
||||
complete: function(){
|
||||
complete: function(){
|
||||
$('#loader_wrapper').css("display", "none");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user