Changed API for input/lists functions, added empty hints when needed
[fa-stable.git] / manufacturing / manage / bom_edit.php
index 181ad2076c3c5c30f488f69caf1024f9f6cfaee8..a10606b147d308d07986cc58a84d3da948e7199b 100644 (file)
@@ -21,9 +21,6 @@ check_db_has_workcentres(_("There are no work centres defined in the system. BOM
 if (isset($_GET["NewItem"]))
 {
        $_POST['stock_id'] = $_GET["NewItem"];
-       if (isset($_GET['item']) && isset($_GET['qty']))
-               add_material_cost($_GET["NewItem"], $_GET['item'], $_GET['qty'], false);
-
 }
 if (isset($_GET['stock_id']))
 {
@@ -50,24 +47,6 @@ elseif (isset($_POST["selected_component"]))
        $selected_component = $_POST["selected_component"];
 }
 
-function add_material_cost($parent, $item, $n, $add=true)
-{
-       $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id='$parent'";
-       $result = db_query($sql);
-       $myrow = db_fetch($result);
-       $material_cost = $myrow['material_cost'];
-       $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id='$item'";
-       $result = db_query($sql);
-       $myrow = db_fetch($result);
-       $material_cost2 = $myrow['material_cost'];
-       if ($add)
-               $material_cost += ($material_cost2 * $n);
-       else
-               $material_cost -= ($material_cost2 * $n);
-       $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
-               WHERE stock_id='$parent'";
-       db_query($sql,"The cost details for the inventory item could not be updated");
-}
 
 //--------------------------------------------------------------------------------------------------
 
@@ -110,7 +89,7 @@ function display_bom_items($selected_parent)
 
        start_table("$table_style width=60%");
        $th = array(_("Code"), _("Description"), _("Location"),
-               _("Work Centre"), _("Quantity"), _("Units"));
+               _("Work Centre"), _("Quantity"), _("Units"),'','');
        table_header($th);
 
        $k = 0;
@@ -123,10 +102,10 @@ function display_bom_items($selected_parent)
                label_cell($myrow["description"]);
         label_cell($myrow["location_name"]);
         label_cell($myrow["WorkCentreDescription"]);
-        label_cell($myrow["quantity"]);
+        label_cell(qty_format($myrow["quantity"]));
         label_cell($myrow["units"]);
-        edit_link_cell(SID . "NewItem=$selected_parent&selected_component=" . $myrow["id"]."&item=".$myrow['component']."&qty=".$myrow['quantity']);
-        delete_link_cell(SID . "delete=" . $myrow["id"]. "&stock_id=" . $_POST['stock_id']."&item=".$myrow['component']."&qty=".$myrow['quantity']);
+        edit_link_cell(SID . "NewItem=$selected_parent&selected_component=" . $myrow["id"]);
+        delete_link_cell(SID . "delete=" . $myrow["id"]. "&stock_id=" . $_POST['stock_id']);
         end_row();
 
        } //END WHILE LIST LOOP
@@ -135,40 +114,34 @@ function display_bom_items($selected_parent)
 
 //--------------------------------------------------------------------------------------------------
 
