From 9cfddaf2ab055e852720da0dd99f5a092083af3a Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Thu, 21 Apr 2016 20:28:36 +0100 Subject: [PATCH] undelete an item in case it's returned (#184) --- application/models/Item.php | 18 +++++++-- application/models/Sale.php | 80 +++++++++++++++++++------------------ 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/application/models/Item.php b/application/models/Item.php index c231a62a2..4bc026ad0 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -218,16 +218,16 @@ class Item extends CI_Model */ public function save(&$item_data, $item_id=false) { - if (!$item_id or !$this->exists($item_id)) + if(!$item_id or !$this->exists($item_id)) { - if($this->db->insert('items',$item_data)) + if($this->db->insert('items', $item_data)) { - $item_data['item_id']=$this->db->insert_id(); + $item_data['item_id'] = $this->db->insert_id(); return true; } return false; } - + $this->db->where('item_id', $item_id); return $this->db->update('items', $item_data); @@ -252,6 +252,16 @@ class Item extends CI_Model return $this->db->update('items', array('deleted' => 1)); } + + /* + Undeletes one item + */ + public function undelete($item_id) + { + $this->db->where('item_id', $item_id); + + return $this->db->update('items', array('deleted' => 0)); + } /* Deletes a list of items diff --git a/application/models/Sale.php b/application/models/Sale.php index f4a73ed98..183449c4b 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -282,17 +282,17 @@ class Sale extends CI_Model } $sales_data = array( - 'sale_time' => date('Y-m-d H:i:s'), - 'customer_id'=> $this->Customer->exists($customer_id) ? $customer_id : null, - 'employee_id'=>$employee_id, - 'comment'=>$comment, - 'invoice_number'=>$invoice_number + 'sale_time' => date('Y-m-d H:i:s'), + 'customer_id' => $this->Customer->exists($customer_id) ? $customer_id : null, + 'employee_id' => $employee_id, + 'comment' => $comment, + 'invoice_number'=> $invoice_number ); // Run these queries as a transaction, we want to make sure we do all or nothing $this->db->trans_start(); - $this->db->insert('sales',$sales_data); + $this->db->insert('sales', $sales_data); $sale_id = $this->db->insert_id(); foreach($payments as $payment_id=>$payment) @@ -306,9 +306,9 @@ class Sale extends CI_Model } $sales_payments_data = array( - 'sale_id'=>$sale_id, - 'payment_type'=>$payment['payment_type'], - 'payment_amount'=>$payment['payment_amount'] + 'sale_id' => $sale_id, + 'payment_type' => $payment['payment_type'], + 'payment_amount'=> $payment['payment_amount'] ); $this->db->insert('sales_payments',$sales_payments_data); } @@ -318,37 +318,41 @@ class Sale extends CI_Model $cur_item_info = $this->Item->get_info($item['item_id']); $sales_items_data = array( - 'sale_id'=>$sale_id, - 'item_id'=>$item['item_id'], - 'line'=>$item['line'], - 'description'=>character_limiter($item['description'], 30), - 'serialnumber'=>character_limiter($item['serialnumber'], 30), - 'quantity_purchased'=>$item['quantity'], - 'discount_percent'=>$item['discount'], - 'item_cost_price' => $cur_item_info->cost_price, - 'item_unit_price'=>$item['price'], - 'item_location'=>$item['item_location'] + 'sale_id' => $sale_id, + 'item_id' => $item['item_id'], + 'line' => $item['line'], + 'description' => character_limiter($item['description'], 30), + 'serialnumber' => character_limiter($item['serialnumber'], 30), + 'quantity_purchased'=> $item['quantity'], + 'discount_percent' => $item['discount'], + 'item_cost_price' => $cur_item_info->cost_price, + 'item_unit_price' => $item['price'], + 'item_location' => $item['item_location'] ); $this->db->insert('sales_items',$sales_items_data); - //Update stock quantity + // Update stock quantity $item_quantity = $this->Item_quantity->get_item_quantity($item['item_id'], $item['item_location']); - $this->Item_quantity->save(array('quantity'=>$item_quantity->quantity - $item['quantity'], - 'item_id'=>$item['item_id'], - 'location_id'=>$item['item_location']), $item['item_id'], $item['item_location']); + $this->Item_quantity->save(array('quantity' => $item_quantity->quantity - $item['quantity'], + 'item_id' => $item['item_id'], + 'location_id' => $item['item_location']), $item['item_id'], $item['item_location']); - //Inventory Count Details - $qty_buy = -$item['quantity']; + // if an items was deleted but later returned it's restored with this rule + if($item['quantity'] < 0) + { + $this->Item->undelete($item['item_id']); + } + + // Inventory Count Details $sale_remarks ='POS '.$sale_id; - $inv_data = array - ( - 'trans_date'=>date('Y-m-d H:i:s'), - 'trans_items'=>$item['item_id'], - 'trans_user'=>$employee_id, - 'trans_location'=>$item['item_location'], - 'trans_comment'=>$sale_remarks, - 'trans_inventory'=>$qty_buy + $inv_data = array( + 'trans_date' => date('Y-m-d H:i:s'), + 'trans_items' => $item['item_id'], + 'trans_user' => $employee_id, + 'trans_location' => $item['item_location'], + 'trans_comment' => $sale_remarks, + 'trans_inventory' => -$item['quantity'] ); $this->Inventory->insert($inv_data); @@ -358,11 +362,11 @@ class Sale extends CI_Model foreach($this->Item_taxes->get_info($item['item_id']) as $row) { $this->db->insert('sales_items_taxes', array( - 'sale_id' =>$sale_id, - 'item_id' =>$item['item_id'], - 'line' =>$item['line'], - 'name' =>$row['name'], - 'percent' =>$row['percent'] + 'sale_id' => $sale_id, + 'item_id' => $item['item_id'], + 'line' => $item['line'], + 'name' => $row['name'], + 'percent' => $row['percent'] )); } }