}
/* Set the session variables to the posted data from the form if the page has called itself */
if (isset($_POST['ExRate'])) {
    $_SESSION['SuppTrans']->ExRate = filter_number_format($_POST['ExRate']);
    $_SESSION['SuppTrans']->Comments = $_POST['Comments'];
    $_SESSION['SuppTrans']->TranDate = $_POST['TranDate'];
    if (mb_substr($_SESSION['SuppTrans']->Terms, 0, 1) == '1') {
        /*Its a day in the following month when due */
        $DayInFollowingMonth = (int) mb_substr($_SESSION['SuppTrans']->Terms, 1);
        $DaysBeforeDue = 0;
    } else {
        /*Use the Days Before Due to add to the invoice date */
        $DayInFollowingMonth = 0;
        $DaysBeforeDue = (int) mb_substr($_SESSION['SuppTrans']->Terms, 1);
    }
    $_SESSION['SuppTrans']->DueDate = CalcDueDate($_SESSION['SuppTrans']->TranDate, $DayInFollowingMonth, $DaysBeforeDue);
    $_SESSION['SuppTrans']->SuppReference = $_POST['SuppReference'];
    if ($_SESSION['SuppTrans']->GLLink_Creditors == 1) {
        /*The link to GL from creditors is active so the total should be built up from GLPostings and GRN entries
        if the link is not active then OvAmount must be entered manually. */
        $_SESSION['SuppTrans']->OvAmount = 0;
        /* for starters */
        if (count($_SESSION['SuppTrans']->GRNs) > 0) {
            foreach ($_SESSION['SuppTrans']->GRNs as $GRN) {
                $_SESSION['SuppTrans']->OvAmount = $_SESSION['SuppTrans']->OvAmount + $GRN->This_QuantityInv * $GRN->ChgPrice;
            }
        }
        if (count($_SESSION['SuppTrans']->GLCodes) > 0) {
            foreach ($_SESSION['SuppTrans']->GLCodes as $GLLine) {
                $_SESSION['SuppTrans']->OvAmount += $GLLine->Amount;
            }
 $TranDate = date('Ymd');
 //probably should use the date edi was created not the date filed in our system
 $TranDateTime = date('Ymd:hi');
 $OrderNo = $TransDetails['order_'];
 $CustBranchCode = $TransDetails['branchcode'];
 $BranchName = $TransDetails['brname'];
 $BranchStreet = $TransDetails['braddress1'];
 $BranchSuburb = $TransDetails['braddress2'];
 $BranchState = $TransDetails['braddress3'];
 $BranchZip = $TransDetails['braddress4'];
 $BranchCountry = $TransDetails['braddress5'];
 $ExchRate = $TransDetails['rate'];
 $TaxTotal = number_format($TransDetails['ovgst'], 2, '.', '');
 $ShipToFreight = number_format(round($TransDetails['ovfreight'], 2), 2, '.', '');
 $SegCount = 1;
 $DatePaymentDue = ConvertToEDIDate(CalcDueDate(ConvertSQLDate($TransDetails['trandate']), $CustDetails['dayinfollowingmonth'], $CustDetails['daysbeforedue']));
 $TotalAmountExclTax = number_format($TransDetails['ovamount'] + $TransDetails['ovfreight'] + $TransDetails['ovdiscount'], 2, '.', '');
 $TotalAmountInclTax = number_format($TransDetails['ovamount'] + $TransDetails['ovfreight'] + $TransDetails['ovdiscount'] + $TransDetails['ovgst'], 2, '.', '');
 //**************Need to get delivery address as may be diff from branch address
 $sql = "SELECT deliverto,\n\t\t\t\tdeladd1,\n\t\t\t\tdeladd2,\n\t\t\t\tdeladd3,\n\t\t\t\tdeladd4,\n\t\t\t\tdeladd5,\n\t\t\t\tdeladd6,\n\t\t\t\tsalesorders.customerref\n\t\t\t\tFROM debtortrans INNER JOIN salesorders ON debtortrans.order_ = salesorders.orderno\n\t\t\t\tWHERE order_ = '" . $OrderNo . "'";
 $ErrMsg = _('There was a problem retrieving the ship to details because');
 $ShipToLines = DB_query($sql, $ErrMsg);
 while ($ShipTo = DB_fetch_array($ShipToLines)) {
     $ShipToName = $ShipTo[0];
     $ShipToStreet = $ShipTo[1];
     $ShipToSuburb = $ShipTo[2];
     $ShipToState = $ShipTo[3];
     $ShipToZip = $ShipTo[4];
     $ShipToCountry = $ShipTo[5];
     $CustOrderNo = $ShipTo[7];
 }