示例#1
0
 public ShoppingStore()
 {
     country        = "Local";
     shoppingCart   = new ShoppingCart();
     paymentCounter = new PaymentCounter(country);
     storeShelf     = new StoreShelf();
 }
示例#2
0
 /// <summary>
 /// Shopping store constructor
 /// </summary>
 public Store()
 {
     country        = "Local";
     shoppingCart   = new ShoppingCart();
     paymentCounter = new PaymentCounter(country);
     storeInventory = new Inventory();
 }
示例#3
0
        private string PreparePaymentCounterData(PaymentCounter counter)
        {
            StringBuilder counterData = new StringBuilder();

            counterData.Append(counterHeader)
            .Append(delimiterL3).Append(counter.CounterId)
            .Append(delimiterL3).Append(counter.NewValue);

            //can optimize via return char[]
            return(counterData.ToString());
        }
        PaymentCounter PrepareCounter(string[] parts)
        {
            var counter = new PaymentCounter();

            counter.CounterId = parts[1];
            counter.OldValue  = Decimal.Parse(parts[2]
                                              .Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)
                                              .Replace(",", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)
                                              );
            return(counter);
        }
        public static PaymentCounter ToModel(this PaymentCounterViewModel viewModel)
        {
            var model = new PaymentCounter();

            model.CounterId  = viewModel.CounterId;
            model.IsRequired = viewModel.IsRequired;
            model.Name       = viewModel.Name;
            model.NewValue   = viewModel.NewValue;
            model.OldValue   = viewModel.OldValue;
            model.Order      = viewModel.Order;
            model.PaymentId  = viewModel.PaymentId;
            return(model);
        }
        public static PaymentCounterViewModel ToViewModel(this PaymentCounter model)
        {
            var viewModel = new PaymentCounterViewModel();

            viewModel.CounterId  = model.CounterId;
            viewModel.IsRequired = model.IsRequired;
            viewModel.Name       = model.Name;
            viewModel.NewValue   = model.NewValue;
            viewModel.OldValue   = model.OldValue;
            viewModel.Order      = model.Order;
            viewModel.PaymentId  = model.PaymentId;
            return(viewModel);
        }
示例#7
0
        public Payment Add(Payment payment)
        {
            var transactions = payment.BudgetTransactions;

            using (var dbTransaction = _db.Database.BeginTransaction())
            {
                try
                {
                    // 1. Inital Payment data
                    var contractor = payment.Contractor;
                    payment = new Payment(payment);
                    payment.PrepareToSave();
                    if (payment.Type == PaymentType.Contractor)
                    {
                        var ctManager = new ContractorManager(_db);
                        if (contractor.ID == Guid.Empty)
                        {
                            contractor = ctManager.Add(payment.CostCenterID, contractor.Name);
                        }
                        payment.ContractorID = contractor.ID;
                    }


                    // 2. Add payment to context
                    var paymentRepo = new PaymentRepository(_db);
                    payment.Sequence = _db.Database.SqlQuery <int>("" +
                                                                   @"
                            SELECT COUNT(*) + 1 AS Sequence 
                            FROM Payment 
                            where CostCenterID = @CostCenterID   
                                and year = @Year
                        "
                                                                   , new SqlParameter("CostCenterID", payment.CostCenterID)
                                                                   , new SqlParameter("Year", payment.Year)
                                                                   ).FirstOrDefault();

                    //payment.Sequence = paymentRepo.Get()
                    //    .Where(p => p.CostCenterID == payment.CostCenterID && p.Year == payment.Year)
                    //    .ToList()
                    //    .Count + 1;

                    // Get payment counter info
                    PaymentCounter pcounter = _db.PaymentCounters.Where(c => c.CostCenterID == payment.CostCenterID && c.Year == payment.Year).FirstOrDefault();
                    if (pcounter == null)
                    {
                        //if not exist, then add new one.
                        var shortName = _db.CostCenters.Where(c => c.CostCenterID == payment.CostCenterID).Select(s => s.ShortName).FirstOrDefault();
                        var split     = shortName.Split(new char[] { ' ' });
                        var shortCode = split[split.Length - 1].Trim(new char[] { ' ', '.' });

                        pcounter = new PaymentCounter()
                        {
                            CostCenterID = payment.CostCenterID,
                            Year         = payment.Year,
                            ShortCode    = shortCode,
                            Number       = 1
                        };

                        _db.PaymentCounters.Add(pcounter);
                        _db.SaveChanges();
                    }
                    else
                    {
                        pcounter.Number += 1;
                        pcounter.NewModifyTimeStamp();
                        _db.Entry(pcounter).State = EntityState.Modified;
                        _db.SaveChanges();
                    }
                    payment.PaymentNo = pcounter.ShortCode + "-" + pcounter.Number.ToString().PadLeft(4, '0');
                    paymentRepo.Add(payment);
                    paymentRepo.Save();

                    // 3. Add each budget transaction by BudgetTransactionManager
                    //BudgetTransactionManager transactionManager = new BudgetTransactionManager(_db);
                    //if (transactions != null)
                    //{
                    //    foreach (var item in transactions)
                    //    {
                    //        item.PaymentID = payment.PaymentID;
                    //        transactionManager.AddOrUpdate(item);
                    //    }
                    //}

                    if (transactions != null)
                    {
                        foreach (var item in transactions)
                        {
                            item.PaymentID = payment.PaymentID;

                            //prepare to create
                            item.BudgetTransactionID = Guid.NewGuid();
                            item.Type   = TransactionType.Transaction;
                            item.Status = RecordStatus.Active;
                            item.NewCreateTimeStamp();

                            using (SqlCommand cmd = new SqlCommand(DbCmdTxt.cmd_upsert_budget_transaction, _db.Database.Connection as SqlConnection, _db.Database.CurrentTransaction.UnderlyingTransaction as SqlTransaction))
                            {
                                cmd.Parameters.AddWithValue("@Id", item.BudgetTransactionID);
                                cmd.Parameters.AddWithValue("@BudgetId", item.BudgetID);
                                cmd.Parameters.AddWithValue("@PaymentId", item.PaymentID);
                                cmd.Parameters.AddWithValue("@Description", item.Description ?? string.Empty);
                                cmd.Parameters.AddWithValue("@Amount", item.Amount);
                                cmd.Parameters.AddWithValue("@PreviousAmount", item.PreviousAmount);
                                cmd.Parameters.AddWithValue("@RemainAmount", item.RemainAmount);
                                cmd.Parameters.AddWithValue("@Type", item.Type);
                                cmd.Parameters.AddWithValue("@Status", item.Status);
                                cmd.Parameters.AddWithValue("@CreatedBy", item.CreatedBy);
                                cmd.Parameters.AddWithValue("@CreatedAt", item.CreatedAt);
                                cmd.Parameters.AddWithValue("@ModifiedBy", item.ModifiedBy);
                                cmd.Parameters.AddWithValue("@ModifiedAt", item.ModifiedAt);
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }

                    if (payment.CreatedBy == "Anonymous")
                    {
                        throw new Exception("พบข้อผิดพลาด: กรุณาเข้าสู่ระบบใหม่อีกครั้ง");
                    }

                    dbTransaction.Commit();
                    return(payment);
                }
                catch (Exception ex)
                {
                    dbTransaction.Rollback();
                    throw ex;
                }
            }
        }