Beispiel #1
0
        private string CreateBarCode()
        {
            string strRetValue = "";

            Data.ProductSubGroup clsProductSubGroup = new Data.ProductSubGroup();
            string strProductCode = clsProductSubGroup.getBarCodeCounter(Int64.Parse(lblProductSubGroupID.Text)).ToString().PadLeft(13 - (lblProductSubGroupID.Text.Length + 2), '0');

            clsProductSubGroup.CommitAndDispose();

            BarcodeHelper ean13 = new BarcodeHelper("99", lblProductSubGroupID.Text, strProductCode);

            strRetValue = ean13.CountryCode + ean13.ManufacturerCode + ean13.ProductCode + ean13.ChecksumDigit;

            return(strRetValue);
        }
        public void CloseTransaction(Data.SalesTransactionItemDetails mTopItemDetails,
                decimal AmountPaid, decimal ChangeAmount, decimal BalanceAmount, decimal CashPayment, decimal ChequePayment,
                decimal CreditCardPayment, decimal CreditPayment, decimal CreditChargeAmount, decimal DebitPayment,
                decimal RewardConvertedPayment, decimal RewardPointsPayment, PaymentTypes PaymentType,
                ArrayList arrCashPaymentDetails, ArrayList arrChequePaymentDetails, ArrayList arrCreditCardPaymentDetails,
                ArrayList arrCreditPaymentDetails, ArrayList arrDebitPaymentDetails)
        {
            mclsSalesTransactionDetails.AmountPaid = AmountPaid;
            mclsSalesTransactionDetails.ChangeAmount = ChangeAmount;
            mclsSalesTransactionDetails.CashPayment = CashPayment;
            mclsSalesTransactionDetails.ChequePayment = ChequePayment;
            mclsSalesTransactionDetails.CreditCardPayment = CreditCardPayment;
            mclsSalesTransactionDetails.CreditPayment = CreditPayment;

            // include credit charge amount
            mclsSalesTransactionDetails.CreditChargeAmount = CreditChargeAmount;
            mclsSalesTransactionDetails.AmountDue += mclsSalesTransactionDetails.CreditChargeAmount;

            mclsSalesTransactionDetails.DebitPayment = DebitPayment;
            mclsSalesTransactionDetails.RewardPointsPayment = RewardConvertedPayment;

            /***
             * Nov 04, 2011 : for payments using reward points
             ***/
            mclsSalesTransactionDetails.RewardPointsPayment = RewardPointsPayment;
            mclsSalesTransactionDetails.RewardConvertedPayment = RewardConvertedPayment;

            /***
             * Oct 17, 2011 : Move this code here.
             * check if will print transaction or not before opening any connection to database.
             ***/
            mboDoNotPrintTransactionDate = false;
            if (mclsTerminalDetails.AutoPrint == PrintingPreference.AskFirst)
            {
                if (MessageBox.Show("Would you like to print this transaction?", "RetailPlus", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, (mclsSysConfigDetails.isDefaultButtonYesInPrintTransaction ? MessageBoxDefaultButton.Button1 : MessageBoxDefaultButton.Button2)) == DialogResult.Yes)
                {
                    mclsTerminalDetails.AutoPrint = PrintingPreference.Normal;

                    if (mclsSysConfigDetails.WillAskDoNotPrintTransactionDate)
                        if (MessageBox.Show("Would you like the system NOT to print the transaction date?", "RetailPlus", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                            mboDoNotPrintTransactionDate = true;
                }
            }

            // Mar 17, 2009
            // open drawer first before printing.
            //OpenDrawerDelegate opendrawerDel = new OpenDrawerDelegate(OpenDrawer);
            //Invoke(opendrawerDel);
            OpenDrawer();

            Cursor.Current = Cursors.WaitCursor;
            clsEvent.AddEventLn("[" + mclsSalesTransactionDetails.CashierName + "]      saving payments...", true);

            // for assignment of payments
            mclsSalesTransactionDetails.PaymentDetails = AssignArrayListPayments(arrCashPaymentDetails, arrChequePaymentDetails, arrCreditCardPaymentDetails, arrCreditPaymentDetails, arrDebitPaymentDetails);

            // start a connection for the database.
            //update the transaction table 
            Data.SalesTransactions clsSalesTransactions = new Data.SalesTransactions(mConnection, mTransaction);
            mConnection = clsSalesTransactions.Connection; mTransaction = clsSalesTransactions.Transaction;
            
            SavePayments(arrCashPaymentDetails, arrChequePaymentDetails, arrCreditCardPaymentDetails, arrCreditPaymentDetails, arrDebitPaymentDetails);

            if (mclsSalesTransactionDetails.CreditChargeAmount != 0)
            {
                //Aug 30, 2014 delete need to move this from here to mainwnd
                clsSalesTransactions.UpdateCreditChargeAmount(mclsSalesTransactionDetails.BranchID, mclsSalesTransactionDetails.TerminalNo, mclsSalesTransactionDetails.TransactionID, mclsSalesTransactionDetails.CreditChargeAmount);
            }

            Data.Products clsProduct = new Data.Products(mConnection, mTransaction);
            mConnection = clsProduct.Connection; mTransaction = clsProduct.Transaction;

            TransactionStatus clsTransactionStatus = TransactionStatus.Closed;
            if (mTopItemDetails.BarCode == Data.Products.DEFAULT_CREDIT_PAYMENT_BARCODE)
            {
                clsTransactionStatus = TransactionStatus.CreditPayment;
            }
            else if (mboIsRefund)
            {
                clsTransactionStatus = TransactionStatus.Refund;

                if (mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.OutOfStockCustomerCode)
                {
                    clsTransactionStatus = TransactionStatus.ClosedOutOfStockRefund;
                }
                else if (mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.WalkInCustomerCode)
                {
                    clsTransactionStatus = TransactionStatus.ClosedWalkInRefund;
                }
                else if (mclsSalesTransactionDetails.isConsignment)
                {
                    clsTransactionStatus = TransactionStatus.ConsignmentRefund;
                }
            }
            else if (mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.OutOfStockCustomerCode)
            {
                clsTransactionStatus = TransactionStatus.ClosedOutOfStock;
            }
            else if (mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.WalkInCustomerCode)
            {
                clsTransactionStatus = TransactionStatus.ClosedWalkIn;
            }
            else if (mclsSalesTransactionDetails.isConsignment)
            {
                clsTransactionStatus = TransactionStatus.Consignment;
            }

            if (mboIsRefund && !mclsTerminalDetails.IsParkingTerminal)
            {
                #region mboIsRefund

                clsEvent.AddEventLn("[" + mclsSalesTransactionDetails.CashierName + "]      updating refund terminal no...", true, mclsSysConfigDetails.WillWriteSystemLog);
                clsSalesTransactions.UpdateTerminalNo(mclsSalesTransactionDetails.TransactionID, mclsSalesTransactionDetails.TerminalNo);

                if (mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.OutOfStockCustomerCode ||
                    mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.WalkInCustomerCode)
                {
                    // 20Feb2015 : Added as an exemption
                    clsEvent.AddEventLn("      no need to apply an ORNo. Customer is " + mclsSalesTransactionDetails.CustomerDetails.ContactCode, true, mclsSysConfigDetails.WillWriteSystemLog);
                }
                
                else if (!mclsSalesTransactionDetails.isConsignment)
                {
                    // 04Sep2014 : Added to put as OR No - Void NO-OR
                    mclsSalesTransactionDetails.ORNo = clsSalesTransactions.CreateORNo(mclsTerminalDetails.ORSeriesBranchID, mclsTerminalDetails.ORSeriesTerminalNo);
                    clsEvent.AddEventLn("      applying ORNo".PadRight(15) + ":" + mclsSalesTransactionDetails.ORNo, true, mclsSysConfigDetails.WillWriteSystemLog);
                }

                clsSalesTransactions.Refund(clsTransactionStatus, mclsSalesTransactionDetails.TransactionID, mclsSalesTransactionDetails.ORNo, -mclsSalesTransactionDetails.ItemSold, -mclsSalesTransactionDetails.QuantitySold, -mclsSalesTransactionDetails.GrossSales, -mclsSalesTransactionDetails.SubTotal, -mclsSalesTransactionDetails.NetSales, -mclsSalesTransactionDetails.ItemsDiscount, -mclsSalesTransactionDetails.SNRItemsDiscount, -mclsSalesTransactionDetails.PWDItemsDiscount, -mclsSalesTransactionDetails.OtherItemsDiscount, -mclsSalesTransactionDetails.Discount, -mclsSalesTransactionDetails.SNRDiscount, -mclsSalesTransactionDetails.PWDDiscount, -mclsSalesTransactionDetails.OtherDiscount, mclsSalesTransactionDetails.TransDiscount, mclsSalesTransactionDetails.TransDiscountType, -mclsSalesTransactionDetails.VAT, -mclsSalesTransactionDetails.VATableAmount, -mclsSalesTransactionDetails.ZeroRatedSales, - mclsSalesTransactionDetails.NonVATableAmount, -mclsSalesTransactionDetails.VATExempt, -mclsSalesTransactionDetails.EVAT, -mclsSalesTransactionDetails.EVATableAmount, -mclsSalesTransactionDetails.NonEVATableAmount, -mclsSalesTransactionDetails.LocalTax, -mclsSalesTransactionDetails.AmountPaid, -CashPayment, -ChequePayment, -CreditCardPayment, -CreditPayment, -DebitPayment, -RewardPointsPayment, -RewardConvertedPayment, -BalanceAmount, -ChangeAmount, PaymentType, mclsSalesTransactionDetails.DiscountCode, mclsSalesTransactionDetails.DiscountRemarks, -mclsSalesTransactionDetails.Charge, mclsSalesTransactionDetails.ChargeAmount, mclsSalesTransactionDetails.ChargeCode, mclsSalesTransactionDetails.ChargeRemarks, mclsSalesTransactionDetails.CashierID, mclsSalesTransactionDetails.CashierName);

                //UpdateTerminalReportDelegate updateterminalDel = new UpdateTerminalReportDelegate(UpdateTerminalReport);
                clsEvent.AddEventLn("[" + mclsSalesTransactionDetails.CashierName + "]      updating refund terminal report...", true, mclsSysConfigDetails.WillWriteSystemLog);
                UpdateTerminalReport(clsTransactionStatus, mclsSalesTransactionDetails.ItemSold, mclsSalesTransactionDetails.QuantitySold, mclsSalesTransactionDetails.SubTotal, mclsSalesTransactionDetails.Discount, mclsSalesTransactionDetails.ItemsDiscount, mclsSalesTransactionDetails.SNRItemsDiscount, mclsSalesTransactionDetails.PWDItemsDiscount, mclsSalesTransactionDetails.OtherItemsDiscount, mclsSalesTransactionDetails.Charge, mclsSalesTransactionDetails.VAT, mclsSalesTransactionDetails.VATableAmount, mclsSalesTransactionDetails.ZeroRatedSales, mclsSalesTransactionDetails.NonVATableAmount, mclsSalesTransactionDetails.VATExempt, mclsSalesTransactionDetails.EVAT, mclsSalesTransactionDetails.EVATableAmount, mclsSalesTransactionDetails.NonEVATableAmount, mclsSalesTransactionDetails.LocalTax, CashPayment, ChequePayment, CreditCardPayment, CreditPayment, DebitPayment, RewardPointsPayment, RewardConvertedPayment, PaymentType);

                //UpdateCashierReportDelegate updatecashierDel = new UpdateCashierReportDelegate(UpdateCashierReport);
                clsEvent.AddEventLn("[" + mclsSalesTransactionDetails.CashierName + "]      updating redunf cashier report...", true, mclsSysConfigDetails.WillWriteSystemLog);
                UpdateCashierReport(clsTransactionStatus, mclsSalesTransactionDetails.ItemSold, mclsSalesTransactionDetails.QuantitySold, mclsSalesTransactionDetails.SubTotal, mclsSalesTransactionDetails.Discount, mclsSalesTransactionDetails.ItemsDiscount, mclsSalesTransactionDetails.SNRItemsDiscount, mclsSalesTransactionDetails.PWDItemsDiscount, mclsSalesTransactionDetails.OtherItemsDiscount, mclsSalesTransactionDetails.Charge, mclsSalesTransactionDetails.VAT, mclsSalesTransactionDetails.VATableAmount, mclsSalesTransactionDetails.ZeroRatedSales, mclsSalesTransactionDetails.NonVATableAmount, mclsSalesTransactionDetails.VATExempt, mclsSalesTransactionDetails.EVAT, mclsSalesTransactionDetails.EVATableAmount, mclsSalesTransactionDetails.NonEVATableAmount, mclsSalesTransactionDetails.LocalTax, CashPayment, ChequePayment, CreditCardPayment, CreditPayment, DebitPayment, RewardPointsPayment, RewardConvertedPayment, PaymentType);

                // Sep 24, 2014 : update back the LastCheckInDate to min date
                Data.Contacts clsContact = new Data.Contacts(mConnection, mTransaction);
                mConnection = clsContact.Connection; mTransaction = clsContact.Transaction;

                clsContact.UpdateLastCheckInDate(mclsSalesTransactionDetails.CustomerID, Constants.C_DATE_MIN_VALUE);

                // Sep 14, 2014 Control printing in mclsFilePrinter.Write
                //if (mclsTerminalDetails.AutoPrint == PrintingPreference.Normal)	//print items if not yet printed
                //{
                clsEvent.AddEventLn("[" + mclsSalesTransactionDetails.CashierName  + "]      printing refund items...", true, mclsSysConfigDetails.WillWriteSystemLog);
                foreach (System.Data.DataRow dr in ItemDataTable.Rows)
                {
                    if (dr["Quantity"].ToString() != "VOID")
                    {
                        string stItemNo = "" + dr["ItemNo"].ToString();
                        string stProductCode = "" + dr["ProductCode"].ToString();
                        if (dr["MatrixDescription"].ToString() != string.Empty && dr["MatrixDescription"].ToString() != null) stProductCode += "-" + dr["MatrixDescription"].ToString();
                        string stProductUnitCode = "" + dr["ProductUnitCode"].ToString();
                        decimal decQuantity = Convert.ToDecimal(dr["Quantity"]);
                        decimal decPrice = Convert.ToDecimal(dr["Price"]);
                        decimal decDiscount = Convert.ToDecimal(dr["Discount"]);
                        decimal decAmount = Convert.ToDecimal(dr["Amount"]);
                        decimal decVAT = Convert.ToDecimal(dr["VAT"]);
                        decimal decEVAT = Convert.ToDecimal(dr["EVAT"]);
                        decimal decPromoApplied = Convert.ToDecimal(dr["PromoApplied"]);
                        string stDiscountCode = "" + dr["DiscountCode"].ToString();
                        DiscountTypes ItemDiscountType = (DiscountTypes)Enum.Parse(typeof(DiscountTypes), dr["ItemDiscountType"].ToString());

                        if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.Default)
                            PrintItem(stItemNo, stProductCode, stProductUnitCode, decQuantity, decPrice, decDiscount, decPromoApplied, decAmount, decVAT, decEVAT, stDiscountCode, ItemDiscountType);
                    }
                }

                if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.Default)
                    PrintReportFooterSection(true, TransactionStatus.Refund, mclsSalesTransactionDetails.ItemSold, mclsSalesTransactionDetails.QuantitySold, mclsSalesTransactionDetails.SubTotal, mclsSalesTransactionDetails.Discount, mclsSalesTransactionDetails.Charge, mclsSalesTransactionDetails.AmountPaid, CashPayment, ChequePayment, CreditCardPayment, CreditPayment, DebitPayment, RewardPointsPayment, RewardConvertedPayment, ChangeAmount, arrChequePaymentDetails, arrCreditCardPaymentDetails, arrCreditPaymentDetails, arrDebitPaymentDetails);
                //}
                // Sep 14, 2014 Control printing in mclsFilePrinter.Write

                // Sep 24, 2011      Lemuel E. Aceron
                // Added order slip wherein all punch items will not change sales and inventory
                // a customer named ORDER SLIP should be defined in contacts
                // lblCustomer.Text.Trim().ToUpper() != Constants.C_RETAILPLUS_ORDER_SLIP_CUSTOMER

                // Added May 7, 2011 to Cater Reserved and Commit functionality
                // !mclsTerminalDetails.ReservedAndCommit

                // Sep 14, 2013: Remove the reserved and commit.
                //if (lblCustomer.Text.Trim().ToUpper() != Constants.C_RETAILPLUS_ORDER_SLIP_CUSTOMER && !mclsTerminalDetails.ReservedAndCommit && !mclsTerminalDetails.IsParkingTerminal)
                if (mclsSalesTransactionDetails.CustomerName.Trim().ToUpper() != Constants.C_RETAILPLUS_ORDER_SLIP_CUSTOMER && 
                    !mclsTerminalDetails.IsParkingTerminal &&
                    mclsSalesTransactionDetails.CustomerDetails.ContactCode.Trim() != mclsSysConfigDetails.OutOfStockCustomerCode) // do not change the inventory if OUT_OF_STOCK_CUSTOMER_CODE
                {
                    clsEvent.AddEventLn("[" + mclsSalesTransactionDetails.CashierName + "]      adding the refund items quantity to inv...", true, mclsSysConfigDetails.WillWriteSystemLog);
                    Data.ProductUnit clsProductUnit = new Data.ProductUnit(mConnection, mTransaction);
                    mConnection = clsProductUnit.Connection; mTransaction = clsProductUnit.Transaction;

                    foreach (System.Data.DataRow dr in ItemDataTable.Rows)
                    {
                        Int64 lProductID = Convert.ToInt64(dr["ProductID"]);
                        Int64 lVariationsMatrixID = Convert.ToInt64(dr["VariationsMatrixID"]);
                        Int32 iProductUnitID = Convert.ToInt32(dr["ProductUnitID"]);
                        decimal decQuantity = 0;
                        decimal decPackageQuantity = 0;
                        decimal decNewQuantity = 0;

                        if (dr["Quantity"].ToString().IndexOf("RETURN") != -1)
                        {
                            decimal decPrice = Convert.ToDecimal(dr["Price"]);
                            decimal decPurchasePrice = Convert.ToDecimal(dr["PurchasePrice"]);

                            decimal decDiscount = Convert.ToDecimal(dr["Discount"]);
                            decimal decAmount = -Convert.ToDecimal(dr["Amount"]);

                            decQuantity = Convert.ToDecimal(dr["Quantity"].ToString().Replace(" - RETURN", "").Trim());
                            decPackageQuantity = Convert.ToDecimal(dr["PackageQuantity"]);
                            decNewQuantity = clsProductUnit.GetBaseUnitValue(lProductID, iProductUnitID, decQuantity * decPackageQuantity);
                            decNewQuantity = -decNewQuantity;

                            clsEvent.AddEventLn("      subtracting refund-return item: prdid-" + lProductID.ToString() + " to inv: qty-" + decNewQuantity.ToString() + "...", true, mclsSysConfigDetails.WillWriteSystemLog);
                            clsProduct.SubtractQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.DEDUCT_REFUND_RETURN) + " @ " + (decAmount / decNewQuantity).ToString("#,##0.#0") + " Buying: " + decPurchasePrice.ToString("#,##0.#0") + " Orig Selling: " + decPrice.ToString("#,##0.#0") + " Discount: " + (decPrice - (decAmount / decNewQuantity)).ToString("#,##0.#0") + " to " + mclsSalesTransactionDetails.CustomerName + " Ter#: " + mclsTerminalDetails.TerminalNo, DateTime.Now, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);
                        }
                        else if (dr["Quantity"].ToString().IndexOf("DEMO") != -1)
                        {
                            decQuantity = Convert.ToDecimal(dr["Quantity"].ToString().Replace(" - DEMO", "").Trim());
                            decPackageQuantity = Convert.ToDecimal(dr["PackageQuantity"]);
                            decNewQuantity = clsProductUnit.GetBaseUnitValue(lProductID, iProductUnitID, decQuantity * decPackageQuantity);

                            clsEvent.AddEventLn("      adding refund-demo item: prdid-" + lProductID.ToString() + " from inv/rsrvd: qty-" + decNewQuantity.ToString() + "...", true, mclsSysConfigDetails.WillWriteSystemLog);
                            clsProduct.AddQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.ADD_REFUND_DEMO_ITEM) + " Ter#: " + mclsTerminalDetails.TerminalNo, mclsSalesTransactionDetails.TransactionDate, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);
                        }
                        else if (dr["Quantity"].ToString() != "VOID")
                        {
                            decQuantity = Convert.ToDecimal(dr["Quantity"]);
                            decPackageQuantity = Convert.ToDecimal(dr["PackageQuantity"]);
                            decNewQuantity = clsProductUnit.GetBaseUnitValue(lProductID, iProductUnitID, decQuantity * decPackageQuantity);

                            clsEvent.AddEventLn("      adding refund item: prdid-" + lProductID.ToString() + " from inv/rsrvd: qty-" + decNewQuantity.ToString() + "...", true, mclsSysConfigDetails.WillWriteSystemLog);
                            clsProduct.AddQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.ADD_REFUND_ITEM) + " Ter#: " + mclsTerminalDetails.TerminalNo, mclsSalesTransactionDetails.TransactionDate, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);
                        }
                    }
                }
                #endregion
            }
            else if (!mboIsRefund)
            {
                #region Normal and ParkingTerminal

                clsEvent.AddEventLn("      closing transaction...", true, mclsSysConfigDetails.WillWriteSystemLog);

                
                if (mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.OutOfStockCustomerCode ||
                    mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.WalkInCustomerCode)
                {
                    // 20Feb2015 : Added as an exemption
                    clsEvent.AddEventLn("      no need to apply an ORNo. Customer is " + mclsSalesTransactionDetails.CustomerDetails.ContactCode, true, mclsSysConfigDetails.WillWriteSystemLog);
                }
                else if (!mclsSalesTransactionDetails.isConsignment)
                {
                    // 04Sep2014 : Added to put as OR No - Void NO-OR
                    if (mTopItemDetails.BarCode == Data.Products.DEFAULT_CREDIT_PAYMENT_BARCODE ||
                            mTopItemDetails.BarCode == Data.Products.DEFAULT_ADVANTAGE_CARD_MEMBERSHIP_FEE_BARCODE ||
                            mTopItemDetails.BarCode == Data.Products.DEFAULT_ADVANTAGE_CARD_RENEWAL_FEE_BARCODE ||
                            mTopItemDetails.BarCode == Data.Products.DEFAULT_ADVANTAGE_CARD_REPLACEMENT_FEE_BARCODE ||
                            mTopItemDetails.BarCode == Data.Products.DEFAULT_CREDIT_CARD_MEMBERSHIP_FEE_BARCODE ||
                            mTopItemDetails.BarCode == Data.Products.DEFAULT_CREDIT_CARD_RENEWAL_FEE_BARCODE ||
                            mTopItemDetails.BarCode == Data.Products.DEFAULT_SUPER_CARD_MEMBERSHIP_FEE_BARCODE ||
                            mTopItemDetails.BarCode == Data.Products.DEFAULT_SUPER_CARD_RENEWAL_FEE_BARCODE ||
                            mTopItemDetails.BarCode == Data.Products.DEFAULT_SUPER_CARD_REPLACEMENT_FEE_BARCODE)
                    {
                        clsEvent.AddEventLn("      no need to apply an ORNo. Product is " + mTopItemDetails.BarCode, true, mclsSysConfigDetails.WillWriteSystemLog);
                    }
                    else
                    {
                        mclsSalesTransactionDetails.ORNo = clsSalesTransactions.CreateORNo(mclsTerminalDetails.ORSeriesBranchID, mclsTerminalDetails.ORSeriesTerminalNo);
                        clsEvent.AddEventLn("      applying ORNo".PadRight(15) + ":" + mclsSalesTransactionDetails.ORNo, true, mclsSysConfigDetails.WillWriteSystemLog);
                    }
                }

                // clsSalesTransactions.UpdateTerminalNo(mclsSalesTransactionDetails.TransactionID, mclsSalesTransactionDetails.TerminalNo);
                // remove the above. Include the TerminalNo in clsSalesTransactions.Close below
                clsSalesTransactions.Close(mclsSalesTransactionDetails.TransactionID, mclsSalesTransactionDetails.TerminalNo, mclsSalesTransactionDetails.ORNo, mclsSalesTransactionDetails.ItemSold, mclsSalesTransactionDetails.QuantitySold, mclsSalesTransactionDetails.GrossSales, mclsSalesTransactionDetails.SubTotal, mclsSalesTransactionDetails.NetSales, mclsSalesTransactionDetails.ItemsDiscount, mclsSalesTransactionDetails.SNRItemsDiscount, mclsSalesTransactionDetails.PWDItemsDiscount, mclsSalesTransactionDetails.OtherItemsDiscount, mclsSalesTransactionDetails.Discount, mclsSalesTransactionDetails.SNRDiscount, mclsSalesTransactionDetails.PWDDiscount, mclsSalesTransactionDetails.OtherDiscount, mclsSalesTransactionDetails.TransDiscount, mclsSalesTransactionDetails.TransDiscountType, mclsSalesTransactionDetails.VAT, mclsSalesTransactionDetails.VATableAmount, mclsSalesTransactionDetails.ZeroRatedSales, mclsSalesTransactionDetails.NonVATableAmount, mclsSalesTransactionDetails.VATExempt, mclsSalesTransactionDetails.EVAT, mclsSalesTransactionDetails.EVATableAmount, mclsSalesTransactionDetails.NonEVATableAmount, mclsSalesTransactionDetails.LocalTax, mclsSalesTransactionDetails.AmountPaid, CashPayment, ChequePayment, CreditCardPayment, CreditPayment, DebitPayment, RewardPointsPayment, RewardConvertedPayment, BalanceAmount, ChangeAmount, PaymentType, mclsSalesTransactionDetails.DiscountCode, mclsSalesTransactionDetails.DiscountRemarks, mclsSalesTransactionDetails.Charge, mclsSalesTransactionDetails.ChargeAmount, mclsSalesTransactionDetails.ChargeCode, mclsSalesTransactionDetails.ChargeRemarks, mclsSalesTransactionDetails.CashierID, mclsSalesTransactionDetails.CashierName, clsTransactionStatus);

                //UpdateTerminalReportDelegate updateterminalDel = new UpdateTerminalReportDelegate(UpdateTerminalReport);
                clsEvent.AddEventLn("      updating terminal report...", true, mclsSysConfigDetails.WillWriteSystemLog);
                UpdateTerminalReport(clsTransactionStatus, mclsSalesTransactionDetails.ItemSold, mclsSalesTransactionDetails.QuantitySold, mclsSalesTransactionDetails.SubTotal, mclsSalesTransactionDetails.Discount, mclsSalesTransactionDetails.ItemsDiscount, mclsSalesTransactionDetails.SNRItemsDiscount, mclsSalesTransactionDetails.PWDItemsDiscount, mclsSalesTransactionDetails.OtherItemsDiscount, mclsSalesTransactionDetails.Charge, mclsSalesTransactionDetails.VAT, mclsSalesTransactionDetails.VATableAmount, mclsSalesTransactionDetails.ZeroRatedSales, mclsSalesTransactionDetails.NonVATableAmount, mclsSalesTransactionDetails.VATExempt, mclsSalesTransactionDetails.EVAT, mclsSalesTransactionDetails.EVATableAmount, mclsSalesTransactionDetails.NonEVATableAmount, mclsSalesTransactionDetails.LocalTax, CashPayment, ChequePayment, CreditCardPayment, CreditPayment, DebitPayment, RewardPointsPayment, RewardConvertedPayment, PaymentType);

                //UpdateCashierReportDelegate updatecashierDel = new UpdateCashierReportDelegate(UpdateCashierReport);
                clsEvent.AddEventLn("      updating cashier's report...", true, mclsSysConfigDetails.WillWriteSystemLog);
                UpdateCashierReport(clsTransactionStatus, mclsSalesTransactionDetails.ItemSold, mclsSalesTransactionDetails.QuantitySold, mclsSalesTransactionDetails.SubTotal, mclsSalesTransactionDetails.Discount, mclsSalesTransactionDetails.ItemsDiscount, mclsSalesTransactionDetails.SNRItemsDiscount, mclsSalesTransactionDetails.PWDItemsDiscount, mclsSalesTransactionDetails.OtherItemsDiscount, mclsSalesTransactionDetails.Charge, mclsSalesTransactionDetails.VAT, mclsSalesTransactionDetails.VATableAmount, mclsSalesTransactionDetails.ZeroRatedSales, mclsSalesTransactionDetails.NonVATableAmount, mclsSalesTransactionDetails.VATExempt, mclsSalesTransactionDetails.EVAT, mclsSalesTransactionDetails.EVATableAmount, mclsSalesTransactionDetails.NonEVATableAmount, mclsSalesTransactionDetails.LocalTax, CashPayment, ChequePayment, CreditCardPayment, CreditPayment, DebitPayment, RewardPointsPayment, RewardConvertedPayment, PaymentType);

                // Sep 24, 2014 : update back the LastCheckInDate to min date
                Data.Contacts clsContact = new Data.Contacts(mConnection, mTransaction);
                mConnection = clsContact.Connection; mTransaction = clsContact.Transaction;

                clsContact.UpdateLastCheckInDate(mclsSalesTransactionDetails.CustomerID, Constants.C_DATE_MIN_VALUE);

                // Sep 24, 2011      Lemuel E. Aceron
                // Added order slip wherein all punch items will not change sales and inventory
                // a customer named ORDER SLIP should be defined in contacts
                //if (lblCustomer.Text.Trim().ToUpper() != Constants.C_RETAILPLUS_ORDER_SLIP_CUSTOMER)

                // Added May 7, 2011 to Cater Reserved and Commit functionality    
                // !mclsTerminalDetails.ReservedAndCommit
                if (mclsTerminalDetails.IsParkingTerminal)
                {
                    clsEvent.AddEventLn("      adding back the parking slot to inv...", true, mclsSysConfigDetails.WillWriteSystemLog);
                    Data.ProductUnit clsProductUnit = new Data.ProductUnit(mConnection, mTransaction);
                    mConnection = clsProductUnit.Connection; mTransaction = clsProductUnit.Transaction;

                    Data.ProductVariationsMatrix clsProductVariationsMatrix = new Data.ProductVariationsMatrix(mConnection, mTransaction);

                    foreach (System.Data.DataRow dr in ItemDataTable.Rows)
                    {
                        long lProductID = Convert.ToInt64(dr["ProductID"]);
                        long lVariationsMatrixID = Convert.ToInt64(dr["VariationsMatrixID"]);
                        int iProductUnitID = Convert.ToInt32(dr["ProductUnitID"]);
                        decimal decQuantity = 0;
                        decimal decPackageQuantity = 0;
                        decimal decNewQuantity = 0;
                        decimal decPrice = Convert.ToDecimal(dr["Price"]);
                        decimal decPurchasePrice = Convert.ToDecimal(dr["PurchasePrice"]);

                        if ((dr["Quantity"].ToString().IndexOf("RETURN") == -1) && (dr["Quantity"].ToString() != "VOID"))
                        {
                            decQuantity = Convert.ToDecimal(dr["Quantity"]);
                            decPackageQuantity = Convert.ToDecimal(dr["PackageQuantity"]);
                            decNewQuantity = clsProductUnit.GetBaseUnitValue(lProductID, iProductUnitID, decQuantity * decPackageQuantity);

                            clsProduct.AddQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.PARKING_OUT), mclsSalesTransactionDetails.TransactionDate, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);
                        }
                    }
                }
                else if (mclsSalesTransactionDetails.CustomerName.Trim().ToUpper() != Constants.C_RETAILPLUS_ORDER_SLIP_CUSTOMER &&
                    !mclsTerminalDetails.IsParkingTerminal &&
                    mclsSalesTransactionDetails.CustomerDetails.ContactCode.Trim() != mclsSysConfigDetails.OutOfStockCustomerCode) // do not change the inventory if OUT_OF_STOCK_CUSTOMER_CODE
                {
                    Data.ProductUnit clsProductUnit = new Data.ProductUnit(mConnection, mTransaction);
                    mConnection = clsProductUnit.Connection; mTransaction = clsProductUnit.Transaction;

                    Data.ProductVariationsMatrix clsProductVariationsMatrix = new Data.ProductVariationsMatrix(mConnection, mTransaction);
                    
                    Data.SalesTransactionItems clsSalesTransactionItems = new Data.SalesTransactionItems(mConnection, mTransaction);

                    foreach (System.Data.DataRow dr in ItemDataTable.Rows)
                    {
                        long lProductID = Convert.ToInt64(dr["ProductID"]);
                        long lVariationsMatrixID = Convert.ToInt64(dr["VariationsMatrixID"]);
                        int iProductUnitID = Convert.ToInt32(dr["ProductUnitID"]);
                        decimal decQuantity = 0;
                        decimal decPackageQuantity = 0;
                        decimal decNewQuantity = 0;
                        decimal decPrice = Convert.ToDecimal(dr["Price"]);
                        decimal decPurchasePrice = Convert.ToDecimal(dr["PurchasePrice"]);

                        decimal decDiscount = Convert.ToDecimal(dr["Discount"]);
                        decimal decAmount = Convert.ToDecimal(dr["Amount"]);

                        if (dr["Quantity"].ToString().IndexOf("RETURN") != -1)
                        {
                            decQuantity = Convert.ToDecimal(dr["Quantity"].ToString().Replace(" - RETURN", "").Trim());
                            decPackageQuantity = Convert.ToDecimal(dr["PackageQuantity"]);
                            decNewQuantity = clsProductUnit.GetBaseUnitValue(lProductID, iProductUnitID, decQuantity * decPackageQuantity);

                            clsEvent.AddEventLn("      adding return item: prdid-" + lProductID.ToString() + " to inv: qty-" + decNewQuantity.ToString() + "...", true, mclsSysConfigDetails.WillWriteSystemLog);
                            clsProduct.AddQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.ADD_RETURN_ITEM) + " Ter#: " + mclsTerminalDetails.TerminalNo, mclsSalesTransactionDetails.TransactionDate, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);

                            // 28JUn2015 : Add the RefReturnTransactionItemsID to the 
                            Int64 intReturnTransactionItemsID = Convert.ToInt64(dr["ReturnTransactionItemsID"]);
                            Int64 intTransactionItemsID = Convert.ToInt64(dr["TransactionItemsID"]);
                            clsSalesTransactionItems.UpdateRefReturnTransactionItemsID(intReturnTransactionItemsID, intTransactionItemsID);
                        }
                        else if (dr["Quantity"].ToString().IndexOf("DEMO") != -1)
                        {
                            decQuantity = Convert.ToDecimal(dr["Quantity"].ToString().Replace(" - DEMO", "").Trim());
                            decPackageQuantity = Convert.ToDecimal(dr["PackageQuantity"]);
                            decNewQuantity = clsProductUnit.GetBaseUnitValue(lProductID, iProductUnitID, decQuantity * decPackageQuantity);

                            clsEvent.AddEventLn("      subtracting demo item: prdid-" + lProductID.ToString() + " from inv/rsrvd: qty-" + decNewQuantity.ToString() + "...", true, mclsSysConfigDetails.WillWriteSystemLog);
                            clsProduct.SubtractQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.DEDUCT_DEMO_RETAIL) + " @ " + (decAmount / decNewQuantity).ToString("#,##0.#0") + " Buying: " + decPurchasePrice.ToString("#,##0.#0") + " Orig Selling: " + decPrice.ToString("#,##0.#0") + " Discount: " + (decPrice - (decAmount / decNewQuantity)).ToString("#,##0.#0") + " to " + mclsSalesTransactionDetails.CustomerName + " Ter#: " + mclsTerminalDetails.TerminalNo, DateTime.Now, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);
                            clsProduct.SubtractReservedQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.DEDUCT_DEMO_RETAIL) + " @ " + (decAmount / decNewQuantity).ToString("#,##0.#0") + " Buying: " + decPurchasePrice.ToString("#,##0.#0") + " Orig Selling: " + decPrice.ToString("#,##0.#0") + " Discount: " + (decPrice - (decAmount / decNewQuantity)).ToString("#,##0.#0") + " to " + mclsSalesTransactionDetails.CustomerName + " Ter#: " + mclsTerminalDetails.TerminalNo, DateTime.Now, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);
                        }
                        else if (dr["Quantity"].ToString() != "VOID")
                        {
                            decQuantity = Convert.ToDecimal(dr["Quantity"]);
                            decPackageQuantity = Convert.ToDecimal(dr["PackageQuantity"]);
                            decNewQuantity = clsProductUnit.GetBaseUnitValue(lProductID, iProductUnitID, decQuantity * decPackageQuantity);

                            clsEvent.AddEventLn("      subtracting sold item: prdid-" + lProductID.ToString() + " from inv/rsrvd: qty-" + decNewQuantity.ToString() + "...", true, mclsSysConfigDetails.WillWriteSystemLog);
                            clsProduct.SubtractQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.DEDUCT_SOLD_RETAIL) + " @ " + (decAmount / decNewQuantity).ToString("#,##0.#0") + " Buying: " + decPurchasePrice.ToString("#,##0.#0") + " Orig Selling: " + decPrice.ToString("#,##0.#0") + " Discount: " + (decPrice - (decAmount / decNewQuantity)).ToString("#,##0.#0") + " to " + mclsSalesTransactionDetails.CustomerName + " Ter#: " + mclsTerminalDetails.TerminalNo, DateTime.Now, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);
                            clsProduct.SubtractReservedQuantity(mclsTerminalDetails.BranchID, lProductID, lVariationsMatrixID, decNewQuantity, Data.Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(Data.PRODUCT_INVENTORY_MOVEMENT.DEDUCT_SOLD_RETAIL) + " @ " + (decAmount / decNewQuantity).ToString("#,##0.#0") + " Buying: " + decPurchasePrice.ToString("#,##0.#0") + " Orig Selling: " + decPrice.ToString("#,##0.#0") + " Discount: " + (decPrice - (decAmount / decNewQuantity)).ToString("#,##0.#0") + " to " + mclsSalesTransactionDetails.CustomerName + " Ter#: " + mclsTerminalDetails.TerminalNo, DateTime.Now, mclsSalesTransactionDetails.TransactionNo, mclsSalesTransactionDetails.CashierName);
                        }
                    }
                }

                // Nov 1, 2011 : Lemu - disabled reward points if product is exempted 
                clsEvent.AddEventLn("      checking if rewards is enabled...", true, mclsSysConfigDetails.WillWriteSystemLog);
                if (mclsSalesTransactionDetails.RewardCardActive && mclsTerminalDetails.RewardPointsDetails.EnableRewardPoints)
                {
                    // Note: mTopItemDetails is already set in the MainWnd.CloseTransaction()
                    if (mTopItemDetails.BarCode == Data.Products.DEFAULT_CREDIT_PAYMENT_BARCODE ||
                        mTopItemDetails.BarCode == Data.Products.DEFAULT_ADVANTAGE_CARD_MEMBERSHIP_FEE_BARCODE ||
                        mTopItemDetails.BarCode == Data.Products.DEFAULT_ADVANTAGE_CARD_RENEWAL_FEE_BARCODE ||
                        mTopItemDetails.BarCode == Data.Products.DEFAULT_ADVANTAGE_CARD_REPLACEMENT_FEE_BARCODE ||
                        mTopItemDetails.BarCode == Data.Products.DEFAULT_CREDIT_CARD_MEMBERSHIP_FEE_BARCODE ||
                        mTopItemDetails.BarCode == Data.Products.DEFAULT_CREDIT_CARD_RENEWAL_FEE_BARCODE ||
                        mTopItemDetails.BarCode == Data.Products.DEFAULT_SUPER_CARD_MEMBERSHIP_FEE_BARCODE ||
                        mTopItemDetails.BarCode == Data.Products.DEFAULT_SUPER_CARD_RENEWAL_FEE_BARCODE ||
                        mTopItemDetails.BarCode == Data.Products.DEFAULT_SUPER_CARD_REPLACEMENT_FEE_BARCODE)
                        {
                            mclsTerminalDetails.RewardPointsDetails.EnableRewardPoints = false;
                        }
                }

                #endregion
            }

            // Oct 23, 2011 : Lemu - Added Reward Points
            #region Reward Payment

            if (mclsSalesTransactionDetails.RewardPointsPayment != 0)
            {
                clsEvent.AddEventLn("      deducting rewards payment...", true, mclsSysConfigDetails.WillWriteSystemLog);
                // this should comes before earning of points otherwise this will be wrong.
                Data.ContactReward clsContactReward = new Data.ContactReward(mConnection, mTransaction);
                mConnection = clsContactReward.Connection; mTransaction = clsContactReward.Transaction;

                clsContactReward.DeductPoints(mclsSalesTransactionDetails.CustomerID, mclsSalesTransactionDetails.RewardPointsPayment);
                string strReason = "Redeemed " + mclsSalesTransactionDetails.RewardPointsPayment + " using Reward Card #: " + mclsSalesTransactionDetails.RewardCardNo;
                clsContactReward.AddMovement(mclsSalesTransactionDetails.CustomerID, mclsSalesTransactionDetails.TransactionDate, mclsSalesTransactionDetails.RewardCurrentPoints, -mclsSalesTransactionDetails.RewardPointsPayment, mclsSalesTransactionDetails.RewardCurrentPoints - mclsSalesTransactionDetails.RewardPointsPayment, mclsSalesTransactionDetails.RewardCardExpiry, strReason, mclsTerminalDetails.TerminalNo, mclsSalesTransactionDetails.CashierName, mclsSalesTransactionDetails.TransactionNo);

                mclsSalesTransactionDetails.RewardPreviousPoints = mclsSalesTransactionDetails.RewardCurrentPoints;
                mclsSalesTransactionDetails.RewardCurrentPoints -= mclsSalesTransactionDetails.RewardPointsPayment;
                mclsSalesTransactionDetails.RewardEarnedPoints = 0;

                clsEvent.AddEventLn("      printing rewards slip...", true, mclsSysConfigDetails.WillWriteSystemLog);
                PrintRewardsRedemptionSlip();

                // do this after printing so that it will be equal
                mclsSalesTransactionDetails.RewardPreviousPoints = mclsSalesTransactionDetails.RewardCurrentPoints;
            }
            #endregion

            #region Add reward points to customer

            if (mclsTerminalDetails.RewardPointsDetails.EnableRewardPoints && 
                mclsSalesTransactionDetails.RewardCardActive && 
                mclsSalesTransactionDetails.RewardsCustomerID != Constants.C_RETAILPLUS_CUSTOMERID &&
                mclsTerminalDetails.RewardPointsDetails.RewardPointsMinimum <= mclsSalesTransactionDetails.AmountDue)
            {
                decimal decRewardPoints = 0;
                try
                {
                    decRewardPoints = (mclsSalesTransactionDetails.AmountDue - mclsSalesTransactionDetails.RewardConvertedPayment) / mclsTerminalDetails.RewardPointsDetails.RewardPointsEvery * mclsTerminalDetails.RewardPointsDetails.RewardPoints;

                    // round down points if RoundDown is enabled
                    if (mclsTerminalDetails.RewardPointsDetails.RoundDownRewardPoints) decRewardPoints = decimal.Floor(decRewardPoints);

                    //Data.Product clsProduct
                    //long lngProductID = 0;
                    foreach (System.Data.DataRow dr in ItemDataTable.Rows)
                    {
                        //lngProductID = decimal.Parse(dr["RewardPoints"].ToString());
                        //decRewardPoints += clsProduct.Details1(mclsTerminalDetails.BranchID, lngProductID).RewardPoints;
                        decRewardPoints += decimal.Parse(dr["RewardPoints"].ToString());
                    }
                }
                catch { }

                mclsSalesTransactionDetails.RewardEarnedPoints = decRewardPoints;
                mclsSalesTransactionDetails.RewardCurrentPoints = mclsSalesTransactionDetails.RewardPreviousPoints + mclsSalesTransactionDetails.RewardEarnedPoints;

                Data.ContactReward clsContactReward = new Data.ContactReward(mConnection, mTransaction);
                mConnection = clsContactReward.Connection; mTransaction = clsContactReward.Transaction;
                
                clsContactReward.AddPoints(mclsSalesTransactionDetails.RewardsCustomerID, mclsSalesTransactionDetails.RewardEarnedPoints);
                clsContactReward.AddPurchase(mclsSalesTransactionDetails.RewardsCustomerID, mclsSalesTransactionDetails.AmountDue);
                string strReason = "Purchase " + mclsSalesTransactionDetails.AmountDue.ToString("#,##0.#0") + " using Reward Card #: " + mclsSalesTransactionDetails.RewardCardNo;
                clsContactReward.AddMovement(mclsSalesTransactionDetails.RewardsCustomerID, mclsSalesTransactionDetails.TransactionDate, mclsSalesTransactionDetails.RewardPreviousPoints, mclsSalesTransactionDetails.RewardEarnedPoints, mclsSalesTransactionDetails.RewardCurrentPoints, mclsSalesTransactionDetails.RewardCardExpiry, strReason, mclsTerminalDetails.TerminalNo, mclsSalesTransactionDetails.CashierName, mclsSalesTransactionDetails.TransactionNo);
            }
            #endregion

            #region  3Nov2014 : autoamtically deposit the change if credit payment
            //#region Auto deposit for creditpayment
            //if (clsTransactionStatus == TransactionStatus.CreditPayment && mclsSysConfigDetails.WillDepositChangeOfCreditPayment)
            //{
            //    InsertAuditLog(AccessTypes.Deposit, "Auto depositing change of trx #: " + mclsSalesTransactionDetails.TransactionNo + ".");
            //    clsEvent.AddEventLn("Auto depositing change of trx #: " + mclsSalesTransactionDetails.TransactionNo + ".", true);

            //    Data.Deposits clsDeposit = new Data.Deposits(mConnection, mTransaction);
            //    mConnection = clsDeposit.Connection; mTransaction = clsDeposit.Transaction;

            //    Data.DepositDetails clsDepositDetails = new Data.DepositDetails()
            //    {
            //        BranchDetails = mclsTerminalDetails.BranchDetails,
            //        TerminalNo = mclsTerminalDetails.TerminalNo,
            //        Amount = mclsSalesTransactionDetails.ChangeAmount,
            //        PaymentType = mclsSalesTransactionDetails.PaymentType,
            //        DateCreated = DateTime.Now,
            //        CashierID = mclsSalesTransactionDetails.CashierID,
            //        CashierName = mclsSalesTransactionDetails.CashierName,
            //        ContactID = mclsSalesTransactionDetails.CustomerDetails.ContactID,
            //        ContactName = mclsSalesTransactionDetails.CustomerDetails.ContactName,
            //        Remarks = "Auto deposit from trx #: " + mclsSalesTransactionDetails.TransactionNo + ".",
            //        CreatedOn = DateTime.Now,
            //        LastModified = DateTime.Now
            //    };
            //    clsDeposit.Insert(clsDepositDetails);

            //    clsContact = new Data.Contacts(mConnection, mTransaction);
            //    mConnection = clsContact.Connection; mTransaction = clsContact.Transaction;

            //    clsContact.AddDebit(clsDepositDetails.ContactID, clsDepositDetails.Amount);
            //    clsDeposit.CommitAndDispose();

            //    InsertAuditLog(AccessTypes.Deposit, "Deposit: type='" + clsDepositDetails.PaymentType.ToString("G") + "' amount='" + clsDepositDetails.Amount.ToString(",##0.#0") + "'" + " @ Branch: " + mclsTerminalDetails.BranchDetails.BranchCode);
            //    clsEvent.AddEventLn("Done! type=" + clsDepositDetails.PaymentType.ToString("G") + " amount=" + clsDepositDetails.Amount.ToString("#,###.#0"), true);
            //}
            //#endregion
            #endregion

            // 22Nov2014 : remove the merge tables when closed or void
            if (mclsTerminalDetails.WithRestaurantFeatures)
                RemoveFromMergeTable(mclsSalesTransactionDetails.CustomerDetails.ContactCode);

            // commit the transactions here.
            // in case error s encoutered n printing. transaction is already committed.
            clsEvent.AddEventLn("      commiting transaction to database...", true, mclsSysConfigDetails.WillWriteSystemLog);
            clsSalesTransactions.CommitAndDispose();


            /***
             * print the transaction
             ***/
            #region printing
            if (mclsTerminalDetails.AutoPrint != PrintingPreference.AskFirst)
            {
                try
                {
                    if (mclsSalesTransactionDetails.isConsignment)
                    {
                        // 18Feb2015 : Print DR only if the transaction is consignment
                        clsEvent.AddEventLn("      printing delivery receipt as consginment...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        PrintDeliveryReceipt();
                    }
                    else if (mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.OutOfStockCustomerCode &&
                        (mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoice ||
                         mclsTerminalDetails.ReceiptType == TerminalReceiptType.DeliveryReceipt ||
                        mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoiceAndDR))
                    {
                        clsEvent.AddEventLn("      printing out of stock orders...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        PrintOSReceipt();
                    }
                    else if (mclsSalesTransactionDetails.CustomerDetails.ContactCode == mclsSysConfigDetails.WalkInCustomerCode &&
                    (mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoice ||
                     mclsTerminalDetails.ReceiptType == TerminalReceiptType.DeliveryReceipt ||
                    mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoiceAndDR))
                    {
                        clsEvent.AddEventLn("      printing walk-in customer quote form...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        PrintWalkInReceipt();
                    }
                    else if (mclsSalesTransactionDetails.TransactionStatus == TransactionStatus.CreditPayment &&
                        mclsSysConfigDetails.CreditPaymentType == CreditPaymentType.Houseware)
                    {
                        // do another report for credit payment if HP
                        PrintCreditPayment();

                        // do this twice as per request of CN trader's and CS
                        PrintCreditPayment();
                    }
                    else if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoice)
                    {
                        clsEvent.AddEventLn("      printing sales invoice...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        PrintSalesInvoice();
                    }
                    else if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.DeliveryReceipt)
                    {
                        clsEvent.AddEventLn("      printing delivery receipt...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        PrintDeliveryReceipt();
                    }
                    else if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoiceAndDR)
                    {
                        clsEvent.AddEventLn("      printing sales invoice & delivery receipt...", true, mclsSysConfigDetails.WillWriteSystemLog);

                        if (mclsSalesTransactionDetails.TransactionStatus != TransactionStatus.Void)
                        {
                            clsEvent.AddEventLn("      will not print sales invoice. trx is void...", true, mclsSysConfigDetails.WillWriteSystemLog);
                            PrintSalesInvoice();
                        }
                        PrintDeliveryReceipt();
                    }
                    // 10Feb2010 : print sales invoice to LX as required by Wireless Link
                    else if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoiceForLX300Printer)
                    {
                        clsEvent.AddEventLn("      printing sales invoice for LX300...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        PrintSalesInvoiceToLX(TerminalReceiptType.SalesInvoiceForLX300Printer);
                    }
                    //Added May 11, 2010
                    else if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoiceOrDR)
                    {
                        clsEvent.AddEventLn("      printing sales invoice or OR...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        if (mclsSalesTransactionDetails.CashPayment != 0 || mclsSalesTransactionDetails.CreditCardPayment != 0)
                            PrintSalesInvoice();
                        if (mclsSalesTransactionDetails.ChequePayment != 0 || mclsSalesTransactionDetails.CreditPayment != 0)
                            PrintDeliveryReceipt();
                    }
                    //Added January 17, 2011
                    else if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoiceForLX300PlusPrinter)
                    {
                        clsEvent.AddEventLn("      printing sales invoice for LX300 Plus...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        PrintSalesInvoiceToLX(TerminalReceiptType.SalesInvoiceForLX300PlusPrinter);
                    }
                    //Added February 22, 2011
                    else if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.SalesInvoiceForLX300PlusAmazon)
                    {
                        clsEvent.AddEventLn("      printing sales invoice for LX300 Plus Amazon...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        PrintSalesInvoiceToLX(TerminalReceiptType.SalesInvoiceForLX300PlusAmazon);
                    }
                    else if (!mboIsRefund) // do not print if refund coz its already printed above
                    {
                        // Sep 14, 2014 Control printing in mclsFilePrinter.Write
                        //if (mclsTerminalDetails.AutoPrint == PrintingPreference.Normal)	//print items if not yet printed
                        //{
                        clsEvent.AddEventLn("      printing items to POS printer...", true, mclsSysConfigDetails.WillWriteSystemLog);
                        foreach (System.Data.DataRow dr in ItemDataTable.Rows)
                        {
                            string stItemNo = "" + dr["ItemNo"].ToString();
                            string stProductUnitCode = "" + dr["ProductUnitCode"].ToString();
                            decimal decPrice = Convert.ToDecimal(dr["Price"]);
                            decimal decDiscount = Convert.ToDecimal(dr["Discount"]);
                            decimal decAmount = Convert.ToDecimal(dr["Amount"]);
                            decimal decVAT = Convert.ToDecimal(dr["VAT"]);
                            decimal decEVAT = Convert.ToDecimal(dr["EVAT"]);
                            decimal decPromoApplied = Convert.ToDecimal(dr["PromoApplied"]);
                            string stProductCode = "" + dr["ProductCode"].ToString();
                            if (dr["MatrixDescription"].ToString() != string.Empty && dr["MatrixDescription"].ToString() != null) stProductCode += "-" + dr["MatrixDescription"].ToString();
                            decimal decQuantity = 0;
                            string stDiscountCode = "" + dr["DiscountCode"].ToString();
                            DiscountTypes ItemDiscountType = (DiscountTypes)Enum.Parse(typeof(DiscountTypes), dr["ItemDiscountType"].ToString());

                            if (dr["Quantity"].ToString().IndexOf("RETURN") != -1)
                            {
                                stProductCode = "" + dr["ProductCode"].ToString() + "-RET";
                                decQuantity = Convert.ToDecimal(dr["Quantity"].ToString().Replace(" - RETURN", "").Trim());
                                decAmount = -decAmount;
                            }
                            else if (dr["Quantity"].ToString().IndexOf("DEMO") != -1)
                            {
                                stProductCode = "" + dr["ProductCode"].ToString() + "-DEMO";
                                decQuantity = Convert.ToDecimal(dr["Quantity"].ToString().Replace(" - DEMO", "").Trim());
                                decAmount = 0;
                            }
                            else if (dr["Quantity"].ToString() != "VOID")
                            {
                                decQuantity = Convert.ToDecimal(dr["Quantity"]);
                            }

                            if (dr["Quantity"].ToString().IndexOf("VOID") != -1)
                            {
                                if (mclsTerminalDetails.WillPrintVoidItem)
                                    if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.Default)
                                        PrintItem(stItemNo, stProductCode, stProductUnitCode, decQuantity, decPrice, decDiscount, decPromoApplied, decAmount, decVAT, decEVAT, stDiscountCode, ItemDiscountType);
                            }
                            else
                            {
                                if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.Default)
                                    PrintItem(stItemNo, stProductCode, stProductUnitCode, decQuantity, decPrice, decDiscount, decPromoApplied, decAmount, decVAT, decEVAT, stDiscountCode, ItemDiscountType);
                            }

                        }
                        if (mclsTerminalDetails.ReceiptType == TerminalReceiptType.Default)
                        {
                            PrintReportFooterSection(true, clsTransactionStatus, mclsSalesTransactionDetails.ItemSold, mclsSalesTransactionDetails.QuantitySold, mclsSalesTransactionDetails.SubTotal, mclsSalesTransactionDetails.Discount, mclsSalesTransactionDetails.Charge, mclsSalesTransactionDetails.AmountPaid, CashPayment, ChequePayment, CreditCardPayment, CreditPayment, DebitPayment, RewardPointsPayment, RewardConvertedPayment, ChangeAmount, arrChequePaymentDetails, arrCreditCardPaymentDetails, arrCreditPaymentDetails, arrDebitPaymentDetails);

                            // print the charge slip if not refund and will print
                            if (mclsTerminalDetails.WillPrintChargeSlip && !mboIsRefund)
                            {
                                clsEvent.AddEventLn("      printing charge slip...", true, mclsSysConfigDetails.WillWriteSystemLog);

                                // Nov 05, 2011 : Print Charge Slip
                                PrintChargeSlip(ChargeSlipType.Customer);
                                PrintChargeSlip(ChargeSlipType.Original);

                                if (mclsTerminalDetails.IncludeCreditChargeAgreement && mclsSalesTransactionDetails.CustomerDetails.CreditDetails.CardTypeDetails.WithGuarantor)
                                //if (!mclsTerminalDetails.IncludeCreditChargeAgreement) //do not print the guarantor if there is no agreement printed
                                {
                                    PrintChargeSlip(ChargeSlipType.Guarantor);
                                }
                            }
                        }
                        //}
                        // Sep 14, 2014 Control printing in mclsFilePrinter.Write
                    }
                }
                catch (Exception ex)
                {
                    clsEvent.AddErrorEventLn(ex);
                    clsEvent.AddEventLn("Error printing transaction no: " + mclsSalesTransactionDetails.TransactionNo + ". Already commited in the database.", true);
                }
            }
            #endregion

            // print all the coupon's
            #region Printing of coupons

            // print coupon's only for customer's with reward cards as per request of Sir Guy,
            // sa susunod ilalagay na sya sa selection
            if (mclsContactDetails.ContactID != Constants.C_RETAILPLUS_CUSTOMERID &&
                mclsContactDetails.RewardDetails.ContactID != 0)
            {
                Data.PromoBySupplier clsPromoBySupplier = new Data.PromoBySupplier(mConnection, mTransaction);
                mConnection = clsPromoBySupplier.Connection; mTransaction = clsPromoBySupplier.Transaction;

                try
                {
                    clsEvent.AddEventLn("Printing coupon if necessary...", true);

                    Data.PromoBySupplierDetails clsPromoBySupplierDetails = clsPromoBySupplier.getPromoBySupplierID();
                    if (clsPromoBySupplierDetails.PromoBySupplierID != 0)
                    {
                        Data.PromoBySupplierItems clsPromoBySupplierItems = new Data.PromoBySupplierItems(mConnection, mTransaction);
                        mConnection = clsPromoBySupplierItems.Connection; mTransaction = clsPromoBySupplierItems.Transaction;

                        Data.ProductGroup clsProductGroup = new Data.ProductGroup(mConnection, mTransaction);
                        mConnection = clsProductGroup.Connection; mTransaction = clsProductGroup.Transaction;

                        Data.ProductSubGroup clsProductSubGroup = new Data.ProductSubGroup(mConnection, mTransaction);
                        mConnection = clsProductSubGroup.Connection; mTransaction = clsProductSubGroup.Transaction;

                        decimal decPromoBySupplierValue = -1;
                        decimal decItemAmount = 0;
                        string strCouponRemarks = "";
                        var lstItems = mclsSalesTransactionDetails.TransactionItemsList;

                        Int64 ProductGroupID = 0, ProductSubGroupID = 0;
                        switch (clsPromoBySupplierDetails.PromoLevel)
                        {
                            case PromoLevel.All:
                                lstItems = mclsSalesTransactionDetails.TransactionItemsList;
                                decPromoBySupplierValue = -1;
                                decItemAmount = mclsSalesTransactionDetails.TransactionItemsList.Sum(x => x.Amount);
                                if (clsPromoBySupplierItems.ApplyPromoBySupplierValue(clsPromoBySupplierDetails, 0, 0, 0, 0, 0, out decPromoBySupplierValue, out strCouponRemarks, mclsSalesTransactionDetails.BranchID))
                                {
                                    if (decPromoBySupplierValue == 0 || decPromoBySupplierValue <= decItemAmount)
                                    {
                                        //print it
                                        PrintCoupon(lstItems[0], strCouponRemarks);
                                    }
                                }

                                break;
                            case PromoLevel.Contact:
                                lstItems = mclsSalesTransactionDetails.TransactionItemsList
                                    .GroupBy(x => new { x.SupplierID, x.SupplierName })
                                    .Select(data => new Data.SalesTransactionItemDetails() { SupplierID = data.Key.SupplierID, SupplierName = data.Key.SupplierName, ProductGroup = "All", ProductSubGroup = "All", Amount = data.Sum(x => x.Amount) }).ToList();

                                foreach (Data.SalesTransactionItemDetails item in lstItems)
                                {
                                    decPromoBySupplierValue = -1;
                                    decItemAmount = item.Amount;
                                    if (clsPromoBySupplierItems.ApplyPromoBySupplierValue(clsPromoBySupplierDetails, item.SupplierID, 0, 0, 0, 0, out decPromoBySupplierValue, out strCouponRemarks, mclsSalesTransactionDetails.BranchID))
                                    {
                                        if (decPromoBySupplierValue == 0 || decPromoBySupplierValue <= decItemAmount)
                                        {
                                            //print it
                                            PrintCoupon(item, strCouponRemarks);
                                        }
                                    }
                                }
                                break;
                            case PromoLevel.ProductGroup:
                                lstItems = mclsSalesTransactionDetails.TransactionItemsList
                                    .GroupBy(x => new { x.SupplierID, x.SupplierName, x.ProductGroup })
                                    .Select(data => new Data.SalesTransactionItemDetails() { SupplierID = data.Key.SupplierID, SupplierName = data.Key.SupplierName, ProductGroup = data.Key.ProductGroup, ProductSubGroup = "All", Amount = data.Sum(x => x.Amount) }).ToList();

                                foreach (Data.SalesTransactionItemDetails item in lstItems)
                                {
                                    decPromoBySupplierValue = -1;
                                    decItemAmount = item.Amount;
                                    ProductGroupID = clsProductGroup.DetailsByName(item.ProductGroup).ProductGroupID;

                                    if (clsPromoBySupplierItems.ApplyPromoBySupplierValue(clsPromoBySupplierDetails, item.SupplierID, ProductGroupID, 0, 0, 0, out decPromoBySupplierValue, out strCouponRemarks, mclsSalesTransactionDetails.BranchID))
                                    {
                                        if (decPromoBySupplierValue == 0 || decPromoBySupplierValue <= decItemAmount)
                                        {
                                            //print it
                                            PrintCoupon(item, strCouponRemarks);
                                        }
                                    }
                                }
                                break;
                            case PromoLevel.ProductSubGroup:
                                lstItems = mclsSalesTransactionDetails.TransactionItemsList
                                    .GroupBy(x => new { x.SupplierID, x.SupplierName, x.ProductGroup, x.ProductSubGroup })
                                    .Select(data => new Data.SalesTransactionItemDetails() { SupplierID = data.Key.SupplierID, SupplierName = data.Key.SupplierName, ProductGroup = data.Key.ProductGroup, ProductSubGroup = data.Key.ProductSubGroup, Amount = data.Sum(x => x.Amount) }).ToList();

                                foreach (Data.SalesTransactionItemDetails item in lstItems)
                                {
                                    decPromoBySupplierValue = -1;
                                    decItemAmount = item.Amount;

                                    ProductGroupID = clsProductGroup.DetailsByName(item.ProductGroup).ProductGroupID;
                                    ProductSubGroupID = clsProductSubGroup.DetailsByName(item.ProductSubGroup).ProductSubGroupID;
                                    if (clsPromoBySupplierItems.ApplyPromoBySupplierValue(clsPromoBySupplierDetails, item.SupplierID, ProductGroupID, ProductSubGroupID, 0, 0, out decPromoBySupplierValue, out strCouponRemarks, mclsSalesTransactionDetails.BranchID))
                                    {
                                        if (decPromoBySupplierValue == 0 || decPromoBySupplierValue <= decItemAmount)
                                        {
                                            //print it
                                            PrintCoupon(item, strCouponRemarks);
                                        }
                                    }
                                }
                                break;
                            case PromoLevel.Product:
                                lstItems = mclsSalesTransactionDetails.TransactionItemsList
                                    .GroupBy(x => new { x.SupplierID, x.SupplierName, x.ProductGroup, x.ProductSubGroup, x.ProductID })
                                    .Select(data => new Data.SalesTransactionItemDetails() { SupplierID = data.Key.SupplierID, SupplierName = data.Key.SupplierName, ProductGroup = data.Key.ProductGroup, ProductSubGroup = data.Key.ProductSubGroup, ProductID = data.Key.ProductID, Amount = data.Sum(x => x.Amount) }).ToList();

                                foreach (Data.SalesTransactionItemDetails item in lstItems)
                                {
                                    decPromoBySupplierValue = -1;
                                    decItemAmount = item.Amount;

                                    ProductGroupID = clsProductGroup.DetailsByName(item.ProductGroup).ProductGroupID;
                                    ProductSubGroupID = clsProductSubGroup.DetailsByName(item.ProductSubGroup).ProductSubGroupID;
                                    if (clsPromoBySupplierItems.ApplyPromoBySupplierValue(clsPromoBySupplierDetails, item.SupplierID, ProductGroupID, ProductSubGroupID, item.ProductID, 0, out decPromoBySupplierValue, out strCouponRemarks, mclsSalesTransactionDetails.BranchID))
                                    {
                                        if (decPromoBySupplierValue == 0 || decPromoBySupplierValue <= decItemAmount)
                                        {
                                            //print it
                                            PrintCoupon(item, strCouponRemarks);
                                        }
                                    }
                                }
                                break;
                            case PromoLevel.Variation:
                                lstItems = mclsSalesTransactionDetails.TransactionItemsList
                                    .GroupBy(x => new { x.SupplierID, x.SupplierName, x.ProductGroup, x.ProductSubGroup, x.ProductID, x.VariationsMatrixID })
                                    .Select(data => new Data.SalesTransactionItemDetails() { SupplierID = data.Key.SupplierID, SupplierName = data.Key.SupplierName, ProductGroup = data.Key.ProductGroup, ProductSubGroup = data.Key.ProductSubGroup, ProductID = data.Key.ProductID, VariationsMatrixID = data.Key.VariationsMatrixID, Amount = data.Sum(x => x.Amount) }).ToList();

                                foreach (Data.SalesTransactionItemDetails item in lstItems)
                                {
                                    decPromoBySupplierValue = -1;
                                    decItemAmount = item.Amount;

                                    ProductGroupID = clsProductGroup.DetailsByName(item.ProductGroup).ProductGroupID;
                                    ProductSubGroupID = clsProductSubGroup.DetailsByName(item.ProductSubGroup).ProductSubGroupID;
                                    if (clsPromoBySupplierItems.ApplyPromoBySupplierValue(clsPromoBySupplierDetails, item.SupplierID, ProductGroupID, ProductSubGroupID, item.ProductID, item.VariationsMatrixID, out decPromoBySupplierValue, out strCouponRemarks, mclsSalesTransactionDetails.BranchID))
                                    {
                                        if (decPromoBySupplierValue == 0 || decPromoBySupplierValue <= decItemAmount)
                                        {
                                            //print it
                                            PrintCoupon(item, strCouponRemarks);
                                        }
                                    }
                                }
                                break;
                        }

                    }
                    clsEvent.AddEventLn("done...", true);
                }
                catch (Exception ex)
                {
                    clsEvent.AddEventLn("Error printing coupon", true);
                    clsEvent.AddErrorEventLn(ex);
                }
                finally
                {
                    clsPromoBySupplier.CommitAndDispose();
                }

            }

            #endregion

            InsertAuditLog(AccessTypes.CloseTransaction, "Close transaction #: " + mclsSalesTransactionDetails.TransactionNo + "... Subtotal: " + mclsSalesTransactionDetails.SubTotal.ToString("#,###.#0") + " Discount: " + mclsSalesTransactionDetails.Discount.ToString("#,###.#0") + " AmountPaid: " + mclsSalesTransactionDetails.AmountPaid.ToString("#,###.#0") + " CashPayment: " + CashPayment.ToString("#,###.#0") + " ChequePayment: " + ChequePayment.ToString("#,###.#0") + " CreditCardPayment: " + CreditCardPayment + " CreditPayment: " + CreditPayment.ToString("#,###.#0") + " DebitPayment: " + DebitPayment.ToString("#,###.#0") + " ChangeAmount: " + ChangeAmount.ToString("#,###.#0") + " @ Branch: " + mclsTerminalDetails.BranchDetails.BranchCode);

            clsEvent.AddEventLn("Done! Transaction no. " + mclsSalesTransactionDetails.TransactionNo + " has been closed. Subtotal: " + mclsSalesTransactionDetails.SubTotal.ToString("#,###.#0") + " Discount: " + mclsSalesTransactionDetails.Discount.ToString("#,###.#0") + " AmountPaid: " + mclsSalesTransactionDetails.AmountPaid.ToString("#,###.#0") + " CashPayment: " + CashPayment.ToString("#,###.#0") + " ChequePayment: " + ChequePayment.ToString("#,###.#0") + " CreditCardPayment: " + CreditCardPayment + " CreditPayment: " + CreditPayment.ToString("#,###.#0") + " DebitPayment: " + DebitPayment.ToString("#,###.#0") + " ChangeAmount: " + ChangeAmount.ToString("#,###.#0"), true);

        }
