Clone Manufactured Item -> Clone BOM.
[fa-stable.git] / manufacturing / manage / bom_edit.php
index 09dfd5e9039f129bf3c94b0d658e6d8bfc5430a4..697629bcf923aad5f574ecd5a515e8a6b84115eb 100644 (file)
@@ -1,17 +1,24 @@
 <?php
-
-$page_security = 9;
-$path_to_root="../..";
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
+$page_security = 'SA_BOM';
+$path_to_root = "../..";
 include_once($path_to_root . "/includes/session.inc");
 
-page(_("Bill Of Materials"));
+page(_($help_context = "Bill Of Materials"));
 
 include_once($path_to_root . "/includes/date_functions.inc");
 include_once($path_to_root . "/includes/ui.inc");
 include_once($path_to_root . "/includes/data_checks.inc");
 
-include_once($path_to_root . "/includes/manufacturing.inc");
-
 check_db_has_bom_stock_items(_("There are no manufactured or kit items defined in the system."));
 
 check_db_has_workcentres(_("There are no work centres defined in the system. BOMs require at least one work centre be defined."));
@@ -20,85 +27,28 @@ simple_page_mode(true);
 $selected_component = $selected_id;
 //--------------------------------------------------------------------------------------------------
 
-//if (isset($_GET["NewItem"]))
-//{
-//     $_POST['stock_id'] = $_GET["NewItem"];
-//}
-//if (isset($_GET['stock_id']))
-//{
-//     $_POST['stock_id'] = $_GET['stock_id'];
-//     $selected_parent =  $_GET['stock_id'];
-//}
-
-/* selected_parent could come from a post or a get */
-/*if (isset($_GET["selected_parent"]))
-{
-       $selected_parent = $_GET["selected_parent"];
-}
-else if (isset($_POST["selected_parent"]))
-{
-       $selected_parent = $_POST["selected_parent"];
-}
-*/
-/* selected_component could also come from a post or a get */
-/*if (isset($_GET["selected_component"]))
+if (isset($_GET['stock_id']))
 {
-       $selected_component = $_GET["selected_component"];
+       $_POST['stock_id'] = $_GET['stock_id'];
+       $selected_parent =  $_GET['stock_id'];
 }
-else
-{
-       $selected_component = get_post("selected_component", -1);
-}
-*/
-
-//--------------------------------------------------------------------------------------------------
-
-function check_for_recursive_bom($ultimate_parent, $component_to_check)
-{
-
-       /* returns true ie 1 if the bom contains the parent part as a component
-       ie the bom is recursive otherwise false ie 0 */
-
-       $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
-       $result = db_query($sql,"could not check recursive bom");
-
-       if ($result != 0)
-       {
-               while ($myrow = db_fetch_row($result))
-               {
-                       if ($myrow[0] == $ultimate_parent)
-                       {
-                               return 1;
-                       }
-
-                       if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
-                       {
-                               return 1;
-                       }
-               } //(while loop)
-       } //end if $result is true
-
-       return 0;
-
-} //end of function check_for_recursive_bom
 
 //--------------------------------------------------------------------------------------------------
 
 function display_bom_items($selected_parent)
 {
-       global $table_style;
-
        $result = get_bom($selected_parent);
-div_start('bom');
-       start_table("$table_style width=60%");
+       div_start('bom');
+       start_table(TABLESTYLE, "width='60%'");
        $th = array(_("Code"), _("Description"), _("Location"),
                _("Work Centre"), _("Quantity"), _("Units"),'','');
        table_header($th);
 
        $k = 0;
+       $found = false;
        while ($myrow = db_fetch($result))
        {
-
+               $found = true;
                alt_table_row_color($k);
 
                label_cell($myrow["component"]);
@@ -108,14 +58,34 @@ div_start('bom');
         qty_cell($myrow["quantity"], false, get_qty_dec($myrow["component"]));
         label_cell($myrow["units"]);
                edit_button_cell("Edit".$myrow['id'], _("Edit"));
-               edit_button_cell("Delete".$myrow['id'], _("Delete"));
+               delete_button_cell("Delete".$myrow['id'], _("Delete"));
         end_row();
 
        } //END WHILE LIST LOOP
        end_table();
-div_end();
+       
+       if ($found) {
+               start_table(TABLESTYLE, "width='60%'");
+               stock_manufactured_items_list_row(_("Copy BOM to another manufacturable item"), 'new_stock_id', $selected_parent, false, true);
+               end_table();
+       }
+
+       div_end();
 }
 
+function copy_bom_items($stock_id, $new_stock_id)
+{
+       $result = get_bom($stock_id);
+       while ($myrow = db_fetch($result))
+       {
+               $_POST['component'] = $myrow["component"];
+               $_POST['loc_code'] = $myrow["loc_code"];
+               $_POST['workcentre_added'] = $myrow["workcentre_added"];
+               $_POST['quantity'] = $myrow["quantity"];
+               on_submit($new_stock_id, -1);
+       }
+}
 //--------------------------------------------------------------------------------------------------
 
 function on_submit($selected_parent, $selected_component=-1)
@@ -129,15 +99,8 @@ function on_submit($selected_parent, $selected_component=-1)
 
        if ($selected_component != -1)
        {
-
-               $sql = "UPDATE ".TB_PREF."bom SET workcentre_added='" . $_POST['workcentre_added'] . "',
-                       loc_code='" . $_POST['loc_code'] . "',
-                       quantity= " . input_num('quantity') . "
-                       WHERE parent='" . $selected_parent . "'
-                       AND id='" . $selected_component . "'";
-               check_db_error("Could not update this bom component", $sql);
-
-               db_query($sql,"could not update bom");
+               update_bom($selected_parent, $selected_component, $_POST['workcentre_added'], $_POST['loc_code'],
+                       input_num('quantity'));
                display_notification(_('Selected component has been updated'));
                $Mode = 'RESET';
        }
