From 62bde8af4f4191f06738f62151d9042103bbfeee Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Wed, 26 Jun 2024 06:58:39 +0000 Subject: [PATCH] FIX_igr File and Zoho : ps --- app/Config/Constants.php | 4 +- app/Controllers/Inwardgateregister.php | 59 +-- app/Controllers/User.php | 666 +++++++++++++------------ 3 files changed, 374 insertions(+), 355 deletions(-) diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 3f830ba5..455424ae 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -223,7 +223,9 @@ define('FINANCE','DEP10'); define('PURCHASE','DEP11'); define('MANAGEMENT','DEP12'); define('HR','DEP13'); -define('SECURITY','DEP14'); +// define('SECURITY','DEP14'); //old +define('SECURITY','DEP27'); + define('SYSTEM_ADMIN','1'); /* PO Type */ diff --git a/app/Controllers/Inwardgateregister.php b/app/Controllers/Inwardgateregister.php index 08ee8d77..59bfb2da 100755 --- a/app/Controllers/Inwardgateregister.php +++ b/app/Controllers/Inwardgateregister.php @@ -293,9 +293,9 @@ class Inwardgateregister extends BaseController { $PONO = $this->request->getPost('PONO'); $DeliveryChellanOrInvoiceNo = $this->request->getPost('InvoiceNo'); - $DeliveryChellan = $this->request->getPost('InvoiceDate'); - $DeliveryChellanDate = get_date_time_format($DeliveryChellan); - $Mat_Rcvd_Dt = $this->request->getPost('MaterialRcvdDate'); + $DeliveryChellan = (string)$this->request->getPost('InvoiceDate'); + $DeliveryChellanDate = get_date_time_format($DeliveryChellan); + $Mat_Rcvd_Dt = (string)$this->request->getPost('MaterialRcvdDate'); $MaterialRcvdDt = get_date_time_format($Mat_Rcvd_Dt); $VehicleNo = $this->request->getPost('VehicleNo'); $CourierNo = $this->request->getPost('CourierNo'); @@ -318,34 +318,37 @@ class Inwardgateregister extends BaseController // $IGRNO = "IGRNO02695"; for ($i = 1; $i <= $constant; $i++) { - $pathname = 'browseFiles' . $i; $file = $this->request->getFile($pathname); - $requestfilename = $file->getName(); - if (!empty($requestfilename)) { - - $files = str_replace(" ", "", $_FILES[$pathname]['name']); - $fcount = 0; - - foreach ($prefile as $value) { - if ($value == $files) { $fcount++; } - } - - if ($fcount == 0) { - if ($file->isValid() && !$file->hasMoved()) { - $path = ROOTPATH.'public/uploads/Igrfiles/'; - if(!is_dir($path)) { mkdir($path, 0777, true); } - $Picture = $file->getName(); - $file->move($path,$Picture); - } - $arr[] = array($Picture); - } - - $prefile[] = $files; - } - } - + if ($file && $file->isValid()) { + $requestfilename = $file->getName(); + if (!empty($requestfilename)) { + $files = str_replace(" ", "", $_FILES[$pathname]['name']); + $fcount = 0; + + foreach ($prefile as $value) { + if ($value == $files) { $fcount++; } + } + + if ($fcount == 0) { + if ($file->isValid() && !$file->hasMoved()) { + $path = ROOTPATH.'public/uploads/Igrfiles/'; + if(!is_dir($path)) { mkdir($path, 0777, true); } + $Picture = $file->getName(); + $file->move($path,$Picture); + log_message('error', "File new" . $i . " uploaded successfully. File name: " . $requestfilename); + } + $arr[] = array($Picture); + } + + $prefile[] = $files; + } + + } else { + log_message('error', "No file uploaded for new" . $i . " or there was an error uploading the file."); + } + } if (!empty($arr)) { foreach ($arr as $ma) { diff --git a/app/Controllers/User.php b/app/Controllers/User.php index 7742e8fa..99515822 100644 --- a/app/Controllers/User.php +++ b/app/Controllers/User.php @@ -1268,344 +1268,358 @@ class User extends BaseController } } - function zohobooks_api_fetch_all() - { - - helper('zohobook'); - $Zohobook_model = model('Zohobook_api_model'); - - try { - - $from = $this->request->getPost('from'); - $to = $this->request->getPost('to'); - - if (isset($from) && isset($to) && $from !== null && $to !== null) { - $date_today = $to; - $date_yesterday = $from; - } else { - $date_today = get_current_date(); // refer datetime helper - $date_yesterday = get_date_days_ago($date_today,'Y-m-d','-3 day'); // refer datetime helper - } - - $chk_acc_token_val = $Zohobook_model->get_data('zohobook_accesstoken', 'id', 1); - - $newDate = add_one_hour_date($chk_acc_token_val[0]['created_at']); // refer datetime helper - - $token = 0; - - if ($newDate > get_current_date('Y-m-d H:i:s')) { - $token = $chk_acc_token_val[0]['token']; - $tokendata['token'] = $token; - } else { - $token = generateNewToken(); - $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 - $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{ - $inv_array = array(); - $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
"; - throw new \Exception($errorMessage); // Throw an exception with the error message - } - - if(!empty($inv_array)){ - foreach ($inv_array as $inv) { - $invoice_id = $inv['invoice_id']; - - //filter only einvoice status pushed invoices only - //&& isset($inv['einvoice_details']) && isset($inv['einvoice_details']['status']) && ($inv['einvoice_details']['status'] == 'pushed') - if ($invoice_id !== "") { - // $organization_id = '655654572'; - $organization_id = '776847408'; - $result2 = getheringInvoiceSubDetails($invoice_id, $organization_id, $headers_new);// refer Zohobook helper - $resall = json_decode($result2, true); - $res2[] = $resall['invoice']; - } - } - - $insert_count = 0; - $update_count = 0; - // echo json_encode($res2);die(); - foreach ($res2 as $rs) { - $gst_no = $rs['gst_no']; - $invoice_number = $rs['invoice_number']; - $invoice_date = $rs['date']; - $status = $rs['status']; - $due_date = $rs['due_date']; - $invoice_time = $rs['created_time']; - $time_array = explode("T", $invoice_time); - $time_array = explode("+", $time_array[1]); - $invoice_time = $time_array[0]; - $sql_invoice_chk = $Zohobook_model->ip_invoices($invoice_number, $invoice_date); - if ($sql_invoice_chk <= 0) { - $gst_sql = $Zohobook_model->ip_clients($gst_no); - if ($gst_sql) { - $client_id = $gst_sql[0]['client_id']; - log_message('error', 'Data fetch success DB : ip_clients , client_id : ' . $client_id); - } else { - log_message('error', 'No data => Data fetch failed DB : ip_clients ' . $gst_no); - $gst_no_data['client_tax_code'] = $gst_no; - $gst_sql = $Zohobook_model->customInsert($gst_no_data, 'ip_clients'); - $client_id = $gst_sql[0]['tax_rate_id']; - log_message('error', 'New Data Created : DB : ip_clients , client_id : ' . $client_id); + function zohobooks_api_fetch_all(){ + // print_r($_GET); + // echo isset($_GET['from']) ? $_GET['from'] : 'n'; + // echo isset($_GET['to']) ? $_GET['to'] : 'n'; + // die(); + try{ + // if (isset($_POST['sync']) || $_GET['from'] != null && $_GET['to'] != null) + // { + // $ch=curl_init(); + //$this->load->helper('zohobooks_fetch_api_helper'); + helper('zohobook'); + $Zohobook_model = model('Zohobook_api_model'); + if(isset($_GET['from']) && $_GET['from'] != null && isset($_GET['to']) && $_GET['to'] != null) + { + $date_today = $_GET['to']; + $date_yesterday = $_GET['from']; } - $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - $charactersLength = strlen($characters); - $randomString = ''; - for ($i = 0; $i < 16; $i++) { - $randomString .= $characters[rand(0, $charactersLength - 1)]; + else + { + $date_today = date('Y-m-d'); + $date_yesterday = date('Y-m-d',strtotime('-3 day',strtotime($date_today))); } - $url_key = $randomString; - $ip_invoices['user_id'] = 1; - $ip_invoices['client_id'] = $client_id; - $ip_invoices['invoice_group_id'] = 6; - $ip_invoices['invoice_status_id'] = ($status == 'draft' ? 1 : (($status == 'sent' || $status == 'overdue') ? 2 : 5)); - $ip_invoices['invoice_date_created'] = $invoice_date; - $ip_invoices['invoice_time_created'] = $invoice_time; - $ip_invoices['invoice_date_due'] = $due_date; - $ip_invoices['invoice_number'] = $invoice_number; - $ip_invoices['invoice_url_key'] = $url_key; - $ip_invoices['receivedstatus'] = 'ST066'; - $ip_invoices['credit_status'] = 0; - $invoice_insert_id = $Zohobook_model->customInsert($ip_invoices, 'ip_invoices'); - if ($invoice_insert_id) { - log_message('error', 'Data insert success DB : ip_invoices client id :' . $client_id); - $insert_count = $insert_count + 1; - //start invoice level custom fields - //custom fields insert - $batch_no = isset($rs['custom_fields'][5]['value']) ? $rs['custom_fields'][5]['value'] : ""; - $vehicle_number = isset($rs['custom_field_hash']['cf_vehicle_number']) ? $rs['custom_field_hash']['cf_vehicle_number'] : ""; - $transport_mode = isset($rs['custom_field_hash']['cf_transport_mode']) ? $rs['custom_field_hash']['cf_transport_mode'] : ""; - $purchase_order_date = isset($rs['custom_field_hash']['cf_purchase_order_date']) ? $rs['custom_field_hash']['cf_purchase_order_date'] : ""; - $driver = isset($rs['custom_field_hash']['cf_driver'])?$rs['custom_field_hash']['cf_driver']:""; - $place_of_supply = isset($rs['custom_field_hash']['cf_place_of_supply'])?$rs['custom_field_hash']['cf_place_of_supply']:""; - // batch_no - if($batch_no){ - $ip_invoice_custom_b['invoice_id'] = $invoice_insert_id; - $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'); - log_message('error', $result_invoice_custom_insert ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom '); + // echo $date_today.'--'.$date_yesterday;die; + $chk_acc_token_val = $Zohobook_model->get_data('zohobook_accesstoken','id' , 1); + // print_r($chk_acc_token_val);//die(); + $date = new \DateTime($chk_acc_token_val[0]['created_at']); + //print_r($date); + $date->add(new \DateInterval('PT1H')); + //print_r($date); + //echo '****************'; + //print_r(date('Y-m-d h:i:s')); + $token = 0; + if ($date->format('Y-m-d H:i:s') > date('Y-m-d H:i:s')) { $token = $chk_acc_token_val[0]['token']; } + else { $token = generateNewToken(); $tokendata['token'] = $token; $Zohobook_model->customUpdate($tokendata , 1,'zohobook_accesstoken'); } + // $token = generateNewToken(); $tokendata['token'] = $token; + //$Zohobook_model->update($tokendata , 1,'zohobook_accesstoken'); + $result = getheringInvoiceDetails($date_today , $date_yesterday , $token); + // echo json_encode($result);die; + $headers_new = $result['headers_new']; + $res = json_decode($result['data'],true); + $inv_array = $res['invoices']; + // print_r($inv_array); + // echo "rrrr"; + // die(); + foreach($inv_array as $inv){ + $invoice_id = $inv['invoice_id']; + //filter only einvoice status pushed invoices only + //&& isset($inv['einvoice_details']) && isset($inv['einvoice_details']['status']) && ($inv['einvoice_details']['status'] == 'pushed') + if($invoice_id !== "" ){ + $organization_id = '776847408'; + $result2=getheringInvoiceSubDetails($invoice_id,$organization_id,$headers_new); + $resall = json_decode($result2,true); + $res2[] = $resall['invoice']; + } } - // vehicle_number - if($vehicle_number){ - $ip_invoice_custom_v['invoice_id'] = $invoice_insert_id; - $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'); - log_message('error', $res_vehicle ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom '); - } - // transport_mode - if($transport_mode){ - $ip_invoice_custom_t['invoice_id'] = $invoice_insert_id; - $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'); - 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){ - $ip_invoice_custom_p['invoice_id'] = $invoice_insert_id; - $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'); - log_message('error', $res_purchase_order_date ? 'Data insert success DB : ip_invoice_custom ' : 'Data insert failed DB : ip_invoice_custom '); - } - // driver - if($driver){ - $ip_invoice_custom_d['invoice_id'] = $invoice_insert_id; - $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'); - 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){ - $ip_invoice_custom_d['invoice_id'] = $invoice_insert_id; - $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'); - 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'])); - // echo('
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('
inx' . $i . '--' . $lineItem['line_item_id']); + $insert_count = 0; + $update_count = 0; + foreach($res2 as $rs) + { + $gst_no = $rs['gst_no']; + $invoice_number = $rs['invoice_number']; + $invoice_date = $rs['date']; + $status = $rs['status']; + $due_date = $rs['due_date']; + + $invoice_time=$rs['created_time']; + $time_array = explode("T", $invoice_time); + $time_array = explode("+",$time_array[1]); + $invoice_time = $time_array[0]; - $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); + $sql_invoice_chk = $Zohobook_model->ip_invoices($invoice_number , $invoice_date); + if($sql_invoice_chk <= 0) + { + + $gst_sql = $Zohobook_model->ip_clients($gst_no); + if($gst_sql) + { + $client_id = $gst_sql[0]['client_id']; + log_message('error', 'Data fetch success DB : ip_clients , client_id : '.$client_id); } - - $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); + else + { + log_message('error', 'No data => Data fetch failed DB : ip_clients '.$gst_no); + $gst_no_data['client_tax_code'] = $gst_no; + $gst_sql = $Zohobook_model->customInsert($gst_no_data,'ip_clients'); + $client_id = $gst_sql[0]['tax_rate_id']; + log_message('error', 'New Data Created : DB : ip_clients , client_id : '.$client_id); } + + + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < 16; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } + $url_key = $randomString; + + $ip_invoices['user_id'] = 1; + $ip_invoices['client_id'] = $client_id; + $ip_invoices['invoice_group_id'] = 6; + $ip_invoices['invoice_status_id'] = ($status == 'draft' ? 1 : (($status == 'sent' || $status == 'overdue') ? 2 : 5)); + $ip_invoices['invoice_date_created'] = $invoice_date; + $ip_invoices['invoice_time_created'] = $invoice_time; + $ip_invoices['invoice_date_due'] = $due_date; + $ip_invoices['invoice_number'] = $invoice_number; + $ip_invoices['invoice_url_key'] = $url_key; + $ip_invoices['receivedstatus'] = 'ST066'; + $ip_invoices['credit_status'] = 0; + $invoice_insert_id = $Zohobook_model->customInsert($ip_invoices,'ip_invoices'); + if($invoice_insert_id) + { + log_message('error', 'Data insert success DB : ip_invoices client id :'. $client_id); + $insert_count = $insert_count + 1; + //start invoice level custom fields + + //custom fields insert + + $batch_no = isset($rs['custom_fields'][5]['value']) ? $rs['custom_fields'][5]['value'] : ""; + // $batch_no = ""; + $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']; + // batch_no + $ip_invoice_custom_b['invoice_id'] = $invoice_insert_id; + $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 '); + // vehicle_number + $ip_invoice_custom_v['invoice_id'] = $invoice_insert_id; + $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 '); + // transport_mode + $ip_invoice_custom_t['invoice_id'] = $invoice_insert_id; + $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 '); + // purchase_order_date + $ip_invoice_custom_p['invoice_id'] = $invoice_insert_id; + $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 '); + // driver + $ip_invoice_custom_d['invoice_id'] = $invoice_insert_id; + $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 '); + // place_of_supply + $ip_invoice_custom_d['invoice_id'] = $invoice_insert_id; + $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 '); + //end invoice level custom fields + + //if line items available + 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); + } - $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; + $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); + } - // 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); + $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 + { + log_message('error', 'Line Items not found in API result'); + } + } + else + { + log_message('error', 'Data insert failed DB : ip_invoice_items client id :'. $client_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; + + } + else // update invoice status, especially for cancelled invoices + { + if($status == "void" || $status == "sent" || $status == "overdue") + { + $sql_invoice_update = array(); + $sql_invoice_update['invoice_status_id'] = (($status == "sent" || $status == "overdue") ? 2 : 5); + // echo 'script' . $invoice_number. $status; + // print_r( $sql_invoice_update); + // echo '
'; + $sql_invoice_update = $Zohobook_model->sql_invoice_update($sql_invoice_update,$invoice_number,$invoice_date,'ip_invoices'); + if($sql_invoice_update) + { + log_message('error', 'Data update success DB : ip_invoices '); } - - $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; + else + { + log_message('error', 'Data update failed DB : ip_invoices '); } + $update_count = $update_count + 1; } - - } else { - // echo "fails"; - log_message('error', 'Line Items not found in API result'); - } - } else { - log_message('error', 'Data insert failed DB : ip_invoice_items client id :' . $client_id); - } - } else // update invoice status, especially for cancelled invoices - { - if ($status == "void" || $status == "sent" || $status == "ovedue") { - $sql_invoice_update = array(); - $sql_invoice_update['invoice_status_id'] = (($status == "sent" || $status == "ovedue") ? 2 : 5); - // echo 'script' . $invoice_number. $status; - // print_r( $sql_invoice_update); - // echo '
'; - $sql_invoice_update = $Zohobook_model->sql_invoice_update($sql_invoice_update, $invoice_number, $invoice_date, 'ip_invoices'); - if ($sql_invoice_update) { - log_message('error', 'Data update success DB : ip_invoices '); - } else { - log_message('error', 'Data update failed DB : ip_invoices '); - } - $update_count = $update_count + 1; - } - } - } - // 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); - throw new \Exception($err); - } - // curl_close($ch); - // } - } catch (\Exception $e) { - $data['err'] = $e->getMessage(); - // $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 + } + // 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); + + // curl_close($ch); + // } + } + catch (\Exception $e) { + $data['err'] = $e->getMessage(); + // $db = \Config\Database::connect(); + // $data['last_query'] = $db->getLastQuery();; + $Zohobook_model->customInsert($data,'zohobook_api_err'); + log_message('error', "Data insertion failed : " . $e->getMessage()); + + return json_encode($response); + } + } + // END zoho API } \ No newline at end of file