Beispiel #3
0
        private void Import()
        {
            if (txtPath.HasFile)
            {
                string fn = System.IO.Path.GetFileName(txtPath.PostedFile.FileName);

                if (fn.Contains("_" + Constants.PURCHASE_ORDER_CODE) == false)
                {
                    string stScript = "<Script>";
                    stScript += "window.alert('Please select a VALID Transfer In file to upload.')";
                    stScript += "</Script>";
                    Response.Write(stScript);
                    return;
                }

                string SaveLocation = "/RetailPlus/temp/uploaded_" + fn;

                txtPath.PostedFile.SaveAs(SaveLocation);
                XmlTextReader xmlReader = new XmlTextReader(SaveLocation);
                xmlReader.WhitespaceHandling = WhitespaceHandling.None;

                TransferOut clsTransferOut = new TransferOut();
                clsTransferOut.GetConnection();
                TransferOutDetails clsTransferOutDetails = new TransferOutDetails();

                TransferOutItem clsTransferOutItem = new TransferOutItem(clsTransferOut.Connection, clsTransferOut.Transaction);
                TransferOutItemDetails clsTransferOutItemDetails;

                Contacts clsContact = new Contacts(clsTransferOut.Connection, clsTransferOut.Transaction);
                ContactDetails clsContactDetails;

                ContactGroups clsContactGroup = new ContactGroups(clsTransferOut.Connection, clsTransferOut.Transaction);
                ContactGroupDetails clsContactGroupDetails;

                Data.Unit clsUnit = new Data.Unit(clsTransferOut.Connection, clsTransferOut.Transaction);
                UnitDetails clsUnitDetails;

                ProductGroup clsProductGroup = new Data.ProductGroup(clsTransferOut.Connection, clsTransferOut.Transaction);
                ProductGroupDetails clsProductGroupDetails;

                ProductSubGroup clsProductSubGroup = new Data.ProductSubGroup(clsTransferOut.Connection, clsTransferOut.Transaction);
                ProductSubGroupDetails clsProductSubGroupDetails;

                Products clsProduct = new Products(clsTransferOut.Connection, clsTransferOut.Transaction);
                ProductDetails clsProductDetails;

                ProductVariations clsProductVariation = new ProductVariations(clsTransferOut.Connection, clsTransferOut.Transaction);
                ProductVariationDetails clsProductVariationDetails;

                Branch clsBranch = new Branch(clsTransferOut.Connection, clsTransferOut.Transaction);
                BranchDetails clsBranchDetails;

                long lngProductID = 0; long lngProductCtr = 0;

                while (xmlReader.Read())
                {
                    switch (xmlReader.NodeType)
                    {
                        case XmlNodeType.Element:

                            if (xmlReader.Name == "TransferOutDetails")
                            {
                                clsTransferOutDetails.TransferOutNo = lnkTransferOutNo.Text;
                                clsTransferOutDetails.TransferOutDate = DateTime.Parse(lblTransferOutDate.Text);

                                clsTransferOutDetails.SupplierCode = xmlReader.GetAttribute("SupplierCode").ToString();
                                clsTransferOutDetails.SupplierContact = xmlReader.GetAttribute("SupplierContact").ToString();
                                clsTransferOutDetails.SupplierAddress = xmlReader.GetAttribute("SupplierAddress").ToString();
                                clsTransferOutDetails.SupplierTelephoneNo = xmlReader.GetAttribute("SupplierTelephoneNo").ToString();
                                clsTransferOutDetails.SupplierModeOfTerms = int.Parse(xmlReader.GetAttribute("SupplierModeOfTerms").ToString());
                                clsTransferOutDetails.SupplierTerms = int.Parse(xmlReader.GetAttribute("SupplierTerms").ToString());
                                clsTransferOutDetails.SupplierID = clsContact.Details(xmlReader.GetAttribute("SupplierCode").ToString()).ContactID;
                                if (clsTransferOutDetails.SupplierID == 0)
                                {
                                    clsContactDetails = new ContactDetails();
                                    clsContactDetails.ContactCode = clsTransferOutDetails.SupplierCode;
                                    clsContactDetails.ContactName = xmlReader.GetAttribute("SupplierName").ToString();
                                    clsContactDetails.BusinessName = clsTransferOutDetails.SupplierContact;
                                    clsContactDetails.Address = clsTransferOutDetails.SupplierAddress;
                                    clsContactDetails.TelephoneNo = clsTransferOutDetails.SupplierTelephoneNo;
                                    clsContactDetails.ModeOfTerms = (ModeOfTerms)Enum.Parse(typeof(ModeOfTerms), clsTransferOutDetails.SupplierModeOfTerms.ToString());
                                    clsContactDetails.Terms = clsTransferOutDetails.SupplierTerms;
                                    clsContactDetails.Remarks = "Added in from Imported TransferOut #";
                                    clsContactDetails.ContactGroupID = int.Parse(Contacts.DEFAULT_SUPPLIER_ID.ToString("d"));
                                    clsContactDetails.DateCreated = DateTime.Now;
                                    clsTransferOutDetails.SupplierID = clsContact.Insert(clsContactDetails);
                                }
                                clsTransferOutDetails.RequiredDeliveryDate = DateTime.Parse(xmlReader.GetAttribute("RequiredDeliveryDate").ToString());
                                clsTransferOutDetails.BranchID = clsBranch.Details(xmlReader.GetAttribute("BranchCode")).BranchID;
                                if (clsTransferOutDetails.BranchID == 0)
                                {
                                    clsBranchDetails = new BranchDetails();
                                    clsBranchDetails.BranchCode = xmlReader.GetAttribute("BranchCode");
                                    clsBranchDetails.BranchName = xmlReader.GetAttribute("BranchName");
                                    clsBranchDetails.Address = xmlReader.GetAttribute("BranchAddress");
                                    clsBranchDetails.DBIP = xmlReader.GetAttribute("BranchDBIP");
                                    clsBranchDetails.DBPort = xmlReader.GetAttribute("BranchDBPort");
                                    clsBranchDetails.Remarks = xmlReader.GetAttribute("BranchRemarks");
                                    clsTransferOutDetails.BranchID = clsBranch.Insert(clsBranchDetails);
                                }

                                clsTransferOutDetails.TransferrerID = long.Parse(xmlReader.GetAttribute("TransferrerID"));
                                clsTransferOutDetails.TransferrerName = xmlReader.GetAttribute("TransferrerName");

                                clsTransferOutDetails.SubTotal = decimal.Parse(xmlReader.GetAttribute("SubTotal"));
                                clsTransferOutDetails.Discount = decimal.Parse(xmlReader.GetAttribute("Discount"));
                                clsTransferOutDetails.DiscountApplied = decimal.Parse(xmlReader.GetAttribute("DiscountApplied"));
                                clsTransferOutDetails.DiscountType = (DiscountTypes)Enum.Parse(typeof(DiscountTypes), xmlReader.GetAttribute("DiscountType"));
                                clsTransferOutDetails.VAT = decimal.Parse(xmlReader.GetAttribute("VAT"));
                                clsTransferOutDetails.VatableAmount = decimal.Parse(xmlReader.GetAttribute("VatableAmount"));
                                clsTransferOutDetails.EVAT = decimal.Parse(xmlReader.GetAttribute("EVAT"));
                                clsTransferOutDetails.EVatableAmount = decimal.Parse(xmlReader.GetAttribute("EVatableAmount"));
                                clsTransferOutDetails.LocalTax = decimal.Parse(xmlReader.GetAttribute("LocalTax"));
                                clsTransferOutDetails.Freight = decimal.Parse(xmlReader.GetAttribute("Freight"));
                                clsTransferOutDetails.Deposit = decimal.Parse(xmlReader.GetAttribute("Deposit"));
                                clsTransferOutDetails.UnpaidAmount = decimal.Parse(xmlReader.GetAttribute("UnpaidAmount"));
                                clsTransferOutDetails.PaidAmount = decimal.Parse(xmlReader.GetAttribute("PaidAmount"));
                                clsTransferOutDetails.TotalItemDiscount = decimal.Parse(xmlReader.GetAttribute("TotalItemDiscount"));
                                clsTransferOutDetails.Status = (TransferOutStatus)Enum.Parse(typeof(TransferOutStatus), xmlReader.GetAttribute("Status"));
                                clsTransferOutDetails.Remarks = xmlReader.GetAttribute("Remarks");
                                clsTransferOutDetails.SupplierDRNo = xmlReader.GetAttribute("SupplierDRNo");
                                clsTransferOutDetails.DeliveryDate = DateTime.Parse(xmlReader.GetAttribute("DeliveryDate"));
                                clsTransferOutDetails.CancelledDate = DateTime.Parse(xmlReader.GetAttribute("CancelledDate"));
                                clsTransferOutDetails.Remarks = xmlReader.GetAttribute("Remarks");
                                clsTransferOutDetails.CancelledRemarks = xmlReader.GetAttribute("CancelledRemarks");
                                clsTransferOutDetails.CancelledByID = long.Parse(xmlReader.GetAttribute("CancelledByID"));

                                clsTransferOut.Update(clsTransferOutDetails);

                            }
                            else if (xmlReader.Name == "TransferOutItem")
                            {
                                clsTransferOutItemDetails = new TransferOutItemDetails();
                                clsTransferOutItemDetails.TransferOutID = long.Parse(lblTransferOutID.Text);

                                clsTransferOutItemDetails.ProductCode = xmlReader.GetAttribute("ProductCode");
                                clsTransferOutItemDetails.BarCode = xmlReader.GetAttribute("BarCode");
                                clsTransferOutItemDetails.Description = xmlReader.GetAttribute("ProductDesc");
                                clsTransferOutItemDetails.ProductSubGroup = xmlReader.GetAttribute("ItemProductSubGroup");
                                clsTransferOutItemDetails.ProductGroup = xmlReader.GetAttribute("ItemProductGroup");
                                clsTransferOutItemDetails.ProductUnitID = Convert.ToInt32(xmlReader.GetAttribute("ItemProductUnitID"));
                                clsTransferOutItemDetails.ProductUnitCode = xmlReader.GetAttribute("ItemProductUnitCode");
                                clsTransferOutItemDetails.Quantity = Convert.ToDecimal(xmlReader.GetAttribute("ItemQuantity"));
                                clsTransferOutItemDetails.UnitCost = Convert.ToDecimal(xmlReader.GetAttribute("ItemUnitCost"));
                                clsTransferOutItemDetails.Discount = Convert.ToDecimal(xmlReader.GetAttribute("ItemDiscount"));
                                clsTransferOutItemDetails.DiscountApplied = Convert.ToDecimal(xmlReader.GetAttribute("ItemDiscountApplied"));
                                clsTransferOutItemDetails.DiscountType = (DiscountTypes)Enum.Parse(typeof(DiscountTypes), xmlReader.GetAttribute("ItemDiscountType"));
                                clsTransferOutItemDetails.Amount = Convert.ToDecimal(xmlReader.GetAttribute("ItemAmount"));
                                clsTransferOutItemDetails.IsVatable = Convert.ToBoolean(Convert.ToInt16(xmlReader.GetAttribute("ItemIsVatable")));
                                clsTransferOutItemDetails.VatableAmount = Convert.ToDecimal(xmlReader.GetAttribute("ItemVatableAmount"));
                                clsTransferOutItemDetails.EVatableAmount = Convert.ToDecimal(xmlReader.GetAttribute("ItemEVatableAmount"));
                                clsTransferOutItemDetails.LocalTax = Convert.ToDecimal(xmlReader.GetAttribute("ItemLocalTax"));
                                clsTransferOutItemDetails.VAT = Convert.ToDecimal(xmlReader.GetAttribute("ItemVAT"));
                                clsTransferOutItemDetails.EVAT = Convert.ToDecimal(xmlReader.GetAttribute("ItemEVAT"));
                                clsTransferOutItemDetails.LocalTax = Convert.ToDecimal(xmlReader.GetAttribute("ItemLocalTax"));
                                clsTransferOutItemDetails.isVATInclusive = Convert.ToBoolean(Convert.ToInt16(xmlReader.GetAttribute("ItemisVATInclusive")));
                                clsTransferOutItemDetails.IsVatable = Convert.ToBoolean(Convert.ToInt16(xmlReader.GetAttribute("ItemIsVatable")));
                                clsTransferOutItemDetails.TransferOutItemStatus = (TransferOutItemStatus)Enum.Parse(typeof(TransferOutItemStatus), xmlReader.GetAttribute("ItemTransferOutItemStatus"));
                                clsTransferOutItemDetails.VariationMatrixID = Convert.ToInt64(xmlReader.GetAttribute("ItemVariationMatrixID"));
                                clsTransferOutItemDetails.MatrixDescription = xmlReader.GetAttribute("ItemBaseVariationDescription");
                                clsTransferOutItemDetails.ProductGroup = xmlReader.GetAttribute("ProductGroup");
                                clsTransferOutItemDetails.ProductSubGroup = xmlReader.GetAttribute("ProductSubGroup");
                                clsTransferOutItemDetails.Remarks = xmlReader.GetAttribute("ItemRemarks");
                                clsTransferOutItemDetails.SellingPrice = Convert.ToDecimal(xmlReader.GetAttribute("ItemSellingPrice"));
                                clsTransferOutItemDetails.SellingVAT = Convert.ToDecimal(xmlReader.GetAttribute("ItemSellingVAT"));
                                clsTransferOutItemDetails.SellingEVAT = Convert.ToDecimal(xmlReader.GetAttribute("ItemSellingEVAT"));
                                clsTransferOutItemDetails.SellingLocalTax = Convert.ToDecimal(xmlReader.GetAttribute("ItemSellingLocalTax"));
                                clsTransferOutItemDetails.OldSellingPrice = Convert.ToDecimal(xmlReader.GetAttribute("ItemOldSellingPrice"));

                                clsTransferOutItemDetails.ProductID = clsProduct.Details(clsTransferOutItemDetails.BarCode).ProductID;
                                lngProductID = clsTransferOutItemDetails.ProductID;
                                if (clsTransferOutItemDetails.ProductID == 0)
                                {
                                    clsTransferOutItemDetails.ProductID = clsProduct.Details(clsTransferOutItemDetails.ProductCode).ProductID;
                                    if (clsTransferOutItemDetails.ProductID == 0)
                                    {
                                        //insert new product
                                        clsProductDetails = new ProductDetails();
                                        clsProductDetails.BarCode = clsTransferOutItemDetails.BarCode;
                                        clsProductDetails.ProductCode = clsTransferOutItemDetails.ProductCode;
                                        clsProductDetails.ProductDesc = clsTransferOutItemDetails.Description;
                                        clsProductDetails.ProductGroupCode = xmlReader.GetAttribute("ProductGroupCode");
                                        clsProductDetails.ProductGroupName = xmlReader.GetAttribute("ProductGroupName");
                                        clsProductDetails.ProductSubGroupCode = xmlReader.GetAttribute("ProductSubGroupCode");
                                        clsProductDetails.ProductSubGroupName = xmlReader.GetAttribute("ProductSubGroupName");
                                        clsProductDetails.BaseUnitCode = xmlReader.GetAttribute("BaseUnitCode");
                                        clsProductDetails.BaseUnitName = xmlReader.GetAttribute("BaseUnitName");
                                        clsProductDetails.DateCreated = DateTime.Now;
                                        clsProductDetails.Price = Convert.ToDecimal(xmlReader.GetAttribute("Price"));
                                        clsProductDetails.PurchasePrice = Convert.ToDecimal(xmlReader.GetAttribute("PurchasePrice"));
                                        clsProductDetails.IncludeInSubtotalDiscount = Convert.ToBoolean(xmlReader.GetAttribute("IncludeInSubtotalDiscount"));
                                        clsProductDetails.VAT = Convert.ToDecimal(xmlReader.GetAttribute("VAT"));
                                        clsProductDetails.EVAT = Convert.ToDecimal(xmlReader.GetAttribute("EVAT"));
                                        clsProductDetails.LocalTax = Convert.ToDecimal(xmlReader.GetAttribute("LocalTax"));
                                        clsProductDetails.Quantity = 0;
                                        clsProductDetails.MinThreshold = Convert.ToDecimal(xmlReader.GetAttribute("MinThreshold"));
                                        clsProductDetails.MaxThreshold = Convert.ToDecimal(xmlReader.GetAttribute("MaxThreshold"));
                                        clsProductDetails.ChartOfAccountIDPurchase = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDPurchase"));
                                        clsProductDetails.ChartOfAccountIDSold = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDSold"));
                                        clsProductDetails.ChartOfAccountIDInventory = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDInventory"));
                                        clsProductDetails.ChartOfAccountIDTaxPurchase = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDTaxPurchase"));
                                        clsProductDetails.ChartOfAccountIDTaxSold = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDTaxSold"));
                                        clsProductDetails.IsItemSold = Convert.ToBoolean(xmlReader.GetAttribute("IsItemSold"));
                                        clsProductDetails.WillPrintProductComposition = Convert.ToBoolean(xmlReader.GetAttribute("WillPrintProductComposition"));
                                        clsProductDetails.UpdatedBy = long.Parse(xmlReader.GetAttribute("UpdatedBy"));
                                        clsProductDetails.UpdatedOn = Convert.ToDateTime(xmlReader.GetAttribute("UpdatedOn"));
                                        clsProductDetails.PercentageCommision = decimal.Parse(xmlReader.GetAttribute("PercentageCommision"));
                                        clsProductDetails.QuantityIN = clsProductDetails.Quantity;
                                        clsProductDetails.QuantityOUT = 0;

                                        clsProductDetails.SupplierCode = clsTransferOutDetails.SupplierCode;
                                        clsProductDetails.SupplierID = clsContact.Details(clsProductDetails.SupplierCode).ContactID;
                                        if (clsProductDetails.SupplierID == 0)
                                        {
                                            clsContactDetails = new ContactDetails();
                                            clsContactDetails.ContactGroupID = clsContactGroup.Details(int.Parse(ContactGroupCategory.SUPPLIER.ToString("d"))).ContactGroupID;
                                            if (clsContactDetails.ContactGroupID == 0)
                                            {
                                                clsContactGroupDetails = new ContactGroupDetails();
                                                clsContactGroupDetails.ContactGroupCode = xmlReader.GetAttribute("SUP");
                                                clsContactGroupDetails.ContactGroupName = xmlReader.GetAttribute("Default Supplier Group");
                                                clsContactGroupDetails.ContactGroupCategory = ContactGroupCategory.SUPPLIER;
                                                clsContactDetails.ContactGroupID = clsContactGroup.Insert(clsContactGroupDetails);
                                            }

                                            clsContactDetails.ContactCode = clsTransferOutDetails.SupplierCode;
                                            clsContactDetails.ContactName = clsTransferOutDetails.SupplierContact;

                                            clsContactDetails.ModeOfTerms = (ModeOfTerms)Enum.Parse(typeof(ModeOfTerms), clsTransferOutDetails.SupplierModeOfTerms.ToString());
                                            clsContactDetails.Terms = clsTransferOutDetails.SupplierTerms;
                                            clsContactDetails.Address = clsTransferOutDetails.SupplierAddress;
                                            clsContactDetails.BusinessName = clsTransferOutDetails.SupplierContact;
                                            clsContactDetails.TelephoneNo = clsTransferOutDetails.SupplierTelephoneNo;
                                            clsContactDetails.Remarks = "Added in XML import";
                                            clsContactDetails.Debit = 0;
                                            clsContactDetails.Credit = 0;
                                            clsContactDetails.IsCreditAllowed = false;
                                            clsContactDetails.CreditLimit = 0;
                                            clsProductDetails.SupplierID = clsContact.Insert(clsContactDetails);
                                        }

                                        clsProductDetails.BaseUnitID = clsUnit.Details(clsProductDetails.BaseUnitCode).UnitID;
                                        if (clsProductDetails.BaseUnitID == 0)
                                        {
                                            clsUnitDetails = new UnitDetails();
                                            clsUnitDetails.UnitCode = clsProductDetails.BaseUnitCode;
                                            clsUnitDetails.UnitName = clsProductDetails.BaseUnitName;
                                            clsProductDetails.BaseUnitID = clsUnit.Insert(clsUnitDetails);
                                        }

                                        clsProductDetails.ProductGroupID = clsProductGroup.Details(clsProductDetails.ProductGroupCode).ProductGroupID;
                                        if (clsProductDetails.ProductGroupID == 0)
                                        {
                                            clsProductGroupDetails = new ProductGroupDetails();
                                            clsProductGroupDetails.ProductGroupCode = clsProductDetails.ProductGroupCode;
                                            clsProductGroupDetails.ProductGroupName = clsProductDetails.ProductGroupName;
                                            clsProductGroupDetails.UnitDetails = new UnitDetails
                                            {
                                                UnitID = clsProductDetails.BaseUnitID
                                            };
                                            clsProductGroupDetails.Price = clsProductDetails.Price;
                                            clsProductGroupDetails.PurchasePrice = clsProductDetails.PurchasePrice;
                                            clsProductGroupDetails.IncludeInSubtotalDiscount = clsProductDetails.IncludeInSubtotalDiscount;
                                            clsProductGroupDetails.VAT = clsProductDetails.VAT;
                                            clsProductGroupDetails.EVAT = clsProductDetails.EVAT;
                                            clsProductGroupDetails.LocalTax = clsProductDetails.LocalTax;
                                            clsProductDetails.ProductGroupID = clsProductGroup.Insert(clsProductGroupDetails);
                                        }

                                        clsProductDetails.ProductSubGroupID = clsProductSubGroup.Details(clsProductDetails.ProductSubGroupCode).ProductSubGroupID;
                                        if (clsProductDetails.ProductSubGroupID == 0)
                                        {
                                            clsProductSubGroupDetails = new ProductSubGroupDetails();
                                            clsProductSubGroupDetails.ProductGroupID = clsProductDetails.ProductGroupID;
                                            clsProductSubGroupDetails.ProductSubGroupCode = clsProductDetails.ProductSubGroupCode;
                                            clsProductSubGroupDetails.ProductSubGroupName = clsProductDetails.ProductSubGroupName;
                                            clsProductSubGroupDetails.BaseUnitID = clsProductDetails.BaseUnitID;
                                            clsProductSubGroupDetails.Price = clsProductDetails.Price;
                                            clsProductSubGroupDetails.PurchasePrice = clsProductDetails.PurchasePrice;
                                            clsProductSubGroupDetails.IncludeInSubtotalDiscount = clsProductDetails.IncludeInSubtotalDiscount;
                                            clsProductSubGroupDetails.VAT = clsProductDetails.VAT;
                                            clsProductSubGroupDetails.EVAT = clsProductDetails.EVAT;
                                            clsProductSubGroupDetails.LocalTax = clsProductDetails.LocalTax;
                                            clsProductDetails.ProductSubGroupID = clsProductSubGroup.Insert(clsProductSubGroupDetails);
                                        }

                                        clsTransferOutItemDetails.ProductID = clsProduct.Insert(clsProductDetails);
                                    }
                                    else
                                    {
                                        //product code already exist but not the same barcode
                                        clsProduct.UpdateBarcode(clsTransferOutItemDetails.ProductID, clsTransferOutItemDetails.BarCode);
                                    }
                                    lngProductID = clsTransferOutItemDetails.ProductID;
                                }

                                clsTransferOutItem.Insert(clsTransferOutItemDetails);

                                clsTransferOutDetails = new TransferOutDetails();
                                clsTransferOutDetails.TransferOutID = clsTransferOutItemDetails.TransferOutID;
                                clsTransferOutDetails.DiscountApplied = Convert.ToDecimal(txtTransferOutDiscountApplied.Text);
                                clsTransferOutDetails.DiscountType = (DiscountTypes)Enum.Parse(typeof(DiscountTypes), cboTransferOutDiscountType.SelectedItem.Value);

                                clsTransferOutDetails.Discount2Applied = Convert.ToDecimal(txtTransferOutDiscount2Applied.Text);
                                clsTransferOutDetails.Discount2Type = (DiscountTypes)Enum.Parse(typeof(DiscountTypes), cboTransferOutDiscount2Type.SelectedItem.Value);

                                clsTransferOutDetails.Discount3Applied = Convert.ToDecimal(txtTransferOutDiscount2Applied.Text);
                                clsTransferOutDetails.Discount3Type = (DiscountTypes)Enum.Parse(typeof(DiscountTypes), cboTransferOutDiscount3Type.SelectedItem.Value);

                                clsTransferOut = new TransferOut(clsTransferOutItem.Connection, clsTransferOutItem.Transaction);
                                clsTransferOut.UpdateDiscount(clsTransferOutItemDetails.TransferOutID, clsTransferOutDetails.DiscountApplied, clsTransferOutDetails.DiscountType, clsTransferOutDetails.Discount2Applied, clsTransferOutDetails.Discount2Type, clsTransferOutDetails.Discount3Applied, clsTransferOutDetails.Discount3Type);

                                clsTransferOutDetails = clsTransferOut.Details(clsTransferOutItemDetails.TransferOutID);
                                UpdateFooter(clsTransferOutDetails);

                                lngProductCtr++;
                            }
                            else if (xmlReader.Name == "Variation")
                            {
                                if (lngProductID != 0)
                                {
                                    clsProductVariationDetails = new ProductVariationDetails();

                                    clsProductVariationDetails.VariationID = clsProductVariation.Details(lngProductID, xmlReader.GetAttribute("VariationCode")).VariationID;
                                    if (clsProductVariationDetails.VariationID == 0)
                                    {
                                        clsProductVariationDetails.ProductID = lngProductID;
                                        clsProductVariationDetails.VariationCode = xmlReader.GetAttribute("VariationCode");
                                        clsProductVariationDetails.VariationType = xmlReader.GetAttribute("VariationType");

                                        clsProductVariation.Insert(clsProductVariationDetails);
                                    }
                                }
                            }
                            else
                            {
                                //lblError.Text += "<b>" + xmlReader.Name + ":</b>" + xmlReader.Value + "<br />";
                            }
                            break;
                        case XmlNodeType.Text:
                            //lblError.Text += "<b>" + xmlReader.LocalName + ":</b>" + xmlReader.Value + "<br />";
                            break;
                    }
                }
                xmlReader.Close();

                clsTransferOut.CommitAndDispose();
                LoadRecord();
                LoadItems();
            }
            else
            {
                string stScript = "<Script>";
                stScript += "window.alert('Please select Transfer In file to upload.')";
                stScript += "</Script>";
                Response.Write(stScript);
            }
        }
