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: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284:
<?php
class Receiving extends CI_Model
{
public function get_info($receiving_id)
{
$this->db->from('receivings');
$this->db->join('people', 'people.person_id = receivings.supplier_id', 'LEFT');
$this->db->join('suppliers', 'suppliers.person_id = receivings.supplier_id', 'LEFT');
$this->db->where('receiving_id', $receiving_id);
return $this->db->get();
}
public function get_receiving_by_reference($reference)
{
$this->db->from('receivings');
$this->db->where('reference', $reference);
return $this->db->get();
}
public function is_valid_receipt($receipt_receiving_id)
{
if(!empty($receipt_receiving_id))
{
$pieces = explode(' ', $receipt_receiving_id);
if(count($pieces) == 2 && preg_match('/(RECV|KIT)/', $pieces[0]))
{
return $this->exists($pieces[1]);
}
else
{
return $this->get_receiving_by_reference($receipt_receiving_id)->num_rows() > 0;
}
}
return FALSE;
}
public function exists($receiving_id)
{
$this->db->from('receivings');
$this->db->where('receiving_id', $receiving_id);
return ($this->db->get()->num_rows() == 1);
}
public function update($receiving_data, $receiving_id)
{
$this->db->where('receiving_id', $receiving_id);
return $this->db->update('receivings', $receiving_data);
}
public function save($items, $supplier_id, $employee_id, $comment, $reference, $payment_type, $receiving_id = FALSE)
{
if(count($items) == 0)
{
return -1;
}
$receivings_data = array(
'receiving_time' => date('Y-m-d H:i:s'),
'supplier_id' => $this->Supplier->exists($supplier_id) ? $supplier_id : NULL,
'employee_id' => $employee_id,
'payment_type' => $payment_type,
'comment' => $comment,
'reference' => $reference
);
$this->db->trans_start();
$this->db->insert('receivings', $receivings_data);
$receiving_id = $this->db->insert_id();
foreach($items as $line=>$item)
{
$cur_item_info = $this->Item->get_info($item['item_id']);
$receivings_items_data = array(
'receiving_id' => $receiving_id,
'item_id' => $item['item_id'],
'line' => $item['line'],
'description' => $item['description'],
'serialnumber' => $item['serialnumber'],
'quantity_purchased' => $item['quantity'],
'receiving_quantity' => $item['receiving_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('receivings_items', $receivings_items_data);
$items_received = $item['receiving_quantity'] != 0 ? $item['quantity'] * $item['receiving_quantity'] : $item['quantity'];
if($cur_item_info->cost_price != $item['price'] && $this->config->item('receiving_calculate_average_price') != FALSE)
{
$this->Item->change_cost_price($item['item_id'], $items_received, $item['price'], $cur_item_info->cost_price);
}
$item_quantity = $this->Item_quantity->get_item_quantity($item['item_id'], $item['item_location']);
$this->Item_quantity->save(array('quantity' => $item_quantity->quantity + $items_received, 'item_id' => $item['item_id'],
'location_id' => $item['item_location']), $item['item_id'], $item['item_location']);
$recv_remarks = 'RECV ' . $receiving_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' => $recv_remarks,
'trans_inventory' => $items_received
);
$this->Inventory->insert($inv_data);
$supplier = $this->Supplier->get_info($supplier_id);
}
$this->db->trans_complete();
if($this->db->trans_status() === FALSE)
{
return -1;
}
return $receiving_id;
}
public function delete_list($receiving_ids, $employee_id, $update_inventory = TRUE)
{
$success = TRUE;
$this->db->trans_start();
foreach($receiving_ids as $receiving_id)
{
$success &= $this->delete($receiving_id, $employee_id, $update_inventory);
}
$this->db->trans_complete();
$success &= $this->db->trans_status();
return $success;
}
public function delete($receiving_id, $employee_id, $update_inventory = TRUE)
{
$this->db->trans_start();
if($update_inventory)
{
$items = $this->get_receiving_items($receiving_id)->result_array();
foreach($items as $item)
{
$inv_data = array(
'trans_date' => date('Y-m-d H:i:s'),
'trans_items' => $item['item_id'],
'trans_user' => $employee_id,
'trans_comment' => 'Deleting receiving ' . $receiving_id,
'trans_location' => $item['item_location'],
'trans_inventory' => $item['quantity_purchased'] * -1
);
$this->Inventory->insert($inv_data);
$this->Item_quantity->change_quantity($item['item_id'], $item['item_location'], $item['quantity_purchased'] * -1);
}
}
$this->db->delete('receivings_items', array('receiving_id' => $receiving_id));
$this->db->delete('receivings', array('receiving_id' => $receiving_id));
$this->db->trans_complete();
return $this->db->trans_status();
}
public function get_receiving_items($receiving_id)
{
$this->db->from('receivings_items');
$this->db->where('receiving_id', $receiving_id);
return $this->db->get();
}
public function get_supplier($receiving_id)
{
$this->db->from('receivings');
$this->db->where('receiving_id', $receiving_id);
return $this->Supplier->get_info($this->db->get()->row()->supplier_id);
}
public function get_payment_options()
{
return array(
$this->lang->line('sales_cash') => $this->lang->line('sales_cash'),
$this->lang->line('sales_check') => $this->lang->line('sales_check'),
$this->lang->line('sales_debit') => $this->lang->line('sales_debit'),
$this->lang->line('sales_credit') => $this->lang->line('sales_credit')
);
}
public function create_temp_table(array $inputs)
{
if(empty($inputs['receiving_id']))
{
if(empty($this->config->item('date_or_time_format')))
{
$where = 'WHERE DATE(receiving_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']);
}
else
{
$where = 'WHERE receiving_time BETWEEN ' . $this->db->escape(rawurldecode($inputs['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($inputs['end_date']));
}
}
else
{
$where = 'WHERE receivings_items.receiving_id = ' . $this->db->escape($inputs['receiving_id']);
}
$this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('receivings_items_temp') .
' (INDEX(receiving_date), INDEX(receiving_time), INDEX(receiving_id))
(
SELECT
MAX(DATE(receiving_time)) AS receiving_date,
MAX(receiving_time) AS receiving_time,
receivings_items.receiving_id,
MAX(comment) AS comment,
MAX(item_location) AS item_location,
MAX(reference) AS reference,
MAX(payment_type) AS payment_type,
MAX(employee_id) AS employee_id,
items.item_id,
MAX(receivings.supplier_id) AS supplier_id,
MAX(quantity_purchased) AS quantity_purchased,
MAX(receivings_items.receiving_quantity) AS receiving_quantity,
MAX(item_cost_price) AS item_cost_price,
MAX(item_unit_price) AS item_unit_price,
MAX(discount_percent) AS discount_percent,
receivings_items.line,
MAX(serialnumber) AS serialnumber,
MAX(receivings_items.description) AS description,
MAX(item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100) AS subtotal,
MAX(item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100) AS total,
MAX((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100) - (item_cost_price * quantity_purchased)) AS profit,
MAX(item_cost_price * quantity_purchased) AS cost
FROM ' . $this->db->dbprefix('receivings_items') . ' AS receivings_items
INNER JOIN ' . $this->db->dbprefix('receivings') . ' AS receivings
ON receivings_items.receiving_id = receivings.receiving_id
INNER JOIN ' . $this->db->dbprefix('items') . ' AS items
ON receivings_items.item_id = items.item_id
' . "
$where
" . '
GROUP BY receivings_items.receiving_id, items.item_id, receivings_items.line
)'
);
}
}
?>