1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83:
<?php
class Item_quantity extends CI_Model
{
public function exists($item_id, $location_id)
{
$this->db->from('item_quantities');
$this->db->where('item_id', $item_id);
$this->db->where('location_id', $location_id);
return ($this->db->get()->num_rows() == 1);
}
public function save($location_detail, $item_id, $location_id)
{
if(!$this->exists($item_id, $location_id))
{
return $this->db->insert('item_quantities', $location_detail);
}
$this->db->where('item_id', $item_id);
$this->db->where('location_id', $location_id);
return $this->db->update('item_quantities', $location_detail);
}
public function get_item_quantity($item_id, $location_id)
{
$this->db->from('item_quantities');
$this->db->where('item_id', $item_id);
$this->db->where('location_id', $location_id);
$result = $this->db->get()->row();
if(empty($result) == TRUE)
{
$result = new stdClass();
foreach($this->db->list_fields('item_quantities') as $field)
{
$result->$field = '';
}
$result->quantity = 0;
}
return $result;
}
public function change_quantity($item_id, $location_id, $quantity_change)
{
$quantity_old = $this->get_item_quantity($item_id, $location_id);
$quantity_new = $quantity_old->quantity + intval($quantity_change);
$location_detail = array('item_id' => $item_id, 'location_id' => $location_id, 'quantity' => $quantity_new);
return $this->save($location_detail, $item_id, $location_id);
}
public function reset_quantity($item_id)
{
$this->db->where('item_id', $item_id);
return $this->db->update('item_quantities', array('quantity' => 0));
}
public function reset_quantity_list($item_ids)
{
$this->db->where_in('item_id', $item_ids);
return $this->db->update('item_quantities', array('quantity' => 0));
}
}
?>