EVOLUTION-NINJA
Edit File: CustomerController.php
<?php namespace App\Controllers; use Config\Database; use App\Models\ProductModel; use App\Models\ProductTypeModel; class CustomerController extends BaseController { // ========================================= // FETCH PRODUCT TYPES // ========================================= public function getProductTypes() { $productTypeModel = new ProductTypeModel(); $types = $productTypeModel ->where('status', 'active') ->findAll(); return $this->response->setJSON($types); } // ========================================= // FETCH PRODUCTS BASED ON TYPE // ========================================= public function getProductsByType() { $product_type = $this->request->getPost('product_type'); $productModel = new ProductModel(); $products = $productModel ->where('product_category', $product_type) ->where('status', 'active') ->findAll(); return $this->response->setJSON($products); } // ========================================= // FETCH PRODUCT DETAILS // ========================================= public function getProductDetails() { $product_id = $this->request->getPost('product_id'); $productModel = new ProductModel(); $product = $productModel ->where('product_id', $product_id) ->first(); return $this->response->setJSON($product); } // public function viewInvoice($id) // { // $model = new \App\Models\CreateCustomerModel(); // $data['customer'] = $model // ->where('id', $id) // ->first(); // return view('invoice/view_invoice', $data); // } // public function viewInvoice($id) // { // $model = new \App\Models\CreateCustomerModel(); // $data['customer'] = $model->where('id', $id)->first(); // $yearMonth = date('Ym'); // // use actual database ID (this gives 453, 231 style naturally) // $data['invoice_no'] = "INV-{$yearMonth}-{$id}"; // return view('invoice/view_invoice', $data); // } // public function viewInvoice($id) // { // $customerModel = new \App\Models\CreateCustomerModel(); // $productModel = new \App\Models\ProductModel(); // // customer data // $customer = $customerModel // ->where('id', $id) // ->first(); // // product details using product_id // $product = $productModel // ->where('product_id', $customer['product_id']) // ->first(); // // attach model name into customer array // $customer['product_model'] = // $product['model_name'] ?? ''; // $data['customer'] = $customer; // $yearMonth = date('Ym'); // $data['invoice_no'] = // "INV-{$yearMonth}-{$id}"; // return view('invoice/view_invoice', $data); // } // public function viewInvoice($id) // { // $customerModel = new \App\Models\CreateCustomerModel(); // $productModel = new \App\Models\ProductModel(); // $aeModel = new \App\Models\AETransactionModel(); // // CUSTOMER // $customer = $customerModel // ->where('id', $id) // ->first(); // // PRODUCT // $product = $productModel // ->where('product_id', $customer['product_id']) // ->first(); // // MODEL NAME // $customer['product_model'] = // $product['model_name'] ?? ''; // // AE REPORT DATA // $aeData = $aeModel // ->where('customer_id', $id) // ->first(); // // FARMER SHARE // $customer['farmer_share'] = // $aeData['farmer_share'] ?? 0; // $data['customer'] = $customer; // $yearMonth = date('Ym'); // $data['invoice_no'] = // "INV-{$yearMonth}-{$id}"; // return view('invoice/view_invoice', $data); // } public function viewInvoice($id) { date_default_timezone_set('Asia/Kolkata'); $db = \Config\Database::connect(); $customer = $db->table('create_customer') ->select('create_customer.*, dealers.dealer_code, dealers.name as dealer_name, dealers.address as dealer_address, dealers.mobile as dealer_phone, dealers.pin_code as dealer_pin_code') ->join('dealers', 'dealers.dealer_code = create_customer.dealer_id', 'left') ->where('create_customer.id', $id) ->get() ->getRowArray(); if (!$customer) { return "Customer not found for ID: " . $id; } // ========================= // PRODUCT MODEL FETCH // ========================= $product = $db->table('products') ->where('product_id', $customer['product_id']) ->get() ->getRowArray(); $customer['product_model'] = $product['model_name'] ?? ''; // ========================= // AE TABLE // ========================= $aeModel = new \App\Models\AETransactionModel(); $aeData = $aeModel ->where('customer_id', $id) ->orderBy('id', 'DESC') ->first() ?? []; $customer['farmer_share'] = $customer['share_amount'] ?? 0; // FIXED: these must come from create_customer (not AE) $customer['central_gov_share'] = $customer['central_gov_share'] ?? 0; $customer['state_gov_share'] = $customer['state_gov_share'] ?? 0; // ========================= // DEFAULT FIELDS // ========================= // $customer['product_model'] = $customer['product_type'] ?? ''; $customer['part_no'] = $customer['part_no'] ?? ''; $customer['hsn_code'] = $customer['hsn_code'] ?? ''; $customer['vin_no'] = $customer['vin_no'] ?? ''; $customer['recipient_id'] = $customer['recipient_id'] ?? ''; // ========================= // GST CALCULATION (FIXED LOGIC) // ========================= $rc_price = (float) ($customer['rc_price'] ?? 0); $basic_price = (float) ($customer['basic_price'] ?? 0); // 1. Get exact total tax amount from the currency values $gst_total = $rc_price - $basic_price; // 2. Split the tax amount evenly (forcing precise decimal precision) $cgst_amount = round($gst_total / 2, 2); $sgst_amount = $gst_total - $cgst_amount; // Ensures remainder goes to SGST so the sum is exact // 3. Recalculate total tax based on split values to prevent penny mismatches $gst_total = $cgst_amount + $sgst_amount; // 4. Calculate display percentages and ROUND THEM to 2 decimal places immediately $gst_percent = $basic_price > 0 ? round(($gst_total / $basic_price) * 100, 2) : 0; $cgst_percent = round($gst_percent / 2, 2); $sgst_percent = round($gst_percent / 2, 2); // 5. Store exact values in array $customer['gst_total'] = $gst_total; $customer['cgst_amount'] = $cgst_amount; $customer['sgst_amount'] = $sgst_amount; $customer['cgst_percent'] = $cgst_percent; $customer['sgst_percent'] = $sgst_percent; // 6. FINAL TOTAL (Your required formula: basic + cgst + sgst) $total = $basic_price + $cgst_amount + $sgst_amount; $customer['total_payable'] = $total; $customer['total_payable'] = $total; // ========================= // FINAL DATA // ========================= $data['customer'] = $customer; $data['invoice_no'] = date('Ym') . $customer['id']; return view('invoice/view_invoice', $data); } public function deliveryChallan($id) { $db = \Config\Database::connect(); $customer = $db->table('create_customer') ->where('id', $id) ->get() ->getRowArray(); if (!$customer) { return "Customer not found"; } // FETCH MODEL NAME FROM PRODUCTS TABLE $product = $db->table('products') ->where('product_id', $customer['product_id']) ->get() ->getRowArray(); $customer['product_model'] = $product['model_name'] ?? ''; $data['customer'] = $customer; return view('invoice/delivery_challan', $data); } }