Changed so that average item material price is automatic updated whenever a PO Delive...
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 4 Mar 2008 09:38:29 +0000 (09:38 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 4 Mar 2008 09:38:29 +0000 (09:38 +0000)
Changed so that a Bill of Materiel, Parent's material price, is automatic updated from underlying items.

CHANGELOG.txt
config.php
includes/page/header.inc
inventory/cost_update.php
manufacturing/manage/bom_edit.php
purchasing/includes/db/grn_db.inc

index 8a0ec98f15083eae152c1999441a3a346695ebaf..8c997c071319872a8effae6ba28fb414d4cc90a4 100644 (file)
@@ -19,6 +19,13 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+04-Mar-2008 Joe Hunt
+! Changed so that average item material price is automatic updated whenever a PO Delivery is received.
+  Changed so that a Bill of Materiel, Parent's material price, is automatic updated from underlying items.
+$ /inventory/cost_update.php
+  /purchasing/includes/db/grn_db.inc and
+  /manufacturing/manage/bom_edit.php. 
+  
 21-Feb-2008 Joe Hunt
 + Added Print for documents in /admin/view_print_transaction.php (according to our new routines).
 $ /admin/view_print_transaction.php
index 67683efcf13b19061c307caa4f3c4d8343ed42f1..a28c6c658ba1e8cc9277236241b939c443a92a9a 100644 (file)
@@ -74,6 +74,9 @@
        /* use Audit Trails in GL */
        $use_audit_trail = 0;
 
+       /* Integrated base Wiki Help URL or null if not used */
+       $help_base_url = $path_to_root.'/modules/wiki/index.php?n='._('Help').'.';
+
        /* allow alpha characters in accounts. 0 = numeric, 1 = alpha numeric, 2 = uppercase alpha numeric */
        $accounts_alpha = 0;
 
                array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
     );
 
+       /*
+       System tabs. This variable should be in future included from separate file for extended module manager
+       */
+       $applications = array (
+               'orders' => _("Sales"),
+               'AP'=>_("Purchases"),
+               'stock'=> _("Items and Inventory"),
+               'manuf'=> _("Manufacturing"),
+               'proj'=>_("Dimensions"),
+               'GL'=>_("Banking and General Ledger"),
+               'system'=>_("Setup")
+       );
+
        //MySQL Backup and Restore Settings
 
     define("BACKUP_PATH", "/admin/backup/");
index c421a66587c0f3f7c4620953623e6ab40417d0a6..12b99eba88621902e141fb4f04fc4a4d03641b55 100644 (file)
@@ -1,12 +1,49 @@
 <?php
 
