コード例 #1
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid invoice number.");
    }
    if (isset($deptid)) {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    if (isset($letters)) {
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>{$e['msg']}</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($deptid)) {
        $deptid = 0;
    } else {
        if (isset($invid)) {
            db_conn("cubit");
            $sql = "UPDATE rec_invoices SET deptid='{$deptid}' WHERE invid='{$invid}' AND deptid<>'{$deptid}'";
            db_exec($sql) or errDie("Error updating invoice department.");
        }
    }
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
        $stkerr = "0,0";
    }
    if (!isset($done)) {
        $done = "";
    }
    if (!isset($stkerr)) {
        $stkerr = "0,0";
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM rec_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected Customer info
    if (isset($letters)) {
        db_connect();
        $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['cusnum']}' AND div = '" . USER_DIV . "'";
        $custRslt = db_exec($sql) or errDie("Unable to view customer");
        if (pg_numrows($custRslt) < 1) {
            db_connect();
            if ($inv['deptid'] == 0) {
                $searchdept = "";
            } else {
                $searchdept = "deptid = '{$inv['deptid']}' AND ";
            }
            # Query server for customer info
            $sql = "\n\t\t\t\tSELECT * FROM customers \n\t\t\t\tWHERE {$searchdept} location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' \n\t\t\t\tORDER BY surname";
            $custRslt = db_exec($sql) or errDie("Unable to view customers");
            if (pg_numrows($custRslt) < 1) {
                $ajax_err = "<li class='err'>No customer names starting with <b>{$letters}</b> in database.</li>";
                //return view_err($_POST, $err);
            } else {
                $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
                $customers .= "<option value='-S' selected>Select Customer</option>";
                while ($cust = pg_fetch_array($custRslt)) {
                    $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
                }
                $customers .= "</select>";
            }
            # Take care of the unset vars
            $cust['addr1'] = "";
            $cust['cusnum'] = "";
            $cust['vatnum'] = "";
            $cust['accno'] = "";
        } else {
            $cust = pg_fetch_array($custRslt);
            $sql = "SELECT * FROM customers WHERE deptid = '{$inv['deptid']}' AND location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
            $cusRslt = db_exec($sql) or errDie("Unable to view customers");
            # Moarn if customer account has been blocked
            if ($cust['blocked'] == 'yes') {
                $error .= "<li class='err'>Error : Selected customer account has been blocked.</li>";
            }
            // $customers = "<input type=hidden name=cusnum value='$cust[cusnum]'>$cust[cusname]  $cust[surname]";
            $cusnum = $cust['cusnum'];
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            // $customers .= "<option value='-S' selected>Select Customer</option>";
            while ($cus = pg_fetch_array($cusRslt)) {
                $sel = "";
                if ($cust['cusnum'] == $cus['cusnum']) {
                    $sel = "selected";
                }
                $customers .= "<option value='{$cus['cusnum']}' {$sel}>{$cus['cusname']} {$cus['surname']}</option>";
            }
            $customers .= "</select>";
        }
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    //	$whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $whs = "<select name='whidss[]'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            if (!user_in_store_team($wh["whid"], USER_ID)) {
                continue;
            }
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # Get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class='err'> There are no Sales People found in Cubit.</li>";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $inv['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # Days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $inv['terms']);
    # Keep the charge vat option stable
    if ($inv['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($inv['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    # Format date
    list($rinv_year, $rinv_month, $rinv_day) = explode("-", $inv['odate']);
    /* --- End Drop Downs --- */
    // get the ID of the first warehouse
    db_conn("exten");
    $sql = "SELECT whid FROM warehouses ORDER BY whid ASC LIMIT 1";
    $rslt = db_exec($sql) or errDie("Error reading warehouses (FWH).");
    if (pg_num_rows($rslt) > 0) {
        $FIRST_WH = pg_fetch_result($rslt, 0, 0);
    } else {
        $FIRST_WH = "-S";
    }
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>UNIT DISCOUNT</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this invoice
    db_connect();
    $sql = "SELECT * FROM recinv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            db_conn('core');
            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
            $Accounts = "\n\t\t\t\t<select name='accounts[]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
            while ($ad = pg_fetch_array($Ri)) {
                if (isb($ad['accid'])) {
                    continue;
                }
                if ($ad['accid'] == $stkd['account']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
            }
            $Accounts .= "</select>";
            $sernos = "";
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
            $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>";
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            # Put in product
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whids[]' value='{$stkd['whid']}'></td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$stkd['description']}'> {$sernos}</td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td><input type='hidden' name='disc[]' value='{$stkd['disc']}'><input type='hidden' name='discp[]' value='{$stkd['discp']}'></td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " " . sprint($stkd['amt']) . "</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t</tr>";
            $key++;
        } else {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # Get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # Get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            $sernos = "<input type='hidden' name='sernos[]' value='{$stkd['serno']}'>{$stkd['serno']}";
            # check permissions
            if (perm("invoice-unitcost-edit.php")) {
                $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>";
            } else {
                $viewcost = "<input type='hidden' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>{$stkd['unitcost']}";
            }
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            # Put in product
            $products .= "\n\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t{$sernos}\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td><input type='text' size='4' name='disc[]' value='{$stkd['disc']}'> OR <input type='text' size='4' name='discp[]' value='{$stkd['discp']}' maxlength='5'>%</td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " " . sprint($stkd['amt']) . "</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t</tr>";
            $key++;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # Look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    } else {
        $SCROLL = "yes";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && isset($cust['pricelist'])) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # Get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # Get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                $sernos = "<input type='hidden' name='sernos[]' value=''>";
                # Get price from price list if it is set
                if (isset($cust['pricelist'])) {
                    # get selected stock in this warehouse
                    db_conn("exten");
                    $sql = "SELECT price FROM plist_prices WHERE listid = '{$cust['pricelist']}' AND stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
                    $plRslt = db_exec($sql);
                    if (pg_numrows($plRslt) > 0) {
                        $pl = pg_fetch_array($plRslt);
                        $stk['selamt'] = $pl['price'];
                    }
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class='err'>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                $stk['selamt'] = sprint($stk['selamt']);
                # Check permissions
                if (perm("invoice-unitcost-edit.php")) {
                    $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stk['selamt']}'>";
                } else {
                    $viewcost = "<input type='hidden' size='8' name='unitcost[]' value='{$stk['selamt']}'>{$stk['selamt']}";
                }
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $amt[$key] = sprint($amt[$key]);
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stk['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t{$sernos}\n\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'></td>\n\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t<td><input type='text' size='4' name='disc[]' value='{$discs[$key]}'> OR <input type='text' size='4' name='discp[]' value='{$discps[$key]}' maxlength='5'>%</td>\n\t\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
            } elseif (isset($accountss[$key]) && $accountss[$key] != "0" && isset($cust['pricelist'])) {
                db_conn('core');
                $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                if (pg_num_rows($Ri) < 1) {
                    return "invalid.";
                }
                $ad = pg_fetch_array($Ri);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                # Input qty if not serialised
                $qtyin = "<input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'>";
                # Check permissions
                $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$unitcosts[$key]}'>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vatcodess[$key] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='2'>{$ad['accname']}<input type='hidden' name='accounts[]' value='{$accountss[$key]}'><input type='hidden' name='whids[]' value='0'></td>\n\t\t\t\t\t\t<td>{$Vatcodes}<input type='hidden' name='stkids[]' value='0'></td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$descriptionss[$key]}'></td>\n\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='disc[]' value='0'><input type='hidden' name='discp[]' value='0'></td>\n\t\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
            } else {
                if (!isset($diffwhBtn)) {
                    # skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    if (!isset($addnon)) {
                        # get warehouse name
                        db_conn("exten");
                        $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                        $whRslt = db_exec($sql);
                        $wh = pg_fetch_array($whRslt);
                        if (isset($ria) && $ria != "") {
                            $len = strlen($ria);
                            if ($ria == "Show All") {
                                $Wh = "";
                                $ria = "";
                            } else {
                                $Wh = "AND (lower(stkdes) LIKE lower('%{$ria}%')) OR (lower(stkcod) LIKE lower('%{$ria}%'))";
                                //								$Wh = "AND lower(substr(stkcod,1,'$len'))=lower('$ria')";
                            }
                        } else {
                            $Wh = "AND FALSE";
                            $ria = "";
                        }
                        # get stock on this warehouse
                        db_connect();
                        $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' AND serd = 'no' {$Wh} ORDER BY stkcod ASC";
                        $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                        if (pg_numrows($stkRslt) < 1) {
                            $error .= "<li class='err'>There are no stock items in the selected warehouse.</li>";
                            continue;
                        }
                        if (pg_numrows($stkRslt) == 1) {
                            $ex = "selected";
                        } else {
                            $ex = "";
                        }
                        if (!isset($sel_frm) || $sel_frm == "stkcod") {
                            $cods = "<select class='width : 15'name='stkidss[]' onChange='javascript:document.form.submit();'>";
                            $cods .= "<option value='-S' disabled selected>Select Number</option>";
                            $count = 0;
                            while ($stk = pg_fetch_array($stkRslt)) {
                                $cods .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                            }
                            $cods .= "</select> ";
                            $descs = "";
                        } else {
                            $descs = "<select class='width : 15'name='stkidss[]' onChange='javascript:document.form.submit();'>";
                            $descs .= "<option value='-S' disabled selected>Select Description</option>";
                            $count = 0;
                            while ($stk = pg_fetch_array($stkRslt)) {
                                $descs .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                            }
                            $descs .= "</select> ";
                            $cods = "";
                        }
                        # put in drop down and warehouse
                        $products .= "\n\t\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t<td>{$cods}</td>\n\t\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t\t<td>{$descs}</td>\n\t\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]'  value='1'></td>\n\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t<td><input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='amts[]' value='0.00'>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t</tr>";
                    } else {
                        db_conn('core');
                        $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                        $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                        $Accounts = "\n\t\t\t\t\t\t\t<select name='accountss[]'  onChange='javascript:document.form.submit();'>\n\t\t\t\t\t\t\t\t<option value='0'>Select Account</option>";
                        while ($ad = pg_fetch_array($Ri)) {
                            if (isb($ad['accid'])) {
                                continue;
                            }
                            $Accounts .= "<option value={$ad['accid']}>{$ad['accname']}</option>";
                        }
                        $Accounts .= "</select>";
                        db_conn('cubit');
                        $Sl = "SELECT * FROM vatcodes ORDER BY code";
                        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                        $Vatcodes = "\n\t\t\t\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t\t\t\t<option value='0'>Select</option>";
                        while ($vd = pg_fetch_array($Ri)) {
                            if ($vd['del'] == "Yes") {
                                $sel = "selected";
                            } else {
                                $sel = "";
                            }
                            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                        }
                        $Vatcodes .= "</select>";
                        //<input type='hidden' name='stkidss[]' value=''>
                        $products .= "\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[]' value='{$FIRST_WH}'></td>\n\t\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='discs[]' value='0'><input type='hidden' name='discps[]' value='0' ></td>\n\t\t\t\t\t\t\t</tr>";
                    }
                }
            }
        }
    } else {
        if (!(isset($diffwhBtn) || isset($addnon))) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                if (isset($wtd) && $wtd != 0) {
                    $whid = $wtd;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                if (isset($ria) && $ria != "") {
                    $len = strlen($ria);
                    if ($ria == "Show All") {
                        $Wh = "";
                        $ria = "";
                    } else {
                        $Wh = "AND lower(substr(stkcod,1,'{$len}'))=lower('{$ria}')";
                        $ria = "";
                    }
                } else {
                    $Wh = "";
                    $ria = "";
                }
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' AND serd = 'no' {$Wh} ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected store.</li>";
                    //ontinue;
                }
                $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                $stks .= "<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "\n\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t<input type='hidden' name='vatcodess[]' value=''>\n\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t<td><input type='hidden' size='3' name='qtyss[]' value='1'>1</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t<td><input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t</tr>";
            } else {
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td><input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t</tr>";
            }
        } else {
            if (isset($addnon)) {
                db_conn('core');
                $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                $Accounts = "\n\t\t\t\t<select name='accountss[]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
                while ($ad = pg_fetch_array($Ri)) {
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                }
                $Accounts .= "</select>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vd['del'] == "Yes") {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[]' value='{$FIRST_WH}'></td>\n\t\t\t\t\t<input type='hidden' name='stkidss[]' value='-S'>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td><input type='hidden' name='discs[]' value='0'><input type='hidden' name='discps[]' value='0'></td>\n\t\t\t\t</tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$whs}</td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td></td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td><input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td></td>\n\t\t\t</tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    # Calculate tradediscm
    if ($inv['traddisc'] > 0) {
        $traddiscm = sprint($inv['traddisc'] / 100 * ($SUBTOT + $inv['delchrg']));
    } else {
        $traddiscm = "0.00";
    }
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    $VAT = sprint($inv['vat']);
    $TOTAL = sprint($inv['total']);
    $inv['delchrg'] = sprint($inv['delchrg']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    # check only if the customer is selected
    if (isset($cusnum) && $cusnum != "-S") {
        #check againg credit limit
        if ($TOTAL + $cust['balance'] > $cust['credlimit']) {
            $error .= "<li class='err'>Warning : Customers Credit limit of <b>" . CUR . " " . sprint($cust["credlimit"]) . "</b> has been exceeded";
        }
        $avcred = $cust['credlimit'] - $cust['balance'];
    } else {
        $avcred = "0.00";
    }
    $inv['delvat'] += 0;
    if ($inv['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $inv['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $inv['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    db_conn('cubit');
    $Sl = "SELECT * FROM costcenters";
    $Ri = db_exec($Sl);
    if (pg_num_rows($Ri) > 0) {
        $ctd = "\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th>Cost Center</th>\n\t\t\t\t\t<th>Percentage</th>\n\t\t\t\t</tr>";
        $i = 0;
        while ($data = pg_fetch_array($Ri)) {
            $Sl = "SELECT * FROM invc WHERE inv='{$invid}' AND cid='{$data['ccid']}'";
            $Rq = db_exec($Sl);
            $cd = pg_fetch_array($Rq);
            $ctd .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$data['centername']}</td>\n\t\t\t\t\t<td><input type='text' name='ct[{$data['ccid']}]' size='5' value='{$cd['amount']}'>%</td>\n\t\t\t\t</tr>";
            $i++;
        }
        $ctd .= "</table>";
    } else {
        $ctd = "";
    }
    // Retrieve default comments from Cubit
    if (empty($inv["comm"])) {
        db_conn("cubit");
        $sql = "SELECT value FROM settings WHERE constant='DEFAULT_COMMENTS'";
        $commRslt = db_exec($sql) or errDie("Unable to retrieve default comment from Cubit.");
        $comment = base64_decode(pg_fetch_result($commRslt, 0));
    } else {
        $comment = $inv["comm"];
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    // Which display method was selected
    if (isset($sel_frm) && $sel_frm == "stkdes") {
        $sel_frm_cod = "";
        $sel_frm_des = "checked";
    } else {
        $sel_frm_cod = "checked";
        $sel_frm_des = "";
    }
    /*--- Start checks --- */
    /* -- Final Layout -- */
    $details_begin = "\n\t\t<center>\n\t\t<h3>Recurring Invoice</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<div id='cust_selection'>";
    if (empty($ajax_err) && (isset($cusnum) || AJAX)) {
        if (isset($cusnum)) {
            $OTS_OPT = onthespot_encode(SELF, "cust_selection", "deptid={$inv['deptid']}&letters={$letters}&cusnum={$cusnum}&invid={$invid}");
            $custedit = "\n\t\t\t\t<td nowrap>\n\t\t\t\t\t<a href='javascript: popupSized(\"cust-edit.php?cusnum={$cusnum}&onthespot={$OTS_OPT}\", \"edit_cust\", 700, 630);'>\n\t\t\t\t\t\tEdit Customer Details\n\t\t\t\t\t</a>\n\t\t\t\t</td>";
        } else {
            $custedit = "";
        }
        $ajaxOut = "\n\t\t\t<input type='hidden' name='letters' value='{$letters}'>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account No.</td>\n\t\t\t\t\t<td valign='center'>{$cust['accno']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t<td valign='center'>{$customers}</td>\n\t\t\t\t\t{$custedit}\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t<td valign='center'>" . nl2br($cust['addr1']) . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer Order number</td>\n\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$inv['cordno']}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer VAT Number</td>\n\t\t\t\t\t<td>{$cust['vatnum']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'>Point of Sale</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Barcode</td>\n\t\t\t\t\t<td><input type='text' size='13' name='bar' value=''></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Using</td>\n\t\t\t\t\t<td>Stock Code<input type='radio' name='sel_frm' value='stkcod' onChange='javascript:document.form.submit();' {$sel_frm_cod}><br>Stock Description<input type='radio' name='sel_frm' value='stkdes' onChange='javascript:document.form.submit();' {$sel_frm_des}></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "' " . ass("Type the first letters of the stock code you are looking for.") . ">\n\t\t\t\t\t<td>Stock Filter</td>\n\t\t\t\t\t<td nowrap><input type='text' size='13' name='ria' value='{$ria}'> <input type='submit' value='Search'> <input type='submit' name='ria' value='Show All'></td>\n\t\t\t\t</tr>\n\t\t\t</table>";
    } else {
        # Query server for depts
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE div = '" . USER_DIV . "' ORDER BY deptname ASC";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($deptRslt) < 1) {
            return "<li class='err'>There are no Departments found in Cubit.</li>";
        } else {
            $depts = "<select id='deptid'>";
            $depts .= "<option value='0'>All Departments</option>";
            while ($dept = pg_fetch_array($deptRslt)) {
                $depts .= "<option value='{$dept['deptid']}'>{$dept['deptname']}</option>";
            }
            $depts .= "</select>";
        }
        if (!isset($ajax_err)) {
            $ajax_err = "";
        }
        $ajaxOut = "\n\t\t\t<script>\n\t\t\t\tfunction updateCustSelection() {\n\t\t\t\t\tdeptid = getObject('deptid').value;\n\t\t\t\t\tletters = getObject('letters').value;\n\t\t\t\t\tajaxRequest('" . SELF . "', 'cust_selection', AJAX_SET, 'letters='+letters+'&deptid='+deptid+'&invid={$invid}');\n\t\t\t\t}\n\t\t\t</script>\n\t\t\t{$ajax_err}\n\t\t\t<table " . TMPL_tblDflts . " width='400'>\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'>New Recurring Invoice</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Department</td>\n\t\t\t\t\t<td valign='center'>{$depts}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>First Letters of customer</td>\n\t\t\t\t\t<td valign='center'><input type='text' size='5' id='letters' maxlength='5'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td valign='center'><input type='button' value='Update' onClick='updateCustSelection();'></td>\n\t\t\t\t</tr>\n\t\t\t</table>";
    }
    if (isset($diffwhBtn) or isset($addprodBtn) or isset($addnon) or isset($upBtn) or isset($saveBtn) or isset($ria)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details_end = "\n\t\t\t\t\t</div>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Invoice Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Invoice No.</td>\n\t\t\t\t\t\t\t<td valign='center'>RI {$inv['invid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Document Ref No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='docref' value='{$inv['docref']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Sales Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='ordno' value='{$inv['ordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='inc' {$chin}> No<input type='radio' size='7' name='chrgvat' value='exc' {$chex}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t\t<td valign='center'>{$salesps}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Invoice Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("rinv", $rinv_year, $rinv_month, $rinv_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Available Credit</td>\n\t\t\t\t\t\t\t<td>" . CUR . " " . sprint($avcred) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='traddisc' value='{$inv['traddisc']}'>%</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='7' name='delchrg' value='{$inv['delchrg']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='2'>{$ctd}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td rowspan='2'>" . mkQuickLinks(ql("cust-credit-stockinv.php", "New Invoice"), ql("rec-invoice-view.php", "View Recurring Invoices"), ql("customers-new.php", "New Customer")) . "\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<th width='25%'>Comments</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='comm' rows='4' cols='20'>{$comment}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right' valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='SUBTOT' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$inv['discount']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$inv['delivery']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><b>VAT {$vat14}</b></td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input name='diffwhBtn' type='submit' value='Different Store'> | <input name='addprodBtn' type='submit' value='Add Product'> | <input name='addnon' type='submit' value='Add Non stock Product'> | <input type='submit' name='upBtn' value='Update'> </td>\n\t\t\t\t<td> | <input type='submit' name='saveBtn' value='Save &raquo'></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    if (AJAX) {
        return $ajaxOut;
    } else {
        return "{$details_begin}{$ajaxOut}{$details_end}";
    }
}
コード例 #2
0
#
#
#
#
#
#
# get settings
require "settings.php";
require "core-settings.php";
require "libs/ext.lib.php";
# decide what to do
if (isset($_GET["purid"]) && isset($_GET["cont"])) {
    $_GET["done"] = "";
    $OUTPUT = details($_GET);
} elseif (isset($_GET["assid"]) && isset($_GET["grpid"])) {
    $_GET["purid"] = create_dummy(0, $_GET["assid"], $_GET["grpid"]);
    $_GET["done"] = "";
    $OUTPUT = details($_GET);
} else {
    if (isset($_POST["key"])) {
        switch ($_POST["key"]) {
            case "search":
                $OUTPUT = search($_POST);
                break;
            case "update":
                $OUTPUT = write($_POST);
                break;
            default:
                $OUTPUT = details($_GET);
        }
    } else {
コード例 #3
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid invoice number.");
    } else {
        $client = "";
        $vatnum = "";
        $cordno = "";
        $deptid = 2;
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    // Initial values
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
        $stkerr = "0,0";
        $cusnum = 0;
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM pinvoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    if (!isset($vatnum)) {
        $vatnum = $inv['vatnum'];
    }
    if (!isset($cordno)) {
        $cordno = $inv['cordno'];
    }
    # check if invoice has been printed
    if ($inv['printed'] == "y") {
        $error = "<li class='err'> Error : Invoice number <b>{$invid}</b> has already been printed.";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    // get the ID of the first warehouse
    db_conn("exten");
    $sql = "SELECT whid FROM warehouses ORDER BY whid ASC LIMIT 1";
    $rslt = db_exec($sql) or errDie("Error reading warehouses (FWH).");
    if (pg_num_rows($rslt) > 0) {
        $FIRST_WH = pg_fetch_result($rslt, 0, 0);
    } else {
        $FIRST_WH = "-S";
    }
    # Get selected Customer info
    db_connect();
    $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['cusnum']}' AND location != 'int' AND div = '" . USER_DIV . "'";
    $custRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($custRslt) < 1) {
        db_connect();
        # Query server for customer info   AND lower(surname) LIKE lower('$letters%')
        $sql = "SELECT cusnum,cusname,surname FROM customers WHERE location != 'int' AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            $customers .= "<option value='0' selected>Select Customer</option>";
            while ($cust = pg_fetch_array($custRslt)) {
                $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
            }
            $customers .= "</select>";
        } else {
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            $customers .= "<option value='0' selected>Select Customer</option>";
            while ($cust = pg_fetch_array($custRslt)) {
                $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
            }
            $customers .= "</select>";
        }
        # Take care of the unset vars
        $cust['addr1'] = "";
        $cust['cusnum'] = "";
        $cust['vatnum'] = "";
        $cust['accno'] = "";
        $cust["bustel"] = $inv["telno"];
        $cust["tel"] = "";
        $cust["cellno"] = "";
    } else {
        $cust = pg_fetch_array($custRslt);
        $sql = "SELECT cusnum, cusname, surname FROM customers WHERE deptid = '{$inv['deptid']}' AND location != 'int' AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $cusRslt = db_exec($sql) or errDie("Unable to view customers");
        # Moarn if customer account has been blocked   AND lower(surname) LIKE lower('$letters%')
        if ($cust['blocked'] == 'yes') {
            $error .= "<li class=err>Error : Selected customer account has been blocked.";
        }
        // $customers = "<input type=hidden name=cusnum value='$cust[cusnum]'>$cust[cusname]  $cust[surname]";
        $cusnum = $cust['cusnum'];
        $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
        $customers .= "<option value='0' selected>Select Customer</option>";
        // $customers .= "<option value='-S' selected>Select Customer</option>";
        while ($cus = pg_fetch_array($cusRslt)) {
            $sel = "";
            if ($cust['cusnum'] == $cus['cusnum']) {
                $sel = "selected";
            }
            $customers .= "<option value='{$cus['cusnum']}' {$sel}>{$cus['cusname']} {$cus['surname']}</option>";
        }
        $customers .= "</select>";
    }
    /* after first customer selection, set telno to customer's (if any) */
    if (isset($prev_cusnum) && $prev_cusnum != $cusnum) {
        if (trim($cust["bustel"]) != "") {
            $inv["telno"] = $cust["bustel"];
        } else {
            if (trim($cust["tel"]) != "") {
                $inv["telno"] = $cust["tel"];
            } else {
                $inv["telno"] = $cust["cellno"];
            }
        }
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            if (!user_in_store_team($wh["whid"], USER_ID)) {
                continue;
            }
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class='err'> There are no Sales People found in Cubit.</li>";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $inv['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # Keep the charge vat option stable
    if ($inv['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($inv['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    /* we came as a correction from receive payment page */
    if (isset($_POST["key"]) && $_POST["key"] == "recvpayment_write") {
        $inv["odate"] == $_POST["date"];
        $inv["pcc"] = $_POST["pcc"];
        $inv["pcheque"] = $_POST["pcheque"];
        $inv["pcash"] = $_POST["pcash"];
    } else {
        list($pinv_year, $pinv_month, $pinv_day) = explode("-", $inv['odate']);
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t<table " . TMPL_tblDflts . " width='100%'>\n\t<tr>\n\t\t<th>STORE</th>\n\t\t<th>ITEM NUMBER</th>\n\t\t<th>VAT CODE</th>\n\t\t<th>SERIAL NO.</th>\n\t\t<th>DESCRIPTION</th>\n\t\t<th>QTY</th>\n\t\t<th>UNIT PRICE</th>\n\t\t<th>UNIT DISCOUNT</th>\n\t\t<th>AMOUNT</th>\n\t\t<th>Remove</th>\n\t<tr>";
    # get selected stock in this invoice
    db_connect();
    $sql = "SELECT * FROM pinv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $line_count = pg_num_rows($stkdRslt);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            db_conn('core');
            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
            $Accounts = "<select name='accounts[]'>\n\t\t\t<option value='0'>Select Account</option>";
            while ($ad = pg_fetch_array($Ri)) {
                if (isb($ad['accid'])) {
                    continue;
                }
                if ($ad['accid'] == $stkd['account']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
            }
            $Accounts .= "</select>";
            $sernos = "\n\t\t\t\t<input type='hidden' name='sernos[]' value='{$stkd['serno']}'>\n\t\t\t\t<input type='hidden' name='sernos_ss[]' value='{$stkd['serno']}'>";
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
            $viewcost = "<input type='text' size='8' name='unitcost[]' value='" . sprint($stkd["unitcost"]) . "'>";
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "<select name='vatcodes[]'>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            # Put in product
            $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whids[]' value='{$stkd['whid']}'></td>\n\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>{$Vatcodes}</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$stkd['description']}'> {$sernos}</td>\n\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='hidden' name='disc[]' value='{$stkd['disc']}'>\n\t\t\t\t\t<input type='hidden' name='discp[]' value='{$stkd['discp']}'>\n\t\t\t\t</td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='" . sprint($stkd["amt"]) . "'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='checkbox' name='remprod[]' value='{$key}'>\n\t\t\t\t</td>\n\t\t\t</tr>";
            $key++;
        } else {
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            if ($stk['units'] <= $stk['minlvl']) {
                $error .= "<li class='err'>{$stk['stkcod']} is below minimum level, please notify stock controller.</li>";
            }
            # Serial number
            if ($stk['serd'] == 'yes' && ($inv['serd'] == 'n' || $stkd["serno"] == "")) {
                $sers = ext_getavserials($stkd['stkid']);
                $sernos = "<select name='sernos[]'>";
                foreach ($sers as $skey => $ser) {
                    $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
                }
                $sernos .= "</select>\n\t\t\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value='*_*_*CUBIT_SERIAL_SELECT_BOX*_*_*' />";
            } else {
                $sernos = "\n\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value='{$stkd['ss']}' />\n\t\t\t\t\t<input type='hidden' name='sernos[]' value='{$stkd['serno']}'>{$stkd['ss']}";
            }
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
            if ($stk['serd'] == 'yes') {
                $qtyin = "<input type='hidden' size='3' name='qtys[]' value='{$stkd['qty']}'>{$stkd['qty']}";
            }
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "<select name='vatcodes[]'>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            # check permissions
            if (perm("invoice-unitcost-edit.php")) {
                $viewcost = "<input type='text' size='8' name='unitcost[]' value='" . sprint($stkd["unitcost"]) . "'>";
            } else {
                $viewcost = "<input type='hidden' size='8' name='unitcost[]' value='" . sprint($stkd["unitcost"]) . "'>" . sprint($stkd["unitcost"]);
            }
            # put in product
            $products .= "\n\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>\n\t\t\t\t\t<a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a>\n\t\t\t\t</td>\n\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t<td>{$sernos}</td>\n\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='text' size='4' name='disc[]' value='{$stkd['disc']}'><b> OR </b>\n\t\t\t\t\t<input type='text' size='4' name='discp[]' value='{$stkd['discp']}' maxlength='5'>%\n\t\t\t\t</td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='" . sprint($stkd["amt"]) . "'> " . CUR . sprint($stkd["amt"]) . "</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='checkbox' name='remprod[]' value='{$key}'>\n\t\t\t\t</td>\n\t\t\t</tr>";
            $key++;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # Look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    } else {
        $SCROLL = "yes";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && strlen($stkidss[$key]) > 0) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                if ($stk['serd'] == 'yes') {
                    $sers = ext_getavserials($stkidss[$key]);
                    $sernos = "<select name='sernos[]' onChange='javascript:document.form.submit();'>";
                    foreach ($sers as $skey => $ser) {
                        $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
                    }
                    $sernos .= "</select>\n\t\t\t\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value='*_*_*CUBIT_SERIAL_SELECT_BOX*_*_*' />";
                } else {
                    $sernos = "\n\t\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value=''>\n\t\t\t\t\t\t<input type='hidden' name='sernos[]' value=''>";
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class='err'>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                # Input qty if not serialised
                $qtyin = "<input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'>";
                if ($stk['serd'] == 'yes') {
                    $qtyin = "<input type='hidden' size='3' name='qtys[]' value='{$qtyss[$key]}'>{$qtyss[$key]}";
                }
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "<select name='vatcodes[]'>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                if (perm("invoice-unitcost-edit.php")) {
                    $viewcost = "<input type='text' size='8' name='unitcost[]' value='" . sprint($stk["selamt"]) . "'>";
                } else {
                    $viewcost = "<input type='hidden' size='8' name='unitcost[]' value='" . sprint($stk["selamt"]) . "'>" . sprint($stk["selamt"]);
                }
                # put in selected warehouse and stock
                $products .= "\n\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='hidden' name='stkids[]' value='{$stk['stkid']}'>\n\t\t\t\t\t\t<a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td>{$sernos}</td>\n\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='text' size='4' name='disc[]' value='{$discs[$key]}'><b> OR </b>\n\t\t\t\t\t\t<input type='text' size='4' name='discp[]' value='{$discps[$key]}' maxlength='5'>%\n\t\t\t\t\t</td>\n\t\t\t\t\t<td><input type='hidden' name='amt[]' value='" . sprint($amt[$key]) . "'> " . CUR . sprint($amt[$key]) . "</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t</tr>";
                $line_count = 1;
                $keyy++;
            } else {
                if (isset($accountss[$key]) && $accountss[$key] != "0") {
                    db_conn('core');
                    $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                    $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                    if (pg_num_rows($Ri) < 1) {
                        return "invalid.";
                    }
                    $ad = pg_fetch_array($Ri);
                    # Calculate amount
                    $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                    # Input qty if not serialised
                    //$qtyin = "<input type=text size=3 name=qtemp value='$qtyss[$key]'>";
                    $qtyin = "<input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'>";
                    # Check permissions
                    $viewcost = "<input type='text' size='8' name='unitcost[]' value='" . sprint($unitcosts[$key]) . "'>";
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "<select name='vatcodes[]'>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vatcodess[$key] == $vd['id']) {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    # Put in selected warehouse and stock
                    $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan=2>\n\t\t\t\t\t\t{$ad['accname']}<input type='hidden' name='accounts[]' value='{$accountss[$key]}'>\n\t\t\t\t\t\t<input type='hidden' name='whids[]' value='0'>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td>{$Vatcodes}<input type='hidden' name='stkids[]' value='0'></td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$descriptionss[$key]}'></td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='hidden' name='disc[]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='discp[]' value='0'>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td><input type='hidden' name='amt[]' value='" . sprint($amt[$key]) . "'> " . CUR . sprint($amt[$key]) . "</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t</tr>";
                    $keyy++;
                } else {
                    if (!isset($diffwhBtn)) {
                        # skip if not selected
                        if ($whid == "-S") {
                            continue;
                        }
                        if (!isset($addnon)) {
                            # get warehouse name
                            db_conn("exten");
                            $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                            $whRslt = db_exec($sql);
                            $wh = pg_fetch_array($whRslt);
                            if (isset($des) and $des != "") {
                                $len = strlen($des);
                                if ($des == "Show All") {
                                    $Wh = "";
                                    $des = "";
                                } else {
                                    $Wh = "AND (lower(substr(stkdes,1,'{$len}'))=lower('{$des}') OR lower(substr(stkcod,1,'{$len}'))=lower('{$des}'))";
                                }
                            } else {
                                $Wh = "AND FALSE";
                                $des = "";
                            }
                            # get stock on this warehouse
                            db_connect();
                            $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY {$sel_frm} ASC";
                            $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                            if (pg_numrows($stkRslt) < 1) {
                                $error .= "<li class='err'>There are no stock items in the selected warehouse.";
                                continue;
                            }
                            /*						# get selected stock in this warehouse
                            						db_connect();
                            						$sql = "SELECT * FROM stock WHERE stkid = '$stkidss[$key]' AND div = '".USER_DIV."' ORDER BY stkcod ASC";
                            						$stkRslt = db_exec($sql);
                            						$stk = pg_fetch_array($stkRslt);
                            						if($stk['serd'] == 'yes'){
                            							$sers = ext_getavserials($stkidss[$key]);
                            							$sernos = "<select class='width : 15'name='sernos[]' onChange='javascript:document.form.submit();'>";
                            							foreach($sers as $skey => $ser){
                            								$sernos .= "<option value='$ser[serno]'>$ser[serno]</option>";
                            							}
                            							$sernos .= "</select>";
                            						}else{
                            							$sernos = "<input type=hidden name=sernos[] value=''>";
                            						}
                            */
                            if (isset($sel_frm) && $sel_frm == "stkdes") {
                                $descs = "<select class='width : 15'name='stkidss[]' onChange=\"document.form.des.value=''; javascript:document.form.submit();\">";
                                $descs .= "<option value='-S' disabled selected>Select Description</option>";
                                $count = 0;
                                while ($stk = pg_fetch_array($stkRslt)) {
                                    // Check if this stock item has been blocked
                                    if (stock_is_blocked($stk["stkid"])) {
                                        continue;
                                    }
                                    if ($stk["units"] <= 0) {
                                        continue;
                                    }
                                    $descs .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                                }
                                $descs .= "</select> ";
                                $cods = "";
                            } else {
                                $cods = "<select class='width : 15'name='stkidss[]' onChange=\"document.form.des.value=''; javascript:document.form.submit();\">";
                                $cods .= "<option value='-S' disabled selected>Select Number</option>";
                                $count = 0;
                                while ($stk = pg_fetch_array($stkRslt)) {
                                    // Check if this stock item has been blocked
                                    if (stock_is_blocked($stk["stkid"])) {
                                        continue;
                                    }
                                    if ($stk["units"] <= 0) {
                                        continue;
                                    }
                                    $cods .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                                }
                                $cods .= "</select> ";
                                $descs = "";
                            }
                            # put in drop down and warehouse
                            $products .= "\n\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='hidden' name='whidss[]' value='{$whid}'>\n\t\t\t\t\t\t\t\t{$wh['whname']}\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td>{$cods}</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>{$descs}</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='text' size='3' name='qtyss[]' value='1'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='text' size='4' name='discs[] value='0'>\n\t\t\t\t\t\t\t\t<b> OR </b>\n\t\t\t\t\t\t\t\t<input type='text' size='4' name='discps[] value='0' maxlength='5'>%\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='hidden' name='amts[]' value='0.00'>\n\t\t\t\t\t\t\t\t" . CUR . " 0.00\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t</tr>";
                        } else {
                            db_conn('core');
                            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts\n\t\t\t\t\t\t\tWHERE acctype='I' ORDER BY accname";
                            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                            $Accounts = "<select name='accountss[]' onChange='javascript:document.form.submit();'>\n\t\t\t\t\t\t<option value='0'>Select Account</option>";
                            while ($ad = pg_fetch_array($Ri)) {
                                if (isb($ad['accid'])) {
                                    continue;
                                }
                                $Accounts .= "\n\t\t\t\t\t\t\t<option value={$ad['accid']}>\n\t\t\t\t\t\t\t\t{$ad['accname']}\n\t\t\t\t\t\t\t</option>";
                            }
                            $Accounts .= "</select>";
                            db_conn('cubit');
                            $Sl = "SELECT * FROM vatcodes ORDER BY code";
                            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                            $Vatcodes = "<select name='vatcodess[]'>";
                            while ($vd = pg_fetch_array($Ri)) {
                                if ($vd['del'] == "Yes") {
                                    $sel = "selected";
                                } else {
                                    $sel = "";
                                }
                                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                            }
                            $Vatcodes .= "</select>";
                            $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[]' value='{$FIRST_WH}'></td>\n\t\t\t\t\t\t\t<input type='hidden' name='stkidss[]' value=''>\n\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='hidden' name='discs[]' value='0'>\n\t\t\t\t\t\t\t\t<input type='hidden' name='discps[]' value='0' >\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>";
                        }
                    }
                }
            }
        }
    } else {
        if (!(isset($diffwhBtn) || isset($addnon))) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                if (isset($wtd) && $wtd != 0) {
                    $whid = $wtd;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                if (isset($des) and $des != "") {
                    $len = strlen($des);
                    if ($des == "Show All") {
                        $Wh = "";
                        $des = "";
                    } else {
                        $Wh = "AND (lower(substr(stkdes,1,'{$len}'))=lower('{$des}') OR lower(substr(stkcod,1,'{$len}'))=lower('{$des}'))";
                    }
                } else {
                    $Wh = "AND FALSE";
                    $des = "";
                }
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected store.";
                }
                $stks = "<select name='stkidss[]' onChange=\"document.form.des.value=''; javascript:document.form.submit();\">";
                $stks .= "<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    // Check if this stock item has been blocked
                    if (stock_is_blocked($stk["stkid"])) {
                        continue;
                    }
                    if ($stk["units"] <= 0) {
                        continue;
                    }
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t<input type='hidden' name='vatcodess[]' value=''>\n\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='text' size='4' name='discs[]' value='0'><b> OR </b>\n\t\t\t\t\t\t<input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td><td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t</tr>";
            } else {
                $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='text' size='4' name='discs[]' value='0'><b> OR </b>\n\t\t\t\t\t\t<input type='text' size='4' name='discps[]' value='0' maxlength='5'>%\n\t\t\t\t\t</td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t</tr>";
            }
        } else {
            if (isset($addnon)) {
                db_conn('core');
                $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                $Accounts = "<select name='accountss[]' onChange='javascript:document.form.submit();'>\n\t\t\t<option value='0'>Select Account</option>";
                while ($ad = pg_fetch_array($Ri)) {
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                }
                $Accounts .= "</select>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "<select name='vatcodess[]'>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vd['del'] == "Yes") {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                //				<input type=hidden name='stkidss[]' value=''>
                $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan=2>\n\t\t\t\t\t{$Accounts}<input type='hidden' name='whidss[]' value='{$FIRST_WH}'>\n\t\t\t\t</td>\n\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='hidden' name='discs[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='discps[]' value='0' >\n\t\t\t\t</td>\n\t\t\t</tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        $products .= "\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>{$whs}</td>\n\t\t\t<td>&nbsp;</td>\n\t\t\t<td>&nbsp;</td>\n\t\t\t<td>&nbsp;</td>\n\t\t\t<td>&nbsp;</td>\n\t\t\t<td>&nbsp;</td>\n\t\t\t<td>\n\t\t\t\t<input type='text' size='4' name='discs[]' value='0'><b> OR </b>\n\t\t\t\t<input type='text' size='4' name='discps[]' value='0' maxlength='5'>%\n\t\t\t</td>\n\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t<td>&nbsp;</td>\n\t\t</tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    $SUBTOT = sprint($inv['subtot']);
    # Calculate tradediscm
    if ($inv['traddisc'] > 0) {
        $traddiscm = sprint($inv['traddisc'] / 100 * ($SUBTOT + $inv['delchrg']));
    } else {
        $traddiscm = "0.00";
    }
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    $VAT = sprint($inv['vat']);
    $TOTAL = sprint($inv['total']);
    $inv['delchrg'] = sprint($inv['delchrg']);
    if (!isset($done)) {
        $done = "";
    }
    /* --- End Some calculations --- */
    if ($inv['terms'] == 1) {
        $tc1 = "";
        $tc2 = "checked";
    } else {
        $tc1 = "checked";
        $tc2 = "";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM settings WHERE constant='PSALES'";
    $Ri = db_exec($Sl) or errDie("Unable to get settings.");
    $data = pg_fetch_array($Ri);
    if ($data['value'] == "Yes") {
        $sc = "checked";
    } else {
        $sc = "";
    }
    $sales = "<td>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<td>{$salesps}</td>\n\t\t\t\t<td>Print</td>\n\t\t\t\t<td><input type='checkbox' name='printsales' {$sc}></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</td>";
    # Query server for depts
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE div = '" . USER_DIV . "' ORDER BY deptname ASC";
    $deptRslt = db_exec($sql) or errDie("Unable to view customers");
    if (pg_numrows($deptRslt) < 1) {
        return "<li class='err'>There are no Departments found in Cubit.</li>";
    } else {
        $depts = "<select name='deptid'>";
        while ($dept = pg_fetch_array($deptRslt)) {
            if ($dept['deptid'] == $inv['deptid']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $depts .= "<option value='{$dept['deptid']}' {$sel}>{$dept['deptname']}</option>";
        }
        $depts .= "</select>";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM users WHERE username='******'";
    $Ri = db_exec($Sl);
    $data = pg_fetch_array($Ri);
    if ($data['help'] != "S") {
        $save = "|<input type='submit' name='saveBtn' value='Save'>";
    } else {
        $save = "";
    }
    if ($inv['rounding'] > 0) {
        $due = sprint($inv['total'] - $inv['rounding']);
        $rd = "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Rounding</td>\n\t\t\t\t\t<td align='right'>R {$inv['rounding']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<th>Amount Due</th>\n\t\t\t\t\t<td align='right'>R {$due}</td>\n\t\t\t\t</tr>";
    } else {
        $rd = "";
    }
    $inv['delvat'] += 0;
    if ($inv['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $inv['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "<select name='delvat'>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $inv['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    if (strlen($client) < 1) {
        $client = $inv['cusname'];
    }
    if ($inv['cusnum'] == 0) {
        $cd = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer</td>\n\t\t\t\t<td valign='center'><input type='text' size='20' name='client' value='{$client}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>VAT Number</td>\n\t\t\t\t<td valign='center'><input type='text' size='20' name='vatnum' value='{$vatnum}'></td>\n\t\t\t</tr>\n\t\t\t";
        $pc = "<input type='hidden' name='pcredit' value='0'>";
    } else {
        $cd = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign=top>Customer Address</td>\n\t\t\t\t<td valign=center>" . nl2br($cust['addr1']) . "</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer VAT Number</td>\n\t\t\t\t<td>{$cust['vatnum']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer Balance</td>\n\t\t\t\t<td nowrap='t'>\n\t\t\t\t\t" . CUR . " {$cust['balance']}\n\t\t\t\t\t<a href='javascript: printer(\"cust-stmnt.php?cusnum={$cusnum}&print=t\");'>Print Statement</a>\n\t\t\t\t</td>\n\t\t\t</tr>";
        $pc = "\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>Amount On Credit</td>\n\t\t\t<td nowrap='t'>\n\t\t\t\t<input size='12' type='text' name='pcredit' id='pcredit' value='{$inv['pcredit']}' onchange='ptot_update();'>\n\t\t\t\t<input type='button' value='&laquo Total' onclick='paytotal(\"pcredit\");' />\n\t\t\t</td>\n\t\t</tr>";
        if ($line_count > 0) {
            $recvpay = "";
        } else {
            $recvpay = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td colspan='2' align='center'><input type='submit' name='recvpay' id='recvpay' onclick='return ptot_recvpay();' value='Receive Payment: " . CUR . " " . sprint($inv["pcc"] + $inv["pcheque"] + $inv["pcash"]) . "' /></td>\n\t\t\t</tr>";
        }
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    if (!isset($des)) {
        $des = "";
    }
    if (isset($sel_frm) && $sel_frm == "stkdes") {
        $sel_frm_cod = "";
        $sel_frm_des = "checked";
    } else {
        $sel_frm_cod = "checked";
        $sel_frm_des = "";
    }
    if (!isset($recvpay)) {
        $recvpay = "";
    }
    if (empty($inv["comm"])) {
        db_conn("cubit");
        $sql = "SELECT value FROM settings WHERE constant='DEFAULT_POS_COMMENTS'";
        $rslt = db_exec($sql) or errDie("Unable to retrieve default comments from Cubit.");
        $inv["comm"] = base64_decode(pg_fetch_result($rslt, 0));
    }
    /* -- Final Layout -- */
    $details = "\n\t<script language=\"JavaScript\"><!--\n\t\tvar windowReference;\n\n\tfunction openRFPopup() {\n\t\twindowReference = window.open('rfid_batch.php?invid={$invid}','windowName','height=500,width=700,toolbar=no,menubar=no,scrollbars=no');\n\t\tif (!windowReference.opener)\n\t\t\twindowReference.opener = self;\n\t\t}\n\t//--></script>\n\n\t<center><h3>New Point of Sale Invoice</h3>\n\t<form method='POST' name='formName'>\n\t<input type='hidden' name='key' value='update' />\n\t<input type='hidden' name='invid' value='{$invid}' />\n\t<input type='hidden' name='SCROLL' value='yes'>\n\t</form>\n\t<script>\n\tfunction ptot_recvpay() {\n\t\tif (ptot_amt() > 0) {\n\t\t\treturn true;\n\t\t} else {\n\t\t\talert('Enter amounts received by customer above.');\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tfunction pfld_num(fn) {\n\t\ti = getObject(fn).value;\n\n\t\tif (i) {\n\t\t\treturn parseFloat(i);\n\t\t} else {\n\t\t\treturn 0;\n\t\t}\n\t}\n\tfunction ptot_amt(nocredit) {\n\t\ti = pfld_num('pcash');\n\t\ti += pfld_num('pcc');\n\t\ti += pfld_num('pcheque');\n\n\t\tif (!nocredit && getObject('pcredit')) {\n\t\t\ti += pfld_num('pcredit');\n\t\t}\n\n\t\treturn i.toFixed(2);\n\t}\n\n\tfunction ptot_update() {\n\t\tgetObject('ptot').innerHTML = '" . CUR . " ' + ptot_amt();\n\n\t\tif (o = getObject('recvpay')) {\n\t\t\to.value = 'Receive Payment: " . CUR . " ' + ptot_amt(true);\n\t\t}\n\t}\n\n\tfunction paytotal(id) {\n\t\tgetObject('pcash').value = '0.00';\n\t\tgetObject('pcc').value = '0.00';\n\t\tgetObject('pcheque').value = '0.00';\n\t\tif (getObject('pcredit')) getObject('pcredit').value = '0.00';\n\n\t\tgetObject(id).value = getObject('itotal').value;\n\n\t\tptot_update();\n\t}\n\t</script>\n\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t<input type='hidden' name='key' value='update'>\n\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t<input type='hidden' id='itotal' value='{$TOTAL}' />\n\t\t<input type='hidden' name='prev_cusnum' value='{$cusnum}' />\n\t\t<input type='hidden' name='SCROLL' value='yes'>\n\t<table " . TMPL_tblDflts . " width='95%'>\n \t\t<tr>\n \t\t\t<td valign='top'>\n\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t<td valign='center'>{$depts}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t\t<td valign='center'>{$customers}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t{$cd}\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Customer Telephone Number</td>\n\t\t\t\t\t\t<td valign='center'><input type='text' size='20' name='telno' value='{$inv['telno']}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Customer Order number</td>\n\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$cordno}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th colspan='2'>Point of Sale</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Barcode</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<input type='text' size='13' name='bar' value=''>\n\t\t\t\t\t\t\t<input type='button' onClick='javascript:openRFPopup();' value='RFID Batch'>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th colspan='2'>Options</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Select Using</td>\n\t\t\t\t\t\t<td>Stock Code<input type='radio' name='sel_frm' value='stkcod' onChange='javascript:document.form.submit();' {$sel_frm_cod}> Stock Description<input type='radio' name='sel_frm' value='stkdes' onChange='javascript:document.form.submit();' {$sel_frm_des}></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Stock Filter</td>\n\t\t\t\t\t\t<td><input type='text' size='13' name='des' value='{$des}'> <input type='submit' value='Search'> <input type='submit' name='des' value='Show All'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t</td>\n\t\t\t<td valign='top' align='right'>\n\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th colspan='2'>Invoice Details</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Invoice No.</td>\n\t\t\t\t\t\t<td valign='center'>{$inv['invid']}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Sales Order No.</td>\n\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='ordno' value='{$inv['ordno']}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Invoice Date</td>\n\t\t\t\t\t\t<td valign='center' nowrap='t'>" . mkDateSelect("pinv", $pinv_year, $pinv_month, $pinv_day) . "</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td nowrap='t'>VAT Inclusive</td>\n\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='inc' {$chin}> No<input type='radio' size='7' name='chrgvat' value='exc' {$chex}></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t{$sales}\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='traddisc' value='{$inv['traddisc']}'>%</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t<td valign='center'><input type='text' size='7' name='delchrg' value='{$inv['delchrg']}'>{$Vatcodes}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th colspan='2'>Payment Details </th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>User</td>\n\t\t\t\t\t\t<td><input type='hidden' name='user' value='" . USER_NAME . "'>" . USER_NAME . "</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td nowrap='t'>Amount Paid Cash</td>\n\t\t\t\t\t\t<td nowrap='t'>\n\t\t\t\t\t\t\t<input size='12' type='text' name='pcash' id='pcash' value='{$inv['pcash']}' onchange='ptot_update();'>\n\t\t\t\t\t\t\t<input type='button' value='&laquo Total' onclick='paytotal(\"pcash\");' />\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td nowrap='t'>Amount Paid Cheque</td>\n\t\t\t\t\t\t<td nowrap='t'>\n\t\t\t\t\t\t\t<input size='12' type='text' name='pcheque' id='pcheque' value='{$inv['pcheque']}' onchange='ptot_update();'>\n\t\t\t\t\t\t\t<input type='button' value='&laquo Total' onclick='paytotal(\"pcheque\");' />\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td nowrap='t'>Amount Paid Credit Card</td>\n\t\t\t\t\t\t<td nowrap='t'>\n\t\t\t\t\t\t\t<input size='12' type='text' name='pcc' id='pcc' value='{$inv['pcc']}' onchange='ptot_update();'>\n\t\t\t\t\t\t\t<input type='button' value='&laquo Total' onclick='paytotal(\"pcc\");' />\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t{$recvpay}\n\t\t\t\t\t{$pc}\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td nowrap='t'>Total Covered</td>\n\t\t\t\t\t\t<td nowrap='t' id='ptot'>" . CUR . " " . sprint($inv["pcash"] + $inv["pcheque"] + $inv["pcc"] + $inv["pcredit"]) . "</td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t</td>\n\t\t</tr>\n\t\t<tr><td><br></td></tr>\n\t\t<tr>\n\t\t\t<td colspan='2'>{$products}</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td width='70%' valign='top'>\n\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td rowspan='2'>" . mkQuickLinks(ql("pos-invoice-new-no-neg.php", "New POS Invoice"), ql("pos-invoice-list.php", "View POS Invoices"), ql("customers-new.php", "New Customer")) . "\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<th width='30%'>Comments</th>\n\t\t\t\t\t\t<td rowspan='5' valign='top' width='40%'>{$error}</td></tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td rowspan='4' align='center' valign='top'><textarea name='comm' rows='4' cols='20'>{$inv['comm']}</textarea></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t</td>\n\t\t\t<td align='right' valign='top' width='30%'>\n\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='SUBTOT' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t<td align='right'>" . CUR . " {$inv['discount']}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t<td align='right'>" . CUR . " {$inv['delivery']}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><b>VAT {$vat14}</b></td>\n\t\t\t\t\t\t<td align='right'>" . CUR . " {$VAT}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t<td align='right'>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t{$rd}\n\t\t\t\t</table>\n\t\t\t</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td align='right'><input name='diffwhBtn' type='submit' value='Different Store'> | <input name='addprodBtn' type='submit' value='Add Product'>| <input name='addnon' type='submit' value='Add Non stock Product'>{$save} </td>\n\t\t\t<td>| <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t</tr>\n\t</table>\n\t<a name='bottom'>\n\t</form>\n\t</center>";
    return $details;
}
コード例 #4
0
function details($_POST, $error = "")
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($purid)) {
        $v->isOk($purid, "num", 1, 20, "Invalid Non-Stock Order number.");
    } else {
        $v->isOk($supid, "num", 1, 20, "Invalid Supplier number.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm = $error . slct();
        return $confirm;
    }
    if (!isset($purid)) {
        $purid = create_dummy(0, $supid);
    }
    # Get Order info
    db_connect();
    $sql = "SELECT * FROM nons_purch_int WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>purchase Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    # check if Order has been printed
    if ($pur['received'] == "y") {
        $error = "<li class='err'> Error : Order number <b>{$purid}</b> has already been received.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    if ($pur['xrate'] == 0) {
        $pur['xrate'] = 1;
    }
    # Get selected supplier info
    db_connect();
    $sql = "SELECT * FROM suppliers WHERE supid = '{$pur['supid']}' AND div = '" . USER_DIV . "'";
    $supRslt = db_exec($sql) or errDie("Unable to view Supplier");
    if (pg_numrows($supRslt) < 1) {
        db_connect();
        # Query server for supplier info
        $sql = "SELECT * FROM suppliers WHERE location = 'int' AND div = '" . USER_DIV . "' ORDER BY supname ASC";
        $supRslt = db_exec($sql) or errDie("Unable to view suppliers");
        if (pg_numrows($supRslt) < 1) {
            $err = "<li class='err'>No Supplier found in database.</li>";
            return view_err($_POST, $err);
        } else {
            $suppliers = "<select name='supid' onChange='javascript:document.form.submit();'>";
            $suppliers .= "<option value='-S' selected>Select Supplier</option>";
            while ($sup = pg_fetch_array($supRslt)) {
                $suppliers .= "<option value='{$sup['supid']}'>{$sup['supname']}</option>";
            }
            $suppliers .= "</select>";
        }
        # take care of the uset vars
        $supaddr = "";
        $accno = "";
        $fcid = $pur['fcid'];
    } else {
        db_connect();
        # Query server for supplier info
        $sql = "SELECT * FROM suppliers WHERE location = 'int' AND div = '" . USER_DIV . "' ORDER BY supname ASC";
        $supRslt = db_exec($sql) or errDie("Unable to view suppliers");
        if (pg_numrows($supRslt) < 1) {
            $err = "<li class='err'>No Supplier found in database.</li>";
            return view_err($_POST, $err);
        } else {
            $supid = $pur['supid'];
            $suppliers = "<select name='supid' onChange='javascript:document.form.submit();'>";
            $sel = "";
            $fcid = $pur['fcid'];
            while ($sup = pg_fetch_array($supRslt)) {
                if ($sup['supid'] == $supid) {
                    $sel = "selected";
                    $supaddr = "{$sup['supaddr']}";
                    $accno = $sup['supno'];
                    $fcid = $sup['fcid'];
                    $listid = $sup['listid'];
                } else {
                    $sel = "";
                    $supaddr = "";
                    $accno = "";
                }
                $suppliers .= "<option value='{$sup['supid']}' {$sel}>{$sup['supname']}</option>";
            }
            $suppliers .= "</select>";
        }
    }
    $currs = getSymbol($fcid);
    $curr = $currs['symbol'];
    $currsel = "{$currs['symbol']} - {$currs['descrip']}";
    if (!isset($ordernum)) {
        $ordernum = '';
    }
    /* --- Start Drop Downs --- */
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    # format date
    list($npuri_year, $npuri_month, $npuri_day) = explode("-", $pur['pdate']);
    list($del_year, $del_month, $del_day) = explode("-", $pur['ddate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th colspan='2'>UNIT PRICE</th>\n\t\t\t\t<th colspan='2'>DUTY</th>\n\t\t\t\t<th>LINE TOTAL</th>\n\t\t\t\t<th>COST PER UNIT</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this Order
    db_connect();
    $sql = "SELECT * FROM nons_purint_items  WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $i++;
        /* -- Calculations -- */
        # Calculate cost amount bought
        $totamt = $stkd['qty'] * $stkd['cunitcost'];
        # Calculate percentage from subtotal
        if ($pur['subtot'] != 0) {
            $perc = ($totamt + $stkd['duty']) / $pur['subtot'] * 100;
        } else {
            $perc = 0;
        }
        # Get percentage from shipping charges
        $shipchrg = sprint($perc / 100 * $pur['shipchrg']);
        # Add shipping charges to amt
        $totamt = sprint($totamt + $shipchrg + $stkd['duty']);
        $unitamt = sprint($totamt / $stkd['qty']);
        /* -- End Calculations --*/
        $stkd['amt'] = sprint($stkd['amt']);
        $tip = "&nbsp;&nbsp;&nbsp;";
        if (isset($vatc[$key])) {
            $tip = "<font color='red'>#</font>";
            $error = "<div class='err'> {$tip}&nbsp;&nbsp;=&nbsp;&nbsp; Vat amount is different from amount calculated by cubit. To allow cubit to recalculate the vat amount, please delete the vat amount from the input box.";
        }
        # put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='10' name='cod[]' value='{$stkd['cod']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='20' name='des[]' value='{$stkd['des']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t<td>{$pur['curr']} <input type='text' size='6' name='cunitcost[]' value='{$stkd['cunitcost']}'> or </td>\n\t\t\t\t<td>" . CUR . " <input type='text' size='6' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t<td>{$pur['curr']} <input type='text' size='6' name='duty[]' value='{$stkd['duty']}'> or </td>\n\t\t\t\t<td><input type='text' size='3' name='dutyp[]' value='{$stkd['dutyp']}'>%</td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> {$pur['curr']} {$stkd['amt']}</td>\n\t\t\t\t<td align='right'>{$pur['curr']} {$unitamt}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='checkbox' name='remprod[]' value='{$key}'>\n\t\t\t\t\t<input type='hidden' name='SCROLL' value='yes'>\n\t\t\t\t</td>\n\t\t\t</tr>";
        $key++;
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
        list($year, $mon, $day) = explode("-", date("Y-m-d"));
        # add one
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='10' name='cod[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='20' name='des[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='1'></td>\n\t\t\t\t<td>{$pur['curr']} <input type='text' size='6' name='cunitcost[]'> or </td>\n\t\t\t\t<td>" . CUR . " <input type='text' size='6' name='unitcost[]'></td>\n\t\t\t\t<td>{$pur['curr']} <input type='text' size='6' name='duty[]'> or </td>\n\t\t\t\t<td><input type='text' size='3' name='dutyp[]'>%</td>\n\t\t\t\t<td>{$pur['curr']} 0.00</td>\n\t\t\t\t<td align='right'>{$pur['curr']} 0.00</td>\n\t\t\t\t<td> </td>\n\t\t\t</tr>";
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        list($year, $mon, $day) = explode("-", date("Y-m-d"));
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='10' name='cod[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='20' name='des[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='1'></td>\n\t\t\t\t<td>{$pur['curr']} <input type='text' size='6' name='cunitcost[]'> or </td>\n\t\t\t\t<td>" . CUR . " <input type='text' size='6' name='unitcost[]'></td>\n\t\t\t\t<td>{$pur['curr']} <input type='text' size='6' name='duty[]'> or </td>\n\t\t\t\t<td><input type='text' size='3' name='dutyp[]'>%</td>\n\t\t\t\t<td>{$pur['curr']} 0.00</td>\n\t\t\t\t<td align='right'>{$pur['curr']} 0.00</td>\n\t\t\t\t<td> </td>\n\t\t\t</tr>";
        $key++;
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = sprint($pur['subtot']);
    # Get Total
    $TOTAL = sprint($pur['total']);
    $pur['tax'] = sprint($pur['tax']);
    $pur['shipchrg'] = sprint($pur['shipchrg']);
    $pur['cusid'] += 0;
    if ($pur['cusid'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $pur['cusid'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $pur['cusid']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    /* --- End Some calculations --- */
    if (isset($diffwhBtn) or isset($upBtn) or isset($doneBtn) or isset($recv) or isset($donePrnt)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details = "\n\t\t<center>\n\t\t<h3>New International Non-Stock Order</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='purid' value='{$purid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t<td valign='center'>{$suppliers}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t<td valign='center'>{$accno}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t\t\t\t<td valign='center'>" . nl2br($supaddr) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Non-Stock Order Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Non-Stock Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='ordernum' value='{$ordernum}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center' nowrap='t'>" . mkDateSelect("npuri", $npuri_year, $npuri_month, $npuri_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Foreign Currency</td>\n\t\t\t\t\t\t\t<td valign='center'>{$currsel} &nbsp;&nbsp;Exchange rate " . CUR . " <input type='text' size='7' name='xrate' value='{$pur['xrate']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Tax</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['curr']} <input type='text' size='7' name='tax' value='{$pur['tax']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Shipping Charges</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['curr']} <input type='text' size='7' name='shipchrg' value='{$pur['fshipchrg']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("del", $del_year, $del_month, $del_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='nons-purch-int-view.php'>View International Non-Stock Orders</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$pur['remarks']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align=right>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>{$pur['curr']} <input type='hidden' name='subtot' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Shipping Charges</td>\n\t\t\t\t\t\t\t<td align='right'>{$pur['curr']} {$pur['shipchrg']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Tax </td>\n\t\t\t\t\t\t\t<td align='right'>{$pur['curr']} {$pur['tax']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>{$pur['curr']} <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' align='center'><input type='button' value='&laquo Back' onClick='javascript:history.back()'> | <input name='diffwhBtn' type='submit' value='Add Item'> | <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}
コード例 #5
0
function details($_POST, $error = "")
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid invoice number.");
    } else {
        $client = "";
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class=err>" . $e["msg"];
        }
        $confirm .= "{$error}<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
        $stkerr = "0,0";
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM pinvoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class=err>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    $client = $inv['cusname'];
    # check if invoice has been printed
    if ($inv['printed'] == "y") {
        $error = "<li class=err> Error : Invoice number <b>{$invid}</b> has already been printed.";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class=err>Department not Found.";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class=err> There are no Stores found in Cubit.";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class=err> There are no Sales People found in Cubit.";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $inv['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # keep the charge vat option stable
    if ($inv['chrgvat'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
    } else {
        $chy = "";
        $chn = "checked=yes";
    }
    # format date
    list($oyear, $omon, $oday) = explode("-", $inv['odate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=100%>\n\t<tr><th>STORE</th><th>ITEM NUMBER</th><th>DESCRIPTION</th><th>QTY</th><th>UNIT PRICE</th><th>UNIT DISCOUNT</th><th>AMOUNT</th><th>Remove</th><tr>";
    # get selected stock in this invoice
    db_connect();
    $sql = "SELECT * FROM pinv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $i++;
        # get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        # put in product
        $products .= "<tr class='bg-odd'><td><input type=hidden name=whids[] value='{$stkd['whid']}'>{$wh['whname']}</td><td><input type=hidden name=stkids[] value='{$stkd['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td><td>" . extlib_rstr($stk['stkdes'], 30) . "</td><td><input type=text size=3 name=qtys[] value='{$stkd['qty']}'></td><td><input type=hidden size=8 name=unitcost[] value='{$stkd['unitcost']}'>{$stkd['unitcost']}</td><td><input type=text size=4 name=disc[] value='{$stkd['disc']}'> OR <input type=text size=4 name=discp[] value='{$stkd['discp']}' maxlength=5>%</td><td><input type=hidden name=amt[] value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td><td><input type=checkbox name=remprod[] value='{$key}'><input type=hidden name=SCROLL value=yes></td></tr>";
        $key++;
    }
    # Look above(remprod keys)
    $keyy = $key;
    # Look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    } else {
        $SCROLL = "yes";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S") {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class=err>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                # put in selected warehouse and stock
                $products .= "<tr class='bg-odd'><td><input type=hidden name=whids[] value='{$whid}'>{$wh['whname']}</td><td><input type=hidden name=stkids[] value='{$stk['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td><td>" . extlib_rstr($stk['stkdes'], 30) . "</td><td><input type=text size=3 name=qtys[] value='{$qtyss[$key]}'></td><td><input type=hidden size=8 name='unitcost[]'  value='{$stk['selamt']}'>{$stk['selamt']}</td><td><input type=text size=4 name=disc[] value='{$discs[$key]}'> OR <input type=text size=4 name=discp[] value='{$discps[$key]}' maxlength=5>%</td><td><input type=hidden name=amt[] value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td><td><input type=checkbox name=remprod[] value='{$keyy}'></td></tr>";
                $keyy++;
            } else {
                if (!isset($diffwhBtn)) {
                    # skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    # get warehouse name
                    db_conn("exten");
                    $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                    $whRslt = db_exec($sql);
                    $wh = pg_fetch_array($whRslt);
                    # get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        $error .= "<li class=err>There are no stock items in the selected warehouse.";
                        continue;
                    }
                    $stks = "<select class='width : 15'name='stkidss[]' onChange='javascript:document.form.submit();'>";
                    $stks .= "<option value='-S' disabled selected>Select Number</option>";
                    $count = 0;
                    while ($stk = pg_fetch_array($stkRslt)) {
                        $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                    }
                    $stks .= "</select> ";
                    # put in drop down and warehouse
                    $products .= "<tr class='bg-odd'><td><input type=hidden name=whidss[] value='{$whid}'>{$wh['whname']}</td><td>{$stks}</td><td> </td><td><input type=text size=3 name='qtyss[]'  value='1'></td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td><input type=hidden name=amts[] value='0.00'>" . CUR . " 0.00</td><td></td></tr>";
                }
            }
        }
    } else {
        if (!isset($diffwhBtn)) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    $err .= "<li>There are no stock items in the selected store.";
                    continue;
                }
                $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                $stks .= "<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "<tr class='bg-odd'><td><input type=hidden name=whidss[] value='{$whid}'>{$wh['whname']}</td><td>{$stks}</td><td> </td><td><input type=text size=3 name=qtyss[] value='1'></td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
            } else {
                $products .= "<tr class='bg-odd'><td>{$whs}</td><td></td><td> </td><td> </td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        $products .= "<tr class='bg-odd'><td>{$whs}</td><td></td><td> </td><td> </td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    $SUBTOT = sprint($inv['subtot']);
    # Calculate tradediscm
    if ($inv['traddisc'] > 0) {
        $traddiscm = sprint($inv['traddisc'] / 100 * $SUBTOT);
    } else {
        $traddiscm = "0.00";
    }
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    $VAT = sprint($inv['vat']);
    $TOTAL = sprint($inv['total']);
    $inv['delchrg'] = sprint($inv['delchrg']);
    /* --- End Some calculations --- */
    /* -- Final Layout -- */
    $details = "<center><h3>Edit Point of Sale Invoice(Cash)</h3>\n\t<form action='" . SELF . "' method=post name=form>\n\t<input type=hidden name=key value=update>\n\t<input type=hidden name=invid value='{$invid}'>\n\t<input type=hidden name=stkerr value='{$stkerr}'>\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=95%>\n \t<tr><td valign=top>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><th colspan=2> Customer Details </th></tr>\n\t\t\t<tr class='bg-odd'><td>Department</td><td valign=center>{$dept['deptname']}</td></tr>\n\t\t\t<tr class='bg-even'><td>Customer</td><td valign=center><input type=text size=20 name=client value='{$client}'></td></tr>\n\t\t\t<tr class='bg-odd'><td>Customer Order number</td><td valign=center><input type=text size=10 name=cordno value='{$inv['cordno']}'></td></tr>\n\t\t\t<tr><th colspan=2>Point of Sale</th></tr>\n\t\t\t<tr class='bg-even'><td>Barcode</td><td><input type=text size=13 name=bar value=''></td></tr>\n\t\t</table>\n\t</td><td valign=top align=right>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><th colspan=2> Invoice Details </th></tr>\n\t\t\t<tr class='bg-even'><td>Invoice No.</td><td valign=center>{$inv['invid']}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Sales Order No.</td><td valign=center><input type=text size=5 name=ordno value='{$inv['ordno']}'></td></tr>\n\t\t\t<tr class='bg-even'><td>Charge VAT</td><td valign=center><table><tr><td>Yes</td><td><input type=radio size=7 name=chrgvat value='yes' {$chy}></td><td>No</td><td><input type=radio size=7 name=chrgvat value='no' {$chn}></td></tr></table></td></tr>\n\t\t\t<tr class='bg-odd'><td>Sales Person</td><td valign=center>{$salesps}</td></tr>\n\t\t\t<input type=hidden name=oday value='{$oday}'><input type=hidden name=omon value='{$omon}'><input type=hidden name=oyear value='{$oyear}'>\n\t\t\t<tr class='bg-even'><td>Trade Discount</td><td valign=center><input type=text size=5 name=traddisc value='{$inv['traddisc']}'>%</td></tr>\n\t\t\t<tr class='bg-odd'><td>Delivery Charge</td><td valign=center><input type=text size=7 name=delchrg value='{$inv['delchrg']}'></td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td><br></td></tr>\n\t<tr><td colspan=2>{$products}</td></tr>\n\t<tr><td width='70%' valign=top>\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=100%>\n\t\t\t<tr><th width=30%>Quick Links</th><th width=30%>Comments</th><td rowspan=5 valign=top width=40%>{$error}</td></tr>\n\t\t\t<tr><td class='bg-odd'><a href='pos-invoice-new.php'>New POS Invoice</a></td><td class='bg-odd' rowspan=4 align=center valign=top><textarea name=comm rows=4 cols=20>{$inv['comm']}</textarea></td></tr>\n\t\t\t<tr class='bg-odd'><td><a href='pos-invoice-list.php'>View POS Invoices</a></td></tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>\n\t</td><td align=right valign=top width='30%'>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=100%>\n\t\t\t<tr class='bg-odd'><td>SUBTOTAL</td><td align=right>" . CUR . " <input type=hidden name=SUBTOT value='{$SUBTOT}'>{$SUBTOT}</td></tr>\n\t\t\t<tr class='bg-even'><td>Trade Discount</td><td align=right>" . CUR . " {$traddiscm}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Delivery Charge</td><td align=right>" . CUR . " {$inv['delchrg']}</td></tr>\n\t\t\t<tr class='bg-even'><td><b>VAT @ {$VATP}%</b></td><td align=right>" . CUR . " {$VAT}</td></tr>\n\t\t\t<tr class='bg-odd'><th>GRAND TOTAL</th><td align=right>" . CUR . " {$TOTAL}</td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td align=right><input type=button value='&laquo Back' onClick='javascript:history.back()'> | <input name=diffwhBtn type=submit value='Different Store'> | <input name=addprodBtn type=submit value='Add Product'> | <input type=submit name='saveBtn' value='Save'> </td><td>| <input type=submit name='upBtn' value='Update'>{$done}</td></tr>\n\t</table><a name=bottom>\n\t</form></center>";
    return $details;
}
コード例 #6
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid invoice number.");
    } else {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (isset($deptid)) {
        db_connect();
        # Query server for customer info
        $sql = "SELECT cusnum,cusname,surname FROM customers WHERE deptid = '{$deptid}' AND location = 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $err = "<li class='err'>No customer names starting with <b>{$letters}</b> in database.</li>";
            return view_err($_POST, $err);
        }
    }
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
        $stkerr = "0,0";
    }
    if (!isset($done)) {
        $done = "";
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # Check if invoice has been printed
    if ($inv['printed'] == "y") {
        $error = "<li class='err'> Error : Invoice number <b>{$invid}</b> has already been printed.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    $currs = getSymbol($inv['fcid']);
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected Customer info
    db_connect();
    $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['cusnum']}'  AND location = 'int' AND div = '" . USER_DIV . "'";
    $custRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($custRslt) < 1) {
        db_connect();
        # Query server for customer info
        $sql = "SELECT cusnum,cusname,surname FROM customers WHERE deptid = '{$inv['deptid']}' AND location = 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $err = "<li class='err'>No customer names starting with <b>{$letters}</b> in database.</li>";
            return view_err($_POST, $err);
        } else {
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            $customers .= "<option value='-S' selected>Select Customer</option>";
            while ($cust = pg_fetch_array($custRslt)) {
                $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
            }
            $customers .= "</select>";
        }
        # Take care of the unset vars
        $cust['addr1'] = "";
        $cust['cusnum'] = "";
        $cust['vatnum'] = "";
        $cust['accno'] = "";
    } else {
        $cust = pg_fetch_array($custRslt);
        $sql = "SELECT cusnum, cusname, surname FROM customers WHERE deptid = '{$inv['deptid']}' AND location = 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $cusRslt = db_exec($sql) or errDie("Unable to view customers");
        # Moarn if customer account has been blocked
        if ($cust['blocked'] == 'yes') {
            $error .= "<li class='err'>Error : Selected customer account has been blocked.";
        }
        // $customers = "<input type=hidden name=cusnum value='$cust[cusnum]'>$cust[cusname]  $cust[surname]";
        $cusnum = $cust['cusnum'];
        $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
        // $customers .= "<option value='-S' selected>Select Customer</option>";
        while ($cus = pg_fetch_array($cusRslt)) {
            $sel = "";
            if ($cust['cusnum'] == $cus['cusnum']) {
                $sel = "selected";
            }
            $customers .= "<option value='{$cus['cusnum']}' {$sel}>{$cus['cusname']} {$cus['surname']}</option>";
        }
        $customers .= "</select>";
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # Get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class='err'> There are no Sales People found in Cubit.</li>";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $inv['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # Days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $inv['terms']);
    # Keep the charge vat option stable
    if ($inv['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($inv['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    # Format date
    list($o_year, $o_month, $o_day) = explode("-", $inv['odate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t<th>SERIAL NO.</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th colspan='2'>UNIT PRICE</th>\n\t\t\t\t<th>UNIT DISCOUNT</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this invoice
    db_connect();
    $sql = "SELECT * FROM inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # Keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $i++;
        # Get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # Get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        # Serial number
        if ($stk['serd'] == 'yes' && $inv['serd'] == 'n') {
            $sers = ext_getavserials($stkd['stkid']);
            $sernos = "<select class='width : 15' name='sernos[]'>";
            foreach ($sers as $skey => $ser) {
                $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
            }
            $sernos .= "</select>";
        } else {
            $sernos = "<input type='hidden' name='sernos[]' value='{$stkd['serno']}'>{$stkd['serno']}";
        }
        # Input qty if not serialised
        $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
        if ($stk['serd'] == 'yes') {
            $qtyin = "<input type='hidden' size='3' name='qtys[]' value='{$stkd['qty']}'>{$stkd['qty']}";
        }
        # check permissions
        if (perm("invoice-unitcost-edit.php")) {
            $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stkd['funitcost']}'>";
            $cunitcost = "<input type='text' size='8' name='cunitcost[]' value='{$stkd['unitcost']}'>";
        } else {
            $viewcost = "<input type='hidden' size='8' name='unitcost[]' value='{$stkd['funitcost']}'>{$stkd['funitcost']}";
            $cunitcost = "<input type='hidden' size='8' name='cunitcost[]' value='{$stkd['unitcost']}'>{$stkd['unitcost']}";
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
        $Vatcodes = "\n\t\t\t<select name='vatcodes[]'>\n\t\t\t\t<option value='0'>Select</option>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatcode'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $Vatcodes .= "</select>";
        # Put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t<td>{$sernos}</td>\n\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t<td>" . CUR . " {$viewcost}</td>\n\t\t\t\t<td>{$inv['currency']} {$cunitcost}</td>\n\t\t\t\t<td>{$inv['currency']}<input type='text' size='4' name='disc[]' value='{$stkd['disc']}'> OR <input type='text' size='4' name='discp[]' value='{$stkd['discp']}' maxlength='5'>%</td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> {$inv['currency']} {$stkd['amt']}</td>\n\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t</tr>";
        $key++;
    }
    # Look above(remprod keys)
    $keyy = $key;
    # Look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    } else {
        $SCROLL = "yes";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && isset($cust['pricelist'])) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # Get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # Get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                if ($stk['serd'] == 'yes') {
                    $sers = ext_getavserials($stkidss[$key]);
                    $sernos = "<select class='width : 15'name='sernos[]' onChange='javascript:document.form.submit();'>";
                    foreach ($sers as $skey => $ser) {
                        $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
                    }
                    $sernos .= "</select>";
                } else {
                    $sernos = "<input type='hidden' name='sernos[]' value=''>";
                }
                # Get price from price list if it is set
                if (isset($cust['pricelist'])) {
                    # get selected stock in this warehouse
                    db_conn("exten");
                    $sql = "SELECT price FROM plist_prices WHERE listid = '{$cust['pricelist']}' AND stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
                    $plRslt = db_exec($sql);
                    if (pg_numrows($plRslt) > 0) {
                        $pl = pg_fetch_array($plRslt);
                        $stk['selamt'] = $pl['price'];
                    }
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class='err'>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                $stk['cselamt'] = sprint($stk['selamt'] / $inv['xrate']);
                # Input qty if not serialised
                $qtyin = "<input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'>";
                if ($stk['serd'] == 'yes') {
                    $qtyin = "<input type='hidden' size='3' name='qtys[]' value='{$qtyss[$key]}'>{$qtyss[$key]}";
                }
                //				$stk['cselamt'] = sprint ($stk['cselamt']);
                $stk['selamt'] = sprint($stk['selamt']);
                # Check permissions
                if (perm("invoice-unitcost-edit.php")) {
                    $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stk['selamt']}'>";
                    $cunitcost = "<input type='text' size='8' name='cunitcost[]' value='{$stk['cselamt']}'>";
                } else {
                    $viewcost = "<input type='hidden' size='8' name='unitcost[]' value='{$stk['selamt']}'>{$stk['selamt']}";
                    $cunitcost = "<input type='hidden' size='8' name='cunitcost[]' value='{$stk['cselamt']}'>{$stk['cselamt']}";
                }
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $amt[$key] = sprint($amt[$key]);
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stk['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t<td>{$sernos}</td>\n\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t<td>" . CUR . " {$viewcost}</td>\n\t\t\t\t\t\t<td>{$inv['currency']} {$cunitcost}</td>\n\t\t\t\t\t\t<td>{$inv['currency']}  <input type='text' size='4' name='disc[]' value='{$discs[$key]}'> OR <input type='text' size='4' name='discp[]' value='{$discps[$key]}' maxlength='5'>%</td>\n\t\t\t\t\t\t<td><input type='hidden' name='amt[]' value='{$amt[$key]}'> {$inv['currency']} {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
            } else {
                if (!isset($diffwhBtn)) {
                    # skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    # get warehouse name
                    db_conn("exten");
                    $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                    $whRslt = db_exec($sql);
                    $wh = pg_fetch_array($whRslt);
                    if (isset($ria) && $ria != "") {
                        $len = strlen($ria);
                        $Wh = "AND lower(substr(stkcod,1,'{$len}'))=lower('{$ria}')";
                    } else {
                        $Wh = "";
                        $ria = "";
                    }
                    # get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        $error .= "<li class='err'>There are no stock items in the selected warehouse.";
                        continue;
                    }
                    if (pg_numrows($stkRslt) == 1) {
                        $ex = "selected";
                    } else {
                        $ex = "";
                    }
                    $stks = "<select class='width : 15'name='stkidss[]' onChange='javascript:document.form.submit();'>";
                    $stks .= "<option value='-S' disabled selected>Select Number</option>";
                    $count = 0;
                    while ($stk = pg_fetch_array($stkRslt)) {
                        $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                    }
                    $stks .= "</select> ";
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vd['del'] == "Yes") {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    # put in drop down and warehouse
                    $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='vatcodess' value='0'></td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td><input type='hidden' size='3' name='qtyss[]'  value='1'>1</td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td>{$inv['currency']} <input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='amts[]' value='0.00'>{$inv['currency']} 0.00</td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t</tr>";
                }
            }
        }
    } else {
        if (!isset($diffwhBtn)) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                if (isset($wtd)) {
                    $whid = $wtd;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                if (isset($ria) && $ria != "") {
                    $len = strlen($ria);
                    $Wh = "AND lower(substr(stkcod,1,'{$len}'))=lower('{$ria}')";
                } else {
                    $Wh = "";
                    $ria = "";
                }
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected store.</li>";
                    //ontinue;
                }
                $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                $stks .= "<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "\n\t\t\t\t\t<input type='hidden' name='vatcodess[]' value=''>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td><input type='hidden' size='3' name='qtyss[]' value='1'>1</td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td>{$inv['currency']} <input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t\t<td>{$inv['currency']} 0.00</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t</tr>";
            } else {
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td>{$inv['currency']}<input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t\t<td>{$inv['currency']} 0.00</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t</tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$whs}</td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td>{$inv['currency']}<input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t<td>{$inv['currency']} 0.00</td>\n\t\t\t\t<td></td>\n\t\t\t</tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    # Calculate tradediscm
    if ($inv['traddisc'] > 0) {
        $traddiscm = sprint($inv['traddisc'] / 100 * ($SUBTOT + $inv['delchrg']));
    } else {
        $traddiscm = "0.00";
    }
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    $VAT = sprint($inv['vat']);
    $TOTAL = sprint($inv['total']);
    $inv['delchrg'] = sprint($inv['delchrg']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    # check only if the customer is selected
    if (isset($cusnum) && $cusnum != "-S") {
        db_connect();
        #check againg credit limit
        if ($TOTAL + $cust['balance'] > $cust['credlimit']) {
            $error .= "<li class='err'>Warning : Customers Credit limit of <b>{$inv['currency']} {$cust['credlimit']}</b> has been exceeded";
            # Check permissions
            if (!perm("invoice-limit-override.php")) {
                $done = "";
            }
        }
        $avcred = $cust['credlimit'] - $cust['balance'];
    } else {
        $avcred = "0.00";
    }
    /*--- Start checks --- */
    $inv['delvat'] += 0;
    if ($inv['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $inv['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $inv['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    db_conn('cubit');
    $Sl = "SELECT * FROM settings WHERE constant='Delivery Note'";
    $Ri = db_exec($Sl) or errDie("Unable to get settings.");
    $data = pg_fetch_array($Ri);
    if ($data['value'] == "Yes") {
        $chp = "checked";
    } else {
        $chp = "";
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    if (!isset($stkerr)) {
        $stkerr = "";
    }
    $avcred = sprint($avcred);
    /* -- Final Layout -- */
    $details = "\n\t\t<center>\n\t\t<h3>New International Invoice</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t\t<input type='hidden' name='letters' value='{$letters}'>\n\t\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td valign='top' width='50%'>\n\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Account No.</td>\n\t\t\t\t\t\t\t\t<td valign='center'>{$cust['accno']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t\t\t\t<td valign='center'>{$customers}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t\t\t\t<td valign='center'>" . nl2br($cust['addr1']) . "</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Customer Order number</td>\n\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$inv['cordno']}'></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Customer Vat Number</td>\n\t\t\t\t\t\t\t\t<td>{$cust['vatnum']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th colspan='2'>Point of Sale</th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Barcode</td>\n\t\t\t\t\t\t\t\t<td><input type='text' size='13' name='bar' value=''></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "' " . ass("Type the first letters of the stock code you are looking for.") . ">\n\t\t\t\t\t\t\t\t<td>Stock Filter</td>\n\t\t\t\t\t\t\t\t<td><input type='text' size='13' name='ria' value='{$ria}' onkeyup='javasript:predict()'></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Print Delivery Note</td>\n\t\t\t\t\t\t\t\t<td><input type='checkbox' name='printdel' {$chp}></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td valign='top' align='right' width='50%'>\n\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th colspan='2'> Invoice Details </th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Invoice No.</td>\n\t\t\t\t\t\t\t\t<td valign='center'>TI {$inv['invid']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Proforma Inv No.</td>\n\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='docref' value='{$inv['docref']}'></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Sales Order No.</td>\n\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='ordno' value='{$inv['ordno']}'></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Foreign Currency</td>\n\t\t\t\t\t\t\t\t<td valign='center'>{$currs['symbol']} - {$currs['name']} &nbsp;&nbsp;Exchange rate " . CUR . " <input type='text' size='7' name='xrate' value='{$inv['xrate']}'></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='inc' {$chin}> No<input type='radio' size='7' name='chrgvat' value='exc' {$chex}></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t\t\t<td valign='center'>{$salesps}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Invoice Date</td>\n\t\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("o", $o_year, $o_month, $o_day) . "</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Available Credit</td>\n\t\t\t\t\t\t\t\t<td>{$inv['currency']} {$avcred}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='traddisc' value='{$inv['traddisc']}'>%</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t\t<td valign='center'>{$inv['currency']}<input type='text' size='7' name='delchrg' value='{$inv['delchrg']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr><td colspan='2'>{$products}</td></tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Comments</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='cust-credit-stockinv.php'>New Invoice</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='comm' rows='4' cols='20'>{$inv['comm']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='invoice-view.php'>View Invoices</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right' valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='50%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>{$inv['currency']} <input type='hidden' name='SUBTOT' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td align='right'>{$inv['currency']} {$inv['discount']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td align='right'>{$inv['currency']} {$inv['delivery']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><b>VAT {$vat14}</b></td>\n\t\t\t\t\t\t\t<td align='right'>{$inv['currency']} {$VAT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>{$inv['currency']} {$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input type='button' value='&laquo Back' onClick='javascript:history.back()'> | <input name='diffwhBtn' type='submit' value='Different Store'> | <input name='addprodBtn' type='submit' value='Add Product'> | <input type='submit' name='saveBtn' value='Save'> </td>\n\t\t\t\t<td>| <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>";
    return $details;
}
コード例 #7
0
function details($_POST, $error = "")
{
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid Non-Stock Invoice number.");
    } elseif (isset($cusnum)) {
        $v->isOk($cusnum, "num", 1, 20, "Invalid Customer number.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return slct($error);
        $confirm = "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($invid)) {
        $invid = create_dummy(0, $cusnum);
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM nons_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # check if invoice has been printed
    if ($inv['done'] == "y") {
        $error = "<li class='err'> Error : invoice number <b>{$invid}</b> has already been printed</li>.";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    $currs = getSymbol($inv['fcid']);
    /* --- Start Drop Downs --- */
    # format date
    list($s_year, $s_month, $s_day) = explode("-", $inv['sdate']);
    # keep the charge vat option stable
    if ($inv['chrgvat'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chnone = "";
    } elseif ($inv['chrgvat'] == "no") {
        $chy = "";
        $chn = "checked=yes";
        $chnone = "";
    } else {
        $chy = "";
        $chn = "";
        $chnone = "checked=yes";
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th colspan='2'>UNIT PRICE</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>VAT Code</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM nons_inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $stkd['amt'] = round($stkd['amt'], 2);
        $chk = "";
        if ($stkd['vatex'] == 'y') {
            $chk = "checked=yes";
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl);
        $vats = "<select name='vatcodes[]'>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatex'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $vats .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $vats .= "</option>";
        $stkd['amt'] = sprint($stkd['amt']);
        # put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='50' name='des[]' value='{$stkd['description']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t<td align='center'> " . CUR . " <input type='text' size='8' name='cunitcost[]' value='{$stkd['cunitcost']}'></td>\n\t\t\t\t<td align='center'> {$inv['currency']} <input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> {$inv['currency']} {$stkd['amt']}</td>\n\t\t\t\t<td align='center'>{$vats}</td>\n\t\t\t\t<td align='center'><input type='checkbox' name='remprod[]' value='{$i}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t</tr>";
        $i++;
    }
    # Look above(remprod keys)
    $keyy = $i;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    if ($i == 0 || isset($diffwhBtn)) {
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl);
        $vats = "<select name='vatcodes[]'>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($vd['del'] == "Yes") {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $vats .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $vats .= "</option>";
        # add one
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='50' name='des[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='1'></td>\n\t\t\t\t<td align='center'>" . CUR . " <input type='text' size='8' name='cunitcost[]'></td>\n\t\t\t\t<td align='center'>{$inv['currency']} <input type='text' size='8' name='unitcost[]'></td>\n\t\t\t\t<td>{$inv['currency']} 0.00</td>\n\t\t\t\t<td>{$vats}</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t</tr>";
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $inv['subtot'];
    # Get Total
    $TOTAL = sprint($inv['total']);
    # Get vat
    $VAT = sprint($inv['vat']);
    /* --- End Some calculations --- */
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['tval']}' AND div = '" . USER_DIV . "'";
    $custRslt = db_exec($sql) or errDie("Unable to view customer");
    $cust = pg_fetch_array($custRslt);
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    $details = "\n\t\t<tr>\n\t\t\t<th colspan='2'> Customer Details </th>\n\t\t</tr>\n\t\t<input type='hidden' name='cusname' value='{$cust['cusname']} {$cust['surname']}'>\n\t\t<input type='hidden' name='cusaddr' value='{$cust['addr1']}'>\n\t\t<input type='hidden' name='cusvatno' value='{$cust['vatnum']}'>\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>Customer</td>\n\t\t\t<td valign='center'>{$cust['cusname']} {$cust['surname']}</td>\n\t\t</tr>\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>Customer Address</td>\n\t\t\t<td valign='center'><pre>{$cust['addr1']}</pre></td>\n\t\t</tr>\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>Customer Vat Number</td>\n\t\t\t<td valign='center'>{$cust['vatnum']}</td>\n\t\t</tr>";
    if (isset($diffwhBtn) or isset($upBtn) or isset($doneBtn)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details = "\n\t\t<center>\n\t\t<h3>New International Non-Stock Invoice</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t{$details}\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer Order number</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$inv['cordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Non-Stock Invoice Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Non-Stock Invoice No.</td>\n\t\t\t\t\t\t\t<td valign='center'>TI {$inv['invid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Proforma Invoice No.</td>\n\t\t\t\t\t\t\t<td><input type='text' name='docref' value='{$inv['docref']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center' nowrap='t'>" . mkDateSelect("s", $s_year, $s_month, $s_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Foreign Currency</td>\n\t\t\t\t\t\t\t<td valign='center'>{$currs['symbol']} - {$currs['name']} &nbsp;&nbsp;Exchange rate " . CUR . " <input type='text' size='7' name='xrate' value='{$inv['xrate']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='yes' {$chy}> No<input type='radio' size='7' name='chrgvat' value='no' {$chn}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='nons-invoice-view.php'>View Non-Stock Invoices</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$inv['remarks']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>{$inv['currency']} <input type='hidden' name='subtot' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t<td align='right'>{$inv['currency']} {$inv['vat']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>{$inv['currency']} <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input type='button' value='&laquo Back' onClick='javascript:history.back()'> | <input name='diffwhBtn' type='submit' value='Add Item'> |</td>\n\t\t\t\t<td><input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}
コード例 #8
0
function details($_POST, $error = "")
{
    extract($_POST);
    # validate input
    include "libs/validate.lib.php";
    $v = new validate();
    if (isset($quoid)) {
        $v->isOk($quoid, "num", 1, 20, "Invalid quote number.");
    }
    if (isset($deptid)) {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    if (isset($letters)) {
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>{$e['msg']}</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($deptid)) {
        $deptid = 0;
    } else {
        if (isset($quoid)) {
            db_conn("cubit");
            $sql = "UPDATE quotes SET deptid='{$deptid}' WHERE quoid='{$quoid}' AND deptid<>'{$deptid}'";
            db_exec($sql) or errDie("Error updating invoice department.");
        }
    }
    if (!isset($quoid)) {
        $quoid = create_dummy($deptid);
    }
    if (!isset($stkerr)) {
        $stkerr = "0,0";
    }
    if (!isset($done)) {
        $done = "";
    }
    if (!isset($sel_frm)) {
        $sel_frm = "stkcod";
    }
    # Get quote info
    db_connect();
    $sql = "SELECT * FROM quotes WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
    $quoRslt = db_exec($sql) or errDie("Unable to get quote information");
    if (pg_numrows($quoRslt) < 1) {
        return "<li class='err'>Quote Not Found</li>";
    }
    $quo = pg_fetch_array($quoRslt);
    # check if quote has been printed
    if ($quo['accepted'] == "y") {
        $error = "<li class='err'> Error : Quote number <b>{$quoid}</b> has already been printed.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    if (!isset($lead)) {
        $lead = $quo["lead"];
    }
    //manual error handling
    if (!isset($quo['ncdate'])) {
        $quo['ncdate'] = "";
    }
    if (strlen($quo['ncdate']) < 1) {
        $ncdate_year = date("Y");
        $ncdate_month = date("m", mktime(0, 0, 0, date("m"), date("d") + 5, date("Y")));
        $ncdate_day = date("d", mktime(0, 0, 0, date("m"), date("d") + 5, date("Y")));
    } else {
        $darr = explode("-", $quo['ncdate']);
        $ncdate_year = $darr['0'];
        $ncdate_month = $darr['1'];
        $ncdate_day = $darr['2'];
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$quo['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected customer info
    if (isset($letters)) {
        db_connect();
        $sql = "SELECT * FROM customers WHERE cusnum = '{$quo['cusnum']}' AND div = '" . USER_DIV . "'";
        $custRslt = db_exec($sql) or errDie("Unable to view customer");
        if (pg_numrows($custRslt) < 1) {
            db_connect();
            if ($inv['deptid'] == 0) {
                $searchdept = "";
            } else {
                $searchdept = "deptid = '{$quo['deptid']}' AND ";
            }
            # Query server for customer info
            $sql = "SELECT cusnum,cusname,surname FROM customers WHERE {$searchdept} location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND div = '" . USER_DIV . "' ORDER BY surname";
            $custRslt = db_exec($sql) or errDie("Unable to view customers");
            if (pg_numrows($custRslt) < 1) {
                $ajax_err = "<li class='err'>No customer names starting with <b>{$letters}</b> in database.</li>";
                //return view_err($_POST, $err);
            } else {
                $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
                $customers .= "<option value='-S' selected>Select Customer</option>";
                while ($cust = pg_fetch_array($custRslt)) {
                    $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
                }
                $customers .= "</select>";
            }
            # take care of the unset vars
            $cust['addr1'] = "";
            $cust['cusnum'] = "";
            $cust['vatnum'] = "";
            $cust['accno'] = "";
        } else {
            $cust = pg_fetch_array($custRslt);
            # moarn if customer account has been blocked
            if ($cust['blocked'] == 'yes') {
                return "<li class='err'>Error : Selected customer account has been blocked.</li>";
            }
            $customers = "<input type='hidden' name=cusnum value='{$cust['cusnum']}'>{$cust['cusname']}  {$cust['surname']}";
            $cusnum = $cust['cusnum'];
        }
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    //no good ... breaks the search feature because it doesnt send search with
    // onChange='javascript:document.form.submit();'
    $whs = "<select name='whidss[]'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            if (!user_in_store_team($wh["whid"], USER_ID)) {
                continue;
            }
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class='err'> There are no Sales People found in Cubit.</li>";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $quo['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $quo['terms']);
    # Keep the charge vat option stable
    if ($quo['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($quo['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    # format date
    list($quote_year, $quote_month, $quote_day) = explode("-", $quo['odate']);
    //	list($followon_year, $followon_month, $followon_day) = explode("-", $quo['ncdate']);
    /* --- End Drop Downs --- */
    // get the ID of the first warehouse
    db_conn("exten");
    $sql = "SELECT whid FROM warehouses ORDER BY whid ASC LIMIT 1";
    $rslt = db_exec($sql) or errDie("Error reading warehouses (FWH).");
    if (pg_num_rows($rslt) > 0) {
        $FIRST_WH = pg_fetch_result($rslt, 0, 0);
    } else {
        $FIRST_WH = "-S";
    }
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>UNIT DISCOUNT</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this quote
    db_connect();
    $sql = "SELECT * FROM quote_items  WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            db_conn('core');
            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
            $Accounts = "\n\t\t\t\t<select name='accounts[]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
            while ($ad = pg_fetch_array($Ri)) {
                if (isb($ad['accid'])) {
                    continue;
                }
                if ($ad['accid'] == $stkd['account']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
            }
            $Accounts .= "</select>";
            $sernos = "";
            $stkd['unitcost'] = sprint($stkd['unitcost']);
            $stkd['amt'] = sprint($stkd['amt']);
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
            $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>";
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            //print "fo";
            # Put in product
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whids[]' value='{$stkd['whid']}'></td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$stkd['description']}'> {$sernos}</td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td><input type='hidden' name='disc[]' value='{$stkd['disc']}'><input type='hidden' name='discp[]' value='{$stkd['discp']}'></td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t</tr>";
            $key++;
        } else {
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            # get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            $stkd['unitcost'] = sprint($stkd['unitcost']);
            $stkd['amt'] = sprint($stkd['amt']);
            # put in product
            $products .= "\n\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t\t<td><input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t\t<td><input type='text' size='4' name='disc[]' value='{$stkd['disc']}'> OR <input type='text' size='4' name='discp[]' value='{$stkd['discp']}' maxlength=5>%</td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t</tr>";
            $key++;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && isset($cust['pricelist'])) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get price from price list if it is set
                if (isset($cust['pricelist'])) {
                    # get selected stock in this warehouse
                    db_conn("exten");
                    $sql = "SELECT price FROM plist_prices WHERE listid = '{$cust['pricelist']}' AND stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
                    $plRslt = db_exec($sql);
                    if (pg_numrows($plRslt) > 0) {
                        $pl = pg_fetch_array($plRslt);
                        $stk['selamt'] = $pl['price'];
                    }
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class='err'>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $stk['selamt'] = sprint($stk['selamt']);
                $amt[$key] = sprint($amt[$key]);
                # put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stk['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'></td>\n\t\t\t\t\t\t<td><input type='text' size='8' name='unitcost[]'  value='{$stk['selamt']}'></td>\n\t\t\t\t\t\t<td><input type='text' size='4' name='disc[]' value='{$discs[$key]}'> OR <input type='text' size='4' name='discp[]' value='{$discps[$key]}' maxlength='5'>%</td>\n\t\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
            } elseif (isset($accountss[$key]) && $accountss[$key] != "0" && isset($cust['pricelist'])) {
                db_conn('core');
                $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                if (pg_num_rows($Ri) < 1) {
                    return "invalid.";
                }
                $ad = pg_fetch_array($Ri);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                # Input qty if not serialised
                $qtyin = "<input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'>";
                $unitcosts[$key] = sprint($unitcosts[$key]);
                $amt[$key] = sprint($amt[$key]);
                # Check permissions
                $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$unitcosts[$key]}'>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vatcodess[$key] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='2'>{$ad['accname']}<input type='hidden' name='accounts[]' value='{$accountss[$key]}'><input type='hidden' name='whids[]' value='0'></td>\n\t\t\t\t\t\t<td>{$Vatcodes}<input type='hidden' name='stkids[]' value='0'></td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$descriptionss[$key]}'></td>\n\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='disc[]' value='0'><input type='hidden' name='discp[]' value='0'></td>\n\t\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
            } else {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                if (!isset($addnon)) {
                    if (isset($filter_store) and $filter_store != "0") {
                        # get warehouse name
                        db_conn("exten");
                        $sql = "SELECT whname FROM warehouses WHERE whid = '{$filter_store}' AND div = '" . USER_DIV . "'";
                        $whRslt = db_exec($sql);
                        $wh = pg_fetch_array($whRslt);
                    }
                    if (isset($des) and $des != "") {
                        $len = strlen($des);
                        if ($des == "Show All") {
                            $Wh = "";
                            $des = "";
                        } else {
                            $Wh = "AND (lower(substr(stkdes,1,'{$len}'))=lower('{$des}') OR lower(substr(stkcod,1,'{$len}'))=lower('{$des}'))";
                        }
                    } else {
                        $Wh = "AND FALSE";
                        $des = "";
                    }
                    $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
                    if (isset($check_setting) and $check_setting == "yes") {
                        if (isset($filter_class) and $filter_class != "0") {
                            $Wh .= " AND prdcls = '{$filter_class}'";
                        }
                        if (isset($filter_cat) and $filter_cat != "0") {
                            $Wh .= " AND catid = '{$filter_cat}'";
                        }
                    }
                    if (isset($filter_store) and $filter_store != "0") {
                        $Wh .= " AND whid = '{$filter_store}'";
                    }
                    # get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        $error .= "<li class='err'>There are no stock items in the selected store.</li>";
                        continue;
                    }
                    if ($sel_frm == "stkcod") {
                        $cods = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                        $cods .= "<option value='-S' disabled selected>Select Number</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            $cods .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $cods .= "</select> ";
                        $descs = "";
                    } else {
                        $descs = "<select style='width:250px' name='stkidss[]' onChange='javascript:document.form.submit();'>";
                        $descs .= "<option value='-S' disabled selected>Select Description</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            $descs .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $descs .= "</select> ";
                        $cods = "";
                    }
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vd['del'] == "Yes") {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    # put in drop down and warehouse
                    $products .= "\n\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$filter_store}'></td>\n\t\t\t\t\t\t\t<td>{$cods}<input type='hidden' name='vatcodess' value='0'></td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t<td>{$descs}</td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]'  value='1'></td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td><input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='amts[]' value='0.00'>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t</tr>";
                } else {
                    db_conn('core');
                    $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                    $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                    $Accounts = "\n\t\t\t\t\t\t<select name='accountss[]'>\n\t\t\t\t\t\t\t<option value='0'>Select Account</option>";
                    while ($ad = pg_fetch_array($Ri)) {
                        if (isb($ad['accid'])) {
                            continue;
                        }
                        $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                    }
                    $Accounts .= "</select>";
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vd['del'] == "Yes") {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[]' value='{$FIRST_WH}'></td>\n\t\t\t\t\t\t\t<inpu type='hidden' name='stkidss[]' value=''>\n\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='discs[]' value='0'><input type='hidden' name='discps[]' value='0' ></td>\n\t\t\t\t\t\t</tr>";
                }
            }
        }
    } else {
        if (!isset($addnon) && !isset($upBtn)) {
            if (isset($filter_store) and $filter_store != "0") {
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$filter_store}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
            }
            if (isset($des) and $des != "") {
                $len = strlen($des);
                if ($des == "Show All") {
                    $Wh = "";
                    $des = "";
                } else {
                    $Wh = "AND (lower(substr(stkdes,1,'{$len}'))=lower('{$des}') OR lower(substr(stkcod,1,'{$len}'))=lower('{$des}'))";
                }
            } else {
                $Wh = "AND FALSE";
                $des = "";
            }
            $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
            if (isset($check_setting) and $check_setting == "yes") {
                if (isset($filter_class) and $filter_class != "0") {
                    $Wh .= " AND prdcls = '{$filter_class}'";
                }
                if (isset($filter_cat) and $filter_cat != "0") {
                    $Wh .= " AND catid = '{$filter_cat}'";
                }
            }
            if (isset($filter_store) and $filter_store != "0") {
                $Wh .= " AND whid = '{$filter_store}'";
            }
            # get stock on this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
            $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
            if (pg_numrows($stkRslt) < 1) {
                if (!isset($err)) {
                    $err = "";
                }
                $err .= "<li>There are no stock items in the selected warehouse.</li>";
            }
            $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
            $stks .= "<option value='-S' disabled selected>Select Number</option>";
            $count = 0;
            while ($stk = pg_fetch_array($stkRslt)) {
                $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
            }
            $stks .= "</select> ";
            $products .= "\n\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t<input type='hidden' name='vatcodess[]' value=''>\n\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$filter_store}'></td>\n\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td><input type='text' size='4' name='discs[]' value='0'> OR <input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td></td>\n\t\t\t\t</tr>";
        } else {
            if (isset($addnon)) {
                db_conn('core');
                $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                $Accounts = "\n\t\t\t\t<select name='accountss[]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
                while ($ad = pg_fetch_array($Ri)) {
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                }
                $Accounts .= "</select>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vd['del'] == "Yes") {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[]' value='{$FIRST_WH}'></td>\n\t\t\t\t\t<inpu type='hidden' name='stkidss[]' value=''>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td><input type='hidden' name='discs[]' value='0'><input type='hidden' name='discps[]' value='0'></td>\n\t\t\t\t</tr>";
            }
        }
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($quo['subtot']);
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($quo['subtot']);
    $VAT = sprint($quo['vat']);
    $TOTAL = sprint($quo['total']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    # check only if the customer is selected
    if (isset($cusnum) && $cusnum != "-S") {
        #check againg credit limit
        if ($cust['credlimit'] != 0 && $TOTAL + $cust['balance'] > $cust['credlimit']) {
            $error .= "<li class='err'>Warning : Customers Credit limit of <b>" . CUR . " {$cust['credlimit']}</b> has been exceeded</li>";
        }
        $avcred = $cust['credlimit'] - $cust['balance'];
    } else {
        $avcred = "0.00";
    }
    $quo['delvat'] += 0;
    if ($quo['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $quo['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $quo['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    // Which display method was selected
    if (isset($sel_frm) && $sel_frm == "stkdes") {
        $sel_frm_cod = "";
        $sel_frm_des = "checked";
    } else {
        $sel_frm_cod = "checked";
        $sel_frm_des = "";
    }
    $sel = "";
    if (isset($lead) and strlen($lead) > 0) {
        $sel = "checked=yes";
    }
    /*--- Start checks --- */
    /* -- Final Layout --No VAT<input type=radio size=7 name=chrgvat value='nov' $chno> */
    $details_begin = "\n\t\t<center>\n\t\t<h3>New Quote</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='quoid' value='{$quoid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<div id='cust_selection'>";
    if (empty($ajax_err) && (isset($cusnum) || AJAX)) {
        if (isset($cusnum)) {
            $OTS_OPT = onthespot_encode(SELF, "cust_selection", "deptid={$quo['deptid']}&letters={$letters}&cusnum={$cusnum}&quoid={$quoid}");
            $custedit = "\n\t\t\t\t<td nowrap>\n\t\t\t\t\t<a href='javascript: popupSized(\"cust-edit.php?cusnum={$cusnum}&onthespot={$OTS_OPT}\", \"edit_cust\", 700, 630);'>Edit Customer Details</a>\n\t\t\t\t</td>";
        } else {
            $custedit = "";
        }
        $optional_filter_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
        if (isset($optional_filter_setting) and $optional_filter_setting == "yes") {
            db_connect();
            $catsql = "SELECT catid, cat, catcod FROM stockcat WHERE div = '" . USER_DIV . "' ORDER BY cat ASC";
            $catRslt = db_exec($catsql);
            if (pg_numrows($catRslt) < 1) {
                $cat_drop = "<input type='hidden' name='filter_cat' value='0'>";
            } else {
                $cat_drop = "<select name='filter_cat'>";
                $cat_drop .= "<option value='0'>All Categories</option>";
                while ($cat = pg_fetch_array($catRslt)) {
                    if (isset($filter_cat) and $filter_cat == $cat['catid']) {
                        $cat_drop .= "<option value='{$cat['catid']}' selected>({$cat['catcod']}) {$cat['cat']}</option>";
                    } else {
                        $cat_drop .= "<option value='{$cat['catid']}'>({$cat['catcod']}) {$cat['cat']}</option>";
                    }
                }
                $cat_drop .= "</select>";
            }
            # Select classification
            $classsql = "SELECT * FROM stockclass WHERE div = '" . USER_DIV . "' ORDER BY classname ASC";
            $clasRslt = db_exec($classsql);
            if (pg_numrows($clasRslt) < 1) {
                $class_drop = "<input type='hidden' name='filter_class' value='0'>";
            } else {
                $class_drop = "<select name='filter_class' style='width: 167'>";
                $class_drop .= "<option value='0'>All Classifications</option>";
                while ($clas = pg_fetch_array($clasRslt)) {
                    if (isset($filter_class) and $filter_class == $clas['clasid']) {
                        $class_drop .= "<option value='{$clas['clasid']}' selected>{$clas['classname']}</option>";
                    } else {
                        $class_drop .= "<option value='{$clas['clasid']}'>{$clas['classname']}</option>";
                    }
                }
                $class_drop .= "</select>";
            }
            $display_optional_filters = "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Category</td>\n\t\t\t\t\t<td>{$cat_drop}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Classification</td>\n\t\t\t\t\t<td>{$class_drop}</td>\n\t\t\t\t</tr>";
        }
        db_conn("exten");
        $sql = "SELECT whid, whname, whno FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
        $whRslt = db_exec($sql);
        if (pg_numrows($whRslt) < 1) {
            $store_drop = "<input type='hidden' name='filter_store' value='0'>";
        } else {
            if (!isset($filter_store)) {
                # check if setting exists
                db_connect();
                $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
                $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
                if (pg_numrows($Rslt) > 0) {
                    $set = pg_fetch_array($Rslt);
                    $filter_store = $set['value'];
                }
            }
            $store_drop = "<select name='filter_store'>";
            $store_drop .= "<option value='0'>All Stores</option>";
            while ($wh = pg_fetch_array($whRslt)) {
                if (isset($filter_store) and $filter_store == $wh['whid']) {
                    $store_drop .= "<option value='{$wh['whid']}' selected>({$wh['whno']}) {$wh['whname']}</option>";
                } else {
                    $store_drop .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
                }
            }
            $store_drop .= "</select>";
        }
        $ajaxOut = "\n\t\t\t<input type='hidden' name='letters' value='{$letters}'>\n\t\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account No.</td>\n\t\t\t\t\t<td valign='center'>{$cust['accno']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t<td valign='center'>{$customers}</td>\n\t\t\t\t\t{$custedit}\n\t\t\t\t\t<td><input type='button' onClick=\"javascript:document.location=('pos-quote-new.php')\" value='Quote Non Customer'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t<td valign='center'>" . nl2br($cust['addr1']) . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer Req number</td>\n\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$quo['cordno']}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer VAT Number</td>\n\t\t\t\t\t<td>{$cust['vatnum']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Next Contact Date</td>\n\t\t\t\t\t<td valign='center'>" . mkDateSelect("ncdate", $ncdate_year, $ncdate_month, $ncdate_day) . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Add As Lead</td>\n\t\t\t\t\t<td valign='center'><input type='checkbox' name='lead' {$sel} value='yes'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Using</td>\n\t\t\t\t\t<td>Stock Code<input type='radio' name='sel_frm' value='stkcod' onChange='javascript:document.form.submit();' {$sel_frm_cod}><br>Stock Description<input type='radio' name='sel_frm' value='stkdes' onChange='javascript:document.form.submit();' {$sel_frm_des}></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'>Additional Filters</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Store</td>\n\t\t\t\t\t<td>{$store_drop}</td>\n\t\t\t\t</tr>\n\t\t\t\t{$display_optional_filters}\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Stock Filter</td>\n\t\t\t\t\t<td><input type='text' size='13' name='des' value='{$des}'> <input type='submit' value='Search'> <input type='submit' name='des' value='Show All'></td>\n\t\t\t\t</tr>\n\t\t\t</table>";
    } else {
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE div = '" . USER_DIV . "' ORDER BY deptname ASC";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($deptRslt) < 1) {
            return "<li class='err'>There are no Departments found in Cubit.</li>";
        } else {
            $depts = "<select id='deptid'>";
            $depts .= "<option value='0'>All Departments</option>";
            while ($dept = pg_fetch_array($deptRslt)) {
                $depts .= "<option value='{$dept['deptid']}'>{$dept['deptname']}</option>";
            }
            $depts .= "</select>";
        }
        if (!isset($ajax_err)) {
            $ajax_err = "";
        }
        $ajaxOut = "\n\t\t\t<script>\n\t\t\t\tfunction updateCustSelection() {\n\t\t\t\t\tdeptid = getObject('deptid').value;\n\t\t\t\t\tletters = getObject('letters').value;\n\t\t\t\t\tajaxRequest('" . SELF . "', 'cust_selection', AJAX_SET, 'letters='+letters+'&deptid='+deptid+'&quoid={$quoid}');\n\t\t\t\t}\n\t\t\t</script>\n\t\t\t{$ajax_err}\n\t\t\t<table " . TMPL_tblDflts . " width='400'>\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'>New Quote</th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>" . REQ . "Select Department</td>\n\t\t\t\t<td valign='center'>{$depts}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>First Letters of customer</td>\n\t\t\t\t<td valign='center'><input type='text' size='5' id='letters' maxlength='5'></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td valign='center'><input type='button' value='Update &raquo' onClick='updateCustSelection();'></td>\n\t\t\t</tr>\n\t\t</table>";
    }
    $avcred = sprint($avcred);
    if (isset($addprodBtn) or isset($addnon) or isset($saveBtn) or isset($upBtn) or isset($doneBtn) or isset($donePrnt) or isset($des)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details_end = "\n\t\t\t\t\t</div>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Quote Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Quote No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$quo['quoid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='ordno' value='{$quo['ordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='inc' {$chin}> No<input type='radio' size='7' name='chrgvat' value='exc' {$chex}> </td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t\t<td valign='center'>{$salesps}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Quote Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("quote", $quote_year, $quote_month, $quote_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Available Credit</td>\n\t\t\t\t\t\t\t<td>" . CUR . " {$avcred}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='traddisc' value='{$quo['traddisc']}'>%</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='7' name='delchrg' value='{$quo['delchrg']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td rowspan='2'>" . mkQuickLinks(ql("quote-view.php", "View Quotes"), ql("customers-new.php", "New Customer")) . "\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<th width='25%'>Comments</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'>\n\t\t\t\t\t\t\t\t<textarea name='comm' rows='4' cols='20'>{$quo['comm']}</textarea>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " <input type='hidden' name='SUBTOT' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$quo['discount']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$quo['delivery']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><b>VAT {$vat14}</b></td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='center' colspan='2' nowrap><input name='addprodBtn' type='submit' value='Add Product'> | <input name='addnon' type='submit' value='Add Non stock Product'> | <input type='submit' name='saveBtn' value='Save'> | <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    if (AJAX) {
        return $ajaxOut;
    } else {
        return "{$details_begin}{$ajaxOut}{$details_end}";
    }
}
コード例 #9
0
function details($_POST, $error = "")
{
    # Get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    if (!isset($button) && isset($starting)) {
        return slct();
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid Non-Stock Invoice number.");
    } elseif (isset($ctyp)) {
        $val = $ctyp . "val";
        if (isset(${$val})) {
            $tval = ${$val};
            $v->isOk($tval, "num", 1, 20, "Invalid Selection.");
        }
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class=err>" . $e["msg"];
        }
        return slct($error);
        $confirm = "{$error}<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($invid) && isset($ctyp)) {
        $val = $ctyp . "val";
        if (!isset(${$val})) {
            ${$val} = "";
        }
        $tval = ${$val};
        if (isset($bankid)) {
            $bankid += 0;
            $acc = $bankid;
        } else {
            $acc = 0;
        }
        $invid = create_dummy(0, $ctyp, $tval, $acc);
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM nons_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class=err>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # check if invoice has been printed
    if ($inv['done'] == "y") {
        $error = "<li class=err> Error : invoice number <b>{$invid}</b> has already been printed.";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    /* --- Start Drop Downs --- */
    # format date
    list($ninv_year, $ninv_month, $ninv_day) = explode("-", $inv['odate']);
    # keep the charge vat option stable
    if ($inv['chrgvat'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chnone = "";
    } elseif ($inv['chrgvat'] == "no") {
        $chy = "";
        $chn = "checked=yes";
        $chnone = "";
    } else {
        $chy = "";
        $chn = "";
        $chnone = "checked=yes";
    }
    # Days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $inv['terms']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=100%>\r\n\t<tr>\r\n\t\t<th>DESCRIPTION</th>\r\n\t\t<th>QTY</th>\r\n\t\t<th>AMOUNT</th>\r\n\t\t<th>VAT Code</th>\r\n\t\t<th>Remove</th>\r\n\t<tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM nons_inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $stkd['amt'] = round($stkd['amt'], 2);
        $chk = "";
        if ($stkd['vatex'] == 'y') {
            $chk = "checked=yes";
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl);
        $vats = "<select name=vatcodes[]>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatex'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $vats .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $vats .= "</option>";
        $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatex']}'";
        $Ri = db_exec($Sl);
        $vd = pg_fetch_array($Ri);
        if (TAX_VAT != $vd['vat_amount'] and $vd['vat_amount'] != "0.00") {
            $showvat = FALSE;
        }
        # put in product
        $products .= "<tr class='bg-odd'>\r\n\t\t\t<td align=center><input type=text size=50 name=des[] value='{$stkd['description']}'></td>\r\n\t\t\t<td align=center><input type=text size=3 name=qtys[] value='{$stkd['qty']}'></td>\r\n\t\t\t<td><input type=hidden name=amt[] value='" . sprint($stkd["amt"]) . "'> " . CUR . " " . sprint($stkd["amt"]) . "</td>\r\n\t\t\t<!--<td align=center><input type=checkbox name=vatex[] value='{$i}' {$chk}></td>-->\r\n\t\t\t<td align=center>{$vats}</td>\r\n\t\t\t<td align=center><input type=checkbox name=remprod[] value='{$i}'><input type=hidden name=SCROLL value=yes></td>\r\n\t\t</tr>";
        $i++;
    }
    # Look above(remprod keys)
    $keyy = $i;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    # Get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class=err> There are no Sales People found in Cubit.";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $inv['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    if ($i == 0 || isset($diffwhBtn)) {
        # add one
        $products .= "<tr class='bg-odd'>\r\n\t\t\t<td align=center><input type=text size=50 name=des[] value=''></td>\r\n\t\t\t<td align=center><input type=text size=3 name=qtys[] value='1'></td>\r\n\t\t\t<td>" . CUR . " 0.00</td>\r\n\t\t\t<td>&nbsp;</td>\r\n\t\t\t<td>&nbsp;</td>\r\n\t\t</tr>";
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $inv['subtot'];
    # Get Total
    $TOTAL = sprint($inv['total']);
    # Get vat
    $VAT = sprint($inv['vat']);
    /* --- End Some calculations --- */
    db_conn('cubit');
    if ($inv['ctyp'] == 's') {
        $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['tval']}' AND div = '" . USER_DIV . "'";
        $custRslt = db_exec($sql) or errDie("Unable to view customer");
        $cust = pg_fetch_array($custRslt);
        if (!empty($cust["cusname"])) {
            $cn = "{$cust['cusname']} {$cust['surname']}";
        } else {
            $cn = "{$cust['surname']}";
        }
        $details = "\r\n\t\t<tr><th colspan=2> Customer Details </th></tr>\r\n\t\t<input type=hidden name=cusname value='{$cn}'>\r\n\t\t<input type=hidden name=cusaddr value='{$cust['addr1']}'>\r\n\t\t<input type=hidden name=cusvatno value='{$cust['vatnum']}'>\r\n\t\t<tr class='bg-odd'><td>Customer</td><td valign=center>{$cust['cusname']} {$cust['surname']}</td></tr>\r\n\t\t<tr class='bg-even'><td>Customer Address</td><td valign=center><pre>{$cust['addr1']}</pre></td></tr>\r\n\t\t<tr class='bg-odd'><td>Customer VAT Number</td><td valign=center>{$cust['vatnum']}</td></tr>";
    } elseif ($inv['ctyp'] == 'c') {
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE deptid = '{$inv['tval']}'";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        $dept = pg_fetch_array($deptRslt);
        $details = "\r\n\t\t<tr><th colspan=2> Customer Details </th></tr>\r\n\t\t<tr class='bg-odd'><td>Customer</td><td valign=middle><input type=text name=cusname value='{$inv['cusname']}'></td></tr>\r\n\t\t<tr class='bg-even'><td valign=top>Customer Address</td><td valign=middle><textarea name=cusaddr cols=18 rows=3>{$inv['cusaddr']}</textarea></td></tr>\r\n\t\t<tr class='bg-odd'><td valign=top>Customer VAT No.</td><td valign=middle><input type=text name=cusvatno value='{$inv['cusvatno']}'></td></tr>";
    } else {
        $details = "\r\n\t\t<tr><th colspan=2> Customer Details </th></tr>\r\n\t\t<tr class='bg-odd'><td>Customer</td><td valign=middle><input type=text name=cusname value='{$inv['cusname']}'></td></tr>\r\n\t\t<tr class='bg-even'><td valign=top>Customer Address</td><td valign=middle><textarea name=cusaddr cols=18 rows=3>{$inv['cusaddr']}</textarea></td></tr>\r\n\t\t<tr class='bg-odd'><td valign=top>Customer VAT No.</td><td valign=middle><input type=text name=cusvatno value='{$inv['cusvatno']}'></td></tr>";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM settings WHERE constant='SALES'";
    $Ri = db_exec($Sl) or errDie("Unable to get settings.");
    $data = pg_fetch_array($Ri);
    if ($data['value'] == "Yes") {
        $sc = "checked";
    } else {
        $sc = "";
    }
    $sales = "<td>\r\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t<tr><td>{$salesps}</td><td>Print</td><td><input type=checkbox name=printsales {$sc}></td></tr>\r\n\t</table>\r\n\t</td>";
    // Retrieve the default comments
    db_conn("cubit");
    $sql = "SELECT value FROM settings WHERE constant='DEFAULT_COMMENTS'";
    $cmntRslt = db_exec($sql) or errDie("Unable to retrieve default comment from Cubit.");
    if (empty($inv["remarks"])) {
        $remarks = base64_decode(pg_fetch_result($cmntRslt, 0));
    } else {
        $remarks = $inv["remarks"];
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    $deldate = explode("-", $inv["deldate"]);
    /* -- Final Layout -- */
    $details = "<center><h3>New Non-Stock Invoices</h3>\r\n\t<form action='" . SELF . "' method=post name=form>\r\n\t<input type=hidden name=key value=update>\r\n\t<input type=hidden name=invid value='{$invid}'>\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=95%>\r\n \t<tr><td valign=top>\r\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\r\n\t\t\t{$details}\r\n\t\t\t<tr class='bg-even'><td>Customer Order number</td><td valign=center><input type=text size=10 name=cordno value='{$inv['cordno']}'></td></tr>\r\n\t\t</table>\r\n\t</td>\r\n\t<td valign=top align=right>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t<tr>\r\n\t\t<th colspan='2'>Non-Stock Invoice Details</th>\r\n\t</tr>\r\n\t<tr class='bg-odd'>\r\n\t\t<td>Non-Stock Invoice No.</td>\r\n\t\t<td valign=center>TI {$inv['invid']}</td>\r\n\t</tr>\r\n\t<tr class='bg-even'>\r\n\t\t<td>Proforma Invoice No.</td>\r\n\t\t<td><input type='text' name='docref' value='{$inv['docref']}'></td>\r\n\t</tr>\r\n\t<tr class='bg-odd'>\r\n\t\t<td>Date</td>\r\n\t\t<td valign='center'>" . mkDateSelect("ninv", $ninv_year, $ninv_month, $ninv_day) . " DD-MM-YYYY</td>\r\n\t</tr>\r\n\t<tr class='bg-even'>\r\n\t\t<td>VAT Inclusive</td>\r\n\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='yes' {$chy}> No<input type=radio size=7 name=chrgvat value='no' {$chn}></td>\r\n\t</tr>\r\n\t<tr class='bg-odd'>\r\n\t\t<td>Terms</td>\r\n\t\t<td valign='center'>{$termssel} Days</td>\r\n\t</tr>\r\n\t<tr class='bg-even'>\r\n\t\t<td>Sales Person</td>\r\n\t\t{$sales}\r\n\t</tr>\r\n\t</table>\r\n\t</td></tr>\r\n\t<tr><td><br></td></tr>\r\n\t<tr><td colspan=2>{$products}</td></tr>\r\n\t<tr><td>\r\n\t\t<p>\r\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t\t\t<tr><th width=25%>Quick Links</th><th width=25%>Remarks</th><td rowspan=5 valign=top width=50%>{$error}</td></tr>\r\n\t\t\t<tr><td class='bg-odd'><a href='nons-invoice-view.php'>View Non-Stock Invoices</a></td><td class='bg-odd' rowspan=4 align=center valign=top><textarea name=remarks rows=4 cols=20>{$remarks}</textarea></td></tr>\r\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\r\n\t\t</table>\r\n\t</td><td align=right>\r\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=80%>\r\n\t\t\t<tr class='bg-odd'><td>SUBTOTAL</td><td align=right>" . CUR . " <input type=hidden name=subtot value='{$SUBTOT}'>{$SUBTOT}</td></tr>\r\n\t\t\t<tr class='bg-odd'><td>VAT {$vat14}</td><td align=right>" . CUR . " {$inv['vat']}</td></tr>\r\n\t\t\t<tr class='bg-even'><th>GRAND TOTAL</th><td align=right>" . CUR . " <input type=hidden name=total value='{$TOTAL}'>{$TOTAL}</td></tr>\r\n\t\t</table>\r\n\t</td></tr>\r\n\t<tr><td align=right><input name=diffwhBtn type=submit value='Add Item'> |</td><td><input type=submit name='upBtn' value='Update'>{$done}</td></tr>\r\n\t</table></form>\r\n\t</center>";
    return $details;
}
コード例 #10
0
function search($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($purnum, "string", 1, 20, "Invalid Purchase number.");
    # display errors, if any
    $error = "";
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class=err>" . $e["msg"];
        }
        return slct($_POST, $error);
    }
    $purs = explode(",", $purnum);
    foreach ($purs as $pur) {
        print $pur . "<br>";
    }
    # Send search squad
    db_connect();
    $sql = "SELECT * FROM purchases WHERE purnum = '{$purnum}' AND div = '" . USER_DIV . "'";
    $srchRslt = db_exec($sql) or errDie("Unable to retrieve purchases from database.");
    if (pg_numrows($srchRslt) > 0) {
        $purid = create_dummy(0, $purnum, 'loc', 'cubit');
        $send['purid'] = $purid;
        return details($send);
    }
    $sql = "SELECT * FROM purch_int WHERE purnum = '{$purnum}' AND div = '" . USER_DIV . "'";
    $srchRslt = db_exec($sql) or errDie("Unable to retrieve purchases from database.");
    if (pg_numrows($srchRslt) > 0) {
        $purid = create_dummy(0, $purnum, 'int', 'cubit');
        $send['purid'] = $purid;
        return details($send);
    }
    $sql = "SELECT * FROM movpurch WHERE purnum = '{$purnum}' AND div = '" . USER_DIV . "'";
    $srchRslt = db_exec($sql) or errDie("Unable to retrieve purchases from database.");
    if (pg_numrows($srchRslt) > 0) {
        $res = pg_fetch_array($srchRslt);
        $purid = create_dummy(0, $purnum, $res['purtype'], $res['prd']);
        $send['purid'] = $purid;
        return details($send);
    }
    return slct($_POST, "<li class=err> - Purchase No. {$purnum} not found.");
}
コード例 #11
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid invoice number.");
    } else {
        $client = "";
        $deptid = 2;
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    $fil = "";
    if (!isset($fcode)) {
        $fcode = "";
    }
    if (!isset($fdes)) {
        $fdes = "";
    }
    if ($fcode != "") {
        $fil .= "AND lower(stkcod) LIKE lower('%{$fcode}%')";
    }
    if ($fdes != "") {
        $fil .= "AND lower(stkdes) LIKE lower('%{$fdes}%')";
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
        $stkerr = "0,0";
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM pinvoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    if (!isset($pass)) {
        $pass = "";
    } else {
        $pass = remval($pass);
        db_conn('cubit');
        $Sl = "SELECT * FROM users WHERE password=md5('{$pass}') AND abo=1000";
        $Ri = db_exec($Sl);
        if (pg_num_rows($Ri) > 0) {
            $ped = true;
        } else {
            $ped = false;
        }
    }
    # check if invoice has been printed
    if ($inv['printed'] == "y") {
        $error = "<li class=err> Error : Invoice number <b>{$invid}</b> has already been printed.";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # Get selected Customer info
    db_connect();
    $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['cusnum']}' AND location != 'int' AND div = '" . USER_DIV . "'";
    $custRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($custRslt) < 1) {
        db_connect();
        # Query server for customer info   AND lower(surname) LIKE lower('$letters%')
        $sql = "SELECT cusnum,cusname,surname FROM customers WHERE location != 'int' AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            $customers .= "<option value='0' selected>Select Customer</option>";
            while ($cust = pg_fetch_array($custRslt)) {
                $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
            }
            $customers .= "</select>";
        } else {
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            $customers .= "<option value='0' selected>Select Customer</option>";
            while ($cust = pg_fetch_array($custRslt)) {
                $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
            }
            $customers .= "</select>";
        }
        # Take care of the unset vars
        $cust['addr1'] = "";
        $cust['cusnum'] = "";
        $cust['vatnum'] = "";
        $cust['accno'] = "";
    } else {
        $cust = pg_fetch_array($custRslt);
        $sql = "SELECT cusnum, cusname, surname FROM customers WHERE deptid = '{$inv['deptid']}' AND location != 'int' AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $cusRslt = db_exec($sql) or errDie("Unable to view customers");
        # Moarn if customer account has been blocked   AND lower(surname) LIKE lower('$letters%')
        if ($cust['blocked'] == 'yes') {
            $error .= "<li class=err>Error : Selected customer account has been blocked.";
        }
        // $customers = "<input type=hidden name=cusnum value='$cust[cusnum]'>$cust[cusname]  $cust[surname]";
        $cusnum = $cust['cusnum'];
        $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
        $customers .= "<option value='0' selected>Select Customer</option>";
        // $customers .= "<option value='-S' selected>Select Customer</option>";
        while ($cus = pg_fetch_array($cusRslt)) {
            $sel = "";
            if ($cust['cusnum'] == $cus['cusnum']) {
                $sel = "selected";
            }
            $customers .= "<option value='{$cus['cusnum']}' {$sel}>{$cus['cusname']} {$cus['surname']}</option>";
        }
        $customers .= "</select>";
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class=err> There are no Stores found in Cubit.";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class=err> There are no Sales People found in Cubit.";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $inv['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # Keep the charge vat option stable
    if ($inv['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($inv['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    # format date
    list($oyear, $omon, $oday) = explode("-", $inv['odate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>STORE</th>\n\t\t\t\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t\t\t\t<th>SERIAL NO.</th>\n\t\t\t\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t\t\t\t<th>QTY</th>\n\t\t\t\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t\t\t\t<th>Remove</th>\n\t\t\t\t\t\t<tr>";
    # get selected stock in this invoice
    db_connect();
    $sql = "SELECT * FROM pinv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            db_conn('core');
            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
            $Accounts = "\n\t\t\t\t\t\t\t<select name='accounts[]'>\n\t\t\t\t\t\t\t\t<option value='0'>Select Account</option>";
            while ($ad = pg_fetch_array($Ri)) {
                if ($ad['accid'] == $stkd['account']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
            }
            $Accounts .= "</select>";
            $sernos = "\n\t\t\t\t<input type='hidden' name='sernos_ss[]' value='{$stkd['ss']}'>\n\t\t\t\t<input type='hidden' name='sernos[]' value='{$stkd['serno']}'>";
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
            $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>";
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            //print "fo";
            # Put in product
            $products .= "\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whids[]' value='{$stkd['whid']}'></td>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$stkd['description']}'> {$sernos}</td>\n\t\t\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t\t\t<input type='hidden' name='disc[]' value='{$stkd['disc']}'>\n\t\t\t\t\t\t\t\t<input type='hidden' name='discp[]' value='{$stkd['discp']}'>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t\t\t\t</tr>";
            $key++;
        } else {
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            if ($stk['units'] <= $stk['minlvl'] && $stk['minlvl'] != 0) {
                $error .= "<li class=err>{$stk['stkcod']} is below minimum level, please notify stock controller.</li>";
            }
            # Serial number
            if ($stk['serd'] == 'yes' && ($inv['serd'] == 'n' || $stkd["serno"] == "")) {
                $sers = ext_getavserials($stkd['stkid']);
                $sernos = "<select class='width : 15' name='sernos[]'>";
                foreach ($sers as $skey => $ser) {
                    $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
                }
                $sernos .= "</select>\n\t\t\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value='*_*_*CUBIT_SERIAL_SELECT_BOX*_*_*' />";
            } else {
                $sernos = "\n\t\t\t\t\t<input type='hidden' name='sernos_ss[{$key}]' value='{$stkd['ss']}' />\n\t\t\t\t\t<input type='hidden' name='sernos[]' value='{$stkd['serno']}'>{$stkd['ss']}";
            }
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
            if ($stk['serd'] == 'yes') {
                $qtyin = "<input type='hidden' size='3' name='qtys[]' value='{$stkd['qty']}'>{$stkd['qty']}";
            }
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            if ($ped) {
                $editp = "<input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>";
            } else {
                $editp = "<input type='hidden' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>{$stkd['unitcost']}";
            }
            # put in product
            $products .= "\n\t\t\t\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t\t<td>{$sernos}</td>\n\t\t\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t\t\t<td>{$editp}</td>\n\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='disc[]' value='{$stkd['disc']}'>\n\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='discp[]' value='{$stkd['discp']}' maxlength=5>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t\t\t\t</tr>";
            $key++;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # Look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    } else {
        $SCROLL = "yes";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S") {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                if ($stk['serd'] == 'yes') {
                    $sers = ext_getavserials($stkidss[$key]);
                    $sernos = "<select class='width : 15'name='sernos[]' onChange='javascript:document.form.submit();'>";
                    foreach ($sers as $skey => $ser) {
                        $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
                    }
                    $sernos .= "</select>\n\t\t\t\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value='*_*_*CUBIT_SERIAL_SELECT_BOX*_*_*' />";
                } else {
                    $sernos = "\n\t\t\t\t\t\t\t<input type='hidden' name='sernos[]' value=''>\n\t\t\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value=''>";
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class='err'>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                # Input qty if not serialised
                $qtyin = "<input type=text size=3 name=qtys[] value='{$qtyss[$key]}'>";
                if ($stk['serd'] == 'yes') {
                    $qtyin = "<input type=hidden size=3 name=qtys[] value='{$qtyss[$key]}'>{$qtyss[$key]}";
                }
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $amt[$key] = sprint($amt[$key]);
                $stk['selamt'] = sprint($stk['selamt']);
                # put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stk['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t\t\t<td>{$sernos}</td>\n\t\t\t\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' size='8' name='unitcost[]'  value='{$stk['selamt']}'>{$stk['selamt']}</td>\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='disc[]' value='{$discs[$key]}'>\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='discp[]' value='{$discps[$key]}' maxlength='5'>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t\t\t\t</tr>";
                $keyy++;
            } elseif (isset($accountss[$key]) && $accountss[$key] != "0") {
                db_conn('core');
                $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                if (pg_num_rows($Ri) < 1) {
                    return "invalid.";
                }
                $ad = pg_fetch_array($Ri);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                # Input qty if not serialised
                //$qtyin = "<input type=text size=3 name=qtemp value='$qtyss[$key]'>";
                $qtyin = "<input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'>";
                # Check permissions
                $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$unitcosts[$key]}'>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vatcodess[$key] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $viewcost = sprint($viewcost);
                $amt[$key] = sprint($amt[$key]);
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td colspan='2'>{$ad['accname']}<input type='hidden' name='accounts[]' value='{$accountss[$key]}'><input type='hidden' name='whids[]' value='0'></td>\n\t\t\t\t\t\t\t\t\t<td>{$Vatcodes}<input type='hidden' name='stkids[]' value='0'></td>\n\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$descriptionss[$key]}'></td>\n\t\t\t\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t\t\t\t<input type='hidden' name='disc[]' value='0'><input type='hidden' name='discp[]' value='0'>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t\t\t\t</tr>";
                $keyy++;
            } else {
                if (!isset($diffwhBtn)) {
                    # skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    if (!isset($addnon)) {
                        # get warehouse name
                        db_conn("exten");
                        $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                        $whRslt = db_exec($sql);
                        $wh = pg_fetch_array($whRslt);
                        # get stock on this warehouse
                        db_connect();
                        $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' {$fil} ORDER BY stkcod ASC";
                        $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                        if (pg_numrows($stkRslt) < 1) {
                            $error .= "<li class=err>There are no stock items in the selected warehouse.";
                            continue;
                        }
                        $stks = "<select class='width : 15'name='stkidss[]' onChange='javascript:document.form.submit();'>";
                        $stks .= "<option value='-S' disabled selected>Select Number</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $stks .= "</select> ";
                        # put in drop down and warehouse
                        $products .= "\n\t\t\t\t\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]'  value='1'></td>\n\t\t\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='discs[]' value='0'>\n\t\t\t\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='discps[]' value='0' maxlength='5'>\n\t\t\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='amts[]' value='0.00'>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t\t\t</tr>";
                    } else {
                        db_conn('core');
                        $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                        $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                        $Accounts = "\n\t\t\t\t\t\t\t\t\t\t<select name='accountss[]' onChange='javascript:document.form.submit();'>\n\t\t\t\t\t\t\t\t\t\t\t<option value='0'>Select Account</option>";
                        while ($ad = pg_fetch_array($Ri)) {
                            $Accounts .= "<option value={$ad['accid']}>{$ad['accname']}</option>";
                        }
                        $Accounts .= "</select>";
                        db_conn('cubit');
                        $Sl = "SELECT * FROM vatcodes ORDER BY code";
                        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                        $Vatcodes = "<select name=vatcodess[]>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                        while ($vd = pg_fetch_array($Ri)) {
                            if ($vd['del'] == "Yes") {
                                $sel = "selected";
                            } else {
                                $sel = "";
                            }
                            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                        }
                        $Vatcodes .= "</select>";
                        $products .= "\n\t\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[]' value='1'></td>\n\t\t\t\t\t\t\t\t\t\t\t<inpu type='hidden' name='stkidss[]' value=''>\n\t\t\t\t\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t\t\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t\t\t\t\t<input type='hidden' name='discs[]' value='0'>\n\t\t\t\t\t\t\t\t\t\t\t<input type='hidden' name='discps[]' value='0' >\n\t\t\t\t\t\t\t\t\t\t</tr>";
                    }
                }
            }
        }
    } else {
        if (!isset($diffwhBtn)) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                if (isset($wtd) && $wtd != 0) {
                    $whid = $wtd;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected store.</li>";
                }
                $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                $stks .= "<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t\t\t\t\t<input type='hidden' name='vatcodess[]' value=''>\n\t\t\t\t\t \t\t\t\t<td>\n\t\t\t\t\t \t\t\t\t\t<input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t \t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t \t\t\t\t\t<td> </td>\n\t\t\t\t\t \t\t\t\t\t<td> </td>\n\t\t\t\t\t \t\t\t\t\t<td> </td>\n\t\t\t\t\t \t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t \t\t\t\t\t<td> </td>\n\t\t\t\t\t \t\t\t\t\t<input type='hidden' size='4' name='discs[]' value='0'>\n\t\t\t\t\t \t\t\t\t\t<input type='hidden' size='4' name='discps[]' value='0' maxlength='5'>\n\t\t\t\t\t \t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t \t\t\t\t\t<td></td>\n\t\t\t\t\t \t\t\t\t</tr>";
            } else {
                $products .= "\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='discs[]' value='0'>\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='discps[]' value='0' maxlength='5'>\n\t\t\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t</tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<input type='hidden' size='4' name='discs[]' value='0'>\n\t\t\t\t\t\t\t<input type='hidden' size='4' name='discps[]' value='0' maxlength='5'>\n\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t</tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    $SUBTOT = sprint($inv['subtot']);
    # Calculate tradediscm
    if ($inv['traddisc'] > 0) {
        $traddiscm = sprint($inv['traddisc'] / 100 * ($SUBTOT + $inv['delchrg']));
    } else {
        $traddiscm = "0.00";
    }
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    $VAT = sprint($inv['vat']);
    $TOTAL = sprint($inv['total']);
    $inv['delchrg'] = sprint($inv['delchrg']);
    if (!isset($done)) {
        $done = "";
    }
    /* --- End Some calculations --- */
    if ($inv['terms'] == 1) {
        $tc1 = "";
        $tc2 = "checked";
    } else {
        $tc1 = "checked";
        $tc2 = "";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM settings WHERE constant='PSALES'";
    $Ri = db_exec($Sl) or errDie("Unable to get settings.");
    $data = pg_fetch_array($Ri);
    if ($data['value'] == "Yes") {
        $sc = "checked";
    } else {
        $sc = "";
    }
    $sales = "<td>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<td>{$salesps}</td>\n\t\t\t\t<td>Print</td>\n\t\t\t\t<td><input type='checkbox' name='printsales' {$sc}></td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</td>";
    # Query server for depts
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE div = '" . USER_DIV . "' ORDER BY deptname ASC";
    $deptRslt = db_exec($sql) or errDie("Unable to view customers");
    if (pg_numrows($deptRslt) < 1) {
        return "<li class='err'>There are no Departments found in Cubit.</li>";
    } else {
        $depts = "<select name='deptid'>";
        while ($dept = pg_fetch_array($deptRslt)) {
            if ($dept['deptid'] == $inv['deptid']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $depts .= "<option value='{$dept['deptid']}' {$sel}>{$dept['deptname']}</option>";
        }
        $depts .= "</select>";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM users WHERE username='******'";
    $Ri = db_exec($Sl);
    $data = pg_fetch_array($Ri);
    if ($data['help'] != "S") {
        $save = "|<input type=submit name='saveBtn' value='Save'>";
    } else {
        $save = "";
    }
    if ($inv['rounding'] > 0) {
        $due = sprint($inv['total'] - $inv['rounding']);
        $rd = "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Rounding</td>\n\t\t\t\t\t<td align='right'>R {$inv['rounding']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<th>Amount Due</th>\n\t\t\t\t\t<td align='right'>R {$due}</td>\n\t\t\t\t</tr>";
    } else {
        $rd = "";
    }
    $inv['delvat'] += 0;
    if ($inv['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $inv['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "<input type=hidden name=delvat value=0>";
    // 	while($vd=pg_fetch_array($Ri)) {
    // 		if($vd['id']==$inv['delvat']) {
    // 			$sel="selected";
    // 		} else {
    // 			$sel="";
    // 		}
    // 		$Vatcodes.="<option value='$vd[id]' $sel>$vd[code]</option>";
    // 	}
    //
    // 	$Vatcodes.="</select>";
    if ($inv['cusnum'] == 0) {
        $cd = "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t<td valign='center'><input type='text' size='20' name='client' value='{$client}'></td>\n\t\t\t\t</tr>";
        $pc = "<input type='hidden' name='pcredit' value='0'>";
    } else {
        $cd = "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t<td valign='center'>" . nl2br($cust['addr1']) . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer VAT Number</td>\n\t\t\t\t\t<td>{$cust['vatnum']}</td>\n\t\t\t\t</tr>";
        $pc = "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Amount On Credit</td>\n\t\t\t\t\t<td><input size='12' type='text' name='pcredit' value='{$inv['pcredit']}'></td>\n\t\t\t\t</tr>";
    }
    $sales = USER_NAME;
    if ($inv['pcash'] == 0) {
        $inv['pcash'] = "";
    }
    if ($inv['pcheque'] == 0) {
        $inv['pcheque'] = "";
    }
    if ($inv['pcc'] == 0) {
        $inv['pcc'] = "";
    }
    /* -- Final Layout -- */
    $details = "\n\t\t\t\t\t<center>\n\t\t\t\t\t<h3>Speed POS</h3>\n\t\t\t\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t\t\t\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t\t\t\t\t<input type='hidden' name='user' value='" . USER_NAME . "'>\n\t\t\t\t\t\t<input type='hidden' name='salespn' value='" . USER_ID . "'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='95%'>\n \t\t\t\t\t\t<tr>\n \t\t\t\t\t\t\t<td valign='top'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$depts}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$customers}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t{$cd}\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='10' name='cordno' value='{$inv['cordno']}'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'>Point of Sale</th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Barcode</td>\n\t\t\t\t\t\t\t\t\t\t<td><input type='text' size='13' name='bar' value=''></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'>Search</th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>By Stock Code</td>\n\t\t\t\t\t\t\t\t\t\t<td><input type='text' size='13' name='fcode' value='{$fcode}'><input type='submit' name='upBtn' value='Search'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>By Stock Description</td>\n\t\t\t\t\t\t\t\t\t\t<td><input type='text' size='13' name='fdes' value='{$fdes}'><input type='submit' name='upBtn' value='Search'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'> Invoice Details </th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Invoice No.</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$inv['invid']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='5' name='ordno' value='{$inv['ordno']}'>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Invoice Date</td>\n\t\t\t\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t\t\t\t<input type='hidden' size='2' name='oday' maxlength='2' value='{$oday}'>{$oday}-\n\t\t\t\t\t\t\t\t\t\t\t<input type='hidden' size='2' name='omon' maxlength='2' value='{$omon}'>{$omon}-\n\t\t\t\t\t\t\t\t\t\t\t<input type='hidden' size='4' name='oyear' maxlength='4' value='{$oyear}'>{$oyear}\n\t\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='7' name='chrgvat' value='inc'>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t\t\t\t\t<td>{$sales}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='5' name='traddisc' value='{$inv['traddisc']}'>\n\t\t\t\t\t\t\t\t\t<input type='hidden' size='7' name='delchrg' value='{$inv['delchrg']}'>{$Vatcodes}\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'>Payment Details </th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Amount Paid Cash</td>\n\t\t\t\t\t\t\t\t\t\t<td><input size='12' type='text' name='pcash' value='{$inv['pcash']}'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Amount Paid Cheque</td>\n\t\t\t\t\t\t\t\t\t\t<td><input size='12' type='text' name='pcheque' value='{$inv['pcheque']}'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Amount Paid Credit Card</td>\n\t\t\t\t\t\t\t\t\t\t<td><input size='12' type='text' name='pcc' value='{$inv['pcc']}'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t{$pc}\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>POS Manager Password</td>\n\t\t\t\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t\t\t\t<input type='password' size='10' name='pass' value='{$pass}'>\n\t\t\t\t\t\t\t\t\t\t\t<input type='submit' value='Continue'>\n\t\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr><td colspan=2>{$done}</td></tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='70%' valign='top'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t\t\t\t<input type='hidden' name='comm' value='{$inv['comm']}'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td>{$error}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td align='right' valign='top' width='30%'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='SUBTOT' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td><b>VAT @ {$VATP}%</b>\n\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t{$rd}\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td align='right'></td>\n\t\t\t\t\t\t\t<td><input type='submit' name='upBtn' value='Update'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t<a name='bottom'>\n\t\t\t\t\t</form>\n\t\t\t\t\t</center>";
    return $details;
}
コード例 #12
0
function details($_POST, $error = "")
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($purid)) {
        $v->isOk($purid, "num", 1, 20, "Invalid Non-Stock Order number.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class=err>" . $e["msg"];
        }
        $confirm .= "{$error}<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($purid)) {
        $purid = create_dummy(0);
    }
    # Get Order info
    db_connect();
    $sql = "SELECT * FROM nons_purchases WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class=err>purchase Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    # check if Order has been printed
    if ($pur['received'] == "y") {
        $error = "<li class=err> Error : Order number <b>{$purid}</b> has already been received.";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    if (!isset($ordernum)) {
        $ordernum = '';
    }
    /* --- Start Drop Downs --- */
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    # format date
    list($pyear, $pmon, $pday) = explode("-", $pur['pdate']);
    # keep the charge vat option stable
    if ($pur['vatinc'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chnv = "";
    } else {
        if ($pur['vatinc'] == 'novat') {
            $chy = "";
            $chn = "";
            $chnv = "checked=yes";
        } else {
            $chy = "";
            $chn = "checked=yes";
            $chnv = "";
        }
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=100%>\r\n\t<tr><th>ITEM NUMBER</th><th>DESCRIPTION</th><th>QTY</th><th>UNIT PRICE</th><th>DELIVERY DATE</th><th>AMOUNT</th><th>VAT</th><th>Remove</th><tr>";
    # get selected stock in this Order
    db_connect();
    $sql = "SELECT * FROM nons_pur_items  WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $i++;
        list($syear, $smon, $sday) = explode("-", $stkd['ddate']);
        $stkd['amt'] = round($stkd['amt'], 2);
        $tip = "&nbsp;&nbsp;&nbsp;";
        if (isset($vatc[$key])) {
            $tip = "<font color=red>#</font>";
            $error = "<div class=err> {$tip}&nbsp;&nbsp;=&nbsp;&nbsp; Vat amount is different from amount calculated by cubit. To allow cubit to recalculate the vat amount, please delete the vat amount from the input box.";
        }
        # put in product
        $products .= "<tr class='bg-odd'><td align=center><input type=text size=10 name=cod[] value='{$stkd['cod']}'></td><td align=center><input type=text size=20 name=des[] value='{$stkd['des']}'></td><td align=center><input type=text size=3 name=qtys[] value='{$stkd['qty']}'></td><td align=center><input type=text size=8 name=unitcost[] value='{$stkd['unitcost']}'></td><td align=center><input type=text size=2 name=dday[] maxlength=2 value='{$sday}'>-<input type=text size=2 name=dmon[] maxlength=2 value='{$smon}'>-<input type=text size=4 name=dyear[] maxlength=4 value='{$syear}'></td><td><input type=hidden name=amt[] value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td><td>{$tip} <input type=text name=vat[] size=9 value='{$stkd['svat']}'></td><td><input type=checkbox name=remprod[] value='{$key}'><input type=hidden name=SCROLL value=yes></td></tr>";
        $key++;
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
        list($year, $mon, $day) = explode("-", date("Y-m-d"));
        # add one
        $products .= "<tr class='bg-odd'><td align=center><input type=text size=10 name=cod[] value=''></td><td align=center><input type=text size=20 name=des[] value=''></td><td align=center><input type=text size=3 name=qtys[] value='1'></td><td align=center><input type=text size=8 name=unitcost[]></td><td align=center><input type=text size=2 name=dday[] maxlength=2 value='{$day}'>-<input type=text size=2 name=dmon[] maxlength=2 value='{$mon}'>-<input type=text size=4 name=dyear[] maxlength=4 value='{$year}'></td><td>" . CUR . " 0.00</td><td><input type=hidden name=novat[] value='1'></td><td> </td></tr>";
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        list($year, $mon, $day) = explode("-", date("Y-m-d"));
        $products .= "<tr class='bg-odd'><td align=center><input type=text size=10 name=cod[] value=''></td><td align=center><input type=text size=20 name=des[] value=''></td><td align=center><input type=text size=3 name=qtys[] value='1'></td><td align=center><input type=text size=8 name=unitcost[]></td><td align=center><input type=text size=2 name=dday[] maxlength=2 value='{$day}'>-<input type=text size=2 name=dmon[] maxlength=2 value='{$mon}'>-<input type=text size=4 name=dyear[] maxlength=4 value='{$year}'></td><td>" . CUR . " 0.00</td><td><input type=hidden name=novat[{$key}] value='1'></td><td> </td></tr>";
        $key++;
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $pur['subtot'];
    # Get Total
    $TOTAL = sprint($pur['total']);
    # Get vat
    $VAT = sprint($pur['vat']);
    /* --- End Some calculations --- */
    /* -- Final Layout -- */
    $details = "<center><h3>New Non-Stock Order</h3>\r\n\t<form action='" . SELF . "' method=post name=form>\r\n\t<input type=hidden name=key value=update>\r\n\t<input type=hidden name=purid value='{$purid}'>\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=95%>\r\n \t<tr><td valign=top>\r\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\r\n\t\t\t<tr><th colspan=2> Supplier Details </th></tr>\r\n\t\t\t<tr class='bg-odd'><td>Supplier</td><td valign=center><input type=text name=supplier value='{$pur['supplier']}'></td></tr>\r\n\t\t\t<tr class='bg-even'><td valign=top>Supplier Address</td><td valign=center><textarea name=supaddr cols=18 rows=3>{$pur['supaddr']}</textarea></td></tr>\r\n\t\t</table>\r\n\t</td><td valign=top align=right>\r\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\r\n\t\t\t<tr><th colspan=2> Non-Stock Order Details </th></tr>\r\n\t\t\t<tr class='bg-odd'><td>Non-Stock Order No.</td><td valign=center>{$pur['purnum']}</td></tr>\r\n\t\t\t<tr class='bg-even'><td>Order No.</td><td valign=center><input type=text size=10 name=ordernum value='{$ordernum}'></td></tr>\r\n\t\t\t<tr class='bg-odd'><td>Terms</td><td valign=center>{$termssel} Days</td></tr>\r\n\t\t\t<tr class='bg-even'><td>Date</td><td valign=center><input type=text size=2 name=pday maxlength=2 value='{$pday}'>-<input type=text size=2 name=pmon maxlength=2 value='{$pmon}'>-<input type=text size=4 name=pyear maxlength=4 value='{$pyear}'> DD-MM-YYYY</td></tr>\r\n\t\t\t<tr class='bg-odd'><td>VAT Inclusive</td><td valign=center>Yes <input type=radio size=7 name=vatinc value='yes' {$chy}> No<input type=radio size=7 name=vatinc value='no' {$chn}> No VAT<input type=radio size=7 name=vatinc value='novat' {$chnv}></td></tr>\r\n\t\t\t<tr class='bg-even'><td>Delivery Charges</td><td valign=center><input type=text size=7 name=shipchrg value='{$pur['shipchrg']}'></td></tr>\r\n\t\t</table>\r\n\t</td></tr>\r\n\t<tr><td><br></td></tr>\r\n\t<tr><td colspan=2>{$products}</td></tr>\r\n\t<tr><td>\r\n\t\t<p>\r\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t\t\t<tr><th width=25%>Quick Links</th><th width=25%>Remarks</th><td rowspan=5 valign=top width=50%>{$error}</td></tr>\r\n\t\t\t<tr><td class='bg-odd'><a href='nons-purchase-view.php'>View Non-Stock Orders</a></td><td class='bg-odd' rowspan=4 align=center valign=top><textarea name=remarks rows=4 cols=20>{$pur['remarks']}</textarea></td></tr>\r\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\r\n\t\t\t<tr class='bg-odd'><td><a href='main.php'>Main Menu</a></td></tr>\r\n\t\t</table>\r\n\t</td><td align=right>\r\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=80%>\r\n\t\t\t<tr class='bg-odd'><td>SUBTOTAL</td><td align=right>" . CUR . " <input type=hidden name=subtot value='{$SUBTOT}'>{$SUBTOT}</td></tr>\r\n\t\t\t<tr class='bg-odd'><td>Delivery Charges</td><td align=right>" . CUR . " {$pur['shipping']}</td></tr>\r\n\t\t\t<tr class='bg-odd'><td>VAT @ " . TAX_VAT . " %</td><td align=right>" . CUR . " {$pur['vat']}</td></tr>\r\n\t\t\t<tr class='bg-even'><th>GRAND TOTAL</th><td align=right>" . CUR . " <input type=hidden name=total value='{$TOTAL}'>{$TOTAL}</td></tr>\r\n\t\t</table>\r\n\t</td></tr>\r\n\t<tr><td align=right><input name=diffwhBtn type=submit value='Add Item'> |</td><td><input type=submit name='upBtn' value='Update'>{$done}</td></tr>\r\n\t</table></form>\r\n\t</center>";
    return $details;
}
コード例 #13
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    if (!isset($button) && isset($starting)) {
        return slct();
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid Non-Stock Invoice number.");
    } elseif (isset($ctyp)) {
        $val = $ctyp . "val";
        if (isset(${$val})) {
            $tval = ${$val};
            $v->isOk($tval, "num", 1, 20, "Invalid Selection.");
        }
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return slct($error);
        $confirm = "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($invid) && isset($ctyp)) {
        $val = $ctyp . "val";
        if (!isset(${$val})) {
            ${$val} = "";
        }
        $tval = ${$val};
        if (isset($bankid)) {
            $bankid += 0;
            $acc = $bankid;
        } else {
            $acc = 0;
        }
        // Retrieve default comments
        db_conn("cubit");
        $sql = "SELECT value FROM settings WHERE constant='DEFAULT_COMMENTS'";
        $commRslt = db_exec($sql) or errDie("Unable to retrieve default comments from Cubit.");
        $comment = base64_decode(pg_fetch_result($commRslt, 0));
        $invid = create_dummy(0, $ctyp, $tval, $acc, $comment);
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM rnons_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    /* --- Start Drop Downs --- */
    # format date
    list($rinv_year, $rinv_month, $rinv_day) = explode("-", $inv['sdate']);
    # keep the charge vat option stable
    if ($inv['chrgvat'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chnone = "";
    } elseif ($inv['chrgvat'] == "no") {
        $chy = "";
        $chn = "checked=yes";
        $chnone = "";
    } else {
        $chy = "";
        $chn = "";
        $chnone = "checked=yes";
    }
    # Days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $inv['terms']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>Account</th>\n\t\t\t\t<th>VAT Code</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM rnons_inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $stkd['amt'] = round($stkd['amt'], 2);
        $chk = "";
        if ($stkd['vatex'] == 'y') {
            $chk = "checked=yes";
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl);
        $vats = "<select name='vatcodes[]'>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatex'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $vats .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $vats .= "</select>";
        db_conn('core');
        $Sl = "SELECT accid,accname FROM accounts WHERE div='" . USER_DIV . "' ORDER BY accname";
        $Ri = db_exec($Sl);
        $accounts = "<select name='accounts[]'>";
        while ($ad = pg_fetch_array($Ri)) {
            if (isb($ad['accid'])) {
                continue;
            }
            if ($ad['accid'] == $stkd['account']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
        }
        $accounts .= "</select>";
        db_conn('cubit');
        # put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='50' name='des[]' value='{$stkd['description']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " " . sprint($stkd["amt"]) . "</td>\n\t\t\t\t<td>{$accounts}</td>\n\t\t\t\t<td align='center'>{$vats}</td>\n\t\t\t\t<td align='center'><input type='checkbox' name='remprod[]' value='{$i}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t</tr>";
        $i++;
    }
    # Look above(remprod keys)
    $keyy = $i;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    if ($i == 0 || isset($diffwhBtn)) {
        db_conn('core');
        $Sl = "SELECT accid,accname FROM accounts WHERE div='" . USER_DIV . "' ORDER BY accname";
        $Ri = db_exec($Sl);
        $accounts = "<select name='accounts[]'>";
        while ($ad = pg_fetch_array($Ri)) {
            if (isb($ad['accid'])) {
                continue;
            }
            $accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
        }
        $accounts .= "</select>";
        # add one
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='50' name='des[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='1'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[]'></td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td>{$accounts}</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t</tr>";
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $inv['subtot'];
    # Get Total
    $TOTAL = sprint($inv['total']);
    # Get vat
    $VAT = sprint($inv['vat']);
    /* --- End Some calculations --- */
    db_conn('cubit');
    if ($inv['ctyp'] == 's') {
        $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['tval']}' AND div = '" . USER_DIV . "'";
        $custRslt = db_exec($sql) or errDie("Unable to view customer");
        $cust = pg_fetch_array($custRslt);
        $details = "\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t</tr>\n\t\t\t<input type='hidden' name='cusname' value='{$cust['surname']}'>\n\t\t\t<input type='hidden' name='cusaddr' value='{$cust['addr1']}'>\n\t\t\t<input type='hidden' name='cusvatno' value='{$cust['vatnum']}'>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer</td>\n\t\t\t\t<td valign='center'>{$cust['surname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer Address</td>\n\t\t\t\t<td valign='center'><pre>{$cust['addr1']}</pre></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer Vat Number</td>\n\t\t\t\t<td valign='center'>{$cust['vatnum']}</td>\n\t\t\t</tr>";
    } elseif ($inv['ctyp'] == 'c') {
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE deptid = '{$inv['tval']}'";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        $dept = pg_fetch_array($deptRslt);
        $details = "\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer</td>\n\t\t\t\t<td valign='center'><input type='text' name='cusname' value='{$inv['cusname']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t<td valign='center'><textarea name='cusaddr' cols='18' rows='3'>{$inv['cusaddr']}</textarea></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Customer VAT No.</td>\n\t\t\t\t<td valign='center'><input type='text' name='cusvatno' value='{$inv['cusvatno']}'></td>\n\t\t\t</tr>";
    } else {
        db_conn('core');
        $Sl = "SELECT accid,accname FROM accounts WHERE div='" . USER_DIV . "' ORDER BY accname";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $accountss = "<select name=account>";
        while ($ad = pg_fetch_array($Ri)) {
            if ($ad['accid'] == $inv['tval']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $accountss .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
        }
        $accountss .= "</select>";
        $details = "\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer</td>\n\t\t\t\t<td valign='center'><input type='text' name='cusname' value='{$inv['cusname']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t<td valign='center'><textarea name='cusaddr' cols='18' rows='3'>{$inv['cusaddr']}</textarea></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Customer VAT No.</td>\n\t\t\t\t<td valign='center'><input type='text' name='cusvatno' value='{$inv['cusvatno']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Ledger Account</td>\n\t\t\t\t<td>{$accountss}</td>\n\t\t\t</tr>";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM costcenters";
    $Ri = db_exec($Sl);
    if (pg_num_rows($Ri) > 0) {
        $ctd = "\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th>Cost Center</th>\n\t\t\t\t\t<th>Percentage</th>\n\t\t\t\t</tr>";
        $i = 0;
        while ($data = pg_fetch_array($Ri)) {
            $Sl = "SELECT * FROM ninvc WHERE inv='{$invid}' AND cid='{$data['ccid']}'";
            $Rq = db_exec($Sl);
            $cd = pg_fetch_array($Rq);
            $ctd .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$data['centername']}</td>\n\t\t\t\t\t<td><input type='text' name='ct[{$data['ccid']}]' size='5' value='{$cd['amount']}'>%</td>\n\t\t\t\t</tr>";
            $i++;
        }
        $ctd .= "</table>";
    } else {
        $ctd = "";
    }
    if (empty($inv["remarks"])) {
        // Retrieve default comments
        db_conn("cubit");
        $sql = "SELECT value FROM settings WHERE constant='DEFAULT_COMMENTS'";
        $commRslt = db_exec($sql) or errDie("Unable to retrieve default comments from Cubit.");
        $comment = base64_decode(pg_fetch_result($commRslt, 0));
    } else {
        $comment = $inv["remarks"];
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    if (isset($diffwhBtn) or isset($upBtn) or isset($doneBtn)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details = "\n\t\t<center>\n\t\t<h3>New Recurring Non-Stock Invoices</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t{$details}\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer Order number</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$inv['cordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Non-Stock Invoice Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Recurring Non-Stock Invoice No.</td>\n\t\t\t\t\t\t\t<td valign='center'>RI {$inv['invid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Proforma Invoice No.</td>\n\t\t\t\t\t\t\t<td><input type='text' name='docref' value='{$inv['docref']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("rinv", $rinv_year, $rinv_month, $rinv_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='yes' {$chy}> No<input type='radio' size='7' name='chrgvat' value='no' {$chn}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='2'>{$ctd}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t" . TBL_BR . "\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='rec-nons-invoice-view.php'>View Recurring Non-Stock Invoices</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$comment}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='subtot' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$inv['vat']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input name='diffwhBtn' type='submit' value='Add Item'> |</td>\n\t\t\t\t<td><input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}
コード例 #14
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($purid)) {
        $v->isOk($purid, "num", 1, 20, "Invalid Order number.");
    } else {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($purid)) {
        $purid = create_dummy($deptid);
    }
    # Get purchase info
    db_connect();
    $sql = "SELECT * FROM purchases WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get purchase information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>Order Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    $deptid = $pur['deptid'];
    # check if purchase has been printed
    if ($pur['received'] == "y") {
        $error = "<li class='err'> Error : Order no. {$pur['purnum']} has already been received.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    if (!isset($ordernum)) {
        $ordernum = '';
    }
    if (!isset($supinv)) {
        $supinv = '';
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$pur['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    $supname = $pur['supname'];
    $supaddr = $pur['supaddr'];
    $supno = $pur['supno'];
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    # format date
    list($p_year, $p_month, $p_day) = explode("-", $pur['pdate']);
    # keep the charge vat option stable
    if ($pur['vatinc'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chv = "";
    } elseif ($pur['vatinc'] == "no") {
        $chy = "";
        $chn = "checked=yes";
        $chv = "";
    } else {
        $chy = "";
        $chn = "";
        $chv = "checked=yes";
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>STORE</th>\n\t\t\t\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t\t\t\t<th>QTY</th>\n\t\t\t\t\t\t\t<th>PRICE PER UNIT</th>\n\t\t\t\t\t\t\t<th>DELIVERY DATE</th>\n\t\t\t\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t\t\t\t<th>VAT</th>\n\t\t\t\t\t\t\t<th>Remove</th>\n\t\t\t\t\t\t<tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM pur_items  WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $i++;
        # get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        list($d_year[$i], $d_month[$i], $d_day[$i]) = explode("-", $stkd['ddate']);
        $stkd['amt'] = sprint($stkd['amt']);
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'";
        $Ri = db_exec($Sl);
        if (pg_num_rows($Ri) < 1) {
            return "Please select the vatcode for all your stock.";
        }
        $vd = pg_fetch_array($Ri);
        if ($pur['vatinc'] == 'no' && $stk['exvat'] != 'yes') {
            $vunitamt = sprint($stkd['unitcost']);
        } else {
            $vunitamt = sprint($stkd['unitcost']);
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
        $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatcode'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $Vatcodes .= "</select>";
        $tip = "&nbsp;&nbsp;&nbsp;";
        if (isset($vatc[$key])) {
            $tip = "<font color=red>#</font>";
            $error = "<div class=err> {$tip}&nbsp;&nbsp;=&nbsp;&nbsp; Vat amount is different from amount calculated by cubit. To allow cubit to recalculate the vat amount, please delete the vat amount from the input box.";
        }
        # put in product
        $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t\t\t\t<td>" . CUR . " <input type='text' size='8' name='unitcost[]' value='{$vunitamt}'></td>\n\t\t\t\t\t\t\t<td>" . mkDateSelecta("d", $key, $d_year[$i], $d_month[$i], $d_day[$i]) . "</td>\n\t\t\t\t\t\t\t<td align='right'><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t\t\t<td>{$tip} <input type='text' name='svat[]' size='9' value='{$stkd['svat']}'></td>\n\t\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t\t\t</tr>";
        $key++;
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    $l = $i++;
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S") {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # Get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # Get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * 0);
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "<select name=vatcodes[]>\n\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stk['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'></td>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='novat[{$keyy}]' value='1'>" . CUR . " <input type='text' size='8' name='unitcost[]' value='0'></td>\n\t\t\t\t\t\t\t\t\t<td>" . mkDateSelecta("d", $key, $d_year[$l], $d_month[$l], $d_day[$l]) . "</td>\n\t\t\t\t\t\t\t\t\t<td align='right'><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t\t\t\t\t</tr>";
                $key++;
            } else {
                if (!isset($diffwhBtn)) {
                    # Skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    # Get warehouse name
                    db_conn("exten");
                    $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                    $whRslt = db_exec($sql);
                    $wh = pg_fetch_array($whRslt);
                    # Get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        $error .= "<li class='err'>There are no stock items in the selected warehouse.</li>";
                        continue;
                    }
                    $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                    $stks .= "<option value='-S' disabled selected>Select Item Number</option>";
                    $count = 0;
                    while ($stk = pg_fetch_array($stkRslt)) {
                        $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                    }
                    $stks .= "</select> ";
                    # Put in drop down and warehouse
                    $products .= "\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]'  value='1'></td>\n\t\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t\t<td>" . mkDateSelecta("d", $key, $d_year[$l], $d_month[$l], $d_day[$l]) . "</td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'><input type='hidden' name='amts[]' value='0.00'>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t</tr>";
                }
            }
            $l++;
        }
    } else {
        if (!isset($diffwhBtn)) {
            # take todays date
            list($date_year, $date_month, $date_day) = explode("-", $pur['pdate']);
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                if (isset($wtd)) {
                    $whid = $wtd;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected warehouse.</li>";
                }
                $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                $stks .= "<option value='-S' disabled selected>Select Item Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td><td> </td>\n\t\t\t\t\t\t\t\t\t<td>" . mkDateSelecta("d", "", $date_year, $date_month, $date_day) . "</td>\n\t\t\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t</tr>";
            } else {
                $products .= "\n\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t\t\t<td>" . mkDateSelecta("d", "", $date_year, $date_month, $date_day) . "</td>\n\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t</tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        # take todays date
        list($date_year, $date_month, $date_day) = explode("-", $pur['pdate']);
        $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td>" . mkDateSelecta("d", "", $date_year, $date_month, $date_day) . "</td>\n\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t</tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = sprint($pur['subtot']);
    # Get Total
    $TOTAL = sprint($pur['total']);
    # Get vat
    $VAT = sprint($pur['vat']);
    # Shipping Charges
    $pur['shipchrg'] = sprint($pur['shipchrg']);
    $pur['delvat'] += 0;
    if ($pur['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $pur['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "<select name='delvat'>\n\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $pur['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    /* --- End Some calculations --- */
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    /* -- Final Layout -- */
    $details = "\n\t\t\t\t\t<center>\n\t\t\t\t\t<h3>New Order</h3>\n\t\t\t\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t\t\t\t<input type='hidden' name='purid' value='{$purid}'>\n\t\t\t\t\t\t<input type='hidden' name='deptid' value='{$deptid}'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t\t\t \t<tr>\n\t\t\t\t\t \t\t<td valign='top'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td></tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='20' name='supname' value='{$supname}'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Supplier No</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='20' name='supno' value='{$supno}'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'><textarea name='supaddr' rows=4 cols='18'>{$supaddr}</textarea></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th colspan='2'> Order Details </th>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Purchase No.</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Order No.</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='ordernum' value='{$pur['ordernum']}'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Supplier Inv</td>\n\t\t\t\t\t\t\t\t\t\t<td><input type='text' name='supinv' size='10' value='{$pur['supinv']}'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("p", $p_year, $p_month, $p_day) . "</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='vatinc' value='yes' {$chy}> No<input type='radio' size='7' name='vatinc' value='no' {$chn}> No Vat<input type='radio' size='7' name='vatinc' value='novat' {$chv}></td>\n\t\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Delivery Charges</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='7' name='shipchrg' value='{$pur['shipchrg']}'></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Delivery Charges VAT Code</td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$Vatcodes}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<p>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='purchase-view.php'>View Orders</a></td>\n\t\t\t\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$pur['remarks']}</textarea></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td align='right'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type=hidden name=subtot value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>Delivery Charges</td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$pur['shipping']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td align='right'><input type='button' value='&laquo Back' onClick='javascript:history.back()'> | <input name=diffwhBtn type=submit value='Different Store'> |</td><td><input type=submit name='upBtn' value='Update'>{$done}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t</form>\n\t\t\t\t\t</center>";
    return $details;
}
コード例 #15
0
function details($_POST, $error = "")
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($purid)) {
        $v->isOk($purid, "num", 1, 20, "Invalid Non-Stock Order number.");
    } else {
        $v->isOk($ctyp, "string", 0, 20, "Invalid purchase type.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($purid)) {
        $purid = create_dummy(0);
    }
    # Get Order info
    db_connect();
    $sql = "SELECT * FROM nons_purchases WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>Purchase Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    # check if Order has been printed
    if ($pur['received'] == "y") {
        $error = "<li class='err'> Error : Order number <b>{$purid}</b> has already been received.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    if (!isset($ordernum)) {
        $ordernum = '';
    }
    /* --- Start Drop Downs --- */
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    # format date
    list($npur_year, $npur_month, $npur_day) = explode("-", $pur['pdate']);
    # keep the charge vat option stable
    if ($pur['vatinc'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chnv = "";
    } else {
        if ($pur['vatinc'] == 'novat') {
            $chy = "";
            $chn = "";
            $chnv = "checked=yes";
        } else {
            $chy = "";
            $chn = "checked=yes";
            $chnv = "";
        }
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>DELIVERY DATE</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>VAT</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this Order
    db_connect();
    $sql = "SELECT * FROM nons_pur_items  WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $i++;
        list($syear, $smon, $sday) = explode("-", $stkd['ddate']);
        $stkd['amt'] = round($stkd['amt'], 2);
        $tip = "&nbsp;&nbsp;&nbsp;";
        if (isset($vatc[$key])) {
            $tip = "<font color='red'>#</font>";
            $error = "<div class='err'> {$tip}&nbsp;&nbsp;=&nbsp;&nbsp; Vat amount is different from amount calculated by cubit. To allow cubit to recalculate the vat amount, please delete the vat amount from the input box.";
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
        $Vatcodes = "\n\t\t\t<select name='vatcodes[]'>\n\t\t\t\t<option value='0'>Select</option>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatcode'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $Vatcodes .= "</select>";
        $stkd['amt'] = sprint($stkd['amt']);
        # put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='10' name='cod[]' value='{$stkd['cod']}'></td>\n\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t<td align='center'><input type='text' size='20' name='des[]' value='{$stkd['des']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t<td align='center'>" . mkDateSelecta("d", array($i), $syear, $smon, $sday) . "</td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t<td>{$tip} <input type='text' name='vat[]' size='9' value='{$stkd['svat']}'></td>\n\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t</tr>";
        $key++;
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
        $Vatcodes = "\n\t\t\t<select name='vatcodes[]'>\n\t\t\t\t<option value='0'>Select</option>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($vd['del'] == "Yes") {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $Vatcodes .= "</select>";
        $trans_date_setting = getCSetting("USE_TRANSACTION_DATE");
        if (isset($trans_date_setting) and $trans_date_setting == "yes") {
            $trans_date_value = getCSetting("TRANSACTION_DATE");
            $date_arr = explode("-", $trans_date_value);
            $item_year = $date_arr[0];
            $item_month = $date_arr[1];
            $item_day = $date_arr[2];
        } else {
            $item_year = date("Y");
            $item_month = date("m");
            $item_day = date("d");
        }
        # add one
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='10' name='cod[]' value=''></td>\n\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t<td align='center'><input type='text' size='20' name='des[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='1'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[]'></td>\n\t\t\t\t<td align='center'>" . mkDateSelecta("d", array($i), $item_year, $item_month, $item_day) . "</td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td><input type='hidden' name='novat[]' value='1'></td>\n\t\t\t\t<td> </td>\n\t\t\t</tr>";
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
        $Vatcodes = "\n\t\t\t<select name='vatcodes[]'>\n\t\t\t\t<option value='0'>Select</option>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($vd['del'] == "Yes") {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $Vatcodes .= "</select>";
        $j = $i + 1;
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='10' name='cod[]' value=''></td>\n\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t</td><td align='center'><input type='text' size='20' name='des[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='1'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[]'></td>\n\t\t\t\t<td align='center'>" . mkDateSelecta("d", array($j)) . "</td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td><input type='hidden' name='novat[{$j}]' value='1'></td>\n\t\t\t\t<td> </td>\n\t\t\t</tr>";
        $key++;
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $pur['subtot'];
    # Get Total
    $TOTAL = sprint($pur['total']);
    # Get vat
    $VAT = sprint($pur['vat']);
    /* --- End Some calculations --- */
    if ($pur['ctyp'] == "s") {
        db_connect();
        $sql = "SELECT * FROM suppliers WHERE location != 'int' AND div = '" . USER_DIV . "' ORDER BY supno ASC";
        $supRslt = db_exec($sql) or errDie("Could not retrieve Suppliers Information from the Database.", SELF);
        $sups = "<select name='supplier'>";
        if (pg_numrows($supRslt) < 1) {
            $sups .= "<option value='-S'></option>";
        }
        while ($sup = pg_fetch_array($supRslt)) {
            if ($sup['supid'] == $pur['supplier']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $sups .= "<option value='{$sup['supid']}' {$sel}>{$sup['supno']} {$sup['supname']}</option>";
        }
        $sups .= "</select>";
        $sdata = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Supplier</td>\n\t\t\t\t<td>{$sups}</td>\n\t\t\t</tr>\n\t\t\t<input type='hidden' name='supaddr' value=''>";
    } elseif ($pur['ctyp'] == "cb") {
        $sdata = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Supplier</td>\n\t\t\t\t<td valign='center'><input type='text' name='supplier' value='{$pur['supplier']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t<td valign='center'><textarea name='supaddr' cols='18' rows='3'>{$pur['supaddr']}</textarea></td>\n\t\t\t</tr>";
    } elseif ($pur['ctyp'] == "c") {
        if (strlen($pur['supplier']) < 1) {
            $pur['supplier'] = "Cash Order";
        }
        //Cash Order
        $sdata = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Supplier</td>\n\t\t\t\t<td valign='center'><input type='text' name='supplier' value='{$pur['supplier']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t<td valign='center'><textarea name='supaddr' cols='18' rows='3'>{$pur['supaddr']}</textarea></td>\n\t\t\t</tr>";
    } elseif ($pur['ctyp'] == "p") {
        if (strlen($pur['supplier']) < 1) {
            $pur['supplier'] = "Petty Cash Order";
        }
        //Petty Cash Order
        $sdata = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Supplier</td>\n\t\t\t\t<td valign='center'><input type='text' name='supplier' value='{$pur['supplier']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t<td valign='center'><textarea name='supaddr' cols='18' rows='3'>{$pur['supaddr']}</textarea></td>\n\t\t\t</tr>";
    } elseif ($pur['ctyp'] == "ac") {
        //<input type='text' name='supplier' value='$pur[supplier]'>
        if (strlen($pur['supplier']) < 1) {
            $pur['supplier'] = "Ledger Account Order";
        }
        //Ledger Account Order
        $sdata = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Supplier</td>\n\t\t\t\t<td valign='center'><input type='text' name='supplier' value='{$pur['supplier']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t<td valign='center'><textarea name='supaddr' cols='18' rows='3'>{$pur['supaddr']}</textarea></td>\n\t\t\t</tr>";
    } elseif ($pur['ctyp'] == "c") {
        $sdata = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Supplier</td>\n\t\t\t\t<td valign='center'><input type='text' name='supplier' value='{$pur['supplier']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t<td valign='center'><textarea name='supaddr' cols='18' rows='3'>{$pur['supaddr']}</textarea></td>\n\t\t\t</tr>";
    } else {
        return slct($_POST);
    }
    $pur['delvat'] += 0;
    if ($pur['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $pur['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $pur['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $ex = "";
    if (strlen($pur['supinv']) and $pur['ctyp'] == "s") {
        db_conn('cubit');
        $Sl = "SELECT purnum,pdate FROM nons_purchases WHERE supplier='{$pur['supplier']}' AND supinv='{$pur['supinv']}' AND purid != '{$purid}'";
        $Ri = db_exec($Sl);
        if (pg_num_rows($Ri) > 0) {
            $pd = pg_fetch_array($Ri);
            $ex .= "<li class='err'>Non Stock Purchase {$pd['purnum']} on {$pd['pdate']} has the same supplier invoice number.</li>";
        }
        for ($i = 1; $i < 13; $i++) {
            db_conn($i);
            $Sl = "SELECT purnum,pdate FROM nons_purchases WHERE supplier='{$pur['supplier']}' AND supinv='{$pur['supinv']}'";
            $Ri = db_exec($Sl);
            if (pg_num_rows($Ri) > 0) {
                $pd = pg_fetch_array($Ri);
                $ex .= "<li class='err'>Non Stock Purchase {$pd['purnum']} on {$pd['pdate']} has same the supplier invoice number.</li>";
            }
        }
        db_conn('cubit');
        $Sl = "SELECT purnum,pdate FROM purchases WHERE supid='{$pur['supplier']}' AND supinv='{$pur['supinv']}'";
        $Ri = db_exec($Sl);
        if (pg_num_rows($Ri) > 0) {
            $pd = pg_fetch_array($Ri);
            $ex .= "<li class='err'>Purchase {$pd['purnum']} on {$pd['pdate']} has the same supplier invoice number.</li>";
        }
        for ($i = 1; $i < 13; $i++) {
            db_conn($i);
            $Sl = "SELECT purnum,pdate FROM purchases WHERE supid='{$pur['supplier']}' AND supinv='{$pur['supinv']}'";
            $Ri = db_exec($Sl);
            if (pg_num_rows($Ri) > 0) {
                $pd = pg_fetch_array($Ri);
                $ex .= "<li class='err'>Purchase {$pd['purnum']} on {$pd['pdate']} has same the supplier invoice number.</li>";
            }
        }
    }
    $Vatcodes .= "</select>";
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    if (!isset($supinv)) {
        $supinv = $pur['supinv'];
    }
    /* -- Final Layout -- */
    $details = "\n\t\t<center>\n\t\t<h3>New Non-Stock Order</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='purid' value='{$purid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t{$sdata}\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan=2> Non-Stock Order Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Non-Stock Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='ordernum' value='{$ordernum}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier Inv No</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='supinv' value='{$supinv}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("npur", $npur_year, $npur_month, $npur_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='vatinc' value='yes' {$chy}> No<input type='radio' size='7' name='vatinc' value='no' {$chn}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charges</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='7' name='shipchrg' value='{$pur['shipchrg']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$ex} {$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='nons-purchase-view.php'>View Non-Stock Orders</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$pur['remarks']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='subtot' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charges</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$pur['shipping']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$pur['vat']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' align='center'><input name='diffwhBtn' type='submit' value='Add Item'> | <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>\n\t\t</center>";
    return $details;
}
コード例 #16
0
function details($_POST, $error = "")
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($quoid)) {
        $v->isOk($quoid, "num", 1, 20, "Invalid quote number.");
    }
    if (isset($deptid)) {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>{$e['msg']}</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($quoid)) {
        //		$quoid = create_dummy(2);
        $quoid = create_dummy($deptid);
        $stkerr = "0,0";
    }
    if (!isset($done)) {
        $done = "";
    }
    # Get quote info
    db_connect();
    $sql = "SELECT * FROM pos_quotes WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
    $quoRslt = db_exec($sql) or errDie("Unable to get quote information");
    if (pg_numrows($quoRslt) < 1) {
        return "<li class='err'>Quote Not Found</li>";
    }
    $quo = pg_fetch_array($quoRslt);
    $cusname = $quo["cusname"];
    $cusaddr = $quo["cusaddr"];
    $telno = $quo["telno"];
    $lead = $quo["lead"];
    if (strlen($quo['ncdate']) < 1) {
        $ncdate_year = date("Y");
        $ncdate_month = date("m", mktime(0, 0, 0, date("m"), date("d") + 5, date("Y")));
        $ncdate_day = date("d", mktime(0, 0, 0, date("m"), date("d") + 5, date("Y")));
    } else {
        $darr = explode("-", $quo['ncdate']);
        $ncdate_year = $darr['0'];
        $ncdate_month = $darr['1'];
        $ncdate_day = $darr['2'];
    }
    # check if quote has been printed
    if ($quo['accepted'] == "y") {
        $error = "<li class='err'> Error : Quote number <b>{$quoid}</b> has already been printed.</li>";
        return $error;
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$quo['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected customer info
    db_connect();
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[%%KEY%%]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class='err'> There are no Sales People found in Cubit.</li>";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $quo['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # days drop downs
    $days = array("0" => "0", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $quo['terms']);
    # Keep the charge vat option stable
    if ($quo['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($quo['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    # format date
    list($pquote_year, $pquote_month, $pquote_day) = explode("-", $quo['odate']);
    /* --- End Drop Downs --- */
    // get the ID of the first warehouse, just when selecting from nons-product to new stock-product
    db_conn("exten");
    $sql = "SELECT whid FROM warehouses ORDER BY whid ASC LIMIT 1";
    $rslt = db_exec($sql) or errDie("Error reading warehouses (FWH).");
    if (pg_num_rows($rslt) > 0) {
        $FIRST_WH = pg_fetch_result($rslt, 0, 0);
    } else {
        $FIRST_WH = "-S";
    }
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>UNIT DISCOUNT</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this quote
    db_connect();
    $sql = "SELECT * FROM pos_quote_items  WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    $ai = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            db_conn('core');
            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
            $Accounts = "\n\t\t\t\t<select name='accounts[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
            while ($ad = pg_fetch_array($Ri)) {
                if (isb($ad['accid'])) {
                    continue;
                }
                if ($ad['accid'] == $stkd['account']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
            }
            $Accounts .= "</select>";
            $sernos = "";
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[{$ai}]' value='{$stkd['qty']}'>";
            $viewcost = "<input type='text' size='8' name='unitcost[{$ai}]' value='{$stkd['unitcost']}'>";
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            # Put in product
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whids[{$ai}]' value='{$stkd['whid']}'></td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[{$ai}]' value='{$stkd['stkid']}'>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptions[{$ai}]' value='{$stkd['description']}'> {$sernos}</td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td><input type='hidden' name='disc[{$ai}]' value='{$stkd['disc']}'><input type='hidden' name='discp[{$ai}]' value='{$stkd['discp']}'></td>\n\t\t\t\t\t<td><input type='hidden' name='amt[{$ai}]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[{$ai}]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t</tr>";
            $key++;
            ++$ai;
        } else {
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            # get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            //if($stk['serd'] == 'yes')
            //	continue;
            # put in product
            $products .= "\n\t\t\t\t<input type='hidden' name='amt[{$ai}]' value='{$stkd['amt']}'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whids[{$ai}]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[{$ai}]' value='{$stkd['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtys[{$ai}]' value='{$stkd['qty']}'></td>\n\t\t\t\t\t<td><input type='hidden' size='8' name='unitcost[{$ai}]' value='{$stkd['unitcost']}'>{$stkd['unitcost']}</td>\n\t\t\t\t\t<td><input type='text' size='4' name='disc[{$ai}]' value='{$stkd['disc']}'> OR <input type='text' size='4' name='discp[{$ai}]' value='{$stkd['discp']}' maxlength=5>%</td>\n\t\t\t\t\t<td>" . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[{$ai}]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t</tr>";
            $key++;
            ++$ai;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (!empty($stkidss[$key]) && $stkidss[$key] != "-S") {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class='err'>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[{$ai}]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $amt[$key] = sprint($amt[$key]);
                $stk["selamt"] = sprint($stk["selamt"]);
                # put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<input type='hidden' size='8' name='unitcost[{$ai}]'  value='{$stk['selamt']}'>\n\t\t\t\t\t<input type='hidden' name='accounts[{$ai}]' value=0>\n\t\t\t\t\t<input type='hidden' name='amt[{$ai}]' value='{$amt[$key]}'>\n\t\t\t\t\t<input type='hidden' name='descriptions[{$ai}]' value=''>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whids[{$ai}]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='stkids[{$ai}]' value='{$stk['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='qtys[{$ai}]' value='{$qtyss[$key]}'></td>\n\t\t\t\t\t\t<td>{$stk['selamt']}</td>\n\t\t\t\t\t\t<td><input type='text' size='4' name='disc[{$ai}]' value='{$discs[$key]}'> OR <input type='text' size='4' name='discp[{$ai}]' value='{$discps[$key]}' maxlength='5'>%</td>\n\t\t\t\t\t\t<td>" . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[{$ai}]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
                ++$ai;
            } elseif (isset($accountss[$key]) && $accountss[$key] != "0") {
                db_conn('core');
                $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                if (pg_num_rows($Ri) < 1) {
                    return "invalid.";
                }
                $ad = pg_fetch_array($Ri);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                # Input qty if not serialised
                $qtyin = "<input type='text' size='3' name='qtys[{$ai}]' value='{$qtyss[$key]}'>";
                # Check permissions
                $viewcost = "<input type='text' size='8' name='unitcost[{$ai}]' value='{$unitcosts[$key]}'>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[{$ai}]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vatcodess[$key] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='2'>{$ad['accname']}<input type='hidden' name='accounts[{$ai}]' value='{$accountss[$key]}'><input type='hidden' name='whids[{$ai}]' value='0'></td>\n\t\t\t\t\t\t<td>{$Vatcodes}<input type='hidden' name='stkids[{$ai}]' value='0'></td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='descriptions[{$ai}]' value='{$descriptionss[$key]}'></td>\n\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='disc[{$ai}]' value='0'><input type='hidden' name='discp[{$ai}]' value='0'></td>\n\t\t\t\t\t\t<td><input type='hidden' name='amt[{$ai}]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[{$ai}]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
                ++$ai;
            } else {
                if ($whid == "-S") {
                    continue;
                }
                if (!isset($addnon)) {
                    if (isset($filter_store) and $filter_store != "0") {
                        # get warehouse name
                        db_conn("exten");
                        $sql = "SELECT whname FROM warehouses WHERE whid = '{$filter_store}' AND div = '" . USER_DIV . "'";
                        $whRslt = db_exec($sql);
                        $wh = pg_fetch_array($whRslt);
                    }
                    if (isset($des) and $des != "") {
                        $len = strlen($des);
                        if ($des == "Show All") {
                            $Wh = "";
                            $des = "";
                        } else {
                            $Wh = "AND (lower(substr(stkdes,1,'{$len}'))=lower('{$des}') OR lower(substr(stkcod,1,'{$len}'))=lower('{$des}'))";
                        }
                    } else {
                        $Wh = "AND FALSE";
                        $des = "";
                    }
                    $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
                    if (isset($check_setting) and $check_setting == "yes") {
                        if (isset($filter_class) and $filter_class != "0") {
                            $Wh .= " AND prdcls = '{$filter_class}'";
                        }
                        if (isset($filter_cat) and $filter_cat != "0") {
                            $Wh .= " AND catid = '{$filter_cat}'";
                        }
                    }
                    if (isset($filter_store) and $filter_store != "0") {
                        $Wh .= " AND whid = '{$filter_store}'";
                    }
                    # get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        $error .= "<li class='err'>There are no stock items in the selected store.</li>";
                        continue;
                    }
                    if ($sel_frm == "stkcod") {
                        $cods = "<select class='width:15' name='stkidss[{$ai}]' onChange='javascript:document.form.submit();'>";
                        $cods .= "<option value='-S' disabled selected>Select Number</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            //if($stk['serd'] == 'yes')
                            //	continue;
                            $cods .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $cods .= "</select> ";
                        $descs = "";
                    } else {
                        $descs = "<select class='width:15' name='stkidss[{$ai}]' onChange='javascript:document.form.submit();'>";
                        $descs .= "<option value='-S' disabled selected>Select Description</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            //if($stk['serd'] == 'yes')
                            //	continue;
                            $descs .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $descs .= "</select> ";
                        $cods = "";
                    }
                    # put in drop down and warehouse
                    $products .= "\n\t\t\t\t\t\t<input type='hidden' name='accountss[{$ai}]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='descriptionss[{$ai}]' value=''>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[{$ai}]' value='{$filter_store}'></td>\n\t\t\t\t\t\t\t<td>{$cods}</td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td>{$descs}</td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[{$ai}]'  value='1'></td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td><input type='text' size='4' name='discs[{$ai}]' value='0'> OR <input type='text' size='4' name='discps[{$ai}]' value='0' maxlength=5>%</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='amts[{$ai}]' value='0.00'>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t</tr>";
                    ++$ai;
                } else {
                    db_conn('core');
                    $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                    $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                    $Accounts = "\n\t\t\t\t\t\t<select name='accountss[{$ai}]'>\n\t\t\t\t\t\t\t<option value='0'>Select Account</option>";
                    while ($ad = pg_fetch_array($Ri)) {
                        if (isb($ad['accid'])) {
                            continue;
                        }
                        $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                    }
                    $Accounts .= "</select>";
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[{$ai}]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vd['del'] == "Yes") {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[{$ai}]' value='{$FIRST_WH}'></td>\n\t\t\t\t\t\t\t<input type='hidden' name='stkidss[{$ai}]' value='-S'>\n\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[{$ai}]'></td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[{$ai}]' value='1'></td>\n\t\t\t\t\t\t\t<td><input type='text' name='unitcosts[{$ai}]' size=7></td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='discs[{$ai}]' value='0'><input type='hidden' name='discps[{$ai}]' value='0' ></td>\n\t\t\t\t\t\t</tr>";
                    ++$ai;
                }
            }
        }
    } else {
        if (isset($addprodBtn) || !isset($addnon)) {
            if (isset($filter_store) and $filter_store != "0") {
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$filter_store}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
            }
            if (isset($des) and $des != "") {
                $len = strlen($des);
                if ($des == "Show All") {
                    $Wh = "";
                    $des = "";
                } else {
                    $Wh = "AND (lower(substr(stkdes,1,'{$len}'))=lower('{$des}') OR lower(substr(stkcod,1,'{$len}'))=lower('{$des}'))";
                }
            } else {
                $Wh = "AND FALSE";
                $des = "";
            }
            $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
            if (isset($check_setting) and $check_setting == "yes") {
                if (isset($filter_class) and $filter_class != "0") {
                    $Wh .= " AND prdcls = '{$filter_class}'";
                }
                if (isset($filter_cat) and $filter_cat != "0") {
                    $Wh .= " AND catid = '{$filter_cat}'";
                }
            }
            if (isset($filter_store) and $filter_store != "0") {
                $Wh .= " AND whid = '{$filter_store}'";
            }
            # get stock on this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
            $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
            if (pg_numrows($stkRslt) < 1) {
                if (!isset($err)) {
                    $err = "";
                }
                $err .= "<li>There are no stock items in the selected warehouse.</li>";
            }
            $stks = "\n\t\t\t\t<select name='stkidss[{$ai}]' onChange='javascript:document.form.submit();'>\n\t\t\t\t\t<option value='-S' disabled selected>Select Number</option>";
            $count = 0;
            while ($stk = pg_fetch_array($stkRslt)) {
                //if($stk['serd'] == 'yes')
                //	continue;
                $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
            }
            $stks .= "</select> ";
            $products .= "\n\t\t\t\t<input type='hidden' name='descriptionss[{$ai}]' value=''>\n\t\t\t\t<input type='hidden' name='vatcodess[{$ai}]' value=''>\n\t\t\t\t<input type='hidden' name='accountss[{$ai}]' value='0'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whidss[{$ai}]' value='{$filter_store}'></td>\n\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[{$ai}]' value='1'></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td><input type='text' size='4' name='discs[{$ai}]' value='0'> OR <input type='text' size='4' name='discps[{$ai}]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td></td>\n\t\t\t\t</tr>";
            ++$ai;
        } else {
            if (isset($addnon)) {
                db_conn('core');
                $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                $Accounts = "\n\t\t\t\t<select name='accountss[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
                while ($ad = pg_fetch_array($Ri)) {
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                }
                $Accounts .= "</select>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t<select name='vatcodess[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vd['del'] == "Yes") {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[{$ai}]' value='{$FIRST_WH}'></td>\n\t\t\t\t\t<input type='hidden' name='stkidss[{$ai}]' value='-S'>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[{$ai}]'></td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[{$ai}]' value='1'></td>\n\t\t\t\t\t<td><input type='text' name='unitcosts[{$ai}]' size='7'></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td><input type='hidden' name='discs[{$ai}]' value='0'><input type='hidden' name='discps[{$ai}]' value='0'></td>\n\t\t\t\t</tr>";
                ++$ai;
            }
        }
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($quo['subtot']);
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($quo['subtot']);
    $VAT = sprint($quo['vat']);
    $TOTAL = sprint($quo['total']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    # check only if the customer is selected
    if (isset($cusnum) && $cusnum != "-S") {
        db_connect();
        # check credit limit (inclide unpaid quotes)
        $sql = "SELECT sum(balance) FROM invoices WHERE cusnum = '{$cusnum}' AND printed = 'y' AND balance <> 0 AND div = '" . USER_DIV . "'";
        $rslt = db_exec($sql);
        $bal = pg_fetch_array($rslt);
        $credbal = $bal['sum'];
        #check againg credit limit
        if ($TOTAL + $credbal > $cust['credlimit']) {
            $error .= "<li class='err'>Warning : Customers Credit limit of <b>" . CUR . " {$cust['credlimit']}</b> has been exceeded</li>";
        }
        $avcred = $cust['credlimit'] - $credbal;
    } else {
        $avcred = "0.00";
    }
    $quo['delvat'] += 0;
    if ($quo['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $quo['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $quo['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    /*--- Start checks --- */
    #attempt to get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$quo['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptid'] = "<li class='err'>Department not Found.</li>";
        $dept['deptid'] = "0";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    if (!isset($dept['deptid'])) {
        # Query server for depts
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE div = '" . USER_DIV . "' ORDER BY deptname ASC";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($deptRslt) < 1) {
            return "<li class='err'>There are no Departments found in Cubit.</li>";
        } else {
            $depts = "<select id='deptid_sel' onChange='updateDept(this);'>";
            $depts .= "<option value='0'>-- SELECT DEPARTMENT --</option>";
            while ($dept = pg_fetch_array($deptRslt)) {
                $depts .= "<option value='{$dept['deptid']}'>{$dept['deptname']}</option>";
            }
            $depts .= "</select>";
        }
        $deptshow = $depts;
        $deptid = 0;
    } else {
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE deptid='{$dept['deptid']}'";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($deptRslt) > 0) {
            $dept = pg_fetch_array($deptRslt);
            $deptshow = $dept["deptname"];
        } else {
            errDie("Invalid department selected.");
        }
        db_conn("cubit");
        $sql = "UPDATE pos_quotes SET deptid='{$dept['deptid']}' WHERE quoid='{$quoid}' AND deptid<>'{$dept['deptid']}'";
        $rslt = db_exec($sql) or errDie("Error updating department id.");
    }
    // Which display method was selected
    if (isset($sel_frm) && $sel_frm == "stkdes") {
        $sel_frm_cod = "";
        $sel_frm_des = "checked";
    } else {
        $sel_frm_cod = "checked";
        $sel_frm_des = "";
    }
    $sel = "";
    if (isset($lead) and strlen($lead) > 0) {
        $sel = "checked=yes";
    }
    if (!isset($old_customer_select)) {
        $old_customer_select = "";
    }
    #get customers
    $get_cust = "SELECT cusnum, surname, vatnum, paddr1 FROM customers WHERE blocked = 'no' AND location = 'loc' ORDER BY cusname";
    $run_cust = db_exec($get_cust) or errDie("Unable to get customer information.");
    if (pg_numrows($run_cust) < 1) {
        $cust_drop = "<input type='hidden' name='customer_select' value=''>No Customers Found.";
    } else {
        $cust_drop = "<select name='customer_select' onChange=\"document.form.submit();\">";
        $cust_drop .= "<option value=''>Select Customer Or Enter Details</option>";
        while ($carr = pg_fetch_array($run_cust)) {
            if (isset($customer_select) and $customer_select == $carr['cusnum']) {
                $cust_drop .= "<option value='{$carr['cusnum']}' selected>{$carr['surname']}</option>";
                if ($old_customer_select != $customer_select) {
                    $cusname = $carr['surname'];
                    $cusaddr = $carr['paddr1'];
                    $telno = $carr['vatnum'];
                }
            } else {
                $cust_drop .= "<option value='{$carr['cusnum']}'>{$carr['surname']}</option>";
            }
        }
        $cust_drop .= "</select>";
    }
    if (isset($addprodBtn) or isset($addnon) or isset($saveBtn) or isset($upBtn) or isset($doneBtn) or isset($des)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $optional_filter_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
    if (isset($optional_filter_setting) and $optional_filter_setting == "yes") {
        db_connect();
        $catsql = "SELECT catid, cat, catcod FROM stockcat WHERE div = '" . USER_DIV . "' ORDER BY cat ASC";
        $catRslt = db_exec($catsql);
        if (pg_numrows($catRslt) < 1) {
            $cat_drop = "<input type='hidden' name='filter_cat' value='0'>";
        } else {
            $cat_drop = "<select name='filter_cat'>";
            $cat_drop .= "<option value='0'>All Categories</option>";
            while ($cat = pg_fetch_array($catRslt)) {
                if (isset($filter_cat) and $filter_cat == $cat['catid']) {
                    $cat_drop .= "<option value='{$cat['catid']}' selected>({$cat['catcod']}) {$cat['cat']}</option>";
                } else {
                    $cat_drop .= "<option value='{$cat['catid']}'>({$cat['catcod']}) {$cat['cat']}</option>";
                }
            }
            $cat_drop .= "</select>";
        }
        # Select classification
        $classsql = "SELECT * FROM stockclass WHERE div = '" . USER_DIV . "' ORDER BY classname ASC";
        $clasRslt = db_exec($classsql);
        if (pg_numrows($clasRslt) < 1) {
            $class_drop = "<input type='hidden' name='filter_class' value='0'>";
        } else {
            $class_drop = "<select name='filter_class' style='width: 167'>";
            $class_drop .= "<option value='0'>All Classifications</option>";
            while ($clas = pg_fetch_array($clasRslt)) {
                if (isset($filter_class) and $filter_class == $clas['clasid']) {
                    $class_drop .= "<option value='{$clas['clasid']}' selected>{$clas['classname']}</option>";
                } else {
                    $class_drop .= "<option value='{$clas['clasid']}'>{$clas['classname']}</option>";
                }
            }
            $class_drop .= "</select>";
        }
        $display_optional_filters = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Select Category</td>\n\t\t\t\t<td>{$cat_drop}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Select Classification</td>\n\t\t\t\t<td>{$class_drop}</td>\n\t\t\t</tr>";
    }
    db_conn("exten");
    $sql = "SELECT whid, whname, whno FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        $store_drop = "<input type='hidden' name='filter_store' value='0'>";
    } else {
        if (!isset($filter_store)) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $filter_store = $set['value'];
            }
        }
        $store_drop = "<select name='filter_store'>";
        $store_drop .= "<option value='0'>All Stores</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            if (isset($filter_store) and $filter_store == $wh['whid']) {
                $store_drop .= "<option value='{$wh['whid']}' selected>({$wh['whno']}) {$wh['whname']}</option>";
            } else {
                $store_drop .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
            }
        }
        $store_drop .= "</select>";
    }
    $details = "\n\t\t<center>\n\t\t<h3>New POS Quote</h3>\n\t\t<script>\n\t\t\tfunction updateDept(seldept) {\n\t\t\t\tsel = seldept.options[seldept.selectedIndex];\n\t\t\t\tdocument.form.deptid.value = sel.value;\n\t\t\t\tgetObject('deptshow').innerHTML = sel.text;\n\t\t\t}\n\t\t</script>\n\t\t<form action='" . SELF . "' method='POST' id='quofrm' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='old_customer_select' value='{$customer_select}'>\n\t\t\t<input type='hidden' name='quoid' value='{$quoid}'>\n\t\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t\t<input type='hidden' name='deptid' value='{$dept['deptid']}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Customer</td>\n\t\t\t\t\t\t\t<td>{$cust_drop}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t<td valign='center'><div id='deptshow'>{$deptshow}</div></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' name='cusname' value='{$cusname}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t\t\t<td valign='center'><textarea name='cusaddr'>" . htmlspecialchars($cusaddr) . "</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Contact Number</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' name='telno' value='{$telno}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Next Contact Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("ncdate", $ncdate_year, $ncdate_month, $ncdate_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Add As Lead</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='checkbox' name='lead' {$sel} value='yes'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Using</td>\n\t\t\t\t\t\t\t<td>Stock Code<input type='radio' name='sel_frm' value='stkcod' onChange='javascript:document.form.submit();' {$sel_frm_cod}><br>Stock Description<input type='radio' name='sel_frm' value='stkdes' onChange='javascript:document.form.submit();' {$sel_frm_des}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'>Additional Filters</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Store</td>\n\t\t\t\t\t\t\t<td>{$store_drop}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t{$display_optional_filters}\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Stock Filter</td>\n\t\t\t\t\t\t\t<td><input type='text' size='13' name='des' value='{$des}'> <input type='submit' value='Search'> <input type='submit' name='des' value='Show All'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> POS Quote Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>POS Quote No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$quo['quoid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='ordno' value='{$quo['ordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='inc' {$chin}> No<input type='radio' size='7' name='chrgvat' value='exc' {$chex}> No Vat<input type='radio' size='7' name='chrgvat' value='nov' {$chno}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t\t<td valign='center'>{$salesps}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Quote Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("pquote", $pquote_year, $pquote_month, $pquote_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='traddisc' value='{$quo['traddisc']}'>%</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='7' name='delchrg' value='{$quo['delchrg']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Comments</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='pos-quote-new.php'>New POS Quote</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='comm' rows='4' cols='20'>{$quo['comm']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='pos-quote-view.php'>View POS Quotes</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type=hidden name=SUBTOT value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$quo['discount']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$quo['delivery']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><b>VAT {$vat14}</b></td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input name='addprodBtn' type='submit' value='Add Product'>| <input name='addnon' type='submit' value='Add Non stock Product'> | <input type='submit' name='saveBtn' value='Save'></td>\n\t\t\t\t<td> | <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}
コード例 #17
0
function details($_POST, $error = "")
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($quoid)) {
        $v->isOk($quoid, "num", 1, 20, "Invalid quote number.");
    } else {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class=err>{$e['msg']}</li>";
        }
        $confirm .= "{$error}<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($quoid)) {
        $quoid = create_dummy($deptid);
        $stkerr = "0,0";
    }
    if (!isset($done)) {
        $done = "";
    }
    # Get quote info
    db_connect();
    $sql = "SELECT * FROM quotes WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
    $quoRslt = db_exec($sql) or errDie("Unable to get quote information");
    if (pg_numrows($quoRslt) < 1) {
        return "<li class=err>Quote Not Found</li>";
    }
    $quo = pg_fetch_array($quoRslt);
    # check if quote has been printed
    if ($quo['accepted'] == "y") {
        $error = "<li class=err> Error : Quote number <b>{$quoid}</b> has already been printed.</li>";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$quo['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class=err>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected customer info
    db_connect();
    $sql = "SELECT * FROM customers WHERE cusnum = '{$quo['cusnum']}' AND div = '" . USER_DIV . "'";
    $custRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($custRslt) < 1) {
        db_connect();
        # Query server for customer info
        $sql = "SELECT cusnum,cusname,surname FROM customers WHERE deptid = '{$quo['deptid']}' AND location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $err = "<li class=err>No customer names starting with <b>{$letters}</b> in database.</li>";
            return view_err($_POST, $err);
        } else {
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            $customers .= "<option value='-S' selected>Select Customer</option>";
            while ($cust = pg_fetch_array($custRslt)) {
                $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
            }
            $customers .= "</select>";
        }
        # take care of the unset vars
        $cust['addr1'] = "";
        $cust['cusnum'] = "";
        $cust['vatnum'] = "";
        $cust['accno'] = "";
    } else {
        $cust = pg_fetch_array($custRslt);
        # moarn if customer account has been blocked
        if ($cust['blocked'] == 'yes') {
            return "<li class=err>Error : Selected customer account has been blocked.</li>";
        }
        $customers = "<input type=hidden name=cusnum value='{$cust['cusnum']}'>{$cust['cusname']}  {$cust['surname']}";
        $cusnum = $cust['cusnum'];
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class=err> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class=err> There are no Sales People found in Cubit.</li>";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $quo['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $quo['terms']);
    # Keep the charge vat option stable
    if ($quo['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($quo['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    # format date
    list($oyear, $omon, $oday) = explode("-", $quo['odate']);
    /* --- End Drop Downs --- */
    // get the ID of the first warehouse
    db_conn("exten");
    $sql = "SELECT whid FROM warehouses ORDER BY whid ASC LIMIT 1";
    $rslt = db_exec($sql) or errDie("Error reading warehouses (FWH).");
    if (pg_num_rows($rslt) > 0) {
        $FIRST_WH = pg_fetch_result($rslt, 0, 0);
    } else {
        $FIRST_WH = "-S";
    }
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=100%>\n\t<tr><th>STORE</th><th>ITEM NUMBER</th><th>VAT CODE</th><th>DESCRIPTION</th><th>QTY</th><th>UNIT PRICE</th><th>UNIT DISCOUNT</th><th>AMOUNT</th><th>Remove</th><tr>";
    # get selected stock in this quote
    db_connect();
    $sql = "SELECT * FROM quote_items  WHERE quoid = '{$quoid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            db_conn('core');
            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
            $Accounts = "<select name=accounts[]>\n\t\t\t<option value='0'>Select Account</option>";
            while ($ad = pg_fetch_array($Ri)) {
                if (isb($ad['accid'])) {
                    continue;
                }
                if ($ad['accid'] == $stkd['account']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
            }
            $Accounts .= "</select>";
            $sernos = "";
            # Input qty if not serialised
            $qtyin = "<input type=text size=3 name=qtys[] value='{$stkd['qty']}'>";
            $viewcost = "<input type=text size=8 name=unitcost[] value='{$stkd['unitcost']}'>";
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "<select name=vatcodes[]>\n\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            //print "fo";
            # Put in product
            $products .= "<tr class='bg-odd'><td colspan=2>{$Accounts}<input type=hidden name=whids[] value='{$stkd['whid']}'></td><td><input type=hidden name=stkids[] value='{$stkd['stkid']}'>{$Vatcodes}</td><td><input type=text size=20 name=descriptions[] value='{$stkd['description']}'> {$sernos}</td><td>{$qtyin}</td><td>{$viewcost}</td><td><input type=hidden name=disc[] value='{$stkd['disc']}'><input type=hidden name=discp[] value='{$stkd['discp']}'></td><td><input type=hidden name=amt[] value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td><td><input type=checkbox name=remprod[] value='{$key}'><input type=hidden name=SCROLL value=yes></td></tr>";
            $key++;
        } else {
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "<select name=vatcodes[]>\n\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            # put in product
            $products .= "<input type=hidden name=accounts[] value=0>\n\t\t\t<input type=hidden name=descriptions[] value=''>\n\t\t\t<tr class='bg-odd'><td><input type=hidden name=whids[] value='{$stkd['whid']}'>{$wh['whname']}</td><td><input type=hidden name=stkids[] value='{$stkd['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td><td>{$Vatcodes}</td><td>" . extlib_rstr($stk['stkdes'], 30) . "</td><td><input type=text size=3 name=qtys[] value='{$stkd['qty']}'></td><td><input type=text size=8 name=unitcost[] value='{$stkd['unitcost']}'></td><td><input type=text size=4 name=disc[] value='{$stkd['disc']}'> OR <input type=text size=4 name=discp[] value='{$stkd['discp']}' maxlength=5>%</td><td><input type=hidden name=amt[] value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td><td><input type=checkbox name=remprod[] value='{$key}'><input type=hidden name=SCROLL value=yes></td></tr>";
            $key++;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && isset($cust['pricelist'])) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # get price from price list if it is set
                if (isset($cust['pricelist'])) {
                    # get selected stock in this warehouse
                    db_conn("exten");
                    $sql = "SELECT price FROM plist_prices WHERE listid = '{$cust['pricelist']}' AND stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
                    $plRslt = db_exec($sql);
                    if (pg_numrows($plRslt) > 0) {
                        $pl = pg_fetch_array($plRslt);
                        $stk['selamt'] = $pl['price'];
                    }
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class=err>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "<select name=vatcodes[]>\n\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                # put in selected warehouse and stock
                $products .= "<input type=hidden name=accounts[] value=0>\n\t\t\t\t<input type=hidden name=descriptions[] value=''>\n\t\t\t\t<tr class='bg-odd'><td><input type=hidden name=whids[] value='{$whid}'>{$wh['whname']}</td><td><input type=hidden name=stkids[] value='{$stk['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td><td>{$Vatcodes}</td><td>" . extlib_rstr($stk['stkdes'], 30) . "</td><td><input type=text size=3 name=qtys[] value='{$qtyss[$key]}'></td><td><input type=text size=8 name='unitcost[]'  value='{$stk['selamt']}'></td><td><input type=text size=4 name=disc[] value='{$discs[$key]}'> OR <input type=text size=4 name=discp[] value='{$discps[$key]}' maxlength=5>%</td><td><input type=hidden name=amt[] value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td><td><input type=checkbox name=remprod[] value='{$keyy}'></td></tr>";
                $keyy++;
            } elseif (isset($accountss[$key]) && $accountss[$key] != "0" && isset($cust['pricelist'])) {
                db_conn('core');
                $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                if (pg_num_rows($Ri) < 1) {
                    return "invalid.";
                }
                $ad = pg_fetch_array($Ri);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                # Input qty if not serialised
                $qtyin = "<input type=text size=3 name=qtys[] value='{$qtyss[$key]}'>";
                # Check permissions
                $viewcost = "<input type=text size=8 name=unitcost[] value='{$unitcosts[$key]}'>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "<select name=vatcodes[]>\n\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vatcodess[$key] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                # Put in selected warehouse and stock
                $products .= "<tr class='bg-odd'><td colspan=2>{$ad['accname']}<input type=hidden name=accounts[] value='{$accountss[$key]}'><input type=hidden name=whids[] value='0'></td><td>{$Vatcodes}<input type=hidden name=stkids[] value='0'></td><td><input type=text size=20 name=descriptions[] value='{$descriptionss[$key]}'></td><td>{$qtyin}</td><td>{$viewcost}</td><td><input type=hidden name=disc[] value='0'><input type=hidden name=discp[] value='0'></td><td><input type=hidden name=amt[] value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td><td><input type=checkbox name=remprod[] value='{$keyy}'></td></tr>";
                $keyy++;
            } else {
                if (!isset($diffwhBtn)) {
                    # skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    if (!isset($addnon)) {
                        # get warehouse name
                        db_conn("exten");
                        $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                        $whRslt = db_exec($sql);
                        $wh = pg_fetch_array($whRslt);
                        # get stock on this warehouse
                        db_connect();
                        $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                        $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                        if (pg_numrows($stkRslt) < 1) {
                            $error .= "<li class=err>There are no stock items in the selected store.</li>";
                            continue;
                        }
                        $stks = "<select class='width:15' name='stkidss[]' onChange='javascript:document.form.submit();'>";
                        $stks .= "<option value='-S' disabled selected>Select Number</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $stks .= "</select> ";
                        db_conn('cubit');
                        $Sl = "SELECT * FROM vatcodes ORDER BY code";
                        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                        $Vatcodes = "<select name=vatcodess[]>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                        while ($vd = pg_fetch_array($Ri)) {
                            if ($vd['del'] == "Yes") {
                                $sel = "selected";
                            } else {
                                $sel = "";
                            }
                            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                        }
                        $Vatcodes .= "</select>";
                        # put in drop down and warehouse
                        $products .= "<input type=hidden name=accountss[] value=0>\n\t\t\t\t\t\t<input type=hidden name=descriptionss[] value=''>\n\t\t\t\t\t\t<tr class='bg-odd'><td><input type=hidden name=whidss[] value='{$whid}'>{$wh['whname']}</td><td>{$stks}<input type=hidden name=vatcodess value=0></td><td></td><td></td><td><input type=text size=3 name='qtyss[]'  value='1'></td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td><input type=hidden name=amts[] value='0.00'>" . CUR . " 0.00</td><td></td></tr>";
                    } else {
                        db_conn('core');
                        $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                        $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                        $Accounts = "<select name=accountss[]>\n\t\t\t\t\t\t<option value='0'>Select Account</option>";
                        while ($ad = pg_fetch_array($Ri)) {
                            if (isb($ad['accid'])) {
                                continue;
                            }
                            $Accounts .= "<option value={$ad['accid']}>{$ad['accname']}</option>";
                        }
                        $Accounts .= "</select>";
                        db_conn('cubit');
                        $Sl = "SELECT * FROM vatcodes ORDER BY code";
                        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                        $Vatcodes = "<select name=vatcodess[]>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                        while ($vd = pg_fetch_array($Ri)) {
                            if ($vd['del'] == "Yes") {
                                $sel = "selected";
                            } else {
                                $sel = "";
                            }
                            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                        }
                        $Vatcodes .= "</select>";
                        $products .= "<tr class='bg-odd'><td colspan=2>{$Accounts}<input type=hidden name=whidss[] value='{$FIRST_WH}'></td>\n\t\t\t\t\t\t<inpu type=hidden name='stkidss[]' value=''>\n\t\t\t\t\t\t<td>{$Vatcodes}</td><td><input type=text size=20 name=descriptionss[]></td><td>\n\t\t\t\t\t\t<input type=text size=3 name='qtyss[]' value='1'></td><td><input type=text name=unitcosts[] size=7></td><td></td><td>\n\t\t\t\t\t\t" . CUR . " 0.00</td><td><input type=hidden name=discs[] value='0'><input type=hidden name=discps[] value='0' >\n\t\t\t\t\t\t</td></tr>";
                    }
                }
            }
        }
    } else {
        if (!(isset($diffwhBtn) || isset($addnon))) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                if (isset($wtd) && $wtd != 0) {
                    $whid = $wtd;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected warehouse.";
                }
                $stks = "<select class='width:15' name='stkidss[]' onChange='javascript:document.form.submit();'>";
                $stks .= "<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "<input type=hidden name=descriptionss[] value=''>\n\t\t\t\t<input type=hidden name=vatcodess[] value=''>\n\t\t\t\t<input type=hidden name=accountss[] value='0'>\n\t\t\t\t<tr class='bg-odd'><td><input type=hidden name=whidss[] value='{$whid}'>{$wh['whname']}</td><td>{$stks}</td><td></td><td></td><td><input type=text size=3 name=qtyss[] value='1'></td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
            } else {
                $products .= "<tr class='bg-odd'><td>{$whs}</td><td></td><td> </td><td> </td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
            }
        } else {
            if (isset($addnon)) {
                db_conn('core');
                $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                $Accounts = "<select name=accountss[]>\n\t\t\t<option value='0'>Select Account</option>";
                while ($ad = pg_fetch_array($Ri)) {
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    $Accounts .= "<option value={$ad['accid']}>{$ad['accname']}</option>";
                }
                $Accounts .= "</select>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "<select name=vatcodess[]>\n\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vd['del'] == "Yes") {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $products .= "<tr class='bg-odd'><td colspan=2>{$Accounts}<input type=hidden name=whidss[] value='{$FIRST_WH}'></td>\n\t\t\t<inpu type=hidden name='stkidss[]' value=''>\n\t\t\t<td>{$Vatcodes}</td><td><input type=text size=20 name=descriptionss[]></td><td>\n\t\t\t<input type=text size=3 name='qtyss[]' value='1'></td><td><input type=text name=unitcosts[] size=7></td><td></td><td>\n\t\t\t" . CUR . " 0.00</td><td><input type=hidden name=discs[] value='0'><input type=hidden name=discps[] value='0' >\n\t\t\t</td></tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        $products .= "<tr class='bg-odd'><td>{$whs}</td><td></td><td></td><td> </td><td> </td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($quo['subtot']);
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($quo['subtot']);
    $VAT = sprint($quo['vat']);
    $TOTAL = sprint($quo['total']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    # check only if the customer is selected
    if (isset($cusnum) && $cusnum != "-S") {
        #check againg credit limit
        if ($cust['credlimit'] != 0 && $TOTAL + $cust['balance'] > $cust['credlimit']) {
            $error .= "<li class=err>Warning : Customers Credit limit of <b>" . CUR . " {$cust['credlimit']}</b> has been exceeded</li>";
        }
        $avcred = $cust['credlimit'] - $cust['balance'];
    } else {
        $avcred = "0.00";
    }
    $quo['delvat'] += 0;
    if ($quo['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $quo['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "<select name=delvat>\n\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $quo['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    /*--- Start checks --- */
    /* -- Final Layout --No VAT<input type=radio size=7 name=chrgvat value='nov' $chno> */
    $details = "<center><h3>New Quote</h3>\n\t<form action='" . SELF . "' method=post name=form>\n\t<input type=hidden name=key value=update>\n\t<input type=hidden name=quoid value='{$quoid}'>\n\t<input type=hidden name=letters value='{$letters}'>\n\t<input type=hidden name=stkerr value='{$stkerr}'>\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=95%>\n \t<tr><td valign=top>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><th colspan=2> Customer Details </th></tr>\n\t\t\t<tr class='bg-odd'><td>Department</td><td valign=center>{$dept['deptname']}</td></tr>\n\t\t\t<tr class='bg-even'><td>Account No.</td><td valign=center>{$cust['accno']}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Customer</td><td valign=center>{$customers}</td></tr>\n\t\t\t<tr class='bg-even'><td valign=top>Customer Address</td><td valign=center>" . nl2br($cust['addr1']) . "</td></tr>\n\t\t\t<tr class='bg-odd'><td>Customer Order number</td><td valign=center><input type=text size=10 name=cordno value='{$quo['cordno']}'></td></tr>\n\t\t\t<tr class='bg-even'><td>Customer VAT Number</td><td>{$cust['vatnum']}</td></tr>\n\t\t</table>\n\t</td><td valign=top align=right>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><th colspan=2> Quote Details </th></tr>\n\t\t\t<tr class='bg-even'><td>Quote No.</td><td valign=center>{$quo['quoid']}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Order No.</td><td valign=center><input type=text size=5 name=ordno value='{$quo['ordno']}'></td></tr>\n\t\t\t<tr class='bg-even'><td>VAT Inclusive</td><td valign=center>Yes <input type=radio size=7 name=chrgvat value='inc' {$chin}> No<input type=radio size=7 name=chrgvat value='exc' {$chex}> </td></tr>\n\t\t\t<tr class='bg-odd'><td>Terms</td><td valign=center>{$termssel} Days</td></tr>\n\t\t\t<tr class='bg-even'><td>Sales Person</td><td valign=center>{$salesps}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Quote Date</td><td valign=center><input type=text size=2 name=oday maxlength=2 value='{$oday}'>-<input type=text size=2 name=omon maxlength=2 value='{$omon}'>-<input type=text size=4 name=oyear maxlength=4 value='{$oyear}'></td></tr>\n\t\t\t<tr class='bg-even'><td>Available Credit</td><td>" . CUR . " {$avcred}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Trade Discount</td><td valign=center><input type=text size=5 name=traddisc value='{$quo['traddisc']}'>%</td></tr>\n\t\t\t<tr class='bg-even'><td>Delivery Charge</td><td valign=center><input type=text size=7 name=delchrg value='{$quo['delchrg']}'>{$Vatcodes}</td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td><br></td></tr>\n\t<tr><td colspan=2>{$products}</td></tr>\n\t<tr><td>\n\t\t<p>\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t\t\t<tr><th width=25%>Quick Links</th><th width=25%>Comments</th><td rowspan=5 valign=top width=50%>{$error}</td></tr>\n\t\t\t<tr><td class='bg-odd'><a href='quote-new.php'>New Quote</a></td><td class='bg-odd' rowspan=4 align=center valign=top><textarea name=comm rows=4 cols=20>{$quo['comm']}</textarea></td></tr>\n\t\t\t<tr class='bg-odd'><td><a href='quote-view.php'>View Quotes</a></td></tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t</table>\n\t</td><td align=right>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=80%>\n\t\t\t<tr class='bg-odd'><td>SUBTOTAL</td><td align=right>" . CUR . " <input type=hidden name=SUBTOT value='{$SUBTOT}'>{$SUBTOT}</td></tr>\n\t\t\t<tr class='bg-even'><td>Trade Discount</td><td align=right>" . CUR . " {$quo['discount']}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Delivery Charge</td><td align=right>" . CUR . " {$quo['delivery']}</td></tr>\n\t\t\t<tr class='bg-even'><td><b>VAT @ {$VATP}%</b></td><td align=right>" . CUR . " {$VAT}</td></tr>\n\t\t\t<tr class='bg-odd'><th>GRAND TOTAL</th><td align=right>" . CUR . " {$TOTAL}</td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td align=right><input name=diffwhBtn type=submit value='Different Store'> | <input name=addprodBtn type=submit value='Add Product'>| <input name=addnon type=submit value='Add Non stock Product'> | <input type=submit name='saveBtn' value='Save'></td><td> | <input type=submit name='upBtn' value='Update'>{$done}</td></tr>\n\t</table>\n\t</form>\n\t</center>";
    return $details;
}
コード例 #18
0
function details($_POST, $error = "")
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid Non-Stock Quote number.");
    } else {
        $invid = create_dummy(0);
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Get quote info
    db_connect();
    $sql = "SELECT * FROM nons_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get quote information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Quote Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # check if quote has been printed
    if ($inv['done'] == "y") {
        $error = "<li class='err'> Error : quote number <b>{$invid}</b> has already been printed.";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    $lead = $inv["lead"];
    if (strlen($inv['ncdate']) < 1) {
        $ncdate_year = date("Y");
        $ncdate_month = date("m", mktime(0, 0, 0, date("m"), date("d") + 5, date("Y")));
        $ncdate_day = date("d", mktime(0, 0, 0, date("m"), date("d") + 5, date("Y")));
    } else {
        $darr = explode("-", $inv['ncdate']);
        $ncdate_year = $darr['0'];
        $ncdate_month = $darr['1'];
        $ncdate_day = $darr['2'];
    }
    /* --- Start Drop Downs --- */
    # format date
    list($nquo_year, $nquo_month, $nquo_day) = explode("-", $inv['odate']);
    # keep the charge vat option stable
    if ($inv['chrgvat'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chnone = "";
    } elseif ($inv['chrgvat'] == "no") {
        $chy = "";
        $chn = "checked=yes";
        $chnone = "";
    } else {
        $chy = "";
        $chn = "";
        $chnone = "checked=yes";
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>VAT Code</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM nons_inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $stkd['amt'] = round($stkd['amt'], 2);
        $chk = "";
        if ($stkd['vatex'] == 'y') {
            $chk = "checked='yes'";
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl);
        $vats = "<select name='vatcodes[]'>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatex'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $vats .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $vats .= "</option>";
        $stkd['amt'] = sprint($stkd['amt']);
        # put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center' nowrap><input type='hidden' name='des[]' value='{$stkd['description']}'>" . nl2br($stkd['description']) . "</td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t<td align='center'>{$vats}</td>\n\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$i}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t</tr>";
        $i++;
    }
    # Look above(remprod keys)
    $keyy = $i;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    if ($i == 0 || isset($diffwhBtn)) {
        # add one
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'>\n\t\t\t\t\t<table id='tblCtrls' width='420px' height='30px' border='0' cellspacing='0' cellpadding='0' bgcolor='#D6D3CE'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='tdClass'>\n\t\t\t\t\t\t\t\t<img alt='Bold' class='buttonClass' src='images/bold.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doBold()'>\n\t\t\t\t\t\t\t\t<img alt='Italic' class='buttonClass' src='images/italic.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doItalic()'>\n\t\t\t\t\t\t\t\t<img alt='Underline' class='buttonClass' src='images/underline.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doUnderline()'>\n\t\t\t\t\t\t\t\t<img alt='Left' class='buttonClass' src='images/left.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doLeft()'>\n\t\t\t\t\t\t\t\t<img alt='Center' class='buttonClass' src='images/center.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doCenter()'>\n\t\t\t\t\t\t\t\t<img alt='Right' class='buttonClass' src='images/right.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doRight()'>\n\t\t\t\t\t\t\t\t<img alt='Ordered List' class='buttonClass' src='images/ordlist.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doOrdList()'>\n\t\t\t\t\t\t\t\t<img alt='Bulleted List' class='buttonClass' src='images/bullist.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doBulList()'>\n\t\t\t\t\t\t\t\t<img alt='Horizontal Rule' class='buttonClass' src='images/rule.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doRule()'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td class='tdClass' align=right>\n\t\t\t\t\t\t\t\t<select name='selSize' onChange='doSize(this.options[this.selectedIndex].value)'>\n\t\t\t\t\t\t\t\t\t<option value=''>-- Font Size --</option>\n\t\t\t\t\t\t\t\t\t<option value='1'>Very Small</option>\n\t\t\t\t\t\t\t\t\t<option value='2'>Small</option>\n\t\t\t\t\t\t\t\t\t<option value='3'>Medium</option>\n\t\t\t\t\t\t\t\t\t<option value='4'>Large</option>\n\t\t\t\t\t\t\t\t\t<option value='5'>Larger</option>\n\t\t\t\t\t\t\t\t\t<option value='6'>Very Large</option>\n\t\t\t\t\t\t\t\t</select>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t<iframe name='editArea' id='editArea' style='width: 420px; height:160px; background: #FFFFFF;'></iframe>\n\t\t\t\t\t<input type='hidden' name='bodydata' value=''>\n\t\t\t\t\t<input type='hidden' name='counter' value='{$i}'>\n\t\t\t\t</td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[{$i}]' value='1'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[{$i}]'></td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t</tr>";
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $inv['subtot'];
    # Get Total
    $TOTAL = sprint($inv['total']);
    # Get vat
    $VAT = sprint($inv['vat']);
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    /* --- End Some calculations --- */
    $sel = "";
    if (isset($lead) and strlen($lead) > 0) {
        $sel = "checked='yes'";
    }
    $showdoc_html = "'" . str_replace("<div style=\"text-align: left;\"><br></div>", "", $bodydata) . "'";
    if (!isset($old_customer_select)) {
        $old_customer_select = "";
    }
    #get customers
    $get_cust = "SELECT cusnum, surname, vatnum, paddr1 FROM customers WHERE blocked = 'no' AND location = 'loc' ORDER BY cusname";
    $run_cust = db_exec($get_cust) or errDie("Unable to get customer information.");
    if (pg_numrows($run_cust) < 1) {
        $cust_drop = "<input type='hidden' name='customer_select' value=''>No Customers Found.";
    } else {
        $cust_drop = "<select name='customer_select' onChange=\"document.editForm.submit();\">";
        $cust_drop .= "<option value=''>Select Customer Or Enter Details</option>";
        while ($carr = pg_fetch_array($run_cust)) {
            if (isset($customer_select) and $customer_select == $carr['cusnum']) {
                $cust_drop .= "<option value='{$carr['cusnum']}' selected>{$carr['surname']}</option>";
                if ($old_customer_select != $customer_select) {
                    $inv['cusname'] = $carr['surname'];
                    $inv['cusaddr'] = $carr['paddr1'];
                    $inv['cusvatno'] = $carr['vatnum'];
                } else {
                    $inv['cusname'] = $cusname;
                    $inv['cusaddr'] = $cusaddr;
                    $inv['cusvatno'] = $cusvatno;
                }
            } else {
                $cust_drop .= "<option value='{$carr['cusnum']}'>{$carr['surname']}</option>";
            }
        }
        $cust_drop .= "</select>";
    }
    $details = "\n\t\t<script language='JavaScript'>\n\t\t\tfunction update() {\n\t\t\t\tdocument.editForm.bodydata.value = editArea.document.body.innerHTML;\n\t\t\t\tdocument.editForm.submit();\n\t\t\t}\n\t\t\tfunction Init() {\n\t\t\t\teditArea.document.designMode = 'On';\n\t\t\t\teditArea.document.body.innerHTML = {$showdoc_html};\n\t\t\t\teditArea.document.execCommand('justifyleft', false, null);\n\t\t\t}\n\t\t\tfunction controlSelOn(ctrl) {\n\t\t\t\tctrl.style.borderColor = '#000000';\n\t\t\t\tctrl.style.backgroundColor = '#B5BED6';\n\t\t\t\tctrl.style.cursor = 'hand';\n\t\t\t}\n\t\t\tfunction controlSelOff(ctrl) {\n\t\t\t\tctrl.style.borderColor = '#D6D3CE';\n\t\t\t\tctrl.style.backgroundColor = '#D6D3CE';\n\t\t\t}\n\t\t\tfunction controlSelDown(ctrl) {\n\t\t\t\tctrl.style.backgroundColor = '#8492B5';\n\t\t\t}\n\t\t\tfunction controlSelUp(ctrl) {\n\t\t\t\tctrl.style.backgroundColor = '#B5BED6';\n\t\t\t}\n\t\t\tfunction doBold() {\n\t\t\t\teditArea.document.execCommand('bold', false, null);\n\t\t\t}\n\t\t\tfunction doItalic() {\n\t\t\t\teditArea.document.execCommand('italic', false, null);\n\t\t\t}\n\t\t\tfunction doUnderline() {\n\t\t\t\teditArea.document.execCommand('underline', false, null);\n\t\t\t}\n\t\t\tfunction doLeft() {\n\t\t\t\teditArea.document.execCommand('justifyleft', false, null);\n\t\t\t}\n\t\t\tfunction doCenter() {\n\t\t\t\teditArea.document.execCommand('justifycenter', false, null);\n\t\t\t}\n\t\t\tfunction doRight() {\n\t\t\t\teditArea.document.execCommand('justifyright', false, null);\n\t\t\t}\n\t\t\tfunction doOrdList() {\n\t\t\t\teditArea.document.execCommand('insertorderedlist', false, null);\n\t\t\t}\n\t\t\tfunction doBulList() {\n\t\t\t\teditArea.document.execCommand('insertunorderedlist', false, null);\n\t\t\t}\n\t\t\tfunction doRule() {\n\t\t\t\teditArea.document.execCommand('inserthorizontalrule', false, null);\n\t\t\t}\n\t\t\tfunction doSize(fSize) {\n\t\t\t\tif(fSize != '')\n\t\t\t\t\teditArea.document.execCommand('fontsize', false, fSize);\n\t\t\t}\n\t\t\twindow.onload = Init;\n\t\t</script>\n\t\t<center>\n\t\t<h3>New Multi Line Quote</h3>\n\t\t<form action='" . SELF . "' method='POST' name='editForm' enctype='multipart/form-data'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='old_customer_select' value='{$customer_select}'>\n\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Customer</td>\n\t\t\t\t\t\t\t<td>{$cust_drop}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t\t\t<td valign='middle'><input type='text' name='cusname' value='{$inv['cusname']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t\t\t<td valign='middle'><textarea name='cusaddr' cols='18' rows='3'>{$inv['cusaddr']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Customer VAT No.</td>\n\t\t\t\t\t\t\t<td valign='middle'><input type='text' name='cusvatno' value='{$inv['cusvatno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Next Contact Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("ncdate", $ncdate_year, $ncdate_month, $ncdate_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Add As Lead</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='checkbox' name='lead' {$sel} value='yes'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Non-Stock Quote Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Non-Stock Quote No.</td>\n\t\t\t\t\t\t\t<td valign='center'>TI {$inv['invid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("nquo", $nquo_year, $nquo_month, $nquo_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='yes' {$chy}> No<input type='radio' size='7' name='chrgvat' value='no' {$chn}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='nons-quote-view.php'>View Non-Stock Quotes</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$inv['remarks']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='subtot' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$inv['vat']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input name='diffwhBtn' onClick='update();' type='submit' value='Add Item'> |</td>\n\t\t\t\t<td><input type='submit' name='upBtn' onClick='update();' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t</form>\n\t\t</center>";
    return $details;
}
コード例 #19
0
function details($_POST, $error = "")
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid Non-Stock Sales Order number.");
    } else {
        $invid = create_dummy(0);
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Get sales order info
    db_connect();
    $sql = "SELECT * FROM nons_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get sales order information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Sales Order Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # check if sales order has been printed
    if ($inv['done'] == "y") {
        $error = "<li class='err'> Error : sales order number <b>{$invid}</b> has already been printed.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    /* --- Start Drop Downs --- */
    # format date
    list($sord_year, $sord_month, $sord_day) = explode("-", $inv['odate']);
    # keep the charge vat option stable
    if ($inv['chrgvat'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chnone = "";
    } elseif ($inv['chrgvat'] == "no") {
        $chy = "";
        $chn = "checked=yes";
        $chnone = "";
    } else {
        $chy = "";
        $chn = "";
        $chnone = "checked=yes";
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>VAT Code</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM nons_inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $stkd['amt'] = round($stkd['amt'], 2);
        $chk = "";
        if ($stkd['vatex'] == 'y') {
            $chk = "checked=yes";
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl);
        $vats = "<select name='vatcodes[]'>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatex'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $vats .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $vats .= "</option>";
        $stkd["amt"] = sprint($stkd["amt"]);
        # put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='50' name='des[]' value='{$stkd['description']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t<td><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t<td align='center'>{$vats}</td>\n\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$i}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t</tr>";
        $i++;
    }
    # Look above(remprod keys)
    $keyy = $i;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    if ($i == 0 || isset($diffwhBtn)) {
        # add one
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'><input type='text' size='50' name='des[]' value=''></td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[]' value='1'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[]'></td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t</tr>";
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $inv['subtot'];
    # Get Total
    $TOTAL = sprint($inv['total']);
    # Get vat
    $VAT = sprint($inv['vat']);
    /* --- End Some calculations --- */
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    if (!isset($old_customer_select)) {
        $old_customer_select = "";
    }
    #get customers
    $get_cust = "SELECT cusnum, surname, vatnum, paddr1 FROM customers WHERE blocked = 'no' AND location = 'loc' ORDER BY cusname";
    $run_cust = db_exec($get_cust) or errDie("Unable to get customer information.");
    if (pg_numrows($run_cust) < 1) {
        $cust_drop = "<input type='hidden' name='customer_select' value=''>No Customers Found.";
    } else {
        $cust_drop = "<select name='customer_select' onChange=\"document.form.submit();\">";
        $cust_drop .= "<option value=''>Select Customer Or Enter Details</option>";
        while ($carr = pg_fetch_array($run_cust)) {
            if (isset($customer_select) and $customer_select == $carr['cusnum']) {
                $cust_drop .= "<option value='{$carr['cusnum']}' selected>{$carr['surname']}</option>";
                if ($old_customer_select != $customer_select) {
                    $inv['cusname'] = $carr['surname'];
                    $inv['cusaddr'] = $carr['paddr1'];
                    $inv['cusvatno'] = $carr['vatnum'];
                } else {
                    $inv['cusname'] = $cusname;
                    $inv['cusaddr'] = $cusaddr;
                    $inv['cusvatno'] = $cusvatno;
                }
            } else {
                $cust_drop .= "<option value='{$carr['cusnum']}'>{$carr['surname']}</option>";
            }
        }
        $cust_drop .= "</select>";
    }
    if (isset($diffwhBtn) or isset($upBtn) or isset($doneBtn) or isset($donePrnt)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details = "\n\t\t<center>\n\t\t<h3>New Non-Stock Sales Orders</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='old_customer_select' value='{$customer_select}'>\n\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Customer</td>\n\t\t\t\t\t\t\t<td>{$cust_drop}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t\t\t<td valign='middle'><input type='text' name='cusname' value='{$inv['cusname']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t\t\t<td valign='middle'><textarea name='cusaddr' cols='18' rows='3'>{$inv['cusaddr']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Customer VAT No.</td>\n\t\t\t\t\t\t\t<td valign='middle'><input type='text' name='cusvatno' value='{$inv['cusvatno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Customer Order No.</td>\n\t\t\t\t\t\t\t<td valign='middle'><input type='text' name='cusordno' value='{$inv['cusordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Non-Stock Sales Order Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Non-Stock Sales Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'>TI {$inv['invid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center' nowrap>" . mkDateSelect("sord", $sord_year, $sord_month, $sord_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='yes' {$chy}> No<input type='radio' size='7' name='chrgvat' value='no' {$chn}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td rowspan='2'>" . mkQuickLinks(ql("quote-view.php", "View Quotes"), ql("customers-new.php", "New Customer")) . "\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<th width='25%'>Comments</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols=20>{$inv['remarks']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='subtot' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$inv['vat']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' align='center'><input name='diffwhBtn' type='submit' value='Add Item'> | <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}
コード例 #20
0
function details($_POST, $error = "")
{
    extract($_POST);
    # validate input
    include "libs/validate.lib.php";
    $v = new validate();
    if (isset($calloutid)) {
        $v->isOk($calloutid, "num", 1, 20, "Invalid callout document number.");
    }
    if (isset($deptid)) {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    if (isset($letters)) {
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>{$e['msg']}</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($deptid)) {
        $deptid = 0;
    } else {
        if (isset($calloutid)) {
            db_conn("cubit");
            $sql = "UPDATE callout_docs SET deptid='{$deptid}' WHERE calloutid='{$calloutid}' AND deptid<>'{$deptid}'";
            db_exec($sql) or errDie("Error updating invoice department.");
        }
    }
    if (!isset($calloutid)) {
        db_conn("exten");
        #only create dummy if needed resources are available ...
        $get_cpeople = "SELECT * FROM calloutpeople";
        $run_cpeople = db_exec($get_cpeople) or errDie("Unable to get call out person information");
        if (pg_numrows($run_cpeople) < 1) {
            return "\n\t\t\t\t<li class='err'>No Call out People Found. Please add at least one.</li>\n\t\t\t\t<br>\n\t\t\t\t<table border=0 cellpadding='2' cellspacing='1' width=15%>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><a href='toms/calloutp-add.php'>Add Call Out Person</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>";
        }
        db_connect();
        $calloutid = create_dummy($deptid);
    }
    if (!isset($stkerr)) {
        $stkerr = "0,0";
    }
    if (!isset($done)) {
        $done = "";
    }
    # Get callout document info
    db_connect();
    $sql = "SELECT * FROM callout_docs WHERE calloutid = '{$calloutid}' AND div = '" . USER_DIV . "'";
    $docRslt = db_exec($sql) or errDie("Unable to get callout document information");
    if (pg_numrows($docRslt) < 1) {
        return "<li class='err'>Callout Document Not Found</li>";
    }
    $doc = pg_fetch_array($docRslt);
    # check if callout document has been printed
    if ($doc['accepted'] == "y") {
        $error = "<li class='err'> Error : Callout Document number <b>{$calloutid}</b> has already been printed.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$doc['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected customer info
    if (isset($letters)) {
        db_connect();
        $sql = "SELECT * FROM customers WHERE cusnum = '{$doc['cusnum']}' AND div = '" . USER_DIV . "'";
        $custRslt = db_exec($sql) or errDie("Unable to view customer");
        if (pg_numrows($custRslt) < 1) {
            db_connect();
            if ($inv['deptid'] == 0) {
                $searchdept = "";
            } else {
                $searchdept = "deptid = '{$doc['deptid']}' AND ";
            }
            # Query server for customer info
            $sql = "SELECT cusnum,cusname,surname FROM customers WHERE {$searchdept} location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND div = '" . USER_DIV . "' ORDER BY surname";
            $custRslt = db_exec($sql) or errDie("Unable to view customers");
            if (pg_numrows($custRslt) < 1) {
                $ajax_err = "<li class='err'>No customer names starting with <b>{$letters}</b> in database.</li>";
                //return view_err($_POST, $err);
            } else {
                $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
                $customers .= "<option value='-S' selected>Select Customer</option>";
                while ($cust = pg_fetch_array($custRslt)) {
                    $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
                }
                $customers .= "</select>";
            }
            # take care of the unset vars
            $cust['addr1'] = "";
            $cust['cusnum'] = "";
            $cust['accno'] = "";
        } else {
            $cust = pg_fetch_array($custRslt);
            # moarn if customer account has been blocked
            if ($cust['blocked'] == 'yes') {
                return "<li class='err'>Error : Selected customer account has been blocked.</li>";
            }
            $customers = "<input type='hidden' name='cusnum' value='{$cust['cusnum']}'>{$cust['cusname']}  {$cust['surname']}";
            $cusnum = $cust['cusnum'];
        }
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # get callout people
    db_conn("exten");
    $sql = "SELECT * FROM calloutpeople WHERE div = '" . USER_DIV . "' ORDER BY calloutp ASC";
    $run_sql = db_exec($sql) or errDie("Unable to get call out people from system.");
    if (pg_numrows($run_sql) < 1) {
        return "<li class='err'> There were no Call Out People found.</li>";
    } else {
        $calloutps = "<select name='calloutp'>";
        while ($arr = pg_fetch_array($run_sql)) {
            if ($doc['calloutp'] == $arr['calloutp']) {
                $calloutps .= "<option value='{$arr['calloutp']}' selected>{$arr['calloutp']}</option>";
            } else {
                $calloutps .= "<option value='{$arr['calloutp']}'>{$arr['calloutp']}</option>";
            }
        }
        $calloutps .= "</select>";
    }
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    # format date
    list($call_year, $call_month, $call_day) = explode("-", $doc['odate']);
    /* --- End Drop Downs --- */
    // get the ID of the first warehouse
    db_conn("exten");
    $sql = "SELECT whid FROM warehouses ORDER BY whid ASC LIMIT 1";
    $rslt = db_exec($sql) or errDie("Error reading warehouses (FWH).");
    if (pg_num_rows($rslt) > 0) {
        $FIRST_WH = pg_fetch_result($rslt, 0, 0);
    } else {
        $FIRST_WH = "-S";
    }
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>ITEM PRICE</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this callout document
    db_connect();
    $sql = "SELECT * FROM cubit.callout_docs_items  WHERE calloutid = '{$calloutid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        $stkd['unitcost'] = sprint($stkd['unitcost']);
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            db_conn('core');
            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
            $Accounts = "\n\t\t\t\t<select name='accounts[]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
            while ($ad = pg_fetch_array($Ri)) {
                if (isb($ad['accid'])) {
                    continue;
                }
                if ($ad['accid'] == $stkd['account']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
            }
            $Accounts .= "</select>";
            $sernos = "";
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
            $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>";
            # Put in product
            $products .= "\n\t\t\t\t<input type='hidden' name='whids[]' value='{$stkd['whid']}'>\n\t\t\t\t<input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>\n\t\t\t\t<input type='hidden' name='SCROLL' value='yes'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$stkd['description']}'> {$sernos}</td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'></td>\n\t\t\t\t</tr>";
            $key++;
        } else {
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            # put in product
            $products .= "\n\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t<input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>\n\t\t\t\t<input type='hidden' name='whids[]' value='{$stkd['whid']}'>\n\t\t\t\t<input type='hidden' name='SCROLL' value='yes'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t<td><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t\t<td><input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'></td>\n\t\t\t\t</tr>";
            $key++;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && isset($cust['pricelist'])) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # get price from price list if it is set
                if (isset($cust['pricelist'])) {
                    # get selected stock in this warehouse
                    db_conn("exten");
                    $sql = "SELECT price FROM plist_prices WHERE listid = '{$cust['pricelist']}' AND stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
                    $plRslt = db_exec($sql);
                    if (pg_numrows($plRslt) > 0) {
                        $pl = pg_fetch_array($plRslt);
                        $stk['selamt'] = $pl['price'];
                    }
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class=err>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate amount
                $amt[$key] = $qtyss[$key] * $stk['selamt'];
                $stk['selamt'] = sprint($stk['selamt']);
                # put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stk['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'></td>\n\t\t\t\t\t\t<td><input type='text' size='8' name='unitcost[]'  value='{$stk['selamt']}'></td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
            } elseif (isset($accountss[$key]) && $accountss[$key] != "0" && isset($cust['pricelist'])) {
                db_conn('core');
                $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                if (pg_num_rows($Ri) < 1) {
                    return "invalid.";
                }
                $ad = pg_fetch_array($Ri);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                # Input qty if not serialised
                $qtyin = "<input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'>";
                # Check permissions
                $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$unitcosts[$key]}'>";
                # Put in selected warehouse and stock
                $products .= "\n\t \t\t\t\t<input type='hidden' name='accounts[]' value='{$accountss[$key]}'>\n\t \t\t\t\t<input type='hidden' name='whids[]' value='0'>\n\t \t\t\t\t<input type='hidden' name='stkids[]' value='0'>\n\t \t\t\t\t<input type='hidden' name='disc[]' value='0'>\n\t \t\t\t\t<input type='hidden' name='discp[]' value='0'>\n\t \t\t\t\t<tr class='" . bg_class() . "'>\n\t \t\t\t\t\t<td colspan='2'>{$ad['accname']}</td>\n\t \t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$descriptionss[$key]}'></td>\n\t \t\t\t\t\t<td>{$qtyin}</td>\n\t \t\t\t\t\t<td>{$viewcost}</td>\n\t \t\t\t\t\t<td><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t \t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t \t\t\t\t</tr>";
                $keyy++;
            } else {
                if (!isset($diffwhBtn)) {
                    # skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    if (!isset($addnon)) {
                        # get warehouse name
                        db_conn("exten");
                        $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                        $whRslt = db_exec($sql);
                        $wh = pg_fetch_array($whRslt);
                        # get stock on this warehouse
                        db_connect();
                        $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                        $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                        if (pg_numrows($stkRslt) < 1) {
                            $error .= "<li class='err'>There are no stock items in the selected store.</li>";
                            continue;
                        }
                        if ($sel_frm == "stkcod") {
                            $cods = "\n\t\t\t\t\t\t\t\t<select class='width:15' name='stkidss[]' onChange='javascript:document.form.submit();'>\n\t\t\t\t\t\t\t\t\t<option value='-S' disabled selected>Select Number</option>";
                            $count = 0;
                            while ($stk = pg_fetch_array($stkRslt)) {
                                $cods .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                            }
                            $cods .= "</select> ";
                            $descs = "";
                        } else {
                            $descs = "<select class='width:15' name='stkidss[]' onChange='javascript:document.form.submit();'>";
                            $descs .= "<option value='-S' disabled selected>Select Description</option>";
                            $count = 0;
                            while ($stk = pg_fetch_array($stkRslt)) {
                                $descs .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                            }
                            $descs .= "</select> ";
                            $cods = "";
                        }
                        # put in drop down and warehouse
                        $products .= "\n\t\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t\t<td>{$cods}</td>\n\t\t\t\t\t\t\t\t<td>{$descs}</td>\n\t\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]'  value='1'></td>\n\t\t\t\t\t\t\t\t<td><input type='hidden' name='amts[]' value='0.00'></td>\n\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t</tr>";
                    }
                }
            }
        }
    } else {
        if (!(isset($diffwhBtn) || isset($addnon))) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                if (isset($wtd) && $wtd != 0) {
                    $whid = $wtd;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected warehouse.</li>";
                }
                $stks = "\n\t\t\t\t\t<select class='width:15' name='stkidss[]' onChange='javascript:document.form.submit();'>\n\t\t\t\t\t\t<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "\n\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t</tr>";
            } else {
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t</tr>";
            }
        } else {
            if (isset($addnon)) {
                db_conn('core');
                $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                $Accounts = "\n\t\t\t\t<select name='accountss[]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
                while ($ad = pg_fetch_array($Ri)) {
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                }
                $Accounts .= "</select>";
                $products .= "\n\t\t\t\t<input type='hidden' name='whidss[]' value='{$FIRST_WH}'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t</tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$whs}</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td></td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td> </td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td></td>\n\t\t\t</tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($doc['subtot']);
    # Calculate subtotal
    $SUBTOT = sprint($doc['subtot']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    // Which display method was selected
    if (isset($sel_frm) && $sel_frm == "stkdes") {
        $sel_frm_cod = "";
        $sel_frm_des = "checked";
    } else {
        $sel_frm_cod = "checked";
        $sel_frm_des = "";
    }
    /*--- Start checks --- */
    $details_begin = "\n\t\t<center>\n\t\t<h3>New CallOut </h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='calloutid' value='{$calloutid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n \t\t\t<tr>\n \t\t\t\t<td valign='top'>\n \t\t\t\t\t<div id='cust_selection'>";
    if (empty($ajax_err) && (isset($cusnum) || AJAX)) {
        if (isset($cusnum)) {
            $OTS_OPT = onthespot_encode(SELF, "cust_selection", "deptid={$doc['deptid']}&letters={$letters}&cusnum={$cusnum}&calloutid={$calloutid}");
            $custedit = "\n\t\t\t\t<td nowrap>\n\t\t\t\t\t<a href='javascript: popupSized(\"cust-edit.php?cusnum={$cusnum}&onthespot={$OTS_OPT}\", \"edit_cust\", 700, 630);'>\n\t\t\t\t\t\tEdit Customer Details\n\t\t\t\t\t</a>\n\t\t\t\t</td>";
        } else {
            $custedit = "";
        }
        $ajaxOut = "\n\t\t\t<input type='hidden' name='letters' value='{$letters}'>\n\t\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t<td valign='center'>{$customers}</td>\n\t\t\t\t\t{$custedit}\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t<td valign='center'>" . nl2br($cust['addr1']) . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer VAT Number</td>\n\t\t\t\t\t<td>{$cust['vatnum']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Using</td>\n\t\t\t\t\t<td>Stock Code<input type='radio' name='sel_frm' value='stkcod' onChange='javascript:document.form.submit();' {$sel_frm_cod}><br>Stock Description<input type='radio' name='sel_frm' value='stkdes' onChange='javascript:document.form.submit();' {$sel_frm_des}></td>\n\t\t\t\t</tr>\n\t\t\t</table>";
    } else {
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE div = '" . USER_DIV . "' ORDER BY deptname ASC";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($deptRslt) < 1) {
            return "<li class='err'>There are no Departments found in Cubit.";
        } else {
            $depts = "<select id='deptid'>";
            $depts .= "<option value='0'>All Departments</option>";
            while ($dept = pg_fetch_array($deptRslt)) {
                $depts .= "<option value='{$dept['deptid']}'>{$dept['deptname']}</option>";
            }
            $depts .= "</select>";
        }
        if (!isset($ajax_err)) {
            $ajax_err = "";
        }
        $ajaxOut = "\n\t\t\t<script>\n\t\t\t\tfunction updateCustSelection() {\n\t\t\t\t\tdeptid = getObject('deptid').value;\n\t\t\t\t\tletters = getObject('letters').value;\n\t\t\t\t\tajaxRequest('" . SELF . "', 'cust_selection', AJAX_SET, 'letters='+letters+'&deptid='+deptid+'&calloutid={$calloutid}');\n\t\t\t\t}\n\t\t\t</script>\n\n\t\t\t{$ajax_err}\n\t\t\t<table " . TMPL_tblDflts . " width='400'>\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'>New Callout Document</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>" . REQ . "Select Department</td>\n\t\t\t\t\t<td valign='center'>{$depts}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>First Letters of customer</td>\n\t\t\t\t\t<td valign='center'><input type='text' size='5' id='letters' maxlength='5'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td valign='center'><input type='button' value='Update &raquo' onClick='updateCustSelection();'></td>\n\t\t\t\t</tr>\n\t\t\t</table>";
    }
    if (isset($diffwhBtn) or isset($addprodBtn) or isset($addnon) or isset($saveBtn) or isset($upBtn) or isset($doneBtn)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details_end = "\n\t\t\t\t</div>\n\t\t\t</td>\n\t\t\t<td valign='top' align='right'>\n\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th colspan='2'>Callout Document Details</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Job Number</td>\n\t\t\t\t\t\t<td valign='center'>{$doc['calloutid']}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Call Out Person</td>\n\t\t\t\t\t\t<td valign='center'>{$calloutps}</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Date Service Required</td>\n\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("call", $call_year, $call_month, $call_day) . "</td>\n\t\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Call Out Rate</td>\n\t\t\t\t\t\t<td valign='center' nowrap>" . CUR . " <input type='text' name='def_travel' value='{$doc['def_travel']}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Labour Rate/Hour</td>\n\t\t\t\t\t\t<td valign='center' nowrap>" . CUR . " <input type='text' name='def_labour' value='{$doc['def_labour']}'></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>&nbsp;</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td colspan='2'>{$products}</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td colspan='4'><textarea name='sign' cols='80' rows='2'>{$doc['sign']}</textarea></td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>\n\t\t\t<p>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t<th width='25%'>Description Of Callout</th>\n\t\t\t\t\t<th width='25%'>Comments</th>\n\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td class='" . bg_class() . "'><a href='callout-new.php'>New Callout Document</a></td>\n\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='calloutdescrip' rows='4' cols='20'>{$doc['calloutdescrip']}</textarea></td>\n\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='comm' rows='4' cols='20'>{$doc['comm']}</textarea></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><a href='callout-view.php'>View Callout Documents</a></td>\n\t\t\t\t</tr>\n\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t</table>\n\t\t\t</td>\n\t\t\t<td align='right'></td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td align='right'>\n\t\t\t\t<input name='diffwhBtn' type='submit' value='Different Store'> |\n\t\t\t\t<input name='addprodBtn' type='submit' value='Add Product'> |\n\t\t\t\t<input name='addnon' type='submit' value='Add Non stock Product'> |\n\t\t\t\t<input type='submit' name='saveBtn' value='Save'></td><td> |\n\t\t\t\t<input type='submit' name='upBtn' value='Update'>{$done}\n\t\t\t</td>\n\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    if (AJAX) {
        return $ajaxOut;
    } else {
        return "{$details_begin}{$ajaxOut}{$details_end}";
    }
}
コード例 #21
0
function details($_POST, $error = "")
{
    extract($_POST);
    # validate input
    include "libs/validate.lib.php";
    $v = new validate();
    if (isset($sordid)) {
        $v->isOk($sordid, "num", 1, 20, "Invalid sales order number.");
    }
    if (isset($deptid)) {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    if (isset($letters)) {
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>{$e['msg']}</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($sordid)) {
        $sordid = create_dummy($deptid);
        $stkerr = "0,0";
    }
    if (!isset($proforma)) {
        $proforma = "";
    }
    if (!isset($done)) {
        $done = "";
    }
    # Get Sales Order info
    db_connect();
    $sql = "SELECT * FROM sorders WHERE sordid = '{$sordid}' AND div = '" . USER_DIV . "'";
    $sordRslt = db_exec($sql) or errDie("Unable to get Sales Order information");
    if (pg_numrows($sordRslt) < 1) {
        return "<li class='err'>Sales Order Not Found</li>";
    }
    $sord = pg_fetch_array($sordRslt);
    # check if Sales Order has been printed
    if ($sord['accepted'] == "y") {
        $error = "<li class='err'> Error : Sales Order number <b>{$sordid}</b> has already been printed.";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$sord['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected customer info
    db_connect();
    $sql = "SELECT * FROM customers WHERE cusnum = '{$sord['cusnum']}' AND div = '" . USER_DIV . "'";
    $custRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($custRslt) < 1) {
        db_connect();
        if ($deptid == "0") {
            $searchdept = "";
        } else {
            $searchdept = "deptid = '{$sord['deptid']}' AND ";
        }
        # Query server for customer info
        $sql = "SELECT cusnum, cusname, surname FROM customers WHERE {$searchdept} location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $err = "<li class='err'>No customer names starting with <b>{$letters}</b> in database.</li>";
            return view_err($_POST, $err);
        } else {
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            $customers .= "<option value='-S' selected>Select Customer</option>";
            while ($cust = pg_fetch_array($custRslt)) {
                $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
            }
            $customers .= "</select>";
        }
        # take care of the unset vars
        $cust['addr1'] = "";
        $cust['cusnum'] = "";
        $cust['vatnum'] = "";
        $cust['accno'] = "";
    } else {
        $cust = pg_fetch_array($custRslt);
        # moarn if customer account has been blocked
        if ($cust['blocked'] == 'yes') {
            return "<li class='err'>Error : Selected customer account has been blocked.</li>";
        }
        $customers = "<input type='hidden' name='cusnum' value='{$cust['cusnum']}'>{$cust['cusname']}  {$cust['surname']}";
        $cusnum = $cust['cusnum'];
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    //	$whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $whs = "<select name='whidss[]'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            if (!user_in_store_team($wh["whid"], USER_ID)) {
                continue;
            }
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class='err'> There are no Sales People found in Cubit.</li>";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $sord['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $sord['terms']);
    # Keep the charge vat option stable
    if ($sord['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($sord['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    if ($sord["display_costs"] == "yes") {
        $dc_sel["yes"] = "checked";
        $dc_sel["no"] = "";
    } else {
        $dc_sel["yes"] = "";
        $dc_sel["no"] = "checked";
    }
    # format date
    list($sord_year, $sord_month, $sord_day) = explode("-", $sord['odate']);
    list($ddate_year, $ddate_month, $ddate_day) = explode("-", $sord["ddate"]);
    /* --- End Drop Downs --- */
    // get the ID of the first warehouse
    db_conn("exten");
    $sql = "SELECT whid FROM warehouses ORDER BY whid ASC LIMIT 1";
    $rslt = db_exec($sql) or errDie("Error reading warehouses (FWH).");
    if (pg_num_rows($rslt) > 0) {
        $FIRST_WH = pg_fetch_result($rslt, 0, 0);
    } else {
        $FIRST_WH = "-S";
    }
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>UNIT DISCOUNT</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t</tr>";
    # get selected stock in this Sales Order
    db_connect();
    $sql = "SELECT * FROM sorders_items  WHERE sordid = '{$sordid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            $Accounts = "\n\t\t\t\t<select name='accounts[]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
            $useaccdrop = getCSetting("USE_NON_STOCK_ACCOUNTS");
            if (isset($useaccdrop) and $useaccdrop == "yes") {
                db_connect();
                $acc_sql = "SELECT * FROM non_stock_account_list ORDER BY accname";
                $run_acc = db_exec($acc_sql) or errDie("Unable to get account information.");
                if (pg_numrows($run_acc) > 0) {
                    while ($acc = pg_fetch_array($run_acc)) {
                        if ($acc['accid'] == $stkd['account']) {
                            $Accounts .= "<option value='{$acc['accid']}' selected>{$acc['accname']}</option>";
                        } else {
                            $Accounts .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
                        }
                    }
                    $Accounts .= "</select>";
                }
            } else {
                db_conn('core');
                $Sl = "SELECT accid, topacc, accnum, accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                while ($ad = pg_fetch_array($Ri)) {
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    if ($ad['accid'] == $stkd['account']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
                }
                $Accounts .= "</select>";
            }
            $sernos = "";
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'>";
            $stkd['unitcost'] = sprint($stkd['unitcost']);
            $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'>";
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            $stkd['amt'] = sprint($stkd['amt']);
            # Put in product
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>\n\t\t\t\t\t\t{$Accounts}\n\t\t\t\t\t\t<input type='hidden' name='whids[]' value='{$stkd['whid']}'>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$stkd['description']}'> {$sernos}</td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td><input type='hidden' name='disc[]' value='{$stkd['disc']}'><input type='hidden' name='discp[]' value='{$stkd['discp']}'></td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t</tr>";
            $key++;
        } else {
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            # get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[]'>";
            //<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            $stkd['unitcost'] = sprint($stkd['unitcost']);
            $stkd['amt'] = sprint($stkd['amt']);
            //			$sql = "SELECT * FROM manufact.jobcards WHERE recipe!='yes' AND completion!='1'";
            //			$job_rslt = db_exec($sql) or errDie("Unable to retrieve jobs.");
            //
            //			$job_sel = "<select name='job_id[]' style='width: 100%'>";
            //			while ($job_data = pg_fetch_array($job_rslt)) {
            //				if ($stkd["jobcard_id"] == $job_data["id"]) {
            //					$sel = "selected";
            //				} else {
            //					$sel = "";
            //				}
            //
            //				$job_sel .= "<option value='$job_data[id]' $sel>
            //					$job_data[id] $job_data[description]
            //				</option>";
            //			}
            //			$job_sel .= "</select>";
            //	<tr>
            //				<td bgcolor='#ff0000' width='10%'>
            //					$job_sel<br />
            //					<input type='submit' name='pur[]' value='Add To Purchase Resource Planning'>
            //				</td>
            //			</tr>
            // rowspan='2'
            # put in product
            $products .= "\n\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t<input type='hidden' name='amt[]' value='{$stkd['amt']}'>\n\t\t\t\t<input type='hidden' name='pqty[{$stk['stkid']}]' value='{$stkd['qty']}' />\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t\t<td><input type='text' size='8' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t\t<td><input type='text' size='4' name='disc[]' value='{$stkd['disc']}'> OR <input type='text' size='4' name='discp[]' value='{$stkd['discp']}' maxlength=5>%</td>\n\t\t\t\t\t<td>" . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t\t</tr>";
            $key++;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    #get negative stock setting
    $neg_setting = getCsetting("SORDER_NEG_STOCK");
    if (!isset($neg_setting) or strlen($neg_setting) < 1) {
        $neg_setting = "yes";
    }
    if ($neg_setting == "yes") {
        $search_neg_stock = "";
    } else {
        $search_neg_stock = " AND (units > 0) ";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && isset($cust['pricelist'])) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$stk['whid']}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get price from price list if it is set
                if (isset($cust['pricelist'])) {
                    # get selected stock in this warehouse
                    db_conn("exten");
                    $sql = "SELECT price FROM plist_prices WHERE listid = '{$cust['pricelist']}' AND stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
                    $plRslt = db_exec($sql);
                    if (pg_numrows($plRslt) > 0) {
                        $pl = pg_fetch_array($plRslt);
                        $stk['selamt'] = $pl['price'];
                    }
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class='err'>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $amt[$key] = sprint($amt[$key]);
                $stk['selamt'] = sprint($stk['selamt']);
                # put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t\t<input type='hidden' name='whids[]' value='{$whid}'>\n\t\t\t\t\t<input type='hidden' name='stkids[]' value='{$stk['stkid']}'>\n\t\t\t\t\t<input type='hidden' name='amt[]' value='{$amt[$key]}'>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$wh['whname']}</td>\n\t\t\t\t\t\t<td><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'></td>\n\t\t\t\t\t\t<td><input type='text' size='8' name='unitcost[]' value='{$stk['selamt']}'></td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<input type='text' size='4' name='disc[]' value='{$discs[$key]}'>\n\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t<input type='text' size='4' name='discp[]' value='{$discps[$key]}' maxlength=5>%\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
            } else {
                if (isset($accountss[$key]) && $accountss[$key] != "0" && isset($cust['pricelist'])) {
                    db_conn('core');
                    $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                    $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                    if (pg_num_rows($Ri) < 1) {
                        return "invalid.";
                    }
                    $ad = pg_fetch_array($Ri);
                    # Calculate amount
                    $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                    # Input qty if not serialised
                    $qtyin = "<input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'>";
                    # Check permissions
                    $unitcosts[$key] = sprint($unitcosts[$key]);
                    $viewcost = "<input type='text' size='8' name='unitcost[]' value='{$unitcosts[$key]}'>";
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vatcodess[$key] == $vd['id']) {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    $amt[$key] = sprint($amt[$key]);
                    # Put in selected warehouse and stock
                    $products .= "\n\t\t\t\t\t<input type='hidden' name='accounts[]' value='{$accountss[$key]}'>\n\t\t\t\t\t<input type='hidden' name='whids[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='stkids[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='disc[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='discp[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='amt[]' value='{$amt[$key]}'>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='2'>{$ad['accname']}</td>\n\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$descriptionss[$key]}'></td>\n\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td nowrap> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                    $keyy++;
                } else {
                    # skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    if (!isset($addnon)) {
                        if (isset($filter_store) and $filter_store != "0") {
                            # get warehouse name
                            db_conn("exten");
                            $sql = "SELECT whname FROM warehouses WHERE whid = '{$filter_store}' AND div = '" . USER_DIV . "'";
                            $whRslt = db_exec($sql);
                            $wh = pg_fetch_array($whRslt);
                        }
                        # get stock on this warehouse
                        db_connect();
                        if (isset($ria) and $ria != "") {
                            $len = strlen($ria);
                            if ($ria == "Show All") {
                                $Wh = "";
                                $ria = "";
                            } else {
                                $Wh = "AND (lower(stkdes) LIKE lower('%{$ria}%')) OR (lower(stkcod) LIKE lower('%{$ria}%'))";
                                $ria = "";
                            }
                        } else {
                            $Wh = "AND FALSE";
                            $ria = "";
                        }
                        $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
                        if (isset($check_setting) and $check_setting == "yes") {
                            if (isset($filter_class) and $filter_class != "0") {
                                $Wh .= " AND prdcls = '{$filter_class}'";
                            }
                            if (isset($filter_cat) and $filter_cat != "0") {
                                $Wh .= " AND catid = '{$filter_cat}'";
                            }
                        }
                        if (isset($filter_store) and $filter_store != "0") {
                            $Wh .= " AND whid = '{$filter_store}'";
                        }
                        $sql = "SELECT * FROM stock WHERE blocked = 'n' {$search_neg_stock} AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
                        $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                        if (pg_numrows($stkRslt) < 1) {
                            $error .= "<li class='err'>There are no stock items in the selected store.</li>";
                            continue;
                        }
                        if ($sel_frm == "stkcod") {
                            $cods = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                            $cods .= "<option value='-S' disabled selected>Select Number</option>";
                            $count = 0;
                            while ($stk = pg_fetch_array($stkRslt)) {
                                $cods .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                            }
                            $cods .= "</select> ";
                            $descs = "";
                        } else {
                            $descs = "<select style='width:250px' name='stkidss[]' onChange='javascript:document.form.submit();'>";
                            $descs .= "<option value='-S' disabled selected>Select Description</option>";
                            $count = 0;
                            while ($stk = pg_fetch_array($stkRslt)) {
                                $descs .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                            }
                            $descs .= "</select> ";
                            $cods = "";
                        }
                        db_conn('cubit');
                        $Sl = "SELECT * FROM vatcodes ORDER BY code";
                        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                        $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                        while ($vd = pg_fetch_array($Ri)) {
                            if ($vd['del'] == "Yes") {
                                $sel = "selected";
                            } else {
                                $sel = "";
                            }
                            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                        }
                        $Vatcodes .= "</select>";
                        # put in drop down and warehouse
                        $products .= "\n\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t\t<input type='hidden' name='whidss[]' value='{$filter_store}'>\n\t\t\t\t\t\t<input type='hidden' name='amts[]' value='0.00'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t<td>{$cods}<input type='hidden' name='vatcodess' value='0'></td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>{$descs}</td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]'  value='1'></td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t" . CUR . " <input type='text' size='4' name='discs[]' value='0'>\n\t\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t\t<input type='text' size='4' name='discps[]' value='0' maxlength='5'>%\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t</tr>";
                    } else {
                        $Accounts = "\n\t\t\t\t\t\t<select name='accountss[]'>\n\t\t\t\t\t\t\t<option value='0'>Select Account</option>";
                        $useaccdrop = getCSetting("USE_NON_STOCK_ACCOUNTS");
                        if (isset($useaccdrop) and $useaccdrop == "yes") {
                            db_connect();
                            $acc_sql = "SELECT * FROM non_stock_account_list ORDER BY accname";
                            $run_acc = db_exec($acc_sql) or errDie("Unable to get account information.");
                            if (pg_numrows($run_acc) > 0) {
                                while ($acc = pg_fetch_array($run_acc)) {
                                    $Accounts .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
                                }
                                $Accounts .= "</select>";
                            }
                        } else {
                            db_conn('core');
                            $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                            $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                            while ($ad = pg_fetch_array($Ri)) {
                                if (isb($ad['accid'])) {
                                    continue;
                                }
                                $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                            }
                            $Accounts .= "</select>";
                        }
                        db_conn('cubit');
                        $Sl = "SELECT * FROM vatcodes ORDER BY code";
                        $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                        $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                        while ($vd = pg_fetch_array($Ri)) {
                            if ($vd['del'] == "Yes") {
                                $sel = "selected";
                            } else {
                                $sel = "";
                            }
                            $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                        }
                        $Vatcodes .= "</select>";
                        $products .= "\n\t\t\t\t\t\t<input type='hidden' name='whidss[]' value='{$FIRST_WH}'>\n\t\t\t\t\t\t<inpu type='hidden' name='stkidss[]' value=''>\n\t\t\t\t\t\t<input type='hidden' name='discs[]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='discps[]' value='0' >\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td colspan='2'>{$Accounts}</td>\n\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t</tr>";
                    }
                }
            }
        }
    } else {
        if (!isset($addnon)) {
            if (isset($filter_store) and $filter_store != "0") {
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$filter_store}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
            }
            # get stock on this warehouse
            db_connect();
            if (isset($ria) and $ria != "") {
                $len = strlen($ria);
                if ($ria == "Show All") {
                    $Wh = "";
                    $ria = "";
                } else {
                    $Wh = "AND (lower(stkdes) LIKE lower('%{$ria}%')) OR (lower(stkcod) LIKE lower('%{$ria}%'))";
                    $ria = "";
                }
            } else {
                $Wh = "AND FALSE";
                $ria = "";
            }
            $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
            if (isset($check_setting) and $check_setting == "yes") {
                if (isset($filter_class) and $filter_class != "0") {
                    $Wh .= " AND prdcls = '{$filter_class}'";
                }
                if (isset($filter_cat) and $filter_cat != "0") {
                    $Wh .= " AND catid = '{$filter_cat}'";
                }
            }
            if (isset($filter_store) and $filter_store != "0") {
                $Wh .= " AND whid = '{$filter_store}'";
            }
            $sql = "SELECT * FROM stock WHERE blocked = 'n' {$search_neg_stock} AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
            $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
            if (pg_numrows($stkRslt) < 1) {
                if (!isset($err)) {
                    $err = "";
                }
                $err .= "<li>There are no stock items in the selected warehouse.</li>";
            }
            $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
            $stks .= "<option value='-S' disabled selected>Select Number</option>";
            $count = 0;
            while ($stk = pg_fetch_array($stkRslt)) {
                $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
            }
            $stks .= "</select> ";
            $products .= "\n\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t<input type='hidden' name='vatcodess[]' value=''>\n\t\t\t\t<input type='hidden' name='whidss[]' value='{$filter_store}'>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='text' size='4' name='discs[]' value='0'>\n\t\t\t\t\t\tOR\n\t\t\t\t\t\t<input type='text' size='4' name='discps[]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t</tr>";
        } else {
            if (isset($addnon)) {
                $Accounts = "\n\t\t\t\t<select name='accountss[]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
                $useaccdrop = getCSetting("USE_NON_STOCK_ACCOUNTS");
                if (isset($useaccdrop) and $useaccdrop == "yes") {
                    db_connect();
                    $acc_sql = "SELECT * FROM non_stock_account_list ORDER BY accname";
                    $run_acc = db_exec($acc_sql) or errDie("Unable to get account information.");
                    if (pg_numrows($run_acc) > 0) {
                        while ($acc = pg_fetch_array($run_acc)) {
                            $Accounts .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
                        }
                        $Accounts .= "</select>";
                    }
                } else {
                    db_conn('core');
                    $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                    $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                    while ($ad = pg_fetch_array($Ri)) {
                        if (isb($ad['accid'])) {
                            continue;
                        }
                        $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                    }
                    $Accounts .= "</select>";
                }
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t<select name='vatcodess[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vd['del'] == "Yes") {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $products .= "\n\t\t\t\t<inpu type='hidden' name='stkidss[]' value=''>\n\t\t\t\t<input type='hidden' name='whidss[]' value='{$FIRST_WH}'>\n\t\t\t\t<input type='hidden' name='discs[]' value='0'>\n\t\t\t\t<input type='hidden' name='discps[]' value='0' >\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}</td>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[]'></td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t<td><input type='text' name='unitcosts[]' size='7'></td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t</tr>";
            }
        }
    }
    // 	$products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($sord['subtot']);
    # Calculate tradediscm
    if ($sord['traddisc'] > 0) {
        $traddiscm = sprint($sord['traddisc'] / 100 * ($SUBTOT + $sord['delchrg']));
    } else {
        $traddiscm = "0.00";
    }
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($sord['subtot']);
    $VAT = sprint($sord['vat']);
    $TOTAL = sprint($sord['total']);
    $sord['delchrg'] = sprint($sord['delchrg']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    # check only if the customer is selected
    if (isset($cusnum) && $cusnum != "-S") {
        #check againg credit limit
        if ($cust['credlimit'] != 0 && $TOTAL + $cust['balance'] > $cust['credlimit']) {
            $error .= "<li class='err'>Warning : Customers Credit limit of <b>" . CUR . " {$cust['credlimit']}</b> has been exceeded:</li>";
        }
        $avcred = $cust['credlimit'] - $cust['balance'];
    } else {
        $avcred = "0.00";
    }
    /*--- Start checks --- */
    if (!isset($letters)) {
        $letters = "";
    }
    $sord['delvat'] += 0;
    if ($sord['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $sord['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $sord['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    $psel1 = "";
    $psel2 = "";
    if (isset($proforma) and $proforma == "yes") {
        $psel1 = "checked=yes";
    } else {
        $psel2 = "checked=yes";
    }
    if ($proforma == "yes") {
        #generate a unique id IF it hasnt been done yet
        if (!isset($pinvnum) or $pinvnum == 0) {
            pglib_transaction("BEGIN") or errDie("Could not start database transaction");
            #get unique id
            $get_uni = "INSERT INTO unique_id (entry) VALUES ('value')";
            $run_uni = db_exec($get_uni) or errDie("Unable to get unique id");
            $pinvnum = pglib_lastid("unique_id", "id");
            $rem_sql = "DELETE FROM unique_id WHERE id = '{$pinvnum}'";
            $run_rem = db_exec($rem_sql) or errDie("Unable to remove unique id check");
            #further check to see if an invoice has this is should be done here...
            pglib_transaction("COMMIT") or errDie("Could not commit database transaction");
        }
        $getpinvnum = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Proforma Invoice Number</td>\n\t\t\t\t<td><input type='hidden' name='pinvnum' value='{$pinvnum}'>{$pinvnum}</td>\n\t\t\t</tr>";
    } else {
        $getpinvnum = "<input type='hidden' name='pinvnum' value='0'>";
    }
    // Which display method was selected
    if (isset($sel_frm) && $sel_frm == "stkdes") {
        $sel_frm_cod = "";
        $sel_frm_des = "checked";
    } else {
        $sel_frm_cod = "checked";
        $sel_frm_des = "";
    }
    // Retrieve VAT Setting
    $sql = "SELECT value FROM cubit.settings WHERE constant='VAT_INC'";
    $vatinc_rslt = db_exec($sql) or errDie("Unable to retrieve vat setting.");
    $vatinc = pg_fetch_result($vatinc_rslt, 0);
    if ($vatinc != "yes" && $vatinc != "no") {
        $vatinc = "no";
    }
    if ($vatinc == 'yes') {
        $chrgvat = "inc";
    } else {
        $chrgvat = "exc";
    }
    if (isset($diffwhBtn) or isset($addprodBtn) or isset($addnon) or isset($saveBtn) or isset($upBtn) or isset($doneBtn) or isset($donePrnt) or isset($ria)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $optional_filter_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
    if (isset($optional_filter_setting) and $optional_filter_setting == "yes") {
        db_connect();
        $catsql = "SELECT catid, cat, catcod FROM stockcat WHERE div = '" . USER_DIV . "' ORDER BY cat ASC";
        $catRslt = db_exec($catsql);
        if (pg_numrows($catRslt) < 1) {
            $cat_drop = "<input type='hidden' name='filter_cat' value='0'>";
        } else {
            $cat_drop = "<select name='filter_cat'>";
            $cat_drop .= "<option value='0'>All Categories</option>";
            while ($cat = pg_fetch_array($catRslt)) {
                if (isset($filter_cat) and $filter_cat == $cat['catid']) {
                    $cat_drop .= "<option value='{$cat['catid']}' selected>({$cat['catcod']}) {$cat['cat']}</option>";
                } else {
                    $cat_drop .= "<option value='{$cat['catid']}'>({$cat['catcod']}) {$cat['cat']}</option>";
                }
            }
            $cat_drop .= "</select>";
        }
        # Select classification
        $classsql = "SELECT * FROM stockclass WHERE div = '" . USER_DIV . "' ORDER BY classname ASC";
        $clasRslt = db_exec($classsql);
        if (pg_numrows($clasRslt) < 1) {
            $class_drop = "<input type='hidden' name='filter_class' value='0'>";
        } else {
            $class_drop = "<select name='filter_class' style='width: 167'>";
            $class_drop .= "<option value='0'>All Classifications</option>";
            while ($clas = pg_fetch_array($clasRslt)) {
                if (isset($filter_class) and $filter_class == $clas['clasid']) {
                    $class_drop .= "<option value='{$clas['clasid']}' selected>{$clas['classname']}</option>";
                } else {
                    $class_drop .= "<option value='{$clas['clasid']}'>{$clas['classname']}</option>";
                }
            }
            $class_drop .= "</select>";
        }
        $display_optional_filters = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Select Category</td>\n\t\t\t\t<td>{$cat_drop}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Select Classification</td>\n\t\t\t\t<td>{$class_drop}</td>\n\t\t\t</tr>";
    }
    db_conn("exten");
    $sql = "SELECT whid, whname, whno FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        $store_drop = "<input type='hidden' name='filter_store' value='0'>";
    } else {
        if (!isset($filter_store)) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $filter_store = $set['value'];
            }
        }
        $store_drop = "<select name='filter_store'>";
        $store_drop .= "<option value='0'>All Stores</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            if (isset($filter_store) and $filter_store == $wh['whid']) {
                $store_drop .= "<option value='{$wh['whid']}' selected>({$wh['whno']}) {$wh['whname']}</option>";
            } else {
                $store_drop .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
            }
        }
        $store_drop .= "</select>";
    }
    $cust_del_addr = $sord['del_addr'];
    $details = "\n\t\t<center>\n\t\t<h3>New Sales Order</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='sordid' value='{$sordid}'>\n\t\t\t<input type='hidden' name='letters' value='{$letters}'>\n\t\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t\t<input type='hidden' name='chrgvat' value='{$chrgvat}' />\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Account No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$cust['accno']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t\t\t<td valign='center'>{$customers}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t\t\t<td valign='center'>" . nl2br($cust['addr1']) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Current Delivery Address</td>\n\t\t\t\t\t\t\t<td valign='center'>" . nl2br($cust_del_addr) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Delivery Address</td>\n\t\t\t\t\t\t\t<td valign='center'>\n\t\t\t\t\t\t\t\t<input type='button' onClick=\"\n\t\t\t\t\t\t\t\t\tvar windowReference;\n\t\t\t\t\t\t\t\t\tfunction openPopup() {\n\t\t\t\t\t\t\t\t\t\twindowReference = window.open('sorder-new-deladdr.php?sordid={$sordid}','windowName','width=500,height=400,status=1');\n\t\t\t\t\t\t\t\t\t\tif (!windowReference.opener)\n\t\t\t\t\t\t\t\t\t\t\twindowReference.opener = self;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\topenPopup();\" value='Change Delivery Address'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer Order number</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$sord['cordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer VAT Number</td>\n\t\t\t\t\t\t\t<td>{$cust['vatnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Using</td>\n\t\t\t\t\t\t\t<td>Stock Code<input type='radio' name='sel_frm' value='stkcod' onChange='javascript:document.form.submit();' {$sel_frm_cod}><br>Stock Description<input type='radio' name='sel_frm' value='stkdes' onChange='javascript:document.form.submit();' {$sel_frm_des}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'>Additional Filters</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Store</td>\n\t\t\t\t\t\t\t<td>{$store_drop}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t{$display_optional_filters}\n\t\t\t\t\t\t<tr class='" . bg_class() . "' " . ass("Type the first letters of the stock code you are looking for.") . ">\n\t\t\t\t\t\t\t<td>Stock Filter</td>\n\t\t\t\t\t\t\t<td nowrap><input type='text' size='13' name='ria' value='{$ria}'> <input type='submit' value='Search'> <input type='submit' name='ria' value='Show All'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Sales Order Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Sales Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$sord['sordid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Proforma Invoice</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' name='proforma' value='yes' {$psel1} onChange='javascript:document.form.submit();'> No <input type='radio' name='proforma' value='no' {$psel2} onChange='javascript:document.form.submit();'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t{$getpinvnum}\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Display Costs</td>\n\t\t\t\t\t\t\t<td>Yes <input type='radio' name='costs' value='yes' {$dc_sel['yes']}> No <input type='radio' name='costs' value='no' {$dc_sel['no']}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='ordno' value='{$sord['ordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='inc' {$chin}> No<input type='radio' size='7' name='chrgvat' value='exc' {$chex}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t\t<td valign='center'>{$salesps}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Sales Order Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("sord", $sord_year, $sord_month, $sord_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery/Due Date</td>\n\t\t\t\t\t\t\t<td valign='center'>\n\t\t\t\t\t\t\t\t" . mkDateSelect("ddate", $ddate_year, $ddate_month, $ddate_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Available Credit</td>\n\t\t\t\t\t\t\t<td>" . CUR . " " . sprint($avcred) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='traddisc' value='{$sord['traddisc']}'>%</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='7' name='delchrg' value='{$sord['delchrg']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t" . TBL_BR . "\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'><table " . TMPL_tblDflts . ">{$products}</table></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td rowspan='2'>" . mkQuickLinks(ql("sorder-view.php", "View Sales Orders"), ql("customers-new.php", "New Customer")) . "\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<th width='25%'>Comments</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='comm' rows='4' cols='20'>{$sord['comm']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " <input type='hidden' name='SUBTOT' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$sord['discount']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$sord['delivery']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><b>VAT {$vat14}</b>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' align='center'><input name='addprodBtn' type='submit' value='Add Product'>| <input name='addnon' type='submit' value='Add Non stock Product'> | <input type='submit' name='saveBtn' value='Save'> | <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}
コード例 #22
0
function details($_POST, $error = "")
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid Non-Stock Invoice number.");
    } else {
        $invid = create_dummy(0);
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class=err>" . $e["msg"];
        }
        $confirm .= "{$error}<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM nons_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class=err>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # check if invoice has been printed
    if ($inv['done'] == "y") {
        $error = "<li class=err> Error : invoice number <b>{$invid}</b> has already been printed.";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    /* --- Start Drop Downs --- */
    # format date
    list($syear, $smon, $sday) = explode("-", $inv['sdate']);
    # keep the charge vat option stable
    if ($inv['chrgvat'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
    } else {
        $chy = "";
        $chn = "checked=yes";
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=100%>\n\t<tr>\n\t\t<th>DESCRIPTION</th>\n\t\t<th>QTY</th>\n\t\t<th>UNIT PRICE</th>\n\t\t<th>AMOUNT</th>\n\t\t<th>Remove</th>\n\t<tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM nons_inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $stkd['amt'] = round($stkd['amt'], 2);
        # put in product
        $products .= "<tr class='bg-odd'>\n\t\t\t<td align=center><input type=text size=50 name=des[] value='{$stkd['description']}'></td>\n\t\t\t<td align=center><input type=text size=3 name=qtys[] value='{$stkd['qty']}'></td>\n\t\t\t<td align=center><input type=text size=8 name=unitcost[] value='{$stkd['unitcost']}'></td>\n\t\t\t<td><input type=hidden name=amt[] value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t<td><input type=checkbox name=remprod[] value='{$i}'><input type=hidden name=SCROLL value=yes></td>\n\t\t</tr>";
        $i++;
    }
    # Look above(remprod keys)
    $keyy = $i;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    if ($i == 0 || isset($diffwhBtn)) {
        # add one
        $products .= "<tr class='bg-odd'>\n\t\t\t<td align=center><input type=text size=50 name=des[] value=''></td>\n\t\t\t<td align=center><input type=text size=3 name=qtys[] value='1'></td>\n\t\t\t<td align=center><input type=text size=8 name=unitcost[]></td>\n\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t<td>&nbsp;</td>\n\t\t</tr>";
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $inv['subtot'];
    # Get Total
    $TOTAL = sprint($inv['total']);
    # Get vat
    $VAT = sprint($inv['vat']);
    /* --- End Some calculations --- */
    /* -- Final Layout -- */
    $details = "<center><h3>New Non-Stock Invoices</h3>\n\t<form action='" . SELF . "' method=post name=form>\n\t<input type=hidden name=key value=update>\n\t<input type=hidden name=invid value='{$invid}'>\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=95%>\n \t<tr><td valign=top>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><th colspan=2> Supplier Details </th></tr>\n\t\t\t<tr class='bg-odd'><td>Supplier</td><td valign=middle><input type=text name=cusname value='{$inv['cusname']}'></td></tr>\n\t\t\t<tr class='bg-even'><td valign=top>Customer Address</td><td valign=middle><textarea name=cusaddr cols=18 rows=3>{$inv['cusaddr']}</textarea></td></tr>\n\t\t\t<tr class='bg-odd'><td valign=top>Customer VAT No.</td><td valign=middle><input type=text name=cusvatno value='{$inv['cusvatno']}'></td></tr>\n\t\t</table>\n\t</td><td valign=top align=right>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><th colspan=2> Non-Stock Invoice Details </th></tr>\n\t\t\t<tr class='bg-odd'><td>Non-Stock Invoice No.</td><td valign=center>TI {$inv['invid']}</td></tr>\n\t\t\t<tr class='bg-even'><td>Date</td><td valign=center><input type=text size=2 name=sday maxlength=2 value='{$sday}'>-<input type=text size=2 name=smon maxlength=2 value='{$smon}'>-<input type=text size=4 name=syear maxlength=4 value='{$syear}'> DD-MM-YYYY</td></tr>\n\t\t\t<tr class='bg-odd'><td>VAT Inclusive</td><td valign=center>Yes <input type=radio size=7 name=chrgvat value='yes' {$chy}> No<input type=radio size=7 name=chrgvat value='no' {$chn}></td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td><br></td></tr>\n\t<tr><td colspan=2>{$products}</td></tr>\n\t<tr><td>\n\t\t<p>\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t\t\t<tr><th width=25%>Quick Links</th><th width=25%>Remarks</th><td rowspan=5 valign=top width=50%>{$error}</td></tr>\n\t\t\t<tr><td class='bg-odd'><a href='nons-invoice-view.php'>View Non-Stock Invoices</a></td><td class='bg-odd' rowspan=4 align=center valign=top><textarea name=remarks rows=4 cols=20>{$inv['remarks']}</textarea></td></tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t<tr class='bg-odd'><td><a href='main.php'>Main Menu</a></td></tr>\n\t\t</table>\n\t</td><td align=right>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=80%>\n\t\t\t<tr class='bg-odd'><td>SUBTOTAL</td><td align=right>" . CUR . " <input type=hidden name=subtot value='{$SUBTOT}'>{$SUBTOT}</td></tr>\n\t\t\t<tr class='bg-odd'><td>VAT @ " . TAX_VAT . " %</td><td align=right>" . CUR . " {$inv['vat']}</td></tr>\n\t\t\t<tr class='bg-even'><th>GRAND TOTAL</th><td align=right>" . CUR . " <input type=hidden name=total value='{$TOTAL}'>{$TOTAL}</td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td align=right><input type=button value='&laquo Back' onClick='javascript:history.back()'> | <input name=diffwhBtn type=submit value='Add Item'> |</td><td><input type=submit name='upBtn' value='Update'>{$done}</td></tr>\n\t</table></form>\n\t</center>";
    return $details;
}
コード例 #23
0
ファイル: pkgsubmit.php プロジェクト: Zariel/arch-aur
 } else {
     $uid = uid_from_sid($_COOKIE["AURSID"]);
     # This is a brand new package
     $q = sprintf("INSERT INTO Packages (Name, License, Version, CategoryID, Description, URL, LocationID, SubmittedTS, SubmitterUID, MaintainerUID, FSPath, URLPath) VALUES ('%s', '%s', '%s-%s', %d, '%s', '%s', 2, UNIX_TIMESTAMP(), %d, %d, '%s', '%s')", mysql_real_escape_string($new_pkgbuild['pkgname']), mysql_real_escape_string($new_pkgbuild['license']), mysql_real_escape_string($new_pkgbuild['pkgver']), mysql_real_escape_string($new_pkgbuild['pkgrel']), mysql_real_escape_string($_REQUEST['category']), mysql_real_escape_string($new_pkgbuild['pkgdesc']), mysql_real_escape_string($new_pkgbuild['url']), $uid, $uid, mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"), mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"));
     $result = db_query($q, $dbh);
     $packageID = mysql_insert_id($dbh);
     # Update package depends
     $depends = explode(" ", $new_pkgbuild['depends']);
     foreach ($depends as $dep) {
         $q = "INSERT INTO PackageDepends (PackageID, DepPkgID, DepCondition) VALUES (";
         $deppkgname = preg_replace("/[<>]?=.*/", "", $dep);
         $depcondition = str_replace($deppkgname, "", $dep);
         if ($deppkgname == "#") {
             break;
         }
         $deppkgid = create_dummy($deppkgname, $_COOKIE['AURSID']);
         $q .= $packageID . ", " . $deppkgid . ", '" . mysql_real_escape_string($depcondition) . "')";
         db_query($q, $dbh);
     }
     # Insert sources
     $sources = explode(" ", $new_pkgbuild['source']);
     foreach ($sources as $src) {
         if ($src != "") {
             $q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
             $q .= $packageID . ", '" . mysql_real_escape_string($src) . "')";
             db_query($q, $dbh);
         }
     }
     pkg_notify(account_from_sid($_COOKIE["AURSID"]), array($packageID));
     header('Location: packages.php?ID=' . $packageID);
 }
コード例 #24
0
function details($_POST, $error = "")
{
    if (PRD_STATE == 'p') {
        return "<br><li class=err> - Error : You cannot acces this function when you are using a closed period.<br><br></li>";
    }
    # Get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid invoice number.");
    } else {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class=err>" . $e["msg"];
        }
        $confirm .= "{$error}<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (isset($deptid)) {
        db_connect();
        # Query server for customer info
        $sql = "SELECT cusnum,cusname,surname FROM customers WHERE deptid = '{$deptid}' AND location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $err = "<li class=err>No customer names starting with <b>{$letters}</b> in database.";
            return view_err($_POST, $err);
        }
    }
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
        $stkerr = "0,0";
    }
    if (!isset($done)) {
        $done = "";
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class=err>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # Check if invoice has been printed
    if ($inv['printed'] == "y") {
        $error = "<li class=err> Error : Invoice number <b>{$invid}</b> has already been printed.";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class=err>Department not Found.";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected Customer info
    db_connect();
    $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['cusnum']}' AND location != 'int' AND div = '" . USER_DIV . "'";
    $custRslt = db_exec($sql) or errDie("Unable to view customer");
    if (pg_numrows($custRslt) < 1) {
        db_connect();
        # Query server for customer info
        $sql = "SELECT cusnum,cusname,surname FROM customers WHERE deptid = '{$inv['deptid']}' AND location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $err = "<li class=err>No customer names starting with <b>{$letters}</b> in database.";
            return view_err($_POST, $err);
        } else {
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            $customers .= "<option value='-S' selected>Select Customer</option>";
            while ($cust = pg_fetch_array($custRslt)) {
                $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']}</option>";
            }
            $customers .= "</select>";
        }
        # Take care of the unset vars
        $cust['addr1'] = "";
        $cust['cusnum'] = "";
        $cust['vatnum'] = "";
        $cust['accno'] = "";
    } else {
        $cust = pg_fetch_array($custRslt);
        $sql = "SELECT cusnum, cusname, surname FROM customers WHERE deptid = '{$inv['deptid']}' AND location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $cusRslt = db_exec($sql) or errDie("Unable to view customers");
        # Moarn if customer account has been blocked
        if ($cust['blocked'] == 'yes') {
            $error .= "<li class=err>Error : Selected customer account has been blocked.";
        }
        // $customers = "<input type=hidden name=cusnum value='$cust[cusnum]'>$cust[cusname]  $cust[surname]";
        $cusnum = $cust['cusnum'];
        $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
        // $customers .= "<option value='-S' selected>Select Customer</option>";
        while ($cus = pg_fetch_array($cusRslt)) {
            $sel = "";
            if ($cust['cusnum'] == $cus['cusnum']) {
                $sel = "selected";
            }
            $customers .= "<option value='{$cus['cusnum']}' {$sel}>{$cus['cusname']} {$cus['surname']}</option>";
        }
        $customers .= "</select>";
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class=err> There are no Stores found in Cubit.";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # Get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class=err> There are no Sales People found in Cubit.";
    } else {
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $inv['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    # Days drop downs
    $days = array("0" => "0", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $inv['terms']);
    # Keep the charge vat option stable
    if ($inv['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($inv['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    # Format date
    list($oyear, $omon, $oday) = explode("-", $inv['odate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=100%>\n\t<tr><th>STORE</th><th>ITEM NUMBER</th><th>SERIAL NO.</th><th>DESCRIPTION</th><th>QTY</th><th>UNIT PRICE</th><th>UNIT DISCOUNT</th><th>AMOUNT</th><th>Remove</th><tr>";
    # get selected stock in this invoice
    db_connect();
    $sql = "SELECT * FROM inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # Keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $i++;
        # Get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # Get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        # Serial number
        if ($stk['serd'] == 'yes' && $inv['serd'] == 'n') {
            $sers = ext_getavserials($stkd['stkid']);
            $sernos = "<select class='width : 15' name='sernos[]'>";
            foreach ($sers as $skey => $ser) {
                $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
            }
            $sernos .= "</select>";
        } else {
            $sernos = "<input type=hidden name=sernos[] value='{$stkd['serno']}'>{$stkd['serno']}";
        }
        # Input qty if not serialised
        $qtyin = "<input type=text size=3 name=qtys[] value='{$stkd['qty']}'>";
        if ($stk['serd'] == 'yes') {
            $qtyin = "<input type=hidden size=3 name=qtys[] value='{$stkd['qty']}'>{$stkd['qty']}";
        }
        # check permissions
        if (perm("invoice-unitcost-edit.php")) {
            $viewcost = "<input type=text size=8 name=unitcost[] value='{$stkd['unitcost']}'>";
        } else {
            $viewcost = "<input type=hidden size=8 name=unitcost[] value='{$stkd['unitcost']}'>{$stkd['unitcost']}";
        }
        # Put in product
        $products .= "<tr class='bg-odd'><td><input type=hidden name=whids[] value='{$stkd['whid']}'>{$wh['whname']}</td><td><input type=hidden name=stkids[] value='{$stkd['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td><td>{$sernos}</td><td>" . extlib_rstr($stk['stkdes'], 30) . "</td><td>{$qtyin}</td><td>{$viewcost}</td><td><input type=text size=4 name=disc[] value='{$stkd['disc']}'> OR <input type=text size=4 name=discp[] value='{$stkd['discp']}' maxlength=5>%</td><td><input type=hidden name=amt[] value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td><td><input type=checkbox name=remprod[] value='{$key}'><input type=hidden name=SCROLL value=yes></td></tr>";
        $key++;
    }
    # Look above(remprod keys)
    $keyy = $key;
    # Look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    } else {
        $SCROLL = "yes";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && isset($cust['pricelist'])) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # Get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # Get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                if ($stk['serd'] == 'yes') {
                    $sers = ext_getavserials($stkidss[$key]);
                    $sernos = "<select class='width : 15'name='sernos[]' onChange='javascript:document.form.submit();'>";
                    foreach ($sers as $skey => $ser) {
                        $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
                    }
                    $sernos .= "</select>";
                } else {
                    $sernos = "<input type=hidden name=sernos[] value=''>";
                }
                # Get price from price list if it is set
                if (isset($cust['pricelist'])) {
                    # get selected stock in this warehouse
                    db_conn("exten");
                    $sql = "SELECT price FROM plist_prices WHERE listid = '{$cust['pricelist']}' AND stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
                    $plRslt = db_exec($sql);
                    if (pg_numrows($plRslt) > 0) {
                        $pl = pg_fetch_array($plRslt);
                        $stk['selamt'] = $pl['price'];
                    }
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class=err>Warning :  Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                # Input qty if not serialised
                $qtyin = "<input type=text size=3 name=qtys[] value='{$qtyss[$key]}'>";
                if ($stk['serd'] == 'yes') {
                    $qtyin = "<input type=hidden size=3 name=qtys[] value='{$qtyss[$key]}'>{$qtyss[$key]}";
                }
                # Check permissions
                if (perm("invoice-unitcost-edit.php")) {
                    $viewcost = "<input type=text size=8 name=unitcost[] value='{$stk['selamt']}'>";
                } else {
                    $viewcost = "<input type=hidden size=8 name=unitcost[] value='{$stk['selamt']}'>{$stk['selamt']}";
                }
                # Put in selected warehouse and stock
                $products .= "<tr class='bg-odd'><td><input type=hidden name=whids[] value='{$whid}'>{$wh['whname']}</td><td><input type=hidden name=stkids[] value='{$stk['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td><td>{$sernos}</td><td>" . extlib_rstr($stk['stkdes'], 30) . "</td><td>{$qtyin}</td><td>{$viewcost}</td><td><input type=text size=4 name=disc[] value='{$discs[$key]}'> OR <input type=text size=4 name=discp[] value='{$discps[$key]}' maxlength=5>%</td><td><input type=hidden name=amt[] value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td><td><input type=checkbox name=remprod[] value='{$keyy}'></td></tr>";
                $keyy++;
            } else {
                if (!isset($diffwhBtn)) {
                    # skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    # get warehouse name
                    db_conn("exten");
                    $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                    $whRslt = db_exec($sql);
                    $wh = pg_fetch_array($whRslt);
                    if (isset($ria) and $ria != "") {
                        $len = strlen($ria);
                        $Wh = "AND lower(substr(stkcod,1,'{$len}'))=lower('{$ria}')";
                    } else {
                        $Wh = "";
                        $ria = "";
                    }
                    # get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        $error .= "<li class=err>There are no stock items in the selected warehouse.";
                        continue;
                    }
                    if (pg_numrows($stkRslt) == 1) {
                        $ex = "selected";
                    } else {
                        $ex = "";
                    }
                    $stks = "<select class='width : 15'name='stkidss[]' onChange='javascript:document.form.submit();'>";
                    $stks .= "<option value='-S' disabled selected>Select Number</option>";
                    $count = 0;
                    while ($stk = pg_fetch_array($stkRslt)) {
                        $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                    }
                    $stks .= "</select> ";
                    # put in drop down and warehouse
                    $products .= "<tr class='bg-odd'><td><input type=hidden name=whidss[] value='{$whid}'>{$wh['whname']}</td><td>{$stks}</td><td> </td><td> </td><td><input type=hidden size=3 name='qtyss[]'  value='1'>1</td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td><input type=hidden name=amts[] value='0.00'>" . CUR . " 0.00</td><td></td></tr>";
                }
            }
        }
    } else {
        if (!isset($diffwhBtn)) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                if (isset($wtd)) {
                    $whid = $wtd;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                if (isset($ria) and $ria != "") {
                    $len = strlen($ria);
                    $Wh = "AND lower(substr(stkcod,1,'{$len}'))=lower('{$ria}')";
                } else {
                    $Wh = "";
                    $ria = "";
                }
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected store.";
                    //ontinue;
                }
                $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                $stks .= "<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . ($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                $products .= "<tr class='bg-odd'><td><input type=hidden name=whidss[] value='{$whid}'>{$wh['whname']}</td><td>{$stks}</td><td> </td><td></td><td><input type=hidden size=3 name=qtyss[] value='1'>1</td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
            } else {
                $products .= "<tr class='bg-odd'><td>{$whs}</td><td> </td><td></td><td> </td><td> </td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        $products .= "<tr class='bg-odd'><td>{$whs}</td><td> </td><td></td><td> </td><td> </td><td> </td><td><input type=text size=4 name=discs[] value='0'> OR <input type=text size=4 name=discps[] value='0' maxlength=5>%</td><td>" . CUR . " 0.00</td><td></td></tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    //print "<br>TOTAL:$inv[total]  VAT:$inv[vat]<br>";
    # Calculate tradediscm
    if ($inv['traddisc'] > 0) {
        $traddiscm = sprint($inv['traddisc'] / 100 * $inv['total']);
    } else {
        $traddiscm = "0.00";
    }
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    $VAT = sprint($inv['vat']);
    $TOTAL = sprint($inv['total']);
    $inv['delchrg'] = sprint($inv['delchrg']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    # check only if the customer is selected
    if (isset($cusnum) && $cusnum != "-S") {
        #check againg credit limit
        if ($TOTAL + $cust['balance'] > $cust['credlimit']) {
            $error .= "<li class=err>Warning : Customers Credit limit of <b>" . CUR . " {$cust['credlimit']}</b> has been exceeded";
            # Check permissions
            if (!perm("invoice-limit-override.php")) {
                $done = "";
            }
        }
        $avcred = $cust['credlimit'] - $cust['balance'];
    } else {
        $avcred = "0.00";
    }
    /*--- Start checks --- */
    /* -- Final Layout -- */
    $details = "<center><h3>New Invoice</h3>\n\t<form action='" . SELF . "' method=post name=form>\n\t<input type=hidden name=key value=update>\n\t<input type=hidden name=invid value='{$invid}'>\n\t<input type=hidden name=letters value='{$letters}'>\n\t<input type=hidden name=stkerr value='{$stkerr}'>\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=95%>\n \t<tr><td valign=top width=50%>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><th colspan=2> Customer Details </th></tr>\n\t\t\t<tr class='bg-odd'><td>Department</td><td valign=center>{$dept['deptname']}</td></tr>\n\t\t\t<tr class='bg-even'><td>Account No.</td><td valign=center>{$cust['accno']}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Customer</td><td valign=center>{$customers}</td></tr>\n\t\t\t<tr class='bg-even'><td valign=top>Customer Address</td><td valign=center>" . nl2br($cust['addr1']) . "</td></tr>\n\t\t\t<tr class='bg-odd'><td>Customer Order number</td><td valign=center><input type=text size=10 name=cordno value='{$inv['cordno']}'></td></tr>\n\t\t\t<tr class='bg-even'><td>Customer Vat Number</td><td>{$cust['vatnum']}</td></tr>\n\t\t\t<tr><th colspan=2>Point of Sale</th></tr>\n\t\t\t<tr class='bg-even'><td>Barcode</td><td><input type=text size=13 name=bar value=''></td></tr>\n\t\t\t<tr class='bg-odd' " . ass("Type the first letters of the stock code you are looking for.") . "><td>Stock Filter</td><td><input type=text size=13 name=ria value='{$ria}' onkeyup='javasript:predict()'></td></tr>\n\t\t</table>\n\t</td><td valign=top align=right width=50%>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0>\n\t\t\t<tr><th colspan=2> Invoice Details </th></tr>\n\t\t\t<tr class='bg-odd'><td>Invoice No.</td><td valign=center>TI {$inv['invid']}</td></tr>\n\t\t\t<tr class='bg-even'><td>Proforma Inv No.</td><td valign=center><input type=text size=5 name=docref value='{$inv['docref']}'></td></tr>\n\t\t\t<tr class='bg-odd'><td>Sales Order No.</td><td valign=center><input type=text size=5 name=ordno value='{$inv['ordno']}'></td></tr>\n\t\t\t<tr class='bg-even'><td>VAT Inclusive</td><td valign=center>Yes <input type=radio size=7 name=chrgvat value='inc' {$chin}> No<input type=radio size=7 name=chrgvat value='exc' {$chex}> Excempt From Vat<input type=radio size=7 name=chrgvat value='nov' {$chno}></td></tr>\n\t\t\t<tr class='bg-odd'><td>Terms</td><td valign=center>{$termssel} Days</td></tr>\n\t\t\t<tr class='bg-even'><td>Sales Person</td><td valign=center>{$salesps}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Invoice Date</td><td valign=center><table><tr><td><input type=text size=2 name=oday maxlength=2 value='{$oday}'></td><td>-</td><td><input type=text size=2 name=omon maxlength=2 value='{$omon}'></td><td>-</td><td><input type=text size=4 name=oyear maxlength=4 value='{$oyear}'></td><td></tr></table></td></tr>\n\t\t\t<tr class='bg-even'><td>Available Credit</td><td>" . CUR . " {$avcred}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Trade Discount</td><td valign=center><input type=text size=5 name=traddisc value='{$inv['traddisc']}'>%</td></tr>\n\t\t\t<tr class='bg-even'><td>Delivery Charge</td><td valign=center><input type=text size=7 name=delchrg value='{$inv['delchrg']}'></td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td><br></td></tr>\n\t<tr><td colspan=2>{$products}</td></tr>\n\t<tr><td>\n\t\t<p>\n\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=100%>\n\t\t\t<tr><th width=25%>Quick Links</th><th width=25%>Comments</th><td rowspan=5 valign=top width=50%>{$error}</td></tr>\n\t\t\t<tr><td class='bg-odd'><a href='cust-credit-stockinv.php'>New Invoice</a></td><td class='bg-odd' rowspan=4 align=center valign=top><textarea name=comm rows=4 cols=20>{$inv['comm']}</textarea></td></tr>\n\t\t\t<tr class='bg-odd'><td><a href='invoice-view.php'>View Invoices</a></td></tr>\n\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t<tr class='bg-odd'><td><a href='main.php'>Main Menu</a></td></tr>\n\t\t</table>\n\t</td><td align=right valign=top>\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' border=0 width=50%>\n\t\t\t<tr class='bg-odd'><td>SUBTOTAL</td><td align=right>" . CUR . " <input type=hidden name=SUBTOT value='{$SUBTOT}'>{$SUBTOT}</td></tr>\n\t\t\t<tr class='bg-even'><td>Trade Discount</td><td align=right>" . CUR . " {$inv['discount']}</td></tr>\n\t\t\t<tr class='bg-odd'><td>Delivery Charge</td><td align=right>" . CUR . " {$inv['delivery']}</td></tr>\n\t\t\t<tr class='bg-even'><td><b>VAT @ {$VATP}%</b></td><td align=right>" . CUR . " {$VAT}</td></tr>\n\t\t\t<tr class='bg-odd'><th>GRAND TOTAL</th><td align=right>" . CUR . " {$TOTAL}</td></tr>\n\t\t</table>\n\t</td></tr>\n\t<tr><td align=right><input type=button value='&laquo Back' onClick='javascript:history.back()'> | <input name=diffwhBtn type=submit value='Different Store'> | <input name=addprodBtn type=submit value='Add Product'> | <input type=submit name='saveBtn' value='Save'> </td><td>| <input type=submit name='upBtn' value='Update'>{$done}</td></tr>\n\t</table><a name=bottom>\n\t</form></center>";
    return $details;
}
コード例 #25
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    if (isset($noduty)) {
        $exd = "<input type='hidden' name='noduty' value='yes'>";
    } else {
        $exd = "";
    }
    # Validate input
    require_lib("validate");
    $v = new validate();
    if (isset($purid)) {
        $v->isOk($purid, "num", 1, 20, "Invalid Order number.");
    } else {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($purid)) {
        $purid = create_dummy($deptid);
    }
    # Get Order info
    db_connect();
    $sql = "SELECT * FROM purch_int WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>Order Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    # check if Order has been printed
    if ($pur['received'] == "y") {
        $error = "<li class='err'> Error : Order number <b>{$purid}</b> has already been received.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$pur['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    $deptid = $pur['deptid'];
    $supid = $pur['supid'];
    # Get selected supplier info
    db_connect();
    $sql = "SELECT * FROM suppliers WHERE supid = '{$pur['supid']}' AND div = '" . USER_DIV . "'";
    $supRslt = db_exec($sql) or errDie("Unable to view Supplier");
    if (pg_numrows($supRslt) < 1) {
        db_connect();
        if ($inv['deptid'] == 0) {
            $searchdept = "";
        } else {
            $searchdept = "deptid = '{$deptid}' AND ";
        }
        # Query server for supplier info
        $sql = "SELECT * FROM suppliers WHERE {$searchdept} location = 'int' AND lower(supname) LIKE lower('{$letters}%') AND div = '" . USER_DIV . "' ORDER BY supname ASC";
        $supRslt = db_exec($sql) or errDie("Unable to view suppliers");
        if (pg_numrows($supRslt) < 1) {
            $err = "<li class='err'>No Supplier names starting with <b>{$letters}</b> in database.</li>";
            return view_err($_POST, $err);
        } else {
            $suppliers = "<select name='supid' onChange='javascript:document.form.submit();'>";
            $suppliers .= "<option value='-S' selected>Select Supplier</option>";
            while ($sup = pg_fetch_array($supRslt)) {
                $suppliers .= "<option value='{$sup['supid']}'>{$sup['supname']}</option>";
            }
            $suppliers .= "</select>";
        }
        # take care of the uset vars
        $supaddr = "";
        $accno = "";
        $fcid = $pur['fcid'];
    } else {
        db_connect();
        # Query server for supplier info
        $sql = "SELECT * FROM suppliers WHERE deptid = '{$deptid}' AND location = 'int' AND lower(supname) LIKE lower('{$letters}%') AND div = '" . USER_DIV . "' ORDER BY supname ASC";
        $supRslt = db_exec($sql) or errDie("Unable to view suppliers");
        if (pg_numrows($supRslt) < 1) {
            $err = "<li class='err'>No Supplier names starting with <b>{$letters}</b> in database.</li>";
            return view_err($_POST, $err);
        } else {
            $suppliers = "<select name='supid' onChange='javascript:document.form.submit();'>";
            $sel = "";
            $fcid = $pur['fcid'];
            while ($sup = pg_fetch_array($supRslt)) {
                if ($sup['supid'] == $supid) {
                    $sel = "selected";
                    $supaddr = "{$sup['supaddr']}";
                    $accno = $sup['supno'];
                    $fcid = $sup['fcid'];
                    $listid = $sup['listid'];
                } else {
                    $sel = "";
                    $supaddr = "";
                    $accno = "";
                }
                $suppliers .= "<option value='{$sup['supid']}' {$sel}>{$sup['supname']}</option>";
            }
            $suppliers .= "</select>";
        }
    }
    # this is a quick fix for pricelist product avaibility
    $listids = array();
    if (isset($listid) && $listid > 0) {
        # Get jobs stkids
        db_conn("exten");
        $sql = "SELECT stkid FROM splist_prices WHERE listid = '{$listid}' AND div = '" . USER_DIV . "'";
        $lstkRslt = db_exec($sql);
        while ($lstk = pg_fetch_array($lstkRslt)) {
            $listids[] = $lstk['stkid'];
        }
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # Days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    # Currency
    $currs = getSymbol($fcid);
    $curr = $currs['symbol'];
    $currsel = "{$currs['symbol']} - {$currs['descrip']}";
    # Format date
    list($ipur_year, $ipur_month, $ipur_day) = explode("-", $pur['pdate']);
    list($del_year, $del_month, $del_day) = explode("-", $pur['ddate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    if (isset($noduty)) {
        $dd = "";
    } else {
        $dd = "<th colspan='2'>DUTY</th>";
    }
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th colspan=2>UNIT PRICE</th>\n\t\t\t\t{$dd}\n\t\t\t\t<th>LINE TOTAL</th>\n\t\t\t\t<th>COST PER UNIT</th>\n\t\t\t\t<th>DEL</th>\n\t\t\t<tr>";
    # Get selected stock in this Order
    db_connect();
    $sql = "SELECT * FROM purint_items  WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $i++;
        # Get warehouse name
        db_conn("exten");
        $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
        $whRslt = db_exec($sql);
        $wh = pg_fetch_array($whRslt);
        # Get selected stock in this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
        $stkRslt = db_exec($sql);
        $stk = pg_fetch_array($stkRslt);
        /* -- Calculations -- */
        # Calculate cost amount bought
        $totamt = $stkd['qty'] * $stkd['cunitcost'];
        $unittot = $totamt;
        # Calculate percentage from subtotal
        if ($pur['subtot'] != 0) {
            $perc = ($totamt + $stkd['duty']) / $pur['subtot'] * 100;
        } else {
            $perc = 0;
        }
        # Get percentage from shipping charges
        $shipchrg = sprint($perc / 100 * $pur['shipchrg']);
        # Add shipping charges to amt
        $totamt = sprint($totamt + $shipchrg + $stkd['duty']);
        $unittot = sprint($unittot + $stkd["duty"]);
        $lineamt = sprint($totamt / $stkd["qty"]);
        $unitamt = sprint($unittot / $stkd['qty']);
        /* -- End Calculations --*/
        $stkd['amt'] = sprint($stkd['amt']);
        if (isset($noduty)) {
            $dd = "\n\t\t\t\t<input type='hidden' name='duty[]' value='{$stkd['duty']}'>\n\t\t\t\t<input type='hidden' name='dutyp[]' value='{$stkd['dutyp']}'>";
        } else {
            $dd = "\n\t\t\t\t<td nowrap>{$pur['curr']} <input type='text' size='6' name='duty[]' value='{$stkd['duty']}'> or </td>\n\t\t\t\t<td><input type='text' size='3' name='dutyp[]' value='{$stkd['dutyp']}'>%</td>";
        }
        # Put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>\n\t\t\t\t\t<a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a>\n\t\t\t\t</td>\n\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$stkd['qty']}'></td>\n\t\t\t\t<td nowrap>{$pur['curr']} <input type='text' size='6' name='cunitcost[]' value='{$stkd['cunitcost']}'><b> OR </b></td>\n\t\t\t\t<td nowrap>" . CUR . " <input type='text' size='6' name='unitcost[]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t{$dd}\n\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$stkd['amt']}'> {$pur['curr']} {$lineamt}</td>\n\t\t\t\t<td align='right' nowrap>{$pur['curr']} {$unitamt}</td>\n\t\t\t\t<td>\n\t\t\t\t\t<input type='checkbox' name='remprod[]' value='{$key}'>\n\t\t\t\t\t<input type='hidden' name='SCROLL' value='yes'>\n\t\t\t\t</td>\n\t\t\t</tr>";
        $key++;
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    if ($pur['xrate'] == 0) {
        $pur['xrate'] = 1;
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S") {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # Get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * $stk['lcsprice'] / $pur['xrate']);
                $cunitcost[$key] = sprint($pur['xrate'] * $stk['lcsprice']);
                if (isset($noduty)) {
                    $dd = "\n\t\t\t\t\t\t<input type='hidden' name='duty[]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='dutyp[]' value='0'>";
                } else {
                    $dd = "\n\t\t\t\t\t\t<td>{$pur['curr']} <input type='text' size='6' name='duty[]' value='0'> or </td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='dutyp[]' value='0'>%</td>";
                }
                # put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<input type='hidden' name='stkids[]' value='{$stk['stkid']}'>\n\t\t\t\t\t\t\t<a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='qtys[]' value='{$qtyss[$key]}'></td>\n\t\t\t\t\t\t<td nowrap>{$pur['curr']} <input type='text' size='6' name='cunitcost[]' value='{$stkd['cunitcost']}'> or </td>\n\t\t\t\t\t\t<td nowrap>" . CUR . " <input type='text' size='6' name='unitcost[]'  value='{$stk['lcsprice']}'></td>\n\t\t\t\t\t\t{$dd}\n\t\t\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$amt[$key]}'> {$pur['curr']} {$amt[$key]}</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<input type='checkbox' name='remprod[]' value='{$keyy}'>\n\t\t\t\t\t\t\t<input type='hidden' name='SCROLL' value='yes'>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>";
                $keyy++;
            } else {
                if (!isset($diffwhBtn)) {
                    # Skip if not selected
                    if ($whid == "-S") {
                        continue;
                    }
                    # Get warehouse name
                    db_conn("exten");
                    $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                    $whRslt = db_exec($sql);
                    $wh = pg_fetch_array($whRslt);
                    # Get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        $error .= "<li class='err'>There are no stock items in the selected warehouse.</li>";
                        continue;
                    }
                    $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                    $stks .= "<option value='-S' disabled selected>Select Item Number</option>";
                    $count = 0;
                    while ($stk = pg_fetch_array($stkRslt)) {
                        $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                    }
                    $stks .= "</select> ";
                    if (isset($noduty)) {
                        $dd = "";
                    } else {
                        $dd = "<td>&nbsp</td><td>&nbsp;</td>";
                    }
                    # Put in drop down and warehouse
                    $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t{$dd}\n\t\t\t\t\t\t\t<td nowrap><input type='hidden' name='amts[]' value='0.00'>{$pur['curr']} 0.00</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t</tr>";
                }
            }
        }
    } else {
        if (!isset($diffwhBtn)) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
                # get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                # get stock on this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE whid = '{$whid}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                if (pg_numrows($stkRslt) < 1) {
                    if (!isset($err)) {
                        $err = "";
                    }
                    $err .= "<li>There are no stock items in the selected warehouse.</li>";
                }
                $stks = "<select name='stkidss[]' onChange='javascript:document.form.submit();'>";
                $stks .= "<option value='-S' disabled selected>Select Item Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $stks .= "</select> ";
                if (isset($noduty)) {
                    $dd = "";
                } else {
                    $dd = "<td>{$pur['curr']} 0.00</td><td></td>";
                }
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[]' value='1'></td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t{$dd}\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t</tr>";
            } else {
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$whs}</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>{$pur['curr']} 0.00</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t\t</tr>";
            }
        }
    }
    /* -- start Listeners -- */
    if (isset($diffwhBtn)) {
        # take todays date
        list($year, $mon, $day) = explode("-", $pur['pdate']);
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>{$whs}</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>{$pur['curr']} 0.00</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t</tr>";
    }
    /* -- End Listeners -- */
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = sprint($pur['subtot']);
    # Get Total
    $TOTAL = sprint($pur['total']);
    $pur['tax'] = sprint($pur['tax']);
    $pur['shipchrg'] = sprint($pur['shipchrg']);
    /* --- End Some calculations --- */
    $pur['jobnum'] += 0;
    if ($pur['jobnum'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $pur['jobnum'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $pur['jobnum']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    if (isset($diffwhBtn) or isset($upBtn) or isset($doneBtn) or isset($donePrnt)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details = "\n\t\t<center>\n\t\t<h3>New International Order</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='purid' value='{$purid}'>\n\t\t\t<input type='hidden' name='deptid' value='{$deptid}'>\n\t\t\t<input type='hidden' name='letters' value='{$letters}'>\n\t\t\t{$exd}\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t<td valign='center'>{$suppliers}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t<td valign='center'>{$accno}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t\t\t\t<td valign='center'>" . nl2br($supaddr) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Order Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Purchase No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("ipur", $ipur_year, $ipur_month, $ipur_day) . " </td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Foreign Currency</td>\n\t\t\t\t\t\t\t<td valign='center'>{$currsel} &nbsp;&nbsp;Exchange rate " . CUR . " <input type='text' size='7' name='xrate' value='{$pur['xrate']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Tax</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['curr']} <input type='text' size='7' name='tax' value='{$pur['tax']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Shipping Charges</td>\n\t\t\t\t\t\t\t<td valign='center' nowrap>{$pur['curr']} <input type='text' size='7' name='shipchrg' value='{$pur['fshipchrg']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Date</td>\n\t\t\t\t\t\t\t<td valign=center>" . mkDateSelect("del", $del_year, $del_month, $del_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='purch-int-view.php'>View International Orders</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$pur['remarks']}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} <input type='hidden' name='subtot' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Shipping Charges</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} {$pur['shipchrg']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Tax </td>\n\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} {$pur['tax']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right' nowrap>{$pur['curr']} <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input type='button' value='&laquo Back' onClick='javascript:history.back()'> | <input name='diffwhBtn' type='submit' value='Different Store'> |</td>\n\t\t\t\t<td nowrap><input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}
コード例 #26
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    if (!isset($button) && isset($starting)) {
        return slct();
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid Non-Stock Invoice number.");
    } elseif (isset($ctyp)) {
        $val = $ctyp . "val";
        if (isset(${$val})) {
            $tval = ${$val};
            $v->isOk($tval, "num", 1, 20, "Invalid Selection.");
        }
    }
    if (isset($cusnum) && customer_overdue($cusnum)) {
        $v->addError(0, "Customer is overdue, account blocked!");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return slct($error);
        $confirm = "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($invid) && isset($ctyp)) {
        $val = $ctyp . "val";
        if (!isset(${$val})) {
            ${$val} = "";
        }
        $tval = ${$val};
        if (isset($bankid)) {
            $bankid += 0;
            $acc = $bankid;
        } else {
            $acc = 0;
        }
        $invid = create_dummy(0, $ctyp, $tval, $acc);
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM nons_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    # check if invoice has been printed
    if ($inv['done'] == "y") {
        $error = "<li class='err'> Error : invoice number <b>{$invid}</b> has already been printed.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    /* --- Start Drop Downs --- */
    # format date
    list($ninv_year, $ninv_month, $ninv_day) = explode("-", $inv['odate']);
    # keep the charge vat option stable
    if ($inv['chrgvat'] == "yes") {
        $chy = "checked=yes";
        $chn = "";
        $chnone = "";
    } elseif ($inv['chrgvat'] == "no") {
        $chy = "";
        $chn = "checked=yes";
        $chnone = "";
    } else {
        $chy = "";
        $chn = "";
        $chnone = "checked=yes";
    }
    # Days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $inv['terms']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>VAT Code</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM nons_inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        # keep track of selected stock amounts
        $amts[$i] = $stkd['amt'];
        $stkd['amt'] = round($stkd['amt'], 2);
        $chk = "";
        if ($stkd['vatex'] == 'y') {
            $chk = "checked=yes";
        }
        db_conn('cubit');
        $Sl = "SELECT * FROM vatcodes ORDER BY code";
        $Ri = db_exec($Sl);
        $vats = "<select name='vatcodes[]'>";
        while ($vd = pg_fetch_array($Ri)) {
            if ($stkd['vatex'] == $vd['id']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $vats .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
        }
        $vats .= "</option>";
        $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatex']}'";
        $Ri = db_exec($Sl);
        $vd = pg_fetch_array($Ri);
        if (TAX_VAT != $vd['vat_amount'] and $vd['vat_amount'] != "0.00") {
            $showvat = FALSE;
        }
        # put in product
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center' nowrap><input type='hidden' name='des[{$i}]' value='{$stkd['description']}'>" . nl2br($stkd['description']) . "</td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[{$i}]' value='{$stkd['qty']}'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[{$i}]' value='{$stkd['unitcost']}'></td>\n\t\t\t\t<td><input type='hidden' name='amt[{$i}]' value='" . sprint($stkd["amt"]) . "'> " . CUR . " " . sprint($stkd["amt"]) . "</td>\n\t\t\t\t<!--<td align='center'><input type='checkbox' name='vatex[{$i}]' value='{$i}' {$chk}></td>-->\n\t\t\t\t<td align='center'>{$vats}</td>\n\t\t\t\t<td align='center'><input type='checkbox' name='remprod[{$i}]' value='{$i}'><input type='hidden' name='SCROLL' value='yes'></td>\n\t\t\t</tr>";
        $i++;
    }
    # Look above(remprod keys)
    $keyy = $i;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    if (!isset($error) and strlen($error) == 0) {
        $bodydata = "";
    }
    //document.editForm.bodydata.value = editArea.document.body.innerHTML;
    //	if (isset ($bodydata) AND strlen ($bodydata) > 0){
    //print "$bodydata";
    //		$doset = "
    //			<script>
    //				editArea.document.body.innerHTML = '".strip_tags($bodydata)."';
    //			</script>";
    //	}else {
    //		$doset = "";
    //	}
    //old
    //					<td align='center'><input type='text' size='50' name='des[]' value=''></td>
    if ($i == 0 || isset($diffwhBtn)) {
        # add one
        $products .= "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td align='center'>\n\t\t\t\t\t<table id='tblCtrls' width='420px' height='30px' border='0' cellspacing='0' cellpadding='0' bgcolor='#D6D3CE'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='tdClass'>\n\t\t\t\t\t\t\t\t<img alt='Bold' class='buttonClass' src='images/bold.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doBold()'>\n\t\t\t\t\t\t\t\t<img alt='Italic' class='buttonClass' src='images/italic.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doItalic()'>\n\t\t\t\t\t\t\t\t<img alt='Underline' class='buttonClass' src='images/underline.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doUnderline()'>\n\t\t\t\t\t\t\t\t<img alt='Left' class='buttonClass' src='images/left.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doLeft()'>\n\t\t\t\t\t\t\t\t<img alt='Center' class='buttonClass' src='images/center.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doCenter()'>\n\t\t\t\t\t\t\t\t<img alt='Right' class='buttonClass' src='images/right.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doRight()'>\n\t\t\t\t\t\t\t\t<img alt='Ordered List' class='buttonClass' src='images/ordlist.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doOrdList()'>\n\t\t\t\t\t\t\t\t<img alt='Bulleted List' class='buttonClass' src='images/bullist.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doBulList()'>\n\t\t\t\t\t\t\t\t<img alt='Horizontal Rule' class='buttonClass' src='images/rule.gif' onMouseOver='controlSelOn(this)' onMouseOut='controlSelOff(this)' onMouseDown='controlSelDown(this)' onMouseUp='controlSelUp(this)' onClick='doRule()'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td class='tdClass' align=right>\n\t\t\t\t\t\t\t\t<select name='selSize' onChange='doSize(this.options[this.selectedIndex].value)'>\n\t\t\t\t\t\t\t\t\t<option value=''>-- Font Size --</option>\n\t\t\t\t\t\t\t\t\t<option value='1'>Very Small</option>\n\t\t\t\t\t\t\t\t\t<option value='2'>Small</option>\n\t\t\t\t\t\t\t\t\t<option value='3'>Medium</option>\n\t\t\t\t\t\t\t\t\t<option value='4'>Large</option>\n\t\t\t\t\t\t\t\t\t<option value='5'>Larger</option>\n\t\t\t\t\t\t\t\t\t<option value='6'>Very Large</option>\n\t\t\t\t\t\t\t\t</select>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t<iframe name='editArea' id='editArea' style='width: 420px; height:160px; background: #FFFFFF;'></iframe>\n\t\t\t\t\t<input type='hidden' name='bodydata' value=''>\n\t\t\t\t\t<input type='hidden' name='counter' value='{$i}'>\n\t\t\t\t</td>\n\t\t\t\t<td align='center'><input type='text' size='3' name='qtys[{$i}]' value='1'></td>\n\t\t\t\t<td align='center'><input type='text' size='8' name='unitcost[{$i}]'></td>\n\t\t\t\t<td>" . CUR . " 0.00</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t\t<td>&nbsp;</td>\n\t\t\t</tr>";
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = $inv['subtot'];
    # Get Total
    $TOTAL = sprint($inv['total']);
    # Get vat
    $VAT = sprint($inv['vat']);
    /* --- End Some calculations --- */
    db_conn('cubit');
    if ($inv['ctyp'] == 's') {
        $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['tval']}' AND div = '" . USER_DIV . "'";
        $custRslt = db_exec($sql) or errDie("Unable to view customer");
        $cust = pg_fetch_array($custRslt);
        if (!empty($cust["cusname"])) {
            $cn = "{$cust['cusname']} {$cust['surname']}";
        } else {
            $cn = "{$cust['surname']}";
        }
        $details = "\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t</tr>\n\t\t\t<input type='hidden' name='cusnum' value='{$cust['cusnum']}' />\n\t\t\t<input type='hidden' name='cusname' value='{$cn}'>\n\t\t\t<input type='hidden' name='cusaddr' value='{$cust['addr1']}'>\n\t\t\t<input type='hidden' name='cusvatno' value='{$cust['vatnum']}'>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer</td>\n\t\t\t\t<td valign='center'>{$cust['cusname']} {$cust['surname']}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer Address</td>\n\t\t\t\t<td valign='center'><pre>{$cust['addr1']}</pre></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer VAT Number</td>\n\t\t\t\t<td valign='center'>{$cust['vatnum']}</td>\n\t\t\t</tr>";
    } elseif ($inv['ctyp'] == 'c') {
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE deptid = '{$inv['tval']}'";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        $dept = pg_fetch_array($deptRslt);
        $details = "\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer</td>\n\t\t\t\t<td valign='middle'><input type='text' name='cusname' value='{$inv['cusname']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t<td valign='middle'><textarea name='cusaddr' cols='18' rows='3'>{$inv['cusaddr']}</textarea></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Customer VAT No.</td>\n\t\t\t\t<td valign='middle'><input type='text' name='cusvatno' value='{$inv['cusvatno']}'></td>\n\t\t\t</tr>";
    } else {
        $details = "\n\t\t\t<tr>\n\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Customer</td>\n\t\t\t\t<td valign='middle'><input type='text' name='cusname' value='{$inv['cusname']}'></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t<td valign='middle'><textarea name='cusaddr' cols='18' rows='3'>{$inv['cusaddr']}</textarea></td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td valign='top'>Customer VAT No.</td>\n\t\t\t\t<td valign='middle'><input type='text' name='cusvatno' value='{$inv['cusvatno']}'></td>\n\t\t\t</tr>";
    }
    # Get sales people
    db_conn("exten");
    $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
    $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
    if (pg_numrows($salespRslt) < 1) {
        return "<li class='err'> There are no Sales People found in Cubit.</li>";
    } else {
        if (isset($cust) and is_array($cust) and strlen($inv['salespn']) < 1) {
            #if salespn == NULL search for customer default ...
            $get_salesp = "SELECT salesp FROM salespeople WHERE salespid = '{$cust['sales_rep']}' LIMIT 1";
            $run_salesp = db_exec($get_salesp) or errDie("Unable to get sales perosn information.");
            $inv['salespn'] = pg_fetch_result($run_salesp, 0, 0);
        }
        $salesps = "<select name='salespn'>";
        while ($salesp = pg_fetch_array($salespRslt)) {
            if ($salesp['salesp'] == $inv['salespn']) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
        }
        $salesps .= "</select>";
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM settings WHERE constant='SALES'";
    $Ri = db_exec($Sl) or errDie("Unable to get settings.");
    $data = pg_fetch_array($Ri);
    if ($data['value'] == "Yes") {
        $sc = "checked";
    } else {
        $sc = "";
    }
    $sales = "\n\t\t<td>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<td>{$salesps}</td>\n\t\t\t\t\t<td>Print</td>\n\t\t\t\t\t<td><input type='checkbox' name='printsales' {$sc}></td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t</td>";
    // Retrieve the default comments
    db_conn("cubit");
    $sql = "SELECT value FROM settings WHERE constant='DEFAULT_COMMENTS'";
    $cmntRslt = db_exec($sql) or errDie("Unable to retrieve default comment from Cubit.");
    if (empty($inv["remarks"])) {
        $remarks = base64_decode(pg_fetch_result($cmntRslt, 0));
    } else {
        $remarks = $inv["remarks"];
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    $deldate = explode("-", $inv["deldate"]);
    $showdoc_html = "'" . str_replace("<div style=\"text-align: left;\"><br></div>", "", $bodydata) . "'";
    if (isset($diffwhBtn) or isset($upBtn) or isset($doneBtn) or isset($print)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    /* -- Final Layout -- */
    $details = "\n\t\t<script language='JavaScript'>\n\t\t\tfunction update() {\n\t\t\t\tdocument.editForm.bodydata.value = editArea.document.body.innerHTML;\n\t\t\t\tdocument.editForm.submit();\n\t\t\t}\n\t\t\tfunction Init() {\n\t\t\t\teditArea.document.designMode = 'On';\n\t\t\t\teditArea.document.body.innerHTML = {$showdoc_html};\n\t\t\t\teditArea.document.execCommand('justifyleft', false, null);\n\t\t\t}\n\t\t\tfunction controlSelOn(ctrl) {\n\t\t\t\tctrl.style.borderColor = '#000000';\n\t\t\t\tctrl.style.backgroundColor = '#B5BED6';\n\t\t\t\tctrl.style.cursor = 'hand';\n\t\t\t}\n\t\t\tfunction controlSelOff(ctrl) {\n\t\t\t\tctrl.style.borderColor = '#D6D3CE';\n\t\t\t\tctrl.style.backgroundColor = '#D6D3CE';\n\t\t\t}\n\t\t\tfunction controlSelDown(ctrl) {\n\t\t\t\tctrl.style.backgroundColor = '#8492B5';\n\t\t\t}\n\t\t\tfunction controlSelUp(ctrl) {\n\t\t\t\tctrl.style.backgroundColor = '#B5BED6';\n\t\t\t}\n\t\t\tfunction doBold() {\n\t\t\t\teditArea.document.execCommand('bold', false, null);\n\t\t\t}\n\t\t\tfunction doItalic() {\n\t\t\t\teditArea.document.execCommand('italic', false, null);\n\t\t\t}\n\t\t\tfunction doUnderline() {\n\t\t\t\teditArea.document.execCommand('underline', false, null);\n\t\t\t}\n\t\t\tfunction doLeft() {\n\t\t\t\teditArea.document.execCommand('justifyleft', false, null);\n\t\t\t}\n\t\t\tfunction doCenter() {\n\t\t\t\teditArea.document.execCommand('justifycenter', false, null);\n\t\t\t}\n\t\t\tfunction doRight() {\n\t\t\t\teditArea.document.execCommand('justifyright', false, null);\n\t\t\t}\n\t\t\tfunction doOrdList() {\n\t\t\t\teditArea.document.execCommand('insertorderedlist', false, null);\n\t\t\t}\n\t\t\tfunction doBulList() {\n\t\t\t\teditArea.document.execCommand('insertunorderedlist', false, null);\n\t\t\t}\n\t\t\tfunction doRule() {\n\t\t\t\teditArea.document.execCommand('inserthorizontalrule', false, null);\n\t\t\t}\n\t\t\tfunction doSize(fSize) {\n\t\t\t\tif(fSize != '')\n\t\t\t\t\teditArea.document.execCommand('fontsize', false, fSize);\n\t\t\t}\n\t\t\twindow.onload = Init;\n\t\t</script>\n\t\t<center>\n\t\t<h3>New Multi Line Invoice</h3>\n\t\t<form action='" . SELF . "' method='POST' name='editForm' enctype='multipart/form-data'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t{$details}\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Customer Order number</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$inv['cordno']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'>Non-Stock Invoice Details</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Non-Stock Invoice No.</td>\n\t\t\t\t\t\t\t<td valign='center'>TI {$inv['invid']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Proforma Invoice No.</td>\n\t\t\t\t\t\t\t<td><input type='text' name='docref' value='{$inv['docref']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("ninv", $ninv_year, $ninv_month, $ninv_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='yes' {$chy}> No<input type=radio size=7 name=chrgvat value='no' {$chn}></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t\t{$sales}\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr><td><br></td></tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='nons-invoice-view.php'>View Non-Stock Invoices</a></td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='4' align='center' valign='top'><textarea name='remarks' rows='4' cols='20'>{$remarks}</textarea></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align=right>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='subtot' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$inv['vat']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right'>" . CUR . " <input type='hidden' name='total' value='{$TOTAL}'>{$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td align='right'><input type='submit' name='diffwhBtn' onClick='update();' value='Add Item'> |</td>\n\t\t\t\t<td><input type='submit' name='upBtn' onClick='update();' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}
コード例 #27
0
function details($_POST, $error = "")
{
    # Get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($invid)) {
        $v->isOk($invid, "num", 1, 20, "Invalid invoice number.");
    }
    if (isset($letter)) {
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    if (isset($deptid)) {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
    }
    if (isset($sel_frm)) {
        $v->isOk($sel_frm, "string", 6, 6, "Invalid select from selection.");
    }
    # display errors, if any
    if ($v->isError()) {
        $error = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return $error . view_err($_POST);
    }
    if (isset($deptid) && isset($letters)) {
        db_connect();
        if ($deptid == "0") {
            $searchdept = "";
        } else {
            $searchdept = "deptid = '{$deptid}' AND ";
        }
        # Query server for customer info
        $sql = "SELECT cusnum, cusname, surname FROM customers WHERE {$searchdept} location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
        $custRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($custRslt) < 1) {
            $ajax_err = "<li class='err'>No customer names starting with <b>{$letters}</b> in database.</li>";
            //return view_err($_POST, $err);
        }
    }
    if (!isset($deptid)) {
        $deptid = 2;
    } else {
        if (isset($invid)) {
            db_conn("cubit");
            $sql = "UPDATE invoices SET deptid='{$deptid}' WHERE invid='{$invid}' AND deptid<>'{$deptid}'";
            db_exec($sql) or errDie("Error updating invoice department.");
        }
    }
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
    }
    if (!isset($stkerr)) {
        $stkerr = "0,0";
    }
    if (!isset($done)) {
        $done = "";
    }
    if (!isset($cust_del_addr)) {
        $cust_del_addr = "";
    }
    if (!isset($sel_frm)) {
        $sel_frm = "stkcod";
    }
    # Get invoice info
    db_connect();
    $sql = "SELECT * FROM invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $invRslt = db_exec($sql) or errDie("Unable to get invoice information");
    if (pg_numrows($invRslt) < 1) {
        return "<li class='err'>Invoice Not Found</li>";
    }
    $inv = pg_fetch_array($invRslt);
    if (!isset($branch)) {
        $branch = $inv['branch'];
    }
    # Check if invoice has been printed
    if ($inv['printed'] == "y") {
        $error = "<li class='err'> Error : Invoice number <b>{$invid}</b> has already been printed.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # Get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected Customer info
    if (isset($letters)) {
        db_connect();
        $sql = "SELECT * FROM customers WHERE cusnum = '{$inv['cusnum']}' AND location != 'int' AND div = '" . USER_DIV . "'";
        $custRslt = db_exec($sql) or errDie("Unable to view customer");
        if (pg_numrows($custRslt) < 1) {
            if ($inv['deptid'] == 0) {
                $searchdept = "";
            } else {
                $searchdept = "deptid = '{$inv['deptid']}' AND ";
            }
            # Query server for customer info
            $sql = "SELECT cusnum, cusname, surname, accno FROM customers WHERE {$searchdept} location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
            $custRslt = db_exec($sql) or errDie("Unable to view customers");
            if (pg_numrows($custRslt) < 1) {
                $ajax_err = "<li class='err'>No customer names starting with <b>{$letters}</b> in database.</li>";
                //return view_err($_POST, $err);
            } else {
                $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
                if (pg_numrows($custRslt) == 1) {
                    $inv['cusnum'] = pg_fetch_result($custRslt, 0, 0);
                } else {
                    $customers .= "<option value='-S' selected>Select Customer</option>";
                }
                while ($cust = pg_fetch_array($custRslt)) {
                    $customers .= "<option value='{$cust['cusnum']}'>{$cust['cusname']} {$cust['surname']} ({$cust['accno']})</option>";
                }
                $customers .= "</select>";
            }
            # Take care of the unset vars
            $cust['addr1'] = "";
            $cust['cusnum'] = "";
            $cust['vatnum'] = "";
            $cust['accno'] = "";
            $cust_del_addr = "";
            $branchdrop = "<input type='hidden' name='branch' value='0'>";
        } else {
            $cust = pg_fetch_array($custRslt);
            #override address
            if ($branch != 0) {
                $get_addr = "SELECT branch_descrip FROM customer_branches WHERE id = '{$branch}' AND div = '" . USER_DIV . "' LIMIT 1";
                $run_addr = db_exec($get_addr);
                if (pg_numrows($run_addr) < 1) {
                    #address missing ... do nothing
                } else {
                    $arr = pg_fetch_array($run_addr);
                    $cust['addr1'] = $arr['branch_descrip'];
                    if ($inv['del_addr'] != $arr['branch_descrip']) {
                        $update_addr = "UPDATE invoices SET del_addr  = '{$arr['branch_descrip']}' WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
                    }
                }
            }
            #if the del_addr of invoice if empty, add the customer's entry and display button so user can edit anyway
            if (strlen($inv['del_addr']) < 1) {
                $cust_del_addr = $cust['del_addr1'];
            } else {
                $cust_del_addr = $inv['del_addr'];
            }
            $sql = "SELECT cusnum, cusname, surname FROM customers WHERE deptid = '{$inv['deptid']}' AND location != 'int' AND lower(surname) LIKE lower('{$letters}%') AND blocked != 'yes' AND div = '" . USER_DIV . "' ORDER BY surname";
            $cusRslt = db_exec($sql) or errDie("Unable to view customers");
            # Moarn if customer account has been blocked
            if ($cust['blocked'] == 'yes') {
                $error .= "<li class='err'>Error : Selected customer account has been blocked.</li>";
            }
            // $customers = "<input type=hidden name=cusnum value='$cust[cusnum]'>$cust[cusname]  $cust[surname]";
            $cusnum = $cust['cusnum'];
            $customers = "<select name='cusnum' onChange='javascript:document.form.submit();'>";
            // $customers .= "<option value='-S' selected>Select Customer</option>";
            while ($cus = pg_fetch_array($cusRslt)) {
                $sel = "";
                if ($cust['cusnum'] == $cus['cusnum']) {
                    $sel = "selected";
                }
                $customers .= "<option value='{$cus['cusnum']}' {$sel}>{$cus['cusname']} {$cus['surname']}</option>";
            }
            $customers .= "</select>";
            #get list of branches
            $get_branches = "SELECT * FROM customer_branches WHERE cusnum = '{$cusnum}' AND div = '" . USER_DIV . "'";
            $run_branches = db_exec($get_branches);
            if (pg_numrows($run_branches) < 1) {
                $branchdrop = "<input type='hidden' name='branch' value='0'>No Branches For This Customer";
            } else {
                $branchdrop = "<select name='branch' onChange='javascript:document.form.submit();'>";
                $branchdrop .= "<option value='0'>Head Office</option>";
                while ($barr = pg_fetch_array($run_branches)) {
                    $sel2 = "";
                    if ($barr['id'] == $branch) {
                        $sel2 = "selected";
                    }
                    $branchdrop .= "<option {$sel2} value='{$barr['id']}'>{$barr['branch_name']}</option>";
                }
                $branchdrop .= "</select>";
            }
        }
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    //old
    //$whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    //fixes broken new store function
    //STORE1
    $whs = "<select name='whidss[]'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> There are no Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    // Is the customer linked to a sales rep
    if (!empty($cust["sales_rep"])) {
        db_conn("exten");
        $sql = "SELECT salesp FROM salespeople WHERE salespid='{$cust['sales_rep']}'";
        $sr_rslt = db_exec($sql) or errDie("Unable to retrieve sales rep from Cubit.");
        $salespname = pg_fetch_result($sr_rslt, 0);
        $salesps = "<input type='hidden' name='salespn' value='{$salespname}'><b>[{$salespname}]</b>";
    } else {
        db_conn("exten");
        $sql = "SELECT * FROM salespeople WHERE div = '" . USER_DIV . "' ORDER BY salesp ASC";
        $salespRslt = db_exec($sql) or errDie("Unable to get sales people.");
        if (pg_numrows($salespRslt) < 1) {
            return "<li class='err'> There are no Sales People found in Cubit.</li>";
        } else {
            $salesps = "<select name='salespn'>";
            while ($salesp = pg_fetch_array($salespRslt)) {
                if ($salesp['salesp'] == $inv['salespn']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $salesps .= "<option value='{$salesp['salesp']}' {$sel}>{$salesp['salesp']}</option>";
            }
            $salesps .= "</select>";
        }
    }
    # Days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $inv['terms']);
    # Keep the charge vat option stable
    if ($inv['chrgvat'] == "inc") {
        $chin = "checked=yes";
        $chex = "";
        $chno = "";
    } elseif ($inv['chrgvat'] == "exc") {
        $chin = "";
        $chex = "checked=yes";
        $chno = "";
    } else {
        $chin = "";
        $chex = "";
        $chno = "checked=yes";
    }
    # Format date
    list($inv_date_year, $inv_date_month, $inv_date_day) = explode("-", $inv['odate']);
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # Select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . "' width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t<th>SERIAL NO.</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t<th>UNIT DISCOUNT</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>Remove</th>\n\t\t\t<tr>";
    # get selected stock in this invoice
    db_connect();
    $sql = "SELECT * FROM inv_items  WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    $ai = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            # Keep track of selected stock amounts
            $stkd["amt"] = sprint($stkd["amt"]);
            $amts[$i] = $stkd['amt'];
            $i++;
            $Accounts = "\n\t\t\t\t<select name='accounts[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
            $useaccdrop = getCSetting("USE_NON_STOCK_ACCOUNTS");
            if (isset($useaccdrop) and $useaccdrop == "yes") {
                db_connect();
                $acc_sql = "SELECT * FROM non_stock_account_list ORDER BY accname";
                $run_acc = db_exec($acc_sql) or errDie("Unable to get account information.");
                if (pg_numrows($run_acc) > 0) {
                    while ($acc = pg_fetch_array($run_acc)) {
                        if ($acc['accid'] == $stkd['account']) {
                            $Accounts .= "<option value='{$acc['accid']}' selected>{$acc['accname']}</option>";
                        } else {
                            $Accounts .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
                        }
                    }
                    $Accounts .= "</select>";
                }
            } else {
                db_conn('core');
                $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                while ($ad = pg_fetch_array($Ri)) {
                    if ($ad['accid'] == $stkd['account']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    $Accounts .= "<option value='{$ad['accid']}' {$sel}>{$ad['accname']}</option>";
                }
            }
            $Accounts .= "</select>";
            $sernos = "\n\t\t\t\t<input type='hidden' name='sernos[{$ai}]' value='{$stkd['serno']}'>\n\t\t\t\t<input type='hidden' name='sernos_ss[{$ai}]' value='{$stkd['serno']}'>";
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[{$ai}]' value='{$stkd['qty']}'>";
            $viewcost = "<input type='text' size='8' name='unitcost[{$ai}]' value='{$stkd['unitcost']}'>";
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            # Put in product
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>\n\t\t\t\t\t\t{$Accounts}\n\t\t\t\t\t\t<input type='hidden' name='whids[{$ai}]' value='{$stkd['whid']}'>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[{$ai}]' value='{$stkd['stkid']}'>{$Vatcodes}</td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptions[{$ai}]' value='{$stkd['description']}'> {$sernos}</td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td><input type='hidden' name='disc[{$ai}]' value='{$stkd['disc']}'><input type='hidden' name='discp[{$ai}]' value='{$stkd['discp']}'></td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[{$ai}]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[{$ai}]' value='{$key}'></td>\n\t\t\t\t</tr>";
            $key++;
            ++$ai;
        } else {
            # Keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # Get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # Get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            # Serial number
            if ($stk['serd'] == 'yes' && ($inv['serd'] == 'n' || $stkd["serno"] == "")) {
                $sers = ext_getavserials($stkd['stkid']);
                $sernos = "<select name='sernos[{$ai}]'>";
                foreach ($sers as $skey => $ser) {
                    $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
                }
                $sernos .= "</select>\n\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value='*_*_*CUBIT_SERIAL_SELECT_BOX*_*_*' />";
            } else {
                $sernos = "\n\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value='{$stkd['ss']}' />\n\t\t\t\t\t<input type='hidden' name='sernos[{$ai}]' value='{$stkd['serno']}'>{$stkd['ss']}";
            }
            # Input qty if not serialised
            $qtyin = "<input type='text' size='3' name='qtys[{$ai}]' value='{$stkd['qty']}'>";
            if ($stk['serd'] == 'yes') {
                $qtyin = "<input type='hidden' size='3' name='qtys[{$ai}]' value='{$stkd['qty']}'>{$stkd['qty']}";
            }
            # check permissions
            if (perm("invoice-unitcost-edit.php")) {
                $viewcost = "<input type='text' size='8' name='unitcost[{$ai}]' value='{$stkd['unitcost']}'>";
            } else {
                $viewcost = "<input type='hidden' size='8' name='unitcost[{$ai}]' value='{$stkd['unitcost']}'>{$stkd['unitcost']}";
            }
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            # Put in product
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='hidden' name='accounts[{$ai}]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='descriptions[{$ai}]' value=''>\n\t\t\t\t\t\t<input type='hidden' name='whids[{$ai}]' value='{$stkd['whid']}'>{$wh['whname']}\n\t\t\t\t\t</td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td>{$sernos}</td>\n\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t<td><input type='text' size='4' name='disc[{$ai}]' value='{$stkd['disc']}'> OR <input type='text' size='4' name='discp[{$ai}]' value='{$stkd['discp']}' maxlength='5'>%</td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[{$ai}]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[{$ai}]' value='{$key}'></td>\n\t\t\t\t</tr>";
            $key++;
            ++$ai;
        }
    }
    # Look above(remprod keys)
    $keyy = $key;
    # Look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    } else {
        $SCROLL = "yes";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S" && isset($cust['pricelist'])) {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # Get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # Get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whid, whname FROM warehouses WHERE whid = (select whid from cubit.stock where stkid = '{$stk['stkid']}' limit 1) AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                if ($stk['serd'] == 'yes') {
                    $sers = ext_getavserials($stkidss[$key]);
                    $sernos = "<select name='sernos[]' onChange='javascript:document.form.submit();'>";
                    foreach ($sers as $skey => $ser) {
                        $sernos .= "<option value='{$ser['serno']}'>{$ser['serno']}</option>";
                    }
                    $sernos .= "</select>\n\t\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value='*_*_*CUBIT_SERIAL_SELECT_BOX*_*_*' />";
                } else {
                    $sernos = "\n\t\t\t\t\t\t<input type='hidden' name='sernos_ss[]' value=''>\n\t\t\t\t\t\t<input type='hidden' name='sernos[{$ai}]' value=''>";
                }
                # Get price from price list if it is set
                if (isset($cust['pricelist'])) {
                    # get selected stock in this warehouse
                    db_conn("exten");
                    $sql = "SELECT price FROM plist_prices WHERE listid = '{$cust['pricelist']}' AND stkid = '{$stk['stkid']}' AND div = '" . USER_DIV . "'";
                    $plRslt = db_exec($sql);
                    if (pg_numrows($plRslt) > 0) {
                        $pl = pg_fetch_array($plRslt);
                        $stk['selamt'] = $pl['price'];
                    }
                }
                /* -- Start Some Checks -- */
                # check if they are selling too much
                if ($stk['units'] - $stk['alloc'] < $qtyss[$key]) {
                    if (!in_array($stk['stkid'], explode(",", $stkerr))) {
                        if ($stk['type'] != 'lab') {
                            $stkerr .= ",{$stk['stkid']}";
                            $error .= "<li class='err'>Warning : Item number <b>{$stk['stkcod']}</b> does not have enough items available.</li>";
                        }
                    }
                }
                if ($stk['units'] <= $stk['minlvl'] && $stk['minlvl'] != 0) {
                    $error .= "<li class='err'>{$stk['stkcod']} is below minimum level, please notify stock controller.</li>";
                }
                /* -- End Some Checks -- */
                # Calculate the Discount discount
                if ($discs[$key] < 1) {
                    if ($discps[$key] > 0) {
                        $discs[$key] = round($discps[$key] / 100 * $stk['selamt'], 2);
                    }
                } else {
                    $discps[$key] = round($discs[$key] * 100 / $stk['selamt'], 2);
                }
                # Calculate amount
                $amt[$key] = $qtyss[$key] * ($stk['selamt'] - $discs[$key]);
                # Input qty if not serialised
                $qtyin = "<input type='text' size='3' name='qtemp' value='{$qtyss[$key]}'>";
                if ($stk['serd'] == 'yes') {
                    $qtyin = "<input type='hidden' size='3' name='qtemp' value='{$qtyss[$key]}'>{$qtyss[$key]}";
                }
                #clean some vars
                $stk['selamt'] = sprint($stk['selamt']);
                $amt[$key] = sprint($amt[$key]);
                # Check permissions
                if (perm("invoice-unitcost-edit.php")) {
                    $viewcost = "<input type='text' size='8' name='unitcost[{$ai}]' value='{$stk['selamt']}'>";
                } else {
                    $viewcost = "<input type='hidden' size='8' name='unitcost[{$ai}]' value='{$stk['selamt']}'>{$stk['selamt']}";
                }
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[{$ai}]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<input type='hidden' name='accounts[{$ai}]' value='0'>\n\t\t\t\t\t\t\t<input type='hidden' name='descriptions[{$ai}]' value=''>\n\t\t\t\t\t\t\t<input type='hidden' name='whids[]' value='{$wh['whid']}'>{$wh['whname']}\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<td><input type='hidden' name='stkids[{$ai}]' value='{$stk['stkid']}'><a href='#bottom' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t<td>{$sernos}</td>\n\t\t\t\t\t\t<td>" . extlib_rstr($stk['stkdes'], 30) . "</td>\n\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t<td><input type='text' size='4' name='disc[{$ai}]' value='{$discs[$key]}'> OR <input type='text' size='4' name='discp[{$ai}]' value='{$discps[$key]}' maxlength='5'>%</td>\n\t\t\t\t\t\t<td nowrap><input type='hidden' name='amt[{$ai}]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[{$ai}]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
                ++$ai;
            } elseif (isset($accountss[$key]) && $accountss[$key] != "0" && isset($cust['pricelist'])) {
                db_conn('core');
                $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                if (pg_num_rows($Ri) < 1) {
                    return "invalid.";
                }
                $ad = pg_fetch_array($Ri);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * $unitcosts[$key]);
                # Input qty if not serialised
                $qtyin = "<input type='text' size='3' name='qtemp' value='{$qtyss[$key]}'>";
                # Check permissions
                $viewcost = "<input type='text' size='8' name='unitcost[{$ai}]' value='{$unitcosts[$key]}'>";
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[{$ai}]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vatcodess[$key] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='2'>\n\t\t\t\t\t\t\t{$ad['accname']}\n\t\t\t\t\t\t\t<input type='hidden' name='accounts[{$ai}]' value='{$accountss[$key]}'>\n\t\t\t\t\t\t\t<input type='hidden' name='whids[{$ai}]' value='0'>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<td>{$Vatcodes}<input type='hidden' name='stkids[{$ai}]' value='0'></td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='descriptions[{$ai}]' value='{$descriptionss[$key]}'></td>\n\t\t\t\t\t\t<td>{$qtyin}</td>\n\t\t\t\t\t\t<td>{$viewcost}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='disc[{$ai}]' value='0'><input type='hidden' name='discp[{$ai}]' value='0'></td>\n\t\t\t\t\t\t<td nowrap><input type='hidden' name='amt[{$ai}]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[{$ai}]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
                $keyy++;
                ++$ai;
            } else {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                if (!isset($addnon)) {
                    if ($filter_store != "0") {
                        # get warehouse name
                        db_conn("exten");
                        $sql = "SELECT whname FROM warehouses WHERE whid = '{$filter_store}' AND div = '" . USER_DIV . "'";
                        $whRslt = db_exec($sql);
                        $wh = pg_fetch_array($whRslt);
                    }
                    if (isset($ria) and $ria != "") {
                        $len = strlen($ria);
                        if ($ria == "Show All") {
                            $Wh = "";
                            $ria = "";
                        } else {
                            $Wh = "AND (lower(stkdes) LIKE lower('%{$ria}%')) OR (lower(stkcod) LIKE lower('%{$ria}%'))";
                            $ria = "";
                        }
                    } else {
                        $Wh = "AND FALSE";
                        $ria = "";
                    }
                    $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
                    if (isset($check_setting) and $check_setting == "yes") {
                        if (isset($filter_class) and $filter_class != "0") {
                            $Wh .= " AND prdcls = '{$filter_class}'";
                        }
                        if (isset($filter_cat) and $filter_cat != "0") {
                            $Wh .= " AND catid = '{$filter_cat}'";
                        }
                    }
                    if (isset($filter_store) and $filter_store != "0") {
                        $Wh .= " AND whid = '{$filter_store}'";
                    }
                    # get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY {$sel_frm} ASC";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        $error .= "<li class='err'>There are no stock items in the selected warehouse.";
                        continue;
                    }
                    if (pg_numrows($stkRslt) == 1) {
                        $ex = "selected";
                        $reload = TRUE;
                        $send_override = "<input type='hidden' name='stkidss[{$ai}]' value='" . pg_fetch_result($stkRslt, 0, 0) . "'><script>document.form.submit();</script>";
                    } else {
                        $ex = "";
                        $reload = FALSE;
                    }
                    if ($sel_frm == "stkcod") {
                        $cods = "<select name='stkidss[{$ai}]' onChange='javascript:document.form1.submit();'>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            // Check if this stock item has been blocked
                            //pointless, as we only get non blocked stock ...
                            //if (stock_is_blocked($stk["stkid"])) {
                            //	continue;
                            //}
                            $cods .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $cods .= "</select> ";
                        $descs = "";
                    } else {
                        $descs = "<select style='width:250px'name='stkidss[{$ai}]' onChange='javascript:document.form.submit();'>";
                        $descs .= "<option value='-S' disabled selected>Select Description</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            // Check if this stock item has been blocked
                            //pointless, as ge only get non blocked stock
                            //								if (stock_is_blocked($stk["stkid"])) {
                            //									continue;
                            //								}
                            $descs .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $descs .= "</select> ";
                        $cods = "";
                    }
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[{$ai}]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vd['del'] == "Yes") {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    # put in drop down and warehouse
                    $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='hidden' name='accountss[{$ai}]' value='0'>\n\t\t\t\t\t\t\t\t<input type='hidden' name='descriptionss[{$ai}]' value=''>\n\t\t\t\t\t\t\t\t<input type='hidden' name='whidss[{$ai}]' value='{$filter_store}'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td>{$cods}</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='vatcodess' value='0'></td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td>{$descs}</td>\n\t\t\t\t\t\t\t<td><input type='hidden' size='3' name='qtyss[{$ai}]'  value='1'>1</td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td><input type='text' size='4' name='discs[{$ai}]' value='0'> OR <input type='text' size='4' name='discps[{$ai}]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t\t\t<td nowrap><input type='hidden' name='amts[{$ai}]' value='0.00'>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t</tr>";
                    ++$ai;
                } else {
                    $Accounts = "\n\t\t\t\t\t\t<select name='accountss[{$ai}]' onChange='javascript:document.form.submit();'>\n\t\t\t\t\t\t\t<option value='0'>Select Account</option>";
                    $useaccdrop = getCSetting("USE_NON_STOCK_ACCOUNTS");
                    if (isset($useaccdrop) and $useaccdrop == "yes") {
                        db_connect();
                        $acc_sql = "SELECT * FROM non_stock_account_list ORDER BY accname";
                        $run_acc = db_exec($acc_sql) or errDie("Unable to get account information.");
                        if (pg_numrows($run_acc) > 0) {
                            while ($acc = pg_fetch_array($run_acc)) {
                                $Accounts .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
                            }
                            $Accounts .= "</select>";
                        }
                    } else {
                        db_conn('core');
                        $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                        $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                        while ($ad = pg_fetch_array($Ri)) {
                            if (isb($ad['accid'])) {
                                continue;
                            }
                            $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                        }
                        $Accounts .= "</select>";
                    }
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[{$ai}]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vd['del'] == "Yes") {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td colspan='2'>\n\t\t\t\t\t\t\t\t{$Accounts}\n\t\t\t\t\t\t\t\t<input type='hidden' name='whidss[{$ai}]' value='1'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<inpu type='hidden' name='stkidss[{$ai}]' value=''>\n\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[{$ai}]'></td>\n\t\t\t\t\t\t\t<td><input type='text' size='3' name='qtyss[{$ai}]' value='1'></td>\n\t\t\t\t\t\t\t<td><input type='text' name='unitcosts[{$ai}]' size='7'></td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t<td nowrap>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='discs[{$ai}]' value='0'><input type='hidden' name='discps[{$ai}]' value='0'></td>\n\t\t\t\t\t\t</tr>";
                    ++$ai;
                }
            }
        }
    } else {
        #HAX !
        if (!isset($addnon) && !isset($upBtn)) {
            if (isset($ria) and $ria != "") {
                $len = strlen($ria);
                if ($ria == "Show All") {
                    $Wh = "";
                    $ria = "";
                } else {
                    $Wh = "AND (lower(stkdes) LIKE lower('%{$ria}%')) OR (lower(stkcod) LIKE lower('%{$ria}%'))";
                    $ria = "";
                }
            } else {
                $Wh = "AND FALSE";
                $ria = "";
            }
            $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
            if (isset($check_setting) and $check_setting == "yes") {
                if (isset($filter_class) and $filter_class != "0") {
                    $Wh .= " AND prdcls = '{$filter_class}'";
                }
                if (isset($filter_cat) and $filter_cat != "0") {
                    $Wh .= " AND catid = '{$filter_cat}'";
                }
            }
            if (isset($filter_store) and $filter_store != "0") {
                $Wh .= " AND whid = '{$filter_store}'";
            }
            # get stock on this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE blocked = 'n' AND div = '" . USER_DIV . "' {$Wh} ORDER BY {$sel_frm} ASC";
            $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
            if (pg_numrows($stkRslt) < 1) {
                if (!isset($err)) {
                    $err = "";
                }
                $err .= "<li>There are no stock items in the selected store.</li>";
                //ontinue;
            }
            if (pg_numrows($stkRslt) == 1) {
                $reload = TRUE;
                $send_override = "<input type='hidden' name='stkidss[{$ai}]' value='" . pg_fetch_result($stkRslt, 0, 0) . "'>";
            } else {
                $reload = FALSE;
                $send_override = "";
            }
            if ($sel_frm == "stkcod") {
                $cods = "<select name='stkidss[{$ai}]' onChange='javascript:document.form.submit();'>";
                //					$cods .= "<option value='-S' disabled selected>Select Number</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $cods .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $cods .= "</select> ";
                $descs = "";
            } else {
                $descs = "<select style='width:250px' name='stkidss[{$ai}]' onChange='javascript:document.form.submit();'>";
                $descs .= "<option value='-S' disabled selected>Select Description</option>";
                $count = 0;
                while ($stk = pg_fetch_array($stkRslt)) {
                    $descs .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                }
                $descs .= "</select> ";
                $cods = "";
            }
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='hidden' name='accountss[{$ai}]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='descriptionss[{$ai}]' value=''>\n\t\t\t\t\t\t<input type='hidden' name='vatcodess[{$ai}]' value=''>\n\t\t\t\t\t\t<input type='hidden' name='whidss[{$ai}]' value='{$filter_store}'>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td>{$cods}</td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td>{$descs}</td>\n\t\t\t\t\t<td><input type='hidden' size='3' name='qtyss[{$ai}]' value='1'>1</td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td><input type='text' size='4' name='discs[{$ai}]' value='0'> OR <input type='text' size='4' name='discps[{$ai}]' value='0' maxlength='5'>%</td>\n\t\t\t\t\t<td nowrap>" . CUR . " 0.00</td>\n\t\t\t\t\t<td></td>\n\t\t\t\t</tr>";
            ++$ai;
        } elseif (isset($addnon)) {
            $Accounts = "\n\t\t\t\t<select name='accountss[{$ai}]'  onChange='javascript:document.form.submit();'>\n\t\t\t\t\t<option value='0'>Select Account</option>";
            $useaccdrop = getCSetting("USE_NON_STOCK_ACCOUNTS");
            if (isset($useaccdrop) and $useaccdrop == "yes") {
                db_connect();
                $acc_sql = "SELECT * FROM non_stock_account_list ORDER BY accname";
                $run_acc = db_exec($acc_sql) or errDie("Unable to get account information.");
                if (pg_numrows($run_acc) > 0) {
                    while ($acc = pg_fetch_array($run_acc)) {
                        $Accounts .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
                    }
                    $Accounts .= "</select>";
                }
            } else {
                db_conn('core');
                $Sl = "SELECT accid,topacc,accnum,accname FROM accounts WHERE acctype='I' ORDER BY accname";
                $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                while ($ad = pg_fetch_array($Ri)) {
                    if (isb($ad['accid'])) {
                        continue;
                    }
                    $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                }
                $Accounts .= "</select>";
            }
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodess[{$ai}]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($vd['del'] == "Yes") {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td colspan='2'>{$Accounts}<input type='hidden' name='whidss[{$ai}]' value='1'></td>\n\t\t\t\t\t<inpu type='hidden' name='stkidss[{$ai}]' value=''>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[{$ai}]'></td>\n\t\t\t\t\t<td><input type='text' size='3' name='qtyss[{$ai}]' value='1'></td>\n\t\t\t\t\t<td><input type='text' name='unitcosts[{$ai}]' size='7'></td>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td nowrap>" . CUR . " 0.00</td>\n\t\t\t\t\t<td><input type='hidden' name='discs[{$ai}]' value='0'><input type='hidden' name='discps[{$ai}]' value='0' ></td>\n\t\t\t\t</tr>";
            ++$ai;
        }
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    # Calculate tradediscm
    if ($inv['traddisc'] > 0) {
        $traddiscm = sprint($inv['traddisc'] / 100 * $inv['total']);
    } else {
        $traddiscm = "0.00";
    }
    $VATP = TAX_VAT;
    # Calculate subtotal
    $SUBTOT = sprint($inv['subtot']);
    $VAT = sprint($inv['vat']);
    $TOTAL = sprint($inv['total']);
    $inv['delchrg'] = sprint($inv['delchrg']);
    /* --- End Some calculations --- */
    /*--- Start checks --- */
    # check only if the customer is selected
    if (isset($cusnum) && $cusnum != "-S") {
        #check againg credit limit
        if ($TOTAL + $cust['balance'] > $cust['credlimit']) {
            $error .= "<li class='err'>Warning : Customers Credit limit of <b>" . CUR . " " . sprint($cust["credlimit"]) . "</b> has been exceeded";
            #limit reached ... check for warn/block
            db_conn("cubit");
            $get_check = "SELECT value FROM set WHERE label = 'CUST_INV_WARN' LIMIT 1";
            $run_check = db_exec($get_check) or errDie("Unable to get credit limit response setting");
            if (pg_numrows($run_check) < 1) {
                #no setting ? do nothing ....
            } else {
                $sarr = pg_fetch_array($run_check);
                if ($sarr['value'] == "block") {
                    #block account ...
                    $done = "";
                }
            }
            # Check permissions
            if (!perm("invoice-limit-override.php")) {
                $done = "";
            }
        }
        $avcred = $cust['credlimit'] - $cust['balance'];
    } else {
        $avcred = "0.00";
    }
    /*--- Start checks --- */
    db_conn('cubit');
    $Sl = "SELECT * FROM settings WHERE constant='SALES'";
    $Ri = db_exec($Sl) or errDie("Unable to get settings.");
    $data = pg_fetch_array($Ri);
    if ($data['value'] == "Yes") {
        $sc = "checked";
    } else {
        $sc = "";
    }
    $sales = "\n\t\t<td>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<td>{$salesps}</td>\n\t\t\t\t\t<td>Print</td>\n\t\t\t\t\t<td><input type='checkbox' name='printsales' {$sc}></td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t</td>";
    $inv['delvat'] += 0;
    if ($inv['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $inv['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $inv['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    db_conn('cubit');
    $Sl = "SELECT * FROM settings WHERE constant='Delivery Note'";
    $Ri = db_exec($Sl) or errDie("Unable to get settings.");
    $data = pg_fetch_array($Ri);
    if ($data['value'] == "Yes") {
        $chp = "checked";
    } else {
        $chp = "";
    }
    if (empty($inv["comm"])) {
        db_conn("cubit");
        $sql = "SELECT value FROM settings WHERE constant='DEFAULT_COMMENTS'";
        $cmntRslt = db_exec($sql) or errDie("Unable to retrieve the default comment from Cubit.");
        $comm = base64_decode(pg_fetch_result($cmntRslt, 0));
    } else {
        $comm = $inv["comm"];
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    // Which display method was selected
    if (isset($sel_frm) && $sel_frm == "stkdes") {
        $sel_frm_cod = "";
        $sel_frm_des = "checked";
    } else {
        $sel_frm_cod = "checked";
        $sel_frm_des = "";
    }
    // Delivery Date
    if (!empty($inv["deldate"])) {
        $deldate = explode("-", $inv["deldate"]);
    } else {
        $trans_date_setting = getCSetting("USE_TRANSACTION_DATE");
        if (isset($trans_date_setting) and $trans_date_setting == "yes") {
            $trans_date_value = getCSetting("TRANSACTION_DATE");
            $date_arr = explode("-", $trans_date_value);
            $deldate[0] = $date_arr[0];
            $deldate[1] = $date_arr[1];
            $deldate[2] = $date_arr[2];
        } else {
            $deldate[0] = date("Y");
            $deldate[1] = date("m");
            $deldate[2] = date("d");
        }
    }
    $details_begin = "\n\t\t<center>\n\t\t<h3>New Invoice</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='SCROLL' value='yes'>\n\t\t\t<input type='hidden' name='key' value='update'>\n\t\t\t<input type='hidden' name='invid' value='{$invid}'>\n\t\t\t<input type='hidden' name='stkerr' value='{$stkerr}'>\n\t\t<table " . TMPL_tblDflts . " width='95%'>\n\t\t \t<tr>\n\t\t \t\t<td valign='top' width='50%'>\n\t\t \t\t\t<div id='cust_selection'>";
    if (empty($ajax_err) && (isset($cusnum) || AJAX)) {
        if (isset($cusnum)) {
            $OTS_OPT = onthespot_encode(SELF, "cust_selection", "deptid={$inv['deptid']}&letters={$letters}&cusnum={$cusnum}&invid={$invid}");
            $custedit = "\n\t\t\t\t<td nowrap>\n\t\t\t\t\t<a href='javascript: popupSized(\"customers-new.php?cusnum={$cusnum}&onthespot={$OTS_OPT}\", \"edit_cust\", 700, 630);'>Edit Customer Details</a>\n\t\t\t\t</td>";
        } else {
            $custedit = "";
        }
        $optional_filter_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
        if (isset($optional_filter_setting) and $optional_filter_setting == "yes") {
            db_connect();
            $catsql = "SELECT catid, cat, catcod FROM stockcat WHERE div = '" . USER_DIV . "' ORDER BY cat ASC";
            $catRslt = db_exec($catsql);
            if (pg_numrows($catRslt) < 1) {
                $cat_drop = "<input type='hidden' name='filter_cat' value='0'>";
            } else {
                $cat_drop = "<select name='filter_cat'>";
                $cat_drop .= "<option value='0'>All Categories</option>";
                while ($cat = pg_fetch_array($catRslt)) {
                    if (isset($filter_cat) and $filter_cat == $cat['catid']) {
                        $cat_drop .= "<option value='{$cat['catid']}' selected>({$cat['catcod']}) {$cat['cat']}</option>";
                    } else {
                        $cat_drop .= "<option value='{$cat['catid']}'>({$cat['catcod']}) {$cat['cat']}</option>";
                    }
                }
                $cat_drop .= "</select>";
            }
            # Select classification
            $classsql = "SELECT * FROM stockclass WHERE div = '" . USER_DIV . "' ORDER BY classname ASC";
            $clasRslt = db_exec($classsql);
            if (pg_numrows($clasRslt) < 1) {
                $class_drop = "<input type='hidden' name='filter_class' value='0'>";
            } else {
                $class_drop = "<select name='filter_class' style='width: 167'>";
                $class_drop .= "<option value='0'>All Classifications</option>";
                while ($clas = pg_fetch_array($clasRslt)) {
                    if (isset($filter_class) and $filter_class == $clas['clasid']) {
                        $class_drop .= "<option value='{$clas['clasid']}' selected>{$clas['classname']}</option>";
                    } else {
                        $class_drop .= "<option value='{$clas['clasid']}'>{$clas['classname']}</option>";
                    }
                }
                $class_drop .= "</select>";
            }
            $display_optional_filters = "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Category</td>\n\t\t\t\t\t<td>{$cat_drop}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Classification</td>\n\t\t\t\t\t<td>{$class_drop}</td>\n\t\t\t\t</tr>";
        }
        db_conn("exten");
        $sql = "SELECT whid, whname, whno FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
        $whRslt = db_exec($sql);
        if (pg_numrows($whRslt) < 1) {
            $store_drop = "<input type='hidden' name='filter_store' value='0'>";
        } else {
            if (!isset($filter_store)) {
                # check if setting exists
                db_connect();
                $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
                $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
                if (pg_numrows($Rslt) > 0) {
                    $set = pg_fetch_array($Rslt);
                    $filter_store = $set['value'];
                }
            }
            $store_drop = "<select name='filter_store'>";
            $store_drop .= "<option value='0'>All Stores</option>";
            while ($wh = pg_fetch_array($whRslt)) {
                if ($wh['whid'] == $filter_store) {
                    $store_drop .= "<option value='{$wh['whid']}' selected>({$wh['whno']}) {$wh['whname']}</option>";
                } else {
                    $store_drop .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
                }
            }
            $store_drop .= "</select>";
        }
        $ajaxOut = "\n\t\t\t<input type='hidden' name='letters' value='{$letters}'>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'> Customer Details </th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Account No.</td>\n\t\t\t\t\t<td valign='center'>{$cust['accno']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer</td>\n\t\t\t\t\t<td valign='center'>{$customers}</td>\n\t\t\t\t\t{$custedit}\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Customer Branch</td>\n\t\t\t\t\t<td valign='center'>{$branchdrop}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Customer Address</td>\n\t\t\t\t\t<td valign='center'>" . nl2br($cust['addr1']) . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Current Delivery Address</td>\n\t\t\t\t\t<td valign='center'>" . nl2br($cust_del_addr) . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td valign='top'>Delivery Address</td>\n\t\t\t\t\t<td valign='center'>\n\t\t\t\t\t\t<input type='button' onClick=\"\n\t\t\t\t\t\t\tvar windowReference;\n\n\t\t\t\t\t\t\tfunction openPopup() {\n\t\t\t\t\t\t\t\twindowReference = window.open('cust-credit-stockinv-deladdr.php?invid={$invid}','windowName','width=500,height=400,status=1');\n\t\t\t\t\t\t\t\tif (!windowReference.opener)\n\t\t\t\t\t\t\t\t\twindowReference.opener = self;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\topenPopup();\" value='Change Delivery Address'>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer Order number</td>\n\t\t\t\t\t<td valign='center'><input type='text' size='10' name='cordno' value='{$inv['cordno']}'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer VAT Number</td>\n\t\t\t\t\t<td>{$cust['vatnum']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'>Point of Sale</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Barcode</td>\n\t\t\t\t\t<td><input type='text' size='13' name='bar' value=''></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Print Delivery Note</td>\n\t\t\t\t\t<td><input type='checkbox' name='printdel' {$chp}></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Using</td>\n\t\t\t\t\t<td>Stock Code<input type='radio' name='sel_frm' value='stkcod' onChange='javascript:document.form.submit();' {$sel_frm_cod}><br>Stock Description<input type='radio' name='sel_frm' value='stkdes' onChange='javascript:document.form.submit();' {$sel_frm_des}></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'>Additional Filters</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Store</td>\n\t\t\t\t\t<td>{$store_drop}</td>\n\t\t\t\t</tr>\n\t\t\t\t{$display_optional_filters}\n\t\t\t\t<tr class='" . bg_class() . "' " . ass("Type the first letters of the stock code you are looking for.") . ">\n\t\t\t\t\t<td>Stock Filter</td>\n\t\t\t\t\t<td nowrap><input type='text' size='13' name='ria' value='{$ria}'> <input type='submit' value='Search'> <input type='submit' name='ria' value='Show All'></td>\n\t\t\t\t</tr>\n\t\t\t</table>";
    } else {
        db_conn("exten");
        $sql = "SELECT * FROM departments WHERE div = '" . USER_DIV . "' ORDER BY deptname ASC";
        $deptRslt = db_exec($sql) or errDie("Unable to view customers");
        if (pg_numrows($deptRslt) < 1) {
            return "<li class='err'>There are no Departments found in Cubit.</li>";
        } else {
            $depts = "<select name='deptid' id='deptid'>";
            $depts .= "<option value='0'>All Departments</option>";
            while ($dept = pg_fetch_array($deptRslt)) {
                $depts .= "<option value='{$dept['deptid']}'>{$dept['deptname']}</option>";
            }
            $depts .= "</select>";
        }
        if (!isset($ajax_err)) {
            $ajax_err = "";
        }
        $ajaxOut = "\n\t\t\t<script>\n\t\t\t\tfunction updateCustSelection() {\n\t\t\t\t\tdeptid = getObject('deptid').value;\n\t\t\t\t\tletters = getObject('letters').value;\n\t\t\t\t\tctyp = getObject('ctypintl').checked;\n\t\t\t\t\tif (ctyp) {\n\t\t\t\t\t\tdocument.location.href='intinvoice-new.php?' + 'letters=' + letters + '&deptid=' + deptid + '&ctyp=' + ctyp + '&invid={$invid}';\n\t\t\t\t\t} else {\n\t\t\t\t\t\tajaxRequest('" . SELF . "', 'cust_selection', AJAX_SET, 'letters='+letters+'&deptid='+deptid+'&ctyp='+ctyp+'&invid={$invid}');\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t</script>\n\t\t\t{$ajax_err}\n\t\t\t<form name='cusselfrm'>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th colspan='2'>Customer Selection</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Select Department</td>\n\t\t\t\t\t<td valign='center'>{$depts}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>First Letters of customer</td>\n\t\t\t\t\t<td valign='center'><input type='text' size='5' id='letters' maxlength='5'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>Customer is International</td>\n\t\t\t\t\t<td valign='center'><input type='checkbox' id='ctypintl'></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td><br /></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td valign='center'><input type='button' value='Update &raquo' onClick='updateCustSelection();'></td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t</form>";
    }
    $avcred = sprint($avcred);
    if (isset($addprodBtn) or isset($addnon) or isset($saveBtn) or isset($upBtn) or isset($ria)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $details_end = "\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td valign='top' align='right' width='50%'>\n\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th colspan='2'> Invoice Details </th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Invoice No.</td>\n\t\t\t\t\t\t\t\t<td valign='center'>TI {$inv['invid']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Proforma Inv No.</td>\n\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='docref' value='{$inv['docref']}'></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Sales Order No.</td>\n\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='ordno' value='{$inv['ordno']}'></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t\t<td valign='center'>Yes <input type='radio' size='7' name='chrgvat' value='inc' {$chin}> No<input type='radio' size='7' name='chrgvat' value='exc' {$chex}></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Sales Person</td>\n\t\t\t\t\t\t\t\t{$sales}\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Invoice Date</td>\n\t\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("inv_date", $inv_date_year, $inv_date_month, $inv_date_day) . "</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Available Credit</td>\n\t\t\t\t\t\t\t\t<td>" . CUR . " {$avcred}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='5' name='traddisc' value='{$inv['traddisc']}'>%</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t\t<td valign='center'><input type='text' size='7' name='delchrg' value='{$inv['delchrg']}'>{$Vatcodes}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Delivery Date</td>\n\t\t\t\t\t\t\t\t<td valign='center'>" . mkDateSelect("del_date", $deldate[0], $deldate[1], $deldate[2]) . "</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t\t\t\t" . TBL_BR . "\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<p>\n\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t\t<th width='25%'>Comments</th>\n\t\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$error}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='customers-new.php?re={$inv['invid']}'>New Customer</a></td>\n\t\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='5' align='center' valign='top'><textarea name='comm' rows='4' cols='20'>{$comm}</textarea></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='cust-credit-stockinv.php'>New Invoice</a></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><a href='invoice-view.php'>View Invoices</a></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><a href='settings/cust-credit-stockinv-newsetting.php?change=yes'>Change Invoice Complete Setting</a></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td align='right' valign='top'>\n\t\t\t\t\t\t<table " . TMPL_tblDflts . " width='50%'>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " <input type='hidden' name='SUBTOT' value='{$SUBTOT}'>{$SUBTOT}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Trade Discount</td>\n\t\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$inv['discount']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td>Delivery Charge</td>\n\t\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$inv['delivery']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<td><b>VAT {$vat14}</b></td>\n\t\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td align='right'><input name='addprodBtn' type='submit' value='Add Product'> | <input name='addnon' type='submit' value='Add Non stock Product'> | <input type='submit' name='saveBtn' value='Save'> </td>\n\t\t\t\t\t<td nowrap>| <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t<a name='bottom'>\n\t\t\t</form>\n\t\t\t</center>\n\t\t\t{$jump_bot}";
    if (!isset($reload)) {
        $reload = FALSE;
        $send_override = "";
    }
    if ($reload) {
        $details_end .= "\n\t\t\t\t\t\t{$send_override}\n\t\t\t\t\t\t<script>\n\t\t\t\t\t\t\tonLoad=document.form.submit();\n\t\t\t\t\t\t</script>\n\t\t\t\t\t";
    }
    if (AJAX) {
        return $ajaxOut;
    } else {
        return "{$details_begin}{$ajaxOut}{$details_end}";
    }
}
コード例 #28
0
function details($_POST, $error = "")
{
    extract($_REQUEST);
    $fields = array();
    $fields["deptid"] = 2;
    $fields["cusnum"] = 0;
    $fields["telno"] = "";
    $fields["cordno"] = "";
    $fields["des"] = "";
    $fields["pinv_day"] = date("d");
    $fields["pinv_month"] = date("m");
    $fields["pinv_year"] = date("Y");
    $fields["vatinc_yes"] = "checked";
    $fields["vatinc_no"] = "";
    $fields["vat14"] = AT14;
    $fields["vat"] = "0.00";
    $fields["total"] = "0.00";
    $fields["rounding"] = "";
    $fields["nhifrm_year"] = date("Y");
    $fields["nhifrm_month"] = date("m");
    $fields["nhifrm_day"] = date("d");
    $fields["nhito_year"] = date("Y");
    $fields["nhito_month"] = date("m");
    $fields["nhito_day"] = date("d");
    $fields["client_collect"] = "";
    $fields["collect"] = "";
    $fields["deliver"] = "";
    $fields["deposit_amt"] = "0.00";
    $fields["deposit_type"] = "CSH";
    $fields["custom_txt"] = "";
    $fields["monthly"] = false;
    $fields["bk_asset"] = 0;
    // 30 Asset
    $fields["bk_id"] = 0;
    $fields["reprint"] = 0;
    extract($fields, EXTR_SKIP);
    if (isset($bk_from)) {
        list($nhifrm_year, $nhifrm_month, $nhifrm_day) = explode("-", $bk_from);
    }
    if (isset($bk_to)) {
        list($nhito_year, $nhito_month, $nhito_day) = explode("-", $bk_to);
    }
    $subtot = 0;
    if (isset($hirenewBtn)) {
        newHire($_POST);
    }
    // Get us an invoice id
    if (!isset($invid)) {
        $invid = create_dummy($deptid);
    } else {
        $sql = "SELECT cusnum FROM hire.hire_invoices WHERE invid='{$invid}'";
        $cn_rslt = db_exec($sql) or errDie("Unable to retrieve invoices.");
        $cusnum = pg_fetch_result($cn_rslt, 0);
        updateTotals($invid);
    }
    $ind_ccol = "";
    $ind_col = "";
    $ind_del = "";
    $collect_ar = array();
    if (!empty($client_collect)) {
        $collect_ar[] = "Client Collect";
    }
    if (!empty($collect)) {
        $collect_ar[] = "Collect";
    }
    if (!empty($deliver)) {
        $collect_ar[] = "Deliver";
    }
    if (empty($client_collect) && empty($collect) && empty($deliver)) {
        $client_collect = "checked";
        $collect_ar[] = "Client Collect";
    }
    $collection = implode(", ", $collect_ar);
    if (empty($monthly)) {
        $sql = "SELECT *, extract('epoch' FROM expected) AS e_exp, extract('epoch' FROM to_date) AS e_to FROM hire.hire_invitems WHERE invid='{$invid}'";
        $item_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
        while ($item_data = pg_fetch_array($item_rslt)) {
            if (!empty($item_data["expected"])) {
                if ($item_data["e_to"] > time()) {
                    $item_data["expected"] = date("Y-m-t", $item_data["e_to"]);
                } else {
                    if ($item_data["e_exp"] < time()) {
                        $item_data["expected"] = date("Y-m-t");
                    }
                }
                $sql = "\r\n\t\t\t\t\tUPDATE hire.hire_invitems \r\n\t\t\t\t\tSET from_date='{$item_data['to_date']}', to_date='{$item_data['expected']}', expected=NULL \r\n\t\t\t\t\tWHERE id='{$item_data['id']}'";
                db_exec($sql) or errDie("Unable to update invoice.");
            }
        }
    }
    // Retrieve the actual invoice
    $sql = "SELECT * FROM hire.hire_invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'";
    $inv_rslt = db_exec($sql) or errDie("Unable to get invoice information");
    $inv_data = pg_fetch_array($inv_rslt);
    if ($cusnum == 0) {
        $cusnum = $inv_data["cusnum"];
    }
    if (empty($cordno)) {
        $cordno = $inv_data["cordno"];
    }
    $pinv_date = explode("-", $inv_data["odate"]);
    $pinv_year = $pinv_date[0];
    $pinv_month = $pinv_date[1];
    $pinv_day = $pinv_date[2];
    // Create the dropdowns ---------------------------------------------------
    // Retrieve departments
    $sql = "SELECT * FROM exten.departments ORDER BY deptname ASC";
    $dept_rslt = db_exec($sql) or errDie("Unable to retrieve departments.");
    // Create departments dropdown
    $dept_sel = "<select name='deptid' style='width: 100%'>";
    while ($dept_data = pg_fetch_array($dept_rslt)) {
        $dept_sel .= "<option value='{$dept_data['deptid']}'>{$dept_data['deptname']}</option>";
    }
    $dept_sel .= "</select>";
    // Check customer basis
    if ($cusnum > 0) {
        checkCustBasis($cusnum);
    }
    // Retrieve customers
    $sql = "SELECT * FROM cubit.customers ORDER BY surname ASC";
    $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customers.");
    // Create customers dropdown
    if (empty($cusnum)) {
        $cust_sel = "\r\n\t\t\t<select name='cusnum' style='width: 100%' onchange='javascript:document.form.submit()'>\r\n\t\t\t\t<option value='0'>[None]</option>";
        while ($cust_data = pg_fetch_array($cust_rslt)) {
            $sel = fsel(isset($cusnum) && $cusnum == $cust_data["cusnum"]);
            $cust_sel .= "<option value='{$cust_data['cusnum']}' {$sel}>{$cust_data['surname']}</option>";
        }
        $cust_sel .= "</select>";
    } else {
        $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$cusnum}'";
        $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customer.");
        $cust_data = pg_fetch_array($cust_rslt);
        $cust_sel = $cust_data["surname"];
    }
    // Retrieve sales people
    $sql = "SELECT * FROM exten.salespeople ORDER BY salesp ASC";
    $salesp_rslt = db_exec($sql) or errDie("Unable to retrieve sales people.");
    // Create sales people dropdown
    $salesp_sel = "<select name='salespid' style='width: 100%'>";
    while ($salesp_data = pg_fetch_array($salesp_rslt)) {
        $salesp_sel .= "<option value='{$salesp_data['salespid']}'>{$salesp_data['salesp']}</option>";
    }
    $salesp_sel .= "</select>";
    // Deposit Options
    $deposit_list = array("CSH" => "Cash", "CHQ" => "Cheque", "CRD" => "Credit Card");
    // Create the deposit dropdown
    $deposit_sel = "<select name='deposit_type'>";
    foreach ($deposit_list as $key => $value) {
        if ($inv_data["deposit_type"] == $key) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $deposit_sel .= "<option value='{$key}' {$sel}>{$value}</option>";
    }
    $deposit_sel .= "</select>";
    // Items Display -------------------------------------------------------
    $basis_list = array("per_day" => "Per Day", "per_hour" => "Per Hour", "per_week" => "Per Week");
    // Retrieve items
    $sql = "SELECT * FROM hire.hire_invitems WHERE invid='{$invid}' ORDER BY id ASC";
    $items_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
    $items_out = "";
    $temp_assets = array();
    while ($items_data = pg_fetch_array($items_rslt)) {
        $i = $items_data["id"];
        // Create the basis display
        $basis_disp = $basis_list[$items_data["basis"]];
        // Retrieve assets
        $sql = "SELECT * FROM cubit.assets WHERE id='{$items_data['asset_id']}'";
        $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
        $ad = pg_fetch_array($asset_rslt);
        // Retrieve asset group
        $sql = "SELECT * FROM cubit.assetgrp WHERE grpid='{$ad['grpid']}'";
        $agrp_rslt = db_exec($sql) or errDie("Unable to retrieve asset group.");
        $agrp_data = pg_fetch_array($agrp_rslt);
        if ($agrp_data["grpname"] == "Temporary Asset") {
            $temp_assets[] = $agrp_data[$ad["id"]];
        }
        // 		if ($ad["serial"] == "CUBIT::-QTY-") {
        // 			$asset_disp = "$ad[des]";
        // 		} else {
        // 			$asset_disp = "$ad[des] ($ad[serial])";
        // 		}
        $asset_disp = $ad["des"] . " " . getSerial($ad["id"], 1);
        $subtot += $items_data["amt"];
        //*$items_data["qty"];
        if ($items_data["basis"] == "per_hour") {
            $from_disp = "Hours: {$items_data['hours']}";
            $to_disp = "";
        } else {
            if ($items_data["basis"] == "per_day") {
                $mfrm_date = $items_data["from_date"];
                $mfrm_date = explode("-", $mfrm_date);
                $mfrm_year[$i] = $mfrm_date[0];
                $mfrm_month[$i] = $mfrm_date[1];
                $mfrm_day[$i] = $mfrm_date[2];
                $mto_date = $items_data["to_date"];
                $mto_date = explode("-", $mto_date);
                $mto_year[$i] = $mto_date[0];
                $mto_month[$i] = $mto_date[1];
                $mto_day[$i] = $mto_date[2];
                $from_disp = mkDateSelectA("mfrm", $i, $mfrm_year[$i], $mfrm_month[$i], $mfrm_day[$i]);
                $to_disp = mkDateSelectA("mto", $i, $mto_year[$i], $mto_month[$i], $mto_day[$i]);
                // 				$from_disp = "
                // 				<input type='hidden' name='mfrm_year[$i]' value='$mfrm_year[$i]' />
                // 				<input type='hidden' name='mfrm_month[$i]' value='$mfrm_month[$i]' />
                // 				<input type='hidden' name='mfrm_day[$i]' value='$mfrm_day[$i]' />
                // 				$mfrm_day[$i]-$mfrm_month[$i]-$mfrm_year[$i]";
                // 				$to_disp = "
                // 				<input type='hidden' name='mto_year[$i]' value='$mto_year[$i]' />
                // 				<input type='hidden' name='mto_month[$i]' value='$mto_month[$i]' />
                // 				<input type='hidden' name='mto_day[$i]' value='$mto_day[$i]' />
                // 				$mto_day[$i]-$mto_month[$i]-$mto_year[$i]";
                $from_date[$i] = "{$mfrm_year[$i]}-{$mfrm_month[$i]}-{$mfrm_day[$i]}";
                $to_date[$i] = "{$mto_year[$i]}-{$mto_month[$i]}-{$mto_day[$i]}";
                $hidden_date = "\r\n\t\t\t\t\t<input type='hidden' name='from_date[{$i}]' value='{$from_date[$i]}' />\r\n\t\t\t\t\t<input type='hidden' name='to_date[{$i}]' value='{$to_date[$i]}' />";
                // 				$from_disp = "$items_data[from_date]";
                // 				$to_disp = "$items_data[to_date]";
            } else {
                if ($items_data["basis"] == "per_week") {
                    $from_disp = "Weeks: {$items_data['weeks']}";
                    $to_disp = "";
                }
            }
        }
        if (!isset($return[$i])) {
            $return[$i] = "";
        }
        if (!isset($hidden_date)) {
            $hidden_date = "";
        }
        if (!isset($rain_days[$i])) {
            $rain_days[$i] = 0;
        }
        if ($items_data["basis"] == "per_day") {
            $rd_disp = "<input type='hidden' name='rain_days[{$i}]' \t\t\t\r\n\t\t\t\t\t\tvalue='{$rain_days[$i]}' size='3' style='text align: center' />";
            if ($items_data["half_day"]) {
                //				$hd_disp = "<input type='checkbox' name='half_day[$i]' value='1' checked /> Half Day</b>";
                $hd_disp = "<input type='hidden' name='half_day[{$i}]' value='1' />";
            } else {
                $hd_disp = "<input type='hidden' name='half_day[{$i}]' value='0' />";
                //				$hd_disp = "<input type='checkbox' name='half_day[$i]' value='1' /> Half Day";
            }
        } else {
            $hd_disp = "";
            $rd_disp = "<input type='hidden' name='rain_days[{$i}]' value='0' />";
        }
        if ($items_data["weekends"]) {
            $weekends[$i] = "checked";
        } else {
            $weekends[$i] = "";
        }
        // Items should not be removed once processed, use reprint to check
        // if this hire note has already been processed.
        if (isset($reprint) && $reprint || !empty($monthly)) {
            $rem_cbox = "";
        } else {
            $rem_cbox = "<td><input type='checkbox' name='remove[{$i}]'></td>";
        }
        if (isset($monthly) && $monthly) {
            if ($items_data["basis"] == "per_day") {
                $ret_cbox = "<td><input type='checkbox' name='return[{$i}]' value='checked' {$return[$i]}></td>";
            } else {
                $ret_cbox = "<td>&nbsp;</td>";
            }
        } else {
            $ret_cbox = "<td><input type='checkbox' name='return[{$i}]' value='checked' {$return[$i]}></td>";
        }
        $amt = sprint($items_data["amt"]);
        if (user_is_admin(USER_ID)) {
            $amount_out = "<input type='text' name='amount[{$i}]' value='{$amt}' size='7' />";
        } else {
            $amount_out = "<input type='hidden' name='amount[{$i}]' value='{$amt}' />{$amt}";
        }
        $items_out .= "\r\n\t\t\t<input type='hidden' name='asset_id[{$i}]' value='{$ad['id']}' />\r\n\t\t\t<input type='hidden' name='basis[{$i}]' value='{$items_data['basis']}' />\r\n\t\t\t<input type='hidden' name='qty[{$i}]' value='{$items_data['qty']}' />\r\n\t\t\t{$hidden_date}\r\n\t\t\t{$rd_disp}\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td>{$basis_disp}</td>\r\n\t\t\t\t<td>{$asset_disp}</td>\r\n\t\t\t\t<td align='center'>{$items_data['qty']}</td>\r\n\t\t\t\t<td align='center'>{$from_disp}</td>\r\n\t\t\t\t<td align='center'>{$to_disp} {$hd_disp}</td>\r\n\t<!--\t\t\t\r\n\t\t\t\t<td align='center'>{$rd_disp}</td>\r\n\t\t\t\t<td align='center'>{$items_data['collection']}</td>\r\n\t-->\r\n\t\t\t\t<td>{$amount_out}</td>\r\n\t\t\t\t{$rem_cbox}\r\n\t\t\t\t{$ret_cbox}\r\n\t\t\t</tr>";
    }
    $temp_assets = implode(",", $temp_assets);
    // New Items --------------------------------------------------------------
    // Avoid undefined variable items_out
    if (empty($items_out)) {
        $items_out = "";
    }
    // Buttons
    if (!empty($cusnum)) {
        if ($deposit_type == "CSH" && $deposit_amt != "0.00") {
            $deposit_open = "popupOpen(\"hire-invoice-print.php?key=cash_receipt&invid={$inv_data['invid']}\")";
        } else {
            $deposit_open = "";
        }
        $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$cusnum}'";
        $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customers.");
        $cust_data = pg_fetch_array($cust_rslt);
        $telno = $cust_data["bustel"];
        $return_btn = "<input type='submit' name='upBtn' value='Return' />";
        if (isset($reprint) && $reprint) {
            $new_btn = "<input type='button' value='Reprint' onclick='javascript:printer(\"hire/hire_note_reprint.php?invid={$inv_data['invid']}\");{$deposit_open}'>";
            $purch_btn = "";
            //$purch_btn = "<input type='button' value='Payment' onclick='javascript:popupOpen(\"".SELF."?key=newpos&cusnum=$cusnum\");' />";
        } else {
            if (!$monthly) {
                $new_btn = "<input name='hirenewBtn' type='submit' value='Process' />";
            } else {
                $new_btn = "<input type='submit' name='upBtn' value='Invoice' />";
            }
            $purch_btn = "";
        }
        $hire_buttons = "\r\n\t\t\t<tr>\r\n\t\t\t\t<td>&nbsp;</td>\r\n\t\t\t\t<td align='center'>\r\n\t\t\t\t\t<input type='submit' name='upBtn' value='Update'>\r\n\t\t\t\t\t{$new_btn}\r\n\t\t\t\t\t{$return_btn}\r\n\t\t\t\t\t{$purch_btn}\r\n\t\t\t\t\t<!--<input type='button' value='Swap Hire' />-->\r\n\t\t\t\t</td>\r\n\t\t\t\t<td>&nbsp;</td>\r\n\t\t\t</tr>";
        $basevis = "visible";
        $credit_limit = CUR . sprint($cust_data["credlimit"] - $cust_data["balance"]);
        $cust_balance = CUR . $cust_data["balance"];
    } else {
        $hire_buttons = "";
        $basevis = "hidden";
        $credit_limit = "";
        $cust_balance = "";
    }
    // Retrieve assets
    $sql = "SELECT *  FROM cubit.assets ORDER BY des ASC";
    $nasset_rslt = db_exec($sql) or errDie("Unable to retrieve asset.");
    // Assets dropdown
    $nasset_sel = "\r\n\t\t<select name='nasset_id' style='visibility: {$basevis}; width: 120px' onchange='assetChange(this);'>\r\n\t\t\t<option value='0'>- SELECT PLANT -</option>";
    while ($ad = pg_fetch_array($nasset_rslt)) {
        $sql = "SELECT * FROM hire.hire_invitems WHERE asset_id='{$ad['id']}' AND invid='{$invid}'";
        $invitem_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
        if (pg_num_rows($invitem_rslt)) {
            continue;
        }
        if (!empty($ad["remaction"])) {
            continue;
        }
        if (isHired($ad["id"], date("Y-m-d"))) {
            continue;
        }
        if (!isSerialized($ad["id"])) {
            $at = "q";
            //$asset_disp = "$ad[des] ($ad[serial2] available.)";
            $units_avail = unitsAvailable($ad["id"], date("Y-m-d"));
            $asset_disp = "{$ad['des']} {$units_avail} available.";
            if ($ad["serial2"] <= 0) {
                continue;
            }
        } else {
            $at = "s";
            $asset_disp = "{$ad['des']} ({$ad['serial']})";
        }
        if ($cust_bk = isBooked($ad["id"], date("Y-m-d"))) {
            $sql = "SELECT surname FROM cubit.customers WHERE cusnum='{$cust_bk}'";
            $surname_rslt = db_exec($sql) or errDie("Unable to retrieve customer.");
            $surname = pg_fetch_result($surname_rslt, 0);
            if (isSerialized($ad["id"])) {
                $asset_disp .= " Booked: {$surname}";
            } else {
                $units_booked = unitsBooked($ad["id"], date("Y-m-d"));
                $asset_disp .= " {$units_booked} Units Booked";
            }
        }
        if ($bk_asset == $ad["id"]) {
            $sel = "selected='selected'";
        } else {
            $sel = "";
        }
        $nasset_sel .= "<option value='{$at}:{$ad['id']}' {$sel}>{$asset_disp}</option>";
    }
    $nasset_sel .= "</select>";
    // Create basis dropdown
    $nbasis_sel = "\r\n\t\t<select name='nbasis' style='width: 100%; visibility: {$basevis};' onchange='basisChange(this);'>\r\n\t\t\t<option value='0'>- BASIS -</option>";
    foreach ($basis_list as $key => $value) {
        $nbasis_sel .= "<option value='{$key}'>{$value}</option>";
    }
    $nbasis_sel .= "</select>";
    // Create asset group dropdown
    $sql = "SELECT grpid, grpname FROM cubit.assetgrp ORDER BY grpname ASC";
    $grp_rslt = db_exec($sql) or errDie("Unable to retrieve groups.");
    if ($cusnum) {
        $OTS_OPT = onthespot_encode(SELF, "cust_selection", "deptid={$deptid}&cusnum={$cusnum}&invid={$invid}");
        // 		$cust_edit = "
        // 			<td nowrap>
        // 			<a href='javascript: popupSized(\"../cust-edit.php?cusnum=$cusnum&onthespot=$OTS_OPT\", \"edit_cust\", 700, 630);'>
        // 				Edit Customer Details
        // 			</a>
        // 			</td>";
        $cust_edit = "";
    } else {
        $cust_edit = "";
    }
    // Retrieve service date
    $sql = "SELECT * FROM hire.hire_invitems WHERE invid='{$invid}'";
    $invi_rslt = db_exec($sql) or errDie("Unable to retrieve item.");
    $sv_warn = "";
    while ($invi_data = pg_fetch_array($invi_rslt)) {
        $sql = "SELECT * FROM cubit.asset_svdates WHERE svdate<=CURRENT_DATE AND asset_id='{$invi_data['asset_id']}'";
        $sv_rslt = db_exec($sql) or errDie("Unable to retrieve service date.");
        $sv_data = pg_fetch_array($sv_rslt);
        // Retrieve asset
        if (pg_num_rows($sv_rslt)) {
            $sql = "SELECT * FROM cubit.assets WHERE id='{$sv_data['asset_id']}'";
            $asset_rslt = db_exec($sql) or errDie("Unable to retrieve asset.");
            $asset_data = pg_fetch_array($asset_rslt);
            $sv_warn .= "<li class='err'><b>SERVICING</b>: " . getSerial($asset_data["id"], 1) . "\r\n\t\t\t{$asset_data['des']} has a service date on {$sv_data['svdate']}.</li>";
        }
        if ($days = checkServicing($invi_data["asset_id"], 1)) {
            $sv_warn .= "<li class='err'><b>SERVICING</b>: {$asset_data['des']} needs servicing.</li>";
        }
    }
    // Check if we should use the default comments
    if (empty($inv_data["comm"])) {
        $sql = "SELECT value FROM cubit.settings WHERE constant='HIRE_COMMENTS'";
        $comment_rslt = db_exec($sql) or errDie("Unable to retrieve comments.");
        $inv_data["comm"] = pg_fetch_result($comment_rslt, 0);
    }
    // Site address
    $addr_sel = "";
    if ($cusnum) {
        // Retrieve branch address
        $sql = "SELECT branch_addr FROM hire.hire_invoices WHERE invid='{$invid}'";
        $addr_rslt = db_exec($sql) or errDie("Unable to retrieve branch address.");
        $branch_addr = pg_fetch_result($addr_rslt, 0);
        $sql = "SELECT id, branch_name FROM cubit.customer_branches WHERE cusnum='{$cusnum}'";
        $bran_rslt = db_exec($sql) or errDie("Unable to retrieve customer branch.");
        $addr_sel = "<select name='branch_addr' style='width: 100%' onchange='javascript:document.form.submit()'>";
        $addr_sel .= "<option value='0'>Physical Address</option>";
        while ($bran_data = pg_fetch_array($bran_rslt)) {
            if ($branch_addr == $bran_data["id"]) {
                $sel = "selected='selected'";
            } else {
                $sel = "";
            }
            $addr_sel .= "<option value='{$bran_data['id']}' {$sel}>{$bran_data['branch_name']}</option>";
        }
        $addr_sel .= "</select>";
        $addr_sel .= "<br />" . branchAddress($branch_addr, $cusnum);
    }
    $booked_items = getBookedItems($cusnum, date("Y-m-d"));
    foreach ($booked_items as $asset_id => $units_booked) {
        $sql = "SELECT des FROM cubit.assets WHERE id='{$asset_id}'";
        $bkdes_rslt = db_exec($sql) or errDie("Unable to retrieve bookings.");
        $bkdes = pg_fetch_result($bkdes_rslt, 0);
        $sv_warn .= "<li class='err'><b>BOOKING</b>: {$units_booked}x " . getSerial($asset_id, 1) . " {$bkdes} booked for this customer.</li>";
    }
    if ($monthly) {
        $ret_out = "Invoice";
    } else {
        $ret_out = "Return";
    }
    // Items should not be removed once processed, use reprint to check
    // if this hire note has already been processed or if its monthly.
    if (isset($reprint) && $reprint || !empty($monthly)) {
        $rem_th = "";
        $rem_nbsp = "";
    } else {
        $rem_th = "<th>Remove</th>";
        $rem_nbsp = "<td>&nbsp;</td>";
    }
    // Use the customer trad discount on default
    $sql = "SELECT traddisc FROM cubit.customers WHERE cusnum='{$cusnum}'";
    $cust_rslt = db_exec($sql) or errDie("Unable to retrieve discount.");
    $trade_discount = pg_fetch_result($cust_rslt, 0);
    // Determine if we got any items, if we do, we don't need to go for the
    // default value anymore, because the customer is already selected.
    $sql = "SELECT count(id) FROM hire.hire_invitems WHERE invid='{$invid}'";
    $count_rslt = db_exec($sql) or errDie("Unable to retrieve items.");
    $count = pg_fetch_result($count_rslt, 0);
    if ($count) {
        $trade_discount = $inv_data["traddisc"];
    }
    if (isset($bk_id) && $bk_id && !isset($bk_done)) {
        $sql = "\r\n\t\t\tSELECT serial FROM hire.bookings\r\n\t\t\t\tLEFT JOIN cubit.assets ON bookings.asset_id=assets.id\r\n\t\t\tWHERE bookings.id='{$bk_id}'";
        $bk_rslt = db_exec($sql) or errDie("Unable to retrieve booking.");
        $serialized = pg_fetch_result($bk_rslt, 0);
        if ($serialized == "Not Serialized") {
            $qty_disabled = "";
        } else {
            $qty_disabled = "disabled='t'";
        }
    } else {
        $qty_disabled = "disabled='t'";
    }
    // New Items
    $new_items_out = "\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>{$nbasis_sel}</td>\r\n\t\t\t<td>{$nasset_sel}</td>\r\n\t\t\t<td align='center'>\r\n\t\t\t\t<input type='text' id='nqty' name='nqty' value='' size='3' class='clear' {$qty_disabled} style='text-align:center' />\r\n\t\t\t</td>\r\n\t\t\t<td align='left' nowrap='t'>\r\n\t\t\t\t<div id='d_wks' style='height: 0px; visibility: hidden;'>\r\n\t\t\t\t\tWeeks: <input type='text' name='weeks' size='5' style='text-align: center;' />\r\n\t\t\t\t</div>\r\n\t\t\t\t<div id='d_hrs' style='height: 0px; visibility: hidden;'>\r\n\t\t\t\t\tHours: <input type='text' name='hours' size='5'\r\n\t\t\t\t\t\tstyle='text-align: center;' />\r\n\t\t\t\t</div>\r\n\t\t\t\t<div id='d_fdate' style='visibility: hidden;'>\r\n\t\t\t\t\t" . mkDateSelect("nhifrm", $nhifrm_year, $nhifrm_month, $nhifrm_day) . "\r\n\t\t\t\t</div>\r\n\t\t\t</td>\r\n\t\t\t<td align='left' nowrap='t'>\r\n\t\t\t\t<div id='d_tdate' style='visibility: hidden;'>\r\n\t\t\t\t\t" . mkDateSelect("nhito", $nhito_year, $nhito_month, $nhito_day) . "\r\n\t<!--\t\t\t\t\r\n\t\t\t\t\t<input type='checkbox' name='nhalf_day' value='checked' />\r\n\t\t\t\t\tHalf Day\r\n\t-->\t\t\t\r\n\t\t\t\t</div>\r\n\t\t\t</td>\r\n\t\t\t<td>&nbsp;</td>\r\n\t\t\t{$rem_nbsp}\r\n\t\t\t<td>&nbsp;</td>\r\n\t\t</tr>";
    /* -- Final Layout -- */
    $details = "\r\n\t\t<script>\r\n\t\t\tfunction basisChange(o) {\r\n\t\t\t\thrs = getObject('d_hrs');\r\n\t\t\t\tfd = getObject('d_fdate');\r\n\t\t\t\ttd = getObject('d_tdate');\r\n\t\t\t\twks = getObject('d_wks');\r\n\r\n\t\t\t\tswitch (o.value) {\r\n\t\t\t\t\tcase 'per_hour':\r\n\t\t\t\t\t\thrs.style.visibility = 'visible';\r\n\t\t\t\t\t\tfd.style.visibility = 'hidden';\r\n\t\t\t\t\t\ttd.style.visibility = 'hidden';\r\n\t\t\t\t\t\twks.style.visibility = 'hidden';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase 'per_day':\r\n\t\t\t\t\t\thrs.style.visibility = 'hidden';\r\n\t\t\t\t\t\tfd.style.visibility = 'visible';\r\n\t\t\t\t\t\ttd.style.visibility = 'visible';\r\n\t\t\t\t\t\twks.style.visibility = 'hidden';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase 'per_week':\r\n\t\t\t\t\t\thrs.style.visibility = 'hidden';\r\n\t\t\t\t\t\tfd.style.visibility = 'hidden';\r\n\t\t\t\t\t\ttd.style.visibility = 'hidden';\r\n\t\t\t\t\t\twks.style.visibility = 'visible';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tdefault:\r\n\t\t\t\t\t\thrs.style.visibility = 'hidden';\r\n\t\t\t\t\t\tfd.style.visibility = 'hidden';\r\n\t\t\t\t\t\ttd.style.visibility = 'hidden';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tfunction assetChange(o) {\r\n\t\t\t\tqo = getObject('nqty');\r\n\r\n\t\t\t\tswitch(o.value.substr(0, 1)) {\r\n\t\t\t\t\tcase 'q':\r\n\t\t\t\t\t\tqo.value = '';\r\n\t\t\t\t\t\tqo.disabled = false;\r\n\t\t\t\t\t\tqo.className = 'std';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase 's':\r\n\t\t\t\t\t\tqo.value = '1';\r\n\t\t\t\t\t\tqo.disabled = true;\r\n\t\t\t\t\t\tqo.className = 'clear';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tdefault:\r\n\t\t\t\t\t\tqo.value = '';\r\n\t\t\t\t\t\tqo.disabled = true;\r\n\t\t\t\t\t\tqo.className = 'clear'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t</script>\r\n\t\t<style>\r\n\t\t\ttd, input, textarea, select { font-size: .75em; }\r\n\t\t</style>\r\n\t\t<center>\r\n\t\t<form method='POST' name='formName'>\r\n\t\t\t<input type='hidden' name='key' value='update'>\r\n\t\t</form>\r\n\t\t<form action='" . SELF . "' method='POST' name='form'>\r\n\t\t\t<input type='hidden' name='collection' value='{$collection}' />\r\n\t\t\t<input type='hidden' name='key' value='update'>\r\n\t\t\t<input type='hidden' name='invid' value='{$invid}' />\r\n\t\t\t<input type='hidden' name='temp_assets' value='{$temp_assets}' />\r\n\t\t\t<input type='hidden' name='monthly' value='{$monthly}' />\r\n\t\t\t<input type='hidden' name='cusnum' value='{$cusnum}' />\r\n\t\t\t<input type='hidden' name='chrgvat' value='no' />\r\n\t\t\t<input type='hidden' name='bk_id' value='{$bk_id}' />\r\n\t\t\t<input type='hidden' name='bk_done' value='1' />\r\n\t\t\t<input type='hidden' name='reprint' value='{$reprint}' />\r\n\t\t<table " . TMPL_tblDflts . " width='100%'>\r\n\t\t \t<tr>\r\n\t\t \t\t<td colspan='3' align='center'><h3>New Hire</h3></td>\r\n\t\t \t</tr>\r\n\t\t \t<tr>\r\n\t\t \t\t<td valign='top'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th colspan='2'> Customer Details </th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Department</td>\r\n\t\t\t\t\t\t\t<td valign='center'>{$dept_sel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Customer</td>\r\n\t\t\t\t\t\t\t<td valign='center'>{$cust_sel}</td>\r\n\t\t\t\t\t\t\t{$cust_edit}\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Customer Telephone Number</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='20' name='telno' value='{$telno}'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Customer Order number</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='10' name='cordno' value='{$cordno}'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Available Credit</td>\r\n\t\t\t\t\t\t\t<td>{$credit_limit}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Balance</td>\r\n\t\t\t\t\t\t\t<td>{$cust_balance}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Customer Address</td>\r\n\t\t\t\t\t\t\t<td>{$addr_sel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t<!--\r\n\t\t\t\t\t\t<tr><th colspan='2'>Point of Hire</th></tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Barcode</td>\r\n\t\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t\t<input type='text' size='13' name='bar' value=''>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td nowrap='t'>Search for description</td>\r\n\t\t\t\t\t\t\t<td><input type='text' size='13' name='des' value='{$des}'></td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td colspan='2' align='center'>\r\n\t\t\t\t\t\t\t\t<input type='submit' value='Search'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Collection Method</td>\r\n\t\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t\t<input type='checkbox' name='client_collect' value='checked' {$client_collect} />\r\n\t\t\t\t\t\t\t\tClient Collect\r\n\t\t\t\t\t\t\t\t<br />\r\n\t\t\t\t\t\t\t\t<input type='checkbox' name='deliver' value='checked' {$deliver} />\r\n\t\t\t\t\t\t\t\tTo be Delivered\r\n\t\t\t\t\t\t\t\t<br />\r\n\t\t\t\t\t\t\t\t<input type='checkbox' name='collect' value='checked' {$collect} />\r\n\t\t\t\t\t\t\t\tTo be Collected\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t-->\r\n\t\t\t\t\t</table>\r\n\t\t\t\t<td valign='top' align='center' style='width: 100%;'>\r\n\t\t\t\t\t<img src='../compinfo/getimg.php' style='border: 1px solid #000' width='230' height='47' />\r\n\t\t\t\t</td>\r\n\t\t\t\t</td>\r\n\t\t\t\t<td valign='top' align='right'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th colspan=2>Hire Details</th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Hire No.</td>\r\n\t\t\t\t\t\t\t<td valign='center'>H{$inv_data['invnum']}" . rev($inv_data["invid"]) . "</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Sales Order No.</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='5' name='ordno' value='{$inv_data['ordno']}'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Hire Date</td>\r\n\t\t\t\t\t\t\t<td valign='center' nowrap='t'>\r\n\t\t\t\t\t\t\t\t" . mkDateSelect("pinv", $pinv_year, $pinv_month, $pinv_day) . "\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Sales Person</td>\r\n\t\t\t\t\t\t\t<td>{$salesp_sel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Trade Discount</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='5' name='traddisc'\r\n\t\t\t\t\t\t\t\tvalue='{$trade_discount}'>%\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td nowrap='t'>Delivery Charge</td>\r\n\t\t\t\t\t\t\t<td valign='center'>\r\n\t\t\t\t\t\t\t\t<input type='text' size='7' name='delchrg'\r\n\t\t\t\t\t\t\t\tvalue='{$inv_data['delchrg']}'>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th colspan='2'>Payment Details </th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>User</td>\r\n\t\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t\t<input type='hidden' name='user' value='" . USER_NAME . "'>\r\n\t\t\t\t\t\t\t\t" . USER_NAME . "\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Deposit Type</td>\r\n\t\t\t\t\t\t\t<td>{$deposit_sel}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Deposit Amount</td>\r\n\t\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t\t<input type='text' name='deposit_amt'\r\n\t\t\t\t\t\t\t\tvalue='" . sprint($inv_data["deposit_amt"]) . "' size='7' />\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td colspan='3'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='100%'>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'></tr>\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<th>Basis</th>\r\n\t\t\t\t\t\t\t<th>Item</th>\r\n\t\t\t\t\t\t\t<th>Qty</th>\r\n\t\t\t\t\t\t\t<th>Hire Date</th>\r\n\t\t\t\t\t\t\t<th>Expected Return</th>\r\n\t\t\t<!--\t\t\t\r\n\t\t\t\t\t\t\t<th>Rain Days</th>\r\n\t\t\t\t\t\t\t<th>Collection</th>\r\n\t\t\t-->\r\n\t\t\t\t\t\t\t<th>Amount</th>\r\n\t\t\t\t\t\t\t{$rem_th}\r\n\t\t\t\t\t\t\t<th>{$ret_out}</th>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t{$items_out}\r\n\t\t\t\t\t\t{$new_items_out}\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td width='70%' valign='top' colspan='2'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<td colspan='10'>{$sv_warn}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<td rowspan='4' nowrap>" . mkQuickLinks(ql("javascript:popupOpen(\"../customers-new.php\")", "New Customer"), ql("../pos-invoice-new.php", "New POS Invoice"), ql("../nons-invoice-new.php", "New Non-Stock Invoice")) . "\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t\t<th>Comments</th>\r\n\t\t\t\t\t\t\t<th>Custom Text</th>\r\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width=40%>{$error}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td rowspan=4 align=center valign=top>\r\n\t\t\t\t\t\t\t\t<textarea name=comm cols=20 style='height: 100%'>{$inv_data['comm']}</textarea>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t\t<td rowspan='4' align='center' valign='top'>\r\n\t\t\t\t\t\t\t\t<textarea name='custom_txt' rows='4' cols='60' style='height: 100%'>{$custom_txt}</textarea>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t\t<td colspan='2' align='right' valign='top' width='30%'>\r\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Delivery Charge</td>\r\n\t\t\t\t\t\t\t<td align=right>" . CUR . " {$inv_data['delivery']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>Trade Discount</td>\r\n\t\t\t\t\t\t\t<td align=right>" . CUR . " {$inv_data['discount']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\r\n\t\t\t\t\t\t\t<td align=right>\r\n\t\t\t\t\t\t\t\t" . CUR . "<input type=hidden name='subtot' value='{$inv_data['subtot']}'>\r\n\t\t\t\t\t\t\t\t" . sprint($inv_data["subtot"]) . "\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<td><b>VAT {$vat14}</b></td>\r\n\t\t\t\t\t\t\t<td align=right>" . CUR . " {$inv_data['vat']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\r\n\t\t\t\t\t\t\t<td align=right>" . CUR . " {$inv_data['total']}</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t{$rounding}\r\n\t\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t{$hire_buttons}\r\n\t\t</table>\r\n\t\t<a name='bottom'>\r\n\t\t</form>\r\n\t\t</center>";
    return $details;
}
コード例 #29
0
function details($_POST, $error = "")
{
    # get vars
    extract($_POST);
    # Redirect, vars?
    if (isset($cash) && $cash == 'yes') {
        header("Location: purchase-new-cash.php?deptid={$deptid}");
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    if (isset($purid)) {
        $v->isOk($purid, "num", 1, 20, "Invalid Order number.");
    } else {
        $v->isOk($deptid, "num", 1, 20, "Invalid department number.");
        $v->isOk($letters, "string", 0, 5, "Invalid First 3 Letters.");
    }
    # display errors, if any
    if ($v->isError()) {
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $error .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirm .= "{$error}<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirm;
    }
    if (!isset($purid)) {
        $purid = create_dummy($deptid);
    }
    $supprice = 0;
    # Get Order info
    db_connect();
    $sql = "SELECT * FROM purchases WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $purRslt = db_exec($sql) or errDie("Unable to get Order information");
    if (pg_numrows($purRslt) < 1) {
        return "<li class='err'>Purchase Not Found</li>";
    }
    $pur = pg_fetch_array($purRslt);
    $deptid = $pur['deptid'];
    # check if Order has been printed
    if ($pur['received'] == "y") {
        $error = "<li class='err'> Error : Order number <b>{$purid}</b> has already been received.</li>";
        $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    if (!isset($ordernum)) {
        $ordernum = '';
    }
    if (!isset($supinv)) {
        $supinv = '';
    }
    # get department
    db_conn("exten");
    $sql = "SELECT * FROM departments WHERE deptid = '{$pur['deptid']}' AND div = '" . USER_DIV . "'";
    $deptRslt = db_exec($sql);
    if (pg_numrows($deptRslt) < 1) {
        $dept['deptname'] = "<li class='err'>Department not Found.</li>";
    } else {
        $dept = pg_fetch_array($deptRslt);
    }
    # Get selected customer info
    db_connect();
    $sql = "SELECT * FROM suppliers WHERE supid = '{$pur['supid']}' AND div = '" . USER_DIV . "'";
    $supRslt = db_exec($sql) or errDie("Unable to view Supplier");
    if (pg_numrows($supRslt) < 1) {
        db_connect();
        if ($inv['deptid'] == 0) {
            $searchdept = "";
        } else {
            $searchdept = "deptid = '{$deptid}' AND ";
        }
        # Query server for supplier info
        $sql = "SELECT * FROM suppliers WHERE {$searchdept} location != 'int' AND lower(supname) LIKE lower('{$letters}%') AND div = '" . USER_DIV . "' ORDER BY supname ASC";
        $supRslt = db_exec($sql) or errDie("Unable to view suppliers");
        if (pg_numrows($supRslt) < 1) {
            $err = "<li class='err'>No Supplier names starting with <b>{$letters}</b> in database.</li>";
            return view_err($_POST, $err);
        } else {
            $suppliers = "<select name='supid' onChange='javascript:document.form.submit();'>";
            $suppliers .= "<option value='-S' selected>Select Supplier</option>";
            while ($sup = pg_fetch_array($supRslt)) {
                $suppliers .= "<option value='{$sup['supid']}'>{$sup['supname']}</option>";
            }
            $suppliers .= "</select>";
        }
        # take care of the uset vars
        $supaddr = "";
        $accno = "";
    } else {
        db_connect();
        # Query server for supplier info
        $sql = "SELECT * FROM suppliers WHERE deptid = '{$deptid}' AND location != 'int' AND lower(supname) LIKE lower('{$letters}%') AND div = '" . USER_DIV . "' ORDER BY supname ASC";
        $supRslt = db_exec($sql) or errDie("Unable to view suppliers");
        if (pg_numrows($supRslt) < 1) {
            $err = "<li class='err'>No Supplier names starting with <b>{$letters}</b> in database.</li>";
            return view_err($_POST, $err);
        } else {
            $suppliers = "<select name='supid' onChange='javascript:document.form.submit();'>";
            $supaddr = "";
            $accno = "";
            while ($sup = pg_fetch_array($supRslt)) {
                if ($sup['supid'] == $pur['supid']) {
                    $sel = "selected";
                    $supaddr = $sup['supaddr'];
                    $accno = $sup['supno'];
                    $supprice = $sup['listid'];
                } else {
                    $sel = "";
                }
                $suppliers .= "<option value='{$sup['supid']}' {$sel}>{$sup['supname']}</option>";
            }
            $suppliers .= "</select>";
        }
        $get_codes = "SELECT * FROM suppstock WHERE suppid = '{$pur['supid']}' ORDER BY stkid";
        $run_codes = db_exec($get_codes) or errDie("Unable to get supplier stock code information");
        if (pg_numrows($run_codes) > 0) {
            while ($codarr = pg_fetch_array($run_codes)) {
                if (strlen($codarr['stkcod']) > 0) {
                    $stockcodes[$codarr['stkid']]['stkcod'] = $codarr['stkcod'];
                }
                if (strlen($codarr['stkdes']) > 0) {
                    $stockcodes[$codarr['stkid']]['stkdes'] = $codarr['stkdes'];
                }
            }
        }
    }
    /* --- Start Drop Downs --- */
    # Select warehouse
    db_conn("exten");
    $whs = "<select name='whidss[]' onChange='javascript:document.form.submit();'>";
    $sql = "SELECT * FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        return "<li class='err'> No Stores found in Cubit.</li>";
    } else {
        $whs .= "<option value='-S' disabled selected>Select Store</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            $whs .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
        }
    }
    $whs .= "</select>";
    # days drop downs
    $days = array("0" => "0", "7" => "7", "14" => "14", "30" => "30", "60" => "60", "90" => "90", "120" => "120");
    $termssel = extlib_cpsel("terms", $days, $pur['terms']);
    # format date
    list($pur_year, $pur_month, $pur_day) = explode("-", $pur['pdate']);
    # keep the charge vat option stable
    if ($pur['vatinc'] == "yes") {
        $chy = "checked='yes'";
        $chn = "";
        $chv = "";
    } elseif ($pur['vatinc'] == "no") {
        $chy = "";
        $chn = "checked='yes'";
        $chv = "";
    } else {
        $chy = "";
        $chn = "";
        $chv = "checked='yes'";
    }
    /* --- End Drop Downs --- */
    /* --- Start Products Display --- */
    # select all products
    $products = "\n\t\t<table " . TMPL_tblDflts . " width='100%'>\n\t\t\t<tr>\n\t\t\t\t<th>STORE</th>\n\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t<th>VAT CODE</th>\n\t\t\t\t<th>DESCRIPTION</th>\n\t\t\t\t<th>QTY</th>\n\t\t\t\t<th>UNIT PER PACK</th>\n\t\t\t\t<th>PRICE PER PACK</th>\n\t\t\t\t<th>UNITS</th>\n\t\t\t\t<th>PRICE PER UNIT</th>\n\t\t\t\t<th>DISCOUNT</th>\n\t\t\t\t<th>DELIVERY DATE</th>\n\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t<th>VAT</th>\n\t\t\t\t<th>REM</th>\n\t\t\t</tr>";
    # get selected stock in this purchase
    db_connect();
    $sql = "SELECT * FROM pur_items  WHERE purid = '{$purid}' AND div = '" . USER_DIV . "'";
    $stkdRslt = db_exec($sql);
    $i = 0;
    $key = 0;
    while ($stkd = pg_fetch_array($stkdRslt)) {
        $stkd['account'] += 0;
        if ($stkd['account'] != 0) {
            if (!isset($stk['exvat'])) {
                $stk['exvat'] = "";
            }
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            db_conn('core');
            $Sl = "SELECT * FROM accounts WHERE accid='{$stkd['account']}'";
            $Rk = db_exec($Sl);
            $ad = pg_fetch_array($Rk);
            list($syear, $smonth, $sday) = explode("-", $stkd['ddate']);
            $stkd['amt'] = sprint($stkd['amt']);
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'";
            $Ri = db_exec($Sl);
            if (pg_num_rows($Ri) < 1) {
                //				return "Please select the vatcode for all your stock.";
                $_POST['done'] = "";
                return details($_POST, "<li class='err'>Please select the vatcode for all your items.</li>");
            }
            $vd = pg_fetch_array($Ri);
            if ($pur['vatinc'] == 'no' && $stk['exvat'] != 'yes' && $vd['zero'] != "Yes") {
                $vunitamt = sprint($stkd['unitcost'] + $stkd['svat'] / $stkd['qty']);
            } else {
                $vunitamt = sprint($stkd['unitcost']);
            }
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            $tip = "&nbsp;&nbsp;&nbsp;";
            if (isset($vatc[$key])) {
                $tip = "<font color='red'>#</font>";
                $error = "<div class='err'> {$tip}&nbsp;&nbsp;=&nbsp;&nbsp; VAT amount is different from amount calculated by cubit. To allow cubit to recalculate the vat amount, please delete the vat amount from the input box.";
            }
            if ($stkd['udiscount'] > 0) {
                $discps = round($stkd['udiscount'] / 100 * $stkd['unitcost'], 2);
            }
            $stkd['amt'] = sprint($stkd['qty'] * ($stkd['unitcost'] - $discps));
            # put in product
            $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whids[]' value='0'></td>\n\t\t\t\t\t<td>\n\t\t\t\t\t\t<input type='hidden' name='stkids[]' value='{$stkd['stkid']}'>\n\t\t\t\t\t\t{$ad['accname']}\n\t\t\t\t\t\t<input type='hidden' name='accounts[]' value='{$stkd['account']}'>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td><input type='text' name='descriptions[]' value='{$stkd['description']}'></td>\n\t\t\t\t\t<td><input type='text' size='4' name='qtys[]' value='{$stkd['qpack']}'></td>\n\t\t\t\t\t<td><input type='text' size='5' name='upack[]' value='{$stkd['upack']}'></td>\n\t\t\t\t\t<td><input type='text' size='8' name='ppack[]' value='" . sprint($stkd['ppack']) . "'></td>\n\t\t\t\t\t<td>" . sprint3($stkd['qty']) . "</td>\n\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$vunitamt}</td>\n\t\t\t\t\t<td><input type='text' size='5' name='udiscount[]' value='{$stkd['udiscount']}'></td>\n\t\t\t\t\t<td>" . mkDateSelecta("d", $key, $syear, $smonth, $sday) . "</td>\n\t\t\t\t\t<td align='right' nowrap><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td>{$tip} <input type='text' name='svat[]' size='7' value='{$stkd['svat']}'></td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'></td>\n\t\t\t\t</tr>";
            $key++;
        } else {
            # keep track of selected stock amounts
            $amts[$i] = $stkd['amt'];
            $i++;
            # get warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
            # get selected stock in this warehouse
            db_connect();
            $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'";
            $stkRslt = db_exec($sql);
            $stk = pg_fetch_array($stkRslt);
            list($syear, $smonth, $sday) = explode("-", $stkd['ddate']);
            $stkd['amt'] = sprint($stkd['amt']);
            db_conn('cubit');
            $Sl = "SELECT * FROM vatcodes ORDER BY code";
            $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
            $Vatcodes = "\n\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t<option value='0'>Select</option>";
            while ($vd = pg_fetch_array($Ri)) {
                if ($stkd['vatcode'] == $vd['id']) {
                    $sel = "selected";
                } else {
                    $sel = "";
                }
                $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
            }
            $Vatcodes .= "</select>";
            if ($pur['vatinc'] == 'no' && $stk['exvat'] != 'yes' && $vd['zero'] != "Yes") {
                $vunitamt = sprint($stkd['unitcost'] + $stkd['svat'] / $stkd['qty']);
            } else {
                $vunitamt = sprint($stkd['unitcost']);
            }
            $tip = "&nbsp;&nbsp;&nbsp;";
            if (isset($vatc[$key])) {
                $tip = "<font color='red'>#</font>";
                $error = "<div class='err'> {$tip}&nbsp;&nbsp;=&nbsp;&nbsp; VAT amount is different from amount calculated by cubit. To allow cubit to recalculate the vat amount, please delete the vat amount from the input box.";
            }
            $discps = 0;
            if ($stkd['udiscount'] > 0) {
                $discps = round($stkd['udiscount'] / 100 * $stkd['unitcost'], 2);
            }
            $stkd['amt'] = sprint($stkd['qty'] * ($stkd['unitcost'] - $discps));
            if (isset($stockcodes[$stk['stkid']]['stkcod'])) {
                $stk['stkcod'] = $stockcodes[$stk['stkid']]['stkcod'];
            }
            if (isset($stockcodes[$stk['stkid']]['stkdes'])) {
                $stk['stkdes'] = $stockcodes[$stk['stkid']]['stkdes'];
            }
            # put in product
            $products .= "\n\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$stkd['whid']}'>{$wh['whname']}</td>\n\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stkd['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t<td><input type='text' size='4' name='qtys[]' value='{$stkd['qpack']}'></td>\n\t\t\t\t\t<td><input type='text' size='5' name='upack[]' value='{$stkd['upack']}'></td>\n\t\t\t\t\t<td><input type='text' size='8' name='ppack[]' value='" . sprint($stkd['ppack']) . "'></td>\n\t\t\t\t\t<td>" . sprint3($stkd['qty']) . "</td>\n\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$vunitamt}</td>\n\t\t\t\t\t<td><input type='text' size='5' name='udiscount[]' value='{$stkd['udiscount']}'></td>\n\t\t\t\t\t<td>" . mkDateSelecta("d", $key, $syear, $smonth, $sday) . "</td>\n\t\t\t\t\t<td align='right' nowrap><input type='hidden' name='amt[]' value='{$stkd['amt']}'> " . CUR . " {$stkd['amt']}</td>\n\t\t\t\t\t<td>{$tip} <input type='text' name='svat[]' size='7' value='{$stkd['svat']}'></td>\n\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$key}'></td>\n\t\t\t\t</tr>";
            $key++;
        }
    }
    // select using selection
    if (!isset($sel_frm)) {
        $sel_frm = "stkcod";
    }
    # Look above(remprod keys)
    $keyy = $key;
    # look above(if i = 0 then there are no products)
    if ($i == 0) {
        $done = "";
    }
    # check if stock warehouse was selected
    if (isset($whidss)) {
        foreach ($whidss as $key => $whid) {
            if (isset($stkidss[$key]) && $stkidss[$key] != "-S") {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                # Get selected warehouse name
                db_conn("exten");
                $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                $whRslt = db_exec($sql);
                $wh = pg_fetch_array($whRslt);
                $Sl = "SELECT * FROM splist_prices WHERE listid='{$supprice}' AND div='" . USER_DIV . "' AND stkid='{$stkidss[$key]}'";
                $Ry = db_exec($Sl) or errDie("Unable to get price.");
                $listdata = pg_fetch_array($Ry);
                $newprice = $listdata['price'];
                # Get selected stock in this warehouse
                db_connect();
                $sql = "SELECT * FROM stock WHERE stkid = '{$stkidss[$key]}' AND div = '" . USER_DIV . "' ORDER BY stkcod ASC";
                $stkRslt = db_exec($sql);
                $stk = pg_fetch_array($stkRslt);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$key] * 0);
                $newprice += 0;
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($stk['vatcode'] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                $discps = 0;
                if ($udiscount[$key] > 0) {
                    $discps = round($udiscount[$key] / 100 * $newprice, 2);
                }
                $amt[$key] = sprint($qtyss[$key] * ($newprice - $discps));
                if (isset($stockcodes[$stk['stkid']]['stkcod'])) {
                    $stk['stkcod'] = $stockcodes[$stk['stkid']]['stkcod'];
                }
                if (isset($stockcodes[$stk['stkid']]['stkdes'])) {
                    $stk['stkdes'] = $stockcodes[$stk['stkid']]['stkdes'];
                }
                # Put in selected warehouse and stock
                $products .= "\n\t\t\t\t\t<input type='hidden' name='accounts[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='descriptions[]' value=''>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><input type='hidden' name='whids[]' value='{$whid}'>{$wh['whname']}</td>\n\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stk['stkid']}'><a href='#' onclick='openwindow(\"stock-amt-det.php?stkid={$stk['stkid']}\")'>{$stk['stkcod']}</a></td>\n\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t\t<td><input type='text' size='4' name='qtys[]' value='{$qtyss[$key]}'></td>\n\t\t\t\t\t\t<td><input type='hidden' name='novat[{$keyy}]' value='1'><input type='text' size='5' name='upack[]' value='{$stk['rate']}'></td>\n\t\t\t\t\t\t<td><input type='text' size='8' name='ppack[]' value='" . sprint($newprice) . "'></td>\n\t\t\t\t\t\t<td>{$stk['rate']}</td>\n\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " 0.00</td>\n\t\t\t\t\t\t<td><input type='text' size='5' name='udiscount[]' value='" . sprint($udiscounts[$key]) . "'></td>\n\t\t\t\t\t\t<td>" . mkDateSelecta("d", $keyy, $d_year[$keyy], $d_month[$keyy], $d_day[$keyy]) . "</td>\n\t\t\t\t\t\t<td align='right' nowrap><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
            } elseif (isset($accountss[$key]) && $accountss[$key] != "0") {
                # skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                db_conn('core');
                $Sl = "SELECT * FROM accounts WHERE accid='{$accountss[$key]}'";
                $Ri = db_exec($Sl) or errDie("Unable to get account data.");
                if (pg_num_rows($Ri) < 1) {
                    return "invalid.";
                }
                $ad = pg_fetch_array($Ri);
                # Calculate amount
                $amt[$key] = sprint($qtyss[$keyy] * 0);
                //$newprice+=0;
                db_conn('cubit');
                $Sl = "SELECT * FROM vatcodes ORDER BY code";
                $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                $Vatcodes = "\n\t\t\t\t\t<select name='vatcodes[]'>\n\t\t\t\t\t\t<option value='0'>Select</option>";
                while ($vd = pg_fetch_array($Ri)) {
                    if ($vatcodess[$keyy] == $vd['id']) {
                        $sel = "selected";
                    } else {
                        $sel = "";
                    }
                    $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                }
                $Vatcodes .= "</select>";
                #we want to open the cost center popup here and now if this is and expense ....
                /*				$sql = "SELECT * FROM bankacct WHERE btype != 'int' AND div = '".USER_DIV."' LIMIT 1";
                				$banks = db_exec($sql);
                				if(pg_numrows($banks) < 1){
                					return "<li class=err> There are no accounts held at the selected Bank.
                					<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct Selection'>";
                				}
                				$barr = pg_fetch_array($banks);
                				$bankid = $barr['bankid'];
                
                				core_connect();
                				$sql = "SELECT * FROM bankacc WHERE accid = '$bankid' AND div = '".USER_DIV."'";
                				$rslt = db_exec($sql) or errDie("Unable to retrieve bank account link from Cubit",SELF);
                				# Check if link exists
                				if(pg_numrows($rslt) <1){
                					return "<li class=err> ERROR : The bank account that you selected doesn't appear to have an account linked to it.";
                				}
                				$banklnk = pg_fetch_array($rslt);*/
                /*				if(cc_TranTypeAcc($accountss[$key], $banklnk['accnum']) != false){
                					$cc_trantype = cc_TranTypeAcc($accountss[$key], $banklnk['accnum']);
                					$date = date("d-m-Y");
                					$amount = $ppack[$key] * $qtyss[$key];
                					$cc = "<script> CostCenter('$cc_trantype', 'Bank Transaction', '$date', '$descriptionss[$key]', '$amount', ''); </script>";
                				}else{
                					$cc = "";
                				}*/
                # THIS PRODUCT DISPLAYS DIRECTLY AFTER A NON STOCK ACCOUNT HAS BEEN SUBMITTED
                # Put in selected warehouse and stock
                $discps = 0;
                if ($udiscount[$key] > 0) {
                    $discps = round($udiscount[$key] / 100 * $newprice, 2);
                }
                $amt[$key] = sprint($qtyss[$key] * ($newprice - $discps));
                $products .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td colspan='2'>\n\t\t\t\t\t\t\t<input type='hidden' name='accounts[]' value='{$accountss[$key]}'>\n\t\t\t\t\t\t\t<input type='hidden' name='whids[]' value='0'>{$ad['accname']}\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t<td><input type='hidden' name='stkids[]' value='0'>{$Vatcodes}</td>\n\t\t\t\t\t\t<td><input type='text' size='20' name='descriptions[]' value='{$descriptionss[$keyy]}'></td>\n\t\t\t\t\t\t<td><input type='text' size='4' name='qtys[]' value='{$qtyss[$keyy]}'></td>\n\t\t\t\t\t\t<td><input type='hidden' name='novat[{$keyy}]' value='1'><input type='text' size='5' name='upack[]' value='{$upack[$key]}'></td>\n\t\t\t\t\t\t<td><input type='text' size='8' name='ppack[{$keyy}]' value='" . sprint($ppack[$keyy]) . "'></td>\n\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " 0.00</td>\n\t\t\t\t\t\t<td><input type='text' size='5' name='udiscount[{$keyy}]' value='" . sprint($udiscount[$keyy]) . "'></td>\n\t\t\t\t\t\t<td>" . mkDateSelecta("d", $keyy, $d_year[$key], $d_month[$key], $d_day[$key]) . "</td>\n\t\t\t\t\t\t<td align='right' nowrap><input type='hidden' name='amt[]' value='{$amt[$key]}'> " . CUR . " {$amt[$key]}</td>\n\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t<td><input type='checkbox' name='remprod[]' value='{$keyy}'></td>\n\t\t\t\t\t</tr>";
            } else {
                # Skip if not selected
                if ($whid == "-S") {
                    continue;
                }
                if (!isset($addnon)) {
                    if (isset($filter_store) and $filter_store != "0") {
                        # Get warehouse name
                        db_conn("exten");
                        $sql = "SELECT whname FROM warehouses WHERE whid = '{$whid}' AND div = '" . USER_DIV . "'";
                        $whRslt = db_exec($sql);
                        $wh = pg_fetch_array($whRslt);
                    }
                    if (isset($des) and $des != "") {
                        $len = strlen($des);
                        if ($des == "Show All") {
                            $Wh = "";
                            $des = "";
                        } else {
                            $Wh = "AND (lower(stkdes) LIKE lower('%{$des}%')) OR (lower(stkcod) LIKE lower('%{$des}%'))";
                        }
                    } else {
                        $Wh = "AND FALSE";
                        $des = "";
                    }
                    $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
                    if (isset($check_setting) and $check_setting == "yes") {
                        if (isset($filter_class) and $filter_class != "0") {
                            $Wh .= " AND prdcls = '{$filter_class}'";
                        }
                        if (isset($filter_cat) and $filter_cat != "0") {
                            $Wh .= " AND catid = '{$filter_cat}'";
                        }
                    }
                    if (isset($filter_store) and $filter_store != "0") {
                        $Wh .= " AND whid = '{$filter_store}'";
                    }
                    # Get stock on this warehouse
                    db_connect();
                    $sql = "SELECT * FROM stock WHERE div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC LIMIT 200";
                    $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
                    if (pg_numrows($stkRslt) < 1) {
                        //$error .= "<li class='err'>There are no stock items in the selected warehouse.";
                        continue;
                    }
                    if ($sel_frm == "stkcod") {
                        $stks = "<select name='stkidss[]' style='width:200px' onChange='javascript:document.form.submit();'>";
                        $stks .= "<option value='-S' disabled selected>Select Number</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            if (isset($stockcodes[$stk['stkid']]['stkcod'])) {
                                $stk['stkcod'] = $stockcodes[$stk['stkid']]['stkcod'];
                            }
                            if (isset($stockcodes[$stkd['stkid']]['stkdes'])) {
                                $stk['stkdes'] = $stockcodes[$stk['stkid']]['stkdes'];
                            }
                            $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $stks .= "</select> ";
                    } else {
                        $stks = "<select name='stkidss[]' style='width:200px' onChange='javascript:document.form.submit();'>";
                        $stks .= "<option value='-S' disabled selected>Select Description</option>";
                        $count = 0;
                        while ($stk = pg_fetch_array($stkRslt)) {
                            if (isset($stockcodes[$stk['stkid']]['stkcod'])) {
                                $stk['stkcod'] = $stockcodes[$stk['stkid']]['stkcod'];
                            }
                            if (isset($stockcodes[$stkd['stkid']]['stkdes'])) {
                                $stk['stkdes'] = $stockcodes[$stk['stkid']]['stkdes'];
                            }
                            $stks .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
                        }
                        $stks .= "</select> ";
                    }
                    # Put in drop down and warehouse
                    $products .= "\n\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t\t<input type='hidden' name='udiscounts[]' value='0.00'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$filter_store}'></td>\n\t\t\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td><input type='text' size='4' name='qtyss[]'  value='1'></td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t<td>" . mkDateSelecta("d", $keyy, $d_year[$keyy], $d_month[$keyy], $d_day[$keyy]) . "</td>\n\t\t\t\t\t\t\t<td align='right' nowrap><input type='hidden' name='amts[]' value='0.00'>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>";
                } else {
                    $Accounts = "\n\t\t\t\t\t\t<select name='accountss[]'>\n\t\t\t\t\t\t\t<option value='0'>Select Account</option>";
                    $useaccdrop = getCSetting("USE_NON_PURCHASES_ACCOUNTS");
                    if (isset($useaccdrop) and $useaccdrop == "yes") {
                        db_connect();
                        $acc_sql = "SELECT * FROM non_purchases_account_list ORDER BY accname";
                        $run_acc = db_exec($acc_sql) or errDie("Unable to get account information.");
                        if (pg_numrows($run_acc) > 0) {
                            while ($acc = pg_fetch_array($run_acc)) {
                                $Accounts .= "<option value='{$acc['accid']}'>{$acc['accname']}</option>";
                            }
                            $Accounts .= "</select>";
                        }
                    } else {
                        db_conn('core');
                        $Sl = "SELECT accid,topacc,accnum,accname FROM accounts ORDER BY accname";
                        $Ri = db_exec($Sl) or errDie("Unable to get accounts.");
                        // WHERE acctype='I'
                        while ($ad = pg_fetch_array($Ri)) {
                            if (isDisabled($ad['accid'])) {
                                continue;
                            }
                            $Accounts .= "<option value='{$ad['accid']}'>{$ad['accname']}</option>";
                        }
                        $Accounts .= "</select>";
                    }
                    db_conn('cubit');
                    $Sl = "SELECT * FROM vatcodes ORDER BY code";
                    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
                    $Vatcodes = "\n\t\t\t\t\t\t<select name='vatcodess[{$keyy}]'>\n\t\t\t\t\t\t\t<option value='0'>Select</option>";
                    while ($vd = pg_fetch_array($Ri)) {
                        if ($vd['del'] == "Yes") {
                            $sel = "selected";
                        } else {
                            $sel = "";
                        }
                        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
                    }
                    $Vatcodes .= "</select>";
                    # Put in drop down and warehouse
                    $products .= "\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td colspan='2'><input type='hidden' name='whidss[]' value='0'>{$Accounts}</td>\n\t\t\t\t\t\t\t<td>{$Vatcodes}</td>\n\t\t\t\t\t\t\t<td><input type='text' size='20' name='descriptionss[{$keyy}]' value=''></td>\n\t\t\t\t\t\t\t<td><input type='text' size='4' name='qtyss[{$keyy}]' value='1'></td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='upack[{$keyy}]' value='1'></td>\n\t\t\t\t\t\t\t<td><input type='text' size='8' name='ppack[{$keyy}]' value=''></td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td><input type='hidden' name='udiscount[{$keyy}]' value='0.00'></td>\n\t\t\t\t\t\t\t<td>" . mkDateSelecta("d", $keyy, $d_year[$key], $d_month[$key], $d_day[$key]) . "</td>\n\t\t\t\t\t\t\t<td align='right' nowrap><input type='hidden' name='amts[{$keyy}]' value='0.00'>" . CUR . " 0.00</td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t\t<td> </td>\n\t\t\t\t\t\t</tr>";
                }
            }
            $keyy++;
        }
    } else {
        $ckey = $keyy;
        # take todays date
        list($year, $month, $day) = explode("-", $pur['pdate']);
        if (isset($filter_store) and $filter_store != "0") {
            # get selected warehouse name
            db_conn("exten");
            $sql = "SELECT whname FROM warehouses WHERE whid = '{$filter_store}' AND div = '" . USER_DIV . "'";
            $whRslt = db_exec($sql);
            $wh = pg_fetch_array($whRslt);
        }
        if (isset($des) and $des != "") {
            $len = strlen($des);
            if ($des == "Show All") {
                $Wh = "";
                $des = "";
            } else {
                $Wh = "AND (lower(stkdes) LIKE lower('%{$des}%')) OR (lower(stkcod) LIKE lower('%{$des}%'))";
            }
        } else {
            $Wh = "AND FALSE";
            $des = "";
        }
        $check_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
        if (isset($check_setting) and $check_setting == "yes") {
            if (isset($filter_class) and $filter_class != "0") {
                $Wh .= " AND prdcls = '{$filter_class}'";
            }
            if (isset($filter_cat) and $filter_cat != "0") {
                $Wh .= " AND catid = '{$filter_cat}'";
            }
        }
        if (isset($filter_store) and $filter_store != "0") {
            $Wh .= " AND whid = '{$filter_store}'";
        }
        # get stock on this warehouse
        db_connect();
        $sql = "SELECT * FROM stock WHERE div = '" . USER_DIV . "' {$Wh} ORDER BY stkcod ASC LIMIT 200";
        $stkRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
        if (pg_numrows($stkRslt) < 1) {
            if (!isset($err)) {
                $err = "";
            }
            $err .= "<li>There are no stock items in the selected warehouse.</li>";
        }
        if ($sel_frm == "stkcod") {
            $stks = "<select name='stkidss[]' style='width:200px' onChange='javascript:document.form.submit();'>";
            $stks .= "<option value='-S' disabled selected>Select Number</option>";
            $count = 0;
            while ($stk = pg_fetch_array($stkRslt)) {
                if (isset($stockcodes[$stk['stkid']]['stkcod'])) {
                    $stk['stkcod'] = $stockcodes[$stk['stkid']]['stkcod'];
                }
                if (isset($stockcodes[$stkd['stkid']]['stkdes'])) {
                    $stk['stkdes'] = $stockcodes[$stk['stkid']]['stkdes'];
                }
                $stks .= "<option value='{$stk['stkid']}'>{$stk['stkcod']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
            }
            $stks .= "</select> ";
        } else {
            $stks = "<select name='stkidss[]' style='width:200px' onChange='javascript:document.form.submit();'>";
            $stks .= "<option value='-S' disabled selected>Select Description</option>";
            $count = 0;
            while ($stk = pg_fetch_array($stkRslt)) {
                if (isset($stockcodes[$stk['stkid']]['stkcod'])) {
                    $stk['stkcod'] = $stockcodes[$stk['stkid']]['stkcod'];
                }
                if (isset($stockcodes[$stkd['stkid']]['stkdes'])) {
                    $stk['stkdes'] = $stockcodes[$stk['stkid']]['stkdes'];
                }
                $stks .= "<option value='{$stk['stkid']}'>{$stk['stkdes']} (" . sprint3($stk['units'] - $stk['alloc']) . ")</option>";
            }
            $stks .= "</select> ";
        }
        $products .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<input type='hidden' name='accountss[]' value='0'>\n\t\t\t\t\t<input type='hidden' name='descriptionss[]' value=''>\n\t\t\t\t\t<input type='hidden' name='vatcodess[]' value=''>\n\t\t\t\t\t<input type='hidden' name='udiscounts[]' value='0.00'>\n\t\t\t\t\t<td><input type='hidden' name='whidss[]' value='{$filter_store}'></td>\n\t\t\t\t\t<td>{$stks}</td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td><input type='text' size='4' name='qtyss[]' value='1'></td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td>" . mkDateSelecta("d", $ckey, $year, $month, $day) . "</td>\n\t\t\t\t\t<td nowrap>" . CUR . " 0.00</td>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td></td>\n\t\t\t\t</tr>";
        $ckey++;
    }
    $products .= "</table>";
    /* --- End Products Display --- */
    /* --- Start Some calculations --- */
    # Get subtotal
    $SUBTOT = sprint($pur['subtot']);
    # Get Total
    $TOTAL = sprint($pur['total']);
    # Get vat
    $VAT = sprint($pur['vat']);
    # Shipping Charges
    $pur['shipchrg'] = sprint($pur['shipchrg']);
    $pur['delvat'] += 0;
    if ($pur['delvat'] == 0) {
        $Sl = "SELECT * FROM vatcodes WHERE del='Yes'";
        $Ri = db_exec($Sl) or errDie("Unable to get data.");
        $vd = pg_fetch_array($Ri);
        $pur['delvat'] = $vd['id'];
    }
    db_conn('cubit');
    $Sl = "SELECT * FROM vatcodes ORDER BY code";
    $Ri = db_exec($Sl) or errDie("Unable to get vat codes");
    $Vatcodes = "\n\t\t<select name='delvat'>\n\t\t\t<option value='0'>Select</option>";
    while ($vd = pg_fetch_array($Ri)) {
        if ($vd['id'] == $pur['delvat']) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $Vatcodes .= "<option value='{$vd['id']}' {$sel}>{$vd['code']}</option>";
    }
    $Vatcodes .= "</select>";
    /* --- End Some calculations --- */
    $ex = "";
    if (strlen($supinv)) {
        db_conn('cubit');
        $Sl = "SELECT purnum,pdate FROM purchases WHERE supid='{$supid}' AND supinv='{$supinv}' AND purid!='{$purid}'";
        $Ri = db_exec($Sl);
        if (pg_num_rows($Ri) > 0) {
            $pd = pg_fetch_array($Ri);
            $ex .= "<li class='err'>Purchase {$pd['purnum']} on {$pd['pdate']} has the same supplier invoice number.</li>";
        }
        for ($i = 1; $i < 13; $i++) {
            db_conn($i);
            $Sl = "SELECT purnum,pdate FROM purchases WHERE supid='{$supid}' AND supinv='{$supinv}'";
            $Ri = db_exec($Sl);
            if (pg_num_rows($Ri) > 0) {
                $pd = pg_fetch_array($Ri);
                $ex .= "<li class='err'>Purchase {$pd['purnum']} on {$pd['pdate']} has same the supplier invoice number.</li>";
            }
        }
        db_conn('cubit');
        $Sl = "SELECT purnum,pdate FROM nons_purchases WHERE supplier='{$supid}' AND supinv='{$supinv}'";
        $Ri = db_exec($Sl);
        if (pg_num_rows($Ri) > 0) {
            $pd = pg_fetch_array($Ri);
            $ex .= "<li class='err'>Non Stock Purchase {$pd['purnum']} on {$pd['pdate']} has the same supplier invoice number.</li>";
        }
        for ($i = 1; $i < 13; $i++) {
            db_conn($i);
            $Sl = "SELECT purnum,pdate FROM nons_purchases WHERE supplier='{$supid}' AND supinv='{$supinv}'";
            $Ri = db_exec($Sl);
            if (pg_num_rows($Ri) > 0) {
                $pd = pg_fetch_array($Ri);
                $ex .= "<li class='err'>Non Stock Purchase {$pd['purnum']} on {$pd['pdate']} has same the supplier invoice number.</li>";
            }
        }
    }
    if (!isset($showvat)) {
        $showvat = TRUE;
    }
    if ($showvat == TRUE) {
        $vat14 = AT14;
    } else {
        $vat14 = "";
    }
    // Which display method was selected
    if (isset($sel_frm) && $sel_frm == "stkdes") {
        $sel_frm_cod = "";
        $sel_frm_des = "checked";
    } else {
        $sel_frm_cod = "checked";
        $sel_frm_des = "";
    }
    if (isset($addnon) or isset($upBtn) or isset($doneBtn) or isset($invoice) or isset($donePrnt)) {
        $jump_bot = "\n\t\t\t<script>\n\t\t\t\twindow.location.hash='bottom';\n\t\t\t</script>";
    } else {
        $jump_bot = "";
    }
    $optional_filter_setting = getCSetting("OPTIONAL_STOCK_FILTERS");
    if (isset($optional_filter_setting) and $optional_filter_setting == "yes") {
        db_connect();
        $catsql = "SELECT catid, cat, catcod FROM stockcat WHERE div = '" . USER_DIV . "' ORDER BY cat ASC";
        $catRslt = db_exec($catsql);
        if (pg_numrows($catRslt) < 1) {
            $cat_drop = "<input type='hidden' name='filter_cat' value='0'>";
        } else {
            $cat_drop = "<select name='filter_cat'>";
            $cat_drop .= "<option value='0'>All Categories</option>";
            while ($cat = pg_fetch_array($catRslt)) {
                if (isset($filter_cat) and $filter_cat == $cat['catid']) {
                    $cat_drop .= "<option value='{$cat['catid']}' selected>({$cat['catcod']}) {$cat['cat']}</option>";
                } else {
                    $cat_drop .= "<option value='{$cat['catid']}'>({$cat['catcod']}) {$cat['cat']}</option>";
                }
            }
            $cat_drop .= "</select>";
        }
        # Select classification
        $classsql = "SELECT * FROM stockclass WHERE div = '" . USER_DIV . "' ORDER BY classname ASC";
        $clasRslt = db_exec($classsql);
        if (pg_numrows($clasRslt) < 1) {
            $class_drop = "<input type='hidden' name='filter_class' value='0'>";
        } else {
            $class_drop = "<select name='filter_class' style='width: 167'>";
            $class_drop .= "<option value='0'>All Classifications</option>";
            while ($clas = pg_fetch_array($clasRslt)) {
                if (isset($filter_class) and $filter_class == $clas['clasid']) {
                    $class_drop .= "<option value='{$clas['clasid']}' selected>{$clas['classname']}</option>";
                } else {
                    $class_drop .= "<option value='{$clas['clasid']}'>{$clas['classname']}</option>";
                }
            }
            $class_drop .= "</select>";
        }
        $display_optional_filters = "\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Select Category</td>\n\t\t\t\t<td>{$cat_drop}</td>\n\t\t\t</tr>\n\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t<td>Select Classification</td>\n\t\t\t\t<td>{$class_drop}</td>\n\t\t\t</tr>";
    }
    db_conn("exten");
    $sql = "SELECT whid, whname, whno FROM warehouses WHERE div = '" . USER_DIV . "' ORDER BY whname ASC";
    $whRslt = db_exec($sql);
    if (pg_numrows($whRslt) < 1) {
        $store_drop = "<input type='hidden' name='filter_store' value='0'>";
    } else {
        if (!isset($filter_store)) {
            # check if setting exists
            db_connect();
            $sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
            $Rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
            if (pg_numrows($Rslt) > 0) {
                $set = pg_fetch_array($Rslt);
                $whid = $set['value'];
            }
        }
        $store_drop = "<select name='filter_store'>";
        $store_drop .= "<option value='0'>All Stores</option>";
        while ($wh = pg_fetch_array($whRslt)) {
            if (isset($filter_store) and $filter_store == $wh['whid']) {
                $store_drop .= "<option value='{$wh['whid']}' selected>({$wh['whno']}) {$wh['whname']}</option>";
            } else {
                $store_drop .= "<option value='{$wh['whid']}'>({$wh['whno']}) {$wh['whname']}</option>";
            }
        }
        $store_drop .= "</select>";
    }
    $details = "\n\t\t<center>\n\t\t<h3>New Order</h3>\n\t\t<form action='" . SELF . "' method='POST' name='form'>\n\t\t\t<input type='hidden' name='SCROLL' value='1' />\n\t\t\t<input type='hidden' name='key' value='update' />\n\t\t\t<input type='hidden' name='purid' value='{$purid}' />\n\t\t\t<input type='hidden' name='deptid' value='{$deptid}' />\n\t\t\t<input type='hidden' name='letters' value='{$letters}' />\n\t\t\t<input type='hidden' name='total' value='{$TOTAL}' />\n\t\t\t<input type='hidden' name='subtot' value='{$SUBTOT}'>\n\t\t<table " . TMPL_tblDflts . " width='97%'>\n\t\t\t<tr>\n\t\t\t\t<td valign='top'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'> Supplier Details </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Department</td>\n\t\t\t\t\t\t\t<td valign='center'>{$dept['deptname']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier</td>\n\t\t\t\t\t\t\t<td valign='center'>{$suppliers}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Account number</td>\n\t\t\t\t\t\t\t<td valign='center'>{$accno}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td valign='top'>Supplier Address</td>\n\t\t\t\t\t\t\t<td valign='center'>" . nl2br($supaddr) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Using</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\tStock Code\n\t\t\t\t\t\t\t\t<input type='radio' name='sel_frm' value='stkcod' onChange='javascript:document.form.submit();' {$sel_frm_cod}> Stock Description<input type='radio' name='sel_frm' value='stkdes' onChange='javascript:document.form.submit();' {$sel_frm_des} />\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'>Additional Filters</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Select Store</td>\n\t\t\t\t\t\t\t<td>{$store_drop}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t{$display_optional_filters}\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Stock Filter</td>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<input type='text' size='13' name='des' value='{$des}'> \n\t\t\t\t\t\t\t\t<input type='submit' value='Search'> \n\t\t\t\t\t\t\t\t<input type='submit' name='des' value='Show All'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td valign='top' align='right' width='35%'>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th colspan='2'>Order Details</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'>{$pur['purnum']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Order No.</td>\n\t\t\t\t\t\t\t<td valign='center'><input type='text' size='10' name='ordernum' value='{$pur['ordernum']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Supplier Inv</td>\n\t\t\t\t\t\t\t<td><input type='text' size='10' name='supinv' value='{$pur['supinv']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Terms</td>\n\t\t\t\t\t\t\t<td valign='center'>{$termssel} Days</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Date</td>\n\t\t\t\t\t\t\t<td valign='center' nowrap='t'>" . mkDateSelect("pur", $pur_year, $pur_month, $pur_day) . "</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT Inclusive</td>\n\t\t\t\t\t\t\t<td valign='center'>\n\t\t\t\t\t\t\t\tYes <input type='radio' size='7' name='vatinc' value='yes' {$chy}> \n\t\t\t\t\t\t\t\tNo <input type='radio' size='7' name='vatinc' value='no' {$chn}></td></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charges</td>\n\t\t\t\t\t\t\t<td valign='center'><input type=text size=7 name=shipchrg value='{$pur['shipchrg']}'></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charges VAT Code</td>\n\t\t\t\t\t\t\t<td valign='center'>{$Vatcodes}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t" . TBL_BR . "\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'>{$products}</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th width='25%'>Quick Links</th>\n\t\t\t\t\t\t\t<th width='25%'>Remarks</th>\n\t\t\t\t\t\t\t<td rowspan='5' valign='top' width='50%'>{$ex} {$error}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'>\n\t\t\t\t\t\t\t\t<a href='supp-new.php?re={$pur['purnum']}'>New Supplier</a>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "' rowspan='5' align='center' valign='top'>\n\t\t\t\t\t\t\t\t<textarea name='remarks' rows='4' cols='20'>{$pur['remarks']}</textarea>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td class='" . bg_class() . "'><a href='purchase-view.php'>View Orders</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td align='right'>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='80%'>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>SUBTOTAL</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$SUBTOT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Delivery Charges</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$pur['shipping']}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>VAT {$vat14}</td>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<th>GRAND TOTAL</th>\n\t\t\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' align='center'><input name='addnon' type='submit' value='Add Non stock Product'> | <input type='submit' name='upBtn' value='Update'>{$done}</td>\n\t\t\t</tr>\n\t\t</table>\n\t\t<a name='bottom'>\n\t\t</form>\n\t\t</center>\n\t\t{$jump_bot}";
    return $details;
}