$InsWOResult = DB_query("INSERT INTO workorders (wo,\r\n\t\t\t\t\t\t\t\t\t\t\t\t loccode,\r\n\t\t\t\t\t\t\t\t\t\t\t\t requiredby,\r\n\t\t\t\t\t\t\t\t\t\t\t\t startdate)\r\n\t\t\t\t\t\t\t\t VALUES (" . $WONo . ",\r\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['DefaultFactoryLocation'] . "',\r\n\t\t\t\t\t\t\t\t\t\t'" . Date('Y-m-d') . "',\r\n\t\t\t\t\t\t\t\t\t\t'" . Date('Y-m-d') . "')", $db, $ErrMsg, $DbgMsg, true); //Need to get the latest BOM to roll up cost $CostResult = DB_query("SELECT SUM((materialcost+labourcost+overheadcost)*bom.quantity) AS cost\r\n\t\t\t\t\t\t\t\t\t\t\t\t\tFROM stockmaster INNER JOIN bom\r\n\t\t\t\t\t\t\t\t\t\t\t\t\tON stockmaster.stockid=bom.component\r\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE bom.parent='" . $StockItem->StockID . "'\r\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND bom.loccode='" . $_SESSION['DefaultFactoryLocation'] . "'", $db); $CostRow = DB_fetch_row($CostResult); if (is_null($CostRow[0]) or $CostRow[0] == 0) { $Cost = 0; prnMsg(_('In automatically creating a work order for') . ' ' . $StockItem->StockID . ' ' . _('an item on this sales order, the cost of this item as accumulated from the sum of the component costs is nil. This could be because there is no bill of material set up ... you may wish to double check this'), 'warn'); } else { $Cost = $CostRow[0]; } // insert parent item info $sql = "INSERT INTO woitems (wo,\r\n\t\t\t\t\t\t\t\t\t\t\t stockid,\r\n\t\t\t\t\t\t\t\t\t\t\t qtyreqd,\r\n\t\t\t\t\t\t\t\t\t\t\t stdcost)\r\n\t\t\t\t\t\t\t\t VALUES ( " . $WONo . ",\r\n\t\t\t\t\t\t\t\t\t\t '" . $StockItem->StockID . "',\r\n\t\t\t\t\t\t\t\t\t\t " . $WOQuantity . ",\r\n\t\t\t\t\t\t\t\t\t\t " . $Cost . ")"; $ErrMsg = _('The work order item could not be added'); $result = DB_query($sql, $db, $ErrMsg, $DbgMsg, true); //Recursively insert real component requirements - see includes/SQL_CommonFunctions.in for function WoRealRequirements WoRealRequirements($db, $WONo, $_SESSION['DefaultFactoryLocation'], $StockItem->StockID); $FactoryManagerEmail = _('A new work order has been created for') . ":\n" . $StockItem->StockID . ' - ' . $StockItem->Descr . ' x ' . $WOQuantity . ' ' . $StockItem->UOM . "\n" . _('These are for') . ' ' . $_SESSION['Items' . $identifier]->CustomerName . ' ' . _('there order ref') . ': ' . $_SESSION['Items' . $identifier]->CustRef . ' ' . _('our order number') . ': ' . $OrderNo; if ($StockItem->Serialised and $StockItem->NextSerialNo > 0) { //then we must create the serial numbers for the new WO also $FactoryManagerEmail .= "\n" . _('The following serial numbers have been reserved for this work order') . ':'; for ($i = 0; $i < $WOQuantity; $i++) { $result = DB_query("SELECT serialno FROM stockserialitems\r\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE serialno='" . ($StockItem->NextSerialNo + $i) . "'\r\n\t\t\t\t\t\t\t\t\t\t\t\tAND stockid='" . $StockItem->StockID . "'", $db); if (DB_num_rows($result) != 0) { $WOQuantity++; prnMsg($StockItem->NextSerialNo + $i . ': ' . _('This automatically generated serial number already exists - it cannot be added to the work order'), 'error'); } else { $sql = 'INSERT INTO woserialnos (wo, stockid, serialno) VALUES (' . $WONo . ",\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'" . $StockItem->StockID . "',\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" . ($StockItem->NextSerialNo + $i) . ')'; $ErrMsg = _('The serial number for the work order item could not be added');
$Cost = 0; prnMsg(_('In automatically creating a work order for') . ' ' . $ContractRow['contractref'] . ' ' . _('an item on this sales order, the cost of this item as accumulated from the sum of the component costs is nil. This could be because there is no bill of material set up ... you may wish to double check this'), 'warn'); } else { $Cost = $CostRow[0]; //cost of contract BOM } $CostResult = DB_query("SELECT SUM(costperunit*quantity) AS cost\n\t\t\t\t\t\t\t\t\tFROM contractreqts\n\t\t\t\t\t\t\t\t\tWHERE contractreqts.contractref='" . $ContractRow['contractref'] . "'", $db); $CostRow = DB_fetch_row($CostResult); //add other requirements cost to cost of contract BOM $Cost += $CostRow[0]; // insert parent item info $sql = "INSERT INTO woitems (wo,\n\t\t\t\t\t\t\t\t\t\t stockid,\n\t\t\t\t\t\t\t\t\t\t qtyreqd,\n\t\t\t\t\t\t\t\t\t\t stdcost)\n\t\t\t\t\t\t\t VALUES ( '" . $WONo . "',\n\t\t\t\t\t\t\t\t\t '" . $ContractRow['contractref'] . "',\n\t\t\t\t\t\t\t\t\t '1',\n\t\t\t\t\t\t\t\t\t '" . $Cost . "')"; $ErrMsg = _('The work order item could not be added'); $result = DB_query($sql, $db, $ErrMsg, $DbgMsg, true); //Recursively insert real component requirements - see includes/SQL_CommonFunctions.in for function WoRealRequirements WoRealRequirements($db, $WONo, $_SESSION['Items' . $identifier]->Location, $ContractRow['contractref']); } //end processing if the order was a contract quotation being changed to an order } //end test to see if the order was a contract quotation being changed to an order $HeaderSQL = "UPDATE salesorders SET debtorno = '" . $_SESSION['Items' . $identifier]->DebtorNo . "',\n\t\t\t\t\t\t\t\t\t\tbranchcode = '" . $_SESSION['Items' . $identifier]->Branch . "',\n\t\t\t\t\t\t\t\t\t\tcustomerref = '" . DB_escape_string($_SESSION['Items' . $identifier]->CustRef) . "',\n\t\t\t\t\t\t\t\t\t\tcomments = '" . DB_escape_string($_SESSION['Items' . $identifier]->Comments) . "',\n\t\t\t\t\t\t\t\t\t\tordertype = '" . $_SESSION['Items' . $identifier]->DefaultSalesType . "',\n\t\t\t\t\t\t\t\t\t\tshipvia = '" . $_POST['ShipVia'] . "',\n\t\t\t\t\t\t\t\t\t\tdeliverydate = '" . FormatDateForSQL(DB_escape_string($_SESSION['Items' . $identifier]->DeliveryDate)) . "',\n\t\t\t\t\t\t\t\t\t\tquotedate = '" . FormatDateForSQL(DB_escape_string($_SESSION['Items' . $identifier]->QuoteDate)) . "',\n\t\t\t\t\t\t\t\t\t\tconfirmeddate = '" . FormatDateForSQL(DB_escape_string($_SESSION['Items' . $identifier]->ConfirmedDate)) . "',\n\t\t\t\t\t\t\t\t\t\tdeliverto = '" . DB_escape_string($_SESSION['Items' . $identifier]->DeliverTo) . "',\n\t\t\t\t\t\t\t\t\t\tdeladd1 = '" . DB_escape_string($_SESSION['Items' . $identifier]->DelAdd1) . "',\n\t\t\t\t\t\t\t\t\t\tdeladd2 = '" . DB_escape_string($_SESSION['Items' . $identifier]->DelAdd2) . "',\n\t\t\t\t\t\t\t\t\t\tdeladd3 = '" . DB_escape_string($_SESSION['Items' . $identifier]->DelAdd3) . "',\n\t\t\t\t\t\t\t\t\t\tdeladd4 = '" . DB_escape_string($_SESSION['Items' . $identifier]->DelAdd4) . "',\n\t\t\t\t\t\t\t\t\t\tdeladd5 = '" . DB_escape_string($_SESSION['Items' . $identifier]->DelAdd5) . "',\n\t\t\t\t\t\t\t\t\t\tdeladd6 = '" . DB_escape_string($_SESSION['Items' . $identifier]->DelAdd6) . "',\n\t\t\t\t\t\t\t\t\t\tcontactphone = '" . $_SESSION['Items' . $identifier]->PhoneNo . "',\n\t\t\t\t\t\t\t\t\t\tcontactemail = '" . $_SESSION['Items' . $identifier]->Email . "',\n\t\t\t\t\t\t\t\t\t\tsalesperson = '" . $_SESSION['Items' . $identifier]->SalesPerson . "',\n\t\t\t\t\t\t\t\t\t\tfreightcost = '" . $_SESSION['Items' . $identifier]->FreightCost . "',\n\t\t\t\t\t\t\t\t\t\tfromstkloc = '" . $_SESSION['Items' . $identifier]->Location . "',\n\t\t\t\t\t\t\t\t\t\tprintedpackingslip = '" . $_POST['ReprintPackingSlip'] . "',\n\t\t\t\t\t\t\t\t\t\tquotation = '" . $_SESSION['Items' . $identifier]->Quotation . "',\n\t\t\t\t\t\t\t\t\t\tdeliverblind = '" . $_SESSION['Items' . $identifier]->DeliverBlind . "'\n\t\t\t\t\t\tWHERE salesorders.orderno='" . $_SESSION['ExistingOrder' . $identifier] . "'"; $DbgMsg = _('The SQL that was used to update the order and failed was'); $ErrMsg = _('The order cannot be updated because'); $InsertQryResult = DB_query($HeaderSQL, $db, $ErrMsg, $DbgMsg, true); foreach ($_SESSION['Items' . $identifier]->LineItems as $StockItem) { /* Check to see if the quantity reduced to the same quantity as already invoiced - so should set the line to completed */ if ($StockItem->Quantity == $StockItem->QtyInv) { $Completed = 1; } else { /* order line is not complete */
} $SQLReceivedDate = FormatDateForSQL($_POST['ReceivedDate']); $StockGLCode = GetStockGLCode($_POST['StockID'], $db); //Recalculate the standard for the item if there were no items previously received against the work order if ($WORow['qtyrecd'] == 0) { $CostResult = DB_query("SELECT SUM((materialcost+labourcost+overheadcost)*bom.quantity) AS cost\n\t\t\t\t\t\t\t\t\tFROM stockmaster INNER JOIN bom\n\t\t\t\t\t\t\t\t\tON stockmaster.stockid=bom.component\n\t\t\t\t\t\t\t\t\tWHERE bom.parent='" . $_POST['StockID'] . "'\n\t\t\t\t\t\t\t\t\tAND bom.loccode='" . $WORow['loccode'] . "'", $db); $CostRow = DB_fetch_array($CostResult); if (is_null($CostRow['cost']) or $CostRow['cost'] == 0) { $Cost = 0; } else { $Cost = $CostRow['cost']; } //Need to refresh the worequirments with the bom components now incase they changed $DelWORequirements = DB_query("DELETE FROM worequirements\n\t\t\t\t\t\t\t\t\t\t\tWHERE wo='" . $_POST['WO'] . "'\n\t\t\t\t\t\t\t\t\t\t\tAND parentstockid='" . $_POST['StockID'] . "'", $db); //Recursively insert real component requirements WoRealRequirements($db, $_POST['WO'], $WORow['loccode'], $_POST['StockID']); //Need to check this against the current standard cost and do a cost update if necessary $sql = "SELECT materialcost+labourcost+overheadcost AS cost,\n\t\t\t\t\t\t sum(quantity) AS totalqoh,\n\t\t\t\t\t\t labourcost,\n\t\t\t\t\t\t overheadcost\n\t\t\t\t\tFROM stockmaster INNER JOIN locstock\n\t\t\t\t\t\tON stockmaster.stockid=locstock.stockid\n\t\t\t\t\tWHERE stockmaster.stockid='" . $_POST['StockID'] . "'\n\t\t\t\t\tGROUP BY\n\t\t\t\t\t\tmaterialcost,\n\t\t\t\t\t\tlabourcost,\n\t\t\t\t\t\toverheadcost"; $ItemResult = DB_query($sql, $db); $ItemCostRow = DB_fetch_array($ItemResult); if ($Cost + $ItemCostRow['labourcost'] + $ItemCostRow['overheadcost'] != $ItemCostRow['cost']) { //the cost roll-up cost <> standard cost if ($_SESSION['CompanyRecord']['gllink_stock'] == 1 and $ItemCostRow['totalqoh'] != 0) { $CostUpdateNo = GetNextTransNo(35, $db); $PeriodNo = GetPeriod(Date($_SESSION['DefaultDateFormat']), $db); $ValueOfChange = $ItemCostRow['totalqoh'] * ($Cost + $ItemCostRow['labourcost'] + $ItemCostRow['overheadcost'] - $ItemCostRow['cost']); $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\tdefaulttag,\n\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\tVALUES (35,\n\t\t\t\t\t\t\t\t'" . $CostUpdateNo . "',\n\t\t\t\t\t\t\t\t'" . Date('Y-m-d') . "',\n\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t'" . $StockGLCode['adjglact'] . "',\n\t\t\t\t\t\t\t\t'" . $_SESSION['DefaultTag'] . "',\n\t\t\t\t\t\t\t\t'" . _('Cost roll on release of WO') . ': ' . $_POST['WO'] . ' - ' . $_POST['StockID'] . ' ' . _('cost was') . ' ' . $ItemCostRow['cost'] . ' ' . _('changed to') . ' ' . $Cost . ' x ' . _('Quantity on hand of') . ' ' . $ItemCostRow['totalqoh'] . "',\n\t\t\t\t\t\t\t\t'" . -$ValueOfChange . "')"; $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The GL credit for the stock cost adjustment posting could not be inserted because'); $DbgMsg = _('The following SQL to insert the GLTrans record was used'); $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true); $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\tdefaulttag,\n\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\tVALUES (35,\n\t\t\t\t\t\t\t\t'" . $CostUpdateNo . "',\n\t\t\t\t\t\t\t\t'" . Date('Y-m-d') . "',\n\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t'" . $StockGLCode['stockact'] . "',\n\t\t\t\t\t\t\t\t'" . $_SESSION['DefaultTag'] . "',\n\t\t\t\t\t\t\t\t'" . _('Cost roll on release of WO') . ': ' . $_POST['WO'] . ' - ' . $_POST['StockID'] . ' ' . _('cost was') . ' ' . $ItemCostRow['cost'] . ' ' . _('changed to') . ' ' . $Cost . ' x ' . _('Quantity on hand of') . ' ' . $ItemCostRow['totalqoh'] . "',\n\t\t\t\t\t\t\t\t'" . $ValueOfChange . "')";
if (is_null($CostRow['cost']) or $CostRow['cost'] == 0) { $Cost = 0; prnMsg(_('The cost of this item as accumulated from the sum of the component costs is nil. This could be because there is no bill of material set up ... you may wish to double check this'), 'warn'); } else { $Cost = $CostRow['cost']; } if (!isset($EOQ)) { $EOQ = 1; } $Result = DB_Txn_Begin(); // insert parent item info $SQL = "INSERT INTO woitems (wo,\n\t\t\t\t\t\t\t\t\t stockid,\n\t\t\t\t\t\t\t\t\t qtyreqd,\n\t\t\t\t\t\t\t\t\t stdcost)\n\t\t\t\t\t\t\t\tVALUES (\n\t\t\t\t\t\t\t\t\t '" . $_POST['WO'] . "',\n\t\t\t\t\t\t\t\t\t '" . $NewItem . "',\n\t\t\t\t\t\t\t\t\t '" . $EOQ . "',\n\t\t\t\t\t\t\t\t\t '" . $Cost . "'\n\t\t\t\t\t\t\t\t)"; $ErrMsg = _('The work order item could not be added'); $result = DB_query($SQL, $ErrMsg); //Recursively insert real component requirements - see includes/SQL_CommonFunctions.in for function WoRealRequirements WoRealRequirements($db, $_POST['WO'], $_POST['StockLocation'], $NewItem); $result = DB_Txn_Commit(); unset($NewItem); } //end if there were no input errors } //adding a new item to the work order if (isset($_POST['submit']) or isset($_POST['Search'])) { //The update button has been clicked echo '<div class="centre"><a href="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '">' . _('Enter a new work order') . '</a>'; echo '<br /><a href="' . $RootPath . '/SelectWorkOrder.php">' . _('Select an existing work order') . '</a>'; echo '<br /><a href="' . $RootPath . '/WorkOrderCosting.php?WO=' . $SelectedWO . '">' . _('Go to Costing') . '</a></div>'; $Input_Error = false; //hope for the best for ($i = 1; $i <= $_POST['NumberOfOutputs']; $i++) { if (!is_numeric($_POST['OutputQty' . $i])) {