コード例 #1
0
                $DbgMsg = _('The following SQL to insert the GLTrans record was used');
                $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
            }
        }
        /*end of if Sales and GL integrated */
        /*Update order header for invoice charged on */
        $SQL = "UPDATE salesorders SET comments = CONCAT(comments,' Inv ','" . $InvoiceNo . "') WHERE orderno= " . $OrderNo;
        $ErrMsg = _('CRITICAL ERROR') . ' ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The sales order header could not be updated with the invoice number');
        $DbgMsg = _('The following SQL to update the sales order was used');
        $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
        /*Now insert the DebtorTrans */
        $SQL = "INSERT INTO debtortrans (\n\t\t\t\ttransno,\n\t\t\t\ttype,\n\t\t\t\tdebtorno,\n\t\t\t\tbranchcode,\n\t\t\t\ttrandate,\n\t\t\t\tprd,\n\t\t\t\treference,\n\t\t\t\ttpe,\n\t\t\t\torder_,\n\t\t\t\tovamount,\n\t\t\t\tovgst,\n\t\t\t\tovfreight,\n\t\t\t\trate,\n\t\t\t\tinvtext,\n\t\t\t\tshipvia\n\t\t\t\t)\n\t\t\tVALUES (\n\t\t\t\t" . $InvoiceNo . ",\n\t\t\t\t10,\n\t\t\t\t'" . $RecurrOrderRow['debtorno'] . "',\n\t\t\t\t'" . $RecurrOrderRow['branchcode'] . "',\n\t\t\t\t'" . $DelDate . "',\n\t\t\t\t" . $PeriodNo . ",\n\t\t\t\t'" . $RecurrOrderRow['customerref'] . "',\n\t\t\t\t'" . $RecurrOrderRow['sales_type'] . "',\n\t\t\t\t" . $OrderNo . ",\n\t\t\t\t" . $TotalFXNetInvoice . ",\n\t\t\t\t" . $TotalFXTax . ",\n\t\t\t\t" . $RecurrOrderRow['freightcost'] . ",\n\t\t\t\t" . $CurrencyRate . ",\n\t\t\t\t'" . $RecurrOrderRow['comments'] . "',\n\t\t\t\t" . $RecurrOrderRow['shipvia'] . ")";
        $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The debtor transaction record could not be inserted because');
        $DbgMsg = _('The following SQL to insert the debtor transaction record was used');
        $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
        $DebtorTransID = DB_Last_Insert_ID($db, 'debtortrans', 'id');
        $SQL = 'INSERT INTO debtortranstaxes (debtortransid,
							taxauthid,
							taxamount)
				VALUES (' . $DebtorTransID . ',
					' . $TaxAuthID . ',
					' . $Tax['FXAmount'] / $CurrencyRate . ')';
        $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The debtor transaction taxes records could not be inserted because');
        $DbgMsg = _('The following SQL to insert the debtor transaction taxes record was used');
        $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
        $SQL = 'COMMIT';
        $Result = DB_query($SQL, $db);
        echo _('Invoice number') . ' ' . $InvoiceNo . ' ' . _('processed') . '<BR>';
        $EmailText .= "\n" . _('This recurring order was set to produce the invoice automatically on invoice number') . ' ' . $InvoiceNo;
    }
    /*end if the recurring order is set to auto invoice */
コード例 #2
0
                $OrderLine->StandardCost = 0;
            }
            if ($MBFlag == 'B' or $MBFlag == 'M') {
                $SQL = "INSERT INTO stockmoves (\r\n\t\t\t\t\t\tstockid,\r\n\t\t\t\t\t\ttype,\r\n\t\t\t\t\t\ttransno,\r\n\t\t\t\t\t\tloccode,\r\n\t\t\t\t\t\ttrandate,\r\n\t\t\t\t\t\tdebtorno,\r\n\t\t\t\t\t\tbranchcode,\r\n\t\t\t\t\t\tprice,\r\n\t\t\t\t\t\tprd,\r\n\t\t\t\t\t\treference,\r\n\t\t\t\t\t\tqty,\r\n\t\t\t\t\t\tdiscountpercent,\r\n\t\t\t\t\t\tstandardcost,\r\n\t\t\t\t\t\tnewqoh,\r\n\t\t\t\t\t\tnarrative )\r\n\t\t\t\t\tVALUES ('" . $OrderLine->StockID . "',\r\n\t\t\t\t\t\t10,\r\n\t\t\t\t\t\t" . $InvoiceNo . ",\r\n\t\t\t\t\t\t'" . $_SESSION['Items']->Location . "',\r\n\t\t\t\t\t\t'" . $DefaultDispatchDate . "',\r\n\t\t\t\t\t\t'" . $_SESSION['Items']->DebtorNo . "',\r\n\t\t\t\t\t\t'" . $_SESSION['Items']->Branch . "',\r\n\t\t\t\t\t\t" . $LocalCurrencyPrice . ",\r\n\t\t\t\t\t\t" . $PeriodNo . ",\r\n\t\t\t\t\t\t'" . $_SESSION['ProcessingOrder'] . "',\r\n\t\t\t\t\t\t" . -$OrderLine->QtyDispatched . ",\r\n\t\t\t\t\t\t" . $OrderLine->DiscountPercent . ",\r\n\t\t\t\t\t\t" . $OrderLine->StandardCost . ",\r\n\t\t\t\t\t\t" . ($QtyOnHandPrior - $OrderLine->QtyDispatched) . ",\r\n\t\t\t\t\t\t'" . DB_escape_string($OrderLine->Narrative) . "' )";
            } else {
                // its an assembly or dummy and assemblies/dummies always have nil stock (by definition they are made up at the time of dispatch  so new qty on hand will be nil
                if (empty($OrderLine->StandardCost)) {
                    $OrderLine->StandardCost = 0;
                }
                $SQL = "INSERT INTO stockmoves (\r\n\t\t\t\t\t\tstockid,\r\n\t\t\t\t\t\ttype,\r\n\t\t\t\t\t\ttransno,\r\n\t\t\t\t\t\tloccode,\r\n\t\t\t\t\t\ttrandate,\r\n\t\t\t\t\t\tdebtorno,\r\n\t\t\t\t\t\tbranchcode,\r\n\t\t\t\t\t\tprice,\r\n\t\t\t\t\t\tprd,\r\n\t\t\t\t\t\treference,\r\n\t\t\t\t\t\tqty,\r\n\t\t\t\t\t\tdiscountpercent,\r\n\t\t\t\t\t\tstandardcost,\r\n\t\t\t\t\t\tnarrative )\r\n\t\t\t\t\tVALUES ('" . $OrderLine->StockID . "',\r\n\t\t\t\t\t\t10,\r\n\t\t\t\t\t\t" . $InvoiceNo . ",\r\n\t\t\t\t\t\t'" . $_SESSION['Items']->Location . "',\r\n\t\t\t\t\t\t'" . $DefaultDispatchDate . "',\r\n\t\t\t\t\t\t'" . $_SESSION['Items']->DebtorNo . "',\r\n\t\t\t\t\t\t'" . $_SESSION['Items']->Branch . "',\r\n\t\t\t\t\t\t" . $LocalCurrencyPrice . ",\r\n\t\t\t\t\t\t" . $PeriodNo . ",\r\n\t\t\t\t\t\t'" . $_SESSION['ProcessingOrder'] . "',\r\n\t\t\t\t\t\t" . -$OrderLine->QtyDispatched . ",\r\n\t\t\t\t\t\t" . $OrderLine->DiscountPercent . ",\r\n\t\t\t\t\t\t" . $OrderLine->StandardCost . ",\r\n\t\t\t\t\t\t'" . DB_escape_string($OrderLine->Narrative) . "')";
            }
            $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Stock movement records could not be inserted because');
            $DbgMsg = _('The following SQL to insert the stock movement records was used');
            $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
            /*Get the ID of the StockMove... */
            $StkMoveNo = DB_Last_Insert_ID($db, 'stockmoves', 'stkmoveno');
            /*Insert the taxes that applied to this line */
            foreach ($OrderLine->Taxes as $Tax) {
                $SQL = 'INSERT INTO stockmovestaxes (stkmoveno,
									taxauthid,
									taxrate,
									taxcalculationorder,
									taxontax)
						VALUES (' . $StkMoveNo . ',
							' . $Tax->TaxAuthID . ',
							' . $Tax->TaxRate . ',
							' . $Tax->TaxCalculationOrder . ',
							' . $Tax->TaxOnTax . ')';
                $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Taxes and rates applicable to this invoice line item could not be inserted because');
                $DbgMsg = _('The following SQL to insert the stock movement tax detail records was used');
                $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
