<?php
+//TODO: Merge SELL and DISPOSE to single file with parameter!
+//TODO: Highlight fields that should be filled (and maybe even check them when submited)
+//TODO: Add support for selling/disposing multiple items at once...
+//TODO: Reuse /item/$item_id/edit
+//TODO: Stop using map_unique()!!!
switch($SUBPATH[0]) {
default: case 1:
echo $this->html->form("$URL/2", 'GET', array(
array('serial','','text',false,'autofocus','item_serial:'),
+ array('quantity','1','text',false,false,'quantity:'),
array(false,'DISPOSE','submit')
));
break;
case 2:
- $item_id = $this->db->map_unique('item_serial', $_GET['serial'], 'item_id', 'item');
- $columns = $this->db->get_columns('item');
- $selectbox = $this->db->columns_get_selectbox($columns, 'item');
+ $item_serial = $_GET['serial'];
+ $item_id = $this->db->map_unique('item_serial', $item_serial, 'item_id', 'item');
$current = $this->db->get_listing('item', $item_id, 1);
- $current[$item_id]['status_id'] = 2;
$current[$item_id]['item_customer'] = 0;
+ $disable_cols = array('status_id','item_price_in','item_customer','item_serial','item_quantity','model_id','vendor_id','room_id');
+
+ $model_id = $this->db->map_unique('item_serial', $item_serial, 'model_id', 'item');
+ $model_price_in = $this->db->map_unique('model_id', $model_id, 'model_price_in', 'model');
+ $model_price_out = $this->db->map_unique('model_id', $model_id, 'model_price_out', 'model');
+
+ if($this->db->map_unique('model_id', $model_id, 'model_countable', 'model')) {
+ $current[$item_id]['status_id'] = 2;
+ $item_quantity = 1;
+ $current[$item_id]['item_price_out'] = $model_price_out;
+ } else {
+ $disable_cols = array_merge($disable_cols,array('item_price_out'));
+ $quantity_removed = $_GET['quantity'];
+ if($quantity_removed <= 0) $this->post_redirect_get("$URL_INTERNAL/1","Can't dispose non-possitive amount of items!");
+ if(!is_numeric($quantity_removed)) $quantity_removed = 1;
+ $quantity_stored = $this->db->map_unique('item_serial', $item_serial, 'item_quantity', 'item', false);
+ if(!is_numeric($quantity_stored)) $quantity_stored = 0;
+ echo("Quantity stored: ".$quantity_stored);
+
+ $item_quantity = $quantity_stored - $quantity_removed;
+ $current[$item_id]['item_quantity'] = $item_quantity;
+
+ $current[$item_id]['item_price_in'] = $item_quantity * $model_price_in;
+ $current[$item_id]['item_price_out'] = $item_quantity * $model_price_out;
+ }
+
+ $columns = $this->db->get_columns('item');
+ $selectbox = $this->db->columns_get_selectbox($columns, 'item');
+
$action = $_SERVER['SCRIPT_NAME']."/item/$item_id/edit";
- echo $this->html->render_insert_form('item', $columns, $selectbox, $current, false, $action);
+ //echo $this->html->render_insert_form('item', $columns, $selectbox, $current, $disable_cols, $action);
+ echo $this->html->render_insert_form_multi(array(
+ //array('item', $columns, $selectbox, $current, $disable_cols, $action),
+ array('item', $columns, $selectbox, $current, $disable_cols, $action)
+ ));
break;
}
<?php
-//TODO: Merge SELL and DISPOSE to single file with parameter!
-//TODO: Highlight fields that should be filled (and maybe even check them when submited)
-//TODO: Add support for selling/disposing multiple items at once...
-//TODO: Reuse /item/$item_id/edit
switch($SUBPATH[0]) {
default: case 1:
echo $this->html->form("$URL/2", 'GET', array(
default: case 1:
echo $this->render_form_add('model');
break;
- case 2:
+ case 2: //TODO: reuse assistants/store for this step
$model_id = trim($_GET['last']);
$columns = $this->db->get_columns('item');
$selectbox = $this->db->columns_get_selectbox($columns, 'item');
default: case 1:
echo $this->html->form("$URL/2", 'GET', array(
array('barcode','','text',false,'autofocus','model_barcode:'),
+ array('quantity','1','text',false,false,'quantity:'),
array(false,'STORE','submit')
));
break;
case 2:
$model_id = $this->db->map_unique('model_barcode', $_GET['barcode'], 'model_id', 'model');
+
+ $disable_cols = array('status_id','item_price_out','item_customer', 'model_id','item_quantity');
+ if($this->db->map_unique('model_barcode', $_GET['barcode'], 'model_countable', 'model')) {
+ //$disable_cols[] = 'item_quantity';
+ $item_serial = '';
+ $item_quantity = 1;
+ $action = $_SERVER['SCRIPT_NAME'].'/item/new';
+ } else {
+ $quantity_added = $_GET['quantity'];
+ if($quantity_added <= 0) $this->post_redirect_get("$URL_INTERNAL/1","Can't store non-possitive amount of items!");
+ if(!is_numeric($quantity_added)) $quantity_added = 1;
+ $quantity_stored = $this->db->map_unique('item_serial', $_GET['barcode'], 'item_quantity', 'item', false);
+ if(!is_numeric($quantity_stored)) $quantity_stored = 0;
+ echo("Quantity stored: ".$quantity_stored);
+
+ $disable_cols[] = 'item_serial';
+ $item_serial = $_GET['barcode'];
+ $item_quantity = $quantity_stored + $quantity_added;
+ $action = $_SERVER['SCRIPT_NAME'].'/item/0/edit';
+ }
$columns = $this->db->get_columns('item');
- $selectbox = $this->db->columns_get_selectbox($columns, 'item');
+ $selectbox = $this->db->columns_get_selectbox($columns, 'item');
//print_r(array('<pre>', $selectbox));
//foreach($selectbox['model_id'] as $id => $name) if($id != $model_id) unset($selectbox['model_id'][$id]);
$current = array(array(
'model_id' => $model_id,
- 'item_quantity' => 1,
- 'status_id' => 1
+ 'item_serial' => $item_serial,
+ 'item_quantity' => $item_quantity,
+ 'status_id' => 1,
+ 'item_price_in' => $this->db->map_unique('model_barcode', $_GET['barcode'], 'model_price_in', 'model'),
+ 'item_price_out' => $this->db->map_unique('model_barcode', $_GET['barcode'], 'model_price_out', 'model')
));
- $action = $_SERVER['SCRIPT_NAME'].'/item/new';
- echo $this->html->render_insert_form('item', $columns, $selectbox, $current, false, $action);
+ echo $this->html->render_insert_form('item', $columns, $selectbox, $current, $disable_cols, $action);
break;
}
* { font-family: arial; }
td,body { background-color: white; }
table { background-color: orange; border: orange; }
-a { text-decoration:none; color: darkblue; }
+a, a img { text-decoration:none; color: darkblue; border:none; }
li a, a:hover { text-decoration:underline; }
.menu li {
return $this->table($table);
}
- function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true) {
- //echo('<pre>'); print_r($selectbox);
- //echo('<pre>'); print_r($current);
- $update = false;
- if(is_array($current)) {
- $update = true;
- $current = array_shift($current);
- }
-
- if(!is_array($hidecols)) $hidecols = array('item_author', 'item_valid_from', 'item_valid_till'); //TODO Autodetect
-
- $action = $action ? " action='$action'" : false;
- $html="<form$action method='POST'>"; //TODO: use $this->form()
- if($multi_insert) $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">';
- //$html.=$this->input('table', $class, 'hidden');
+ function render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update) {
+ $html = '';
foreach($columns as $column) {
$html.=T($class).':<b>'.T($column['Field']).'</b>: ';
$name="values[$class][".$column['Field'].'][]';
}
$html.='<br />';
}
+ return $html;
+ }
- if($multi_insert) { //TODO, move to separate JS file
- $html.=<<<EOF
- </div></span>
- <br style="clear:both" />
- <script>
- function duplicate_element(what, where) {
- var node = document.getElementsByName(what)[0];
- node.parentNode.appendChild(node.cloneNode(true));
- }
- </script>
- <a href='#' onClick="duplicate_element('input_set')">+</a>
-EOF;
+ function render_insert_form_multi($array) {
+ $html = '';
+ $head=false;
+
+ foreach($array as $key => $args) {
+ $parts=array('inputs');
+ if(!$head) { $head = true;
+ $parts[]='head';
+ }
+ if(!isset($array[$key+1])) {
+ $parts[]='foot';
+ }
+ $args[] = true;
+ $args[] = $parts;
+ $html .= call_user_func_array(array($this, 'render_insert_form'), $args);
}
+ return $html;
+ }
+
+ function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true, $parts=false) {
+ $html = '';
+ //print_r($parts);
+ //echo('<pre>'); print_r($selectbox);
+ //echo('<pre>'); print_r($current);
+ $update = false;
+ if(is_array($current)) {
+ $update = true;
+ $current = array_shift($current);
+ }
+
+ if(!is_array($hidecols)) $hidecols = array();
+ $hidecols = array_merge($hidecols, array('item_author', 'item_valid_from', 'item_valid_till')); //TODO Autodetect
- $btn = is_array($current) ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
- $html.=$this->input(false, $btn, 'submit');
- $html.='</form>';
+ if(!is_array($parts) || in_array('head', $parts)) {
+ $action = $action ? " action='$action'" : false;
+ $html.="<form$action method='POST'>"; //TODO: use $this->form()
+ if($multi_insert) $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">';
+ }
+
+ if(!is_array($parts) || in_array('inputs', $parts))
+ $html.=$this->render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update);
+
+ if(!is_array($parts) || in_array('foot', $parts)) {
+ if($multi_insert) { //TODO, move to separate JS file
+ $html.=<<<EOF
+ </div></span>
+ <br style="clear:both" />
+ <script>
+ function duplicate_element(what, where) {
+ var node = document.getElementsByName(what)[0];
+ node.parentNode.appendChild(node.cloneNode(true));
+ }
+ </script>
+ <a href='#' onClick="duplicate_element('input_set')">+</a>
+EOF;
+ }
+
+ $btn = is_array($current) ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
+ $html.=$this->input(false, $btn, 'submit');
+ $html.='</form>';
+ }
return $html;
}
}
return $indexed;
}
- function get_columns($class) {
+ function get_columns($class,$disable_cols=array()) { //TODO: Not sure if compatible with non-MySQL DBs
$class = $this->escape($class);
$sql = "SHOW COLUMNS FROM $class;";
- return $this->safe_query_fetch($sql);
+ $columns = $this->safe_query_fetch($sql);
+ /*foreach($columns as $colk => $col) foreach($col as $key => $val) {
+ if(in_array($col['Field'],$disable_cols)) $columns[$colk]['Extra']='auto_increment';
+ }*/
+ return $columns;
}
function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
return array_filter($selectbox, 'ksort');
}
- function map_unique($key, $value, $select, $table) { //TODO: Guess $select and $table if not passed
+ function map_unique($key, $value, $select, $table, $fatal=true) { //TODO: Guess $select and $table if not passed
$history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : '';
$value=$this->quote($value);
$sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
$result = $this->safe_query_fetch($sql);
- if(isset($result[0][$select])) return $result[0][$select]; else die(trigger_error(T('Record not found!'))); //TODO post_redirect_get...
+ if(isset($result[0][$select])) return $result[0][$select]; else if($fatal) die(trigger_error(T('Record not found!'))); //TODO post_redirect_get...
}
function contains_history($table) {
'model_descript' => 'Popis modelu',
'model_barcode' => 'Čárový kód modelu',
'model_barcode:' => 'Čárový kód modelu:',
+ 'model_price_in' => 'Předpokládaná cena',
'model_price_out' => 'Doporučená cena',
'room_name' => 'Sklad',