From 7e3b2bba47e934890b61307c647914818b59f113 Mon Sep 17 00:00:00 2001 From: heama Date: Wed, 10 Apr 2024 17:03:08 +0530 Subject: [PATCH] Sales --- app/Controllers/Sales.php | 29 ++++++++++++++++++++++++----- app/Views/sales_order_form.php | 27 +++++++++++++++++++-------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php index ad5f4a5..a746dc4 100644 --- a/app/Controllers/Sales.php +++ b/app/Controllers/Sales.php @@ -74,7 +74,7 @@ $vehicles = $VehicleModel->where('isactive',1)->where('vehicle_id',$sales['vehic ->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]); @@ -163,10 +163,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 +194,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 +204,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 +233,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/Views/sales_order_form.php b/app/Views/sales_order_form.php index b0e3e89..6a238cd 100644 --- a/app/Views/sales_order_form.php +++ b/app/Views/sales_order_form.php @@ -28,7 +28,7 @@ ' ?> - > Select a vehicle' ?> @@ -55,13 +55,24 @@
- - -
+ + + + +