ipinvoice_model = new Ipinvoice_model(); $this->ipattachment_model = new Ipattachment_model(); $this->session = session(); $this->isLoggedIn(); } function formatIndianCurrency($amount) { // Convert the amount to a string with 2 decimal places $formattedAmount = number_format((float)$amount, 2, '.', ''); // Split the integer and decimal parts $parts = explode('.', $formattedAmount); $integerPart = $parts[0]; $decimalPart = isset($parts[1]) ? $parts[1] : '00'; // Format the integer part for Indian numbering system if (strlen($integerPart) > 3) { $lastThreeDigits = substr($integerPart, -3); // Get the last 3 digits (thousands) $remainingDigits = substr($integerPart, 0, -3); // Get the remaining digits // Add commas to the remaining digits in groups of 2 $remainingDigits = preg_replace('/\B(?=(\d{2})+(?!\d))/', ',', $remainingDigits); // Combine the formatted parts $integerPart = $remainingDigits . ',' . $lastThreeDigits; } // Combine the integer and decimal parts return $integerPart . '.' . $decimalPart; } 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, 2, '.', ','); $data['this_month_sales'] = $this->ipinvoice_model->thisMonthSales(); $data['this_month_sales']->Sales = $this->formatIndianCurrency($data['this_month_sales']->Sales); $data['this_year_sales'] = $this->ipinvoice_model->thisYearSales(); $data['this_year_sales']->Sales = $this->formatIndianCurrency($data['this_year_sales']->Sales, 2, '.', ','); $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 "
";
// 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')
{
$currentYear = date('Y');
$currentMonth = date('m');
$startOfMonth = "$currentYear-$currentMonth-01";
$endOfMonth = date('Y-m-t');
$fromDate = $startOfMonth;
$toDate = $endOfMonth;
} 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 "";
// 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);
}
// 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 "";
// 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]);
}
}