Priprava na nepocitatelny polozky
authorThomas Mudrunka <tomas@mudrunka.cz>
Thu, 25 Aug 2011 18:22:32 +0000 (20:22 +0200)
committerThomas Mudrunka <tomas@mudrunka.cz>
Thu, 25 Aug 2011 18:22:32 +0000 (20:22 +0200)
assistants/dispose.inc.php
assistants/sell.inc.php
assistants/store-single.inc.php
assistants/store.inc.php
index.php
locale/cs/messages.inc.php

index fcaf7f2cdda4246adf42f674cbb113be8dd223c2..6d13b1568b055de122ba3d731c1c66f565ad0284 100644 (file)
@@ -1,21 +1,58 @@
 <?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;
 }
index 4bdfbfd2a6b5a907a45ee436ad99f0b1d97f669a..6a1a3b8d51a8f83e5f1c03857cc43b6c62ef27b6 100644 (file)
@@ -1,8 +1,4 @@
 <?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(
index dc550b8247b3b099344459f5872b2de4131b4648..7af375d19962043843b6541d7763789d28a980f5 100644 (file)
@@ -4,7 +4,7 @@ switch($SUBPATH[0]) {
        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');
index cacee8f53c02dfabd70e94d5029e60d4300b4324..b7818abb33dee6eb32be15557979cf5d02ca6e4b 100644 (file)
@@ -3,23 +3,46 @@ switch($SUBPATH[0]) {
        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;
 }
index 5c54331a1e53f5144f62b6da2eb544a8047c613b..8af511cb76b3634042364ccbff9892be5281a80b 100755 (executable)
--- a/index.php
+++ b/index.php
@@ -167,7 +167,7 @@ class Sklad_HTML extends HTML { //TODO: Split into few more methods
 * { 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 {
@@ -322,21 +322,8 @@ EOF;
                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'].'][]';
@@ -356,24 +343,70 @@ EOF;
                        }
                        $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;
        }
 }
@@ -476,10 +509,14 @@ class Sklad_DB extends PDO {
                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') {
@@ -500,12 +537,12 @@ class Sklad_DB extends PDO {
                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) {
index 679f4502e62cf6c8fcf3463a61bfc4cbd79351cd..e6db9c206e2d02022e48326f06b0227d54fc5379 100644 (file)
@@ -26,6 +26,7 @@ $LOCALE_MESSAGES = array(
                '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',
This page took 0.200969 seconds and 4 git commands to generate.