-function on_submit($selected_parent, $selected_component)
+function on_submit($selected_parent, $selected_component=null)
 {
-       if (!is_numeric($_POST['quantity']))
-       {
-               display_error(_("The quantity entered must be numeric."));
-               return;
-       }
-
-       if ($_POST['quantity'] <= 0)
+       if (!check_num('quantity', 0))
        {
-               display_error(_("The quantity entered must be greater than zero."));
+               display_error(_("The quantity entered must be numeric and greater than zero."));
+               set_focus('quantity');
                return;
        }
 
-
        if (isset($selected_parent) && isset($selected_component))
        {
 
                $sql = "UPDATE ".TB_PREF."bom SET workcentre_added='" . $_POST['workcentre_added'] . "',
                        loc_code='" . $_POST['loc_code'] . "',
-                       quantity= " . $_POST['quantity'] . "
+                       quantity= " . input_num('quantity') . "
                        WHERE parent='" . $selected_parent . "'
                        AND id='" . $selected_component . "'";
                check_db_error("Could not update this bom component", $sql);
 
-               add_material_cost($selected_parent, $_POST['item'], $_POST['quantity'], true);
-
                db_query($sql,"could not update bom");
 
        }
        elseif (!isset($selected_component) && isset($selected_parent))
        {
 
-               /*Selected component is null cos no item selected on first time round so must be                                adding a record must be Submitting new entries in the new component form */
+               /*Selected component is null cos no item selected on first time round
+               so must be adding a record must be Submitting new entries in the new
+               component form */
 
                //need to check not recursive bom component of itself!
                If (!check_for_recursive_bom($selected_parent, $_POST['component']))
@@ -185,11 +158,12 @@ function on_submit($selected_parent, $selected_component)
                        if (db_num_rows($result) == 0)
                        {
                                $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
-                                       VALUES ('$selected_parent', '" . $_POST['component'] . "', '" . $_POST['workcentre_added'] . "', '" . $_POST['loc_code'] . "', " . $_POST['quantity'] . ")";
+                                       VALUES ('$selected_parent', '" . $_POST['component'] . "', '"
+                                       . $_POST['workcentre_added'] . "', '" . $_POST['loc_code'] . "', "
+                                       . input_num('quantity') . ")";
 
                                db_query($sql,"check failed");
 
-                               add_material_cost($selected_parent, $_POST['component'], $_POST['quantity'], true);
                                //$msg = _("A new component part has been added to the bill of material for this item.");
 
                        }
@@ -211,10 +185,9 @@ function on_submit($selected_parent, $selected_component)
 
 if (isset($_GET['delete']))
 {
-       $sql = "DELETE FROM ".TB_PREF."bom WHERE id='".$_GET['delete']."'";
-       db_query($sql,"Could not delete this bom components");
 
-       add_material_cost($_GET['stock_id'], $_GET['item'], $_GET['qty'], false);
+       $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $_GET['delete']. "'";
+       db_query($sql,"Could not delete this bom components");
 
        display_note(_("The component item has been deleted from this bom."));
 
@@ -227,6 +200,7 @@ start_form(false, true);
 
 echo "<center>" . _("Select a manufacturable item:") . "&nbsp;";
 stock_bom_items_list('stock_id', null, false, true);
+echo "</center>";
 
 end_form();
 
@@ -235,10 +209,12 @@ end_form();
 if (isset($_POST['stock_id']))
 { //Parent Item selected so display bom or edit component
        $selected_parent = $_POST['stock_id'];
-
-       if (isset($selected_parent) && isset($_POST['Submit']))
+       if (isset($selected_parent) && isset($_POST['Submit'])) {
+         if(isset($selected_component))
                on_submit($selected_parent, $selected_component);
-
+         else
+               on_submit($selected_parent);
+       }
        //--------------------------------------------------------------------------------------
 
        display_bom_items($selected_parent);
@@ -266,9 +242,8 @@ if (isset($_POST['stock_id']))
 
                $_POST['loc_code'] = $myrow["loc_code"];
                $_POST['workcentre_added']  = $myrow["workcentre_added"];
-               $_POST['quantity'] = $myrow["quantity"];
+               $_POST['quantity'] = qty_format($myrow["quantity"]);
 
-               hidden('item', $myrow["component"]);
                hidden('selected_parent', $selected_parent);
                hidden('selected_component', $selected_component);
                label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
@@ -283,7 +258,7 @@ if (isset($_POST['stock_id']))
                label_cell(_("Component:"));
 
                echo "<td>";
-               stock_component_items_list('component', $selected_parent, $_POST['component'], false, true);
+               stock_component_items_list('component', $selected_parent, null, false, true);
                echo "</td>";
                end_row();
        }
@@ -293,9 +268,9 @@ if (isset($_POST['stock_id']))
 
        if (!isset($_POST['quantity']))
        {
-               $_POST['quantity'] = 1;
+               $_POST['quantity'] = qty_format(1);
        }
-       text_row(_("Quantity:"), 'quantity', $_POST['quantity'], 10, 18);
+       qty_row(_("Quantity:"), 'quantity', $_POST['quantity']);
 
        end_table(1);
        submit_center('Submit', _("Add/Update"));