Beispiel #1
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),
                });
            });
        }
Beispiel #2
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);
        }
Beispiel #3
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);
            }
        }