diff --git a/app/Config/Routes.php b/app/Config/Routes.php index a1ffcee..b5a2ba1 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -42,7 +42,9 @@ $routes->get("new_product/(:any)", "Products::new_product/$1"); $routes->get("delete_product/(:any)", "Products::delete_product/$1"); $routes->post("get_vendors_by_model", "Products::get_vendors_by_model"); $routes->post("get_models", "Products::get_models"); +$routes->get("get_makes", "Products::get_makes"); $routes->post("save_make", "Products::save_make"); +$routes->post("save_model", "Products::save_model"); //end products// //Sales order // @@ -118,5 +120,10 @@ $routes->post('create_manufacturer', 'Manufacturer::create_manufacturer'); $routes->get('purchase_index', 'Purchase::purchase_index'); $routes->get("new_purchase/(:any)", "Purchase::new_purchase/$1"); -$routes->get('get_product/(:any)', 'Purchase::get_product/$1'); +$routes->get('get_vendor/(:any)', 'Purchase::get_vendor/$1'); $routes->post("add_purchase", "Purchase::add_purchase"); +$routes->get("delete_purchase/(:any)", "Purchase::delete_purchase/$1"); +$routes->post("getVendorProducts", "Purchase::getVendorProducts"); +$routes->post("delete_purchase_product", "Purchase::delete_purchase_product"); +// Reorder Level// +$routes->get('reorder_level_index', 'Reorderlevel::reorder_level_index'); diff --git a/app/Controllers/Products.php b/app/Controllers/Products.php index 76e14cf..c93149b 100644 --- a/app/Controllers/Products.php +++ b/app/Controllers/Products.php @@ -294,5 +294,35 @@ public function save_make() { // Send JSON response back to the client return $this->response->setJSON($response); } +public function get_makes() +{ + $BikeMakeModel = new BikeMakeModel(); // Adjust this according to your actual model name + $makes = $BikeMakeModel->findAll(); + return $this->response->setJSON($makes); +} +public function save_model() { + $response = []; + $BikemodelsModel = new BikemodelsModel(); + + $make_id = $this->request->getPost('make_id'); + $model = $this->request->getPost('modelvalue'); + if (!empty($make_id) && !empty($model)) { + $modelData = [ + 'model_name' => $model, + 'make_id' => $make_id, + ]; + + $BikemodelsModel->insert($modelData); + + + $response['success'] = true; + } else { + + $response['success'] = false; + } + + + return $this->response->setJSON($response); +} } \ No newline at end of file diff --git a/app/Controllers/Purchase.php b/app/Controllers/Purchase.php index 780f6a3..1b17194 100644 --- a/app/Controllers/Purchase.php +++ b/app/Controllers/Purchase.php @@ -7,6 +7,7 @@ use App\Models\PurchaseOrderModel; use App\Models\VendorModel; use App\Models\BranchModel; use App\Models\BusinessModel; + use App\Models\PurchaseOrderChildModel; class Purchase extends BaseController @@ -21,189 +22,266 @@ class Purchase extends BaseController { $PurchaseOrderModel = new PurchaseOrderModel(); - $purchase = $PurchaseOrderModel->findAll(); + $purchase = $PurchaseOrderModel->getPurchaseOrdersWithProducts($this->session->get('logged_user_branch_id')); // echo "
";
         // print_r($product);die;
         $data['purchase']=$purchase;
     
-        echo "
";
-        print_r($data);die;
+        // echo "
";
+        // print_r($data);die;
         return view('purchase_list',$data);
     }
           
