From 203e7bd0421f726be956111f3c2c0e60bbe04ba5 Mon Sep 17 00:00:00 2001 From: heama Date: Wed, 27 Sep 2023 18:12:52 +0530 Subject: [PATCH] invoice,subscription --- app/Config/Routes.php | 5 ++ app/Controllers/Home.php | 1 + app/Controllers/Invoice.php | 66 +++++++++++++++++------- app/Controllers/Subscription.php | 56 +++++++++++++++++++-- app/Models/InvoiceModel.php | 7 ++- app/Models/SchemeModel.php | 34 +++++++++++++ app/Models/SubscriptionModel.php | 68 +++++++++++++++++++++++++ app/Views/address_pdf_template.php | 80 ++++++++++++++++++++++++++++++ app/Views/invoice_form.php | 31 +++++++++--- app/Views/invoice_list.php | 12 +++-- app/Views/print_addresses_form.php | 80 ++++++++++++++++++++++++++++++ app/Views/subscribers_list.php | 65 +++++++++++++++++++++++- 12 files changed, 468 insertions(+), 37 deletions(-) create mode 100644 app/Views/address_pdf_template.php create mode 100644 app/Views/print_addresses_form.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index ad9f009f..9266f649 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -86,6 +86,9 @@ $routes->get("delete_scheme/(:any)", "Scheme::delete_scheme/$1"); $routes->get("subscribers_list/", "Subscription::index"); $routes->get("new_subscription/", "Subscription::new_subscription/"); $routes->post("add_subscription/", "Subscription::add_subscription/"); +$routes->get('subscription/generate_pdf', 'Subscription::generate_pdf'); + + #adres priknt $routes->get('addressprint/', 'Address::index'); $routes->post('address/generatePdf', 'Address::generatePdf'); @@ -102,6 +105,8 @@ $routes->post("save_invoice/", "Invoice::save_invoice/"); $routes->get("delete_invoice/(:any)", "Invoice::delete_invoice/$1"); $routes->add('approve_invoice/(:num)', 'Invoice::approve_invoice/$1'); $routes->get('generate_invoice_pdf/(:num)', 'Invoice::generate_invoice_pdf/$1'); +$routes->get('print_address/(:num)', 'Invoice::print_address/$1'); + diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 0cbf639b..db71d573 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -23,6 +23,7 @@ class Home extends BaseController $data['customer'] = $model->customers($where); $data['customer']['label'] = "Customer"; $this->render_page('dashboard', $data); + } } diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index 8fcd81bb..394ebd20 100644 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -91,18 +91,7 @@ class Invoice extends BaseController helper('session'); $model = new InvoiceModel(); - if ($this->request->getPost('save_as_draft')) { - $status = 'Draft'; - } elseif ($this->request->getPost('save_as_approved')) { - $status = 'Approved'; - } else { - // Handle other cases or set a default status - $status = 'Draft'; // Default to Draft if no button clicked - } - - // ... Other data retrieval and processing ... - - $data['status'] = $status; + // print_r($this->request->getPost()); // echo "

