siddharth_application/application/controllers/quality.php
2023-06-12 15:14:08 +05:30

888 lines
28 KiB
PHP

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/libraries/BaseController.php';
require APPPATH . '/third_party/mpdf/mpdf.php';
/**
* Class : purchaseorder (PurchaseorderController)
* User Class to control all user related operations.
* @author : VenbaMail Mali
* @version : 1.1
* @since : 15 Feb 2016
*/
class quality extends BaseController
{
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->load->model('quality_model');
$this->load->library('form_validation');
//$this->load->library('dompdf_gen');
$this->isLoggedIn();
}
/**
* This function used to load the first screen of the user
*/
public function index()
{
$data['products'] = $this->quality_model->getAllprodcuts();
$data['customers'] = $this->quality_model->getAllcustomers();
$data['existspec'] = $this->quality_model->getAllspec();
$data['existspecinward'] = $this->quality_model->getAllspecInward();
$this->global['pageTitle'] = 'Siddharth : Quality Master';
$this->loadViews("qualitymasterlistview", $this->global, $data , NULL);
}
public function reportList()
{
$data['records'] = $this->quality_model->getTestMasterforList();
$this->global['pageTitle'] = 'Siddharth : Prodcut Report List ';
$this->loadViews("reportmasterlist", $this->global, $data , NULL);
}
public function reportListInward()
{
$data['records'] = $this->quality_model->getTestMasterforListInward();
$this->global['pageTitle'] = 'Siddharth : Report List Inward';
$this->loadViews("reportmasterlist_inward", $this->global, $data , NULL);
}
function addNewPage()
{
$data['productsoutward'] = $this->quality_model->getAllprodcuts();
$data['customersoutward'] = $this->quality_model->getAllcustomers();
$data['productsinward'] = $this->quality_model->getAllprodcutsInward();
$data['customersinward'] = $this->quality_model->getAllcustomersInward();
$this->global['pageTitle'] = 'Siddharth : Quality Master';
$this->loadViews("qualitymaster", $this->global, $data , NULL);
}
function saveSpectficationData()
{
$product = $this->input->post('param2');
$customer = $this->input->post('param3');
$jsondata = $this->input->post('param1');
$type = $this->input->post('param4');
//echo $type;die();
$createdby = $this->session->userdata ( 'userId' );
$phpdata = json_decode($jsondata,true);
$total = 0;
// echo $product .'-'. $customer;
//print_r($phpdata);die();
foreach($phpdata as $data)
{
$name = $data['Name'];
$name = strtoupper($name);
$uom = $data['UOM'];
$min = $data['Min'];
$max = $data['Max'];
$temp = $data['Temp'];
$rawsandtest = $data['Raw Sand Test'];
$res = 0;
if(strtolower($rawsandtest) == 'yes')
{
$rawsandtest = 1;
}
else
{
$rawsandtest = 0;
}
$isExist = $this->quality_model->checkExist($product,$customer,$name,$type);
//echo $isExist[0]->count;die();
if($isExist[0]->count == 0)
{
$tostore = array('customer_id'=>$customer,'product_id'=>$product,'testtype'=>$name,'uom'=>$uom,'min'=>$min,'max'=>$max,'temp'=>$temp,'type'=>$type,'subtype' => $rawsandtest,'created_by'=>$createdby);
//print_r($tostore);
$res = $this->quality_model->addSpecMast($tostore);
//echo 'i'.$res;
}
else if($isExist[0]->count == 1)
{
$tostore = array('uom'=>$uom,'min'=>$min,'max'=>$max,'temp'=>$temp,'type'=>$type,'subtype' => $rawsandtest,'updated_by'=>$createdby);
$res = $this->quality_model->updateSpecMast($tostore,$product,$customer,$name,$type);
//echo 'u'.$res;
}
$total +=$res;
}
echo $total.'- Specification Saved Successfully';
}
function deleteSpecification()
{
$spname = $this->input->post('param1');
$product = $this->input->post('param2');
$customer = $this->input->post('param3');
$type = $this->input->post('param4');
$res = $this->quality_model->deleteSpecification($spname,$product,$customer,$type);
if($res == 1)
{
echo "Deleted Successfully..!";
}
else
{
echo "Something Went Wrong..!Try later";
}
}
function report()
{
$data['records'] = $this->quality_model->getDataforReport();
$data['emplist'] = $this->quality_model->getEmplistforReport();
$this->global['pageTitle'] = 'Siddharth : Inspection Report Screen';
$this->loadViews("qualityreport", $this->global, $data, NULL);
}
function viewSpec()
{
$prodcutid = $this->uri->segment(3);
$customerid =$this->uri->segment(4);
$type =$this->uri->segment(5);
$data['specifications'] = $this->quality_model->getIndividualSpec($prodcutid,$customerid,$type);
$this->global['pageTitle'] = 'Siddharth : View reports Lists';
$this->loadViews("viewIndSpec", $this->global, $data, NULL);
}
function viewIndReport()
{
$reportid = $this->uri->segment(3);
$data['reportdata'] = $this->quality_model->getIndividualReportData($reportid);
$this->global['pageTitle'] = 'Siddharth : View Individual Report';
$this->loadViews("viewindreport", $this->global, $data, NULL);
}
function viewIndInwardReport()
{
$reportid = $this->uri->segment(3);
$data['reportdata'] = $this->quality_model->getIndividualInwardReportData($reportid);
$this->global['pageTitle'] = 'Siddharth : View Individual Report';
$this->loadViews("viewindInwardreport", $this->global, $data, NULL);
}
function printReportPDF()
{
$type = '';
$html = '';
$filename = '';
if($this->input->post('type'))
{
$type = $this->input->post('type');
}
$reportid = $this->input->post('rid');
if($type != 'INWARD' or $type == ''){
$data['reportdata'] = $this->quality_model->getIndividualReportData($reportid);
$filename = 'PRE DESPATCH INSPECTION REPORT'.$reportid;
$html = $this->load->View("viewindreportpdf",$data,true);
}
else if($type == 'INWARD')
{
$data['reportdata'] = $this->quality_model->getIndividualInwardReportData($reportid);
$filename = 'INWARD MATERIAL INSPECTION REPORT'.$reportid;
$html = $this->load->View("viewindinwardreportpdf",$data,true);
}
// echo $html;die;
ob_end_clean();
$mpdf=new mPDF('utf-8','A4-P',7,10,10, 10, 10, 24, 4, 6);
$mpdf->SetDisplayMode('fullpage');
$mpdf->showImageErrors = true;
$mpdf->list_indent_first_level = 1;
$mpdf->setAutoTopMargin = 'stretch';
$mpdf->setAutoBottomMargin = 'stretch';
$mpdf->WriteHTML($html);
$mpdf->Output($filename.'.pdf','I');
}
function getAllSpecforReport()
{
$productid = $this->input->post('param1');
$customerid = $this->input->post('param2');
$res = '';
if($this->input->post('param3'))
{
$ttype = $this->input->post('param3');
$res = $this->quality_model->getSpecifications($productid,$customerid,$ttype);
}
else
{
$res = $this->quality_model->getSpecifications($productid,$customerid);
}
echo json_encode($res);
}
function saveTestReportData()
{
date_default_timezone_get('Asia/Kolkata');
$testjson = $this->input->post('param1');
$loijson = $this->input->post('param2');
$afsjson = $this->input->post('param3');
$invoiceno = $this->input->post('param4');
$batchno = $this->input->post('param5');
$chart = $this->input->post('param7');
$batchno = date_format(date_create($batchno),'Y-m-d');
$approver = $this->input->post('param6');
$testphpdata = json_decode($testjson,true);
$loiphpdata = json_decode($loijson,true);
$afsphpdata = json_decode($afsjson,true);
//print_r($afsphpdata);die();
$createdon = date('Y-m-d H:i:s');
$createby = $this->session->userdata ( 'userId' );
$document = null;
if(!empty($_FILES['rfile']['name']))
{
$fs = $this->uploadFile();
$document = $fs;
}
$masterreportdata = array('invoiceid'=>$invoiceno,'batchdate'=>$batchno,'createdby'=>$createby,'createdon'=>$createdon,'approvedby'=>$approver,'chartdata'=>$chart,'document'=>$document);
$MASTERID = $this->quality_model->saveMasterTestData($masterreportdata);
$descrition = '';
$min = '';
$max = '';
$uom = '';
$act1 = '';
$act2 = '';
$act3 = '';
$average = '';
$results = '';
$remarks1 = '';
foreach($testphpdata as $data)
{
//print_r($data);die();
$descrition = $data['Description'];
$min = $data['SpecificationMin'];
$max = $data['SpecificationMax'];
$uom = $data['SpecificationUOM'];
$act1 = $data['ACT X1'];
$act2 = $data['ACT X2'];
$act3 = $data['ACT X3'];
$average = $data['Average'];
$results = $data['Results'];
$remarks1 = $data['Remarks'];
$testreportdata = array('testreport_id'=>$MASTERID,'description'=>$descrition,'min'=>$min,'max'=>$max,'UOM'=>$uom,'act1'=>$act1,'act2'=>$act2,'act3'=>$act3,'average'=>$average,'result'=>$results,'remarks'=>$remarks1);
$reportid = $this->quality_model->saveTestReportData($testreportdata);
}
$start = '';
$end = '';
$temp = '';
$w1 = '';
$w2 = '';
$w3 = '';
$loip = '';
$remark = '';
foreach($loiphpdata as $loi)
{
$start = $loi['Heating Timestart'];
$end = $loi['Heating Timeend'];
$temp = $loi['Temp *c'];
$w1 = $loi['Crucible Weight w1(g)'];
$w2 = $loi['Before Heatingw2(g)'];
$w3 = $loi['After Heatingw3(g)'];
$loip = $loi['L.O.I %'];
$remark = $loi['Remarks'];
//echo $loip;
//echo $remark;die();
$loitabledata = array('testreport_id'=>$MASTERID,'start_time'=>$start,'end_time'=>$end,'temp'=>$temp,'w1'=>$w1,'w2'=>$w2,'w3'=>$w3,'loi_percentage'=>$loip,'remarks'=>$remark);
$loiid = $this->quality_model->saveTestReportLOIData($loitabledata);
}
$count =0;
foreach($afsphpdata as $afs)
{
$count++;
if($count != 12){
$ssize = $afs['Sieve Size µm'];
$sandweight = $afs['Sand Weight'];
$Percentage = $afs['Percentage'];
$Multiplier = $afs['Multiplier'];
$Product = $afs['Product'];
$afstabledata = array('testreport_id'=>$MASTERID,'sieve_size'=>$ssize,'sandweight'=>$sandweight,'percentage'=>$Percentage,'multiplier'=>$Multiplier,'product'=>$Product);
$afsid = $this->quality_model->saveTestReportAFSData($afstabledata);
}
}
echo "Report Data Saved Successfully!";
}
function saveInwardTestReportData()
{
date_default_timezone_get('Asia/Kolkata');
$testjson = $this->input->post('param1');
$loijson = $this->input->post('param2');
$afsjson = $this->input->post('param3');
$igrno = $this->input->post('param4');
$igrlineitemno = $this->input->post('param5');
$chart = $this->input->post('param7');
$report_date = $this->input->post('param8');
$report_date = date_format(date_create($report_date),'Y-m-d');
$approver = $this->input->post('param6');
$testphpdata = json_decode($testjson,true);
$loiphpdata = json_decode($loijson,true);
$afsphpdata = json_decode($afsjson,true);
//print_r($afsphpdata);die();
$createdon = date('Y-m-d H:i:s');
$createby = $this->session->userdata ( 'userId' );
$document = null;
if(!empty($_FILES['rfile']['name']))
{
$fs = $this->uploadFile();
$document = $fs;
}
$masterreportdata = array('igrno'=>$igrno,'igrlineitemno'=>$igrlineitemno,'batchdate'=>$report_date,'createdby'=>$createby,'createdon'=>$createdon,'approvedby'=>$approver,'chartdata'=>$chart,'document'=>$document);
//print_r($masterreportdata);die;
$MASTERID = $this->quality_model->saveMasterTestData($masterreportdata);
foreach($testphpdata as $data)
{
//print_r($data);die();
$descrition = $data['Description'];
$min = $data['SpecificationMin'];
$max = $data['SpecificationMax'];
$uom = $data['SpecificationUOM'];
$act1 = $data['ACT X1'];
$act2 = $data['ACT X2'];
$act3 = $data['ACT X3'];
$average = $data['Average'];
$results = $data['Results'];
$remarks1 = $data['Remarks'];
$testreportdata = array('testreport_id'=>$MASTERID,'description'=>$descrition,'min'=>$min,'max'=>$max,'UOM'=>$uom,'act1'=>$act1,'act2'=>$act2,'act3'=>$act3,'average'=>$average,'result'=>$results,'remarks'=>$remarks1);
$reportid = $this->quality_model->saveTestReportData($testreportdata);
}
// $start = '';
// $end = '';
// $temp = '';
// $w1 = '';
// $w2 = '';
// $w3 = '';
// $loip = '';
// $remark = '';
foreach($loiphpdata as $loi)
{
$start = $loi['Heating Timestart'];
$end = $loi['Heating Timeend'];
$temp = $loi['Temp *c'];
$w1 = $loi['Crucible Weight w1(g)'];
$w2 = $loi['Before Heatingw2(g)'];
$w3 = $loi['After Heatingw3(g)'];
$loip = $loi['L.O.I %'];
$remark = $loi['Remarks'];
//echo $loip;
//echo $remark;die();
$loitabledata = array('testreport_id'=>$MASTERID,'start_time'=>$start,'end_time'=>$end,'temp'=>$temp,'w1'=>$w1,'w2'=>$w2,'w3'=>$w3,'loi_percentage'=>$loip,'remarks'=>$remark);
$loiid = $this->quality_model->saveTestReportLOIData($loitabledata);
}
$count =0;
foreach($afsphpdata as $afs)
{
$count++;
if($count != 12){
$ssize = $afs['Sieve Size µm'];
$mesh_size = $afs['Mesh Size'];
$sandweight = $afs['Sand Weight'];
$Percentage = $afs['Percentage'];
$Multiplier = $afs['Multiplier'];
$Product = $afs['Product'];
$afstabledata = array('testreport_id'=>$MASTERID,'sieve_size'=>$ssize,'mesh_size'=>$mesh_size,'sandweight'=>$sandweight,'percentage'=>$Percentage,'multiplier'=>$Multiplier,'product'=>$Product);
$afsid = $this->quality_model->saveTestReportAFSData($afstabledata);
}
}
echo "Report Data Saved Successfully!";
}
function addNewInward_Report()
{
$data['records'] = $this->quality_model->getIGRNOforReport();
$data['reports'] = $this->quality_model->getIGRNOReport(); ;
$data['emplist'] = $this->quality_model->getEmplistforReport();
$this->global['pageTitle'] = 'Siddharth : Inward Inspection';
$this->loadViews("inward_report_create", $this->global,$data,NULL);
}
function getIGRLineItemNo()
{
$igrno = $this->input->post('param1');
$data['igrdetails'] = $this->quality_model->getIGRLineItemNo($igrno);
//print_r($data['igrdetails']);
echo json_encode($data['igrdetails']);
}
function getIGRDetails()
{
$igrno = $this->input->post('param1');
$igrlineitemno = $this->input->post('param2');
$data['igrdetails'] = $this->quality_model->getIGRDataforReport($igrno,$igrlineitemno);
//print_r($data['igrdetails']);
echo json_encode($data['igrdetails']);
}
function pageNotFound()
{
$this->global['pageTitle'] = 'CodeInsect : 404 - Page Not Found';
$this->loadViews("404", $this->global, NULL, NULL);
}
function uploadFile()
{
$pathinfo = pathinfo($_FILES['rfile']['name']);
$config['upload_path'] = 'uploads/QAD/';
$config['allowed_types'] = '*';
$filename = $_FILES['rfile']['name'];
$ext = end(explode('.',$filename));
$ext = strtolower($ext);
$fn = 'qad' . date('Y-m-d') . time() . '.' . $ext;
//print_r($fn);die();
$config['file_name'] = $fn;
//echo $config['file_name'];
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('rfile'))
{
$uploadData = $this->upload->data();
$uploadfilename = $uploadData['file_name'];
return $uploadfilename;
}
else
{
$error = array('error' => $this->upload->display_errors());
$uploadfilename = '';
print_r($error);
return null;
}
}
// start zoho API
function zohobooks_api_fetch_all(){
//echo 'scuess';die();
log_message('error'," ***ZOHO***");
if (isset($_POST['sync']))
{
$con = new mysqli($this->db->hostname,$this->db->username,$this->db->password,$this->db->database);
$ch=curl_init();
$organization_id = '655654572';
$client_id = '1000.T9H4WV9XTGUJRB6HRDMQANB6VT04NH';
$refresh_token = '1000.6608673cee76166874b326b8183a38ab.5fa4b2fa427fe64c5a8700f7170e3a58';
$client_secret = '91ec3fc047493706ae9c15c086741d90ef1de3cd6c';
$redirect_uri = 'https://sia.sidharthindustries.com/SIA/';
$date_today = date('Y-m-d');
$date_yesterday = date('Y-m-d',strtotime('-7 day',strtotime($date_today)));
log_message('error'," ***ZOHO***".$date_today."**".$date_yesterday);
// $date_yesterday = '2022-09-01';
// echo $date_yesterday.'-'.$date_today;//die();
$url_org ="https://books.zoho.com/api/v3/invoices?";
$fields=array(
"usestate" => "true",
"organization_id" =>"655654572",
"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);
$result=curl_exec($ch);
log_message('error'," Response ".$result);
$res = json_decode($result);
if($res->code=="57")
{
log_message('error'," IF condition ");
$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);
$ress = json_decode($res_token);
$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);
$inv_array = $res['invoices'];
foreach($inv_array[0] as $inv){
$invoice_id = $inv['invoice_id'];
log_message('error', " # Invoice ID : " . $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'];
log_message('error', "Zoho URL Invoice ( " . $invoice_id . " )");
log_message('error', "Response : ".$result2);
// print_r($result2);
// exit();
}
}
//echo count($res2);
log_message('error', "Zoho URL Invoice ( " . $invoice_id . " ) count ".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'];
$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_isgt_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'];
log_message('error invoice_number',$invoice_number);
$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);
log_message('error invoice chk ',$sql_invoice_chk);
log_message('error return rows ',$res_invoice->num_rows);
if($res_invoice->num_rows<=0)
{
log_message('error ',$res_invoice->num_rows);
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 16; $i++)
{
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$url_key = $randomString;
$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);
log_message('error'," Invoice Inserted ".$result_invoice_insert);
log_message('error'," Invoice Inserted id ".$con->insert_id);
log_message('error',"query invoice => ",$sql_invoice_insert);
//exit();
$invoice_insert_id=$con->insert_id;
$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;
//tax amount -- ip_invoice_item_amounts - table
$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);
//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);
$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);
$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);
$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";
}
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 'new invoice inserted count- '. $insert_count . '/'.'cancelled status updated count - ' . $update_count;
}
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
}
?>