diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 5b65b69..ec50383 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -50,7 +50,7 @@ $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"); -$routes->post("save_client", "Sales::save_client"); +$routes->post("save_vehicle", "Sales::save_vehicle"); //end salle order// //Client// @@ -58,6 +58,7 @@ $routes->get('client_index/', 'Client::client_index'); $routes->post("add_client", "Client::add_client"); $routes->get("new_client/(:any)", "Client::new_client/$1"); $routes->get("delete_client/(:any)", "Client::delete_client/$1"); +$routes->post("save_client", "Client::client_quick_create"); //end clent //Vendor// $routes->get('vendor_index/', 'Vendor::vendor_index'); diff --git a/app/Controllers/Client.php b/app/Controllers/Client.php index 7f63ade..a4388ac 100644 --- a/app/Controllers/Client.php +++ b/app/Controllers/Client.php @@ -104,5 +104,43 @@ class Client extends BaseController return redirect()->to('client_index'); } + + public function client_quick_create(){ + // Load ClientModel + $ClientModel = new ClientModel(); + + // Validate inputs + $validationRules = [ + 'client_name' => 'required|max_length[100]', + 'mobile_no' => 'required' // Assuming mobile number is 10 digits long + ]; + + if (!$this->validate($validationRules)) { + // If validation fails, return validation errors + return $this->response->setJSON(['success' => false, 'errors' => $this->validator->getErrors()]); + } + + // If validation passes, proceed to save the client + $data = [ + 'client_name' => $this->request->getPost('client_name'), + 'mobile_no' => $this->request->getPost('mobile_no'), + 'branch_id'=> $this->session->get('logged_user_branch_id'), + 'city'=>'Chennai', + 'state'=>'Tamil Nadu', + 'isactive' => 1 + ]; + + // Attempt to insert the client data + $id = $ClientModel->insert($data); + if ($id) { + $clientData = $ClientModel->where('client_id',$id)->first(); + + // If insertion succeeds, return success response + return $this->response->setJSON(['success' => true, 'message' => 'Client saved successfully.','clientData'=>$clientData]); + } else { + // If insertion fails, return error response + return $this->response->setJSON(['success' => false, 'message' => 'Failed to save client.']); + } + } } \ No newline at end of file diff --git a/app/Controllers/Products.php b/app/Controllers/Products.php index 9ade4f0..f76fa8a 100644 --- a/app/Controllers/Products.php +++ b/app/Controllers/Products.php @@ -65,7 +65,7 @@ class Products extends BaseController $ProductModel = new ProductModel(); $tax= $this->request->getPost('tax')/2; - + // print_r($this->request->getPost());die; $data = [ 'product_name' => $this->request->getPost('productname'), 'product_category' => $this->request->getPost('productcategory'), @@ -82,6 +82,8 @@ class Products extends BaseController 'vendor' => $this->request->getPost('vendor'), 'purchase_order_level' => $this->request->getPost('purchase_order_level'), 'quality' => $this->request->getPost('quality'), + 'make_id' => $this->request->getPost('make'), + 'models_id' => json_encode($this->request->getPost('model')), 'isactive' => 1 , 'branch_id' => $this->session->get('logged_user_branch_id') ]; @@ -124,26 +126,53 @@ class Products extends BaseController // Retrieve the make_id from the AJAX request public function get_vendors_by_model() { - $make_id = $this->request->getPost('make_id'); + $model_ids = $this->request->getPost('model_ids'); - // print_r( $model_ids);die; - - // If make_id or model_ids is not provided, return an empty response - - - // Create an instance of VendorModel + $make_id = $this->request->getPost('make_id'); $vendorModel = new VendorModel(); - - // Call the get_vendors_by_model method from the model - $vendors = $vendorModel->get_vendors_by_model($make_id, $model_ids); - // print_r($vendors); - // Return the response as JSON - // return $this->response->setJSON(['success' => true, 'message' => 'Client saved successfully.']); - if ($vendors) { - // Return the retrieved vendors as JSON response - return $this->response->setJSON($vendors); - } else { - // If no vendors were found, return an empty response or handle the error accordingly + $Vendors = $vendorModel->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll(); + if($Vendors){ + + $data = []; + + foreach ($model_ids as $modelKey => $modelValue) + { + foreach ($Vendors as $vendorsKey => $vendorsValue) + { + $modelsArray = json_decode($vendorsValue['model'], true); + if (is_array($modelsArray)) + { + $findIfModelExist = array_search($modelValue, $modelsArray); + if ($findIfModelExist !== false) + { + array_push($data,$vendorsValue); + } + } + } + } + + if (count($data)) { + + $arrayOfArrays = array_map(function($obj) { + return (array) $obj; + }, $data); + + // Remove duplicates based on string representation of each object + $uniqueArray = array_map('unserialize', array_unique(array_map('serialize', $arrayOfArrays))); + + // Convert the unique array of associative arrays back to an array of objects + $uniqueObjectsArray = array_map(function($arr) { + return (object) $arr; + }, $uniqueArray); + + return $this->response->setJSON($uniqueArray); + + } else { + + return $this->response->setJSON([]); + } + }else { + return $this->response->setJSON([]); } } diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php index 9f2895e..376bcf3 100644 --- a/app/Controllers/Sales.php +++ b/app/Controllers/Sales.php @@ -6,6 +6,8 @@ use App\Models\SalesOrderModel; use App\Models\SalesOrderProductModel; use App\Models\ProductModel; use App\Models\ClientModel; +use App\Models\BikeMakeModel; +use App\Models\VehicleModel; use Mpdf\Mpdf; class Sales extends BaseController { @@ -23,7 +25,7 @@ class Sales extends BaseController $SalesOrderModel = new SalesOrderModel(); $sales=$SalesOrderModel->getSalesOrdersWithProducts($this->session->get('logged_user_branch_id')); $data['sales']=$sales; - +// print_r($sales);die; return view('sales_list',$data); } @@ -39,13 +41,18 @@ class Sales extends BaseController $client=$ClientModel ->where('isactive',1) ->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll(); -// print_r($client);die; + $VehicleModel = new VehicleModel(); + $vehicle = $VehicleModel->getCustomerandVehicle($this->session->get('logged_user_branch_id')); + // ->where('isactive',1) + // ->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll(); +// print_r($vehicle);die; $ProductModel = new ProductModel(); $product=$ProductModel ->where('isactive',1) ->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll(); $data['product']=$product; $data['client']=$client; + $data['vehicle']=$vehicle; } else if ($sales_order_id !== '0') { $data['page_name']="Edit Sale Order"; $ClientModel = new ClientModel(); @@ -58,14 +65,19 @@ $sales_product=$SalesProductOrderModel->where('sales_order_id', $sales_order_id) $data['sales_product']=$sales_product; $data['client']=$client; // print_r($sales_product);die; - +$VehicleModel = new VehicleModel(); + $vehicle=$VehicleModel + ->where('isactive',1) + ->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll(); + $data['vehicle']=$vehicle; $ProductModel = new ProductModel(); $product=$ProductModel->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll(); $data['product']=$product; } // print_r($data);die; $data['sales_order_id'] = $sales_order_id; - + $bikeMakeModel = new BikeMakeModel(); + $data['makeData'] =$bikeMakeModel->findAll(); return view('sales_order_form',$data); } @@ -78,7 +90,8 @@ $data['product']=$product; // Prepare data for sales order $data = [ - 'client_id' => $this->request->getPost('client_id'), + 'client_name' => $this->request->getPost('client_id'), + 'vehicle_id'=>$this->request->getPost('vehicle_id'), 'billing_address' => $this->request->getPost('billing_address'), 'billing_city' => $this->request->getPost('city'), 'mobile_no' => $this->request->getPost('mobile_no'), @@ -88,13 +101,13 @@ $data['product']=$product; 'terms_condition' => $this->request->getPost('terms_condition'), 'subtotal' => $this->request->getPost('sub_total'), 'tax' => $this->request->getPost('invoice_tax'), - '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 'branch_id'=> $this->session->get('logged_user_branch_id'), ]; -// print_r($data);die; +// print_r($data);die; // Insert or update sales order $sales_order_id = $this->request->getPost('sales_order_id'); if (!empty($sales_order_id)) { @@ -280,38 +293,36 @@ public function delete_sales_product() // Output the PDF to the browser for download $mpdf->Output('invoice_' . date('Y-m-d H-i-s') . '.pdf', 'D'); } - public function save_client(){ + + + public function save_vehicle(){ // Load ClientModel - $ClientModel = new ClientModel(); + $VehicleModel = new VehicleModel(); // Validate inputs - $validationRules = [ - 'client_name' => 'required|max_length[100]', - 'mobile_no' => 'required' // Assuming mobile number is 10 digits long - ]; - - if (!$this->validate($validationRules)) { - // If validation fails, return validation errors - return $this->response->setJSON(['success' => false, 'errors' => $this->validator->getErrors()]); - } + // If validation passes, proceed to save the client $data = [ - 'client_name' => $this->request->getPost('client_name'), + 'client_id' => $this->request->getPost('client_id'), + 'mobile_no' => $this->request->getPost('mobile_no'), + 'reg_no' => $this->request->getPost('reg_no'), + 'model' => $this->request->getPost('model'), + 'make' => $this->request->getPost('make'), 'branch_id'=> $this->session->get('logged_user_branch_id'), 'city'=>'Chennai', 'state'=>'Tamil Nadu', 'isactive' => 1 ]; - + // Attempt to insert the client data - $id = $ClientModel->insert($data); + $id = $VehicleModel->insert($data); if ($id) { - $clientData = $ClientModel->where('client_id',$id)->first(); + $vehicleData = $VehicleModel->where('vehicle_id',$id)->first(); // If insertion succeeds, return success response - return $this->response->setJSON(['success' => true, 'message' => 'Client saved successfully.','clientData'=>$clientData]); + return $this->response->setJSON(['success' => true, 'message' => 'Client saved successfully.','vehicleData'=>$vehicleData]); } else { // If insertion fails, return error response return $this->response->setJSON(['success' => false, 'message' => 'Failed to save client.']); diff --git a/app/Models/ProductModel.php b/app/Models/ProductModel.php index a0f7a1f..63c54ff 100644 --- a/app/Models/ProductModel.php +++ b/app/Models/ProductModel.php @@ -5,7 +5,7 @@ class ProductModel extends Model { protected $table = 'products'; protected $primaryKey = 'product_id'; - protected $allowedFields = ['product_id','quality','product_name','branch_id','search_tag','product_category','mfr_part_no','purchase_order_level','serial_no','unit_price','commision_rate','sgst','cgst','purchase_cost','usage_unit','vendor','qty_stock','reorder_level','handler','qty_in_demand','product_image','description','isactive']; + protected $allowedFields = ['product_id','make_id','models_id','quality','product_name','branch_id','search_tag','product_category','mfr_part_no','purchase_order_level','serial_no','unit_price','commision_rate','sgst','cgst','purchase_cost','usage_unit','vendor','qty_stock','reorder_level','handler','qty_in_demand','product_image','description','isactive']; public function getTax() diff --git a/app/Models/SalesOrderModel.php b/app/Models/SalesOrderModel.php index 48855f6..8edbadc 100644 --- a/app/Models/SalesOrderModel.php +++ b/app/Models/SalesOrderModel.php @@ -5,14 +5,14 @@ class SalesOrderModel extends Model { protected $table = 'sales_order'; protected $primaryKey = 'sales_order_id'; - 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']; + protected $allowedFields = ['sales_order_id','client_name','vehicle_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('sales_order.*') ->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() @@ -22,11 +22,11 @@ class SalesOrderModel extends Model public function getSalesOrdersWithProducts($logged_user_branch_id) { // Select required fields from both tables - $this->select('sales_order.*, COUNT(sales_order_product.sales_order_id) AS product_count,client.client_name'); + $this->select('sales_order.*, COUNT(sales_order_product.sales_order_id) AS product_count,vehicle.reg_no'); // Join the sales_order_product table based on sales_order_id $this->join('sales_order_product', 'sales_order_product.sales_order_id = sales_order.sales_order_id'); - $this->join('client', 'client.client_id = sales_order.client_id'); + $this->join('vehicle', 'vehicle.vehicle_id = sales_order.vehicle_id'); // Group by sales_order_id to get count of products per sales order $this->groupBy('sales_order.sales_order_id'); diff --git a/app/Models/VehicleModel.php b/app/Models/VehicleModel.php index 23d716b..9f6b2fd 100644 --- a/app/Models/VehicleModel.php +++ b/app/Models/VehicleModel.php @@ -12,7 +12,7 @@ class VehicleModel extends Model public function getCustomerandVehicle($branch_id) { // Select required fields from both tables - $this->select('vehicle.*, client.client_name , make.make as make_name , model.model_name as model_name'); + $this->select('vehicle.*, client.client_name ,client.address,client.city,client.state,client.postal_code, make.make as make_name , model.model_name as model_name'); $this->join('client', 'client.client_id = vehicle.client_id'); $this->join('make', 'make.make_id = vehicle.make'); $this->join('model', 'model.model_id = vehicle.model'); diff --git a/app/Views/client_form.php b/app/Views/client_form.php index a8c072a..e962a9a 100644 --- a/app/Views/client_form.php +++ b/app/Views/client_form.php @@ -21,6 +21,7 @@