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 "
";
// 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 "";
// 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 = '| Invoice Date | Invoice No | Client Name | Product | Quantity | Rec Qty | Rate | Sub Total | GST | Total | Status |
|---|---|---|---|---|---|---|---|---|---|---|
| ' . date('d-m-Y', strtotime($invoice->invoice_date_created)) . ' | ' . $invoice->invoice_number . ' | ' . $invoice->client_name . ' | ' . $invoice->item_name . ' | ' . number_format($invoice->item_quantity, 2, '.', ',') . ' | ' . (!empty($invoice->received_qty) ? $invoice->received_qty : '') . ' | ' . number_format($invoice->item_price, 2, '.', ',') . ' | ' . number_format($subtotal, 2, '.', ',') . ' | ' . number_format($cgst_amount + $sgst_amount, 2, '.', ',') . ' | ' . number_format($total, 2, '.', ',') . ' | ' . $invoice->status . ' |
";
// 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.']);
}
}
}