443 lines
18 KiB
PHP
Executable File
443 lines
18 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class Ipinvoice_model extends Model
|
|
{
|
|
|
|
|
|
/**
|
|
* This function is used to get the user listing count
|
|
* @param string $searchText : This is optional search text
|
|
* @param number $page : This is pagination offset
|
|
* @param number $segment : This is pagination limit
|
|
* @return array $result : This is result
|
|
*/
|
|
function saleInvoiceListing($fromDate,$toDate)
|
|
{
|
|
$builder = $this->db->table('ip_invoices')
|
|
->select('ip_invoices.invoice_id, ip_invoices.user_id, ip_invoices.client_id,
|
|
ip_invoices.invoice_group_id, ip_invoices.invoice_status_id, ip_invoices.invoice_date_due, ip_invoices.invoice_date_created,
|
|
ip_invoices.invoice_number, ip_invoices.invoice_terms, ip_invoices.status, ip_invoices.e_invoice_number, ip_invoices.invoice_url, ip_clients.client_name,
|
|
ip_payment_methods.payment_method_name, ip_users.user_name,
|
|
ip_products.product_name, ip_invoice_items.item_price, ip_products.cgst, ip_products.sgst,
|
|
ip_invoice_items.item_quantity, ip_invoice_items.item_price, ip_invoice_items.item_name, ip_invoice_items.item_quantity,
|
|
ip_invoice_amounts.invoice_total, ip_invoice_amounts.invoice_item_subtotal, ip_invoice_amounts.invoice_item_tax_total')
|
|
->join('ip_payment_methods', 'ip_invoices.payment_method = ip_payment_methods.payment_method_id', 'left')
|
|
->join('ip_clients', 'ip_invoices.client_id = ip_clients.client_id', 'left')
|
|
->join('ip_users', 'ip_invoices.user_id = ip_users.user_id', 'left')
|
|
->join('ip_invoice_items', 'ip_invoices.invoice_id = ip_invoice_items.invoice_id', 'left')
|
|
->join('ip_products', 'ip_invoice_items.item_product_id = ip_products.product_id', 'left')
|
|
->join('ip_invoice_amounts', 'ip_invoices.invoice_id = ip_invoice_amounts.invoice_id', 'left');
|
|
$builder->where('ip_invoices.invoice_date_created >=', $fromDate );
|
|
$builder->where('ip_invoices.invoice_date_created <=', $toDate );
|
|
$builder->orderBy('ip_invoices.invoice_date_created', 'DESC');
|
|
|
|
$query = $builder->get();
|
|
$result = $query->getResult();
|
|
// $results = $query->getResultArray();
|
|
return $result;
|
|
// $invoices = [];
|
|
|
|
// foreach ($results as $row) {
|
|
// $invoice_id = $row['invoice_id'];
|
|
|
|
// // If the invoice_id doesn't exist in the $invoices array, initialize it
|
|
// if (!isset($invoices[$invoice_id])) {
|
|
// $invoices[$invoice_id] = [
|
|
// 'invoice_id' => $row['invoice_id'],
|
|
// 'user_id' => $row['user_id'],
|
|
// 'client_id' => $row['client_id'],
|
|
// 'invoice_group_id' => $row['invoice_group_id'],
|
|
// 'invoice_status_id' => $row['invoice_status_id'],
|
|
// 'invoice_date_due' => $row['invoice_date_due'],
|
|
// 'invoice_date_created' => $row['invoice_date_created'],
|
|
// 'invoice_number' => $row['invoice_number'],
|
|
// 'invoice_terms' => $row['invoice_terms'],
|
|
// 'status' => $row['status'],
|
|
// 'e_invoice_number' => $row['e_invoice_number'],
|
|
// 'invoice_url' => $row['invoice_url'],
|
|
// 'client_name' => $row['client_name'],
|
|
// 'payment_method_name' => $row['payment_method_name'],
|
|
// 'user_name' => $row['user_name'],
|
|
// 'invoice_total' => $row['invoice_total'],
|
|
// 'invoice_item_subtotal' => $row['invoice_item_subtotal'],
|
|
// 'invoice_item_tax_total' => $row['invoice_item_tax_total'],
|
|
// 'products' => [] // Initialize the products array
|
|
// ];
|
|
// }
|
|
|
|
// // Add the product to the products array for the current invoice
|
|
// $invoices[$invoice_id]['products'][] = [
|
|
// 'product_name' => $row['product_name'],
|
|
// 'item_quantity' => $row['item_quantity'],
|
|
// 'item_price' => $row['item_price'],
|
|
// ];
|
|
// }
|
|
|
|
// // Return the grouped invoices
|
|
// return $invoices;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* This function is used to add new user to system
|
|
* @return number $insert_id : This is last inserted id
|
|
*/
|
|
function addNewUser($userInfo)
|
|
{
|
|
|
|
$this->db->transStart();
|
|
$builder = $this->db->table('tbl_users');
|
|
$builder->insert($userInfo);
|
|
|
|
$insert_id = $this->db->affectedRows();
|
|
|
|
$this->db->transComplete();
|
|
|
|
return $insert_id;
|
|
}
|
|
|
|
/**
|
|
* This function used to get user information by id
|
|
* @param number $userId : This is user id
|
|
* @return array $result : This is user information
|
|
*/
|
|
function getInvoice($invoiceId)
|
|
{
|
|
$builder = $this->db->table('ip_invoices')
|
|
// ->select('')
|
|
->where('invoice_id', $invoiceId);
|
|
$query = $builder->get();
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
|
|
/**
|
|
* This function used to get user information by id
|
|
* @param number $userId : This is user id
|
|
* @return array $result : This is user information
|
|
*/
|
|
function getinvoiceAttachment($invoiceId)
|
|
{
|
|
$builder = $this->db->table('ip_invoice_attachment')
|
|
// ->select('')
|
|
->where('invoice_id', $invoiceId);
|
|
$query = $builder->get();
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
|
|
// Ipinvoice_model
|
|
public function deleteAttachment($invoice_attachment_id) {
|
|
// Get the attachment details to get the file path
|
|
$builder = $this->db->table('ip_invoice_attachment');
|
|
$builder->where('invoice_attachment_id', $invoice_attachment_id);
|
|
$query = $builder->get();
|
|
$attachment = $query->getRowArray(); // Get single row as an associative array
|
|
|
|
if ($attachment) {
|
|
// Construct the file path
|
|
$fileName = $attachment['attachment_name']; // Make sure this is the correct field name
|
|
$filePath = './public/uploads/images/invoice_files/' . $fileName;
|
|
|
|
// Debug: Log the file path
|
|
log_message('error', 'Attempting to delete file: ' . $filePath);
|
|
|
|
// Check if the path is actually a file
|
|
if (file_exists($filePath) && !is_dir($filePath)) {
|
|
unlink($filePath); // Delete the file
|
|
} else {
|
|
log_message('error', 'File does not exist or is a directory: ' . $filePath);
|
|
}
|
|
|
|
// Delete the record from the database
|
|
$builder->where('invoice_attachment_id', $invoice_attachment_id);
|
|
$builder->delete();
|
|
|
|
return $this->db->affectedRows() > 0;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* This function is used to update the user information
|
|
* @param array $userInfo : This is users updated information
|
|
* @param number $userId : This is user id
|
|
*/
|
|
function editUser($userInfo, $userId)
|
|
{
|
|
$this->db->table('tbl_users')
|
|
->where('EmpID', $userId)
|
|
->update($userInfo);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* This function is used to delete the user information
|
|
* @param number $userId : This is user id
|
|
* @return boolean $result : TRUE / FALSE
|
|
*/
|
|
function deleteUser($userId, $userInfo)
|
|
{
|
|
$this->db->table('tbl_users')
|
|
->where('userId', $userId)
|
|
->update($userInfo);
|
|
|
|
return $this->db->affectedRows();
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* This function is used to get the All employees
|
|
* @return array $result : This is result of the query
|
|
*/
|
|
function getAllEmployees()
|
|
{
|
|
/*->select('EmpID,FirstName,LastName');
|
|
$builder = $this->db->table('t_employee_details');
|
|
$query = $builder->get();
|
|
|
|
return $query->getResult(); */
|
|
$builder = $this->db->table('t_employee_details EMP')
|
|
->select('EMP.EmpID,EMP.FirstName,EMP.LastName,EMP.Designation,EMP.EmailId,EMP.ContactNumber,DEPT.DEPCode,DEPT.DepartmentName')
|
|
->join('t_departmentdetails DEPT', 'EMP.Departmentcode = DEPT.DEPCode')
|
|
->where('EMP.IsActive ', 1);
|
|
$query = $builder->get();
|
|
return $query->getResult();
|
|
}
|
|
|
|
|
|
/**
|
|
* 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'))
|
|
->whereIn('a.invoice_status_id', [2,5]);
|
|
|
|
$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)
|
|
->whereIn('a.invoice_status_id', [2,5]);
|
|
|
|
// 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)
|
|
->whereIn('a.invoice_status_id', [2,5]);
|
|
|
|
// 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();
|
|
}
|
|
|
|
} |