EVOLUTION-NINJA
Edit File: ProductController.php
<?php namespace App\Controllers; use App\Models\ProductModel; use App\Models\ProductTypeModel; class ProductController extends BaseController { public function create() { $productTypeModel = new ProductTypeModel(); $data['product_types'] = $productTypeModel ->where('status', 'active') ->orderBy('id', 'DESC') ->findAll(); return view('createproject/createProject',$data); } public function createCustomer() { $productTypeModel = new ProductTypeModel(); $dealerModel = new DealerModel(); // DEALERS $data['dealers'] = $dealerModel ->where('status', 'active') ->findAll(); // PRODUCT TYPES $data['product_types'] = $productTypeModel ->where('status', 'active') ->findAll(); return view('customer/create_customer', $data); } // ============================== // GET PRODUCTS BY CATEGORY // ============================== public function getProductsByCategory() { $category = $this->request->getPost('category'); $productModel = new ProductModel(); $products = $productModel ->where('product_category', $category) ->where('status', 'active') ->findAll(); return $this->response->setJSON($products); } // ============================== // GET PRODUCT DETAILS // ============================== public function getProductDetails() { $product_id = $this->request->getPost('product_id'); $productModel = new ProductModel(); $product = $productModel->find($product_id); return $this->response->setJSON($product); } // public function store() // { // $productModel = new \App\Models\ProductModel(); // $rc_price = $this->request->getPost('rc_price'); // $ae_commission = $this->request->getPost('ae_commission'); // $gst_difference = $this->request->getPost('gst_difference'); // $ndp_with_gst= $this->request->getPost('ndp_with_gst'); // $ndp = $this->request->getPost('ndp'); // // 🔥 CALCULATE BILLING PRICE // $billing_price = $rc_price - $ae_commission - $gst_difference; // $dealer_margin = $billing_price - $ndp_with_gst; // // $data = [ // // 'model_name' => $this->request->getPost('model_name'), // // 'product_category' => $this->request->getPost('product_category'), // // 'rc_price' => $rc_price, // // 'ndp_with_gst' => $ndp_with_gst, // // 'gst_difference' => $gst_difference, // // 'ae_commission' => $ae_commission, // // 'dealer_margin' => $dealer_margin, // // 'billing_price' => $billing_price, // ✅ AUTO STORED // // 'status' => $this->request->getPost('status') // // ]; // $data = [ // 'model_name' => $this->request->getPost('model_name'), // 'product_category' => $this->request->getPost('product_category'), // 'basic_price' => $this->request->getPost('basic_price'), // 'ndp' => $ndp, // 'rc_price' => $rc_price, // 'ndp_with_gst' => $ndp_with_gst, // 'gst_difference' => $gst_difference, // 'ae_commission' => $ae_commission, // 'dealer_margin' => $dealer_margin, // 'billing_price' => $billing_price, // 'status' => $this->request->getPost('status') // ]; // if ($productModel->insert($data)) { // return $this->response->setJSON([ // 'status' => 1, // 'message' => 'Product added successfully' // ]); // } else { // return $this->response->setJSON([ // 'status' => 0, // 'message' => 'Failed to add product' // ]); // } // } public function store() { $productModel = new \App\Models\ProductModel(); $rc_price = $this->request->getPost('rc_price'); $basic_price = $this->request->getPost('basic_price'); // GST % $gst_percent = (($rc_price - $basic_price) / $basic_price) * 100; $data = [ 'model_name' => $this->request->getPost('model_name'), 'product_category' => $this->request->getPost('product_category'), 'rc_price' => $rc_price, 'basic_price' => $basic_price, 'ndp' => $this->request->getPost('ndp'), 'gst_percent' => round($gst_percent, 2), 'status' => $this->request->getPost('status') ]; if ($productModel->insert($data)) { return $this->response->setJSON([ 'status' => 1, 'message' => 'Product added successfully' ]); } else { return $this->response->setJSON([ 'status' => 0, 'message' => 'Failed to add product' ]); } } public function list() { return view('products/list'); } // public function getProducts() // { // $productModel = new \App\Models\ProductModel(); // $search = $this->request->getGet('search'); // if ($search) { // $products = $productModel // ->like('model_name', $search) // ->findAll(); // } else { // $products = $productModel->findAll(); // } // return $this->response->setJSON($products); // } public function getProducts() { $productModel = new \App\Models\ProductModel(); $search = $this->request->getGet('search'); if ($search) { $products = $productModel ->where('status', 'active') ->like('model_name', $search) ->findAll(); } else { $products = $productModel ->where('status', 'active') ->findAll(); } return $this->response->setJSON($products); } public function searchSuggestions() { $productModel = new \App\Models\ProductModel(); $term = $this->request->getGet('term'); $products = $productModel ->like('model_name', $term) ->findAll(); $suggestions = []; foreach ($products as $p) { $suggestions[] = $p['model_name']; } return $this->response->setJSON($suggestions); } public function edit($id) { $productModel = new ProductModel(); $productTypeModel = new ProductTypeModel(); $data['product'] = $productModel->find($id); $data['product_types'] = $productTypeModel ->where('status', 'active') ->orderBy('id', 'DESC') ->findAll(); return view('createproject/project_edit', $data); } // public function update($id) // { // $productModel = new \App\Models\ProductModel(); // $rc_price = $this->request->getPost('rc_price'); // $ae_commission = $this->request->getPost('ae_commission'); // $gst_difference = $this->request->getPost('gst_difference'); // $ndp_with_gst= $this->request->getPost('ndp_with_gst'); // $ndp = $this->request->getPost('ndp'); // // 🔥 CALCULATE BILLING PRICE // $billing_price = $rc_price - $ae_commission - $gst_difference; // $dealer_margin = $billing_price - $ndp_with_gst; // // $data = [ // // 'model_name' => $this->request->getPost('model_name'), // // 'product_category' => $this->request->getPost('product_category'), // // 'rc_price' => $rc_price, // // 'ndp_with_gst' => $ndp_with_gst, // // 'gst_difference' => $gst_difference, // // 'ae_commission' => $ae_commission, // // 'dealer_margin' => $dealer_margin, // // 'billing_price' => $billing_price, // ✅ AUTO STORED // // 'status' => $this->request->getPost('status') // // ]; // $data = [ // 'model_name' => $this->request->getPost('model_name'), // 'product_category' => $this->request->getPost('product_category'), // 'basic_price' => $this->request->getPost('basic_price'), // 'ndp' => $ndp, // 'rc_price' => $rc_price, // 'ndp_with_gst' => $ndp_with_gst, // 'gst_difference' => $gst_difference, // 'ae_commission' => $ae_commission, // 'dealer_margin' => $dealer_margin, // 'billing_price' => $billing_price, // 'status' => $this->request->getPost('status') // ]; // $productModel->update($id, $data); // return $this->response->setJSON([ // 'status' => 1, // 'message' => 'Product updated successfully' // ]); // } public function update($id) { $productModel = new \App\Models\ProductModel(); $rc_price = $this->request->getPost('rc_price'); $basic_price = $this->request->getPost('basic_price'); // GST % $gst_percent = (($rc_price - $basic_price) / $basic_price) * 100; $data = [ 'model_name' => $this->request->getPost('model_name'), 'product_category' => $this->request->getPost('product_category'), 'rc_price' => $rc_price, 'basic_price' => $basic_price, 'ndp' => $this->request->getPost('ndp'), 'gst_percent' => round($gst_percent, 2), 'status' => $this->request->getPost('status') ]; $productModel->update($id, $data); return $this->response->setJSON([ 'status' => 1, 'message' => 'Product updated successfully' ]); } public function delete() { $id = $this->request->getPost('id'); $productModel = new \App\Models\ProductModel(); $productModel->delete($id); return $this->response->setJSON([ 'status' => 1, 'message' => 'Product deleted successfully' ]); } public function testProductTypes() { $model = new \App\Models\ProductTypeModel(); $data = $model ->where('status', 'active') ->findAll(); return $this->response->setJSON($data); } public function aeCalculator() { $productModel = new \App\Models\ProductModel(); $data['products'] = $productModel ->where('status', 'active') ->findAll(); return view('AEmaster/ae_calculator', $data); } public function saveAECalculator() { $model = new \App\Models\AECalculatorModel(); $product_id = $this->request->getPost('product_id'); $ae_commission_percent = $this->request->getPost('ae_commission_percent'); $rc_price = $this->request->getPost('rc_price'); $ndp = $this->request->getPost('ndp'); // CHECK DUPLICATE $duplicate = $model ->where('product_id', $product_id) ->where('ae_commission_percent', $ae_commission_percent) ->where('rc_price', $rc_price) ->where('ndp', $ndp) ->first(); if ($duplicate) { return $this->response->setJSON([ 'status' => 0, 'message' => 'Duplicate calculator entry already exists' ]); } // INSERT DATA $data = [ 'product_id' => $product_id, 'model_name' => $this->request->getPost('model_name'), 'rc_price' => $rc_price, 'basic_price' => $this->request->getPost('basic_price'), 'ndp' => $ndp, 'gst_percent' => $this->request->getPost('gst_percent'), 'ae_commission_percent' => $ae_commission_percent, 'ae_commission' => $this->request->getPost('ae_commission'), 'gst_on_ae_commission' => $this->request->getPost('gst_on_ae_commission'), 'billing_price' => $this->request->getPost('billing_price'), 'gst_on_rc_price' => $this->request->getPost('gst_on_rc_price'), 'gst_on_ndp' => $this->request->getPost('gst_on_ndp'), 'gst_difference' => $this->request->getPost('gst_difference'), 'ndp_with_gst' => $this->request->getPost('ndp_with_gst'), 'dealer_margin' => $this->request->getPost('dealer_margin') ]; if ($model->insert($data)) { return $this->response->setJSON([ 'status' => 1, 'message' => 'AE Calculator data saved successfully' ]); } else { return $this->response->setJSON([ 'status' => 0, 'message' => 'Failed to save' ]); } } public function calculatorReport() { return view('AEmaster/ae_calculator_report'); } public function getCalculatorReport() { $db = \Config\Database::connect(); $builder = $db->table('ae_calculator_history'); $builder->select('*'); // FILTERS if ($this->request->getPost('model_name')) { $builder->where( 'model_name', $this->request->getPost('model_name') ); } if ($this->request->getPost('gst_percent')) { $builder->where( 'gst_percent', $this->request->getPost('gst_percent') ); } if ($this->request->getPost('ae_commission_percent')) { $builder->where( 'ae_commission_percent', $this->request->getPost('ae_commission_percent') ); } if ($this->request->getPost('created_at')) { $builder->like( 'created_at', $this->request->getPost('created_at') ); } $builder->orderBy('id', 'DESC'); $data = $builder->get()->getResultArray(); return $this->response->setJSON($data); } }