コード例 #1
0
ファイル: POS.cs プロジェクト: kimykunjun/test
        public bool SavePOS(ACMSLogic.POS pos)
        {
            ACMSDAL.ConnectionProvider connProvider = new ConnectionProvider();
            //ACMSDAL.ConnectionProvider connProvider1 = new ConnectionProvider();

            try
            {
                DataTable branchReceiptNoTable = new DataTable();
                int latestReceiptNo = GetLatestReceiptNo(connProvider, pos, ref branchReceiptNoTable) + 1;
                string receiptNo = pos.StrBranchCode.ToString().Trim() + pos.NTerminalID.ToString().Trim() + latestReceiptNo.ToString().Trim();

                pos.StrReceiptNo = receiptNo;

                //Derek Instalment Plan Need to Add PaymentPlan Table Here
                //Get pos.NCategoryID here to determine if the item is Pay O/S
                //Here Can get the new Receipt No Value
                //3 Main table that always need to update

                TblReceipt receipt = new TblReceipt();
                TblReceiptEntries receiptEntries = new TblReceiptEntries();
                TblReceiptPayment receiptPayment = new TblReceiptPayment();
                TblPaymentPlan myPaymentPlan = new TblPaymentPlan();

                receipt.MainConnectionProvider = connProvider;
                receiptEntries.MainConnectionProvider = connProvider;
                receiptPayment.MainConnectionProvider = connProvider;
                myPaymentPlan.MainConnectionProvider = connProvider;

                connProvider.OpenConnection();
                connProvider.BeginTransaction("SaveReceipt");

                DataTable receiptTable = pos.ReceiptMasterTable.Copy();
                DataTable receiptEntriesTable = pos.ReceiptItemsTable.Copy();
                DataTable receiptPaymentTable = pos.ReceiptPaymentTable.Copy();
                DataTable freeMemberPackageTable = receipt.FillSchema("Select * From tblMemberPackage"); // use to store the free member package.
                DataTable freeMemberCreditPackageTable = receipt.FillSchema("Select * From tblMemberCreditPackage");

                if (pos.PaymentPlanTable != null && pos.NCategoryID != 0)
                {
                    DataTable paymentPlanTable = pos.PaymentPlanTable.Copy();

                    if (paymentPlanTable != null && paymentPlanTable.Rows.Count > 0)
                    {
                        foreach (DataRow r in paymentPlanTable.Rows)
                        {
                            r["strReceiptNo"] = pos.StrReceiptNo;

                            if (r["nInstalmentNo"].ToString() == "1")
                            {
                                r["strPaymentReceiptNo"] = pos.StrReceiptNo;

                                myPaymentPlan.IPPPrimaryEntry(r["strReceiptNo"].ToString(), Convert.ToInt32(r["nInstalmentNo"]),
                                                                r["strPaymentReceiptNo"].ToString(), Convert.ToDecimal(r["mPaymentPlanAmt"]),
                                                                Convert.ToDecimal(r["mPaidAmount"]), Convert.ToDecimal(r["mAdjustedPaymentPlanAmt"]),
                                                                Convert.ToDecimal(r["mOutstandingAmt"]), Convert.ToDateTime(r["dtPaymentDate"]),
                                                                Convert.ToDateTime(r["dtDueDate"]), Convert.ToDateTime(r["dtFinalDueDate"]),
                                                                Convert.ToInt32(r["nPaymentPlanID"]));

                            }
                            else
                            {
                                myPaymentPlan.IPPSubsequentEntry(r["strReceiptNo"].ToString(), Convert.ToInt32(r["nInstalmentNo"]),
                                                                    Convert.ToDecimal(r["mPaymentPlanAmt"]), Convert.ToDecimal(r["mPaidAmount"]),
                                                                    Convert.ToDecimal(r["mAdjustedPaymentPlanAmt"]), Convert.ToDecimal(r["mOutstandingAmt"]),
                                                                    Convert.ToDateTime(r["dtDueDate"]), Convert.ToDateTime(r["dtFinalDueDate"]),
                                                                    Convert.ToInt32(r["nPaymentPlanID"]));
                            }
                        }
                        paymentPlanTable.AcceptChanges();
                    }
                    pos.PaymentPlanTable.AcceptChanges();

                }
                else if (pos.PaymentPlanTable != null && pos.NCategoryID == 0)
                {
                    DataTable paymentPlanTable = pos.PaymentPlanTable.Copy();

                    if (paymentPlanTable != null && paymentPlanTable.Rows.Count > 0)
                    {
                        foreach (DataRow r in paymentPlanTable.Rows)
                        {
                            if (r["lastestUpdate"].ToString() == "1")
                            {
                                r["strPaymentReceiptNo"] = pos.StrReceiptNo;

                                myPaymentPlan.IPPPrimaryUpdate(Convert.ToInt32(r["nPaymentPlanID"]), r["strReceiptNo"].ToString(),
                                                                Convert.ToInt32(r["nInstalmentNo"]), r["strPaymentReceiptNo"].ToString(),
                                                                Convert.ToDecimal(r["mPaidAmount"]), Convert.ToDecimal(r["mAdjustedPaymentPlanAmt"]),
                                                                Convert.ToDecimal(r["mOutstandingAmt"]), Convert.ToDateTime(r["dtPaymentDate"]));

                            }
                            else
                            {
                                myPaymentPlan.IPPSubsequentUpdate(Convert.ToInt32(r["nPaymentPlanID"]), r["strReceiptNo"].ToString(),
                                                                    Convert.ToInt32(r["nInstalmentNo"]), Convert.ToDecimal(r["mPaidAmount"]),
                                                                    Convert.ToDecimal(r["mAdjustedPaymentPlanAmt"]), Convert.ToDecimal(r["mOutstandingAmt"]));
                            }
                        }

                        //paymentPlanTable.AcceptChanges();
                    }
                    pos.PaymentPlanTable.AcceptChanges();
                }

                receipt.SaveData(receiptTable);
                receiptEntries.SaveData(receiptEntriesTable);
                receiptPayment.SaveData(receiptPaymentTable);

                receiptEntries.StrReceiptNo = pos.StrReceiptNo;
                DataTable savedReceiptEntriesTable = receiptEntries.SelectAllWstrReceiptNoLogic();

                pos.ReceiptMasterTable.AcceptChanges();

                if (pos.StrFreebieCode.HasValue && pos.StrFreebieCode.ToString() != "")
                {
                    //Update Bill Freebie here
                    if (pos.ReceiptFreebieTable.Rows.Count > 0)
                    {
                        TblReceiptFreebie receiptFreebie = new TblReceiptFreebie();
                        receiptFreebie.MainConnectionProvider = connProvider;
                        DataTable receiptFreebieTable = pos.ReceiptFreebieTable.Copy();

                        // Save Bill Receipt Freebie
                        receiptFreebie.SaveData(receiptFreebieTable);

                        //TblProductInventory proInven = new TblProductInventory();
                        //proInven.MainConnectionProvider = connProvider;

                        //// deduct stock
                        //foreach (DataRow r in receiptFreebieTable.Rows)
                        //{
                        //    proInven.IncreaseQuantity(r["strItemCode"].ToString(), pos.StrBranchCode, -1);
                        //}
                    }
                    if (pos.ReceiptPackageTable.Rows.Count > 0)
                    {
                        // update package

                        foreach (DataRow r in pos.ReceiptPackageTable.Rows)
                        {
                            TblCreditPackage p = new TblCreditPackage();
                            p.StrCreditPackageCode = r["strPackageCode"].ToString();
                            p.SelectOne();

                            if (p.StrDescription.ToString() != "Null")
                            {
                                DataRow rowToAdd = freeMemberCreditPackageTable.NewRow();

                                ACMSLogic.MemberPackage.InitMemberCreditPackageRowInPOS(rowToAdd, pos.StrReceiptNo, pos.StrMembershipID,
                                    r["strPackageCode"].ToString(), r["strPromotionCode"].ToString(), true);

                                // Add to freeMemberPackageTable and will save
                                freeMemberCreditPackageTable.Rows.Add(rowToAdd);
                            }
                            else
                            {
                                DataRow rowToAdd = freeMemberPackageTable.NewRow();

                                ACMSLogic.MemberPackage.InitMemberPackageRowInPOS(rowToAdd, pos.StrReceiptNo, pos.StrMembershipID,
                                    r["strPackageCode"].ToString(), r["strPromotionCode"].ToString(), true);

                                // Add to freeMemberPackageTable and will save
                                freeMemberPackageTable.Rows.Add(rowToAdd);
                            }
                        }
                    }
                    //else
                    //{
                    //    throw new Exception("Bill Freebie have no free any product or package although the strFreebieCode is not null");
                    //}
                }

                if (pos.ReceiptItemsFreebiePackageTable.Rows.Count > 0)
                {
                    foreach (DataRow r in pos.ReceiptItemsFreebiePackageTable.Rows)
                    {
                        TblCreditPackage p = new TblCreditPackage();
                        p.StrCreditPackageCode = r["strPackageCode"].ToString();
                        p.SelectOne();

                        if (p.StrDescription.ToString() != "Null")
                        {
                            DataRow rowToAdd = freeMemberCreditPackageTable.NewRow();

                            ACMSLogic.MemberPackage.InitMemberCreditPackageRowInPOS(rowToAdd, pos.StrReceiptNo, pos.StrMembershipID,
                                r["strPackageCode"].ToString(), r["strPromotionCode"].ToString(), true);

                            // Add to freeMemberPackageTable and will save
                            freeMemberCreditPackageTable.Rows.Add(rowToAdd);
                        }
                        else
                        {
                            DataRow rowToAdd = freeMemberPackageTable.NewRow();
                            // Kean Yiap
                            if (pos.NCategoryID == 34)
                            {
                                MemberPackage.InitMemberPackageRowInPOS(rowToAdd, pos.StrReceiptNo, pos.StrMembershipID, r["strPackageCode"].ToString(), "",
                                    true, -1, pos.dtPackageStart, pos.NProrateDays);
                            }
                            else
                            {
                                ACMSLogic.MemberPackage.InitMemberPackageRowInPOS(rowToAdd, pos.StrReceiptNo, pos.StrMembershipID,
                                    r["strPackageCode"].ToString(), r["strPromotionCode"].ToString(), true);
                            }
                            // Add to freeMemberPackageTable and will save
                            freeMemberPackageTable.Rows.Add(rowToAdd);
                        }
                    }
                }

                if (pos.ReceiptItemsFreebieProductTable.Rows.Count > 0)
                {
                    //					Update product
                    TblReceiptFreebie receiptFreebie = new TblReceiptFreebie();
                    receiptFreebie.MainConnectionProvider = connProvider;
                    DataTable receiptItemsFreebieProductTable = pos.ReceiptFreebieTable.Clone(); // i have to use this structure bcoz i wan to save to this table;

                    foreach (DataRow r in pos.ReceiptItemsFreebieProductTable.Rows)
                    {
                        DataRow rowToAdd = receiptItemsFreebieProductTable.NewRow();
                        rowToAdd["strReceiptNo"] = pos.StrReceiptNo;
                        rowToAdd["strItemCode"] = r["strProductCode"];
                        rowToAdd["strPromotionCode"] = r["strPromotionCode"];

                        DataRow[] tempRows = savedReceiptEntriesTable.Select("nTempEntryID = " + r["nEntryID"].ToString(),
                            "", DataViewRowState.CurrentRows);

                        if (tempRows.Length > 0)
                            rowToAdd["nEntryID"] = tempRows[0]["nEntryID"];

                        receiptItemsFreebieProductTable.Rows.Add(rowToAdd);
                    }

                    receiptFreebie.SaveData(receiptItemsFreebieProductTable);

                    //TblProductInventory proInven = new TblProductInventory();
                    //proInven.MainConnectionProvider = connProvider;

                    //foreach (DataRow r in receiptItemsFreebieProductTable.Rows)
                    //{
                    //    proInven.IncreaseQuantity(r["strItemCode"].ToString(), pos.StrBranchCode, -1);
                    //}
                }

                if (freeMemberPackageTable.Rows.Count > 0)
                {
                    // update the member package to database
                    TblMemberPackage memberPackage = new TblMemberPackage();
                    memberPackage.MainConnectionProvider = connProvider;
                    memberPackage.SaveData(freeMemberPackageTable);
                }

                if (freeMemberCreditPackageTable.Rows.Count > 0)
                {
                    // update the member package to database
                    TblMemberCreditPackage memberPackage = new TblMemberCreditPackage();
                    memberPackage.MainConnectionProvider = connProvider;
                    memberPackage.SaveData(freeMemberCreditPackageTable);
                }

            //part 2
                DeleteUselessIPP(connProvider, pos);
                UpdateRegistrationFee(connProvider, pos);
                UpdateOldPackSetDeactivate(connProvider, pos); //Update when upgrade
                PostToMemberCreditPackage(connProvider, pos);
                PostToMemberPackage(connProvider, pos);
                UpdateUsedCashVoucher(connProvider, pos);

                int ParentCategoryID = pos.NCategoryID;

                if (pos.NCategoryID == 10)
                {
                    ParentCategoryID = UpgradePackage(connProvider, pos);
                    if (ParentCategoryID == 10)
                    {
                        ParentCategoryID = UpgradeCreditPackage(connProvider, pos);
                    }
                }

                TopUpCreditPackage(connProvider, pos);
                TopUpSingleTreatmentTransaction(connProvider, pos);
                MakeDeposit(connProvider, pos);

                CashVoucherTransaction(connProvider, pos);
                LockerTransaction(connProvider, pos);
                ForgetCardTransaction(connProvider, pos);
                ReplaceMembershipCardTransaction(connProvider, pos);
                //MineralWaterTransaction(connProvider, pos);
                TblRewards rewardcode = new TblRewards();
                ACMSDAL.TblCategory category = new ACMSDAL.TblCategory();
                category.NCategoryID = pos.NCategoryID;
                category.SelectOne();
                int SalesCategory = ACMS.Convert.ToInt32(category.NSalesCategoryID);

                //Derek Instalment Plan - Here it assigned the Category to the Pay O/S Receipt Record
                if (pos.NCategoryID == 0)
                {
                    ParentCategoryID = PayOutstanding(connProvider, pos);
                    pos.StrRewardsCode = rewardcode.SelectRewards(ParentCategoryID, pos.StrBranchCode);
                }
                else
                    pos.StrRewardsCode = rewardcode.SelectRewards(pos.NCategoryID, pos.StrBranchCode);

                if (pos.StrRewardsCode.HasValue && pos.StrRewardsCode.ToString() != "" && pos.StrRewardsCode.ToString() != "Null")
                {
                    TblRewards rewards = new TblRewards();
                    rewards.StrRewardsCode = pos.StrRewardsCode.ToString();
                    rewards.SelectOne();

                    double dRewardPoint = 0;

                    double dRewardPercent = (rewards.DRewardsPercent.IsNull ? 0 : rewards.DRewardsPercent.Value);
                    double dRewardValue = (rewards.DRewardsValue.IsNull ? 0 : rewards.DRewardsValue.Value);

                    //jackie 23 others give 50% GIRO
                    //if (ParentCategoryID == 23 && pos.MNettAmount > 60)//pos.MNettAmount
                    //{
                    //    dRewardPercent = 50;

                    //}

                    if (dRewardPercent > 0 && dRewardValue == 0)
                    {
                        decimal mnettTotal = 0;

                        if (pos.MOutstandingAmount > 0)
                        {
                            mnettTotal = pos.MNettAmount - (pos.MOutstandingAmount - pos.MGSTAmount);
                        }
                        else
                        {
                            mnettTotal = pos.MNettAmount;
                        }

                        dRewardPoint = ((double)mnettTotal) * dRewardPercent / 100;
                    }
                    else
                    {
                        if (pos.NCategoryID == 0)
                        {
                            dRewardPoint = dRewardValue;
                        }
                    }
                    if (pos.NThisMonthBirthday == 1)
                    {
                        DialogResult result1 = MessageBox.Show("Member is having birthday this months. Do you want to give double point?", "Warning",
                        MessageBoxButtons.YesNo);

                        if (result1 == DialogResult.Yes)
                            dRewardPoint *= 2;
                    }

                    TblRewardsTransaction rewardTran = new TblRewardsTransaction();
                    rewardTran.MainConnectionProvider = connProvider;
                    if (pos.NCategoryID == 20)
                    {
                        rewardTran.StrMembershipID = pos.StrNewMembershipID;
                    }
                    else
                    {
                        rewardTran.StrMembershipID = pos.StrMembershipID;
                    }
                    rewardTran.DtDate = pos.DtDate;
                    rewardTran.NTypeID = 1;
                    rewardTran.NEmployeeID = pos.NCashierID;
                    rewardTran.DRewardsPoints = dRewardPoint;
                    rewardTran.StrReferenceNo = pos.StrReceiptNo;
                    if (dRewardPoint > 0) // rewards point > 0 then insert records //3004
                    {
                        rewardTran.Insert();
                    }
                }

                if (pos.StrRewardsCode.ToString() == "Null") pos.StrRewardsCode = "";

                DataTable QtyTable = receiptEntries.Get_StrCodeReceiptNo(pos.StrReceiptNo);
                //check is the receipt fully paid
                string Nett = pos.MNettAmount.ToString();
                string Outstanding = pos.MOutstandingAmount.ToString();

                //if (pos.MOutstandingAmount == 0 && (pos.NCategoryID == 11 || pos.NCategoryID == 12))
                //    PostToStock(connProvider, QtyTable, pos);

                receiptTable = pos.ReceiptMasterTable.Copy();
                int PosCategory = ACMS.Convert.ToInt32(receiptTable.Rows[0]["nCategoryID"]);
                if (PosCategory == 24)
                {
                    receiptTable.Rows[0]["fDeposit"] = true;
                    receipt.SaveData(receiptTable);
                }

                if (pos.StrBillReferenceNo != "")
                {
                    receiptTable.Rows[0]["strBillReferenceNo"] = pos.StrBillReferenceNo;
                    receipt.SaveData(receiptTable);
                }

                if (pos.NCategoryID == 0 || pos.NCategoryID == 10)
                {
                    receipt.StrReceiptNo = pos.StrReceiptNo;
                    receipt.SelectOne();
                    receipt.MOutstandingAmount = Convert.ToDecimal(pos.MOutstandingAmount);
                    receipt.NCategoryID = ParentCategoryID;
                    receipt.Update();
                    //TblReceipt receipt3 = new TblReceipt();
                    //receipt3.StrReceiptNo = pos.StrReceiptNo;
                    //receipt3.SelectOne();
                    //receipt3.MOutstandingAmount = Convert.ToDecimal(pos.MOutstandingAmount);
                    //receipt3.NCategoryID = ParentCategoryID;
                    //receipt3.Update();
                }

                if (pos.StrDeposit != "")
                {
                    receipt.StrReceiptNo = pos.StrDeposit;
                    DataTable tblDeposit = receipt.SelectOne();
                    if (tblDeposit.Rows.Count > 0)
                    {
                        receipt.FDeposit = false;
                        receipt.Update();

                    }
                    else
                    {
                        TblMemberPackage ConvertedPackage = new TblMemberPackage();
                        ConvertedPackage.NPackageID = ACMS.Convert.ToSqlInt32(pos.StrDeposit);
                        ConvertedPackage.NStatusID = 1;
                        ConvertedPackage.UpdateConvertedPackageStatus();
                    }
                }

                if (pos.ReceiptItemsTable.Select("", "", DataViewRowState.ModifiedCurrent).Length > 0)
                {
                    receiptEntriesTable = pos.ReceiptItemsTable.Copy();
                    receipt.SaveData(receiptEntriesTable);
                }
                //1503
                branchReceiptNoTable.Rows[0]["nReceiptNo"] = latestReceiptNo;

                SaveUpToDateReceiptNo(connProvider, branchReceiptNoTable);
                connProvider.CommitTransaction();

                if (pos.StrDeposit != "")
                {
                    TblReceipt receipt2 = new TblReceipt();
                    receipt2.StrReceiptNo = pos.StrDeposit;
                    receipt2.SelectOne();
                    receipt2.StrChildReceiptNo = receiptNo;
                    receipt2.Update();
                }
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connProvider.CurrentTransaction != null)
                    connProvider.CurrentTransaction.Dispose();
                if (connProvider.DBConnection != null)
                {
                    if (connProvider.DBConnection.State == ConnectionState.Open)
                        connProvider.DBConnection.Close();

                }
            }
        }