diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 670f66f9..53adc2dd 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -128,6 +128,8 @@ $routes->get('purchaseorder/AddComplaint','Purchaseorder::AddComplaint'); $routes->post('purchaseorder/SaveComplaint','Purchaseorder::SaveComplaint'); $routes->get('purchaseorder/Nonconfirmativelist','Purchaseorder::Nonconfirmativelist'); +//Zohobooks sync +$routes->post('user/zohobooks_api_fetch_all','User::zohobooks_api_fetch_all'); // Application Payslip Routes $routes->match(['get', 'post', 'put', 'delete'],'monthlyListings', 'Monthlypay::monthlyListing'); @@ -175,6 +177,7 @@ $routes->match(['get', 'post', 'put', 'delete'],'EmergencyPO', 'Emergencypurchas $routes->match(['get', 'post', 'put', 'delete'],'AdvanceRequest', 'Purchaseorder::advancerequest'); $routes->match(['get', 'post', 'put', 'delete'],'PORelease', 'Purchaseorder::porelease'); $routes->match(['get', 'post', 'put', 'delete'],'ViewPO', 'Purchaseorder::viewfullpurchaseorder'); + // $routes->match(['get', 'post', 'put', 'delete'],'POApproval', 'Purchaseorder::poapproval'); $routes->get('amendmentpurchaseorder','Amendmentpurchaseorder::index'); $routes->get('EditAmendPO', 'Amendmentpurchaseorder::EditAmendPurchaseOrder'); @@ -195,6 +198,7 @@ $routes->get('EditPO', 'Purchaseorder::EditPurchaseOrder'); $routes->get('purchaseorder/CreatePOPrint', 'Purchaseorder::CreatePOPrint'); $routes->post('purchaseorder/ReleasePO', 'Purchaseorder::ReleasePO'); +$routes->post('purchaseorder/EditRevenuePurchaseOrder', 'Purchaseorder/EditRevenuePurchaseOrder'); $routes->post('VerifyWithSupplierForPendingPO', 'Purchaseorder::VerifyWithSupplierForPendingPO'); @@ -310,3 +314,6 @@ $routes->get('qualityreportlist', 'Quality::reportList'); $routes->get('qualityreportlistinward', 'Quality::reportListInward'); $routes->get('shortagematerialListing', 'Rawmaterialdetails::shortagematerialListing'); +$routes->get('/pdf', 'PdfController::index'); +$routes->match(['get', 'post', 'put', 'delete'],'pdf/preview', 'PdfController::preview'); +$routes->match(['get', 'post', 'put', 'delete'],'pdf/download', 'PdfController::download'); diff --git a/app/Controllers/Purchaseorder.php b/app/Controllers/Purchaseorder.php index ab012300..19255697 100644 --- a/app/Controllers/Purchaseorder.php +++ b/app/Controllers/Purchaseorder.php @@ -1297,69 +1297,133 @@ class Purchaseorder extends BaseController // echo \Mpdf\Mpdf::VERSION; // die; - $mpdf = new Mpdf([ - 'mode' => 'utf-8', - 'format' => 'A4', // Page format: A4 with portrait orientation - // 'format' => [148, 210], // Set custom width and height in millimeters - 'default_font_size' => 6, - 'default_font' => 'sans-serif', - 'margin_left' => 10, - 'margin_right' => 10, - 'margin_top' => 10, - 'margin_bottom' => 10, - 'margin_header' => 82, - 'margin_footer' => 38, - 'orientation' => 'P', - ]); + // $mpdf = new Mpdf([ + // 'mode' => 'utf-8', + // 'format' => 'A4', // Page format: A4 with portrait orientation + // // 'format' => [148, 210], // Set custom width and height in millimeters + // 'default_font_size' => 6, + // 'default_font' => 'sans-serif', + // 'margin_left' => 10, + // 'margin_right' => 10, + // 'margin_top' => 10, + // 'margin_bottom' => 10, + // 'margin_header' => 82, + // 'margin_footer' => 38, + // 'orientation' => 'P', + // ]); - $mpdf->autoLangToFont = true; - $mpdf->autoScriptToLang = true; - // Set PDF properties - $mpdf->SetTitle('Invoice'); - $mpdf->SetAuthor($data['CompanyDetails'][0]->CompanyName); - $mpdf->SetCreator(''); + // $mpdf->autoLangToFont = true; + // $mpdf->autoScriptToLang = true; + // // Set PDF properties + // $mpdf->SetTitle('Invoice'); + // $mpdf->SetAuthor($data['CompanyDetails'][0]->CompanyName); + // $mpdf->SetCreator(''); - $htmlFooter = '
-
'; - $mpdf->setHTMLFooter($htmlFooter); + // $htmlFooter = '
+ //
'; + // $mpdf->setHTMLFooter($htmlFooter); - // Generate the PDF content (HTML) - if ($PoStatus == PO_RELEASED || $PoStatus == PO_SERVICE_COMPLETED || $PoStatus == MRIR_APPROVED || $PoStatus == MRIR_REJECTED || $PoStatus == IGR_CREATED || $PoStatus == '' || $PoStatus == SPECIAL_PO) { - $mpdf->SetWatermarkText(''); - } else { - $mpdf->SetWatermarkText('DRAFT'); - } + // // Generate the PDF content (HTML) + // if ($PoStatus == PO_RELEASED || $PoStatus == PO_SERVICE_COMPLETED || $PoStatus == MRIR_APPROVED || $PoStatus == MRIR_REJECTED || $PoStatus == IGR_CREATED || $PoStatus == '' || $PoStatus == SPECIAL_PO) { + // $mpdf->SetWatermarkText(''); + // } else { + // $mpdf->SetWatermarkText('DRAFT'); + // } - $WatermarkImage = base_url()."public/assets/images/mpdf.png"; - $mpdf->SetWatermarkImage($WatermarkImage, 8, 10); + // $WatermarkImage = base_url()."public/assets/images/mpdf.png"; + // $mpdf->SetWatermarkImage($WatermarkImage, 8, 10); - $mpdf->showWatermarkImage = true; - $mpdf->watermark_font = 'DejaVuSansCondensed'; - $mpdf->showWatermarkText = true; + // $mpdf->showWatermarkImage = true; + // $mpdf->watermark_font = 'DejaVuSansCondensed'; + // $mpdf->showWatermarkText = true; // Set auto margins - $mpdf->setAutoTopMargin = 'stretch'; - $mpdf->setAutoBottomMargin = 'stretch'; + // $mpdf->setAutoTopMargin = 'stretch'; + // $mpdf->setAutoBottomMargin = 'stretch'; // Set HTML header - $HtmlHeading = view('includes/pdfheader', $data); - $mpdf->SetHTMLHeader($HtmlHeading); + // $HtmlHeading = view('includes/pdfheader', $data); + // $mpdf->SetHTMLHeader($HtmlHeading); // Set HTML body - $html = view('capitalpopdf', $data); - $mpdf->WriteHTML($html); // Use WriteHTML() method to add body content + // $html = view('capitalpopdf', $data); + // $mpdf->WriteHTML($html); // Use WriteHTML() method to add body content // Set HTML footer - $HTMLFooter = view('includes/pdffooter', $data); - $mpdf->setFooter($HTMLFooter . "Page {PAGENO} of {nb}"); + // $HTMLFooter = view('includes/pdffooter', $data); + // $mpdf->setFooter($HTMLFooter . "Page {PAGENO} of {nb}"); // Output PDF - $mpdf->Output('CapitalPOReport' . $PONO . '.pdf', 'D'); + // $mpdf->Output('CapitalPOReport' . $PONO . '.pdf', 'D'); + // $pdf = myPdf('P'); + + // $pdf->writeHTML( "Hello World" ); + + // $pdf->Output( FCPATH . "writable/uploads/test.pdf", 'F'); + + // return redirect()->to( site_url('/') ); } + function myPdf( $orientation ) { + + $defaultFontConfig = ( new Mpdf\Config\FontVariables() )->getDefaults(); + $fontData = $defaultFontConfig[ 'fontdata' ] + [ + 'homemadeapple' => [ + 'R' => 'HomemadeApple-Regular.ttf', + ], + 'roboto' => [ + 'R' => 'Roboto-Regular.ttf', + 'I' => 'Roboto-Italic.ttf', + 'B' => 'Roboto-Bold.ttf', + 'BI' => 'Roboto-BoldItalic.ttf', + ], + 'robotocondensed' => [ + 'R' => 'RobotoCondensed-Regular.ttf', + 'I' => 'RobotoCondensed-Italic.ttf', + 'B' => 'RobotoCondensed-Bold.ttf', + 'BI' => 'RobotoCondensed-BoldItalic.ttf', + ], + ]; + $defaultConfig = ( new Mpdf\Config\ConfigVariables() )->getDefaults(); + $fontDirs = $defaultConfig[ 'fontDir' ]; + if ( $orientation == 'L' ) { + $myPdf = new \Mpdf\Mpdf( + array( + 'format' => 'A4-L', + 'margin_left' => 10, + 'margin_right' => 10, + 'margin_top' => 30, + 'margin_bottom' => 20, + 'margin_header' => 8, + 'margin_footer' => 8, + 'orientation' => 'L', + 'mode' => 's', + 'fontDir' => array_merge( $fontDirs, [ FCPATH . 'public/assets-app/fonts', ] ), + 'fontdata' => $fontData, + ) ); + } + else { + $myPdf = new \Mpdf\Mpdf( array( + 'format' => 'A4-P', + 'margin_left' => 20, + 'margin_right' => 10, + 'margin_top' => 40, + 'margin_bottom' => 20, + 'margin_header' => 8, + 'margin_footer' => 8, + 'orientation' => 'P', + 'mode' => 's', + 'fontDir' => array_merge( $fontDirs, [ FCPATH . 'public/assets-app/fonts', ] ), + 'fontdata' => $fontData, + ) ); + } + + return $myPdf; + + } diff --git a/app/Controllers/User.php b/app/Controllers/User.php index 3c513982..451f6115 100644 --- a/app/Controllers/User.php +++ b/app/Controllers/User.php @@ -10,7 +10,8 @@ use App\Models\Costcenter_model; use App\Models\Dashboard_model; use App\Models\Employeedetails_model; use App\Models\User_model; - +use App\Models\Zohobooks_api_model; +require_once 'vendor/autoload.php'; /** * Module : User @@ -1269,310 +1270,326 @@ class User extends BaseController } } - - // start zoho API 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($_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 = get_current_date(); - // $date_yesterday = format_date($date_today, 1); - //$date_today = '2022-08-23'; - $date_yesterday = format_date($date_today, 7); - // echo $date_yesterday;die(); - - - $url_org = "https://books.zoho.com/api/v3/invoices?"; - - $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); - - $result = curl_exec($ch); - - $res = json_decode($result); - - - - if ($res->code == "57") { - - //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); - - $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); + 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'); + } + + $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(); + log_message('ERROR', $res['message']); + echo "An Error Occur ==> " .$res['message']." \n
"; + throw new \Exception($res['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 !== "") { - - $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); - + // $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']; - $sku = $rs['line_items'][0]['sku']; $invoice_number = $rs['invoice_number']; $invoice_date = $rs['date']; + $status = $rs['status']; $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) { - - + $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); + } $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); - - $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"; + $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'); + 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 + 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'); + 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 + 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'); + 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 + 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'); + 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 + 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'); + 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 + 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'); + 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); + } + $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 { + 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") { - $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; + 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 . '/' . 'cancelled status updated count - ' . $update_count; - } else { - - $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); - - } + echo 'new invoice inserted count- ' . $insert_count . '/' . 'status updated count - ' . $update_count; + }else{ + $err = 'invoice details not founded ( from '.$date_yesterday.' into '.$date_today.' )'; + log_message('ERROR', $err); + echo $err; + throw new \Exception($err); } - curl_close($ch); + // 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()); } } - // END zoho API + // END zoho API } \ No newline at end of file diff --git a/app/Helpers/datetime_helper.php b/app/Helpers/datetime_helper.php index 20350485..3f781570 100755 --- a/app/Helpers/datetime_helper.php +++ b/app/Helpers/datetime_helper.php @@ -143,6 +143,28 @@ if (!function_exists('get_shorten_financial_year')) { } } +if (!function_exists('add_one_hour_date')) { + function add_one_hour_date($dateString) { + $date = new DateTime($dateString); + $date->add(new DateInterval('PT1H')); + return $date->format('Y-m-d H:i:s'); + } +} + +if (!function_exists('get_date_days_ago')) { + function get_date_days_ago($date = null, $format = 'Y-m-d', $days = '-2 days') { + + if (is_null($date)) { + $date = new \DateTime('now'); + } else { + $date = new \DateTime($date); + } + $timestamp = $date->getTimestamp(); + $newDate = date($format, strtotime($days, $timestamp)); + + return $newDate; + } +} if (!function_exists('generate_pos_number')) { function generate_pos_number($last_pono) { diff --git a/app/Helpers/zoho_helper.php b/app/Helpers/zohobook_helper.php similarity index 57% rename from app/Helpers/zoho_helper.php rename to app/Helpers/zohobook_helper.php index bf5bd60c..e0495d85 100644 --- a/app/Helpers/zoho_helper.php +++ b/app/Helpers/zohobook_helper.php @@ -108,3 +108,67 @@ if (! function_exists('getheringInvoiceSubDetails')) { } ?> + +function generateNewToken() +{ + $ch=curl_init(); + + $organization_id = '776847408'; + $client_id = '1000.0WMDLCXAZV5DR60IKE9P49YTYGVL6C'; + $refresh_token = '1000.0fcf314cc7b7b598df6e519461bbe9b8.f77688a2254137532ae33ad5e152a064'; + $client_secret = '99da3a05cfa13cf91eb6476a3c44cfa3e21b6106a8'; + $redirect_uri = 'https://resicoindustries.com/RIA/'; + $accesstoken = '1000.095b66fc95fc81874c1cfc7d15b0904e.10bc9956d9b9ec14cce1350ee0cf8254'; + + + $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'; + $headers=array('Authorization:Zoho-oauthtoken '.$accesstoken,'Content-Type:application/json'); + 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); + return $ress->access_token; +} + + +function getheringInvoiceDetails($date_today , $date_yesterday , $accesstoken) +{ + $ch=curl_init(); + $organization_id = '776847408'; + $url_org ="https://books.zoho.com/api/v3/invoices?"; + $fields=array("usestate" => "true","organization_id" => $organization_id,"date_start" => $date_yesterday,"date_end" => $date_today); + $data_all = http_build_query($fields); + $headers=array('Authorization:Zoho-oauthtoken '.$accesstoken,'Content-Type:application/json'); + $data_access = $url_org.$data_all; + curl_setopt($ch, CURLOPT_URL, $url_org.$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); + $data['headers_new'] = $headers; + $data['data'] = curl_exec($ch); + return $data; +} + + +function getheringInvoiceSubDetails($invoice_id , $organization_id , $headers) +{ + $ch=curl_init(); + $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); + return curl_exec($ch); +} diff --git a/app/Views/serviceporeport.php b/app/Views/serviceporeport.php index 413e2099..69ab6179 100644 --- a/app/Views/serviceporeport.php +++ b/app/Views/serviceporeport.php @@ -39,7 +39,7 @@ $Adv='';
- + diff --git a/app/Views/supplierListing.php b/app/Views/supplierListing.php index 1d4dc686..84c0458c 100644 --- a/app/Views/supplierListing.php +++ b/app/Views/supplierListing.php @@ -44,7 +44,7 @@
SNO# PONO#
- + diff --git a/app/Views/userListing.php b/app/Views/userListing.php index 1c9348f5..ab347e3c 100644 --- a/app/Views/userListing.php +++ b/app/Views/userListing.php @@ -42,7 +42,7 @@
Supplier ID Supplier Name
- + diff --git a/app/Views/viewfullpurchaseorder.php b/app/Views/viewfullpurchaseorder.php index e7bee22c..41827d3e 100644 --- a/app/Views/viewfullpurchaseorder.php +++ b/app/Views/viewfullpurchaseorder.php @@ -19,7 +19,7 @@
User Id User Name
- + diff --git a/public/assets/images/spinner.gif b/public/assets/images/spinner.gif new file mode 100644 index 00000000..06c091d7 Binary files /dev/null and b/public/assets/images/spinner.gif differ diff --git a/public/uploads/spinner.gif b/public/uploads/spinner.gif new file mode 100644 index 00000000..06c091d7 Binary files /dev/null and b/public/uploads/spinner.gif differ
# PONO#