277 lines
11 KiB
PHP
Executable File
277 lines
11 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
use App\Models\Ipinvoice_model;
|
|
use App\Models\Ipattachment_model;
|
|
require_once 'vendor/autoload.php';
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
class Sales extends BaseController
|
|
{
|
|
protected $ipinvoice_model;
|
|
protected $ipattachment_model;
|
|
protected $session;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->ipinvoice_model = new Ipinvoice_model();
|
|
$this->ipattachment_model = new Ipattachment_model();
|
|
$this->session = session();
|
|
$this->isLoggedIn();
|
|
}
|
|
|
|
function formatIndianCurrency($num,$type) {
|
|
$num = explode('.', $num); // Split the number into integer and decimal parts
|
|
$integerPart = $num[0]; // Get the integer part
|
|
if($type == 'qty'){ return $integerPart; }
|
|
// Apply the Indian number format to the integer part
|
|
$lastThreeDigits = substr($integerPart, -3);
|
|
$remainingDigits = substr($integerPart, 0, -3);
|
|
|
|
if ($remainingDigits != '') {
|
|
$formattedNumber = preg_replace('/\B(?=(\d{2})+(?!\d))/', ',', $remainingDigits) . ',' . $lastThreeDigits;
|
|
} else {
|
|
$formattedNumber = $lastThreeDigits;
|
|
}
|
|
|
|
return $formattedNumber;
|
|
}
|
|
|
|
public function sales_dashboard()
|
|
{
|
|
$this->global['pageTitle'] = 'Sales Dashboard';
|
|
$data = [];
|
|
|
|
$data['today_sales'] = $this->ipinvoice_model->todaySales();
|
|
$data['today_sales']->Sales = $this->formatIndianCurrency($data['today_sales']->Sales,'money');
|
|
$data['today_sales']->Qty = $this->formatIndianCurrency($data['today_sales']->Qty,'qty');
|
|
|
|
$data['this_month_sales'] = $this->ipinvoice_model->thisMonthSales();
|
|
$data['this_month_sales']->Sales = $this->formatIndianCurrency($data['this_month_sales']->Sales,'money');
|
|
$data['this_month_sales']->Qty = $this->formatIndianCurrency($data['this_month_sales']->Qty,'qty');
|
|
|
|
$data['this_year_sales'] = $this->ipinvoice_model->thisYearSales();
|
|
$data['this_year_sales']->Sales = $this->formatIndianCurrency($data['this_year_sales']->Sales,'money');
|
|
$data['this_year_sales']->Qty = $this->formatIndianCurrency($data['this_year_sales']->Qty,'qty');
|
|
|
|
$data['this_year_sales_trend'] = $this->ipinvoice_model->thisYearSalesTrend();
|
|
$data['get_today_invoices'] = $this->ipinvoice_model->getTodayInvoices();
|
|
|
|
// Define months from April to March
|
|
$months = ['Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar'];
|
|
|
|
// Initialize arrays for current and last year sales
|
|
$currentYearSales = array_fill(0, 12, 0);
|
|
$lastYearSales = array_fill(0, 12, 0);
|
|
|
|
// Populate arrays with data
|
|
foreach ($months as $index => $month) {
|
|
if (isset($data['this_year_sales_trend'][$month]['Current Year'])) {
|
|
$currentYearSales[$index] = $data['this_year_sales_trend'][$month]['Current Year'];
|
|
}
|
|
if (isset($data['this_year_sales_trend'][$month]['Last Year'])) {
|
|
$lastYearSales[$index] = $data['this_year_sales_trend'][$month]['Last Year'];
|
|
}
|
|
}
|
|
|
|
// Assign to data array
|
|
$data['currentYearSales'] = $currentYearSales;
|
|
$data['lastYearSales'] = $lastYearSales;
|
|
|
|
// echo "<pre>";
|
|
// print_r($data);
|
|
// die;
|
|
|
|
$this->loadViews("sales_dashboard", $this->global, $data, NULL);
|
|
}
|
|
|
|
// Sales Invoice List
|
|
function sales_invoice(){
|
|
$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'] = 'Sales Invoice';
|
|
$data['sales_invoice'] = $this->ipinvoice_model->saleInvoiceListing($fromDate , $toDate);
|
|
// echo "<pre>";
|
|
// print_r($data);die;
|
|
$data['fromDate'] = date('d-m-Y', strtotime($fromDate));
|
|
$data['toDate'] = date('d-m-Y', strtotime($toDate));
|
|
|
|
|
|
$this->loadViews("sales_invoice", $this->global, $data, NULL);
|
|
}
|
|
|
|
public function download_sales_invoice()
|
|
{
|
|
// Fetch data from the model
|
|
$fromDate = $this->request->getPost('fromDate');
|
|
$toDate = $this->request->getPost('toDate');
|
|
$fromDate = date('Y-m-d', strtotime($fromDate));
|
|
$toDate = date('Y-m-d', strtotime($toDate));
|
|
|
|
$sales_invoice = $this->ipinvoice_model->saleInvoiceListing($fromDate, $toDate);
|
|
|
|
// Start building the table
|
|
$html = '<table id="datatable-buttosns" class="table dt-responsive nowrap w-100">
|
|
<thead>
|
|
<tr>
|
|
<th>Invoice Date</th>
|
|
<th>Invoice No</th>
|
|
<th>Client Name</th>
|
|
<th>Product</th>
|
|
<th>Quantity</th>
|
|
<th>Rec Qty</th>
|
|
<th>Rate</th>
|
|
<th>Sub Total</th>
|
|
<th>GST</th>
|
|
<th>Total</th>
|
|
<th>Status</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>';
|
|
|
|
foreach ($sales_invoice as $invoice) {
|
|
$subtotal = $invoice->item_price * $invoice->item_quantity;
|
|
$cgst_amount = ($subtotal * $invoice->cgst) / 100;
|
|
$sgst_amount = ($subtotal * $invoice->sgst) / 100;
|
|
$total = $subtotal + $cgst_amount + $sgst_amount;
|
|
|
|
$html .= '<tr>
|
|
<td>' . date('d-m-Y', strtotime($invoice->invoice_date_created)) . '</td>
|
|
<td>' . $invoice->invoice_number . '</td>
|
|
<td>' . $invoice->client_name . '</td>
|
|
<td>' . $invoice->item_name . '</td>
|
|
<td>' . number_format($invoice->item_quantity, 2, '.', ',') . '</td>
|
|
<td>' . (!empty($invoice->received_qty) ? $invoice->received_qty : '') . '</td>
|
|
<td>' . number_format($invoice->item_price, 2, '.', ',') . '</td>
|
|
<td>' . number_format($subtotal, 2, '.', ',') . '</td>
|
|
<td>' . number_format($cgst_amount + $sgst_amount, 2, '.', ',') . '</td>
|
|
<td>' . number_format($total, 2, '.', ',') . '</td>
|
|
<td>' . $invoice->status . '</td>
|
|
</tr>';
|
|
}
|
|
|
|
$html .= '</tbody></table>';
|
|
|
|
return $this->response->setJSON(['html' => $html]);
|
|
}
|
|
|
|
|
|
// 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_number'));
|
|
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]);
|
|
}
|
|
|
|
|
|
|
|
function updateInvRecQty()
|
|
{
|
|
$invoice_id = $this->request->getPost('invoice_id');
|
|
$received_qty = $this->request->getPost('received_qty');
|
|
|
|
if ($invoice_id && $received_qty) {
|
|
|
|
$result = $this->ipinvoice_model->updateInvRecQty($invoice_id , $received_qty);
|
|
|
|
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.']);
|
|
}
|
|
}
|
|
} |