internal static void UpdateAfterInsertNewPCode(PacificCode newPacificCode)
        {

            // Lỗi: cần kiểm tra lại khi nào Store Null, nếu Null thì ko cho lưu
            Store existStore = mpdb.Stores.Where(s => s.ID == newPacificCode.StoreID).Single();
            
            if (existStore != null)
            {
                if (existStore.NumberSales == null)
                {
                    existStore.NumberSales = 1;
                }
                else 
                {
                    existStore.NumberSales++;
                }

                if (existStore.TotalSales == null)
                {
                    existStore.TotalSales = newPacificCode.ActualAmount;
                }
                else
                {
                    existStore.TotalSales += newPacificCode.ActualAmount;
                }
            }

            mpdb.SubmitChanges();
        }
        internal static void UpdateAfterInsertNewPCode(PacificCode newPacificCode)
        {
            Customer existCustomer = mpdb.Customers.Where(c => c.ID == newPacificCode.CustomerID).Single();

            // NumberTransaction
            if (existCustomer.NumberTransaction == null)
            {
                existCustomer.NumberTransaction = 1;
            }
            else
            {
                existCustomer.NumberTransaction++;
            }

            // TotalAmount
            if (existCustomer.TotalAmount == null)
            {
                existCustomer.TotalAmount = newPacificCode.ActualAmount;
            }
            else
            {
                existCustomer.TotalAmount += newPacificCode.ActualAmount;
            }

            // Xác nhận lưu 
            mpdb.SubmitChanges();
        }
 internal static int AddNew(PacificCode pacificCode)
 {
     int resultID = 0;
     mpdb.PacificCodes.InsertOnSubmit(pacificCode);            
     mpdb.SubmitChanges();
     resultID = pacificCode.ID;
     return resultID;
 }
        internal static int AddNew(PacificCode newPCode)
        {
            // Lưu thông tin liên quan lên Store, Customer   
            // Sẽ sửa cho tự động lưu..(clr-trg)..

            StoreDAO.UpdateAfterInsertNewPCode(newPCode);
            CustomerDAO.UpdateAfterInsertNewPCode(newPCode);

            return PacificCodeDAO.AddNew(newPCode);
        }
        internal static void AddNew(PacificCode newSuccessPacificCode)
        {
            Transaction newTranSaction = new Transaction();
            
            newTranSaction.CreateDate = newSuccessPacificCode.Date;
            newTranSaction.CustomerID = newSuccessPacificCode.CustomerID;
            newTranSaction.StoreID = newSuccessPacificCode.StoreID;
            newTranSaction.Amount = newSuccessPacificCode.InitialAmount;
            newTranSaction.Origine = "Buy MP";
            //newTranSaction.StatusID = 1; // StatusID = 1 : Status = '11'

            mpdb.Transactions.InsertOnSubmit(newTranSaction);
            mpdb.SubmitChanges();

        }
        internal static PacificCode GenerateNewCode()
        {
            Random randomNumer = new Random();
            PacificCode newPCode = new PacificCode();

            newPCode.PacificCode1 = "";
            
            for (int i = 0; i<=15; i++)
            {
                arrCode[i] = randomNumer.Next(10);
                newPCode.PacificCode1 += arrCode[i].ToString();
            }

            return newPCode;
        }
        internal static string GenSucessCreatePacificCodeMessage(PacificCode newPCode)
        { 
            // Load từ tập tin XML, thay vào các biến giá trị
            // Tách chuỗi sPacificCode thành định dạng abcd-efgh-ijkm-mnop
            string sPacificCode = newPCode.PacificCode1;
            

            string sFormatPacificCode = sPacificCode.Substring(0, 4) + "-"
                + sPacificCode.Substring(4, 4) + "-" + sPacificCode.Substring(8, 4) + "-"
                + sPacificCode.Substring(12, 4);

            string sResult = "Bạn đã mua thành công một PacificCode: " + sFormatPacificCode
                + " có giá trị  " + newPCode.InitialAmount + " VND. Tài khoản này có giá trị đến ngày " + newPCode.Date;
            return sResult;                
        }
        internal static PacificCode getNewPacificCode(int storeId, int customerId, int amount)
        {
            PacificCode newPacificCode = new PacificCode();

            //string xmlRuleFile = Directory.GetCurrentDirectory() + "\\App_Data\\PacificCodeR.xml";
            string xmlRuleFile = AppDomain.CurrentDomain.BaseDirectory + "\\App_Data\\PacificCodeR.xml";

            bool bGetNewPacificCode = false;
            do
            {
                // Tạo Random
                // newPacificCode = PacificCodeBUS.GenerateNewCode(); 
                
                // Tạo theo Rule
                newPacificCode = PacificCodeBUS.GenerateNewCode(xmlRuleFile); 

                // Nếu đã tồn tại(tỉ lệ xảy ra là 1/10^11)
                bGetNewPacificCode = !PacificCodeBUS.checkExist(newPacificCode.PacificCode1);

            } while (bGetNewPacificCode == false);

            // 
            newPacificCode.StoreID = storeId;
            newPacificCode.CustomerID = customerId;
            newPacificCode.InitialAmount = amount;
            newPacificCode.ActualAmount = amount;

            newPacificCode.Date = DateTime.Now;

            // Xác định ngày hết hạn, mặc định là 1 năm từ ngày mua
            int iYear = DateTime.Now.Year + 1;
            int iMonth = DateTime.Now.Month;
            int iDay = DateTime.Now.Day;

            newPacificCode.ExpireDate = new DateTime(iYear,iMonth,iDay);

            PacificCodeBUS.AddNew(newPacificCode);

            return newPacificCode;            
        }
		private void detach_PacificCodes(PacificCode entity)
		{
			this.SendPropertyChanging();
			entity.Customer = null;
		}
 partial void DeletePacificCode(PacificCode instance);
 partial void UpdatePacificCode(PacificCode instance);
 partial void InsertPacificCode(PacificCode instance);
		private void attach_PacificCodes(PacificCode entity)
		{
			this.SendPropertyChanging();
			entity.Store = this;
		}
		private void detach_PacificCodes(PacificCode entity)
		{
			this.SendPropertyChanging();
			entity.PacificCodeStatus = null;
		}