From 1b3219a6434651a6900f6c0704251f286f208478 Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Thu, 6 Mar 2025 17:36:00 +0530 Subject: [PATCH] Credit notes : GWM --- app/Config/Routes.php | 4 +- app/Controllers/Sales.php | 30 +++ app/Controllers/User.php | 64 +++-- app/Helpers/zohobook_helper.php | 89 +++---- app/Models/CreditNoteModel.php | 89 +++++++ app/Views/credit_notes.php | 355 +++++++++++++++++++++++++++ app/Views/includes/new_header.php | 1 + app/Views/viewinwardgateregister.php | 8 +- 8 files changed, 564 insertions(+), 76 deletions(-) create mode 100644 app/Models/CreditNoteModel.php create mode 100644 app/Views/credit_notes.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 6f97e693..107ed188 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -435,11 +435,11 @@ $routes->get('getInvoiceAttachment', 'Sales::getInvoiceAttachments'); $routes->get('getInvoiceAttachments', 'Sales::getInvoiceAttachments'); $routes->get('deleteAttachment', 'Sales::deleteAttachment'); $routes->post('saveAttachment', 'Sales::saveAttachment'); -$routes->get('sales_invoice', 'Sales::sales_invoice'); -$routes->post('sales_invoice', 'Sales::sales_invoice'); $routes->post('download_sales_invoice', 'Sales::download_sales_invoice'); $routes->post('updateInvRecQty', 'Sales::updateInvRecQty'); +$routes->match(['GET', 'POST'],'sales_invoice', 'Sales::sales_invoice'); $routes->match(['GET', 'POST'], 'sales_report', 'Sales::sales_report'); +$routes->match(['GET', 'POST'],'credit_notes', 'Sales::credit_notes'); // transporter Routes $routes->get('transporterListing', 'Supplier::transporterListing'); diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php index a58fb3ee..e81ce7f9 100755 --- a/app/Controllers/Sales.php +++ b/app/Controllers/Sales.php @@ -6,6 +6,7 @@ use App\Controllers\BaseController; use App\Models\Ipinvoice_model; use App\Models\Ipattachment_model; +use App\Models\CreditNoteModel; require_once 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; @@ -15,6 +16,7 @@ class Sales extends BaseController { protected $ipinvoice_model; protected $ipattachment_model; + protected $CreditNoteModel; protected $session; public function __construct() @@ -22,6 +24,7 @@ class Sales extends BaseController parent::__construct(); $this->ipinvoice_model = new Ipinvoice_model(); $this->ipattachment_model = new Ipattachment_model(); + $this->CreditNoteModel = new CreditNoteModel(); $this->session = session(); $this->isLoggedIn(); } @@ -119,6 +122,33 @@ class Sales extends BaseController } + // credit_notes List + function credit_notes(){ + $fromDate=''; + $toDate=''; + + if ($this->request->getMethod() === 'GET') + { + $toDate = date('Y-m-d'); + $fromDate = date('Y-m-d', strtotime('-90 days', strtotime($toDate))); + } elseif ($this->request->getMethod() === 'POST') { + $fromDate = $this->request->getPost('fromDate'); + $toDate = $this->request->getPost('toDate'); + $fromDate = date('Y-m-d', strtotime($fromDate)); + $toDate = date('Y-m-d', strtotime($toDate)); + } + + $this->global['pageTitle'] = 'Credit Notes'; + $data['credit_notes'] = $this->CreditNoteModel->getAllCreditNotes($fromDate , $toDate); + // dd($data); + $data['fromDate'] = date('d-m-Y', strtotime($fromDate)); + $data['toDate'] = date('d-m-Y', strtotime($toDate)); + + + $this->loadViews("credit_notes", $this->global, $data, NULL); + } + + public function sales_report() { $headline = ''; diff --git a/app/Controllers/User.php b/app/Controllers/User.php index 6209aefd..49acf7b1 100755 --- a/app/Controllers/User.php +++ b/app/Controllers/User.php @@ -14,6 +14,7 @@ use App\Models\User_model; use App\Models\Ipinvoice_model; use App\Models\Ipattachment_model; use App\Models\Zohobooks_api_model; +use App\Models\CreditNoteModel; require_once 'vendor/autoload.php'; @@ -34,6 +35,7 @@ class User extends BaseController protected $user_model; protected $ipinvoice_model; protected $ipattachment_model; + protected $CreditNoteModel; protected $session; /** @@ -50,6 +52,7 @@ class User extends BaseController $this->user_model = new User_model(); $this->ipinvoice_model = new Ipinvoice_model(); $this->ipattachment_model = new Ipattachment_model(); + $this->CreditNoteModel = new CreditNoteModel(); $this->session = session(); helper('form'); //$this->load->library('form_validation'); $this->isLoggedIn(); @@ -1325,17 +1328,13 @@ class User extends BaseController } } + + 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) { @@ -1345,15 +1344,13 @@ class User extends BaseController $date_today = date('Y-m-d'); $date_yesterday = date('Y-m-d', strtotime('-3 day', strtotime($date_today))); } - // 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']; @@ -1365,17 +1362,23 @@ class User extends BaseController // $token = generateNewToken(); $tokendata['token'] = $token; //$Zohobook_model->update($tokendata , 1,'zohobook_accesstoken'); $result = getheringInvoiceDetails($date_today, $date_yesterday, $token); + $headers_new = $result['headers_new']; $res = json_decode($result['data'], true); $inv_array = $res['invoices']; - // $cteditNotes = getAllCreditNotes($date_today, $date_yesterday, $token); - // dd($cteditNotes); - // dd($inv_array); - // print_r($inv_array); - // echo "rrrr"; - // die(); + $tokenWithCreditNoteScope = generateNewTokenForCreditNoteScope(); + $cteditNotesRes = getAllCreditNotes($date_today, $date_yesterday, $tokenWithCreditNoteScope); + $cteditNotesData = json_decode($cteditNotesRes['data'],true); + + if(count($cteditNotesData['creditnotes'])) + { + $this->saveOrUpdateCreditNote($cteditNotesData['creditnotes']); + } + + + foreach ($inv_array as $inv) { $invoice_id = $inv['invoice_id']; //filter only einvoice status pushed invoices only @@ -1699,6 +1702,27 @@ class User extends BaseController } // END zoho API + public function saveOrUpdateCreditNote($data) + { + + foreach ($data as $key => $value) { + + $existingCreditNote = $this->CreditNoteModel->where('creditnote_id', $value['creditnote_id'])->first(); + + if ($existingCreditNote) { + // Update existing record + $this->CreditNoteModel->update($value['creditnote_id'], $value); + + } else { + // Insert new record + $this->CreditNoteModel->insert($value); + + } + } + + } + + function fetchEmployeeDetails() { $EmpID = $this->request->getPost('EmpID'); diff --git a/app/Helpers/zohobook_helper.php b/app/Helpers/zohobook_helper.php index 4d957e05..c92a8c99 100755 --- a/app/Helpers/zohobook_helper.php +++ b/app/Helpers/zohobook_helper.php @@ -110,69 +110,58 @@ if (! function_exists('test_helper')) { } } + +if (! function_exists('generateNewTokenForCreditNoteScope')) { + function generateNewTokenForCreditNoteScope() + { + // Initialize cURL + $ch = curl_init(); + + // Zoho API credentials + $organization_id = '776847408'; + $client_id = '1000.0WMDLCXAZV5DR60IKE9P49YTYGVL6C'; + $refresh_token = '1000.461f55fee6248d21aafe4c10b05c37dc.9fc8b11993aba1ca95e539740f3d411f'; + $client_secret = '99da3a05cfa13cf91eb6476a3c44cfa3e21b6106a8'; + $redirect_uri = 'https://resicoindustries.com/RIA'; + + + // Construct URL to get the authentication token + $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'; + + // Set cURL options + curl_setopt($ch, CURLOPT_URL, $url_getauthtoken); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + // Execute cURL request + $res_token = curl_exec($ch); + + // Decode the response + $ress = json_decode($res_token); + + // Return access token + return $ress->access_token; + } +} + if (! function_exists('getAllCreditNotes')) { - // function getAllCreditNotes($date_today, $date_yesterday, $accesstoken) - // { - // // Initialize cURL - // $ch = curl_init(); - - // // Zoho API credentials - // $organization_id = '776847408'; - // // $url_org = "https://books.zoho.com/api/v3/creditnotes?"; - // $url_org = "https://www.zohoapis.com/books/v3/creditnotes?"; - - // // Construct query parameters - // $fields = array( - // "usestate" => "true", - // "organization_id" => $organization_id, - // "date_start" => $date_yesterday, - // "date_end" => $date_today - // ); - // $data_all = http_build_query($fields); - - // // Set headers - // $headers = array( - // 'Authorization: Zoho-oauthtoken '.$accesstoken, - // 'Content-Type: application/json' - // ); - - // // Construct URL - // $data_access = $url_org.$data_all; - - // // Set cURL options - // curl_setopt($ch, CURLOPT_URL, $data_access); - // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - - // curl_setopt($ch, CURLOPT_HEADER, 0); - // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); - // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - - // // Execute cURL request - // $result = curl_exec($ch); - - // // Store headers and data - // $data['headers_new'] = $headers; - // $data['data'] = curl_exec($ch); - // // Return data - // return $data; - // } - function getAllCreditNotes($date_today, $date_yesterday, $accesstoken) { // Initialize cURL $ch = curl_init(); - var_dump($accesstoken); + // Zoho API credentials $organization_id = '776847408'; - $url_org = "https://books.zoho.com/api/v3/creditnotes?"; + $url_org = "https://www.zohoapis.com/books/v3/creditnotes?"; // Construct query parameters manually (Zoho might need raw values) - $query_string = "usestate=true&organization_id=$organization_id&date_start=$date_yesterday&date_end=$date_today"; + $query_string = "organization_id=$organization_id&date_start=$date_yesterday&date_end=$date_today"; // Set headers $headers = array( 'Authorization: Zoho-oauthtoken ' . $accesstoken, - 'Content-Type: application/json' + // 'Content-Type: application/json' ); // Set cURL options diff --git a/app/Models/CreditNoteModel.php b/app/Models/CreditNoteModel.php new file mode 100644 index 00000000..5bcd0406 --- /dev/null +++ b/app/Models/CreditNoteModel.php @@ -0,0 +1,89 @@ +where('date >=', $fromDate ) + ->where('date <=', $toDate ) + ->orderBy('date', 'DESC') + ->findAll(); + } + + /** + * Fetch a single credit note by ID + */ + public function getCreditNoteById($id) + { + return $this->where('creditnote_id', $id)->first(); + } + + /** + * Insert a new credit note + */ + public function createCreditNote($data) + { + return $this->insert($data); + } + + /** + * Update an existing credit note + */ + public function updateCreditNote($id, $data) + { + return $this->update($id, $data); + } + + /** + * Delete a credit note by ID + */ + public function deleteCreditNote($id) + { + return $this->delete($id); + } +} diff --git a/app/Views/credit_notes.php b/app/Views/credit_notes.php new file mode 100644 index 00000000..d6fab6ce --- /dev/null +++ b/app/Views/credit_notes.php @@ -0,0 +1,355 @@ + + + + + +
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+ + +
+ +
+
+ + + + + + + + + + + + +
+ + +
+
+
+ +
+ +
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DateCredit NoteInvoiceClient Item PriceItem qtySub TotalGSTTotalStatus
+
+
+
+
+
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/app/Views/includes/new_header.php b/app/Views/includes/new_header.php index 0601e659..10910a6d 100755 --- a/app/Views/includes/new_header.php +++ b/app/Views/includes/new_header.php @@ -654,6 +654,7 @@