Пример #1
0
        public void Update(Payment payment)
        {
            var transactions = payment.BudgetTransactions.ToList();

            using (var dbTransaction = _db.Database.BeginTransaction())
            {
                try
                {
                    var contractor = payment.Contractor;
                    payment = new Payment(payment);

                    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;
                    }

                    // Add payment to context
                    var paymentRepo = new PaymentRepository(_db);
                    paymentRepo.Update(payment);
                    paymentRepo.Save();

                    // Update transaction
                    //var transmanager = new BudgetTransactionManager(_db);
                    //transmanager.UpdateByPayment(payment, transactions);

                    if (transactions != null)
                    {
                        // DELTE Old payment
                        using (SqlCommand cmd = new SqlCommand("DELETE FROM BudgetTransaction WHERE PaymentID = @PaymentID", _db.Database.Connection as SqlConnection, _db.Database.CurrentTransaction.UnderlyingTransaction as SqlTransaction))
                        {
                            cmd.Parameters.AddWithValue("@PaymentID", payment.PaymentID);
                            cmd.ExecuteNonQuery();
                        }

                        // Insert New Item
                        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.ModifiedBy == "Anonymous")
                    {
                        throw new Exception("พบข้อผิดพลาด: กรุณาเข้าสู่ระบบใหม่อีกครั้ง");
                    }

                    dbTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbTransaction.Rollback();
                    throw ex;
                }
            }
        }
Пример #2
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;
                }
            }
        }