コード例 #3
0
function CreateCreditNote($Header, $LineDetails, $User, $Password)
{
    /* Create a customer credit note in KwaMoja.
     * Needs an associative array for the $Header
     * and an array of assocative arrays for the $LineDetails
     * $Header contains an associative array in the format:
     * Header['debtorno'] - the customer code
     * Header['branchcode']  - the branch code
     * Header['trandate'] - the date of the credit note
     * Header['tpe'] - the sales type
     * Header['fromstkloc'] - the inventory location where the stock is put back into
     * Header['customerref'] - the customer's reference
     * Header['shipvia'] - the shipper required by KwaMoja
     *
     * and $LineDetails contains an array of associative arrays of the format:
     *
     * $LineDetails[0]['stockid']
     * $LineDetails[0]['price']
     * $LineDetails[0]['qty'] - expected to be a negative quantity (a negative sale)
     * $LineDetails[0]['discountpercent']
     */
    $Errors = array();
    $db = db($User, $Password);
    if (gettype($db) == 'integer') {
        $Errors[0] = NoAuthorisation;
        return $Errors;
    }
    $Errors = VerifyDebtorExists($Header['debtorno'], sizeof($Errors), $Errors, $db);
    $Errors = VerifyBranchNoExists($Header['debtorno'], $Header['branchcode'], sizeof($Errors), $Errors, $db);
    /*Does not deal with serialised/lot track items - for use by POS */
    /*Get Company Defaults */
    $ReadCoyResult = api_DB_query("SELECT debtorsact,\n\t\t\t\t\t\t\t\t\t\t\t\tfreightact,\n\t\t\t\t\t\t\t\t\t\t\t\tgllink_debtors,\n\t\t\t\t\t\t\t\t\t\t\t\tgllink_stock\n\t\t\t\t\t\t\t\t\t\tFROM companies\n\t\t\t\t\t\t\t\t\t\tWHERE coycode=1", $db);
    $CompanyRecord = DB_fetch_array($ReadCoyResult);
    if (DB_error_no($db) != 0) {
        $Errors[] = NoCompanyRecord;
    }
    $HeaderSQL = "SELECT custbranch.area,\n\t\t\t\t\t\t\t custbranch.taxgroupid,\n\t\t\t\t\t\t\t debtorsmaster.currcode,\n\t\t\t\t\t\t\t rate,\n\t\t\t\t\t\t\t salesman\n\t\t\t\t\t\t\tFROM debtorsmaster\n\t\t\t\t\t\t\tINNER JOIN custbranch\n\t\t\t\t\t\t\tON debtorsmaster.debtorno = custbranch.debtorno\n\t\t\t\t\t\t\tINNER JOIN currencies\n\t\t\t\t\t\t\tON debtorsmaster.currcode=currencies.currabrev\n\t\t\t\t\t\t\tWHERE custbranch.debtorno = '" . $Header['debtorno'] . "'\n\t\t\t\t\t\t\tAND custbranch.branchcode='" . $Header['branchcode'] . "'";
    $HeaderResult = api_DB_query($HeaderSQL, $db);
    if (DB_error_no($db) != 0) {
        $Errors[] = NoReadCustomerBranch;
    }
    $CN_Header = DB_fetch_array($HeaderResult);
    $TaxProvResult = api_DB_query("SELECT taxprovinceid FROM locations WHERE loccode='" . $Header['fromstkloc'] . "'", $db);
    if (DB_error_no($db) != 0) {
        $Errors[] = NoTaxProvince;
    }
    $myrow = DB_fetch_row($TaxProvResult);
    $DispTaxProvinceID = $myrow[0];
    /*Start an SQL transaction */
    $result = DB_Txn_Begin($db);
    /*Now Get the next credit note number - function in SQL_CommonFunctions*/
    $CreditNoteNo = GetNextTransNo(11, $db);
    $PeriodNo = GetCurrentPeriod($db);
    $TotalFXNetCredit = 0;
    $TotalFXTax = 0;
    $TaxTotals = array();
    $LineCounter = 0;
    foreach ($LineDetails as $CN_Line) {
        $LineSQL = "SELECT taxcatid,\n\t\t\t\t\t\t\t\tmbflag,\n\t\t\t\t\t\t\t\tmaterialcost+labourcost+overheadcost AS standardcost\n\t\t\t\t\t\tFROM stockmaster\n\t\t\t\t\t\tWHERE stockid ='" . $CN_Line['stockid'] . "'";
        $LineResult = api_DB_query($LineSQL, $db);
        if (DB_error_no($db) != 0 or DB_num_rows($LineResult) == 0) {
            $Errors[] = NoReadItem;
            return $Errors;
        }
        $LineRow = DB_fetch_array($LineResult);
        $StandardCost = $LineRow['standardcost'];
        $LocalCurrencyPrice = $CN_Line['price'] * (1 - floatval($CN_Line['discountpercent'])) / $CN_Header['rate'];
        $LineNetAmount = $CN_Line['price'] * $CN_Line['qty'] * (1 - floatval($CN_Line['discountpercent']));
        /*Gets the Taxes and rates applicable to this line from the TaxGroup of the branch and TaxCategory of the item
        		and the taxprovince of the dispatch location */
        $SQL = "SELECT taxgrouptaxes.calculationorder,\n\t\t\t\t\t\t\ttaxauthorities.description,\n\t\t\t\t\t\t\ttaxgrouptaxes.taxauthid,\n\t\t\t\t\t\t\ttaxauthorities.taxglcode,\n\t\t\t\t\t\t\ttaxgrouptaxes.taxontax,\n\t\t\t\t\t\t\ttaxauthrates.taxrate\n\t\t\t\t\tFROM taxauthrates INNER JOIN taxgrouptaxes ON\n\t\t\t\t\t\ttaxauthrates.taxauthority=taxgrouptaxes.taxauthid\n\t\t\t\t\t\tINNER JOIN taxauthorities ON\n\t\t\t\t\t\ttaxauthrates.taxauthority=taxauthorities.taxid\n\t\t\t\t\tWHERE taxgrouptaxes.taxgroupid='" . $CN_Header['taxgroupid'] . "'\n\t\t\t\t\tAND taxauthrates.dispatchtaxprovince='" . $DispTaxProvinceID . "'\n\t\t\t\t\tAND taxauthrates.taxcatid = '" . $LineRow['taxcatid'] . "'\n\t\t\t\t\tORDER BY taxgrouptaxes.calculationorder";
        $GetTaxRatesResult = api_DB_query($SQL, $db);
        if (DB_error_no($db) != 0) {
            $Errors[] = TaxRatesFailed;
        }
        $LineTaxAmount = 0;
        while ($myrow = DB_fetch_array($GetTaxRatesResult)) {
            if (!isset($TaxTotals[$myrow['taxauthid']]['FXAmount'])) {
                $TaxTotals[$myrow['taxauthid']]['FXAmount'] = 0;
            }
            $TaxAuthID = $myrow['taxauthid'];
            $TaxTotals[$myrow['taxauthid']]['GLCode'] = $myrow['taxglcode'];
            $TaxTotals[$myrow['taxauthid']]['TaxRate'] = $myrow['taxrate'];
            $TaxTotals[$myrow['taxauthid']]['TaxAuthDescription'] = $myrow['description'];
            if ($myrow['taxontax'] == 1) {
                $TaxAuthAmount = ($LineNetAmount + $LineTaxAmount) * $myrow['taxrate'];
            } else {
                $TaxAuthAmount = $LineNetAmount * $myrow['taxrate'];
            }
            $TaxTotals[$myrow['taxauthid']]['FXAmount'] += $TaxAuthAmount;
            /*Make an array of the taxes and amounts including GLcodes for later posting - need debtortransid
            		so can only post once the debtor trans is posted - can only post debtor trans when all tax is calculated */
            $LineTaxes[$LineCounter][$myrow['calculationorder']] = array('TaxCalculationOrder' => $myrow['calculationorder'], 'TaxAuthID' => $myrow['taxauthid'], 'TaxAuthDescription' => $myrow['description'], 'TaxRate' => $myrow['taxrate'], 'TaxOnTax' => $myrow['taxontax'], 'TaxAuthAmount' => $TaxAuthAmount);
            $LineTaxAmount += $TaxAuthAmount;
        }
        //end loop around Taxes
        $TotalFXNetCredit += $LineNetAmount;
        $TotalFXTax += $LineTaxAmount;
        if ($LineRow['mbflag'] == 'B' or $LineRow['mbflag'] == 'M') {
            $Assembly = False;
            /* Need to get the current location quantity
            			will need it later for the stock movement */
            $SQL = "SELECT locstock.quantity\n\t\t\t\t\t\tFROM locstock\n\t\t\t\t\t\tWHERE locstock.stockid='" . $CN_Line['stockid'] . "'\n\t\t\t\t\t\tAND loccode= '" . $Header['fromstkloc'] . "'";
            $Result = api_DB_query($SQL, $db);
            if (DB_num_rows($Result) == 1) {
                $LocQtyRow = DB_fetch_row($Result);
                $QtyOnHandPrior = $LocQtyRow[0];
            } else {
                /* There must be some error this should never happen */
                $QtyOnHandPrior = 0;
            }
            $SQL = "UPDATE locstock\n\t\t\t\t\t\tSET quantity = locstock.quantity - " . $CN_Line['qty'] . "\n\t\t\t\t\t\tWHERE locstock.stockid = '" . $CN_Line['stockid'] . "'\n\t\t\t\t\t\tAND loccode = '" . $Header['fromstkloc'] . "'";
            $Result = api_DB_query($SQL, $db, '', '', true);
            $SQL = "INSERT INTO stockmoves (stockid,\n\t\t\t\t\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\t\t\t\t\ttransno,\n\t\t\t\t\t\t\t\t\t\t\t\tloccode,\n\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\t\t\tprice,\n\t\t\t\t\t\t\t\t\t\t\t\tprd,\n\t\t\t\t\t\t\t\t\t\t\t\treference,\n\t\t\t\t\t\t\t\t\t\t\t\tqty,\n\t\t\t\t\t\t\t\t\t\t\t\tdiscountpercent,\n\t\t\t\t\t\t\t\t\t\t\t\tstandardcost,\n\t\t\t\t\t\t\t\t\t\t\t\tnewqoh)\n\t\t\t\t\t\tVALUES ('" . $CN_Line['stockid'] . "',\n\t\t\t\t\t\t\t\t'11',\n\t\t\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t'" . $Header['fromstkloc'] . "',\n\t\t\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . "',\n\t\t\t\t\t\t\t\t'" . $Header['branchcode'] . "',\n\t\t\t\t\t\t\t\t'" . $LocalCurrencyPrice . "',\n\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t'" . $Header['customerref'] . "',\n\t\t\t\t\t\t\t\t'" . -$CN_Line['qty'] . "',\n\t\t\t\t\t\t\t\t'" . $CN_Line['discountpercent'] . "',\n\t\t\t\t\t\t\t\t'" . $StandardCost . "',\n\t\t\t\t\t\t\t\t'" . ($QtyOnHandPrior - $CN_Line['qty']) . "' )";
            $Result = api_DB_query($SQL, $db, '', '', true);
        } else {
            if ($LineRow['mbflag'] == 'A') {
                /* its an assembly */
                /*Need to get the BOM for this part and make
                		stock moves for the components then update the Location stock balances */
                $Assembly = True;
                $StandardCost = 0;
                /*To start with - accumulate the cost of the comoponents for use in journals later on */
                $SQL = "SELECT bom.component,\n\t\t\t\t\t\t\t\tbom.quantity,\n\t\t\t\t\t\t\t\tstockmaster.materialcost+stockmaster.labourcost+stockmaster.overheadcost AS standard\n\t\t\t\t\t\t\tFROM bom INNER JOIN stockmaster\n\t\t\t\t\t\t\tON bom.component=stockmaster.stockid\n\t\t\t\t\t\t\tWHERE bom.parent='" . $CN_Line['stockid'] . "'\n\t\t\t\t\t\t\tAND bom.effectiveto >= '" . Date('Y-m-d') . "'\n\t\t\t\t\t\t\tAND bom.effectiveafter < '" . Date('Y-m-d') . "'";
                $AssResult = api_DB_query($SQL, $db);
                while ($AssParts = DB_fetch_array($AssResult, $db)) {
                    $StandardCost += $AssParts['standard'] * $AssParts['quantity'];
                    /* Need to get the current location quantity
                    			will need it later for the stock movement */
                    $SQL = "SELECT locstock.quantity\n\t\t\t\t\t\t\tFROM locstock\n\t\t\t\t\t\t\tWHERE locstock.stockid='" . $AssParts['component'] . "'\n\t\t\t\t\t\t\tAND loccode= '" . $Header['fromstkloc'] . "'";
                    $Result = api_DB_query($SQL, $db);
                    if (DB_num_rows($Result) == 1) {
                        $LocQtyRow = DB_fetch_row($Result);
                        $QtyOnHandPrior = $LocQtyRow[0];
                    } else {
                        /*There must be some error this should never happen */
                        $QtyOnHandPrior = 0;
                    }
                    if (empty($AssParts['standard'])) {
                        $AssParts['standard'] = 0;
                    }
                    $SQL = "INSERT INTO stockmoves (stockid,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttransno,\n\t\t\t\t\t\t\t\t\t\t\t\t\tloccode,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\t\t\t\tprd,\n\t\t\t\t\t\t\t\t\t\t\t\t\treference,\n\t\t\t\t\t\t\t\t\t\t\t\t\tqty,\n\t\t\t\t\t\t\t\t\t\t\t\t\tstandardcost,\n\t\t\t\t\t\t\t\t\t\t\t\t\tshow_on_inv_crds,\n\t\t\t\t\t\t\t\t\t\t\t\t\tnewqoh)\n\t\t\t\t\t\t\t\t\t\tVALUES ('" . $AssParts['component'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t 11,\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $Header['fromstkloc'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $Header['debtorno'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $Header['branchcode'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . _('Assembly') . ': ' . $CN_Line['stockid'] . ' ' . $Header['customerref'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . -$AssParts['quantity'] * $CN_Line['qty'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $AssParts['standard'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t 0,\n\t\t\t\t\t\t\t\t\t\t\t\t '" . ($QtyOnHandPrior - $AssParts['quantity'] * $CN_Line['qty']) . "'\t)";
                    $Result = DB_query($SQL, $db, '', '', true);
                    $SQL = "UPDATE locstock\n\t\t\t\t\t\t\tSET quantity = locstock.quantity - " . $AssParts['quantity'] * $CN_Line['qty'] . "\n\t\t\t\t\t\t\tWHERE locstock.stockid = '" . $AssParts['component'] . "'\n\t\t\t\t\t\t\tAND loccode = '" . $Header['fromlocstk'] . "'";
                    $Result = DB_query($SQL, $db, '', '', true);
                }
                /* end of assembly explosion and updates */
            }
        }
        /* end of its an assembly */
        if ($LineRow['mbflag'] == 'A' or $LineRow['mbflag'] == 'D') {
            /*it's a Dummy/Service item or an Assembly item - still need stock movement record
             * but quantites on hand are always nil */
            $SQL = "INSERT INTO stockmoves (stockid,\n\t\t\t\t\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\t\t\t\t\ttransno,\n\t\t\t\t\t\t\t\t\t\t\t\tloccode,\n\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\t\t\tprice,\n\t\t\t\t\t\t\t\t\t\t\t\tprd,\n\t\t\t\t\t\t\t\t\t\t\t\treference,\n\t\t\t\t\t\t\t\t\t\t\t\tqty,\n\t\t\t\t\t\t\t\t\t\t\t\tdiscountpercent,\n\t\t\t\t\t\t\t\t\t\t\t\tstandardcost,\n\t\t\t\t\t\t\t\t\t\t\t\tnewqoh)\n\t\t\t\t\t\tVALUES ('" . $CN_Line['stockid'] . "',\n\t\t\t\t\t\t\t\t'11',\n\t\t\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t'" . $Header['fromstkloc'] . "',\n\t\t\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . "',\n\t\t\t\t\t\t\t\t'" . $Header['branchcode'] . "',\n\t\t\t\t\t\t\t\t'" . $LocalCurrencyPrice . "',\n\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t'" . $Header['customerref'] . "',\n\t\t\t\t\t\t\t\t'" . -$CN_Line['qty'] . "',\n\t\t\t\t\t\t\t\t'" . $CN_Line['discountpercent'] . "',\n\t\t\t\t\t\t\t\t'" . $StandardCost . "',\n\t\t\t\t\t\t\t\t'0' )";
            $Result = api_DB_query($SQL, $db, '', '', true);
        }
        /*Get the ID of the StockMove... */
        $StkMoveNo = DB_Last_Insert_ID($db, 'stockmoves', 'stkmoveno');
        /*Insert the taxes that applied to this line */
        foreach ($LineTaxes[$LineCounter] as $Tax) {
            $SQL = "INSERT INTO stockmovestaxes (stkmoveno,\n\t\t\t\t\t\t\t\t\ttaxauthid,\n\t\t\t\t\t\t\t\t\ttaxrate,\n\t\t\t\t\t\t\t\t\ttaxcalculationorder,\n\t\t\t\t\t\t\t\t\ttaxontax)\n\t\t\t\t\t\tVALUES ('" . $StkMoveNo . "',\n\t\t\t\t\t\t\t'" . $Tax['TaxAuthID'] . "',\n\t\t\t\t\t\t\t'" . $Tax['TaxRate'] . "',\n\t\t\t\t\t\t\t'" . $Tax['TaxCalculationOrder'] . "',\n\t\t\t\t\t\t\t'" . $Tax['TaxOnTax'] . "')";
            $Result = DB_query($SQL, $db, '', '', true);
        }
        /*Insert Sales Analysis records */
        $SQL = "SELECT COUNT(*),\n\t\t\t\t\t\tsalesanalysis.stkcategory,\n\t\t\t\t\t\tsalesanalysis.area,\n\t\t\t\t\t\tsalesanalysis.salesperson,\n\t\t\t\t\t\tsalesanalysis.periodno,\n\t\t\t\t\t\tsalesanalysis.typeabbrev,\n\t\t\t\t\t\tsalesanalysis.cust,\n\t\t\t\t\t\tsalesanalysis.custbranch,\n\t\t\t\t\t\tsalesanalysis.stockid\n\t\t\t\t\tFROM salesanalysis,\n\t\t\t\t\t\tcustbranch,\n\t\t\t\t\t\tstockmaster\n\t\t\t\t\tWHERE salesanalysis.stkcategory=stockmaster.categoryid\n\t\t\t\t\tAND salesanalysis.stockid=stockmaster.stockid\n\t\t\t\t\tAND salesanalysis.cust=custbranch.debtorno\n\t\t\t\t\tAND salesanalysis.custbranch=custbranch.branchcode\n\t\t\t\t\tAND salesanalysis.area=custbranch.area\n\t\t\t\t\tAND salesanalysis.salesperson=custbranch.salesman\n\t\t\t\t\tAND salesanalysis.typeabbrev ='" . $Header['tpe'] . "'\n\t\t\t\t\tAND salesanalysis.periodno='" . $PeriodNo . "'\n\t\t\t\t\tAND salesanalysis.cust " . LIKE . "  '" . $Header['debtorno'] . "'\n\t\t\t\t\tAND salesanalysis.custbranch  " . LIKE . " '" . $Header['branchcode'] . "'\n\t\t\t\t\tAND salesanalysis.stockid  " . LIKE . " '" . $CN_Line['stockid'] . "'\n\t\t\t\t\tAND salesanalysis.budgetoractual='1'\n\t\t\t\t\tGROUP BY salesanalysis.stockid,\n\t\t\t\t\t\tsalesanalysis.stkcategory,\n\t\t\t\t\t\tsalesanalysis.cust,\n\t\t\t\t\t\tsalesanalysis.custbranch,\n\t\t\t\t\t\tsalesanalysis.area,\n\t\t\t\t\t\tsalesanalysis.periodno,\n\t\t\t\t\t\tsalesanalysis.typeabbrev,\n\t\t\t\t\t\tsalesanalysis.salesperson";
        $Result = api_DB_query($SQL, $db, '', '', true);
        $myrow = DB_fetch_row($Result);
        if ($myrow[0] > 0) {
            /*Update the existing record that already exists */
            $SQL = "UPDATE salesanalysis\n\t\t\t\t\t\tSET amt=amt+" . $CN_Line['price'] * $CN_Line['qty'] / $CN_Header['rate'] . ",\n\t\t\t\t\t\tqty=qty +" . $CN_Line['qty'] . ",\n\t\t\t\t\t\tdisc=disc+" . $CN_Line['discountpercent'] * $CN_Line['price'] * $CN_Line['qty'] / $CN_Header['rate'] . "\n\t\t\t\t\t\tWHERE salesanalysis.area='" . $myrow[2] . "'\n\t\t\t\t\t\tAND salesanalysis.salesperson='" . $myrow[3] . "'\n\t\t\t\t\t\tAND typeabbrev ='" . $Header['tpe'] . "'\n\t\t\t\t\t\tAND periodno = '" . $PeriodNo . "'\n\t\t\t\t\t\tAND cust  " . LIKE . " '" . $Header['debtorno'] . "'\n\t\t\t\t\t\tAND custbranch  " . LIKE . "  '" . $Header['branchcode'] . "'\n\t\t\t\t\t\tAND stockid  " . LIKE . " '" . $CN_Line['stockid'] . "'\n\t\t\t\t\t\tAND salesanalysis.stkcategory ='" . $myrow[1] . "'\n\t\t\t\t\t\tAND budgetoractual='1'";
        } else {
            /* insert a new sales analysis record */
            $SQL = "INSERT INTO salesanalysis (\ttypeabbrev,\n\t\t\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\t\t\tamt,\n\t\t\t\t\t\t\t\t\t\t\t\t\tcost,\n\t\t\t\t\t\t\t\t\t\t\t\t\tcust,\n\t\t\t\t\t\t\t\t\t\t\t\t\tcustbranch,\n\t\t\t\t\t\t\t\t\t\t\t\t\tqty,\n\t\t\t\t\t\t\t\t\t\t\t\t\tdisc,\n\t\t\t\t\t\t\t\t\t\t\t\t\tstockid,\n\t\t\t\t\t\t\t\t\t\t\t\t\tarea,\n\t\t\t\t\t\t\t\t\t\t\t\t\tbudgetoractual,\n\t\t\t\t\t\t\t\t\t\t\t\t\tsalesperson,\n\t\t\t\t\t\t\t\t\t\t\t\t\tstkcategory )\n\t\t\t\t\t\t\t\tSELECT '" . $Header['tpe'] . "',\n\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t'" . $CN_Line['price'] * $CN_Line['qty'] / $CN_Header['rate'] . "',\n\t\t\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . "',\n\t\t\t\t\t\t\t\t\t'" . $Header['branchcode'] . "',\n\t\t\t\t\t\t\t\t\t'" . $CN_Line['qty'] . "',\n\t\t\t\t\t\t\t\t\t'" . $CN_Line['discountpercent'] * $CN_Line['price'] * $CN_Line['qty'] / $CN_Header['rate'] . "',\n\t\t\t\t\t\t\t\t\t'" . $CN_Line['stockid'] . "',\n\t\t\t\t\t\t\t\t\tcustbranch.area,\n\t\t\t\t\t\t\t\t\t1,\n\t\t\t\t\t\t\t\t\tcustbranch.salesman,\n\t\t\t\t\t\t\t\t\tstockmaster.categoryid\n\t\t\t\t\t\t\t\tFROM stockmaster, custbranch\n\t\t\t\t\t\t\t\tWHERE stockmaster.stockid = '" . $CN_Line['stockid'] . "'\n\t\t\t\t\t\t\t\tAND custbranch.debtorno = '" . $Header['debtorno'] . "'\n\t\t\t\t\t\t\t\tAND custbranch.branchcode='" . $Header['branchcode'] . "'";
        }
        $Result = api_DB_query($SQL, $db, '', '', true);
        if ($CompanyRecord['gllink_stock'] == 1 and $StandardCost != 0) {
            /*first the cost of sales entry - GL accounts are retrieved using the function GetCOGSGLAccount from includes/GetSalesTransGLCodes.inc  */
            $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\t\t\tVALUES (11,\n\t\t\t\t\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . GetCOGSGLAccount($CN_Header['area'], $CN_Line['stockid'], $Header['tpe'], $db) . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . " - " . $CN_Line['stockid'] . " x " . $CN_Line['qty'] . " @ " . $StandardCost . "',\n\t\t\t\t\t\t\t\t\t\t'" . $StandardCost * $CN_Line['qty'] . "')";
            $Result = api_DB_query($SQL, $db, '', '', true);
            /*now the stock entry - this is set to the cost act in the case of a fixed asset disposal */
            $StockGLCode = GetStockGLCode($CN_Line['stockid'], $db);
            $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\t\t\tVALUES (11,\n\t\t\t\t\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $StockGLCode['stockact'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . " - " . $CN_Line['stockid'] . " x " . $CN_Line['qty'] . " @ " . $StandardCost . "',\n\t\t\t\t\t\t\t\t\t\t'" . -$StandardCost * $CN_Line['qty'] . "')";
            $Result = api_DB_query($SQL, $db, '', '', true);
        }
        /* end of if GL and stock integrated and standard cost !=0  and not an asset */
        if ($CompanyRecord['gllink_debtors'] == 1 and $CN_Line['price'] != 0) {
            //Post sales transaction to GL credit sales
            $SalesGLAccounts = GetSalesGLAccount($CN_Header['area'], $CN_Line['stockid'], $Header['tpe'], $db);
            $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\tamount )\n\t\t\t\t\tVALUES ('11',\n\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t'" . $SalesGLAccounts['salesglcode'] . "',\n\t\t\t\t\t\t'" . $Header['debtorno'] . " - " . $CN_Line['stockid'] . " x " . $CN_Line['qty'] . " @ " . $CN_Line['price'] . "',\n\t\t\t\t\t\t'" . -$CN_Line['price'] * $CN_Line['qty'] / $CN_Header['rate'] . "'\n\t\t\t\t\t)";
            $Result = api_DB_query($SQL, $db, '', '', true);
            if ($CN_Line['discountpercent'] != 0) {
                $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\tVALUES (11,\n\t\t\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t'" . $SalesGLAccounts['discountglcode'] . "',\n\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . " - " . $CN_Line['stockid'] . " @ " . $CN_Line['discountpercent'] * 100 . "%',\n\t\t\t\t\t\t\t\t'" . $CN_Line['price'] * $CN_Line['qty'] * $CN_Line['discountpercent'] / $CN_Header['rate'] . "')";
                $Result = DB_query($SQL, $db, '', '', true);
            }
            /*end of if discount !=0 */
        }
        /*end of if sales integrated with gl */
        $LineCounter++;
        //needed for the array of taxes by line
    }
    /*end of OrderLine loop */
    $TotalCreditLocalCurr = ($TotalFXNetCredit + $TotalFXTax) / $CN_Header['rate'];
    if ($CompanyRecord['gllink_debtors'] == 1) {
        /*Now post the tax to the GL at local currency equivalent */
        if ($CompanyRecord['gllink_debtors'] == 1 and $TaxAuthAmount != 0) {
            /*Loop through the tax authorities array to post each total to the taxauth glcode */
            foreach ($TaxTotals as $Tax) {
                $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\t\tamount )\n\t\t\t\t\t\t\t\t\t\t\tVALUES (11,\n\t\t\t\t\t\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $Tax['GLCode'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . "-" . $Tax['TaxAuthDescription'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . -$Tax['FXAmount'] / $CN_Header['rate'] . "' )";
                $Result = api_DB_query($SQL, $db, '', '', true);
            }
        }
        /*Post debtors transaction to GL credit debtors, and debit sales */
        if ($TotalCreditLocalCurr != 0) {
            $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\t\t\tVALUES ('11',\n\t\t\t\t\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $CompanyRecord['debtorsact'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $TotalCreditLocalCurr . "')";
            $Result = api_DB_query($SQL, $db, '', '', true);
        }
        EnsureGLEntriesBalance(11, $CreditNoteNo, $db);
    }
    /*end of if Sales and GL integrated */
    /*Now insert the DebtorTrans */
    $SQL = "INSERT INTO debtortrans (transno,\n\t\t\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\tinputdate,\n\t\t\t\t\t\t\t\t\t\tprd,\n\t\t\t\t\t\t\t\t\t\treference,\n\t\t\t\t\t\t\t\t\t\ttpe,\n\t\t\t\t\t\t\t\t\t\tovamount,\n\t\t\t\t\t\t\t\t\t\tovgst,\n\t\t\t\t\t\t\t\t\t\trate,\n\t\t\t\t\t\t\t\t\t\tshipvia)\n\t\t\t\t\t\t\t\t\tVALUES (\n\t\t\t\t\t\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t\t\t\t\t11,\n\t\t\t\t\t\t\t\t\t\t'" . $Header['debtorno'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['branchcode'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['trandate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . date('Y-m-d H-i-s') . "',\n\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['customerref'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['tpe'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $TotalFXNetCredit . "',\n\t\t\t\t\t\t\t\t\t\t'" . $TotalFXTax . "',\n\t\t\t\t\t\t\t\t\t\t'" . $CN_Header['rate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Header['shipvia'] . "')";
    $Result = api_DB_query($SQL, $db, '', '', true);
    $DebtorTransID = DB_Last_Insert_ID($db, 'debtortrans', 'id');
    /*for each Tax - need to insert into debtortranstaxes */
    foreach ($TaxTotals as $TaxAuthID => $Tax) {
        $SQL = "INSERT INTO debtortranstaxes (debtortransid,\n\t\t\t\t\t\t\t\t\t\t\t\ttaxauthid,\n\t\t\t\t\t\t\t\t\t\t\t\ttaxamount)\n\t\t\t\t\t\t\t\tVALUES ('" . $DebtorTransID . "',\n\t\t\t\t\t\t\t\t\t\t'" . $TaxAuthID . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Tax['FXAmount'] / $CN_Header['rate'] . "')";
        $Result = api_DB_query($SQL, $db, '', '', true);
    }
    #Now figure out if there was an invoice in the same POS transaction to allocate against?
    $SQL = "SELECT id,\n\t\t\t\t\tovamount+ovgst AS total,\n\t\t\t\t\talloc\n\t\t\t\tFROM debtortrans\n\t\t\t\tWHERE customerref='" . $Header['customerref'] . "'\n\t\t\t\tAND type=10\n\t\t\t\tAND settled=0";
    $Result = api_DB_query($SQL, $db, '', '', true);
    $TotalCreditFX = $TotalFXNetCredit + $TotalFXTax;
    #Should be negative number
    $Allocated = 0;
    if (DB_num_rows($Result) > 0) {
        while ($InvoiceRow = DB_fetch_array($Result) and $Allocated > $TotalCreditFX) {
            if ($InvoiceRow['total'] - $InvoiceRow['alloc'] + $TotalCreditFX - $Allocated > 0) {
                /*Then we can allocate all of the (remaining) credit against this invoice */
                $AllocateAmount = $InvoiceRow['total'] - $InvoiceRow['alloc'] + $TotalCreditFX - $Allocated;
            } elseif ($InvoiceRow['total'] - $InvoiceRow['alloc'] - $Allocated > 0) {
                $AllocateAmount = $InvoiceRow['total'] - $InvoiceRow['alloc'] - $Allocated;
            } else {
                $AllocateAmount = 0;
            }
            if ($AllocateAmount > 0) {
                $SQL = "INSERT INTO\tcustallocns (datealloc,\n\t\t\t\t\t\t\t\t\t\t\t\t\t amt,\n\t\t\t\t\t\t\t\t\t\t\t\t\t transid_allocfrom,\n\t\t\t\t\t\t\t\t\t\t\t\t\t transid_allocto)\n\t\t\t\t\t\t\tVALUES ('" . date('Y-m-d') . "',\n\t\t\t\t\t\t\t\t\t'" . $AllocateAmount . "',\n\t\t\t\t\t\t\t\t\t'" . $DebtorTransID . "',\n\t\t\t\t\t\t\t\t\t'" . $InvoiceRow['id'] . "')";
                $InsertAllocResult = api_DB_query($SQL, $db, '', '', true);
            }
            if (abs($InvoiceRow['total'] - $InvoiceRow['alloc'] - $AllocateAmount) < 0.005) {
                $Settled = 1;
            } else {
                $Settled = 0;
            }
            $SQL = "UPDATE debtortrans SET alloc = alloc + " . $AllocateAmount . ",\n\t\t\t\t\t\t\t\t\t\t\t\tsettled = '" . $Settled . "'\n\t\t\t\t\t\tWHERE id = '" . $InvoiceRow['id'] . "'";
            $UpdateAllocResult = api_DB_query($SQL, $db, '', '', true);
            $Allocated -= $AllocateAmount;
        }
        if (abs($TotalCreditFX - $Allocated) < 0.005) {
            $Settled = 1;
        } else {
            $Settled = 0;
        }
        $SQL = "UPDATE debtortrans SET alloc = alloc + " . $Allocated . ",\n\t\t\t\t\t\t\t\t\t\t\t\tsettled = '" . $Settled . "'\n\t\t\t\t\tWHERE id = '" . $DebtorTransID . "'";
        $UpdateAllocResult = api_DB_query($SQL, $db, '', '', true);
    }
    if (sizeof($Errors) == 0) {
        $Result = DB_Txn_Commit($db);
        $Errors[0] = 0;
        $Errors[1] = $CreditNoteNo;
    } else {
        $Result = DB_Txn_Rollback($db);
    }
    return $Errors;
}
コード例 #4
0
         /* if the tax is not 0 */
     }
     /*end of loop to post the tax */
     /* Now the control account */
     $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\tamount)\n\t\t\t\t\t VALUES (21,\n\t\t\t\t\t \t'" . $CreditNoteNo . "',\n\t\t\t\t\t\t'" . $SQLCreditNoteDate . "',\n\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t'" . $_SESSION['SuppTrans']->CreditorsAct . "',\n\t\t\t\t\t\t'" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('Credit Note') . ' ' . $_SESSION['SuppTrans']->SuppReference . ' ' . $_SESSION['SuppTrans']->CurrCode . locale_number_format($_SESSION['SuppTrans']->OvAmount + $_SESSION['SuppTrans']->OvGST, $_SESSION['SuppTrans']->CurrDecimalPlaces) . ' @ ' . _('a rate of') . ' ' . $_SESSION['SuppTrans']->ExRate . "',\n\t\t\t\t\t\t'" . ($LocalTotal + $TaxTotal / $_SESSION['SuppTrans']->ExRate) . "')";
     $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction for the control total could not be added because');
     $DbgMsg = _('The following SQL to insert the GL transaction was used');
     $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
 }
 /*Thats the end of the GL postings */
 /*Now insert the credit note into the SuppTrans table*/
 $SQL = "INSERT INTO supptrans (transno,\n\t\t\t\t\t\ttype,\n\t\t\t\t\t\tsupplierno,\n\t\t\t\t\t\tsuppreference,\n\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\tduedate,\n\t\t\t\t\t\tinputdate,\n\t\t\t\t\t\tovamount,\n\t\t\t\t\t\tovgst,\n\t\t\t\t\t\trate,\n\t\t\t\t\t\ttranstext)\n\t\t\t\tVALUES (\n\t\t\t\t\t'" . $CreditNoteNo . "',\n\t\t\t\t\t21,\n\t\t\t\t\t'" . $_SESSION['SuppTrans']->SupplierID . "',\n\t\t\t\t\t'" . $_SESSION['SuppTrans']->SuppReference . "',\n\t\t\t\t\t'" . $SQLCreditNoteDate . "',\n\t\t\t\t\t'" . FormatDateForSQL($_SESSION['SuppTrans']->DueDate) . "',\n\t\t\t\t\t'" . Date('Y-m-d H-i-s') . "',\n\t\t\t\t\t'" . -$_SESSION['SuppTrans']->OvAmount . "',\n\t\t\t\t\t'" . -$TaxTotal . "',\n\t\t\t\t\t'" . $_SESSION['SuppTrans']->ExRate . "',\n\t\t\t\t\t'" . $_SESSION['SuppTrans']->Comments . "')";
 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The supplier credit note transaction could not be added to the database because');
 $DbgMsg = _('The following SQL to insert the supplier credit note was used');
 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
 $SuppTransID = DB_Last_Insert_ID($db, 'supptrans', 'id');
 /* Insert the tax totals for each tax authority where tax was charged on the invoice */
 foreach ($_SESSION['SuppTrans']->Taxes as $TaxTotals) {
     $SQL = "INSERT INTO supptranstaxes (supptransid,\n\t\t\t\t\t\t\t\t\t\t\t\ttaxauthid,\n\t\t\t\t\t\t\t\t\t\t\t\ttaxamount)\n\t\t\t\t\t\t\t\t\tVALUES ('" . $SuppTransID . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $TaxTotals->TaxAuthID . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . -$TaxTotals->TaxOvAmount . "')";
     $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The supplier transaction taxes records could not be inserted because');
     $DbgMsg = _('The following SQL to insert the supplier transaction taxes record was used:');
     $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
 }
 /* Now update the GRN and PurchOrderDetails records for amounts invoiced 
  * can't use the previous loop around GRNs as this was only for where the creditors->GL link was active*/
 foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN) {
     $SQL = "UPDATE purchorderdetails SET qtyinvoiced = qtyinvoiced - " . $EnteredGRN->This_QuantityInv . " \n\t\t\t\t\tWHERE podetailitem = '" . $EnteredGRN->PODetailItem . "'";
     $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The quantity credited of the purchase order line could not be updated because');
     $DbgMsg = _('The following SQL to update the purchase order details was used');
     $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
     $SQL = "UPDATE grns SET quantityinv = quantityinv - " . $EnteredGRN->This_QuantityInv . " WHERE grnno = '" . $EnteredGRN->GRNNo . "'";
コード例 #5
0
        prnMsg($msg, 'success');
        if ($_POST['Cert'] == 1) {
            $result = DB_query("SELECT prodspeckey, lotkey FROM qasamples\n\t\t\t\t\t\t\tWHERE sampleid = '" . $SelectedSampleID . "'");
            $myrow = DB_fetch_row($result);
            if ($myrow[0] > '') {
                $sql = "UPDATE qasamples SET cert='0'\n\t\t\t\t\t\tWHERE sampleid <> '" . $SelectedSampleID . "'\n\t\t\t\t\t\tAND prodspeckey='" . $myrow[0] . "'\n\t\t\t\t\t\tAND lotkey='" . $myrow[1] . "'";
                $msg = _('All other samples for this Specification and Lot was marked as Cert=No');
                $ErrMsg = _('The update of the QA Sample failed because');
                $DbgMsg = _('The SQL that was used and failed was');
                $result = DB_query($sql, $ErrMsg, $DbgMsg);
                prnMsg($msg, 'success');
            }
        }
    } else {
        CreateQASample($_POST['ProdSpecKey'], $_POST['LotKey'], $_POST['Identifier'], $_POST['Comments'], $_POST['Cert'], $_POST['DuplicateOK'], $db);
        $SelectedSampleID = DB_Last_Insert_ID($db, 'qasamples', 'sampleid');
        if ($SelectedSampleID > '') {
            $msg = _('Created New Sample');
            prnMsg($msg, 'success');
        }
    }
    unset($SelectedSampleID);
    unset($_POST['ProdSpecKey']);
    unset($_POST['LotKey']);
    unset($_POST['Identifier']);
    unset($_POST['Comments']);
    unset($_POST['Cert']);
} elseif (isset($_GET['delete'])) {
    //the link to delete a selected record was clicked instead of the submit button
    // PREVENT DELETES IF DEPENDENT RECORDS
    $sql = "SELECT COUNT(*) FROM sampleresults WHERE sampleresults.sampleid='" . $SelectedSampleID . "'\n\t\t\t\t\t\t\t\t\t\t\tAND sampleresults.testvalue > ''";
コード例 #6
0
ファイル: Z_ImportFixedAssets.php プロジェクト: rrsc/KwaMoja
     $DepnType = 1;
 } else {
     $DepnType = 0;
 }
 if ($InputError == false) {
     //no errors
     $TransNo = GetNextTransNo(49, $db);
     $PeriodNo = GetPeriod(ConvertSQLDate($_POST['DateToEnter']), $db);
     //attempt to insert the stock item
     $sql = "INSERT INTO fixedassets (description,\n\t\t\t\t\t\t\t\t\t\t\tlongdescription,\n\t\t\t\t\t\t\t\t\t\t\tassetcategoryid,\n\t\t\t\t\t\t\t\t\t\t\tserialno,\n\t\t\t\t\t\t\t\t\t\t\tbarcode,\n\t\t\t\t\t\t\t\t\t\t\tassetlocation,\n\t\t\t\t\t\t\t\t\t\t\tcost,\n\t\t\t\t\t\t\t\t\t\t\taccumdepn,\n\t\t\t\t\t\t\t\t\t\t\tdepntype,\n\t\t\t\t\t\t\t\t\t\t\tdepnrate,\n\t\t\t\t\t\t\t\t\t\t\tdatepurchased)\n\t\t\t\t\t\t\tVALUES ('" . $Description . "',\n\t\t\t\t\t\t\t\t\t'" . $LongDescription . "',\n\t\t\t\t\t\t\t\t\t'" . $AssetCategoryID . "',\n\t\t\t\t\t\t\t\t\t'" . $SerialNo . "',\n\t\t\t\t\t\t\t\t\t'" . $BarCode . "',\n\t\t\t\t\t\t\t\t\t'" . $AssetLocationCode . "',\n\t\t\t\t\t\t\t\t\t'" . $Cost . "',\n\t\t\t\t\t\t\t\t\t'" . $AccumDepn . "',\n\t\t\t\t\t\t\t\t\t'" . $DepnType . "',\n\t\t\t\t\t\t\t\t\t'" . $DepnRate . "',\n\t\t\t\t\t\t\t\t\t'" . FormatDateForSQL($DatePurchased) . "')";
     $ErrMsg = _('The asset could not be added because');
     $DbgMsg = _('The SQL that was used to add the asset and failed was');
     $result = DB_query($sql, $db, $ErrMsg, $DbgMsg);
     if (DB_error_no($db) == 0) {
         //the insert of the new code worked so bang in the fixedassettrans records too
         $AssetID = DB_Last_Insert_ID($db, 'fixedassets', 'assetid');
         $sql = "INSERT INTO fixedassettrans ( assetid,\n\t\t\t\t\t\t\t\t\t\t\t\ttranstype,\n\t\t\t\t\t\t\t\t\t\t\t\ttransno,\n\t\t\t\t\t\t\t\t\t\t\t\ttransdate,\n\t\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\t\tinputdate,\n\t\t\t\t\t\t\t\t\t\t\t\tfixedassettranstype,\n\t\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\t\t\tVALUES ( '" . $AssetID . "',\n\t\t\t\t\t\t\t\t\t\t\t'49',\n\t\t\t\t\t\t\t\t\t\t\t'" . $TransNo . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $_POST['DateToEnter'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . Date('Y-m-d') . "',\n\t\t\t\t\t\t\t\t\t\t\t'cost',\n\t\t\t\t\t\t\t\t\t\t\t'" . $Cost . "')";
         $ErrMsg = _('The transaction for the cost of the asset could not be added because');
         $DbgMsg = _('The SQL that was used to add the fixedasset trans record that failed was');
         $InsResult = DB_query($sql, $db, $ErrMsg, $DbgMsg);
         $sql = "INSERT INTO fixedassettrans ( assetid,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttranstype,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttransno,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttransdate,\n\t\t\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\t\t\tinputdate,\n\t\t\t\t\t\t\t\t\t\t\t\t\tfixedassettranstype,\n\t\t\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\t\t\tVALUES ( '" . $AssetID . "',\n\t\t\t\t\t\t\t\t\t\t\t'49',\n\t\t\t\t\t\t\t\t\t\t\t'" . $TransNo . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $_POST['DateToEnter'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . Date('Y-m-d') . "',\n\t\t\t\t\t\t\t\t\t\t\t'depn',\n\t\t\t\t\t\t\t\t\t\t\t'" . $AccumDepn . "')";
         $ErrMsg = _('The transaction for the cost of the asset could not be added because');
         $DbgMsg = _('The SQL that was used to add the fixedasset trans record that failed was');
         $InsResult = DB_query($sql, $db, $ErrMsg, $DbgMsg);
         if (DB_error_no($db) == 0) {
             prnMsg(_('Inserted the new asset:') . ' ' . $Description, 'info');
         }
     }
 }
 // there were errors checking the row so no inserts
 $Row++;
コード例 #7
0
ファイル: ReportCreator.php プロジェクト: rrsc/KwaMoja
         // it's a form so write a default form break record
         $sql = "INSERT INTO " . DBRptFields . " (reportid, entrytype, params, displaydesc)\n\t\t\t\t\t\t\tVALUES (" . $ReportID . ", 'grouplist', '', '');";
         $Result = DB_query($sql, $db, '', '', false, true);
     }
     $sql = "INSERT INTO " . DBRptFields . " (reportid, entrytype, fieldname, displaydesc)\n\t\t\t\t\t\tVALUES (" . $ReportID . ", 'dateselect', '', 'a');";
     $Result = DB_query($sql, $db, '', '', false, true);
 } else {
     // copy the report and all fields to the new report name
     $OrigID = $ReportID;
     // Set the report id to 0 to prepare to copy
     $sql = "UPDATE " . DBReports . " SET id=0 WHERE id=" . $ReportID . ";";
     $Result = DB_query($sql, $db, '', '', false, true);
     $sql = "INSERT INTO " . DBReports . " SELECT * FROM " . DBReports . " WHERE id = 0;";
     $Result = DB_query($sql, $db, '', '', false, true);
     // Fetch the id entered
     $ReportID = DB_Last_Insert_ID($db, DBReports, 'id');
     // Restore original report ID from 0
     $sql = "UPDATE " . DBReports . " SET id=" . $OrigID . " WHERE id=0;";
     $Result = DB_query($sql, $db, '', '', false, true);
     // Set the report name and group name per the form
     $sql = "UPDATE " . DBReports . " SET\n\t\t\t\t\t\t\treportname = '" . DB_escape_string($_POST['ReportName']) . "'\n\t\t\t\t\t\tWHERE id =" . $ReportID . ";";
     $Result = DB_query($sql, $db, '', '', false, true);
     // fetch the fields and duplicate
     $sql = "SELECT * FROM " . DBRptFields . " WHERE reportid=" . $OrigID . ";";
     $Result = DB_query($sql, $db, '', '', false, true);
     while ($temp = DB_fetch_array($Result)) {
         $field[] = $temp;
     }
     foreach ($field as $row) {
         $sql = "INSERT INTO " . DBRptFields . " (reportid, entrytype, seqnum, fieldname,\n\t\t\t\t\t\t\t\tdisplaydesc, visible, columnbreak, params)\n\t\t\t\t\t\t\tVALUES (" . $ReportID . ", '" . $row['entrytype'] . "', " . $row['seqnum'] . ",\n\t\t\t\t\t\t\t\t'" . $row['fieldname'] . "', '" . $row['displaydesc'] . "', '" . $row['visible'] . "',\n\t\t\t\t\t\t\t\t'" . $row['columnbreak'] . "', '" . $row['params'] . "');";
         $Result = DB_query($sql, $db, '', '', false, true);
コード例 #8
0
     prnMsg(_('The end date of this recurring order must be after the start date'), 'error');
 }
 if (isset($_POST['MakeRecurringOrder']) and $_POST['Quotation'] == 1) {
     $InputErrors = 1;
     prnMsg(_('A recurring order cannot be made from a quotation'), 'error');
 }
 if ($InputErrors == 0) {
     /*Error checks above all passed ok so lets go*/
     if ($NewRecurringOrder == 'Yes') {
         /* finally write the recurring order header to the database and then the line details*/
         $DelDate = FormatDateforSQL($_SESSION['Items' . $identifier]->DeliveryDate);
         $HeaderSQL = "INSERT INTO recurringsalesorders (\n\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\tcustomerref,\n\t\t\t\t\t\t\t\t\t\tcomments,\n\t\t\t\t\t\t\t\t\t\torddate,\n\t\t\t\t\t\t\t\t\t\tordertype,\n\t\t\t\t\t\t\t\t\t\tdeliverto,\n\t\t\t\t\t\t\t\t\t\tdeladd1,\n\t\t\t\t\t\t\t\t\t\tdeladd2,\n\t\t\t\t\t\t\t\t\t\tdeladd3,\n\t\t\t\t\t\t\t\t\t\tdeladd4,\n\t\t\t\t\t\t\t\t\t\tdeladd5,\n\t\t\t\t\t\t\t\t\t\tdeladd6,\n\t\t\t\t\t\t\t\t\t\tcontactphone,\n\t\t\t\t\t\t\t\t\t\tcontactemail,\n\t\t\t\t\t\t\t\t\t\tfreightcost,\n\t\t\t\t\t\t\t\t\t\tfromstkloc,\n\t\t\t\t\t\t\t\t\t\tshipvia,\n\t\t\t\t\t\t\t\t\t\tlastrecurrence,\n\t\t\t\t\t\t\t\t\t\tstopdate,\n\t\t\t\t\t\t\t\t\t\tfrequency,\n\t\t\t\t\t\t\t\t\t\tautoinvoice)\n\t\t\t\t\t\t\t\t\tvalues (\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DebtorNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->Branch . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->CustRef . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->Comments . "',\n\t\t\t\t\t\t\t\t\t\t'" . Date('Y-m-d H:i') . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DefaultSalesType . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DeliverTo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DelAdd1 . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DelAdd2 . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DelAdd3 . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DelAdd4 . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DelAdd5 . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->DelAdd6 . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->PhoneNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->Email . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->FreightCost . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->Location . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_SESSION['Items' . $identifier]->ShipVia . "',\n\t\t\t\t\t\t\t\t\t\t'" . FormatDateforSQL($_POST['StartDate']) . "',\n\t\t\t\t\t\t\t\t\t\t'" . FormatDateforSQL($_POST['StopDate']) . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_POST['Frequency'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $_POST['AutoInvoice'] . "')";
         $ErrMsg = _('The recurring order cannot be added because');
         $DbgMsg = _('The SQL that failed was');
         $InsertQryResult = DB_query($HeaderSQL, $ErrMsg, $DbgMsg, true);
         $RecurrOrderNo = DB_Last_Insert_ID($db, 'recurringsalesorders', 'recurrorderno');
         echo 'xxx' . $RecurrOrderNo;
         $StartOf_LineItemsSQL = "INSERT INTO recurrsalesorderdetails (recurrorderno,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tstkcode,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tunitprice,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tquantity,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tdiscountpercent,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tnarrative)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tVALUES ('";
         foreach ($_SESSION['Items' . $identifier]->LineItems as $StockItem) {
             $LineItemsSQL = $StartOf_LineItemsSQL . $RecurrOrderNo . "',\n\t\t\t\t\t\t\t\t'" . $StockItem->StockID . "',\n\t\t\t\t\t\t\t\t'" . filter_number_format($StockItem->Price) . "',\n\t\t\t\t\t\t\t\t'" . filter_number_format($StockItem->Quantity) . "',\n\t\t\t\t\t\t\t\t'" . filter_number_format($StockItem->DiscountPercent) . "',\n\t\t\t\t\t\t\t\t'" . $StockItem->Narrative . "')";
             $Ins_LineItemResult = DB_query($LineItemsSQL, $ErrMsg, $DbgMsg, true);
         }
         /* inserted line items into sales order details */
         $result = DB_Txn_Commit();
         prnmsg(_('The new recurring order template has been added'), 'success');
     } else {
         /* must be updating an existing recurring order */
         $HeaderSQL = "UPDATE recurringsalesorders SET\n\t\t\t\t\t\tstopdate =  '" . FormatDateforSQL($_POST['StopDate']) . "',\n\t\t\t\t\t\tfrequency = '" . $_POST['Frequency'] . "',\n\t\t\t\t\t\tautoinvoice = '" . $_POST['AutoInvoice'] . "'\n\t\t\t\t\tWHERE recurrorderno = '" . $_POST['ExistingRecurrOrderNo'] . "'";
         $ErrMsg = _('The recurring order cannot be updated because');
         $UpdateQryResult = DB_query($HeaderSQL, $ErrMsg);
         prnmsg(_('The recurring order template has been updated'), 'success');
コード例 #9
0
ファイル: TaxProvinces.php プロジェクト: rrsc/KwaMoja
                prnMsg(_('The tax province no longer exists'), 'error');
            }
        }
    } elseif ($InputError != 1) {
        /*SelectedTaxProvince is null cos no item selected on first time round so must be adding a record*/
        $sql = "SELECT count(*) FROM taxprovinces\n\t\t\t\tWHERE taxprovincename " . LIKE . " '" . $_POST['TaxProvinceName'] . "'";
        $result = DB_query($sql, $db);
        $myrow = DB_fetch_row($result);
        if ($myrow[0] > 0) {
            $InputError = 1;
            prnMsg(_('The tax province cannot be created because another with the same name already exists'), 'error');
        } else {
            $sql = "INSERT INTO taxprovinces (taxprovincename )\n\t\t\t\t\tVALUES ('" . $_POST['TaxProvinceName'] . "')";
            $ErrMsg = _('Could not add tax province');
            $result = DB_query($sql, $db, $ErrMsg);
            $TaxProvinceID = DB_Last_Insert_ID($db, 'taxprovinces', 'taxprovinceid');
            $sql = "INSERT INTO taxauthrates (taxauthority, dispatchtaxprovince, taxcatid)\n\t\t\t\t\tSELECT taxauthorities.taxid, '" . $TaxProvinceID . "', taxcategories.taxcatid\n\t\t\t\t\tFROM taxauthorities CROSS JOIN taxcategories";
            $ErrMsg = _('Could not add tax authority rates for the new dispatch tax province. The rates of tax will not be able to be added - manual database interaction will be required to use this dispatch tax province');
            $result = DB_query($sql, $db, $ErrMsg);
        }
        if (!$result) {
            prnMsg(_('Errors were encountered adding this tax province'), 'error');
        } else {
            prnMsg(_('New tax province added'), 'success');
        }
    }
    unset($SelectedTaxProvince);
    unset($_POST['SelectedTaxProvince']);
    unset($_POST['TaxProvinceName']);
} elseif (isset($_GET['delete'])) {
    //the link to delete a selected record was clicked instead of the submit button
コード例 #10
0
ファイル: ReportMaker.php プロジェクト: strollClouds/snkStudy
function SaveNewReport($ReportID, $AllowOverwrite)
{
    global $db, $Prefs;
    // input error check reportname, blank duplicate, bad characters, etc.
    // Delete any special characters from ReportName
    if ($_POST['ReportName'] == '') {
        // no report name was entered, error and reload form
        $Rtn['result'] = 'error';
        $Rtn['default'] = false;
        $Rtn['message'] = RPT_NORPT;
        return $Rtn;
    }
    // check for duplicate report name and error or overwrite if allowed
    $sql = "SELECT id, defaultreport FROM " . DBReports . " WHERE reportname='" . addslashes($_POST['ReportName']) . "';";
    $Result = DB_query($sql, $db, '', '', false, true);
    if (DB_num_rows($Result) > 0) {
        $myrow = DB_fetch_array($Result);
    }
    if (isset($myrow)) {
        // then we have a duplicate report name do some checking
        if ($myrow['defaultreport']) {
            // it's a default don't allow overwrite no matter what, return
            $Rtn['result'] = 'warn';
            $Rtn['default'] = true;
            $Rtn['message'] = RPT_SAVEDEF;
            return $Rtn;
        } elseif (!$AllowOverwrite) {
            // verify user wants to replace, return
            $Rtn['result'] = 'warn';
            $Rtn['default'] = false;
            $Rtn['message'] = RPT_SAVEDUP;
            return $Rtn;
        }
        // check for the same report to update or replace a different report than ReportID
        if ($myrow['id'] != $ReportID) {
            // erase the report to overwrite and duplicate ReportID
            $sql = "DELETE FROM " . DBReports . " WHERE id = '" . $myrow['id'] . "'";
            $Result = DB_query($sql, $db, '', '', false, true);
            $sql = "DELETE FROM " . DBRptFields . " WHERE reportid = '" . $myrow['id'] . "'";
            $Result = DB_query($sql, $db, '', '', false, true);
        } else {
            // just return because the save as name is the same as the current report name
            $Rtn['message'] = RPT_REPORT . $Prefs['reportname'] . RPT_WASSAVED . $_POST['ReportName'];
            $Rtn['ReportID'] = $ReportID;
            $Rtn['result'] = 'success';
            return $Rtn;
        }
    }
    // Input validated perform requested operation
    $OrigID = $ReportID;
    // Set the report id to 0 to prepare to duplicate
    $sql = "UPDATE " . DBReports . " SET id=0 WHERE id='" . $ReportID . "'";
    $Result = DB_query($sql, $db, '', '', false, true);
    $sql = "INSERT INTO " . DBReports . " SELECT * FROM " . DBReports . " WHERE id=0;";
    $Result = DB_query($sql, $db, '', '', false, true);
    // Fetch the id entered
    $ReportID = DB_Last_Insert_ID($db, 'reports', 'id');
    // Restore original report ID from 0
    $sql = "UPDATE " . DBReports . " SET id='" . $OrigID . "' WHERE id=0;";
    $Result = DB_query($sql, $db, '', '', false, true);
    // Set the report name per the form and make a non-default report
    $sql = "UPDATE " . DBReports . " SET reportname='" . addslashes($_POST['ReportName']) . "', defaultreport='0' WHERE id ='" . $ReportID . "'";
    $Result = DB_query($sql, $db, '', '', false, true);
    // fetch the fields and duplicate
    $sql = "SELECT * FROM " . DBRptFields . " WHERE reportid='" . $OrigID . "'";
    $Result = DB_query($sql, $db, '', '', false, true);
    while ($temp = DB_fetch_array($Result)) {
        $field[] = $temp;
    }
    foreach ($field as $row) {
        $sql = "INSERT INTO " . DBRptFields . " (reportid, entrytype, seqnum, fieldname,\n\t\t\t\tdisplaydesc, visible, columnbreak, params)\n\t\t\tVALUES ('" . $ReportID . "', '" . $row['entrytype'] . "', '" . $row['seqnum'] . "',\n\t\t\t\t'" . $row['fieldname'] . "', '" . $row['displaydesc'] . "', '" . $row['visible'] . "',\n\t\t\t\t'" . $row['columnbreak'] . "', '" . $row['params'] . "');";
        $Result = DB_query($sql, $db, '', '', false, true);
    }
    $Rtn['message'] = RPT_REPORT . $Prefs['reportname'] . RPT_WASSAVED . $_POST['ReportName'];
    $Rtn['ReportID'] = $ReportID;
    $Rtn['result'] = 'success';
    return $Rtn;
}
コード例 #11
0
 $SQL = "INSERT INTO supptrans (type,\n\t\t\t\t\ttransno,\n\t\t\t\t\tsuppreference,\n\t\t\t\t\tsupplierno,\n\t\t\t\t\ttrandate,\n\t\t\t\t\tduedate,\n\t\t\t\t\tinputdate,\n\t\t\t\t\tsettled,\n\t\t\t\t\trate,\n\t\t\t\t\tovamount,\n\t\t\t\t\tdiffonexch,\n\t\t\t\t\talloc) ";
 $SQL = $SQL . "VALUES (22,\n\t\t\t\t'" . $SuppPaymentNo . "',\n\t\t\t\t'" . $PaytReference . "',\n\t\t\t\t'" . $SupplierID . "',\n\t\t\t\t'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',\n\t\t\t\t'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',\n\t\t\t\t'" . date('Y-m-d H-i-s') . "',\n\t\t\t\t1,\n\t\t\t\t'" . $_POST['ExRate'] . "',\n\t\t\t\t'" . -$AccumBalance . "',\n\t\t\t\t'" . -$AccumDiffOnExch . "',\n\t\t\t\t'" . -$AccumBalance . "')";
 $ProcessResult = DB_query($SQL, $db, '', '', false, false);
 if (DB_error_no($db) != 0) {
     $title = _('Payment Processing - Problem Report');
     include 'header.inc';
     prnMsg(_('None of the payments will be processed because the payment record for') . ' ' . $SupplierName . ' ' . _('could not be inserted because') . ' - ' . DB_error_msg($db), 'error');
     echo '<br /><a href="' . $rootpath . '/index.php">' . _('Back to the menu') . '</a>';
     if ($debug == 1) {
         prnMsg(_('The SQL that failed was') . ' ' . $SQL, 'error');
     }
     $ProcessResult = DB_Txn_Rollback($db);
     include 'footer.inc';
     exit;
 }
 $PaymentTransID = DB_Last_Insert_ID($db, 'supptrans', 'id');
 /*Do the inserts for the allocation record against the payment for this charge */
 foreach ($Allocs as $AllocTrans) {
     /*loop through the array of allocations */
     $SQL = "INSERT INTO suppallocs (amt,\n\t\t\t\t\t\tdatealloc,\n\t\t\t\t\t\ttransid_allocfrom,\n\t\t\t\t\t\ttransid_allocto)\n\t\t\t\tVALUES (\n\t\t\t\t\t\t'" . $AllocTrans->Amount . "',\n\t\t\t\t\t\t'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',\n\t\t\t\t\t\t'" . $PaymentTransID . "',\n\t\t\t\t\t\t'" . $AllocTrans->TransID . "')";
     $ProcessResult = DB_query($SQL, $db);
     if (DB_error_no($db) != 0) {
         $title = _('Payment Processing - Problem Report') . '.... ';
         include 'header.inc';
         prnMsg(_('None of the payments will be processed since an allocation record for') . $SupplierName . _('could not be inserted because') . ' - ' . DB_error_msg($db), 'error');
         echo '<br /><a href="' . $rootpath . '/index.php">' . _('Back to the menu') . '</a>';
         if ($debug == 1) {
             prnMsg(_('The SQL that failed was') . $SQL, 'error');
         }
         $ProcessResult = DB_Txn_Rollback($db);
         include 'footer.inc';
コード例 #12
0
ファイル: api_salesorders.php プロジェクト: rrsc/KwaMoja
function InvoiceSalesOrder($OrderNo, $User, $Password)
{
    $Errors = array();
    $db = db($User, $Password);
    if (gettype($db) == 'integer') {
        $Errors[] = NoAuthorisation;
        return $Errors;
    }
    $Errors = VerifyOrderHeaderExists($OrderNo, sizeof($Errors), $Errors, $db);
    if (sizeof($Errors) != 0) {
        return $Errors;
    }
    /*Does not deal with assembly items or serialise/lot track items - for use by POS */
    /*Get Company Defaults */
    $ReadCoyResult = api_DB_query("SELECT debtorsact,\n\t\t\t\t\t\t\t\t\t\t\t\tfreightact,\n\t\t\t\t\t\t\t\t\t\t\t\tgllink_debtors,\n\t\t\t\t\t\t\t\t\t\t\t\tgllink_stock\n\t\t\t\t\t\t\t\t\t\tFROM companies\n\t\t\t\t\t\t\t\t\t\tWHERE coycode=1", $db);
    $CompanyRecord = DB_fetch_array($ReadCoyResult);
    if (DB_error_no($db) != 0) {
        $Errors[] = NoCompanyRecord;
    }
    $OrderHeaderSQL = "SELECT salesorders.debtorno,\n\t\t\t\t \t\t\t\t  debtorsmaster.name,\n\t\t\t\t\t\t\t\t  salesorders.branchcode,\n\t\t\t\t\t\t\t\t  salesorders.customerref,\n\t\t\t\t\t\t\t\t  salesorders.orddate,\n\t\t\t\t\t\t\t\t  salesorders.ordertype,\n\t\t\t\t\t\t\t\t  salesorders.shipvia,\n\t\t\t\t\t\t\t\t  custbranch.area,\n\t\t\t\t\t\t\t\t  custbranch.taxgroupid,\n\t\t\t\t\t\t\t\t  debtorsmaster.currcode,\n\t\t\t\t\t\t\t\t  currencies.rate,\n\t\t\t\t\t\t\t\t  salesorders.fromstkloc,\n\t\t\t\t\t\t\t\t  custbranch.salesman\n\t\t\t\t\t\t\tFROM salesorders\n\t\t\t\t\t\t\tINNER JOIN debtorsmaster\n\t\t\t\t\t\t\tON salesorders.debtorno = debtorsmaster.debtorno\n\t\t\t\t\t\t\tINNER JOIN custbranch\n\t\t\t\t\t\t\tON salesorders.debtorno = custbranch.debtorno\n\t\t\t\t\t\t\tAND salesorders.branchcode = custbranch.branchcode\n\t\t\t\t\t\t\tINNER JOIN locations\n\t\t\t\t\t\t\tON locations.loccode=salesorders.fromstkloc\n\t\t\t\t\t\t\tINNER JOIN currencies\n\t\t\t\t\t\t\tON debtorsmaster.currcode=currencies.currabrev\n\t\t\t\t\t\t\tWHERE salesorders.orderno = '" . $OrderNo . "'";
    $OrderHeaderResult = api_DB_query($OrderHeaderSQL, $db);
    if (DB_error_no($db) != 0) {
        $Errors[] = NoReadOrder;
    }
    $OrderHeader = DB_fetch_array($OrderHeaderResult);
    $TaxProvResult = api_DB_query("SELECT taxprovinceid FROM locations WHERE loccode='" . $OrderHeader['fromstkloc'] . "'", $db);
    if (DB_error_no($db) != 0) {
        $Errors[] = NoTaxProvince;
    }
    $myrow = DB_fetch_row($TaxProvResult);
    $DispTaxProvinceID = $myrow[0];
    $LineItemsSQL = "SELECT stkcode,\n\t\t\t\t\t\t\t\tunitprice,\n\t\t\t\t\t\t\t\tquantity,\n\t\t\t\t\t\t\t\tdiscountpercent,\n\t\t\t\t\t\t\t\ttaxcatid,\n\t\t\t\t\t\t\t\tmbflag,\n\t\t\t\t\t\t\t\tmaterialcost+labourcost+overheadcost AS standardcost\n\t\t\t\t\t\tFROM salesorderdetails INNER JOIN stockmaster\n\t\t\t\t\t\tON salesorderdetails.stkcode = stockmaster.stockid\n\t\t\t\t\t\tWHERE orderno ='" . $OrderNo . "'\n\t\t\t\t\t\tAND completed=0";
    $LineItemsResult = api_DB_query($LineItemsSQL, $db);
    if (DB_error_no($db) != 0 or DB_num_rows($LineItemsResult) == 0) {
        $Errors[] = NoReadOrderLines;
        return $Errors;
    }
    /*Start an SQL transaction */
    $result = DB_Txn_Begin($db);
    /*Now Get the next invoice number - function in SQL_CommonFunctions*/
    $InvoiceNo = GetNextTransNo(10, $db);
    $PeriodNo = GetCurrentPeriod($db);
    $TaxTotals = array();
    $TotalFXNetInvoice = 0;
    $TotalFXTax = 0;
    $LineCounter = 0;
    while ($OrderLineRow = DB_fetch_array($LineItemsResult)) {
        $StandardCost = $OrderLineRow['standardcost'];
        $LocalCurrencyPrice = $OrderLineRow['unitprice'] * (1 - floatval($OrderLineRow['discountpercent'])) / $OrderHeader['rate'];
        $LineNetAmount = $OrderLineRow['unitprice'] * $OrderLineRow['quantity'] * (1 - floatval($OrderLineRow['discountpercent']));
        /*Gets the Taxes and rates applicable to this line from the TaxGroup of the branch and TaxCategory of the item
        		and the taxprovince of the dispatch location */
        $SQL = "SELECT taxgrouptaxes.calculationorder,\n\t\t\t\t\t\t\ttaxauthorities.description,\n\t\t\t\t\t\t\ttaxgrouptaxes.taxauthid,\n\t\t\t\t\t\t\ttaxauthorities.taxglcode,\n\t\t\t\t\t\t\ttaxgrouptaxes.taxontax,\n\t\t\t\t\t\t\ttaxauthrates.taxrate\n\t\t\t\t\tFROM taxauthrates INNER JOIN taxgrouptaxes ON\n\t\t\t\t\t\ttaxauthrates.taxauthority=taxgrouptaxes.taxauthid\n\t\t\t\t\t\tINNER JOIN taxauthorities ON\n\t\t\t\t\t\ttaxauthrates.taxauthority=taxauthorities.taxid\n\t\t\t\t\tWHERE taxgrouptaxes.taxgroupid='" . $OrderHeader['taxgroupid'] . "'\n\t\t\t\t\tAND taxauthrates.dispatchtaxprovince='" . $DispTaxProvinceID . "'\n\t\t\t\t\tAND taxauthrates.taxcatid = '" . $OrderLineRow['taxcatid'] . "'\n\t\t\t\t\tORDER BY taxgrouptaxes.calculationorder";
        $GetTaxRatesResult = api_DB_query($SQL, $db);
        if (DB_error_no($db) != 0) {
            $Errors[] = TaxRatesFailed;
        }
        $LineTaxAmount = 0;
        while ($myrow = DB_fetch_array($GetTaxRatesResult)) {
            if (!isset($TaxTotals[$myrow['taxauthid']]['FXAmount'])) {
                $TaxTotals[$myrow['taxauthid']]['FXAmount'] = 0;
            }
            $TaxAuthID = $myrow['taxauthid'];
            $TaxTotals[$myrow['taxauthid']]['GLCode'] = $myrow['taxglcode'];
            $TaxTotals[$myrow['taxauthid']]['TaxRate'] = $myrow['taxrate'];
            $TaxTotals[$myrow['taxauthid']]['TaxAuthDescription'] = $myrow['description'];
            if ($myrow['taxontax'] == 1) {
                $TaxAuthAmount = ($LineNetAmount + $LineTaxAmount) * $myrow['taxrate'];
            } else {
                $TaxAuthAmount = $LineNetAmount * $myrow['taxrate'];
            }
            $TaxTotals[$myrow['taxauthid']]['FXAmount'] += $TaxAuthAmount;
            /*Make an array of the taxes and amounts including GLcodes for later posting - need debtortransid
            		so can only post once the debtor trans is posted - can only post debtor trans when all tax is calculated */
            $LineTaxes[$LineCounter][$myrow['calculationorder']] = array('TaxCalculationOrder' => $myrow['calculationorder'], 'TaxAuthID' => $myrow['taxauthid'], 'TaxAuthDescription' => $myrow['description'], 'TaxRate' => $myrow['taxrate'], 'TaxOnTax' => $myrow['taxontax'], 'TaxAuthAmount' => $TaxAuthAmount);
            $LineTaxAmount += $TaxAuthAmount;
        }
        //end loop around Taxes
        $TotalFXNetInvoice += $LineNetAmount;
        $TotalFXTax += $LineTaxAmount;
        /*Now update SalesOrderDetails for the quantity invoiced and the actual dispatch dates. */
        $SQL = "UPDATE salesorderdetails\n\t\t\t\t\tSET qtyinvoiced = qtyinvoiced + " . $OrderLineRow['quantity'] . ",\n\t\t\t\t\t\tactualdispatchdate = '" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\tcompleted='1'\n\t\t\t\t\tWHERE orderno = '" . $OrderNo . "'\n\t\t\t\t\tAND stkcode = '" . $OrderLineRow['stkcode'] . "'";
        $Result = api_DB_query($SQL, $db, '', '', true);
        if ($OrderLineRow['mbflag'] == 'B' or $OrderLineRow['mbflag'] == 'M') {
            $Assembly = False;
            /* Need to get the current location quantity
            			will need it later for the stock movement */
            $SQL = "SELECT locstock.quantity\n\t\t\t\t\t\tFROM locstock\n\t\t\t\t\t\tWHERE locstock.stockid='" . $OrderLineRow['stkcode'] . "'\n\t\t\t\t\t\tAND loccode= '" . $OrderHeader['fromstkloc'] . "'";
            $Result = api_DB_query($SQL, $db);
            if (DB_num_rows($Result) == 1) {
                $LocQtyRow = DB_fetch_row($Result);
                $QtyOnHandPrior = $LocQtyRow[0];
            } else {
                /* There must be some error this should never happen */
                $QtyOnHandPrior = 0;
            }
            $SQL = "UPDATE locstock\n\t\t\t\t\t\tSET quantity = locstock.quantity - " . $OrderLineRow['quantity'] . "\n\t\t\t\t\t\tWHERE locstock.stockid = '" . $OrderLineRow['stkcode'] . "'\n\t\t\t\t\t\tAND loccode = '" . $OrderHeader['fromstkloc'] . "'";
            $Result = api_DB_query($SQL, $db, '', '', true);
            $SQL = "INSERT INTO stockmoves (stockid,\n\t\t\t\t\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\t\t\t\t\ttransno,\n\t\t\t\t\t\t\t\t\t\t\t\tloccode,\n\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\t\t\tprice,\n\t\t\t\t\t\t\t\t\t\t\t\tprd,\n\t\t\t\t\t\t\t\t\t\t\t\treference,\n\t\t\t\t\t\t\t\t\t\t\t\tqty,\n\t\t\t\t\t\t\t\t\t\t\t\tdiscountpercent,\n\t\t\t\t\t\t\t\t\t\t\t\tstandardcost,\n\t\t\t\t\t\t\t\t\t\t\t\tnewqoh)\n\t\t\t\t\t\tVALUES ('" . $OrderLineRow['stkcode'] . "',\n\t\t\t\t\t\t\t\t'10',\n\t\t\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['fromstkloc'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['branchcode'] . "',\n\t\t\t\t\t\t\t\t'" . $LocalCurrencyPrice . "',\n\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t'" . $OrderNo . "',\n\t\t\t\t\t\t\t\t'" . -$OrderLineRow['quantity'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderLineRow['discountpercent'] . "',\n\t\t\t\t\t\t\t\t'" . $StandardCost . "',\n\t\t\t\t\t\t\t\t'" . ($QtyOnHandPrior - $OrderLineRow['quantity']) . "' )";
            $Result = api_DB_query($SQL, $db, '', '', true);
        } else {
            if ($OrderLineRow['mbflag'] == 'A') {
                /* its an assembly */
                /*Need to get the BOM for this part and make
                		stock moves for the components then update the Location stock balances */
                $Assembly = True;
                $StandardCost = 0;
                /*To start with - accumulate the cost of the comoponents for use in journals later on */
                $SQL = "SELECT bom.component,\n\t\t\t\t\t\t\t\tbom.quantity,\n\t\t\t\t\t\t\t\tstockmaster.materialcost+stockmaster.labourcost+stockmaster.overheadcost AS standard\n\t\t\t\t\t\t\tFROM bom INNER JOIN stockmaster\n\t\t\t\t\t\t\tON bom.component=stockmaster.stockid\n\t\t\t\t\t\t\tWHERE bom.parent='" . $OrderLineRow['stkcode'] . "'\n\t\t\t\t\t\t\tAND bom.effectiveto >= '" . Date('Y-m-d') . "'\n\t\t\t\t\t\t\tAND bom.effectiveafter < '" . Date('Y-m-d') . "'";
                $AssResult = api_DB_query($SQL, $db);
                while ($AssParts = DB_fetch_array($AssResult, $db)) {
                    $StandardCost += $AssParts['standard'] * $AssParts['quantity'];
                    /* Need to get the current location quantity
                    			will need it later for the stock movement */
                    $SQL = "SELECT locstock.quantity\n\t\t\t\t\t\t\tFROM locstock\n\t\t\t\t\t\t\tWHERE locstock.stockid='" . $AssParts['component'] . "'\n\t\t\t\t\t\t\tAND loccode= '" . $OrderHeader['fromstkloc'] . "'";
                    $Result = api_DB_query($SQL, $db);
                    if (DB_num_rows($Result) == 1) {
                        $LocQtyRow = DB_fetch_row($Result);
                        $QtyOnHandPrior = $LocQtyRow[0];
                    } else {
                        /*There must be some error this should never happen */
                        $QtyOnHandPrior = 0;
                    }
                    if (empty($AssParts['standard'])) {
                        $AssParts['standard'] = 0;
                    }
                    $SQL = "INSERT INTO stockmoves (stockid,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttransno,\n\t\t\t\t\t\t\t\t\t\t\t\t\tloccode,\n\t\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\t\t\t\tprd,\n\t\t\t\t\t\t\t\t\t\t\t\t\treference,\n\t\t\t\t\t\t\t\t\t\t\t\t\tqty,\n\t\t\t\t\t\t\t\t\t\t\t\t\tstandardcost,\n\t\t\t\t\t\t\t\t\t\t\t\t\tshow_on_inv_crds,\n\t\t\t\t\t\t\t\t\t\t\t\t\tnewqoh)\n\t\t\t\t\t\t\t\t\t\tVALUES ('" . $AssParts['component'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t 10,\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $OrderHeader['fromstkloc'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $DefaultDispatchDate . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $OrderHeader['debtorno'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $OrderHeader['branchcode'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . _('Assembly') . ': ' . $OrderLineRow['stkcode'] . ' ' . _('Order') . ': ' . $OrderNo . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . -$AssParts['quantity'] * $OrderLineRow['quantity'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t '" . $AssParts['standard'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t 0,\n\t\t\t\t\t\t\t\t\t\t\t\t '" . ($QtyOnHandPrior - $AssParts['quantity'] * $OrderLineRow['quantity']) . "'\t)";
                    $Result = DB_query($SQL, $db, '', '', true);
                    $SQL = "UPDATE locstock\n\t\t\t\t\t\t\tSET quantity = locstock.quantity - " . $AssParts['quantity'] * $OrderLineRow['quantity'] . "\n\t\t\t\t\t\t\tWHERE locstock.stockid = '" . $AssParts['component'] . "'\n\t\t\t\t\t\t\tAND loccode = '" . $OrderHeader['fromlocstk'] . "'";
                    $Result = DB_query($SQL, $db, '', '', true);
                }
                /* end of assembly explosion and updates */
            }
        }
        /* end of its an assembly */
        if ($OrderLineRow['mbflag'] == 'A' or $OrderLineRow['mbflag'] == 'D') {
            /*it's a Dummy/Service item or an Assembly item - still need stock movement record
             * but quantites on hand are always nil */
            $SQL = "INSERT INTO stockmoves (stockid,\n\t\t\t\t\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\t\t\t\t\ttransno,\n\t\t\t\t\t\t\t\t\t\t\t\tloccode,\n\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\t\t\tprice,\n\t\t\t\t\t\t\t\t\t\t\t\tprd,\n\t\t\t\t\t\t\t\t\t\t\t\treference,\n\t\t\t\t\t\t\t\t\t\t\t\tqty,\n\t\t\t\t\t\t\t\t\t\t\t\tdiscountpercent,\n\t\t\t\t\t\t\t\t\t\t\t\tstandardcost,\n\t\t\t\t\t\t\t\t\t\t\t\tnewqoh)\n\t\t\t\t\t\tVALUES ('" . $OrderLineRow['stkcode'] . "',\n\t\t\t\t\t\t\t\t'10',\n\t\t\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['fromstkloc'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['branchcode'] . "',\n\t\t\t\t\t\t\t\t'" . $LocalCurrencyPrice . "',\n\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t'" . $OrderNo . "',\n\t\t\t\t\t\t\t\t'" . -$OrderLineRow['quantity'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderLineRow['discountpercent'] . "',\n\t\t\t\t\t\t\t\t'" . $StandardCost . "',\n\t\t\t\t\t\t\t\t'0' )";
            $Result = api_DB_query($SQL, $db, '', '', true);
        }
        /*Get the ID of the StockMove... */
        $StkMoveNo = DB_Last_Insert_ID($db, 'stockmoves', 'stkmoveno');
        /*Insert the taxes that applied to this line */
        foreach ($LineTaxes[$LineCounter] as $Tax) {
            $SQL = "INSERT INTO stockmovestaxes (stkmoveno,\n\t\t\t\t\t\t\t\t\ttaxauthid,\n\t\t\t\t\t\t\t\t\ttaxrate,\n\t\t\t\t\t\t\t\t\ttaxcalculationorder,\n\t\t\t\t\t\t\t\t\ttaxontax)\n\t\t\t\t\t\tVALUES ('" . $StkMoveNo . "',\n\t\t\t\t\t\t\t'" . $Tax['TaxAuthID'] . "',\n\t\t\t\t\t\t\t'" . $Tax['TaxRate'] . "',\n\t\t\t\t\t\t\t'" . $Tax['TaxCalculationOrder'] . "',\n\t\t\t\t\t\t\t'" . $Tax['TaxOnTax'] . "')";
            $Result = DB_query($SQL, $db, '', '', true);
        }
        /*Insert Sales Analysis records */
        $SQL = "SELECT COUNT(*),\n\t\t\t\t\t\tsalesanalysis.stkcategory,\n\t\t\t\t\t\tsalesanalysis.area,\n\t\t\t\t\t\tsalesanalysis.salesperson,\n\t\t\t\t\t\tsalesanalysis.periodno,\n\t\t\t\t\t\tsalesanalysis.typeabbrev,\n\t\t\t\t\t\tsalesanalysis.cust,\n\t\t\t\t\t\tsalesanalysis.custbranch,\n\t\t\t\t\t\tsalesanalysis.stockid\n\t\t\t\t\tFROM salesanalysis,\n\t\t\t\t\t\tcustbranch,\n\t\t\t\t\t\tstockmaster\n\t\t\t\t\tWHERE salesanalysis.stkcategory=stockmaster.categoryid\n\t\t\t\t\tAND salesanalysis.stockid=stockmaster.stockid\n\t\t\t\t\tAND salesanalysis.cust=custbranch.debtorno\n\t\t\t\t\tAND salesanalysis.custbranch=custbranch.branchcode\n\t\t\t\t\tAND salesanalysis.area=custbranch.area\n\t\t\t\t\tAND salesanalysis.salesperson=custbranch.salesman\n\t\t\t\t\tAND salesanalysis.typeabbrev ='" . $OrderHeader['ordertype'] . "'\n\t\t\t\t\tAND salesanalysis.periodno='" . $PeriodNo . "'\n\t\t\t\t\tAND salesanalysis.cust " . LIKE . "  '" . $OrderHeader['debtorno'] . "'\n\t\t\t\t\tAND salesanalysis.custbranch  " . LIKE . " '" . $OrderHeader['branchcode'] . "'\n\t\t\t\t\tAND salesanalysis.stockid  " . LIKE . " '" . $OrderLineRow['stkcode'] . "'\n\t\t\t\t\tAND salesanalysis.budgetoractual='1'\n\t\t\t\t\tGROUP BY salesanalysis.stockid,\n\t\t\t\t\t\tsalesanalysis.stkcategory,\n\t\t\t\t\t\tsalesanalysis.cust,\n\t\t\t\t\t\tsalesanalysis.custbranch,\n\t\t\t\t\t\tsalesanalysis.area,\n\t\t\t\t\t\tsalesanalysis.periodno,\n\t\t\t\t\t\tsalesanalysis.typeabbrev,\n\t\t\t\t\t\tsalesanalysis.salesperson";
        $ErrMsg = _('The count of existing Sales analysis records could not run because');
        $DbgMsg = _('SQL to count the no of sales analysis records');
        $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
        $myrow = DB_fetch_row($Result);
        if ($myrow[0] > 0) {
            /*Update the existing record that already exists */
            $SQL = "UPDATE salesanalysis\n\t\t\t\t\t\tSET amt=amt+" . filter_number_format($OrderLineRow['unitprice'] * $OrderLineRow['quantity'] / $OrderHeader['rate']) . ",\n\t\t\t\t\t\tqty=qty +" . $OrderLineRow['quantity'] . ",\n\t\t\t\t\t\tdisc=disc+" . filter_number_format($OrderLineRow['discountpercent'] * $OrderLineRow['unitprice'] * $OrderLineRow['quantity'] / $OrderHeader['rate']) . "\n\t\t\t\t\t\tWHERE salesanalysis.area='" . $myrow[2] . "'\n\t\t\t\t\t\tAND salesanalysis.salesperson='" . $myrow[3] . "'\n\t\t\t\t\t\tAND typeabbrev ='" . $OrderHeader['ordertype'] . "'\n\t\t\t\t\t\tAND periodno = '" . $PeriodNo . "'\n\t\t\t\t\t\tAND cust  " . LIKE . " '" . $OrderHeader['debtorno'] . "'\n\t\t\t\t\t\tAND custbranch  " . LIKE . "  '" . $OrderHeader['branchcode'] . "'\n\t\t\t\t\t\tAND stockid  " . LIKE . " '" . $OrderLineRow['stkcode'] . "'\n\t\t\t\t\t\tAND salesanalysis.stkcategory ='" . $myrow[1] . "'\n\t\t\t\t\t\tAND budgetoractual='1'";
        } else {
            /* insert a new sales analysis record */
            $SQL = "INSERT INTO salesanalysis (\ttypeabbrev,\n\t\t\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\t\t\tamt,\n\t\t\t\t\t\t\t\t\t\t\t\t\tcost,\n\t\t\t\t\t\t\t\t\t\t\t\t\tcust,\n\t\t\t\t\t\t\t\t\t\t\t\t\tcustbranch,\n\t\t\t\t\t\t\t\t\t\t\t\t\tqty,\n\t\t\t\t\t\t\t\t\t\t\t\t\tdisc,\n\t\t\t\t\t\t\t\t\t\t\t\t\tstockid,\n\t\t\t\t\t\t\t\t\t\t\t\t\tarea,\n\t\t\t\t\t\t\t\t\t\t\t\t\tbudgetoractual,\n\t\t\t\t\t\t\t\t\t\t\t\t\tsalesperson,\n\t\t\t\t\t\t\t\t\t\t\t\t\tstkcategory )\n\t\t\t\t\t\t\t\tSELECT '" . $OrderHeader['ordertype'] . "',\n\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t'" . $OrderLineRow['unitprice'] * $OrderLineRow['quantity'] / $OrderHeader['rate'] . "',\n\t\t\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . "',\n\t\t\t\t\t\t\t\t\t'" . $OrderHeader['branchcode'] . "',\n\t\t\t\t\t\t\t\t\t'" . $OrderLineRow['quantity'] . "',\n\t\t\t\t\t\t\t\t\t'" . $OrderLineRow['discountpercent'] * $OrderLineRow['unitprice'] * $OrderLineRow['quantity'] / $OrderHeader['rate'] . "',\n\t\t\t\t\t\t\t\t\t'" . $OrderLineRow['stkcode'] . "',\n\t\t\t\t\t\t\t\t\tcustbranch.area,\n\t\t\t\t\t\t\t\t\t1,\n\t\t\t\t\t\t\t\t\tcustbranch.salesman,\n\t\t\t\t\t\t\t\t\tstockmaster.categoryid\n\t\t\t\t\t\t\t\tFROM stockmaster, custbranch\n\t\t\t\t\t\t\t\tWHERE stockmaster.stockid = '" . $OrderLineRow['stkcode'] . "'\n\t\t\t\t\t\t\t\tAND custbranch.debtorno = '" . $OrderHeader['debtorno'] . "'\n\t\t\t\t\t\t\t\tAND custbranch.branchcode='" . $OrderHeader['branchcode'] . "'";
        }
        $Result = api_DB_query($SQL, $db, '', '', true);
        if ($CompanyRecord['gllink_stock'] == 1 and $StandardCost != 0) {
            /*first the cost of sales entry - GL accounts are retrieved using the function GetCOGSGLAccount from includes/GetSalesTransGLCodes.inc  */
            $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\t\t\tVALUES (10,\n\t\t\t\t\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . GetCOGSGLAccount($OrderHeader['area'], $OrderLineRow['stkcode'], $OrderHeader['ordertype'], $db) . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . " - " . $OrderLineRow['stkcode'] . " x " . $OrderLineRow['quantity'] . " @ " . $StandardCost . "',\n\t\t\t\t\t\t\t\t\t\t'" . $StandardCost * $OrderLineRow['quantity'] . "')";
            $Result = api_DB_query($SQL, $db, '', '', true);
            /*now the stock entry - this is set to the cost act in the case of a fixed asset disposal */
            $StockGLCode = GetStockGLCode($OrderLineRow['stkcode'], $db);
            $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\t\t\tVALUES (10,\n\t\t\t\t\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $StockGLCode['stockact'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . " - " . $OrderLineRow['stkcode'] . " x " . $OrderLineRow['quantity'] . " @ " . $StandardCost . "',\n\t\t\t\t\t\t\t\t\t\t'" . -$StandardCost * $OrderLineRow['quantity'] . "')";
            $Result = api_DB_query($SQL, $db, '', '', true);
        }
        /* end of if GL and stock integrated and standard cost !=0  and not an asset */
        if ($CompanyRecord['gllink_debtors'] == 1 and $OrderLineRow['unitprice'] != 0) {
            //Post sales transaction to GL credit sales
            $SalesGLAccounts = GetSalesGLAccount($OrderHeader['area'], $OrderLineRow['stkcode'], $OrderHeader['ordertype'], $db);
            $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\tamount )\n\t\t\t\t\tVALUES ('10',\n\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t'" . $SalesGLAccounts['salesglcode'] . "',\n\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . " - " . $OrderLineRow['stkcode'] . " x " . $OrderLineRow['quantity'] . " @ " . $OrderLineRow['unitprice'] . "',\n\t\t\t\t\t\t'" . -$OrderLineRow['unitprice'] * $OrderLineRow['quantity'] / $OrderHeader['rate'] . "'\n\t\t\t\t\t)";
            $Result = api_DB_query($SQL, $db, '', '', true);
            if ($OrderLineRow['discountpercent'] != 0) {
                $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\tVALUES (10,\n\t\t\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t'" . $SalesGLAccounts['discountglcode'] . "',\n\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . " - " . $OrderLineRow['stkcode'] . " @ " . $OrderLineRow['discountpercent'] * 100 . "%',\n\t\t\t\t\t\t\t\t'" . $OrderLineRow['unitprice'] * $OrderLineRow['quantity'] * $OrderLineRow['discountpercent'] / $OrderHeader['rate'] . "')";
                $Result = DB_query($SQL, $db, '', '', true);
            }
            /*end of if discount !=0 */
        }
        /*end of if sales integrated with gl */
        $LineCounter++;
        //needed for the array of taxes by line
    }
    /*end of OrderLine loop */
    $TotalInvLocalCurr = ($TotalFXNetInvoice + $TotalFXTax) / $OrderHeader['rate'];
    if ($CompanyRecord['gllink_debtors'] == 1) {
        /*Now post the tax to the GL at local currency equivalent */
        if ($CompanyRecord['gllink_debtors'] == 1 and $TaxAuthAmount != 0) {
            /*Loop through the tax authorities array to post each total to the taxauth glcode */
            foreach ($TaxTotals as $Tax) {
                $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\t\tamount )\n\t\t\t\t\t\t\t\t\t\t\tVALUES (10,\n\t\t\t\t\t\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $Tax['GLCode'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . "-" . $Tax['TaxAuthDescription'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . -$Tax['FXAmount'] / $OrderHeader['rate'] . "' )";
                $Result = api_DB_query($SQL, $db, '', '', true);
            }
        }
        /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
        if ($TotalInvLocalCurr != 0) {
            $SQL = "INSERT INTO gltrans (type,\n\t\t\t\t\t\t\t\t\t\t\ttypeno,\n\t\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\t\tperiodno,\n\t\t\t\t\t\t\t\t\t\t\taccount,\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\n\t\t\t\t\t\t\t\t\t\t\tamount)\n\t\t\t\t\t\t\t\t\tVALUES ('10',\n\t\t\t\t\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $CompanyRecord['debtorsact'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $TotalInvLocalCurr . "')";
            $Result = api_DB_query($SQL, $db, '', '', true);
        }
        EnsureGLEntriesBalance(10, $InvoiceNo, $db);
    }
    /*end of if Sales and GL integrated */
    /*Update order header for invoice charged on */
    $SQL = "UPDATE salesorders SET comments = CONCAT(comments,' Inv ','" . $InvoiceNo . "') WHERE orderno= '" . $OrderNo . "'";
    $Result = api_DB_query($SQL, $db, '', '', true);
    /*Now insert the DebtorTrans */
    $SQL = "INSERT INTO debtortrans (transno,\n\t\t\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\t\t\tdebtorno,\n\t\t\t\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\t\t\t\ttrandate,\n\t\t\t\t\t\t\t\t\t\tinputdate,\n\t\t\t\t\t\t\t\t\t\tprd,\n\t\t\t\t\t\t\t\t\t\treference,\n\t\t\t\t\t\t\t\t\t\ttpe,\n\t\t\t\t\t\t\t\t\t\torder_,\n\t\t\t\t\t\t\t\t\t\tovamount,\n\t\t\t\t\t\t\t\t\t\tovgst,\n\t\t\t\t\t\t\t\t\t\trate,\n\t\t\t\t\t\t\t\t\t\tshipvia,\n\t\t\t\t\t\t\t\t\t\tsalesperson)\n\t\t\t\t\t\t\t\t\tVALUES (\n\t\t\t\t\t\t\t\t\t\t'" . $InvoiceNo . "',\n\t\t\t\t\t\t\t\t\t\t10,\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['debtorno'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['branchcode'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['orddate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . date('Y-m-d H-i-s') . "',\n\t\t\t\t\t\t\t\t\t\t'" . $PeriodNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['customerref'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['ordertype'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderNo . "',\n\t\t\t\t\t\t\t\t\t\t'" . $TotalFXNetInvoice . "',\n\t\t\t\t\t\t\t\t\t\t'" . $TotalFXTax . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['rate'] . "',\n\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['shipvia'] . "',\n+\t\t\t\t\t\t\t\t\t\t'" . $OrderHeader['salesman'] . "')";
    $Result = api_DB_query($SQL, $db, '', '', true);
    $DebtorTransID = DB_Last_Insert_ID($db, 'debtortrans', 'id');
    /*for each Tax - need to insert into debtortranstaxes */
    foreach ($TaxTotals as $TaxAuthID => $Tax) {
        $SQL = "INSERT INTO debtortranstaxes (debtortransid,\n\t\t\t\t\t\t\t\t\t\t\t\ttaxauthid,\n\t\t\t\t\t\t\t\t\t\t\t\ttaxamount)\n\t\t\t\t\t\t\t\tVALUES ('" . $DebtorTransID . "',\n\t\t\t\t\t\t\t\t\t\t'" . $TaxAuthID . "',\n\t\t\t\t\t\t\t\t\t\t'" . $Tax['FXAmount'] / $OrderHeader['rate'] . "')";
        $Result = api_DB_query($SQL, $db, '', '', true);
    }
    if (sizeof($Errors) == 0) {
        $Result = DB_Txn_Commit($db);
        $Errors[0] = 0;
        $Errors[1] = $InvoiceNo;
    } else {
        $Result = DB_Txn_Rollback($db);
    }
    return $Errors;
}
コード例 #13
0
        }
        $msg = _('Tax category name changed');
    } elseif ($InputError != 1) {
        /*SelectedTaxCategory is null cos no item selected on first time round so must be adding a record*/
        $sql = "SELECT count(*) FROM taxcategories\n\t\t\t\tWHERE taxcatname " . LIKE . " '" . $_POST['TaxCategoryName'] . "'";
        $result = DB_query($sql, $db);
        $myrow = DB_fetch_row($result);
        if ($myrow[0] > 0) {
            $InputError = 1;
            prnMsg(_('The tax category cannot be created because another with the same name already exists'), 'error');
        } else {
            $result = DB_Txn_Begin($db);
            $sql = "INSERT INTO taxcategories (\n\t\t\t\t\t\ttaxcatname )\n\t\t\t\tVALUES (\n\t\t\t\t\t'" . $_POST['TaxCategoryName'] . "'\n\t\t\t\t\t)";
            $ErrMsg = _('The new tax category could not be added');
            $result = DB_query($sql, $db, $ErrMsg, true);
            $LastTaxCatID = DB_Last_Insert_ID($db, 'taxcategories', 'taxcatid');
            $sql = 'INSERT INTO taxauthrates (taxauthority,
					dispatchtaxprovince,
					taxcatid)
				SELECT taxauthorities.taxid,
 					taxprovinces.taxprovinceid,
					' . $LastTaxCatID . '
				FROM taxauthorities CROSS JOIN taxprovinces';
            $result = DB_query($sql, $db, $ErrMsg, true);
            $result = DB_Txn_Commit($db);
        }
        $msg = _('New tax category added');
    }
    if ($InputError != 1) {
        prnMsg($msg, 'success');
    }
コード例 #14
0
    }
    if (isset($SelectedTaxAuthID)) {
        /*SelectedTaxAuthID could also exist if submit had not been clicked this code
        		would not run in this case cos submit is false of course  see the
        		delete code below*/
        $sql = "UPDATE taxauthorities\n\t\t\t\t\tSET taxglcode ='" . $_POST['TaxGLCode'] . "',\n\t\t\t\t\tpurchtaxglaccount ='" . $_POST['PurchTaxGLCode'] . "',\n\t\t\t\t\tdescription = '" . $_POST['Description'] . "',\n\t\t\t\t\tbank = '" . $_POST['Bank'] . "',\n\t\t\t\t\tbankacctype = '" . $_POST['BankAccType'] . "',\n\t\t\t\t\tbankacc = '" . $_POST['BankAcc'] . "',\n\t\t\t\t\tbankswift = '" . $_POST['BankSwift'] . "'\n\t\t\t\tWHERE taxid = '" . $SelectedTaxAuthID . "'";
        $ErrMsg = _('The update of this tax authority failed because');
        $result = DB_query($sql, $db, $ErrMsg);
        $msg = _('The tax authority for record has been updated');
    } elseif ($InputError != 1) {
        /*Selected tax authority is null cos no item selected on first time round so must be adding a	record must be submitting new entries in the new tax authority form */
        $sql = "INSERT INTO taxauthorities (\n\t\t\t\t\t\ttaxglcode,\n\t\t\t\t\t\tpurchtaxglaccount,\n\t\t\t\t\t\tdescription,\n\t\t\t\t\t\tbank,\n\t\t\t\t\t\tbankacctype,\n\t\t\t\t\t\tbankacc,\n\t\t\t\t\t\tbankswift)\n\t\t\tVALUES (\n\t\t\t\t'" . $_POST['TaxGLCode'] . "',\n\t\t\t\t'" . $_POST['PurchTaxGLCode'] . "',\n\t\t\t\t'" . $_POST['Description'] . "',\n\t\t\t\t'" . $_POST['Bank'] . "',\n\t\t\t\t'" . $_POST['BankAccType'] . "',\n\t\t\t\t'" . $_POST['BankAcc'] . "',\n\t\t\t\t'" . $_POST['BankSwift'] . "'\n\t\t\t\t)";
        $Errmsg = _('The addition of this tax authority failed because');
        $result = DB_query($sql, $db, $ErrMsg);
        $msg = _('The new tax authority record has been added to the database');
        $NewTaxID = DB_Last_Insert_ID($db, 'taxauthorities', 'taxid');
        $sql = "INSERT INTO taxauthrates (\n\t\t\t\t\ttaxauthority,\n\t\t\t\t\tdispatchtaxprovince,\n\t\t\t\t\ttaxcatid\n\t\t\t\t\t)\n\t\t\t\tSELECT\n\t\t\t\t\t'" . $NewTaxID . "',\n\t\t\t\t\ttaxprovinces.taxprovinceid,\n\t\t\t\t\ttaxcategories.taxcatid\n\t\t\t\tFROM taxprovinces,\n\t\t\t\t\ttaxcategories";
        $InsertResult = DB_query($sql, $db);
    }
    //run the SQL from either of the above possibilites
    if (isset($InputError) and $InputError != 1) {
        unset($_POST['TaxGLCode']);
        unset($_POST['PurchTaxGLCode']);
        unset($_POST['Description']);
        unset($SelectedTaxID);
    }
    prnMsg($msg);
} elseif (isset($_GET['delete'])) {
    //the link to delete a selected record was clicked instead of the submit button
    // PREVENT DELETES IF DEPENDENT RECORDS IN OTHER TABLES
    $sql = "SELECT COUNT(*)\n\t\t\tFROM taxgrouptaxes\n\t\tWHERE taxauthid='" . $SelectedTaxAuthID . "'";
コード例 #15
0
ファイル: SpecialOrder.php プロジェクト: xuxudodo/weberp-cvs
     $result = DB_query($sql, $db, $ErrMsg, $DbgMsg, true);
 }
 /* end of the loop round the detail line items on the order */
 echo '<BR><BR>' . _('Purchase order') . ' ' . $_SESSION['SPL']->PurchOrderNo . ' ' . _('on') . ' ' . $_SESSION['SPL']->SupplierName . ' ' . _('has been created');
 echo "<BR><A HREF='{$rootpath}/PO_PDFPurchOrder.php?" . SID . '&OrderNo=' . $_SESSION['SPL']->PurchOrderNo . "'>" . _('Print Purchase Order') . '</A>';
 /*Now insert the sales order too */
 /*First get the customer delivery information */
 $sql = "SELECT salestype,\n\t\t\t\tbrname,\n\t\t\t\tbraddress1,\n\t\t\t\tbraddress2,\n\t\t\t\tbraddress3,\n\t\t\t\tbraddress4,\n\t\t\t\tbraddress5,\n\t\t\t\tbraddress6,\n\t\t\t\tdefaultshipvia,\n\t\t\t\temail,\n\t\t\t\tphoneno\n\t\t\tFROM custbranch INNER JOIN debtorsmaster\n\t\t\t\tON custbranch.debtorno=debtorsmaster.debtorno\n\t\t\tWHERE custbranch.debtorno='" . $_SESSION['SPL']->CustomerID . "'\n\t\t\tAND custbranch.branchcode = '" . $_SESSION['SPL']->BranchCode . "'";
 $ErrMsg = _('The delivery and sales type for the customer could not be retrieved for this special order') . " " . $SPLLine->LineNo . " " . _('because');
 $DbgMsg = _('The SQL statement used to get the delivery details and that failed was');
 $result = DB_query($sql, $db, $ErrMsg, $DbgMsg, true);
 $BranchDetails = DB_fetch_array($result);
 $HeaderSQL = "INSERT INTO salesorders (debtorno,\n\t\t\t\t\t\t\tbranchcode,\n\t\t\t\t\t\t\tcustomerref,\n\t\t\t\t\t\t\torddate,\n\t\t\t\t\t\t\tordertype,\n\t\t\t\t\t\t\tshipvia,\n\t\t\t\t\t\t\tdeliverto,\n\t\t\t\t\t\t\tdeladd1,\n\t\t\t\t\t\t\tdeladd2,\n\t\t\t\t\t\t\tdeladd3,\n\t\t\t\t\t\t\tdeladd4,\n\t\t\t\t\t\t\tdeladd5,\n\t\t\t\t\t\t\tdeladd6,\n\t\t\t\t\t\t\tcontactphone,\n\t\t\t\t\t\t\tcontactemail,\n\t\t\t\t\t\t\tfromstkloc,\n\t\t\t\t\t\t\tdeliverydate)\n\t\t\t\t\tVALUES ('" . $_SESSION['SPL']->CustomerID . "',\n\t\t\t\t\t\t'" . $_SESSION['SPL']->BranchCode . "',\n\t\t\t\t\t\t'" . $_SESSION['SPL']->CustRef . "',\n\t\t\t\t\t\t'" . Date("Y-m-d") . "',\n\t\t\t\t\t\t'" . $BranchDetails['salestype'] . "',\n\t\t\t\t\t\t" . $BranchDetails['defaultshipvia'] . ",\n\t\t\t\t\t\t'" . $BranchDetails['brname'] . "',\n\t\t\t\t\t\t'" . $BranchDetails['braddress1'] . "',\n\t\t\t\t\t\t'" . $BranchDetails['braddress2'] . "',\n\t\t\t\t\t\t'" . $BranchDetails['braddress3'] . "',\n\t\t\t\t\t\t'" . $BranchDetails['braddress4'] . "',\n\t\t\t\t\t\t'" . $BranchDetails['braddress5'] . "',\n\t\t\t\t\t\t'" . $BranchDetails['braddress6'] . "',\n\t\t\t\t\t\t'" . $BranchDetails['phoneno'] . "',\n\t\t\t\t\t\t'" . $BranchDetails['email'] . "',\n\t\t\t\t\t\t'" . $_SESSION['SPL']->StkLocation . "',\n\t\t\t\t\t\t'" . $OrderDate . "')";
 $ErrMsg = _('The sales order cannot be added because');
 $InsertQryResult = DB_query($HeaderSQL, $db, $ErrMsg);
 $SalesOrderNo = DB_Last_Insert_ID($db, 'salesorders', 'orderno');
 $StartOf_LineItemsSQL = "INSERT INTO salesorderdetails (orderno,\n\t\t\t\t\t\t\t\t\tstkcode,\n\t\t\t\t\t\t\t\t\tunitprice,\n\t\t\t\t\t\t\t\t\tquantity,\n\t\t\t\t\t\t\t\t\torderlineno)\n\t\t\t\t\t\tVALUES (" . $SalesOrderNo;
 $ErrMsg = _('There was a problem inserting a line into the sales order because');
 foreach ($_SESSION['SPL']->LineItems as $StockItem) {
     $LineItemsSQL = $StartOf_LineItemsSQL . ", '" . $StockItem->PartCode . "'," . $StockItem->Price . ", " . $StockItem->Quantity . ", " . $StockItem->LineNo . ")";
     $Ins_LineItemResult = DB_query($LineItemsSQL, $db, $ErrMsg);
 }
 /* inserted line items into sales order details */
 unset($_SESSION['SPL']);
 prnMsg(_('Sales Order Number') . ' ' . $SalesOrderNo . ' ' . _('has been entered') . '. <P>' . _('Orders created on a cash sales account may need the delivery details for the order to be modified') . '. <br><br>' . _('A freight charge may also be applicable'), 'success');
 if (count($_SESSION['AllowedPageSecurityTokens']) > 1) {
     /* Only allow print of packing slip for internal staff - customer logon's cannot go here */
     echo "<P><A HREF='{$rootpath}/PrintCustOrder.php?" . SID . "&TransNo=" . $SalesOrderNo . "'>" . _('Print packing slip') . ' (' . _('Preprinted stationery') . ')</A>';
     echo "<P><A HREF='{$rootpath}/PrintCustOrder_generic.php?" . SID . '&TransNo=' . $SalesOrderNo . "'>" . _('Print packing slip') . ' (' . _('Laser') . ')</A>';
 }
 $Result = DB_Txn_Commit($db);