diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 1148444..2f6048d 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -47,6 +47,7 @@ $routes->post("add_sales", "Sales::add_sales"); $routes->get("new_sale/(:any)", "Sales::new_sale/$1"); $routes->get("delete_sale/(:any)", "Sales::delete_sale/$1"); $routes->post("delete_sales_product", "Sales::delete_sales_product"); +$routes->get("download_invoice/(:any)", "Sales::download_invoice/$1"); //end salle order// //Client// diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php index 270e23a..3471c6c 100644 --- a/app/Controllers/Sales.php +++ b/app/Controllers/Sales.php @@ -6,9 +6,16 @@ use App\Models\SalesOrderModel; use App\Models\SalesOrderProductModel; use App\Models\ProductModel; use App\Models\ClientModel; - +use Mpdf\Mpdf; class Sales extends BaseController { + public $session; + public function __construct() + { + + $this->session = session(); + +} public function sales_order_index() { @@ -79,7 +86,8 @@ $data['product']=$product; 'discount'=> $this->request->getPost('discount'), 'total'=> $this->request->getPost('grand_total'), 'status'=> $this->request->getPost('status'), - 'isactive' => 1 // Assuming this is a default value or handled separately + 'isactive' => 1, // Assuming this is a default value or handled separately + 'branch_id'=> $this->session->get('logged_user_branch_id'), ]; // print_r($data);die; // Insert or update sales order @@ -176,5 +184,74 @@ public function delete_sales_product() return redirect()->to('sales_order_index'); } + public function download_invoice($sales_order_id) + { + // echo "hello"; die; + // Fetch the invoice data based on $invoice_id + $model = new SalesOrderModel(); + $data = $model->getSalesPdf($sales_order_id); + + $items = $model->getSalesOrderProductsForPdf($sales_order_id); + + + + // print_r($items);die(); + + // print_r($invoiceItems);die(); + + // Create an mPDF object + $mpdf = new Mpdf([ + 'mode' => '', + 'format' => 'A5', + 'default_font_size' => 0, + 'default_font' => '', + 'margin_left' => 2, + 'margin_right' => 2, + 'margin_top' => 6, + 'margin_bottom' => 6, + 'margin_header' => 6, + 'margin_footer' =>-30, + 'orientation' => 'P', + ]); + + + + $mpdf->autoLangToFont = true; $mpdf->autoScriptToLang = true; + // Set PDF properties + $mpdf->SetTitle('Invoice'); + $mpdf->SetAuthor($data[0]->branch_name); + $mpdf->SetCreator(''); + + $htmlFooter='
+
'; + $mpdf->setHTMLFooter($htmlFooter); + + // Generate the PDF content (HTML) + if ($data[0]->status === 'Draft') { + // Set the watermark text and options + $mpdf->SetWatermarkText('Draft'); + $mpdf->showWatermarkText = true; + } + if ($data[0]->status === 'Cancelled') { + // Set the watermark text and options + $mpdf->SetWatermarkText('Cancelled'); + $mpdf->showWatermarkText = true; + } + if ($data[0]->status === 'Void') { + // Set the watermark text and options + $mpdf->SetWatermarkText('Void'); + $mpdf->showWatermarkText = true; + } + + + // Generate the PDF content (HTML) with data + $html = view('invoice_pdf_template', ['sales' => $data,'items'=>$items]); +// echo $html;die; + // Load HTML into the mPDF instance + $mpdf->WriteHTML($html); + + // Output the PDF to the browser for download + $mpdf->Output('invoice_' . date('Y-m-d H-i-s') . '.pdf', 'D'); + } } \ No newline at end of file diff --git a/app/Models/SalesOrderModel.php b/app/Models/SalesOrderModel.php index caa4a0c..59403a1 100644 --- a/app/Models/SalesOrderModel.php +++ b/app/Models/SalesOrderModel.php @@ -5,7 +5,19 @@ class SalesOrderModel extends Model { protected $table = 'sales_order'; protected $primaryKey = 'sales_order_id'; - protected $allowedFields = ['sales_order_id','client_id','order_number','status','mobile_no','billing_address','billing_city','billing_state','billing_country','billing_postal_code','total','tax','subtotal','discount','terms_condition','isactive']; + protected $allowedFields = ['sales_order_id','client_id','order_number','branch_id','status','mobile_no','billing_address','billing_city','billing_state','billing_country','billing_postal_code','total','tax','subtotal','discount','terms_condition','isactive']; + + public function getSalesPdf($sales_order_id){ + return $this->db->table('sales_order') + ->where('sales_order_id', $sales_order_id) + ->join('client as C','C.client_id= sales_order.client_id','left') + ->join('branches as B','B.branch_id = sales_order.branch_id','left') + ->select('sales_order.*,C.client_name,C.mobile_no,C.address,C.city,C.state') + ->select('B.branch_name ,B.address as company_address,B.city as company_city,B.state as company_state,B.postal_code as company_postal_code,B.mobile_no as company_mobile_no') + + ->get() + ->getResult(); + } public function getSalesOrdersWithProducts() { @@ -25,7 +37,19 @@ class SalesOrderModel extends Model // Get the results return $this->findAll(); } - + public function getSalesOrderProductsForPdf($sales_order_id) +{ + // Select required fields from the sales_order_product table + $result = $this->db->table('sales_order_product') + ->where('sales_order_product.sales_order_id', $sales_order_id) + ->join('products', 'products.product_id = sales_order_product.product_id') + ->select('sales_order_product.*, products.product_name') + ->get() + ->getResult(); + + return $result; +} + } ?> \ No newline at end of file diff --git a/app/Views/invoice_pdf_template.php b/app/Views/invoice_pdf_template.php new file mode 100644 index 0000000..c74606a --- /dev/null +++ b/app/Views/invoice_pdf_template.php @@ -0,0 +1,292 @@ + + + + + + + + + + + + +
+ + + + + + + + + + +
+ + THE MECHANIC +
+

company_address ?>,
+ company_city ?>, + company_state ?> + company_postal_code ? " - ".$value->company_postal_code:"" ?>.

+ +

company_mobile_no ?>

+
+
+ + + + + + + + + + + +
order_number) ? 'Order # ' . $value->order_number : 'Invoice # ' . $value->invoice_number; ?>
Invoice Date : created_at)) ?>
+
+ + + + + + + + + + +
Billing Address
+ + client_name ?>
+ address." ," ?>
+ city ?> state?> + billing_postal_code ." - "?> + billing_country ? $value->billing_country . "." : $value->billing_country ?>
+ + mobile_no." ." ?> + + +
+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + discount!=0): ?> + + + + + + + + + +status === 'Approved'): ?> + + + + + +
Subtotal :   ₹subtotal, 2, '.', ',') ?>
Tax :   ₹tax, 2, '.', ',') ?>
Discount :   ₹discount, 2, '.', ',') ?>
Total :   ₹total, 2, '.', ',') ?>
Paid :   ₹total_amount, 2, '.', ',') ?>
+ +
+ +
+ + +
+

Thank you!

+ + +
+ + + + + + + + + + + + + diff --git a/app/Views/sales_list.php b/app/Views/sales_list.php index 47aba92..e610afc 100644 --- a/app/Views/sales_list.php +++ b/app/Views/sales_list.php @@ -44,7 +44,10 @@ - @@ -57,6 +60,9 @@ " class="edit-button"> + + " class="edit-button"> + " class="delete-button">