"; // Array ( [customer_name] => 1 [shipping_address] => 2 [event_next_id] => 134 [event_id] => 1 [invoice_number] => NBF2023-INV-00133 [order_number] => 12 [invoice_date] => 2023-09-07 [due_date] => 2023-09-07 [item_details] => Array ( [0] => 1 ) [quantity] => Array ( [0] => 2 [1] => 2 ) [rate] => Array ( [0] => 3 [1] => 3 ) [tax] => [amount] => Array ( [0] => 5 [1] => 5 ) [itemDetails] => Array ( [0] => 1 ) [subtotal] => [discount] => 9 [total] => [terms] => i don't like... ) @@ -126,7 +115,7 @@ class Invoice extends BaseController 'payment_status'=> 'Pending',//"Paid," "Pending," "Failed," "Refunded," "Canceled," "Authorized," and "Completed." 'event_id'=> (int)$this->request->getPost('event_id'), 'business_id'=> (int)get_business_id(), - 'status' => $status, + 'status' => $this->request->getPost('status'), 'isactive'=> 1]; $invoice_id = $this->request->getPost('invoice_id'); @@ -252,14 +241,15 @@ class Invoice extends BaseController } public function approve_invoice($id) { - // Validate $invoice_id (e.g., check if it's a valid invoice ID) + // Validate $id (e.g., check if it's a valid invoice ID) + // Update the status in the database to "Approved" $model = new InvoiceModel(); $data = ['status' => 'Approved']; - + $model->update($id, $data); - // Return a response (e.g., success message) - return $this->response->setJSON(['message' => 'Invoice approved successfully']); + // Redirect back to the invoice list + return redirect()->route('invoice_list'); } @@ -282,16 +272,54 @@ public function generate_invoice_pdf($id) $mpdf->SetCreator(''); // Generate the PDF content (HTML) - $html = view('invoice_pdf_template', ['data' => $data,'invoiceItems' => $invoiceItems]); + if ($data[0]->status === 'Draft') { + // Set the watermark text and options + $mpdf->SetWatermarkText('Draft'); + $mpdf->showWatermarkText = true; + } + + // Generate the PDF content (HTML) with data + $html = view('invoice_pdf_template', ['data' => $data, 'invoiceItems' => $invoiceItems]); // Load HTML into the mPDF instance $mpdf->WriteHTML($html); // Output the PDF to the browser for download $mpdf->Output('invoice_' . $id . '.pdf', 'D'); -} + +} +public function print_address($id) +{ + // Fetch the invoice data based on $id + $model = new InvoiceModel(); + $invoiceData = $model->getInvoiceData($id); + // print_r($invoiceData);die(); + $mpdf = new \Mpdf\Mpdf(); + $mpdf->SetTitle('Customer Address'); + $mpdf->SetAuthor('Your Company Name'); + $mpdf->SetCreator(''); + + // Generate the PDF content (HTML) with customer and address data + $html = view('address_pdf_template', ['invoiceData' => $invoiceData]); + + + // Load the mPDF library + + + // Set PDF properties + + + // Load HTML content into mPDF + $mpdf->WriteHTML($html); + + // Output the PDF for download + $pdfFileName = 'customer_address_' . $id . '.pdf'; + $mpdf->Output($pdfFileName, 'D'); + +} + } // public function generate_invoice_pdf($id) // { diff --git a/app/Controllers/Subscription.php b/app/Controllers/Subscription.php index 74dcec30..4fec4727 100644 --- a/app/Controllers/Subscription.php +++ b/app/Controllers/Subscription.php @@ -6,7 +6,7 @@ namespace App\Controllers; use App\Models\CustomerModel; use App\Models\SubscriptionModel; use App\Models\SchemeModel; - +use Mpdf\Mpdf; class Subscription extends BaseController { public function index() @@ -73,7 +73,55 @@ public function add_subscription() { return redirect()->to('subscribers_list')->with('data', $data); } - - -} +// Subscription.php (Controller) + +public function generate_pdf() +{ + $schemeName = $this->request->getGet('scheme_name'); + + // Call the model method to get customer addresses + $subscriptionModel = new SubscriptionModel(); + $addresses = $subscriptionModel->getCustomerAddressesBySchemeName($schemeName); + $billingInfo = $subscriptionModel->getCustomerAddressesBySchemeName($schemeName, 1); + $shippingInfo = $subscriptionModel->getCustomerAddressesBySchemeName($schemeName, 2); + $customerName = $subscriptionModel->getCustomerNameBySchemeName($schemeName); + + // Create an HTML content string using the view file + if ($customerName !== 'Customer not found') { + // Create an HTML content string using the view file + $html = view('print_addresses_form', ['customerName' => $customerName, 'billingInfo' => $billingInfo, 'shippingInfo' => $shippingInfo]); + + // Generate a PDF from the HTML content + $pdf = new Mpdf(); + + $pdf->WriteHTML($html); + + // Output the PDF to the browser in a new tab + $pdf->Output('CustomerAddress.pdf', 'D'); + } else { + // Handle the case where the customer is not found + echo 'Customer not found'; + } +} +} +// public function generate_pdf() +// { +// $schemeName = $this->request->getGet('scheme_name'); + +// // Call the model method to get the customer's name and address (billing or shipping) +// $subscriptionModel = new SubscriptionModel(); + +// // For billing address +// $billingInfo = $subscriptionModel->getCustomerAddressesBySchemeName($schemeName, 1); + +// // For shipping address +// $shippingInfo = $subscriptionModel->getCustomerAddressesBySchemeName($schemeName, 2); +// print_r($billingInfo); +// print_r($shippingInfo); + + +// // Handle the PDF generation using $billingInfo['customer_name'], $billingInfo['address'] (for billing address) +// // and $shippingInfo['customer_name'], $shippingInfo['address'] (for shipping address) +// } + diff --git a/app/Models/InvoiceModel.php b/app/Models/InvoiceModel.php index abb3e9ed..3da90aef 100644 --- a/app/Models/InvoiceModel.php +++ b/app/Models/InvoiceModel.php @@ -8,7 +8,7 @@ class InvoiceModel extends Model protected $table = 'invoice'; protected $primaryKey = 'invoice_id'; // protected $allowedFields = ['invoice_id','invoice_number','customer_id','invoice_date','event_id','business_id','created_on','business_id']; -protected $allowedFields = ['invoice_id', 'invoice_number', 'customer_id', 'invoice_date', 'due_date', 'subtotal', 'tax', 'discount', 'total_amount', 'payment_status', 'order_number', 'payment_method', 'event_id', 'business_id', 'shipping_address_id', 'billing_address_id', 'created_on', 'created_by', 'updated_on', 'updated_by', 'isactive','billing_address','shipping_address']; +protected $allowedFields = ['invoice_id', 'invoice_number', 'customer_id', 'invoice_date', 'due_date', 'subtotal', 'tax', 'discount', 'total_amount', 'payment_status', 'order_number', 'payment_method', 'event_id', 'business_id', 'shipping_address_id', 'billing_address_id', 'created_on', 'created_by', 'updated_on', 'updated_by', 'isactive','billing_address','shipping_address','status']; public function saveInvoiceItemDetails($data){ $i = 0; @@ -99,9 +99,12 @@ public function updateData($table, $data, $where) ->where('invoice_id', $id) ->join('customers as C','C.customer_id= invoice.customer_id','left') ->join('business as B','B.business_id = invoice.business_id','left') - ->select('invoice.*,concat(C.first_name," ",C.last_name) as customer_name,B.title,B.business_logo,B.address,B.city,B.state,B.postal_code,B.email,B.mobile_no') + ->join('customer_addresses as A','A.customer_id=invoice.customer_id and A.address_type=1','left') + ->select('invoice.*,concat(C.first_name," ",C.last_name) as customer_name,A.address_1, A.address_2,A.postal_code,A.city, A.state,C.mobile_no,B.title,B.business_logo,B.address,B.city,B.state,B.postal_code,B.email,B.mobile_no') + ->get() ->getResult(); + } public function getInvoiceItems($id) { diff --git a/app/Models/SchemeModel.php b/app/Models/SchemeModel.php index 4c4cfcca..a93e7ac9 100644 --- a/app/Models/SchemeModel.php +++ b/app/Models/SchemeModel.php @@ -36,6 +36,7 @@ class SchemeModel extends Model return $schemes; } + // public function getSchemeDuration($schemeId) // { // $query = $this->select('duration') @@ -49,6 +50,39 @@ class SchemeModel extends Model // return null; // } // } +public function getSchemeNamesByBusiness($business_id) +{ + $builder = $this->builder(); + $builder->select('scheme_id, scheme_name'); + $builder->where('business_id', $business_id); + + $query = $builder->get(); + + $schemes = []; + + foreach ($query->getResult() as $row) { + $schemes[$row->scheme_id] = $row->scheme_name; + } + + return $schemes; +} +public function getSchemeNames() +{ + $builder = $this->builder(); + $builder->select('scheme_id, scheme_name'); + $query = $builder->get(); + + $schemes =[]; + foreach ($query->getResult() as $row) { + $schemes[$row->scheme_id] = $row->scheme_name; + } + + return $schemes; + print_r($schemes);die(); +} + + + } diff --git a/app/Models/SubscriptionModel.php b/app/Models/SubscriptionModel.php index fe271023..9ce1a9ac 100644 --- a/app/Models/SubscriptionModel.php +++ b/app/Models/SubscriptionModel.php @@ -21,5 +21,73 @@ public function getAllSubscribersWithNames($where) $builder->where($where); return $builder->get()->getResultArray(); } +public function getCustomerNameBySchemeName($schemeName) +{ + $builder = $this->db->table('subscription'); + $builder->select('customers.first_name, customers.last_name'); + $builder->select('customer_addresses.address_1, customer_addresses.address_2'); + $builder->join('customer_addresses', 'customer_addresses.customer_id = subscription.customer_id'); + $builder->join('customers', 'customers.customer_id = subscription.customer_id'); + $builder->join('schemes', 'schemes.scheme_id = subscription.scheme_id'); + $builder->where('schemes.scheme_name', $schemeName); + + $query = $builder->get(); + + if ($query->getNumRows() > 0) { + $row = $query->getRow(); + return $row->first_name . ' ' . $row->last_name; + } else { + return 'Customer not found'; // You can return a default value or handle the case where the customer is not found. + } } +public function getCustomerAddressesBySchemeName($schemeName) +{ + $builder = $this->db->table('subscription'); + $builder->select('CONCAT(customers.first_name, " ", customers.last_name) as customer_name, customer_addresses.address_1, customer_addresses.address_2, customer_addresses.city, customer_addresses.state,customer_addresses.postal_code, customer_addresses.address_type,business.address,business.city,business.state,business.postal_code,business.country'); + $builder->join('customers', 'customers.customer_id = subscription.customer_id'); + $builder->join('customer_addresses', 'customer_addresses.customer_id = subscription.customer_id'); + $builder->join('business', 'business.business_id = subscription.business_id'); + $builder->join('schemes', 'schemes.scheme_id = subscription.scheme_id'); + $builder->where('schemes.scheme_name', $schemeName); + + $query = $builder->get(); + + $addresses = []; + + if ($query->getNumRows() > 0) { + foreach ($query->getResult() as $row) { + $addressType = $row->address_type; + $addresses[$addressType] = [ + 'customer_name' => $row->customer_name, + 'address_1' => $row->address_1, + 'address_2' =>$row->address_2, + 'city' => $row->city, + 'state' => $row->state, + 'postal_code'=>$row->postal_code, + ]; + } + } else { + $addresses = [ + 'billing' => [ + 'customer_name' => 'Customer not found', + 'address_line' => 'Billing Address not found', + 'city' => '', + 'state' => '', + ], + 'shipping' => [ + 'customer_name' => 'Customer not found', + 'address_line' => 'Shipping Address not found', + 'city' => '', + 'state' => '', + ], + ]; + } + + return $addresses; +} +} + + + + diff --git a/app/Views/address_pdf_template.php b/app/Views/address_pdf_template.php new file mode 100644 index 00000000..0b299746 --- /dev/null +++ b/app/Views/address_pdf_template.php @@ -0,0 +1,80 @@ + + + + + + Shipping Label Template + + + + +
+
+ +
+ +
+