Beispiel #4
0
        private string CreateBarCode()
        {
            string strRetValue = "";

            Data.ProductSubGroup clsProductSubGroup = new Data.ProductSubGroup();
            string strProductCode = clsProductSubGroup.getBarCodeCounter(Int64.Parse(lblProductSubGroupID.Text)).ToString().PadLeft(13 - (lblProductSubGroupID.Text.Length + 2), '0');
            clsProductSubGroup.CommitAndDispose();

            BarcodeHelper ean13 = new BarcodeHelper("99", lblProductSubGroupID.Text, strProductCode);
            strRetValue = ean13.CountryCode + ean13.ManufacturerCode + ean13.ProductCode + ean13.ChecksumDigit;

            return strRetValue;
        }
        private void Upload1()
        {
            //string fn = System.IO.Path.GetFileName(TextBox1.Text);
            //string SaveLocation = "/RetailPlus/temp/uploaded_" + fn;

            //System.IO.File.Copy(TextBox1.Text, SaveLocation);
            //txtPath.PostedFile.SaveAs(SaveLocation);
            XmlTextReader xmlReader = new XmlTextReader(TextBox1.Text);
            xmlReader.WhitespaceHandling = WhitespaceHandling.None;

            Branch clsBranch = new Branch();
            BranchDetails clsBranchDetails = clsBranch.Details(Convert.ToInt16(cboBranchUpload.SelectedItem.Value.ToString()));
            clsBranch.CommitAndDispose();

            RemoteBranchInventory clsBranchInventory = new RemoteBranchInventory();
            clsBranchInventory.GetConnectionToBranch(clsBranchDetails.DBIP);

            Contacts clsBranchContact = new Contacts(clsBranchInventory.Connection, clsBranchInventory.Transaction);
            ContactDetails clsBranchContactDetails;

            ContactGroups clsBranchContactGroup = new ContactGroups(clsBranchInventory.Connection, clsBranchInventory.Transaction);
            ContactGroupDetails clsContactGroupDetails;

            Data.Unit clsBranchUnit = new Data.Unit(clsBranchInventory.Connection, clsBranchInventory.Transaction);
            UnitDetails clsUnitDetails;

            ProductGroup clsBranchProductGroup = new Data.ProductGroup(clsBranchInventory.Connection, clsBranchInventory.Transaction);
            ProductGroupDetails clsBranchProductGroupDetails;

            ProductSubGroup clsBranchProductSubGroup = new Data.ProductSubGroup(clsBranchInventory.Connection, clsBranchInventory.Transaction);
            ProductSubGroupDetails clsBranchProductSubGroupDetails;

            //Data.Variation clsBranchVariation = new Variation(clsBranchInventory.Connection, clsBranchInventory.Transaction);
            Products clsBranchProduct = new Products(clsBranchInventory.Connection, clsBranchInventory.Transaction);
            ProductDetails clsBranchProductDetails;

            ProductVariations clsBranchProductVariation = new ProductVariations(clsBranchInventory.Connection, clsBranchInventory.Transaction);
            ProductVariationDetails clsBranchProductVariationDetails;

            long lngBranchProductID = 0; long lngProductCtr = 0; long lngProductInserted = 0;

            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:

                        if (xmlReader.Name == "Item")
                        {
                            lngProductCtr++;

                            clsBranchProductDetails = new ProductDetails();
                            clsBranchProductDetails.BarCode = xmlReader.GetAttribute("BarCode");
                            clsBranchProductDetails.ProductCode = xmlReader.GetAttribute("ProductCode");
                            lblError.Text += "Checking <b>" + clsBranchProductDetails.ProductCode + "</b> if exist... ";

                            //check product by barcode
                            clsBranchProductDetails.ProductID = clsBranchProduct.Details(Constants.BRANCH_ID_MAIN, clsBranchProductDetails.BarCode).ProductID;
                            lngBranchProductID = clsBranchProductDetails.ProductID;
                            if (clsBranchProductDetails.ProductID != 0)
                            {
                                lblError.Text += " [Y] barcode exist... next item...<br />";
                                break;
                            }

                            //check product by product code
                            clsBranchProductDetails.ProductID = clsBranchProduct.Details(Constants.BRANCH_ID_MAIN, clsBranchProductDetails.ProductCode).ProductID;
                            lngBranchProductID = clsBranchProductDetails.ProductID;
                            if (clsBranchProductDetails.ProductID != 0)
                            {
                                clsBranchProduct.UpdateBarcode(clsBranchProductDetails.ProductID, clsBranchProductDetails.BarCode);
                                lblError.Text += " [Y] barcode not exist, product code exist. barcode updated. next item...<br />";
                                break;
                            }

                            lblError.Text += " [N] inserting product... ";

                            clsBranchProductDetails.BarCode = xmlReader.GetAttribute("BarCode");
                            clsBranchProductDetails.ProductDesc = xmlReader.GetAttribute("ProductDesc");
                            clsBranchProductDetails.ProductGroupCode = xmlReader.GetAttribute("ProductGroupCode");
                            clsBranchProductDetails.ProductGroupName = xmlReader.GetAttribute("ProductGroupName");
                            clsBranchProductDetails.ProductSubGroupCode = xmlReader.GetAttribute("ProductSubGroupCode");
                            clsBranchProductDetails.ProductSubGroupName = xmlReader.GetAttribute("ProductSubGroupName");
                            clsBranchProductDetails.BaseUnitCode = xmlReader.GetAttribute("BaseUnitCode");
                            clsBranchProductDetails.BaseUnitName = xmlReader.GetAttribute("BaseUnitName");
                            clsBranchProductDetails.DateCreated = DateTime.Now;
                            clsBranchProductDetails.Price = Convert.ToDecimal(xmlReader.GetAttribute("Price"));
                            clsBranchProductDetails.PurchasePrice = Convert.ToDecimal(xmlReader.GetAttribute("PurchasePrice"));
                            clsBranchProductDetails.IncludeInSubtotalDiscount = Convert.ToBoolean(xmlReader.GetAttribute("IncludeInSubtotalDiscount"));
                            clsBranchProductDetails.VAT = Convert.ToDecimal(xmlReader.GetAttribute("VAT"));
                            clsBranchProductDetails.EVAT = Convert.ToDecimal(xmlReader.GetAttribute("EVAT"));
                            clsBranchProductDetails.LocalTax = Convert.ToDecimal(xmlReader.GetAttribute("LocalTax"));
                            clsBranchProductDetails.Quantity = 0;
                            clsBranchProductDetails.MinThreshold = Convert.ToDecimal(xmlReader.GetAttribute("MinThreshold"));
                            clsBranchProductDetails.MaxThreshold = Convert.ToDecimal(xmlReader.GetAttribute("MaxThreshold"));
                            clsBranchProductDetails.ChartOfAccountIDPurchase = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDPurchase"));
                            clsBranchProductDetails.ChartOfAccountIDSold = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDSold"));
                            clsBranchProductDetails.ChartOfAccountIDInventory = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDInventory"));
                            clsBranchProductDetails.ChartOfAccountIDTaxPurchase = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDTaxPurchase"));
                            clsBranchProductDetails.ChartOfAccountIDTaxSold = int.Parse(xmlReader.GetAttribute("ChartOfAccountIDTaxSold"));
                            clsBranchProductDetails.IsItemSold = Convert.ToBoolean(xmlReader.GetAttribute("IsItemSold"));
                            clsBranchProductDetails.WillPrintProductComposition = Convert.ToBoolean(xmlReader.GetAttribute("WillPrintProductComposition"));
                            clsBranchProductDetails.UpdatedBy = long.Parse(xmlReader.GetAttribute("UpdatedBy"));
                            clsBranchProductDetails.UpdatedOn = Convert.ToDateTime(xmlReader.GetAttribute("UpdatedOn"));
                            clsBranchProductDetails.PercentageCommision = decimal.Parse(xmlReader.GetAttribute("PercentageCommision"));
                            clsBranchProductDetails.QuantityIN = decimal.Parse(xmlReader.GetAttribute("QuantityIN"));
                            clsBranchProductDetails.QuantityOUT = decimal.Parse(xmlReader.GetAttribute("QuantityOUT"));

                            clsBranchProductDetails.SupplierCode = xmlReader.GetAttribute("ContactCode");
                            clsBranchProductDetails.SupplierID = clsBranchContact.Details(clsBranchProductDetails.SupplierCode).ContactID;
                            if (clsBranchProductDetails.SupplierID == 0)
                            {
                                clsBranchContactDetails = new ContactDetails();
                                clsBranchContactDetails.ContactGroupID = clsBranchContactGroup.Details(xmlReader.GetAttribute("ContactGroupCode")).ContactGroupID;
                                if (clsBranchContactDetails.ContactGroupID == 0)
                                {
                                    clsContactGroupDetails = new ContactGroupDetails();
                                    clsContactGroupDetails.ContactGroupCode = xmlReader.GetAttribute("ContactCode");
                                    clsContactGroupDetails.ContactGroupName = xmlReader.GetAttribute("ContactCode");
                                    clsContactGroupDetails.ContactGroupCategory = (ContactGroupCategory)Enum.Parse(typeof(ContactGroupCategory), xmlReader.GetAttribute("ContactGroupCategory"));
                                    clsBranchContactDetails.ContactGroupID = clsBranchContactGroup.Insert(clsContactGroupDetails);
                                }

                                clsBranchContactDetails.ContactCode = xmlReader.GetAttribute("ContactCode");
                                clsBranchContactDetails.ContactName = xmlReader.GetAttribute("ContactName");

                                clsBranchContactDetails.ModeOfTerms = (ModeOfTerms)Enum.Parse(typeof(ModeOfTerms), xmlReader.GetAttribute("ModeOfTerms"));
                                clsBranchContactDetails.Terms = Convert.ToInt32(xmlReader.GetAttribute("Terms"));
                                clsBranchContactDetails.Address = xmlReader.GetAttribute("Address");
                                clsBranchContactDetails.BusinessName = xmlReader.GetAttribute("BusinessName");
                                clsBranchContactDetails.TelephoneNo = xmlReader.GetAttribute("TelephoneNo");
                                clsBranchContactDetails.Remarks = xmlReader.GetAttribute("Remarks");
                                clsBranchContactDetails.Debit = Convert.ToDecimal(xmlReader.GetAttribute("Debit"));
                                clsBranchContactDetails.Credit = Convert.ToDecimal(xmlReader.GetAttribute("Credit"));
                                clsBranchContactDetails.IsCreditAllowed = Convert.ToBoolean(xmlReader.GetAttribute("IsCreditAllowed"));
                                clsBranchContactDetails.CreditLimit = Convert.ToDecimal(xmlReader.GetAttribute("CreditLimit"));
                                clsBranchContactDetails.ContactID = clsBranchContact.Insert(clsBranchContactDetails);
                            }

                            clsBranchProductDetails.BaseUnitCode = xmlReader.GetAttribute("BaseUnitCode");
                            clsBranchProductDetails.BaseUnitID = clsBranchUnit.Details(clsBranchProductDetails.BaseUnitCode).UnitID;
                            if (clsBranchProductDetails.BaseUnitID == 0)
                            {
                                clsUnitDetails = new UnitDetails();
                                clsUnitDetails.UnitCode = xmlReader.GetAttribute("BaseUnitCode");
                                clsUnitDetails.UnitName = xmlReader.GetAttribute("BaseUnitName");
                                clsBranchProductDetails.BaseUnitID = clsBranchUnit.Insert(clsUnitDetails);
                            }


                            clsBranchProductDetails.ProductGroupCode = xmlReader.GetAttribute("ProductGroupCode");
                            clsBranchProductDetails.ProductGroupID = clsBranchProductGroup.Details(clsBranchProductDetails.ProductGroupCode).ProductGroupID;
                            if (clsBranchProductDetails.ProductGroupID == 0)
                            {
                                lblError.Text += "inserting product group....";
                                clsBranchProductGroupDetails = new ProductGroupDetails();
                                clsBranchProductGroupDetails.ProductGroupCode = xmlReader.GetAttribute("ProductGroupCode");
                                clsBranchProductGroupDetails.ProductGroupName = xmlReader.GetAttribute("ProductGroupName");
                                clsBranchProductGroupDetails.UnitDetails = new UnitDetails
                                {
                                    UnitID = clsBranchProductDetails.BaseUnitID
                                };
                                clsBranchProductGroupDetails.Price = clsBranchProductDetails.Price;
                                clsBranchProductGroupDetails.PurchasePrice = clsBranchProductDetails.PurchasePrice;
                                clsBranchProductGroupDetails.IncludeInSubtotalDiscount = clsBranchProductDetails.IncludeInSubtotalDiscount;
                                clsBranchProductGroupDetails.VAT = clsBranchProductDetails.VAT;
                                clsBranchProductGroupDetails.EVAT = clsBranchProductDetails.EVAT;
                                clsBranchProductGroupDetails.LocalTax = clsBranchProductDetails.LocalTax;
                                clsBranchProductDetails.ProductGroupID = clsBranchProductGroup.Insert(clsBranchProductGroupDetails);
                            }

                            clsBranchProductDetails.ProductSubGroupCode = xmlReader.GetAttribute("ProductSubGroupCode");
                            clsBranchProductDetails.ProductSubGroupID = clsBranchProductSubGroup.Details(clsBranchProductDetails.ProductSubGroupCode).ProductSubGroupID;
                            if (clsBranchProductDetails.ProductSubGroupID == 0)
                            {
                                lblError.Text += "inserting product sub-group....";
                                clsBranchProductSubGroupDetails = new ProductSubGroupDetails();
                                clsBranchProductSubGroupDetails.ProductGroupID = clsBranchProductDetails.ProductGroupID;
                                clsBranchProductSubGroupDetails.ProductSubGroupCode = xmlReader.GetAttribute("ProductSubGroupCode");
                                clsBranchProductSubGroupDetails.ProductSubGroupName = xmlReader.GetAttribute("ProductSubGroupName");
                                clsBranchProductSubGroupDetails.BaseUnitID = clsBranchProductDetails.BaseUnitID;
                                clsBranchProductSubGroupDetails.Price = clsBranchProductDetails.Price;
                                clsBranchProductSubGroupDetails.PurchasePrice = clsBranchProductDetails.PurchasePrice;
                                clsBranchProductSubGroupDetails.IncludeInSubtotalDiscount = clsBranchProductDetails.IncludeInSubtotalDiscount;
                                clsBranchProductSubGroupDetails.VAT = clsBranchProductDetails.VAT;
                                clsBranchProductSubGroupDetails.EVAT = clsBranchProductDetails.EVAT;
                                clsBranchProductSubGroupDetails.LocalTax = clsBranchProductDetails.LocalTax;
                                clsBranchProductDetails.ProductSubGroupID = clsBranchProductSubGroup.Insert(clsBranchProductSubGroupDetails);
                            }

                            clsBranchProductDetails.ProductID = clsBranchProduct.Insert(clsBranchProductDetails);
                            lngBranchProductID = clsBranchProductDetails.ProductID;
                            lngProductInserted++;

                            lblError.Text += " [done]. next item...<br />";
                        }
                        else if (xmlReader.Name == "Variation")
                        {
                            if (lngBranchProductID != 0)
                            {
                                clsBranchProductVariationDetails = new ProductVariationDetails();

                                clsBranchProductVariationDetails.VariationID = clsBranchProductVariation.Details(lngBranchProductID, xmlReader.GetAttribute("VariationCode")).VariationID;
                                if (clsBranchProductVariationDetails.VariationID == 0)
                                {
                                    clsBranchProductVariationDetails.ProductID = lngBranchProductID;
                                    clsBranchProductVariationDetails.VariationCode = xmlReader.GetAttribute("VariationCode");
                                    clsBranchProductVariationDetails.VariationType = xmlReader.GetAttribute("VariationType");

                                    clsBranchProductVariation.Insert(clsBranchProductVariationDetails);
                                }
                            }
                        }
                        else
                        {
                            lblError.Text += "<b>" + xmlReader.Name + ":</b>" + xmlReader.Value + "<br />";
                        }
                        break;
                    case XmlNodeType.Text:
                        lblError.Text += "<b>" + xmlReader.LocalName + ":</b>" + xmlReader.Value + "<br />";
                        break;
                }
            }
            xmlReader.Close();

            clsBranchInventory.CommitAndDispose();
            lblError.Text = "<b>" + lngProductInserted.ToString() + " out of " + lngProductCtr.ToString() + " has been successfully transferred.</b><br /><br />" + lblError.Text;
        }
        private void SynchronizeFromBranch()
        {
            try
            {
                lblError.Text = string.Empty;

                Branch clsBranch = new Branch();
                BranchDetails clsBranchDetails = clsBranch.Details(Convert.ToInt16(cboSynchronizeFromBranch.SelectedItem.Value.ToString()));
                clsBranch.CommitAndDispose();

                if (IPAddress.IsOpen(clsBranchDetails.DBIP, int.Parse(clsBranchDetails.DBPort)) == false)
                {
                    lblError.Text = "Sorry cannot connect to Branch '" + cboSynchronizeFromBranch.SelectedItem.Text + "'. Please check you connection to IP Address :" + clsBranchDetails.DBIP + ". <br /><br />";
                    lblError.Text += "HOW TO CHECK : <br /><br />";
                    lblError.Text += "  1. Open command prompt<br />";
                    lblError.Text += "  2. Type ping[space][IP Address]<br /><br />";
                    lblError.Text += "If the answer is 'Request timed out.' then contact system administrator.<br />";
                    lblError.Text += "Else if the answer is 'Reply...' Follow the next steps.<br /><br />";
                    lblError.Text += "  3. Type telnet[space][IP Address][sapce][IP Port]<br /><br />";

                    return;
                }

                Session.Timeout = 60 * 60 * 30;

                Products clsProduct = new Products();
                clsProduct.GetConnection();
                ProductDetails clsProductDetails;
                ContactGroups clsContactGroup = new ContactGroups(clsProduct.Connection, clsProduct.Transaction);
                Contacts clsContact = new Contacts(clsProduct.Connection, clsProduct.Transaction);
                Data.Unit clsUnit = new Data.Unit(clsProduct.Connection, clsProduct.Transaction);
                Data.ProductGroup clsProductGroup = new Data.ProductGroup(clsProduct.Connection, clsProduct.Transaction);
                Data.ProductSubGroup clsProductSubGroup = new Data.ProductSubGroup(clsProduct.Connection, clsProduct.Transaction);
                Data.Variation clsVariation = new Variation(clsProduct.Connection, clsProduct.Transaction);

                RemoteBranchInventory clsBranchInventory = new RemoteBranchInventory();
                clsBranchInventory.GetConnectionToBranch(clsBranchDetails.DBIP, clsBranchDetails.DBPort);

                Products clsBranchProduct = new Products(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                ProductDetails[] arrBranchProductDetails = clsBranchProduct.List();

                ContactGroups clsBranchContactGroup = new ContactGroups(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Contacts clsBranchContact = new Contacts(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Data.Unit clsBranchUnit = new Data.Unit(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Data.ProductGroup clsBranchProductGroup = new Data.ProductGroup(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Data.ProductSubGroup clsBranchProductSubGroup = new Data.ProductSubGroup(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Data.Variation clsBranchVariation = new Variation(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                
                foreach (ProductDetails clsBranchProductDetails in arrBranchProductDetails)
                {
                    clsProductDetails = clsBranchProductDetails;
                    try
                    {
                        clsProductDetails.ProductID = clsProduct.Details(clsProductDetails.BarCode).ProductID;
                        if (clsProductDetails.ProductID != 0)
                        {
                            lblError.Text += clsProductDetails.BarCode + " already exist.<br /><br />";
                            clsProduct.UpdatePurchasing(clsProductDetails.ProductID, clsBranchProductDetails.MatrixID, clsProductDetails.SupplierID, clsProductDetails.BaseUnitID, clsProductDetails.PurchasePrice);
                            clsProduct.UpdateSellingPrice(clsProductDetails.ProductID, clsBranchProductDetails.MatrixID, clsProductDetails.SupplierID, clsProductDetails.BaseUnitID, clsProductDetails.Price, clsProductDetails.Price1, clsProductDetails.Price2, clsProductDetails.Price3, clsProductDetails.Price4, clsProductDetails.Price5);
                        }
                        else
                        {
                            clsProductDetails.ProductID = clsProduct.DetailsByCode(Constants.BRANCH_ID_MAIN, clsProductDetails.BarCode).ProductID;
                            if (clsProductDetails.ProductID != 0)
                            {
                                lblError.Text += clsProductDetails.ProductCode + " already exist.<br /><br />";
                                clsProduct.UpdateBarcode(clsProductDetails.ProductID, clsProductDetails.BarCode);
                                clsProduct.UpdatePurchasing(clsProductDetails.ProductID, clsBranchProductDetails.MatrixID, clsProductDetails.SupplierID, clsProductDetails.BaseUnitID, clsProductDetails.PurchasePrice);
                                clsProduct.UpdateSellingPrice(clsProductDetails.ProductID, clsBranchProductDetails.MatrixID, clsProductDetails.SupplierID, clsProductDetails.BaseUnitID, clsProductDetails.Price, clsProductDetails.Price1, clsProductDetails.Price2, clsProductDetails.Price3, clsProductDetails.Price4, clsProductDetails.Price5);
                            }
                            else
                            {
                                clsProductDetails.SupplierID = clsContact.Details(clsProductDetails.SupplierCode).ContactID;
                                if (clsProductDetails.SupplierID == 0)
                                {
                                    ContactDetails clsBranchContactDetails = clsBranchContact.Details(clsProductDetails.SupplierCode);
                                    if (clsContactGroup.Details(clsBranchContactDetails.ContactGroupID).ContactGroupID == 0)
                                    {
                                        ContactGroupDetails clsBranchContactGroupDetails = clsBranchContactGroup.Details(clsBranchContactDetails.ContactGroupID);
                                        clsBranchContactDetails.ContactGroupID = clsContactGroup.Insert(clsBranchContactGroupDetails);
                                    }
                                    clsProductDetails.SupplierID = clsContact.Insert(clsBranchContactDetails);
                                }

                                clsProductDetails.BaseUnitID = clsUnit.Details(clsProductDetails.BaseUnitCode).UnitID;
                                if (clsProductDetails.BaseUnitID == 0)
                                {
                                    UnitDetails clsBranchUnitDetails = clsBranchUnit.Details(clsBranchProductDetails.BaseUnitID);
                                    clsProductDetails.BaseUnitID = clsUnit.Insert(clsBranchUnitDetails);
                                }

                                clsProductDetails.ProductGroupID = clsProductGroup.Details(clsProductDetails.ProductGroupCode).ProductGroupID;
                                if (clsProductDetails.ProductGroupID == 0)
                                {
                                    ProductGroupDetails clsBranchProductGroupDetails = clsBranchProductGroup.Details(clsBranchProductDetails.ProductGroupID);
                                    clsProductDetails.ProductGroupID = clsProductGroup.Insert(clsBranchProductGroupDetails);
                                }

                                clsProductDetails.ProductSubGroupID = clsProductSubGroup.Details(clsProductDetails.ProductSubGroupCode).ProductSubGroupID;
                                if (clsProductDetails.ProductSubGroupID == 0)
                                {
                                    ProductSubGroupDetails clsBranchProductSubGroupDetails = clsBranchProductSubGroup.Details(clsBranchProductDetails.ProductSubGroupID);
                                    clsProductDetails.ProductSubGroupID = clsProductSubGroup.Insert(clsBranchProductSubGroupDetails);
                                }

                                clsProductDetails.Quantity = 0;
                                clsProductDetails.QuantityIN = 0;
                                clsProductDetails.QuantityOUT = 0;

                                try
                                {
                                    clsProductDetails.ProductID = clsProduct.Insert(clsProductDetails);
                                    lblError.Text += clsProductDetails.ProductCode + " inserted.<br /><br />";
                                }
                                catch (Exception exProduct){
                                    lblError.Text += "<div class=ms-alternating> ERROR INSERTING ITEM: " + clsProductDetails.ProductCode + " err: " + exProduct.Message + ".</div><br /><br />";
                                    if (clsProduct.Connection.State == ConnectionState.Closed)
                                    {
                                        clsProduct = new Products();
                                        clsProduct.GetConnection();
                                        clsContactGroup = new ContactGroups(clsProduct.Connection, clsProduct.Transaction);
                                        clsContact = new Contacts(clsProduct.Connection, clsProduct.Transaction);
                                        clsUnit = new Data.Unit(clsProduct.Connection, clsProduct.Transaction);
                                        clsProductGroup = new Data.ProductGroup(clsProduct.Connection, clsProduct.Transaction);
                                        clsProductSubGroup = new Data.ProductSubGroup(clsProduct.Connection, clsProduct.Transaction);
                                        clsVariation = new Variation(clsProduct.Connection, clsProduct.Transaction);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception exProducts)
                    {
                        lblError.Text += "<div class=ms-alternating> ERROR INSERTING ITEM: " + clsProductDetails.ProductCode + " err: " + exProducts.Message + ".</div><br /><br />";
                        if (clsProduct.Connection.State == ConnectionState.Closed)
                        {
                            clsProduct = new Products();
                            clsProduct.GetConnection();
                            clsContactGroup = new ContactGroups(clsProduct.Connection, clsProduct.Transaction);
                            clsContact = new Contacts(clsProduct.Connection, clsProduct.Transaction);
                            clsUnit = new Data.Unit(clsProduct.Connection, clsProduct.Transaction);
                            clsProductGroup = new Data.ProductGroup(clsProduct.Connection, clsProduct.Transaction);
                            clsProductSubGroup = new Data.ProductSubGroup(clsProduct.Connection, clsProduct.Transaction);
                            clsVariation = new Variation(clsProduct.Connection, clsProduct.Transaction);
                        }
                    }
                }

                clsProduct.CommitAndDispose();
                clsBranchInventory.CommitAndDispose();

                lblError.Text = "Done synchronizing products from Branch: " + clsBranchDetails.BranchCode + "<br /><br />" + lblError.Text;
            }
            catch (Exception ex)
            {
                lblError.Text += "ERROR WHILE CREATING INSERT STATEMENT: " + ex.Message;
            }
        }