@@ -153,21 +116,11 @@ function on_submit($selected_parent, $selected_component=-1)
                {
 
                        /*Now check to see that the component is not already on the bom */
-                       $sql = "SELECT component FROM ".TB_PREF."bom
-                               WHERE parent='$selected_parent'
-                               AND component='" . $_POST['component'] . "'
-                               AND workcentre_added='" . $_POST['workcentre_added'] . "'
-                               AND loc_code='" . $_POST['loc_code'] . "'" ;
-                       $result = db_query($sql,"check failed");
-
-                       if (db_num_rows($result) == 0)
+                       if (!is_component_already_on_bom($_POST['component'], $_POST['workcentre_added'],
+                               $_POST['loc_code'], $selected_parent))
                        {
-                               $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
-                                       VALUES ('$selected_parent', '" . $_POST['component'] . "', '"
-                                       . $_POST['workcentre_added'] . "', '" . $_POST['loc_code'] . "', "
-                                       . input_num('quantity') . ")";
-
-                               db_query($sql,"check failed");
+                               add_bom($selected_parent, $_POST['component'], $_POST['workcentre_added'],
+                                       $_POST['loc_code'], input_num('quantity'));
                                display_notification(_("A new component part has been added to the bill of material for this item."));
                                $Mode = 'RESET';
                        }
@@ -189,8 +142,7 @@ function on_submit($selected_parent, $selected_component=-1)
 
 if ($Mode == 'Delete')
 {
-       $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $selected_component. "'";
-       db_query($sql,"Could not delete this bom components");
+       delete_bom($selected_id);
 
        display_notification(_("The component item has been deleted from this bom"));
        $Mode = 'RESET';
@@ -198,64 +150,73 @@ if ($Mode == 'Delete')
 
 if ($Mode == 'RESET')
 {
-       $selected_component = -1;
+       $selected_id = -1;
        unset($_POST['quantity']);
 }
 
 //--------------------------------------------------------------------------------------------------
 
-start_form(false, true);
+if (list_updated('new_stock_id')) {
+       copy_bom_items($_POST['stock_id'], $_POST['new_stock_id']);
+       $item = get_item($_POST['new_stock_id']);
+       $_POST['stock_id'] = $_POST['new_stock_id'];
+       $Ajax->activate('_page_body');
+       display_notification(_("BOM copied to ") . $item['description']);
+}
 
-echo "<center>" . _("Select a manufacturable item:") . "&nbsp;";
-stock_bom_items_list('selected_parent', null, false, true);
-echo "</center><br>";
+start_form();
 
-end_form();
-if (isset($_POST['_selected_parent_update']))
+start_form(false, true);
+start_table(TABLESTYLE_NOBORDER);
+start_row();
+stock_manufactured_items_list_cells(_("Select a manufacturable item:"), 'stock_id', null, false, true);
+end_row();
+if (list_updated('stock_id'))
+{
+       $selected_id = -1;
        $Ajax->activate('_page_body');
+}
+end_table();
+br();
+
+end_form();
 //--------------------------------------------------------------------------------------------------
 
-if (get_post('selected_parent') != '')
+if (get_post('stock_id') != '')
 { //Parent Item selected so display bom or edit component
-       $selected_parent = $_POST['selected_parent'];
+       $selected_parent = $_POST['stock_id'];
        if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
-               on_submit($selected_parent, $selected_component);
+               on_submit($selected_parent, $selected_id);
        //--------------------------------------------------------------------------------------
 
 start_form();
        display_bom_items($selected_parent);
-//end_form();
        //--------------------------------------------------------------------------------------
        echo '<br>';
-//     start_form(false, true);
 
-       start_table($table_style2);
+       start_table(TABLESTYLE2);
 
-       if ($selected_component != -1)
+       if ($selected_id != -1)
        {
                if ($Mode == 'Edit') {
                        //editing a selected component from the link to the line item
-                       $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM ".TB_PREF."bom,".TB_PREF."stock_master
-                               WHERE id='$selected_component'
-                               AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
-
-                       $result = db_query($sql, "could not get bom");
-                       $myrow = db_fetch($result);
+                       $myrow = get_component_from_bom($selected_id);
 
                        $_POST['loc_code'] = $myrow["loc_code"];
+                       $_POST['component'] = $myrow["component"]; // by Tom Moulton
                        $_POST['workcentre_added']  = $myrow["workcentre_added"];
                        $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["component"]));
+                       label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
                }
-               hidden('component', $selected_component);
-               label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
+               hidden('selected_id', $selected_id);
        }
        else
        {
                start_row();
-               label_cell(_("Component:"));
+               label_cell(_("Component:"), "class='label'");
 
                echo "<td>";
-               stock_component_items_list('component', $selected_parent, null, false, true);
+               echo stock_component_items_list('component', $selected_parent, null, false, true);
                if (get_post('_component_update')) 
                {
                        $Ajax->activate('quantity');
@@ -263,7 +224,7 @@ start_form();
                echo "</td>";
                end_row();
        }
-       hidden('selected_parent', $selected_parent);
+//     hidden('stock_id', $selected_parent);
 
        locations_list_row(_("Location to Draw From:"), 'loc_code', null);
        workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
@@ -272,11 +233,10 @@ start_form();
        qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
 
        end_table(1);
-       submit_add_or_update_center($selected_component == -1, '', true);
+       submit_add_or_update_center($selected_id == -1, '', 'both');
        end_form();
 }
 // ----------------------------------------------------------------------------------
 
 end_page();
 
-?>