address ?>,
city ?>, state ?>,

+

postal_code ?>

+

email ?>

+

mobile_no ?>

+
+
+

To:

+

customer_name ?>

+

address_1 ?>address_2 ?>

+

city ?> state ?>

+

postal_code ?>

+
+
+ Barcode +
+ +
+ + + diff --git a/app/Views/invoice_form.php b/app/Views/invoice_form.php index ad720db3..c9116071 100644 --- a/app/Views/invoice_form.php +++ b/app/Views/invoice_form.php @@ -228,16 +228,20 @@ +
- - - - - + + + + + + + + + +
@@ -683,4 +687,15 @@ $(document).ready(function() { } }); - \ No newline at end of file + + diff --git a/app/Views/invoice_list.php b/app/Views/invoice_list.php index bd53f8a7..dbc09b5b 100644 --- a/app/Views/invoice_list.php +++ b/app/Views/invoice_list.php @@ -20,8 +20,10 @@ Discount Tax --> Status + Approve INV Action view + Print @@ -31,16 +33,20 @@ invoice_number;?> invoice_date;?> customer_name;?> - status;?> - invoice_id; ?>" class="approve-button"> Approve + status;?> + invoice_id; ?>" class="approve-button"> + invoice_id; ?>" class="edit-button"> -invoice_id}") ?>" class="download-pdf-button"> Download PDF +invoice_id}") ?>" class="download-pdf-button"> + + + invoice_id}") ?>" class="print-address-button"> diff --git a/app/Views/print_addresses_form.php b/app/Views/print_addresses_form.php new file mode 100644 index 00000000..be574358 --- /dev/null +++ b/app/Views/print_addresses_form.php @@ -0,0 +1,80 @@ + + + + + + Shipping Label Template + + + + +
+
+ +
+ +
+ +

+
+ +
+

To:

+

+ +

+

 

+

+
+
+ Barcode +
+ +
+ + + diff --git a/app/Views/subscribers_list.php b/app/Views/subscribers_list.php index f25b6c62..f8757b63 100644 --- a/app/Views/subscribers_list.php +++ b/app/Views/subscribers_list.php @@ -3,7 +3,11 @@

@@ -35,8 +39,67 @@ -
+ + + + + + + + + + + +
+