ria/app/Controllers/Sales.php
2024-09-11 08:54:25 +05:30

153 lines
5.4 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';
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();
}
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();
// 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(){
$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_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]);
}
}