-    public function new_purchase($purchase_id)
+    public function new_purchase($purchase_order_id)
     {
-        if ($purchase_id === '0') {
+        if ($purchase_order_id === '0') {
            
             $data['purchase'] = [];  
             $PurchaseOrderModel = new PurchaseOrderModel();
+
+            $VendorModel = new VendorModel();
+            $vendor = $VendorModel->select('vendor_id,vendor_name')->findAll();
+
+
+            $BranchModel = new BranchModel();
+            $BusinessModel = new BusinessModel();
+            $branch = $BranchModel->where('branch_id', $this->session->get('logged_user_branch_id'))->get()->getRowArray();
+            $business = $BusinessModel->where('business_id', $this->session->get('logged_user_business_id'))->get()->getRowArray();
             $ProductModel = new ProductModel();
-            $product = $ProductModel->select('product_id,product_name')->findAll();
+            $product=$ProductModel
+            ->where('isactive',1)
+            ->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
+            $data['product']=$product;   
+            $data['branch'] = $branch;  
+            $data['business'] = $business;  
 
             $data['page_name']="Add Purchase Order";
-            $data['product']=$product;
+            $data['vendor']=$vendor;
 
-        } else if ($purchase_id !== '0') {
-            $data['page_name']="Edit Product";
-            $ProductModel = new ProductModel();
-            $products=$ProductModel->where('product_id', $product_id)->get()->getRowArray();
-           
-            $data['products']=$products;  
+            $data['vendor_id'] = [];
+        } else if ($purchase_order_id !== '0') {
+            $data['page_name']="Edit Purchase Order";
 
-            $BikemakeModel = new BikemakeModel();
-            $data['make'] = $BikemakeModel->findAll();
-            $BikemodelsModel = new BikemodelsModel();
-            $make_id = $products['make_id'];
-            $data['models'] = $BikemodelsModel->where('make_id', $make_id)->findAll();
-          
-            $tax=$ProductModel->getTax();
-            $data['tax']=$tax;
+            $PurchaseOrderModel = new PurchaseOrderModel();
+            $purchase=$PurchaseOrderModel->where('purchase_order_id', $purchase_order_id)->get()->getRowArray();
+            $data['purchase'] = $purchase;
+               
+            
+            
             $VendorModel = new VendorModel();
-            $data['vendors']=$VendorModel->findAll();         
+            $vendor = $VendorModel->select('vendor_id,vendor_name')->findAll();
+            $data['vendor'] = $vendor;
+
+            $data['vendor_id'] = $purchase['vendor_id'];
+// print_r($vendor);die;
+            $vendorId=$purchase['vendor_id'];
+            $encodedVendorId = json_encode([$vendorId]);
+            $productModel = new ProductModel();
+
+    // Query the database to find the product based on make_id and model_id
+    $product = $productModel->where('JSON_CONTAINS(vendor, \'' . $encodedVendorId . '\')', null, false)
+                            ->findAll();
+    $data['product']=$product;
+// print_r($product);die;
+
+
+
+            $PurchaseOrderChildModel = new PurchaseOrderChildModel();
+            $purchase_product=$PurchaseOrderChildModel->where('purchase_order_id', $purchase_order_id)->findAll();
+            $data['purchase_product']=$purchase_product;
+
         }
-        $data['purchase_id'] = $purchase_id;
-   
+        // echo "
";
+        // print_r($data);die;
         echo view('purchase_form',$data);
     }
 
     public function add_purchase()
     {
-
         $PurchaseOrderModel = new PurchaseOrderModel();
-    
-
+        $ProductModel = new ProductModel();
+        $PurchaseOrderChildModel = new PurchaseOrderChildModel(); 
         $data = [
-            'product_id' => $this->request->getPost('product_id'),
             'vendor_id' => $this->request->getPost('vendor_id'),
-            'product_quantity' => $this->request->getPost('product_quantity'),
-            'unit_price' => $this->request->getPost('unit_price'),
-            'total_price' => $this->request->getPost('total_price'),
             'order_date' => $this->request->getPost('order_date'),
             'contact_person_name' => $this->request->getPost('contact_person_name'),
             'contact_person_mobile'=> $this->request->getPost('contact_person_mobile'),
             'status'=> $this->request->getPost('purchase_status'),
-            'billing_addresss' => $this->request->getPost('billing_addresss'),
+            'billing_address' => $this->request->getPost('billing_address'),
             'billing_state' => $this->request->getPost('billing_state'),
             'billing_city' => $this->request->getPost('billing_city'),
             'billing_postal_code' => $this->request->getPost('billing_postal_code'), 
-            'shipping_addresss' => $this->request->getPost('shipping_addresss'), 
-            'shipping_state' => json_encode($this->request->getPost('shipping_state')), 
+            'shipping_address' => $this->request->getPost('shipping_address'), 
+            'shipping_state' => $this->request->getPost('shipping_state'), 
             'shipping_city' =>$this->request->getPost('shipping_city'),
             'shipping_postal_code' => $this->request->getPost('shipping_postal_code'),
-            'description' => $this->request->getPost('description'),
+            
+            'terms_condition' => $this->request->getPost('terms_condition'),
+            'subtotal' => $this->request->getPost('sub_total'),
+            'tax' => $this->request->getPost('invoice_tax'),
+           
+            'total'=> $this->request->getPost('grand_total'),
+            'branch_id'=> $this->session->get('logged_user_branch_id'),
         ];
-      
-        // $product_id = $this->request->getPost('product_id');
-    
-       
-        // if (!empty($product_id)) {
+
+        // print_r($data);
+        $purchase_order_id = $this->request->getPost('purchase_order_id');
+        //  echo  $purchase_order_id;die;
+        if (!empty($purchase_order_id)) {
         
-        //     $PurchaseOrderModel->update($product_id, $data);
-        // } else {
+            $PurchaseOrderModel->update($purchase_order_id, $data);
+        } else {
             
             $PurchaseOrderModel->insert($data);
-        // }
-    
+            $purchase_order_id = $PurchaseOrderModel->getInsertID(); 
+        }
+        $orderNumber = 'PO' .  str_pad($purchase_order_id, 8, '0', STR_PAD_LEFT);
+        // print_r($orderNumber);die;
+            // Update sales order with generated order number
+            $PurchaseOrderModel->update($purchase_order_id, ['order_number' => $orderNumber]);
+        //  print_r();die;
+            // Prepare data for sales order product
+            $product_ids = $this->request->getPost('item_details');
+            $quantities = $this->request->getPost('quantity');
+            // $discounts = $this->request->getPost('discount_amount');
+            // $discount_types = $this->request->getPost('discount_type');
+            $amounts = $this->request->getPost('amount');
+            $itemtax=$this->request->getPost('item-tax');
+            $unitprice=$this->request->getPost('unit-price');
+            $purchase_order_child_id = $this->request->getPost('purchase_order_child_id');
+        // print_r($purchase_order_child_id);die;
         
-        return redirect()->to('product_index');
+        // print_r($status);die;
+        $status = $this->request->getPost('purchase_status');
+            foreach ($product_ids as $key => $product_id) {
+                $qty = isset($quantities[$key]) ? $quantities[$key] : 0;
+                if ($status == 'Released') 
+                    // echo "hello";die;
+                    $this->addBackProductQuantity($ProductModel, $product_id, $qty);
+              
+                $discount = isset($discounts[$key]) ? $discounts[$key] : 0;
+                $discount_type = isset($discount_types[$key]) ? $discount_types[$key] : '';
+                $tax=isset($itemtax[$key]) ? $itemtax[$key] : 0;
+                $rate=isset($unitprice[$key]) ? $unitprice[$key] : 0;
+                $amount = isset($amounts[$key]) ? $amounts[$key] : 0;
+                // $new_qty = $this->calculateUpdatedQuantity($ProductModel, $product_id, $qty);
+                // print_r($discount_type);die;
+                $child_data = [
+                    'purchase_order_id' => $purchase_order_id,
+                    'product_id' => $product_id,
+                    'qty' => $qty,
+                    // 'discount' => $discount,
+                    // 'discount_type' => $discount_type,
+                    'net_price'=>$rate,
+                    'tax'=>$tax,
+                    'amount' => $amount,
+                    'isactive'=>1,
+                ];
+            //   print_r($child_data);die;
+                if (!empty($purchase_order_child_id[$key])) {
+                    
+                //    print_r($purchase_order_child_id[$key]);die;
+                    $PurchaseOrderChildModel->update($purchase_order_child_id[$key], $child_data);
+                    
+                } else {
+                    $PurchaseOrderChildModel->insert($child_data);
+                   
+                }
+               
+                
+            }
+            
+        
+        return redirect()->to('purchase_index');
     }
+    private function addBackProductQuantity($ProductModel, $product_id, $sold_qty)
+{
+    // Fetch current product quantity
+    $product = $ProductModel->find($product_id);
+    $current_qty = $product['qty_stock'];
 
-    public function delete_product($product_id)
+    // Calculate updated quantity
+    $new_qty = $current_qty + $sold_qty;
+
+    // Update quantity in Product table
+    $ProductModel->update($product_id, ['qty_stock' => $new_qty]);
+}
+
+    public function delete_purchase($purchase_order_id)
     {
-        $model = new ProductModel();
-        $where = ['product_id' => $product_id, 'isactive' => 1 , 'branch_id' => $this->session->get('logged_user_branch_id')]; // Assuming 'isactive' is a column in your database
-        $existingProduct = $model->where($where)->first();
+        $PurchaseOrderModel = new PurchaseOrderModel();
+        $where = ['purchase_order_id' => $purchase_order_id, 'isactive' => 1 ]; // Assuming 'isactive' is a column in your database
+        $existingPurchase = $PurchaseOrderModel->where($where)->first();
     
-        if ($existingProduct) {
+        if ($existingPurchase) {
             $data['isactive'] = 0;
             
-            if ($model->update($product_id, $data)) {
+            if ($PurchaseOrderModel->update($purchase_order_id, $data)) {
                 session()->setFlashdata('success', 'Deleted successfully.');
-                $this->logger->info("Users: has been Inactivated successfully. Inactivated ID = ".$product_id); 
+                $this->logger->info("Purchase Order: has been Inactivated successfully. Inactivated ID = ".$purchase_order_id); 
             }
         }
     
        
-        return redirect()->to('product_index');
+        return redirect()->to('purchase_index');
     }
   
-    // Retrieve the make_id from the AJAX request
-    public function get_vendors_by_model()
-    {
-        
-        $model_ids = $this->request->getPost('model_ids');
-        $make_id = $this->request->getPost('make_id');
-        $vendorModel = new VendorModel();
-        $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([]);
-        }
-    }
+ 
 
 
-    public function get_product($product_id)
+    public function get_vendor($vendor_id)
     {
 
-        $ProductModel = new ProductModel();
-        $BranchModel = new BranchModel();
-        $BusinessModel = new BusinessModel();
+        $VendorModel = new VendorModel();
 
-        $branch = $BranchModel->where('branch_id', $this->session->get('logged_user_branch_id'))->get()->getRowArray();
-        $business = $BusinessModel->where('business_id', $this->session->get('logged_user_business_id'))->get()->getRowArray();
+        $products =$VendorModel->where('vendor_id',$vendor_id)->select('vendor_name,contact_person_name1,contact_person_mobile1')->get()->getRowArray(); 
 
 
-        $products=$ProductModel->where('product_id', $product_id)->get()->getRowArray();
-        if ($products['prefered_vendor']) {
-            $VendorModel = new VendorModel();
-            $products +=$VendorModel->where('vendor_id',$products['prefered_vendor'])->select('vendor_name,contact_person_name1,contact_person_mobile1')->get()->getRowArray(); 
-        }
-
-        $products['branch']=$branch;
-        $products['business']=$business;
-
         return json_encode($products);
 
     }
+    public function getVendorProducts()
+{
+    // Retrieve make_id and model_id from the request
+    // $makeId = $this->request->getPost('');
+    $vendorId = $this->request->getPost('vendor_id');
+
+    // Encode the modelId before searching
+    $encodedVendorId = json_encode([$vendorId]);
+
+    // Load the ProductModel
+    $productModel = new ProductModel();
+
+    // Query the database to find the product based on make_id and model_id
+    $product = $productModel
+                            ->where('JSON_CONTAINS(vendor, \'' . $encodedVendorId . '\')', null, false)
+                            ->findAll();
+
+    // print_r($product);die;
+    if($product){
+        // Send JSON response
+        return $this->response->setJSON(['product'=>$product] );
+    } else {
+        // If product is not found, return an empty response or appropriate message
+        return $this->response->setJSON(['product'=>[] , 'modelName'=>$modelName ,'makeName'=>$makeName]);
+    }
+}
+public function delete_purchase_product()
+{
+    $purchase_order_child_id = $this->request->getPost('purchase_order_child_id');
+    
+    if (!empty($purchase_order_child_id)) {
+        $PurchaseOrderChildModel = new PurchaseOrderChildModel();
+        $data['isactive'] = 0;
+        
+        if ($PurchaseOrderChildModel->update($purchase_order_child_id, $data)) {
+            // Return success response
+            return $this->response->setJSON(['success' => true]);
+        }
+    }
+
+    // Return error response if deletion fails
+    return $this->response->setJSON(['success' => false]);
+}
 
 }
\ No newline at end of file
diff --git a/app/Controllers/Reorderlevel.php b/app/Controllers/Reorderlevel.php
new file mode 100644
index 0000000..4a67041
--- /dev/null
+++ b/app/Controllers/Reorderlevel.php
@@ -0,0 +1,32 @@
+session = session();
+		
+    }
+ 
+    public function reorder_level_index()
+    {
+        
+        $productModel = new ProductModel();
+        $reorder = $productModel->where('qty_stock <= purchase_order_level')->findAll();
+
+        // print_r($reorder);die;
+        $data['reorder']=$reorder;
+       
+        return view('reorder_level_list',$data);
+    }
+}
+?>
\ No newline at end of file
diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php
index ad5f4a5..1125603 100644
--- a/app/Controllers/Sales.php
+++ b/app/Controllers/Sales.php
@@ -69,12 +69,13 @@ class Sales extends BaseController
 $SalesOrderModel = new SalesOrderModel(); 
 $sales=$SalesOrderModel->where('sales_order_id', $sales_order_id)->get()->getRowArray();
 $data['sales']=$sales;
