Exemple #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);
                });
            }
        }
Exemple #2
0
        private static BookingOrder Parse(IDataReader reader)
        {
            var ps = new PaymentRecordService();

            reader.Read();
            var order = new BookingOrder
            {
                Id           = reader["Id"].ToString(),
                CheckinDate  = DateTime.Parse(reader["CheckinDate"].ToString()),
                CheckOutDate = DateTime.Parse(reader["CheckoutDate"].ToString()),
                Note         = reader["Note"]?.ToString() ?? string.Empty,
                Status       = reader["Status"].ToString(),
                TotalGuest   = int.Parse(reader["TotalGuest"].ToString()),
                StayLength   = reader["StayLength"].ToString(),
                HouseId      = reader["HouseId"].ToString(),
                PaymentCycle = new PaymentCycle(reader["PaymentCycle"].ToString())
            };

            order.Deposit = new Deposit
            {
                OrderId = order.Id,
                Id      = reader["DepositId"].ToString(),
                Amount  = reader["DepositAmount"].ToString(),
                Unit    = reader["DepositUnit"].ToString(),
                Date    = DateTime.Parse(reader["DepositDate"].ToString())
            };

            reader.NextResult();
            order.Customers = new List <Customer>();
            while (reader.Read())
            {
                var customer = CustomerService.GetById(reader["CustomerId"].ToString());
                order.Customers.Add(customer);
            }

            reader.NextResult();
            order.Rooms = new List <Room>();
            while (reader.Read())
            {
                var room = RoomService.GetById(reader["RoomId"].ToString());
                order.Rooms.Add(room);
            }

            order.Discounts = ps.GetByOrderId(order.Id, "Order-Discount");
            order.Payments  = ps.GetByOrderId(order.Id, "Order-Payment");

            return(order);
        }
Exemple #3
0
        public static void Delete(string id)
        {
            var ms = new MoneySourceService();
            var ps = new PaymentRecordService();

            SqlHelper.StartTransaction(ConfigManager.ConnectionString, (connection, transaction) =>
            {
                var order = GetById(id);

                var msDict = new Dictionary <string, MoneySource>();
                var pmList = new List <PaymentRecord>();
                pmList.AddRange(order.Discounts);
                pmList.AddRange(order.Payments);
                foreach (var item in pmList)
                {
                    if (!msDict.ContainsKey(item.MoneySourceId))
                    {
                        msDict.Add(item.MoneySourceId, ms.GetById(item.MoneySourceId));
                    }
                }

                foreach (var item in pmList)
                {
                    ReverseSource(msDict[item.MoneySourceId], item);
                    ps.Delete(item.Id, connection, transaction);
                }

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

                SqlHelper.ExecuteCommand(connection, transaction, CommandType.StoredProcedure, SP_ORDER_DELETE,
                                         new[]
                {
                    new SqlParameter("@id", id),
                });
            });
        }
Exemple #4
0
        public static BookingOrder Create(string mode, BookingOrder order)
        {
            var ms = new MoneySourceService();
            var ps = new PaymentRecordService();
            var id = IdHelper.Generate();

            order.Id = "o-" + id;

            if (order.Deposit == null)
            {
                order.Deposit = new Deposit();
            }

            order.Deposit.Id      = "d-" + IdHelper.Generate();
            order.Deposit.Date    = DateTime.Now;
            order.Deposit.OrderId = order.Id;

            order.TotalGuest = order.Customers.Count;

            order.Status = mode.Equals("Checkin", StringComparison.CurrentCultureIgnoreCase) ? "Active" : "Reserved";


            SqlHelper.StartTransaction(ConfigManager.ConnectionString, (connection, transaction) =>
            {
                // insert order
                SqlHelper.ExecuteCommand(connection, transaction, CommandType.StoredProcedure, SP_ORDER_INSERT, CreateParams(order));

                // insert customer
                foreach (var customer in order.Customers)
                {
                    string spName;
                    if (customer.Id.StartsWith("customer-"))
                    {
                        customer.Id = "c-" + IdHelper.Generate();
                        spName      = SP_CUSTOMER_INSERT;
                    }
                    else
                    {
                        spName = SP_CUSTOMER_UPDATE;
                    }

                    SqlHelper.ExecuteCommand(connection, transaction, CommandType.StoredProcedure, spName, CreateParams(customer));
                    SqlHelper.ExecuteCommand(connection, transaction, CommandType.StoredProcedure, SP_ORDER_CUSTOMER_INSERT,
                                             new[]
                    {
                        new SqlParameter("@orderId", order.Id),
                        new SqlParameter("@customerId", customer.Id),
                    });
                }

                // insert room
                foreach (var room in order.Rooms)
                {
                    SqlHelper.ExecuteCommand(connection, transaction, CommandType.StoredProcedure, SP_ORDER_ROOM_INSERT,
                                             new []
                    {
                        new SqlParameter("@orderId", order.Id),
                        new SqlParameter("@roomId", room.Id),
                    });
                }
                // insert deposit
                SqlHelper.ExecuteCommand(connection, transaction, CommandType.StoredProcedure, SP_DEPOSIT_INSERT, CreateParams(order.Deposit, order.Id));


                var dict = new Dictionary <string, MoneySource>();
                foreach (var d in order.Discounts)
                {
                    if (!dict.ContainsKey(d.MoneySourceId))
                    {
                        dict.Add(d.MoneySourceId, ms.GetById(d.MoneySourceId));
                    }
                }
                foreach (var p in order.Payments)
                {
                    if (!dict.ContainsKey(p.MoneySourceId))
                    {
                        dict.Add(p.MoneySourceId, ms.GetById(p.MoneySourceId));
                    }
                }

                // insert discount
                foreach (var discount in order.Discounts)
                {
                    discount.Date    = DateTime.Now;
                    discount.OrderId = order.Id;
                    discount.Type    = "Order-Discount";

                    var source = dict[discount.MoneySourceId];
                    UpdateSource(source, discount);
                    ps.Insert(discount, connection, transaction);
                }


                // insert payment
                foreach (var payment in order.Payments)
                {
                    payment.OrderId = order.Id;
                    payment.Date    = DateTime.Now;
                    payment.Type    = "Order-Payment";
                    var source      = dict[payment.MoneySourceId];
                    UpdateSource(source, payment);
                    ps.Insert(payment, connection, transaction);
                }

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

            NotificationService.Insert(order);

            return(order);
        }
Exemple #5
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);
            }
        }