function SyncOrderInformation($TimeDifference, $ShowMessages, $LastTimeRun, $db, $db_oc, $oc_tableprefix, $EmailText = '')
{
    if ($EmailText != '') {
        $EmailText = $EmailText . "Sync OpenCart Order Information" . "\n" . PrintTimeInformation($db);
    }
    $SQL = "SELECT \t" . $oc_tableprefix . "order.order_id,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.customer_id,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.firstname AS customerfirstname,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.lastname AS customerlastname,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.email,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.telephone,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.fax,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.comment,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_firstname AS paymentfirstname,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_lastname AS paymentlastname,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_company AS paymentcompany,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_address_1,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_address_2,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_city,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_postcode,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_zone,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_country,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.payment_method,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_firstname AS shippingfirstname,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_lastname AS shippinglastname,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_company AS shippingcompany,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_address_1,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_address_2,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_city,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_postcode,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_zone,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_country,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_method,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.shipping_code,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.total,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.order_status_id,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.currency_code,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.currency_value,\r\n\t\t\t\t\t" . $oc_tableprefix . "order.date_modified\r\n\t\t\tFROM " . $oc_tableprefix . "order\r\n\t\t\tWHERE " . $oc_tableprefix . "order.order_status_id >= 1\r\n\t\t\t\tAND ( " . $oc_tableprefix . "order.date_added >= '" . $LastTimeRun . "'\r\n\t\t\t\t\tOR " . $oc_tableprefix . "order.date_modified >= '" . $LastTimeRun . "')\r\n\t\t\tORDER BY " . $oc_tableprefix . "order.order_id";
    $result = DB_query_oc($SQL);
    if (DB_num_rows($result) != 0) {
        if ($ShowMessages) {
            echo '<p class="page_title_text" align="center"><strong>' . _('Orders from OpenCart') . '</strong></p>';
            echo '<div>';
            $TableHeader = '<tr>
								<th>' . _('OC #') . '</th>
								<th>' . _('webERP #') . '</th>
								<th>' . _('Name') . '</th>
								<th>' . _('eMail') . '</th>
								<th>' . _('Shipping Cost') . '</th>
								<th>' . _('Shipper') . '</th>
								<th>' . _('Currency') . '</th>
								<th>' . _('Country') . '</th>
								<th>' . _('Action') . '</th>
							</tr>';
            $TableHeaderForItems = '<tr>
								<th>' . _('OC #') . '</th>
								<th>' . _('webERP #') . '</th>
								<th>' . _('OrderLine') . '</th>
								<th>' . _('Code') . '</th>
								<th>' . _('Unit Price') . '</th>
								<th>' . _('Quantity') . '</th>
								<th>' . _('Action') . '</th>
							</tr>';
        }
        $DbgMsg = _('The SQL statement that failed was');
        $UpdateErrMsg = _('The SQL to update OpenCart orders in webERP failed');
        $InsertErrMsg = _('The SQL to insert OpenCart orders in webERP failed');
        $k = 0;
        //row colour counter
        $i = 0;
        while ($myrow = DB_fetch_array($result)) {
            if ($ShowMessages) {
                echo '<table class="selection">';
                echo $TableHeader;
                echo '<tr class="EvenTableRows">';
            }
            /* FIELD MATCHING */
            $CustomerCode = GetWeberpCustomerIdFromCurrency($myrow['currency_code'], $db);
            $CustomerName = CapitalizeName($myrow['customerfirstname'] . ' ' . $myrow['customerlastname']);
            $PaymentName = CapitalizeName($myrow['paymentfirstname'] . ' ' . $myrow['paymentlastname']);
            $ShippingName = CapitalizeName($myrow['shippingfirstname'] . ' ' . $myrow['shippinglastname']);
            $SalesType = OPENCART_DEFAULT_CUSTOMER_SALES_TYPE;
            $DefaultShipVia = GetWeberpShippingMethod($myrow['shipping_method']);
            $Quotation = 1;
            // is NOT a firm order until we check the payments
            $FreightCost = RoundPriceFromCart(GetTotalFromOrder("shipping", $myrow['order_id'], $db_oc, $oc_tableprefix) * $myrow['currency_value'], $myrow['currency_code']);
            $CouponDiscount = RoundPriceFromCart(GetTotalFromOrder("coupon", $myrow['order_id'], $db_oc, $oc_tableprefix) * $myrow['currency_value'], $myrow['currency_code']);
            $OrderDiscount = RoundPriceFromCart(GetTotalFromOrder("dco", $myrow['order_id'], $db_oc, $oc_tableprefix) * $myrow['currency_value'], $myrow['currency_code']);
            $OpenCartOrderNumber = $myrow['order_id'];
            $Salesman = OPENCART_DEFAULT_SALESMAN;
            $Location = OPENCART_DEFAULT_LOCATION;
            $Comments = str_replace("'", "", $myrow['comment']);
            $WebERPDateOrder = date('Y-m-d H:i:s', strtotime($myrow['date_modified'] . -$TimeDifference . ' hours'));
            if ($CustomerCode == 'WEB-KL-IDR') {
                $Area = OPENCART_DEFAULT_AREA_INDONESIA;
            } else {
                $Area = OPENCART_DEFAULT_AREA;
            }
            if ($CustomerCode != 'Error') {
                // First process order header
                if (DataExistsInWebERP($db, 'salesorders', 'customerref', $myrow['order_id'])) {
                    $Action = "Update";
                } else {
                    $Action = "Insert";
                    do {
                        $OrderNo = GetNextSequenceNo(30);
                        $CheckDoesntExistResult = DB_query("SELECT count(*) FROM salesorders WHERE orderno='" . $OrderNo . "'", $db);
                        $CheckDoesntExistRow = DB_fetch_row($CheckDoesntExistResult);
                    } while ($CheckDoesntExistRow[0] == 1);
                    $sqlInsert = "INSERT INTO salesorders (\r\n\t\t\t\t\t\t\t\t\torderno,\r\n\t\t\t\t\t\t\t\t\tdebtorno,\r\n\t\t\t\t\t\t\t\t\tbranchcode,\r\n\t\t\t\t\t\t\t\t\tcustomerref,\r\n\t\t\t\t\t\t\t\t\tcomments,\r\n\t\t\t\t\t\t\t\t\torddate,\r\n\t\t\t\t\t\t\t\t\tordertype,\r\n\t\t\t\t\t\t\t\t\tshipvia,\r\n\t\t\t\t\t\t\t\t\tdeliverto,\r\n\t\t\t\t\t\t\t\t\tdeladd1,\r\n\t\t\t\t\t\t\t\t\tdeladd2,\r\n\t\t\t\t\t\t\t\t\tdeladd3,\r\n\t\t\t\t\t\t\t\t\tdeladd4,\r\n\t\t\t\t\t\t\t\t\tdeladd5,\r\n\t\t\t\t\t\t\t\t\tdeladd6,\r\n\t\t\t\t\t\t\t\t\tcontactphone,\r\n\t\t\t\t\t\t\t\t\tcontactemail,\r\n\t\t\t\t\t\t\t\t\tsalesperson,\r\n\t\t\t\t\t\t\t\t\tfromstkloc,\r\n\t\t\t\t\t\t\t\t\tfreightcost,\r\n\t\t\t\t\t\t\t\t\tquotation,\r\n\t\t\t\t\t\t\t\t\tarea,\r\n\t\t\t\t\t\t\t\t\tdeliverydate,\r\n\t\t\t\t\t\t\t\t\tquotedate,\r\n\t\t\t\t\t\t\t\t\tconfirmeddate)\r\n\t\t\t\t\t\t\t\tVALUES (\r\n\t\t\t\t\t\t\t\t\t'" . $OrderNo . "',\r\n\t\t\t\t\t\t\t\t\t'" . $CustomerCode . "',\r\n\t\t\t\t\t\t\t\t\t'" . $CustomerCode . "',\r\n\t\t\t\t\t\t\t\t\t'" . $OpenCartOrderNumber . "',\r\n\t\t\t\t\t\t\t\t\t'" . $Comments . "',\r\n\t\t\t\t\t\t\t\t\t'" . $WebERPDateOrder . "',\r\n\t\t\t\t\t\t\t\t\t'" . $SalesType . "',\r\n\t\t\t\t\t\t\t\t\t'" . $DefaultShipVia . "',\r\n\t\t\t\t\t\t\t\t\t'" . $ShippingName . "',\r\n\t\t\t\t\t\t\t\t\t'" . DB_escape_string($myrow['shipping_address_1']) . "',\r\n\t\t\t\t\t\t\t\t\t'" . DB_escape_string($myrow['shipping_address_2']) . "',\r\n\t\t\t\t\t\t\t\t\t'" . DB_escape_string($myrow['shipping_city']) . "',\r\n\t\t\t\t\t\t\t\t\t'" . DB_escape_string($myrow['shipping_zone']) . "',\r\n\t\t\t\t\t\t\t\t\t'" . DB_escape_string($myrow['shipping_postcode']) . "',\r\n\t\t\t\t\t\t\t\t\t'" . DB_escape_string($myrow['shipping_country']) . "',\r\n\t\t\t\t\t\t\t\t\t'" . DB_escape_string($myrow['telephone']) . "',\r\n\t\t\t\t\t\t\t\t\t'" . DB_escape_string($myrow['email']) . "',\r\n\t\t\t\t\t\t\t\t\t'" . $Salesman . "',\r\n\t\t\t\t\t\t\t\t\t'" . $Location . "',\r\n\t\t\t\t\t\t\t\t\t'" . $FreightCost . "',\r\n\t\t\t\t\t\t\t\t\t'" . $Quotation . "',\r\n\t\t\t\t\t\t\t\t\t'" . $Area . "',\r\n\t\t\t\t\t\t\t\t\t'" . $myrow['date_modified'] . "',\r\n\t\t\t\t\t\t\t\t\t'" . $myrow['date_modified'] . "',\r\n\t\t\t\t\t\t\t\t\t'" . $myrow['date_modified'] . "')";
                    $resultInsert = DB_query($sqlInsert, $db, $InsertErrMsg, $DbgMsg, true);
                }
                if ($ShowMessages) {
                    printf('<td>%s</td>
							<td>%s</td>
							<td>%s</td>
							<td>%s</td>
							<td>%s</td>
							<td>%s</td>
							<td>%s</td>
							<td>%s</td>
							<td>%s</td>
							</tr>', $myrow['order_id'], $OrderNo, $ShippingName, $myrow['email'], $FreightCost, $DefaultShipVia, $myrow['currency_code'], $myrow['shipping_country'], $Action);
                }
                if ($EmailText != '') {
                    $EmailText = $EmailText . $myrow['order_id'] . " = " . $OrderNo . " = " . $ShippingName . " = " . $myrow['email'] . " = " . $myrow['currency_code'] . " = " . $myrow['shipping_country'] . " --> " . $Action . "\n";
                }
                // Now the items of the order
                $SQLItemsOrder = "SELECT " . $oc_tableprefix . "order_product.model,\r\n\t\t\t\t\t\t\t\t\t\t" . $oc_tableprefix . "order_product.quantity,\r\n\t\t\t\t\t\t\t\t\t\t" . $oc_tableprefix . "order_product.price,\r\n\t\t\t\t\t\t\t\t\t\t" . $oc_tableprefix . "order_product.total,\r\n\t\t\t\t\t\t\t\t\t\t" . $oc_tableprefix . "order_product.tax,\r\n\t\t\t\t\t\t\t\t\t\t" . $oc_tableprefix . "order_product.reward\r\n\t\t\t\t\t\t\t\tFROM " . $oc_tableprefix . "order_product\r\n\t\t\t\t\t\t\t\tWHERE " . $oc_tableprefix . "order_product.order_id = " . $myrow['order_id'] . "\r\n\t\t\t\t\t\t\t\tORDER BY " . $oc_tableprefix . "order_product.order_product_id";
                $resultItemsOrder = DB_query_oc($SQLItemsOrder);
                $ItemsOrder = 0;
                if ($ShowMessages) {
                    echo '<table class="selection">';
                    echo $TableHeaderForItems;
                    echo '<tr class="OddTableRows">';
                }
                while ($myitems = DB_fetch_array($resultItemsOrder)) {
                    $ItemsOrder++;
                    if ($Action == "Update") {
                        $Action = "Update";
                    } else {
                        $Price = RoundPriceFromCart($myitems['price'] * $myrow['currency_value'], $myrow['currency_code']);
                        $sqlInsert = "INSERT INTO salesorderdetails\r\n\t\t\t\t\t\t\t\t\t\t\t(orderlineno,\r\n\t\t\t\t\t\t\t\t\t\t\torderno,\r\n\t\t\t\t\t\t\t\t\t\t\tstkcode,\r\n\t\t\t\t\t\t\t\t\t\t\tunitprice,\r\n\t\t\t\t\t\t\t\t\t\t\tquantity,\r\n\t\t\t\t\t\t\t\t\t\t\titemdue,\r\n\t\t\t\t\t\t\t\t\t\t\tdiscountpercent)\r\n\t\t\t\t\t\t\t\t\tVALUES ('" . $ItemsOrder . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $OrderNo . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $myitems['model'] . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $Price . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $myitems['quantity'] . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $myrow['date_modified'] . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'0')";
                        // prices come already net from OpenCart
                        $resultInsert = DB_query($sqlInsert, $db, $InsertErrMsg, $DbgMsg, true);
                        // prepare the RL for the items just ordered online
                        $sqlUpdate = "UPDATE locstock\r\n\t\t\t\t\t\t\t\t\t\tSET reorderlevel = reorderlevel + " . $myitems['quantity'] . "\r\n\t\t\t\t\t\t\t\t\t\tWHERE stockid = '" . $myitems['model'] . "'\r\n\t\t\t\t\t\t\t\t\t\tAND loccode = '" . $Location . "'";
                        $resultUpdate = DB_query($sqlUpdate, $db, $UpdateErrMsg, $DbgMsg, true);
                        if ($ShowMessages) {
                            printf('<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									</tr>', $myrow['order_id'], $OrderNo, $ItemsOrder, $myitems['model'], $Price, $myitems['quantity'], $Action);
                        }
                        if ($EmailText != '') {
                            $EmailText = $EmailText . "            " . $ItemsOrder . " = " . $myitems['model'] . " = " . $ShippingName . " = " . $Price . " = " . $myitems['quantity'] . " --> " . $Action . "\n";
                        }
                    }
                }
                if ($CouponDiscount != 0) {
                    $ItemsOrder++;
                    // we need to register the coupon use
                    $CouponCode = GetTotalTitleFromOrder("coupon", $myrow['order_id'], $db_oc, $oc_tableprefix);
                    $CouponStockId = OPENCART_ONLINE_COUPON_CODE;
                    $CouponQty = 1;
                    if ($Action == "Update") {
                        $Action = "Update";
                    } else {
                        $sqlInsert = "INSERT INTO salesorderdetails\r\n\t\t\t\t\t\t\t\t\t\t\t(orderlineno,\r\n\t\t\t\t\t\t\t\t\t\t\torderno,\r\n\t\t\t\t\t\t\t\t\t\t\tstkcode,\r\n\t\t\t\t\t\t\t\t\t\t\tunitprice,\r\n\t\t\t\t\t\t\t\t\t\t\tquantity,\r\n\t\t\t\t\t\t\t\t\t\t\titemdue,\r\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\r\n\t\t\t\t\t\t\t\t\t\t\tdiscountpercent)\r\n\t\t\t\t\t\t\t\t\tVALUES ('" . $ItemsOrder . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $OrderNo . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $CouponStockId . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $CouponDiscount . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $CouponQty . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $myrow['date_modified'] . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $CouponCode . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'0')";
                        // prices come already net from OpenCart
                        $resultInsert = DB_query($sqlInsert, $db, $InsertErrMsg, $DbgMsg, true);
                        if ($ShowMessages) {
                            printf('<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									</tr>', $myrow['order_id'], $OrderNo, $ItemsOrder, $CouponStockId, $CouponDiscount, $CouponQty, $Action);
                        }
                        if ($EmailText != '') {
                            $EmailText = $EmailText . "            " . $ItemsOrder . " = " . $CouponStockId . " = " . $CouponDiscount . " = " . $CouponQty . " --> " . $Action . "\n";
                        }
                    }
                }
                if ($OrderDiscount != 0) {
                    $ItemsOrder++;
                    // we need to register the dco discount use (GENERAL ORDER DISCOUNT)
                    $DiscountCode = GetTotalTitleFromOrder("dco", $myrow['order_id'], $db_oc, $oc_tableprefix);
                    $DiscountStockId = OPENCART_ONLINE_ORDER_DISCOUNT_CODE;
                    $DiscountQty = 1;
                    if ($Action == "Update") {
                        $Action = "Update";
                    } else {
                        $sqlInsert = "INSERT INTO salesorderdetails\r\n\t\t\t\t\t\t\t\t\t\t\t(orderlineno,\r\n\t\t\t\t\t\t\t\t\t\t\torderno,\r\n\t\t\t\t\t\t\t\t\t\t\tstkcode,\r\n\t\t\t\t\t\t\t\t\t\t\tunitprice,\r\n\t\t\t\t\t\t\t\t\t\t\tquantity,\r\n\t\t\t\t\t\t\t\t\t\t\titemdue,\r\n\t\t\t\t\t\t\t\t\t\t\tnarrative,\r\n\t\t\t\t\t\t\t\t\t\t\tdiscountpercent)\r\n\t\t\t\t\t\t\t\t\tVALUES ('" . $ItemsOrder . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $OrderNo . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $DiscountStockId . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $OrderDiscount . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $DiscountQty . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $myrow['date_modified'] . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'" . $DiscountCode . "',\r\n\t\t\t\t\t\t\t\t\t\t\t'0')";
                        // prices come already net from OpenCart
                        $resultInsert = DB_query($sqlInsert, $db, $InsertErrMsg, $DbgMsg, true);
                        if ($ShowMessages) {
                            printf('<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									<td>%s</td>
									</tr>', $myrow['order_id'], $OrderNo, $ItemsOrder, $DiscountStockId, $OrderDiscount, $DiscountQty, $Action);
                        }
                        if ($EmailText != '') {
                            $EmailText = $EmailText . "            " . $ItemsOrder . " = " . $DiscountStockId . " = " . $OrderDiscount . " = " . $DiscountQty . " --> " . $Action . "\n";
                        }
                    }
                }
                $i++;
                if ($ShowMessages) {
                    echo '</table>';
                    echo '</table>';
                }
            } else {
                // Order does not belong to a valid customer for any reason, escape it
                if ($ShowMessages) {
                    prnMsg('Sales Order from ' . $myrow['email'] . ' is not valid as is not a valid currency code.', 'warn');
                }
            }
        }
        if ($ShowMessages) {
            echo '</div>
					</form>';
        }
    }
    if ($ShowMessages) {
        prnMsg(locale_number_format($i, 0) . ' ' . _('Orders synchronized from OpenCart to webERP'), 'success');
    }
    if ($EmailText != '') {
        $EmailText = $EmailText . locale_number_format($i, 0) . ' ' . _('Orders synchronized from OpenCart to webERP') . "\n\n";
    }
    return $EmailText;
}
function TransactionCommissionGL($CustomerCode, $BankAccount, $CommissionAccount, $Commission, $Currency, $Rate, $PaymentSystem, $TransactionID, $PeriodNo, $db)
{
    $PaymentNo = GetNextSequenceNo(1);
    /*now enter the BankTrans entry */
    //First get the currency and rate for the bank account
    $BankResult = DB_query("SELECT rate FROM bankaccounts INNER JOIN currencies ON bankaccounts.currcode=currencies.currabrev WHERE accountcode='" . $BankAccount . "'", $db);
    $BankRow = DB_fetch_array($BankResult);
    $FunctionalRate = $BankRow['rate'];
    $SQL = "INSERT INTO banktrans (type,\r\n\t\t\t\t\t\t\t\ttransno,\r\n\t\t\t\t\t\t\t\tbankact,\r\n\t\t\t\t\t\t\t\tref,\r\n\t\t\t\t\t\t\t\texrate,\r\n\t\t\t\t\t\t\t\tfunctionalexrate,\r\n\t\t\t\t\t\t\t\ttransdate,\r\n\t\t\t\t\t\t\t\tbanktranstype,\r\n\t\t\t\t\t\t\t\tamount,\r\n\t\t\t\t\t\t\t\tcurrcode)\r\n\t\t\t\t\t\tVALUES (1,\r\n\t\t\t\t\t\t\t'" . $PaymentNo . "',\r\n\t\t\t\t\t\t\t'" . $BankAccount . "',\r\n\t\t\t\t\t\t\t'" . $PaymentSystem . ' ' . _('Transaction Fees') . ' ' . $CustomerCode . ' ' . $TransactionID . "',\r\n\t\t\t\t\t\t\t'" . $Rate / $FunctionalRate . "',\r\n\t\t\t\t\t\t\t'" . $FunctionalRate . "',\r\n\t\t\t\t\t\t\t'" . Date('Y-m-d') . "',\r\n\t\t\t\t\t\t\t'" . $PaymentSystem . ' ' . _('Transaction Fees') . "',\r\n\t\t\t\t\t\t\t'" . -($Commission * $Rate / $FunctionalRate) . "',\r\n\t\t\t\t\t\t\t'" . $Currency . "'\r\n\t\t\t\t\t\t)";
    $DbgMsg = _('The SQL that failed to insert the bank account transaction was');
    $ErrMsg = _('Cannot insert a bank transaction');
    $result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
    // Insert GL entries too if integration enabled
    if ($_SESSION['CompanyRecord']['gllink_debtors'] == 1) {
        /* then enter GLTrans records for discount, bank and debtors */
        /* Bank account entry first */
        $Narrative = $CustomerCode . ' ' . $PaymentSystem . ' ' . _('Fees for Transaction ID') . ': ' . $TransactionID;
        $SQL = "INSERT INTO gltrans (\ttype,\r\n\t\t\t\t\t\t\t\t\ttypeno,\r\n\t\t\t\t\t\t\t\t\ttrandate,\r\n\t\t\t\t\t\t\t\t\tperiodno,\r\n\t\t\t\t\t\t\t\t\taccount,\r\n\t\t\t\t\t\t\t\t\tnarrative,\r\n\t\t\t\t\t\t\t\t\tamount)\r\n\t\t\t\tVALUES (1,\r\n\t\t\t\t\t\t'" . $PaymentNo . "',\r\n\t\t\t\t\t\t'" . Date('Y-m-d') . "',\r\n\t\t\t\t\t\t'" . $PeriodNo . "',\r\n\t\t\t\t\t\t'" . $BankAccount . "',\r\n\t\t\t\t\t\t'" . $Narrative . "',\r\n\t\t\t\t\t\t'" . -($Commission / $Rate) . "'\r\n\t\t\t\t\t)";
        $DbgMsg = _('The SQL that failed to insert the Paypal transaction fee from the bank account debit was');
        $ErrMsg = _('Cannot insert a GL transaction for the bank account debit');
        $result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
        /* Now Credit Debtors account with receipts + discounts */
        $SQL = "INSERT INTO gltrans ( type,\r\n\t\t\t\t\t\t\t\t\ttypeno,\r\n\t\t\t\t\t\t\t\t\ttrandate,\r\n\t\t\t\t\t\t\t\t\tperiodno,\r\n\t\t\t\t\t\t\t\t\taccount,\r\n\t\t\t\t\t\t\t\t\tnarrative,\r\n\t\t\t\t\t\t\t\t\tamount)\r\n\t\t\t\t\tVALUES (1,\r\n\t\t\t\t\t\t\t'" . $PaymentNo . "',\r\n\t\t\t\t\t\t\t'" . Date('Y-m-d') . "',\r\n\t\t\t\t\t\t\t'" . $PeriodNo . "',\r\n\t\t\t\t\t\t\t'" . $CommissionAccount . "',\r\n\t\t\t\t\t\t\t'" . $Narrative . "',\r\n\t\t\t\t\t\t\t'" . $Commission / $Rate . "' )";
        $DbgMsg = _('The SQL that failed to insert the Paypal transaction fee for the commission account credit was');
        $ErrMsg = _('Cannot insert a GL transaction for the debtors account credit');
        $result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
        EnsureGLEntriesBalanceOpenCart(1, $PaymentNo);
    }
    //end if there is GL work to be done - ie config is to link to GL
}