From a7bfff8865ad586e5b1f5346ef8bc14869da5a00 Mon Sep 17 00:00:00 2001 From: heama Date: Thu, 11 Apr 2024 11:17:57 +0530 Subject: [PATCH] Purchase-sale-Product-vendor --- app/Config/Routes.php | 2 + app/Controllers/Purchase.php | 165 ++++++- app/Controllers/Sales.php | 3 +- app/Models/PurchaseOrderChildModel.php | 11 + app/Models/PurchaseOrderModel.php | 19 +- app/Views/product_form.php | 2 +- app/Views/purchase_form.php | 647 ++++++++++++++++++++++--- app/Views/purchase_list.php | 20 +- app/Views/vendor_form.php | 6 +- 9 files changed, 775 insertions(+), 100 deletions(-) create mode 100644 app/Models/PurchaseOrderChildModel.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 9e6c45b..b5a2ba1 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -123,5 +123,7 @@ $routes->get("new_purchase/(:any)", "Purchase::new_purchase/$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/Purchase.php b/app/Controllers/Purchase.php index 4086a38..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,9 +22,7 @@ class Purchase extends BaseController { $PurchaseOrderModel = new PurchaseOrderModel(); - $purchase = $PurchaseOrderModel->select('purchase_order.*, vendor.vendor_name') - ->join('vendor', 'vendor.vendor_id = purchase_order.vendor_id') - ->findAll(); + $purchase = $PurchaseOrderModel->getPurchaseOrdersWithProducts($this->session->get('logged_user_branch_id')); // echo "
";
         // print_r($product);die;
         $data['purchase']=$purchase;
@@ -48,7 +47,11 @@ class Purchase extends BaseController
             $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
+            ->where('isactive',1)
+            ->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
+            $data['product']=$product;   
             $data['branch'] = $branch;  
             $data['business'] = $business;  
 
@@ -61,12 +64,31 @@ class Purchase extends BaseController
 
             $PurchaseOrderModel = new PurchaseOrderModel();
             $purchase=$PurchaseOrderModel->where('purchase_order_id', $purchase_order_id)->get()->getRowArray();
-           
+            $data['purchase'] = $purchase;
+               
+            
+            
             $VendorModel = new VendorModel();
             $vendor = $VendorModel->select('vendor_id,vendor_name')->findAll();
-            $data['vendor']=$vendor;
-            $data['purchase'] = $purchase;
+            $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;
 
         }
         // echo "
";
@@ -77,6 +99,8 @@ class Purchase extends BaseController
     public function add_purchase()
     {
         $PurchaseOrderModel = new PurchaseOrderModel();
+        $ProductModel = new ProductModel();
+        $PurchaseOrderChildModel = new PurchaseOrderChildModel(); 
         $data = [
             'vendor_id' => $this->request->getPost('vendor_id'),
             'order_date' => $this->request->getPost('order_date'),
@@ -91,23 +115,97 @@ class Purchase extends BaseController
             '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'),
         ];
-        // echo "
";
-        // print_r($this->request->getPost());die;
+
+        // print_r($data);
         $purchase_order_id = $this->request->getPost('purchase_order_id');
-    
+        //  echo  $purchase_order_id;die;
         if (!empty($purchase_order_id)) {
         
             $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;
+        
+        // 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'];
+
+    // 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)
     {
@@ -142,5 +240,48 @@ class Purchase extends BaseController
         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/Sales.php b/app/Controllers/Sales.php
index a746dc4..1125603 100644
--- a/app/Controllers/Sales.php
+++ b/app/Controllers/Sales.php
@@ -69,6 +69,7 @@ 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'))
@@ -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]);
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/product_form.php b/app/Views/product_form.php
index 1db817f..2d56f70 100644
--- a/app/Views/product_form.php
+++ b/app/Views/product_form.php
@@ -78,7 +78,7 @@
                             
                             
                         
-                        
+
+

Purchase Details :


-
-
- - -
+ +
@@ -47,17 +45,21 @@
- +
- +
+ + +

Billing Addresss :


@@ -98,104 +100,595 @@

-

Description Details :


+ +
+
+

Item Details

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Item DetailsQtyRateTaxAmount
+ + + + + + + + + + +
+
+ + + +
+
+
+
+
+

Calculation

+
+ + +
+
+ + + +
+ + + + + +
+ + +
+ + +
+
+
+

Terms & Condition :


- - + + + + +
- + + + +
+ + + + + + + + + + + -$(document).ready(function() { - // $('#vendor_id').change(function() { - // var vendor_id = $(this).val(); - - // $.ajax({ - // url: '' + vendor_id, - // type: 'GET', - // dataType: 'json', - // success: function(response) { - // if (response) { - - // $('#contact_person_name').val(response.contact_person_name1); - // $('#contact_person_mobile').val(response.contact_person_mobile1); - // $('#total_price').val(response.unit_price * response.reorder_level); - - - // var currentDate = new Date().toISOString().split('T')[0]; // Get current date in yyyy-mm-dd format - // $('#order_date').val(currentDate); - - - // $('#shipping_addresss').val(response.branch.address); - // $('#shipping_state').val(response.branch.state); - // $('#shipping_city').val(response.branch.city); - // $('#shipping_postal_code').val(response.branch.postal_code); - - - // $('#billing_addresss').val(response.business.address); - // $('#billing_state').val(response.business.state); - // $('#billing_city').val(response.business.city); - // $('#billing_postal_code').val(response.business.postal_code); - // console.log(response.branch); - // } - - // }, - // error: function(xhr, status, error) { - // // Handle error - // console.error(xhr.responseText); - // } - // }); - // }); -}); - diff --git a/app/Views/purchase_list.php b/app/Views/purchase_list.php index 4ca0f97..7476aab 100644 --- a/app/Views/purchase_list.php +++ b/app/Views/purchase_list.php @@ -28,10 +28,15 @@ + Order Number Vendor Name Order Date Contact Person Name - Contact Person Mobile + Contact Person Mobile + Quantity + Subtotal + Tax + Total Status Actions @@ -41,11 +46,16 @@ + - - % - % - % + + + + + + + + diff --git a/app/Views/vendor_form.php b/app/Views/vendor_form.php index bd643c5..299fca6 100644 --- a/app/Views/vendor_form.php +++ b/app/Views/vendor_form.php @@ -39,12 +39,12 @@ -
+
-
+
-
+