예제 #1
0
        public void UpdatePayment(PaymentRecord payment, SqlConnection con, SqlTransaction trans)
        {
            var ps = new PaymentRecordService();

            var source = GetById(payment.MoneySourceId);
            var amount = Convert.ToSingle(payment.Amount);

            var original       = ps.GetById(payment.Id);
            var originalAmount = Convert.ToSingle(original.Amount);

            if (payment.Type.Equals("Expense", StringComparison.CurrentCultureIgnoreCase) ||
                payment.Type.Equals("Order-Discount", StringComparison.CurrentCultureIgnoreCase))
            {
                amount = -amount;
            }

            if (original.Type.Equals("Income", StringComparison.CurrentCultureIgnoreCase) ||
                original.Type.Equals("Order-Payment", StringComparison.CurrentCultureIgnoreCase))
            {
                originalAmount = -originalAmount;
            }

            if (payment.Unit.Equals("VND"))
            {
                source.BalanceVND += amount;
            }
            else
            {
                source.BalanceUSD += amount;
            }

            if (original.Unit.Equals("VND"))
            {
                source.BalanceVND += originalAmount;
            }
            else
            {
                source.BalanceUSD += originalAmount;
            }


            if (con != null && trans != null)
            {
                ps.Update(payment, con, trans);

                _adapter.Update(source, con, trans);
            }
            else
            {
                SqlHelper.StartTransaction(ConfigManager.ConnectionString, (connection, transaction) =>
                {
                    ps.Update(payment, connection, transaction);

                    _adapter.Update(source, con, trans);
                });
            }
        }
예제 #2
0
        private static void UpdatePaymentForOrder(BookingOrder order, BookingOrder originalOrder, SqlConnection connection, SqlTransaction transaction)
        {
            var ms = new MoneySourceService();
            var ps = new PaymentRecordService();

            var msDict = new Dictionary <string, MoneySource>();

            foreach (var discount in order.Discounts)
            {
                if (!msDict.ContainsKey(discount.MoneySourceId))
                {
                    msDict.Add(discount.MoneySourceId, ms.GetById(discount.MoneySourceId));
                }
            }
            foreach (var payment in order.Payments)
            {
                if (!msDict.ContainsKey(payment.MoneySourceId))
                {
                    msDict.Add(payment.MoneySourceId, ms.GetById(payment.MoneySourceId));
                }
            }
            foreach (var discount in originalOrder.Discounts.Where(d => !order.Discounts.Exists(d1 => d1.Id == d.Id)))
            {
                if (!msDict.ContainsKey(discount.MoneySourceId))
                {
                    msDict.Add(discount.MoneySourceId, ms.GetById(discount.MoneySourceId));
                }
            }
            foreach (var payment in originalOrder.Payments.Where(p => !order.Payments.Exists(p1 => p1.Id == p.Id)))
            {
                if (!msDict.ContainsKey(payment.MoneySourceId))
                {
                    msDict.Add(payment.MoneySourceId, ms.GetById(payment.MoneySourceId));
                }
            }

            foreach (var discount in order.Discounts)
            {
                if (discount.Id.StartsWith("discount-"))
                {
                    var id = IdHelper.Generate();
                    discount.Id      = "d-" + id;
                    discount.Date    = DateTime.Now;
                    discount.OrderId = order.Id;
                    ps.Insert(discount, connection, transaction);
                    UpdateSource(msDict[discount.MoneySourceId], discount);
                }
                else
                {
                    ps.Update(discount, connection, transaction);
                    var source  = msDict[discount.MoneySourceId];
                    var oldItem = ps.GetById(discount.Id);
                    ReverseSource(source, oldItem);
                    UpdateSource(source, discount);
                }
            }
            foreach (var discount in originalOrder.Discounts.Where(d => !order.Discounts.Exists(d1 => d1.Id == d.Id)))
            {
                var source = msDict[discount.MoneySourceId];
                ReverseSource(source, discount);
                ps.Delete(discount.Id, connection, transaction);
            }

            // update payment
            foreach (var payment in order.Payments)
            {
                if (payment.Id.StartsWith("payment-"))
                {
                    var id = IdHelper.Generate();
                    payment.Id      = "p-" + id;
                    payment.Date    = DateTime.Now;
                    payment.OrderId = order.Id;
                    ps.Insert(payment, connection, transaction);
                    UpdateSource(msDict[payment.MoneySourceId], payment);
                }
                else
                {
                    ps.Update(payment, connection, transaction);
                    var source  = msDict[payment.MoneySourceId];
                    var oldItem = ps.GetById(payment.Id);
                    ReverseSource(source, oldItem);
                    UpdateSource(source, payment);
                }
            }
            foreach (var payment in originalOrder.Payments.Where(p => !order.Payments.Exists(p1 => p1.Id == p.Id)))
            {
                var source = msDict[payment.MoneySourceId];
                ReverseSource(source, payment);
                ps.Delete(payment.Id, connection, transaction);
            }

            foreach (var source in msDict.Values)
            {
                ms.Update(source, connection, transaction);
            }
        }