diff --git a/app/Config/Routes.php b/app/Config/Routes.php index e6cf6a5a..03225b3d 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -412,6 +412,7 @@ $routes->get('deleteAttachment', 'Sales::deleteAttachment'); $routes->post('saveAttachment', 'Sales::saveAttachment'); $routes->get('sales_invoice', 'Sales::sales_invoice'); $routes->post('sales_invoice', 'Sales::sales_invoice'); +$routes->post('download_sales_invoice', 'Sales::download_sales_invoice'); $routes->post('updateInvRecQty', 'Sales::updateInvRecQty'); // transporter Routes diff --git a/app/Controllers/Expense.php b/app/Controllers/Expense.php index a9be54aa..3d545ab0 100755 --- a/app/Controllers/Expense.php +++ b/app/Controllers/Expense.php @@ -48,8 +48,17 @@ class Expense extends BaseController $this->loadViews("expense_list", $this->global, $data, NULL); } + private function generateSerialNumber($financialYear, $lastSerial) + { + // If no last serial number exists, start with 00001 + $lastNumber = $lastSerial ? (int)explode('/', $lastSerial)[1] : 0; + $newNumber = str_pad($lastNumber + 1, 5, '0', STR_PAD_LEFT); + return "$financialYear/$newNumber"; + } + public function addExpense() { + try{ $file = $this->request->getFile('transporterfile'); $fileName = ''; if ($file->isValid() && !$file->hasMoved()) { @@ -59,6 +68,28 @@ class Expense extends BaseController $status = $this->request->getPost('status'); $paymentMethod = $status === 'Paid' ? $this->request->getPost('payment_method') : 0; + + // Fetch the current month + $currentMonth = date('n'); // 'n' gives the month without leading zeros (1-12) + + // Calculate the current financial year + if ($currentMonth >= 4) { // If it's April or later + $startYear = date('Y'); // Current year + $endYear = $startYear + 1; // Next year + } else { // If it's January, February, or March + $endYear = date('Y'); // Current year + $startYear = $endYear - 1; // Previous year + } + + // Format the financial year as "yy-yy" + $financialYear = substr($startYear, -2) . '-' . substr($endYear, -2); + + // Fetch the last serial number for the financial year + $lastSerial = $this->expense_model->getLastSerialNumber($financialYear); + + // Generate the next serial number + $newSerialNumber = $this->generateSerialNumber($financialYear, $lastSerial); + $data = [ 'transporter_file' => $fileName, @@ -70,14 +101,21 @@ class Expense extends BaseController 'sgst' => $this->request->getPost('sgst'), 'igst' => $this->request->getPost('igst'), 'total' => $this->request->getPost('total'), + 'bill_no' => $this->request->getPost('bill_no'), + 'bill_date' => $this->request->getPost('bill_date'), + 'serial_number' => $newSerialNumber, ]; - + $insert_id = $this->expense_model->insertExpense($data); if ($insert_id) { return json_encode('true'); } else { return json_encode('false'); } + } catch (\Exception $e) { + // Handle the exception + echo "Error: " . $e->getMessage(); + } } public function getExpenseDetails() @@ -117,6 +155,8 @@ class Expense extends BaseController 'sgst' => $this->request->getPost('sgst'), 'igst' => $this->request->getPost('igst'), 'total' => $this->request->getPost('total'), + 'bill_no' => $this->request->getPost('bill_no'), + 'bill_date' => $this->request->getPost('bill_date'), ]; if(!empty($fileName)){ $data['transporter_file'] = $fileName; diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php index c530c886..48b2656b 100755 --- a/app/Controllers/Sales.php +++ b/app/Controllers/Sales.php @@ -8,6 +8,8 @@ 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 { @@ -40,6 +42,7 @@ class Sales extends BaseController return $formattedNumber; } + public function sales_dashboard() { $this->global['pageTitle'] = 'Sales Dashboard'; @@ -97,12 +100,12 @@ class Sales extends BaseController { $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)); - } + } 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); @@ -114,6 +117,62 @@ class Sales extends BaseController $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 . ' | +