+
 $VehicleModel = new VehicleModel();
 $vehicles = $VehicleModel->where('isactive',1)->where('vehicle_id',$sales['vehicle_id'])
                         ->where('branch_id',$this->session->get('logged_user_branch_id'))
                         ->first();
 $makeId = $vehicles['make'];
-    $modelId = $vehicle['model'];
+    $modelId = $vehicles['model'];
 
     // Encode the modelId before searching
     $encodedModelId = json_encode([$modelId]);
@@ -143,7 +144,7 @@ $VehicleModel = new VehicleModel();
         $SalesOrderModel->insert($data);
         $sales_order_id = $SalesOrderModel->getInsertID(); // Get the last inserted ID
     }
-    $orderNumber = 'SO-' . date('md') . '-' . str_pad($sales_order_id, 5, '0', STR_PAD_LEFT);
+    $orderNumber = 'SO' .  str_pad($sales_order_id, 8, '0', STR_PAD_LEFT);
 // print_r($orderNumber);die;
     // Update sales order with generated order number
     $SalesOrderModel->update($sales_order_id, ['order_number' => $orderNumber]);
@@ -163,10 +164,14 @@ $VehicleModel = new VehicleModel();
 $status = $this->request->getPost('status');
     foreach ($product_ids as $key => $product_id) {
         $qty = isset($quantities[$key]) ? $quantities[$key] : 0;
-        if ($status == 'Paid') {
-            // echo"hello";die;
-            // Calculate updated quantity and update Product table
+        if ($status == 'Created') {
+            // echo "hello";die;
             $this->updateProductQuantity($ProductModel, $product_id, $qty);
+        }elseif ($status == 'Cancelled'){
+            // echo "cancel";die;
+
+            // echo"hello";die;
+            $this->addBackProductQuantity($ProductModel, $product_id, $qty);
         }
        
         $discount = isset($discounts[$key]) ? $discounts[$key] : 0;
@@ -190,7 +195,9 @@ $status = $this->request->getPost('status');
     //    print_r($product_data);die;
         if (!empty($sales_product_id[$key])) {
             
+           
             $SalesOrderProductModel->update($sales_product_id[$key], $product_data);
+            
         } else {
             $SalesOrderProductModel->insert($product_data);
            
@@ -198,11 +205,23 @@ $status = $this->request->getPost('status');
        
         
     }
-
+    
 
     return redirect()->to('sales_order_index');
 }
 
+private function addBackProductQuantity($ProductModel, $product_id, $sold_qty)
+{
+    // Fetch current product quantity
+    $product = $ProductModel->find($product_id);
+    $current_qty = $product['qty_stock'];
+
+    // Calculate updated quantity
+    $new_qty = $current_qty + $sold_qty;
+
+    // Update quantity in Product table
+    $ProductModel->update($product_id, ['qty_stock' => $new_qty]);
+}
 private function updateProductQuantity($ProductModel, $product_id, $sold_qty)
 {
     // Fetch current product quantity
@@ -215,6 +234,7 @@ private function updateProductQuantity($ProductModel, $product_id, $sold_qty)
     // Update quantity in Product table
     $ProductModel->update($product_id, ['qty_stock' => $new_qty]);
 }
+
 public function delete_sales_product()
 {
     $sales_order_product_id = $this->request->getPost('sales_order_product_id');
diff --git a/app/Models/PurchaseOrderChildModel.php b/app/Models/PurchaseOrderChildModel.php
new file mode 100644
index 0000000..b81006f
--- /dev/null
+++ b/app/Models/PurchaseOrderChildModel.php
@@ -0,0 +1,11 @@
+select('purchase_order.*, COUNT(purchase_order_child.purchase_order_id) AS product_count,vendor.vendor_name');
+        
+        // Join the sales_order_product table based on sales_order_id
+        $this->join('purchase_order_child', 'purchase_order_child.purchase_order_id = purchase_order.purchase_order_id');
+        $this ->join('vendor', 'vendor.vendor_id = purchase_order.vendor_id');
+        // Group by sales_order_id to get count of products per sales order
+        $this->groupBy('purchase_order.purchase_order_id');
     
+        // Add condition to fetch only active sales orders
+        $this->where('purchase_order_child.isactive', 1);
+        $this ->where('purchase_order.branch_id',$logged_user_branch_id);
+        $this->orderBy('purchase_order.purchase_order_id','DESC');
+        // Get the results
+        return $this->findAll();
+    } 
 }
\ No newline at end of file
diff --git a/app/Views/layout/header.php b/app/Views/layout/header.php
index 75b82cc..e3f0aba 100644
--- a/app/Views/layout/header.php
+++ b/app/Views/layout/header.php
@@ -174,6 +174,13 @@
                                      Manufacturer 
                                 
                             
+                            
  • + + + Reorder Level Products + + + get('logged_user_role') == 'Floor Manager' || diff --git a/app/Views/product_form.php b/app/Views/product_form.php index d1ecae4..2d56f70 100644 --- a/app/Views/product_form.php +++ b/app/Views/product_form.php @@ -39,7 +39,7 @@
    - +
    -
    +
    + + +
    +
    + + +
    + +
    + + + + + + + +