+function help_url($title, $app)
+{
+
+       global $help_page_url, $help_base_url, $applications, $help_page_title;
+
+       if (isset($help_page_url))
+       {
+               return $help_base_url.$help_page_url;
+       }
+
+       $help_page_url = '';
+       if (isset($help_page_title))
+       {
+               $help_page_url = $help_page_title;
+       }
+       elseif ($title==_("Main Menu"))
+       {
+               $help_page_url = $applications[$app];
+       }
+       else
+       {
+               $help_page_url = $title;
+       }
+
+       return $help_base_url.urlencode(str_replace(' ', '', ucwords($help_page_url)));
+}
+
 function page_header($title, $no_menu=false, $is_index=false, $onload="", $js="")
 {
        // titles and screen header
-       global $db_connections, $path_to_root, $def_app;
+       global $db_connections, $path_to_root, $def_app, $applications,
+               $help_base_url, $help_page_url, $use_popup_windows;
        $theme = user_theme();
        $local_path_to_root = $path_to_root;
 
+       if ($help_base_url != null && $use_popup_windows && $js == '')
+       {
+               if (!function_exists('get_js_open_window'))
+               {
+                       include_once($path_to_root.'/includes/ui/ui_view.inc');
+               }
+               $js = get_js_open_window(900, 500);
+       }
+
        if (!isset($no_menu))
        {
                $no_menu = false;
@@ -15,17 +52,17 @@ function page_header($title, $no_menu=false, $is_index=false, $onload="", $js=""
                session_register("sel_app");
        if (isset($_SESSION["App"]) && is_object($_SESSION["App"]) && isset($_SESSION["App"]->selected_application) &&
                $_SESSION["App"]->selected_application != "")
-               $sel_app = $_SESSION["App"]->selected_application;
+               $sel_app = $_SESSION["App"]->selected_application;
        elseif (isset($_SESSION["sel_app"]) && $_SESSION["sel_app"] != "")
                $sel_app = $_SESSION["sel_app"];
        else
                $sel_app = $def_app;
        $_SESSION["sel_app"] = $sel_app;
 
-    echo "<html dir='" . $_SESSION['language']->dir . "' >";
-    echo "<head><title>$title</title>";
-    $encoding = $_SESSION['language']->encoding;
-    $local_stylesheet = $_SESSION['language']->get_stylesheet();
+       echo "<html dir='" . $_SESSION['language']->dir . "' >";
+       echo "<head><title>$title</title>";
+       $encoding = $_SESSION['language']->encoding;
+       $local_stylesheet = $_SESSION['language']->get_stylesheet();
        echo "<meta http-equiv='Content-type' content='text/html'; charset='$encoding'>";
        echo "<link href='$local_path_to_root/themes/$theme/default.css' rel='stylesheet' type='text/css' /> \n";
        if (file_exists($local_path_to_root. "/".$local_stylesheet))
@@ -37,33 +74,37 @@ function page_header($title, $no_menu=false, $is_index=false, $onload="", $js=""
                echo "<body>";
        else
                echo "<body onload=\"$onload\">";
-    echo "<table class='callout_main' border='0' cellpadding='0' cellspacing='0'>\n";
+       echo "<table class='callout_main' border='0' cellpadding='0' cellspacing='0'>\n";
        echo "<tr>\n";
-    echo "<td colspan='2' rowspan='2'>\n";
+       echo "<td colspan='2' rowspan='2'>\n";
 
        echo "<table class='main_page' border='0' cellpadding='0' cellspacing='0'>\n";
        echo "<tr>\n";
        echo "<td>\n";
-    echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+       echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
        echo "<tr>\n";
-    echo "<td class='quick_menu'>\n";
+       echo "<td class='quick_menu'>\n";
        if (!$no_menu)
        {
                echo "<table cellpadding=0 cellspacing=0 width='100%'><tr><td>";
                echo "<div class=tabs>";
-               echo "<a ".($sel_app == "orders" ? "class='selected' " : "")."href='$local_path_to_root/index.php?application=orders". SID ."'>" . _("Sales") . "</a>";
-               echo "<a ".($sel_app == "AP" ? "class='selected' " : "")."href='$local_path_to_root/index.php?application=AP". SID ."'>" . _("Purchases") . "</a>";
-               echo "<a ".($sel_app == "stock" ? "class='selected' " : "")."href='$local_path_to_root/index.php?application=stock". SID ."'>" . _("Items and Inventory") . "</a>";
-               echo "<a ".($sel_app == "manuf" ? "class='selected' " : "")."href='$local_path_to_root/index.php?application=manuf". SID ."'>" . _("Manufacturing") . "</a>";
-               echo "<a ".($sel_app == "proj" ? "class='selected' " : "")."href='$local_path_to_root/index.php?application=proj". SID ."'>" . _("Dimensions") . "</a>";
-               echo "<a ".($sel_app == "GL" ? "class='selected' " : "")."href='$local_path_to_root/index.php?application=GL". SID ."'>" . _("Banking and General Ledger") . "</a>";
-               echo "<a ".($sel_app == "system" ? "class='selected' " : "")."href='$local_path_to_root/index.php?application=system". SID ."'>" . _("Setup") . "</a>";
+               foreach($applications as $app => $name)
+               {
+                       echo "<a ".($sel_app == $app ? "class='selected' " : "").
+                       "href='$local_path_to_root/index.php?application=".$app.
+                               SID ."'>" .$name . "</a>";
+               }
                echo "</div>";
 
                echo "</td></tr></table>";
 
                echo "<table class=logoutBar>";
                echo "<tr><td class=headingtext3>" . $db_connections[$_SESSION["wa_current_user"]->company]["name"] . " | " . $_SERVER['SERVER_NAME'] . " | " . $_SESSION["wa_current_user"]->name . "</td>";
+               if ($help_base_url != null)
+               {
+
+                       echo "<td align=right ><a target = '_blank' onclick=" .'"'."javascript:openWindow(this.href,this.target); return false;".'" '. "href='". help_url($title, $sel_app)."'>" . _("Help") . "</a></td>";
+               }
                echo "<td class=logoutBarRight><a href='$local_path_to_root/access/logout.php?'>" . _("Logout") . "</a>";
                echo "</td></tr></table>";
        }
index 845ec8494ddeaf8d77cd0aa67683376d55f12aa3..6b6c5950434280ae0f5608d13263711471685411 100644 (file)
@@ -30,18 +30,18 @@ if (isset($_GET['stock_id']))
 if (isset($_POST['UpdateData']))
 {
 
-       $old_cost = $_POST['OldMaterialCost'] + $_POST['OldLabourCost'] + $_POST['OldLabourCost'];
+       $old_cost = $_POST['OldMaterialCost'] + $_POST['OldLabourCost'] + $_POST['OldOverheadCost'];
        $new_cost = $_POST['material_cost'] + $_POST['labour_cost'] + $_POST['overhead_cost'];
 
        $should_update = true;
 
-       if (!is_numeric($_POST['material_cost']) || !is_numeric($_POST['labour_cost']) || 
-               !is_numeric($_POST['overhead_cost'])) 
+       if (!is_numeric($_POST['material_cost']) || !is_numeric($_POST['labour_cost']) ||
+               !is_numeric($_POST['overhead_cost']))
        {
                display_error( _("The entered cost is not numeric."));
                $should_update = false;
-       } 
-       elseif ($old_cost == $new_cost) 
+       }
+       elseif ($old_cost == $new_cost)
        {
                display_error( _("The new cost is the same as the old cost. Cost was not updated."));
                $should_update = false;
@@ -89,20 +89,20 @@ hidden("OldLabourCost", $myrow["labour_cost"]);
 hidden("OldOverheadCost", $myrow["overhead_cost"]);
 
 start_table($table_style2);
-label_row(_("Last Cost"), number_format2($myrow["last_cost"],user_price_dec()), 
+label_row(_("Last Cost"), number_format2($myrow["last_cost"],user_price_dec()),
        "class='tableheader2'", "nowrap align=right");
 
-text_row(_("Standard Material Cost Per Unit"), "material_cost", 
+text_row(_("Standard Material Cost Per Unit"), "material_cost",
        number_format($myrow["material_cost"],user_price_dec()), "", "", "class='tableheader2'");
 
 if ($myrow["mb_flag"]=='M')
 {
-       text_row(_("Standard Labour Cost Per Unit"), "labour_cost", 
+       text_row(_("Standard Labour Cost Per Unit"), "labour_cost",
                number_format($myrow["labour_cost"],user_price_dec()), "", "", "class='tableheader2'");
-       text_row(_("Standard Overhead Cost Per Unit"), "overhead_cost", 
+       text_row(_("Standard Overhead Cost Per Unit"), "overhead_cost",
                number_format($myrow["overhead_cost"],user_price_dec()), "", "", "class='tableheader2'");
-} 
-else 
+}
+else
 {
        hidden("labour_cost", 0);
        hidden("overhead_cost", 0);
index 2069f86b0e87386693dea066663082d137282173..181ad2076c3c5c30f488f69caf1024f9f6cfaee8 100644 (file)
@@ -4,7 +4,7 @@ $page_security = 9;
 $path_to_root="../..";
 include_once($path_to_root . "/includes/session.inc");
 
-page(_("Bill Of Materials"));  
+page(_("Bill Of Materials"));
 
 include_once($path_to_root . "/includes/date_functions.inc");
 include_once($path_to_root . "/includes/ui.inc");
@@ -20,7 +20,10 @@ check_db_has_workcentres(_("There are no work centres defined in the system. BOM
 
 if (isset($_GET["NewItem"]))
 {
-       $_POST['stock_id'] = $_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']))
 {
@@ -41,16 +44,34 @@ else if (isset($_POST["selected_parent"]))
 if (isset($_GET["selected_component"]))
 {
        $selected_component = $_GET["selected_component"];
-} 
+}
 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");
+}
 
 //--------------------------------------------------------------------------------------------------
 
-function check_for_recursive_bom($ultimate_parent, $component_to_check) 
+function check_for_recursive_bom($ultimate_parent, $component_to_check)
 {
 
        /* returns true ie 1 if the bom contains the parent part as a component
@@ -59,7 +80,7 @@ function check_for_recursive_bom($ultimate_parent, $component_to_check)
        $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
        $result = db_query($sql,"could not check recursive bom");
 
-       if ($result != 0) 
+       if ($result != 0)
        {
                while ($myrow = db_fetch_row($result))
                {
@@ -81,21 +102,21 @@ function check_for_recursive_bom($ultimate_parent, $component_to_check)
 
 //--------------------------------------------------------------------------------------------------
 
-function display_bom_items($selected_parent) 
+function display_bom_items($selected_parent)
 {
        global $table_style;
-       
+
        $result = get_bom($selected_parent);
-          
+
        start_table("$table_style width=60%");
        $th = array(_("Code"), _("Description"), _("Location"),
                _("Work Centre"), _("Quantity"), _("Units"));
        table_header($th);
-       
+
        $k = 0;
-       while ($myrow = db_fetch($result)) 
+       while ($myrow = db_fetch($result))
        {
-               
+
                alt_table_row_color($k);
 
                label_cell($myrow["component"]);
@@ -104,11 +125,11 @@ function display_bom_items($selected_parent)
         label_cell($myrow["WorkCentreDescription"]);
         label_cell($myrow["quantity"]);
         label_cell($myrow["units"]);
-        edit_link_cell(SID . "NewItem=$selected_parent&selected_component=" . $myrow["id"]);
-        delete_link_cell(SID . "delete=" . $myrow["id"]. "&stock_id=" . $_POST['stock_id']);
+        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']);
         end_row();
 
-       } //END WHILE LIST LOOP 
+       } //END WHILE LIST LOOP
        end_table();
 }
 
@@ -116,83 +137,87 @@ function display_bom_items($selected_parent)
 
 function on_submit($selected_parent, $selected_component)
 {
-       if (!is_numeric($_POST['quantity'])) 
+       if (!is_numeric($_POST['quantity']))
        {
                display_error(_("The quantity entered must be numeric."));
                return;
        }
 
-       if ($_POST['quantity'] <= 0) 
+       if ($_POST['quantity'] <= 0)
        {
                display_error(_("The quantity entered must be greater than zero."));
                return;
        }
-       
-       
-       if (isset($selected_parent) && isset($selected_component)) 
+
+
+       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'] . " 
-                       WHERE parent='" . $selected_parent . "' 
+               $sql = "UPDATE ".TB_PREF."bom SET workcentre_added='" . $_POST['workcentre_added'] . "',
+                       loc_code='" . $_POST['loc_code'] . "',
+                       quantity= " . $_POST['quantity'] . "
+                       WHERE parent='" . $selected_parent . "'
                        AND id='" . $selected_component . "'";
-               check_db_error("Could not update this bom component", $sql);                            
+               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)) 
+       }
+       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 */
 
                //need to check not recursive bom component of itself!
-               If (!check_for_recursive_bom($selected_parent, $_POST['component'])) 
+               If (!check_for_recursive_bom($selected_parent, $_POST['component']))
                {
 
                        /*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'] . "' 
+                       $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 (db_num_rows($result) == 0)
                        {
-                               $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity) 
+                               $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'] . ")";
 
-                               db_query($sql,"check failed");                                          
+                               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.");
 
-                       } 
-                       else 
+                       }
+                       else
                        {
                                /*The component must already be on the bom */
-                               display_error(_("The selected component is already on this bom. You can modify it's quantity but it cannot appear more than once on the same bom.")); 
+                               display_error(_("The selected component is already on this bom. You can modify it's quantity but it cannot appear more than once on the same bom."));
                        }
 
                } //end of if its not a recursive bom
-               else 
+               else
                {
                        display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
                }
-       } 
+       }
 }
 
 //--------------------------------------------------------------------------------------------------
 
-if (isset($_GET['delete'])) 
+if (isset($_GET['delete']))
 {
-       
-       $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $_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);
+
        display_note(_("The component item has been deleted from this bom."));
-       
+
 }
 
 //--------------------------------------------------------------------------------------------------
@@ -207,18 +232,18 @@ end_form();
 
 //--------------------------------------------------------------------------------------------------
 
-if (isset($_POST['stock_id'])) 
+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']))
-               on_submit($selected_parent, $selected_component); 
+               on_submit($selected_parent, $selected_component);
 
        //--------------------------------------------------------------------------------------
 
-       display_bom_items($selected_parent);    
+       display_bom_items($selected_parent);
 
-       if (isset($selected_parent) && isset($selected_component)) 
+       if (isset($selected_parent) && isset($selected_component))
        {
                hyperlink_params($_SERVER['PHP_SELF'], _("Add a new Component"), "NewItem=$selected_parent");
        }
@@ -229,10 +254,10 @@ if (isset($_POST['stock_id']))
 
        start_table($table_style2);
 
-       if (isset($selected_component)) 
+       if (isset($selected_component))
        {
                //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 
+               $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";
 
@@ -243,21 +268,22 @@ if (isset($_POST['stock_id']))
                $_POST['workcentre_added']  = $myrow["workcentre_added"];
                $_POST['quantity'] = $myrow["quantity"];
 
+               hidden('item', $myrow["component"]);
                hidden('selected_parent', $selected_parent);
                hidden('selected_component', $selected_component);
                label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
 
-       } 
-       else 
+       }
+       else
        { //end of if $selected_component
 
                hidden('selected_parent', $selected_parent);
 
                start_row();
                label_cell(_("Component:"));
-               
+
                echo "<td>";
-               stock_component_items_list('component', $selected_parent, $_POST['component'], false, true);    
+               stock_component_items_list('component', $selected_parent, $_POST['component'], false, true);
                echo "</td>";
                end_row();
        }
@@ -269,7 +295,7 @@ if (isset($_POST['stock_id']))
        {
                $_POST['quantity'] = 1;
        }
-       text_row(_("Quantity:"), 'quantity', $_POST['quantity'], 10, 18);               
+       text_row(_("Quantity:"), 'quantity', $_POST['quantity'], 10, 18);
 
        end_table(1);
        submit_center('Submit', _("Add/Update"));
@@ -277,7 +303,7 @@ if (isset($_POST['stock_id']))
        end_form();
 }
 
-// ---------------------------------------------------------------------------------- 
+// ----------------------------------------------------------------------------------
 
 end_page();
 
index b6738fe0041306ee26c9f5f5cb9b2cc9c26784cd..910cf9a6f30af6e43fb2cbb91ddf7b0436cc10f3 100644 (file)
@@ -5,45 +5,63 @@
 function add_grn(&$po, $date_, $reference, $location)
 {
        begin_transaction();
-       
-       $grn = add_grn_batch($po->order_no, $po->supplier_id, $reference, $location, $date_); 
-       
-       foreach ($po->line_items as $order_line) 
+
+       $grn = add_grn_batch($po->order_no, $po->supplier_id, $reference, $location, $date_);
+
+       foreach ($po->line_items as $order_line)
        {
 
-               if ($order_line->receive_qty != 0 && $order_line->receive_qty != "" && isset($order_line->receive_qty)) 
+               if ($order_line->receive_qty != 0 && $order_line->receive_qty != "" && isset($order_line->receive_qty))
                {
-                       
+
                        /*Update sales_order_details for the new quantity received and the standard cost used for postings to GL and recorded in the stock movements for FIFO/LIFO stocks valuations*/
 
-                       if ($order_line->qty_received == 0) 
-                       { 
+                       if ($order_line->qty_received == 0)
+                       {
                                /*This must be the first receipt of goods against this line */
-                          /*Need to get the standard cost as it is now so we can process GL jorunals later*/
-                          $order_line->standard_cost = get_standard_cost($order_line->stock_id);
+                               /*Need to get the standard cost as it is now so we can process GL jorunals later*/
+                               $order_line->standard_cost = get_standard_cost($order_line->stock_id);
                        }
 
+                       //------------------- update average material cost ------------------------------------------ Joe Hunt Mar-03-2008
+                       $currency = get_supplier_currency($po->supplier_id);
+                       if ($currency != null)
+                               $price_in_home_currency = to_home_currency($order_line->price, $currency, $date_);
+                       else
+                               $price_in_home_currency = $order_line->price;
+                       $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id='$order_line->stock_id'";
+                       $result = db_query($sql);
+                       $myrow = db_fetch($result);
+                       $material_cost = $myrow['material_cost'];
+                       $qoh = get_qoh_on_date($order_line->stock_id, null, $date_);
+                       $material_cost = ($qoh * $material_cost + $order_line->receive_qty * $price_in_home_currency) /
+                               ($qoh + $order_line->receive_qty);
+                       $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
+                               WHERE stock_id='$order_line->stock_id'";
+                       db_query($sql,"The cost details for the inventory item could not be updated");
+                       //----------------------------------------------------------------------------------------------------------------
+
                        /*Need to insert a grn item */
-                       
-                       $grn_item = add_grn_detail_item($grn, $order_line->po_detail_rec, 
-                               $order_line->stock_id, $order_line->item_description, 
-                               $order_line->standard_cost,     $order_line->receive_qty); 
+
+                       $grn_item = add_grn_detail_item($grn, $order_line->po_detail_rec,
+                               $order_line->stock_id, $order_line->item_description,
+                               $order_line->standard_cost,     $order_line->receive_qty);
 
                        /* Update location stock records - NB  a po cannot be entered for a service/kit parts */
-                       
-            add_stock_move(25, $order_line->stock_id, $grn, $location, $date_, "", 
+
+            add_stock_move(25, $order_line->stock_id, $grn, $location, $date_, "",
                $order_line->receive_qty, $order_line->standard_cost,
-               $po->supplier_id, 1, $order_line->price);                       
-                       
+               $po->supplier_id, 1, $order_line->price);
+
                } /*quantity received is != 0 */
        } /*end of order_line loop */
-       
+
        add_forms_for_sys_type(25, $grn, $location);
-       
-       references::save_last($reference, 25);  
-       
+
+       references::save_last($reference, 25);
+
        commit_transaction();
-       
+
        return $grn;
 }
 
@@ -52,61 +70,61 @@ function add_grn(&$po, $date_, $reference, $location)
 function add_grn_batch($po_number, $supplier_id, $reference, $location, $date_)
 {
        $date = date2sql($date_);
-       
-       $sql = "INSERT INTO ".TB_PREF."grn_batch (purch_order_no, delivery_date, supplier_id, reference, loc_code) 
+
+       $sql = "INSERT INTO ".TB_PREF."grn_batch (purch_order_no, delivery_date, supplier_id, reference, loc_code)
                        VALUES ($po_number, '$date', '$supplier_id', '$reference', '$location')";
 
        db_query($sql, "A grn batch record could not be inserted.");
-       
-       return db_insert_id();  
+
+       return db_insert_id();
 }
 
 //-------------------------------------------------------------------------------------------------------------
 
-function add_grn_detail_item($grn_batch_id, $po_detail_item, $item_code, $description, $standard_unit_cost, 
+function add_grn_detail_item($grn_batch_id, $po_detail_item, $item_code, $description, $standard_unit_cost,
        $quantity_received)
 {
-       $sql = "UPDATE ".TB_PREF."purch_order_details 
-        SET quantity_received = quantity_received + $quantity_received, 
-        std_cost_unit=$standard_unit_cost 
+       $sql = "UPDATE ".TB_PREF."purch_order_details
+        SET quantity_received = quantity_received + $quantity_received,
+        std_cost_unit=$standard_unit_cost
         WHERE po_detail_item = $po_detail_item";
-        
+
        db_query($sql, "a purchase order details record could not be updated. This receipt of goods has not been processed ");
-               
-       $sql = "INSERT INTO ".TB_PREF."grn_items (grn_batch_id, po_detail_item, item_code, description, qty_recd) 
+
+       $sql = "INSERT INTO ".TB_PREF."grn_items (grn_batch_id, po_detail_item, item_code, description, qty_recd)
                VALUES ($grn_batch_id, $po_detail_item, '$item_code', '$description', $quantity_received)";
 
        db_query($sql, "A GRN detail item could not be inserted.");
-       
+
        return db_insert_id();
 }
 
 //----------------------------------------------------------------------------------------
 
-function get_grn_items($grn_batch_id=0, $supplier_id="", $outstanding_only=false, 
+function get_grn_items($grn_batch_id=0, $supplier_id="", $outstanding_only=false,
        $is_invoiced_only=false)
 {
     $sql = "SELECT ".TB_PREF."grn_batch.*, ".TB_PREF."grn_items.*, ".TB_PREF."purch_order_details.unit_price,
-               ".TB_PREF."purch_order_details.std_cost_unit, units 
+               ".TB_PREF."purch_order_details.std_cost_unit, units
        FROM ".TB_PREF."grn_batch, ".TB_PREF."grn_items, ".TB_PREF."purch_order_details, ".TB_PREF."stock_master
-       WHERE ".TB_PREF."grn_items.grn_batch_id=".TB_PREF."grn_batch.id 
+       WHERE ".TB_PREF."grn_items.grn_batch_id=".TB_PREF."grn_batch.id
                AND ".TB_PREF."grn_items.po_detail_item=".TB_PREF."purch_order_details.po_detail_item
        AND ".TB_PREF."stock_master.stock_id=".TB_PREF."grn_items.item_code ";
-                       
+
        if ($grn_batch_id != 0)
                $sql .= " AND ".TB_PREF."grn_batch.id=$grn_batch_id AND ".TB_PREF."grn_items.grn_batch_id=$grn_batch_id ";
-       
+
        if ($is_invoiced_only)
                $sql .= " AND ".TB_PREF."grn_items.quantity_inv > 0";
-       
+
        if ($outstanding_only)
-       $sql .= " AND ".TB_PREF."grn_items.qty_recd - ".TB_PREF."grn_items.quantity_inv > 0"; 
-    
+       $sql .= " AND ".TB_PREF."grn_items.qty_recd - ".TB_PREF."grn_items.quantity_inv > 0";
+
        if ($supplier_id != "")
                $sql .= " AND ".TB_PREF."grn_batch.supplier_id ='$supplier_id' ";
-       
+
        $sql .= " ORDER BY ".TB_PREF."grn_batch.delivery_date, ".TB_PREF."grn_batch.id, ".TB_PREF."grn_items.id";
-    
+
        return db_query($sql, "Could not retreive GRNS");
 }
 
@@ -116,42 +134,42 @@ function get_grn_items($grn_batch_id=0, $supplier_id="", $outstanding_only=false
 
 function get_grn_item_detail($grn_item_no)
 {
-       $sql = "SELECT ".TB_PREF."grn_items.*, ".TB_PREF."purch_order_details.unit_price,  
-       ".TB_PREF."grn_items.qty_recd - ".TB_PREF."grn_items.quantity_inv AS QtyOstdg, 
+       $sql = "SELECT ".TB_PREF."grn_items.*, ".TB_PREF."purch_order_details.unit_price,
+       ".TB_PREF."grn_items.qty_recd - ".TB_PREF."grn_items.quantity_inv AS QtyOstdg,
        ".TB_PREF."purch_order_details.std_cost_unit
-               FROM ".TB_PREF."grn_items, ".TB_PREF."purch_order_details, ".TB_PREF."stock_master 
+               FROM ".TB_PREF."grn_items, ".TB_PREF."purch_order_details, ".TB_PREF."stock_master
                WHERE ".TB_PREF."grn_items.po_detail_item=".TB_PREF."purch_order_details.po_detail_item
                        AND ".TB_PREF."stock_master.stock_id=".TB_PREF."grn_items.item_code
                        AND ".TB_PREF."grn_items.id=$grn_item_no";
-       
+
        $result = db_query($sql, "could not retreive grn item details");
        return db_fetch($result);
-}      
-       
-//----------------------------------------------------------------------------------------     
+}
+
+//----------------------------------------------------------------------------------------
 
 function read_grn_items_to_order($grn_batch, &$order)
 {
-       $result = get_grn_items($grn_batch);      
+       $result = get_grn_items($grn_batch);
 
-       if (db_num_rows($result) > 0) 
+       if (db_num_rows($result) > 0)
        {
 
-               while ($myrow = db_fetch($result)) 
+               while ($myrow = db_fetch($result))
                {
 
                        if (is_null($myrow["units"]))
                        {
                                $units = "";
-                       } 
-                       else 
+                       }
+                       else
                        {
                                $units = $myrow["units"];
                        }
 
-                       $order->add_to_order($order->lines_on_order+1, $myrow["item_code"], 
+                       $order->add_to_order($order->lines_on_order+1, $myrow["item_code"],
                                1,$myrow["description"], $myrow["unit_price"],$units,
-                               sql2date($myrow["delivery_date"]), $myrow["quantity_inv"], 
+                               sql2date($myrow["delivery_date"]), $myrow["quantity_inv"],
                                $myrow["qty_recd"]);
 
                        $order->line_items[$order->lines_on_order]->po_detail_rec = $myrow["po_detail_item"];
@@ -166,23 +184,23 @@ function read_grn_items_to_order($grn_batch, &$order)
 function read_grn($grn_batch, &$order)
 {
        $sql= "SELECT * FROM ".TB_PREF."grn_batch WHERE id=$grn_batch";
-               
-       $result = db_query($sql, "The grn sent is not valid");  
 
-       $row = db_fetch($result);       
-       $po_number = $row["purch_order_no"];                    
-       
+       $result = db_query($sql, "The grn sent is not valid");
+
+       $row = db_fetch($result);
+       $po_number = $row["purch_order_no"];
+
        $result = read_po_header($po_number, $order);
-       
-       if ($result) 
+
+       if ($result)
        {
-               
+
                $order->orig_order_date = sql2date($row["delivery_date"]);
                $order->location = $row["loc_code"];
-               $order->reference = $row["reference"];                  
-               
+               $order->reference = $row["reference"];
+
                read_grn_items_to_order($grn_batch, $order);
-       }       
+       }
 }
 
 //----------------------------------------------------------------------------------------------------------
@@ -192,7 +210,7 @@ function read_grn($grn_batch, &$order)
 function get_po_grns($po_number)
 {
     $sql = "SELECT * FROM ".TB_PREF."grn_batch WHERE purch_order_no=$po_number";
-                               
+
        return db_query($sql, "The grns for the po $po_number could not be retreived");
 }
 
@@ -201,9 +219,9 @@ function get_po_grns($po_number)
 function exists_grn($grn_batch)
 {
        $sql = "SELECT id FROM ".TB_PREF."grn_batch WHERE id=$grn_batch";
-       $result = db_query($sql, "Cannot retreive a grn");      
-       
-    return (db_num_rows($result) > 0); 
+       $result = db_query($sql, "Cannot retreive a grn");
+
+    return (db_num_rows($result) > 0);
 }
 
 //----------------------------------------------------------------------------------------------------------
@@ -215,8 +233,8 @@ function exists_grn_on_invoices($grn_batch)
                AND quantity != 0
                AND grn_batch_id=$grn_batch";
        $result = db_query($sql, "Cannot query GRNs");
-    
-    return (db_num_rows($result) > 0);                                 
+
+    return (db_num_rows($result) > 0);
 }
 
 //----------------------------------------------------------------------------------------------------------
@@ -227,40 +245,40 @@ function void_grn($grn_batch)
        // can't be voided
        if (exists_grn_on_invoices($grn_batch))
                return false;
-               
+
        begin_transaction();
-               
+
        void_bank_trans(25, $grn_batch, true);
-       void_gl_trans(25, $grn_batch, true);    
-               
+       void_gl_trans(25, $grn_batch, true);
+
        // clear the quantities of the grn items in the POs and invoices
        $result = get_grn_items($grn_batch);
-       
-    if (db_num_rows($result) > 0) 
+
+    if (db_num_rows($result) > 0)
     {
-    
-       while ($myrow = db_fetch($result)) 
+
+       while ($myrow = db_fetch($result))
         {
-               
-               $sql = "UPDATE ".TB_PREF."purch_order_details 
-                SET quantity_received = quantity_received - " . $myrow["qty_recd"] . " 
+
+               $sql = "UPDATE ".TB_PREF."purch_order_details
+                SET quantity_received = quantity_received - " . $myrow["qty_recd"] . "
                 WHERE po_detail_item = " . $myrow["po_detail_item"];
-                
+
                db_query($sql, "a purchase order details record could not be voided.");
         }
-    }  
-       
-       // clear the quantities in the grn items                
-       $sql = "UPDATE ".TB_PREF."grn_items SET qty_recd=0, quantity_inv=0 
+    }
+
+       // clear the quantities in the grn items
+       $sql = "UPDATE ".TB_PREF."grn_items SET qty_recd=0, quantity_inv=0
                WHERE grn_batch_id=$grn_batch";
-                               
-       db_query($sql, "A grn detail item could not be voided.");                               
-                               
+
+       db_query($sql, "A grn detail item could not be voided.");
+
     // clear the stock move items
-    void_stock_move(25, $grn_batch);   
-    
+    void_stock_move(25, $grn_batch);
+
        commit_transaction();
-       
+
        return true;
 }