| 1 | <?php |
| 2 | //TODO: Merge SELL and DISPOSE to single file with parameter! |
| 3 | //TODO: Highlight fields that should be filled (and maybe even check them when submited) |
| 4 | //TODO: Add support for selling/disposing multiple items at once... |
| 5 | //TODO: Reuse /item/$item_id/edit |
| 6 | //TODO: Stop using map_unique()!!! |
| 7 | //TODO: we can use empty selectbox[] if no selectboxes are shown |
| 8 | switch($SUBPATH[0]) { |
| 9 | default: case 1: |
| 10 | echo $this->html->form("$URL/2", 'GET', array( |
| 11 | array('serial','','text',false,'autofocus','item_serial:'), |
| 12 | array('quantity','1','text',false,false,'quantity:'), |
| 13 | array(false,'DISPOSE','submit') |
| 14 | )); |
| 15 | break; |
| 16 | case 2: |
| 17 | $item_serial = $_GET['serial']; |
| 18 | $item_id = $this->db->map_unique('item_serial', $item_serial, 'item_id', 'item'); |
| 19 | |
| 20 | $current = $this->db->get_listing('item', $item_id, 1); |
| 21 | $forked_item = $current; |
| 22 | |
| 23 | $disable_cols = array('status_id','item_price_in','item_serial','item_quantity','model_id','vendor_id','room_id','item_customer'); |
| 24 | |
| 25 | $model_id = $this->db->map_unique('item_serial', $item_serial, 'model_id', 'item'); |
| 26 | $model_price_in = $this->db->map_unique('model_id', $model_id, 'model_price_in', 'model'); |
| 27 | $model_price_out = $this->db->map_unique('model_id', $model_id, 'model_price_out', 'model'); |
| 28 | |
| 29 | $model_countable = $this->db->map_unique('model_id', $model_id, 'model_countable', 'model'); |
| 30 | if($model_countable) { |
| 31 | $current[$item_id]['status_id'] = 2; |
| 32 | $current[$item_id]['item_customer'] = 0; |
| 33 | $item_quantity = 1; |
| 34 | $current[$item_id]['item_price_out'] = $model_price_out; |
| 35 | } else { |
| 36 | $disable_cols = array_merge($disable_cols,array('item_price_out','item_note')); |
| 37 | $quantity_removed = $_GET['quantity']; |
| 38 | if($quantity_removed <= 0) $this->post_redirect_get("$URL_INTERNAL/1","Can't dispose non-possitive amount of items!"); |
| 39 | if(!is_numeric($quantity_removed)) $quantity_removed = 1; |
| 40 | $quantity_stored = $this->db->map_unique('item_serial', $item_serial, 'item_quantity', 'item', false); |
| 41 | if(!is_numeric($quantity_stored)) $quantity_stored = 0; |
| 42 | $item_quantity = $quantity_stored - $quantity_removed; |
| 43 | |
| 44 | |
| 45 | echo("Stock: ".$quantity_stored."<br />Disposing/Selling: ".$quantity_removed."<br />Keeping: ".$item_quantity); |
| 46 | |
| 47 | $current[$item_id]['item_quantity'] = $item_quantity; |
| 48 | $current[$item_id]['item_price_in'] = $item_quantity * $model_price_in; |
| 49 | $current[$item_id]['item_price_out'] = $item_quantity * $model_price_out; |
| 50 | |
| 51 | $forked_item[$item_id]['item_id'] = ''; |
| 52 | $forked_item[$item_id]['item_serial'] .= '@'.time(); |
| 53 | $forked_item[$item_id]['status_id'] = '2'; |
| 54 | $forked_item[$item_id]['item_quantity'] = $quantity_removed; |
| 55 | $forked_item[$item_id]['item_price_in'] = $quantity_removed * $model_price_in; |
| 56 | $forked_item[$item_id]['item_price_out'] = $quantity_removed * $model_price_out; |
| 57 | $forked_item[$item_id]['item_customer'] = 0; |
| 58 | |
| 59 | $forked_disable_cols = array('model_id','vendor_id','item_serial','item_quantity','room_id','status_id','item_price_in','item_price_out','item_customer'); |
| 60 | } |
| 61 | |
| 62 | $columns = $this->db->get_columns('item'); |
| 63 | $selectbox = $this->db->columns_get_selectbox($columns, 'item'); |
| 64 | |
| 65 | $action = $_SERVER['SCRIPT_NAME']."/item/$item_id/edit"; |
| 66 | //echo $this->html->render_insert_form('item', $columns, $selectbox, $current, $disable_cols, $action); |
| 67 | |
| 68 | $insert_form[]=array('item', $columns, $selectbox, $current, $disable_cols, $action, false); |
| 69 | if(!$model_countable) $insert_form[]=array('item', $columns, $selectbox, $forked_item, $forked_disable_cols, $action, false); |
| 70 | echo $this->html->render_insert_form_multi($insert_form); |
| 71 | break; |
| 72 | } |