CHANGE_SALES_DASHBOARD_DATA

This commit is contained in:
aadhavan valli 2024-08-19 11:15:32 +05:30
parent 84dccac2ec
commit 065f49cbcd
8 changed files with 376 additions and 107 deletions

View File

@ -27,7 +27,6 @@ $routes->post('user/Deleteuserdepartment', 'User::Deleteuserdepartment');
// $routes->match(['GET', 'POST', 'PUT', 'DELETE'],'resetPasswordConfirmUser/(:any)/(:any)', 'Login::resetPasswordConfirmUser/$1/$2');
// $routes->match(['GET', 'POST', 'PUT', 'DELETE'],'createPasswordUser', 'Login::createPasswordUser');
$routes->get('dashboard', 'User::index');
$routes->get('sales_invoice', 'User::sales_invoice');
$routes->get('reports', 'Report::index');
// User Routes
@ -360,9 +359,10 @@ $routes->get('shortagematerialListing', 'Rawmaterialdetails::shortagematerialLis
$routes->get('inprocess', 'Inprocess::index');
// Sales Invoice routes
$routes->get('invoicedetails/ViewInvoice', 'User::ViewInvoice');
$routes->get('getInvoiceAttachments', 'User::getInvoiceAttachments');
$routes->get('deleteAttachment', 'User::deleteAttachment');
$routes->post('saveAttachment', 'User::saveAttachment');
// Sales Controller
$routes->get('sales_dashboard', 'Sales::sales_dashboard');
$routes->get('invoicedetails/ViewInvoice', 'Sales::ViewInvoice');
$routes->get('getInvoiceAttachments', 'Sales::getInvoiceAttachments');
$routes->get('deleteAttachment', 'Sales::deleteAttachment');
$routes->post('saveAttachment', 'Sales::saveAttachment');
$routes->get('sales_invoice', 'Sales::sales_invoice');

127
app/Controllers/Sales.php Normal file
View File

@ -0,0 +1,127 @@
<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use App\Models\Ipinvoice_model;
require_once 'vendor/autoload.php';
class Sales extends BaseController
{
protected $ipinvoice_model;
protected $session;
public function __construct()
{
parent::__construct();
$this->ipinvoice_model = new Ipinvoice_model();
$this->session = session();
$this->isLoggedIn();
}
public function sales_dashboard()
{
$this->global['pageTitle'] = 'Sales Dashboard';
$data = [];
$data['today_sales'] = $this->ipinvoice_model->todaySales();
$data['this_month_sales'] = $this->ipinvoice_model->thisMonthSales();
$data['this_year_sales'] = $this->ipinvoice_model->thisYearSales();
$data['this_year_sales_trend'] = $this->ipinvoice_model->thisYearSalesTrend();
$data['get_today_invoices'] = $this->ipinvoice_model->getTodayInvoices();
echo "<pre>";
print_r($data);die;
$this->loadViews("sales_dashboard", $this->global, $data, NULL);
}
// Sales Invoice List
function sales_invoice(){
$this->global['pageTitle'] = 'Sales Invoice';
$data['sales_invoice'] = $this->ipinvoice_model->saleInvoiceListing();
// echo "<pre>";
// print_r($data);die;
$this->loadViews("sales_invoice", $this->global, $data, NULL);
}
// View Invoice
function ViewInvoice($InvoiceNO = '')
{
if ($InvoiceNO == '') {
$InvoiceID = $_GET['InvoiceID'];
} else {
$InvoiceID = $InvoiceNO;
}
$data['invoiceDetails'] = $this->ipinvoice_model->getInvoice($InvoiceID);
$data['invoiceAttachment'] = $this->ipinvoice_model->getinvoiceAttachment($InvoiceID);
// echo "<pre>";
// print_r($data);die;
$this->global['pageTitle'] = 'View Invoice';
$this->loadViews("editInvoice", $this->global, $data, NULL);
}
// Invoice Attachments
function getInvoiceAttachments(){
$data['invoiceAttachment'] = $this->ipinvoice_model->getinvoiceAttachment($this->request->getGet('invoice_id'));
return json_encode($data);
}
// Delete Attachment
function deleteAttachment()
{
$invoice_attachment_id = $this->request->getGet('invoice_attachment_id'); // Get the attachment ID from the request
// Ensure ID is not empty
if ($invoice_attachment_id) {
// Load the model
// Call the model method to delete the attachment
$result = $this->ipinvoice_model->deleteAttachment($invoice_attachment_id);
if ($result) {
echo json_encode(['status' => 'success', 'message' => 'Attachment deleted successfully.']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to delete attachment.']);
}
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid attachment ID.']);
}
}
// Save Attachment
function saveAttachment() {
$invoiceId = $this->request->getPost('invoice_id');
$fileNames = $this->request->getPost('file_name[]');
$files = $this->request->getFiles();
if ($files && isset($files['emp_file']) && is_array($files['emp_file'])) {
foreach ($files['emp_file'] as $key => $file) {
if ($file->isValid() && !$file->hasMoved()) {
// Generate a unique name for the file
$newFileName = $file->getRandomName();
// Move the file to the target directory
$file->move('./public/uploads/images/invoice_files/', $newFileName);
// Insert the file info into the database
$data = [
'invoice_id' => $invoiceId,
'file_name' => $newFileName,
'attachment_name' => $fileNames[$key]
];
$this->ipattachment_model->addNewAttachment($data);
}
}
} else {
return $this->response->setJSON(['success' => false, 'message' => 'No files uploaded or incorrect input name.']);
}
return $this->response->setJSON(['success' => true]);
}
}

View File

@ -1637,99 +1637,4 @@ class User extends BaseController
}
// END zoho API
// Sales Invoice List
function sales_invoice(){
$this->global['pageTitle'] = 'Sales Invoice';
$data['sales_invoice'] = $this->ipinvoice_model->saleInvoiceListing();
// echo "<pre>";
// print_r($data);die;
$this->loadViews("sales_invoice", $this->global, $data, NULL);
}
// Sales Invoice List
// Sales Invoice
function ViewInvoice($InvoiceNO = ''){
if ($InvoiceNO == '') {
$InvoiceID = $_GET['InvoiceID'];
} else {
$InvoiceID = $InvoiceNO;
}
$data['invoiceDetails'] = $this->ipinvoice_model->getInvoice($InvoiceID);
$data['invoiceAttachment'] = $this->ipinvoice_model->getinvoiceAttachment($InvoiceID);
// echo "<pre>";
// print_r($data);die;
$this->global['pageTitle'] = 'View Invoice';
$this->loadViews("editInvoice", $this->global, $data, NULL);
}
// Sales Invoice
// Sales Invoice
function getInvoiceAttachments(){
$data['invoiceAttachment'] = $this->ipinvoice_model->getinvoiceAttachment($this->request->getGet('invoice_id'));
return json_encode($data);
}
// Sales Invoice
// User Controller
function deleteAttachment() {
$invoice_attachment_id = $this->request->getGet('invoice_attachment_id'); // Get the attachment ID from the request
// Ensure ID is not empty
if ($invoice_attachment_id) {
// Load the model
// Call the model method to delete the attachment
$result = $this->ipinvoice_model->deleteAttachment($invoice_attachment_id);
if ($result) {
echo json_encode(['status' => 'success', 'message' => 'Attachment deleted successfully.']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to delete attachment.']);
}
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid attachment ID.']);
}
}
function saveAttachment() {
$invoiceId = $this->request->getPost('invoice_id');
$fileNames = $this->request->getPost('file_name[]');
$files = $this->request->getFiles();
if ($files && isset($files['emp_file']) && is_array($files['emp_file'])) {
foreach ($files['emp_file'] as $key => $file) {
if ($file->isValid() && !$file->hasMoved()) {
// Generate a unique name for the file
$newFileName = $file->getRandomName();
// Move the file to the target directory
$file->move('./public/uploads/images/invoice_files/', $newFileName);
// Insert the file info into the database
$data = [
'invoice_id' => $invoiceId,
'file_name' => $newFileName,
'attachment_name' => $fileNames[$key]
];
$this->ipattachment_model->addNewAttachment($data);
}
}
} else {
return $this->response->setJSON(['success' => false, 'message' => 'No files uploaded or incorrect input name.']);
}
return $this->response->setJSON(['success' => true]);
}
}

View File

@ -225,4 +225,217 @@ class Ipinvoice_model extends Model
}
/**
* This function is used to get the Today Sales
* @return array $result : This is result of the query
*/
function todaySales()
{
$builder = $this->db->table('ip_invoices a')
->select('COUNT(DISTINCT(a.invoice_number)) as Invoices, IFNULL(SUM(b.item_quantity), 0) as Qty, IFNULL(SUM(c.item_subtotal), 0) as Sales')
->join('ip_invoice_items b', 'a.invoice_id = b.invoice_id', 'left')
->join('ip_invoice_item_amounts c', 'b.item_id = c.item_id', 'left')
->where('a.invoice_date_created', date('Y-m-d'))
->where('a.invoice_status_id', 2);
$query = $builder->get();
return $query->getRow(); // Assuming you want a single row of aggregated results
}
/**
* This function is used to get the This Month Sales
* @return array $result : This is result of the query
*/
function thisMonthSales()
{
// Calculate the start date for the current fiscal month
$fiscalMonthStart = date('Y') . '-04-01'; // Assuming the fiscal year starts in April
$currentYear = date('Y');
$currentMonth = date('m');
// If the current month is before April, subtract one year for the fiscal year start
if ($currentMonth < 4) {
$fiscalMonthStart = ($currentYear - 1) . '-' . $currentMonth . '-01';
} else {
$fiscalMonthStart = $currentYear . '-' . $currentMonth . '-01';
}
// Build the query
$builder = $this->db->table('ip_invoices a')
->select('COUNT(DISTINCT(a.invoice_number)) as Invoices,
IFNULL(SUM(b.item_quantity), 0) as Qty,
IFNULL(SUM(c.item_subtotal), 0) as Sales')
->join('ip_invoice_items b', 'a.invoice_id = b.invoice_id', 'left')
->join('ip_invoice_item_amounts c', 'b.item_id = c.item_id', 'left')
->where('a.invoice_date_created >=', $fiscalMonthStart)
->where('a.invoice_status_id', 2);
// Execute the query
$query = $builder->get();
// Log the SQL query for debugging
log_message('debug', $builder->getCompiledSelect());
// Return the aggregated results
return $query->getRow();
}
/**
* This function is used to get the This Year Sales
* @return array $result : This is result of the query
*/
function thisYearSales()
{
// Calculate the start date for the current fiscal year
$fiscalYearStart = date('Y') . '-04-01';
$currentYear = date('Y');
$currentMonth = date('m');
// If the current month is before April, subtract one year for the fiscal year start
if ($currentMonth < 4) {
$fiscalYearStart = ($currentYear - 1) . '-04-01';
} else {
$fiscalYearStart = $currentYear . '-04-01';
}
// Build the query
$builder = $this->db->table('ip_invoices a')
->select('COUNT(DISTINCT(a.invoice_number)) as Invoices,
IFNULL(SUM(b.item_quantity), 0) as Qty,
IFNULL(SUM(c.item_subtotal), 0) as Sales')
->join('ip_invoice_items b', 'a.invoice_id = b.invoice_id', 'left')
->join('ip_invoice_item_amounts c', 'b.item_id = c.item_id', 'left')
->where('a.invoice_date_created >=', $fiscalYearStart)
->where('a.invoice_status_id', 2);
// Execute the query
$query = $builder->get();
// Log the SQL query for debugging
log_message('debug', $builder->getCompiledSelect());
// Return the aggregated results
return $query->getRow();
}
/**
* This function is used to get the This Year Sales Trend
* @return array $result : This is result of the query
*/
function thisYearSalesTrend()
{
$currentYear = date('Y');
$currentMonth = date('m');
// Calculate the start date for the current and last fiscal years
if ($currentMonth < 4) {
$fiscalYearStartCurrent = ($currentYear - 1) . '-04-01';
$fiscalYearStartLast = ($currentYear - 2) . '-04-01';
$fiscalYearEndLast = ($currentYear - 1) . '-03-31';
} else {
$fiscalYearStartCurrent = $currentYear . '-04-01';
$fiscalYearStartLast = ($currentYear - 1) . '-04-01';
$fiscalYearEndLast = $currentYear . '-03-31';
}
// Build query for the current fiscal year
$builderCurrent = $this->db->table('ip_invoices a')
->select("DATE_FORMAT(a.invoice_date_created, '%b') as Month,
IFNULL(SUM(c.item_total), 0) as Sales")
->join('ip_invoice_items b', 'a.invoice_id = b.invoice_id', 'left')
->join('ip_invoice_item_amounts c', 'b.item_id = c.item_id', 'left')
->where('a.invoice_date_created >=', $fiscalYearStartCurrent)
->where('a.invoice_status_id', 2)
->groupBy("DATE_FORMAT(a.invoice_date_created, '%b')")
->orderBy('MONTH(a.invoice_date_created)');
// Build query for the last fiscal year
$builderLast = $this->db->table('ip_invoices a')
->select("DATE_FORMAT(a.invoice_date_created, '%b') as Month,
IFNULL(SUM(c.item_total), 0) as Sales")
->join('ip_invoice_items b', 'a.invoice_id = b.invoice_id', 'left')
->join('ip_invoice_item_amounts c', 'b.item_id = c.item_id', 'left')
->where('a.invoice_date_created >=', $fiscalYearStartLast)
->where('a.invoice_date_created <', $fiscalYearStartCurrent)
->where('a.invoice_status_id', 2)
->groupBy("DATE_FORMAT(a.invoice_date_created, '%b')")
->orderBy('MONTH(a.invoice_date_created)');
// Execute the queries
$currentYearData = $builderCurrent->get()->getResultArray();
$lastYearData = $builderLast->get()->getResultArray();
// Combine the data
$salesComparison = [];
foreach ($lastYearData as $lastYearRow) {
$month = $lastYearRow['Month'];
$salesComparison[$month]['Last Year'] = $lastYearRow['Sales'];
}
foreach ($currentYearData as $currentYearRow) {
$month = $currentYearRow['Month'];
$salesComparison[$month]['Current Year'] = $currentYearRow['Sales'];
}
// Log the SQL queries for debugging
log_message('debug', $builderCurrent->getCompiledSelect());
log_message('debug', $builderLast->getCompiledSelect());
// Return the comparison results
return $salesComparison;
}
/**
* This function is used to get the Today invoices
* @return array $result : This is result of the query
*/
function getTodayInvoices()
{
// Define the custom field ID for the vehicle number
$vehicleCustomFieldId = 8; // Adjust this ID if necessary
// Build the query to get today's invoices with detailed information
$builder = $this->db->table('ip_invoices ii')
->select("
ii.invoice_number AS 'Invoice No.',
DATE_FORMAT(ii.invoice_date_created, '%d-%m-%Y') AS 'Invoice Date',
TIME_FORMAT(ii.invoice_time_created, '%l:%i %p') AS 'Invoice Time',
icf.invoice_custom_fieldvalue AS 'Vehicle No.',
IF(iit.item_description = '', iit.item_name, iit.item_description) AS 'Product',
ic.client_name AS 'Client',
SUM(iit.item_quantity) AS 'Quantity',
SUM(iit.item_price) AS 'Rate',
SUM(iia.item_subtotal) AS 'Value',
SUM(iia.item_cgst_amt) AS 'CGST',
SUM(iia.item_sgst_amt) AS 'SGST',
SUM(iia.item_igst_amt) AS 'IGST',
SUM(IFNULL(tcs.tax_rate_percent * iia.item_total / 100, 0)) AS 'TCS',
SUM(IFNULL(tcs.tax_rate_percent * iia.item_total / 100, 0)) + SUM(iia.item_total) AS 'Total'
")
->join('ip_invoice_custom icf', 'icf.invoice_id = ii.invoice_id', 'left')
->join('ip_invoice_tax_rates tc', 'tc.invoice_id = ii.invoice_id', 'left')
->join('ip_invoice_items iit', 'iit.invoice_id = ii.invoice_id', 'left')
->join('ip_invoice_item_amounts iia', 'iia.item_id = iit.item_id', 'left')
->join('ip_clients ic', 'ic.client_id = ii.client_id', 'left')
->join('ip_tax_rates sgst', 'sgst.tax_rate_id = iit.sgst_item_tax_rate_id', 'left')
->join('ip_tax_rates cgst', 'cgst.tax_rate_id = iit.cgst_item_tax_rate_id', 'left')
->join('ip_tax_rates igst', 'igst.tax_rate_id = iit.igst_item_tax_rate_id', 'left')
->join('ip_tax_rates tcs', 'tcs.tax_rate_id = tc.tax_rate_id', 'left')
->where('icf.invoice_custom_fieldid', $vehicleCustomFieldId)
->where('ii.invoice_status_id', 2)
->where('ii.invoice_date_created', date('Y-m-d'))
->groupBy('ii.invoice_number, iit.item_description')
->orderBy('ii.invoice_number');
// Execute the query
$query = $builder->get();
// Log the SQL query for debugging
log_message('debug', $builder->getCompiledSelect());
// Return the detailed invoice data
return $query->getResultArray();
}
}

View File

@ -401,7 +401,7 @@ function validatePF() {
if (PF != '') {
if (regpf.test(PF) == false) {
alert('Please Enter Valid PF Number');
alert('Please Enter Valid UAN');
return (false);
} else {
return true;
@ -1001,7 +1001,7 @@ legend {
<div class="col-md-3">
<div class="form-group"><span>PF Number</span>
<div class="form-group"><span>UAN</span>
<input type="text" id="pfno" name="pfno" maxlength="22"
onchange="validatePF();" class="form-control"
style="text-transform:uppercase"

View File

@ -500,7 +500,7 @@ function validatePF() {
if (PF != '') {
if (regpf.test(PF) == false) {
alert('Please Enter Valid PF Number');
alert('Please Enter Valid UAN');
return (false);
} else {
return true;
@ -1302,7 +1302,7 @@ legend {
<div class="col-md-3">
<div class="form-group"><span>PF Number</span>
<div class="form-group"><span>UAN</span>
<input type="text" id="pfno" name="pfno" maxlength="22"
value="<?php echo $pfno; ?>"
onchange="validatePF();" class="form-control"

View File

@ -341,7 +341,8 @@
</a>
</li><!--Dashboard active treeview -->
<li class="treeview">
<a id="salesDashboardLin" href="https://vu.venbait.in/d/d9eb820b-60aa-4fe6-9de0-531924185b77/sales-at-a-glance?orgId=2&from=1715215911333&to=1715237511333" target="_blank">
<!-- <a id="salesDashboardLin" href="https://vu.venbait.in/d/d9eb820b-60aa-4fe6-9de0-531924185b77/sales-at-a-glance?orgId=2&from=1715215911333&to=1715237511333" target="_blank"> -->
<a id="salesDashboardLin" href="<?php echo base_url(); ?>sales_dashboard">
<i class="fa fa-dashboard"></i><span>Sales Dashboard</span>
</a>
</li><!--Dashboard active treeview -->

View File

@ -0,0 +1,23 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1><center>Sales Dashboard</center></h1>
</section>
<section class="content">
<div class="row">
<div class="col-xs-12 text-right">
<div class="form-group"></div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header"></div><!-- /.box-header -->
<div class="box-body"></div><!-- /.box-body -->
</div><!-- /.box -->
</div>
</div>
</section>
</div>