diff --git a/app/Config/Routes.php b/app/Config/Routes.php
index 80aac532..949b564f 100755
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -438,11 +438,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->match(['GET', 'POST'],'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 1f962afc..59ab3187 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();
@@ -1337,17 +1340,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) {
@@ -1357,15 +1356,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'];
@@ -1377,17 +1374,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
@@ -1711,6 +1714,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 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Sales
+
+ -
+
Credit Notes List
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ | Date |
+ Credit Note |
+ Invoice |
+ Client |
+ Item Price |
+ Item qty |
+ Sub Total |
+ GST |
+ Total |
+ Status |
+
+
+
+
+
+
+
+
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ 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 @@