public JsonRs PaymentCheckOut(int checkinID, string Tdate, int khunggio)
        {
            var rs = new JsonRs();

            using (var db = _connectionData.OpenDbConnection())
            {
                using (var tran = db.OpenTransaction())
                {
                    try
                    {
                        DateTime dateToLeave;
                        DateTime.TryParse(Tdate, CultureInfo.GetCultureInfo("vi-vn"), DateTimeStyles.None, out dateToLeave);
                        var check = db.Select <tbl_SaleOrder>(x => x.CheckInID == checkinID).FirstOrDefault();

                        if (check != null)
                        {
                            rs.Status  = "00";
                            rs.Message = "Khách hàng này đã ghi nhận thanh toán lúc : " + check.DateCreated.GetValueOrDefault().ToString("dd/MM/yyyy mm:ss");
                            return(rs);
                        }
                        //lấy thời gian checkin
                        var checkin = db.Single <tbl_CheckIn>(x => x.Id == checkinID);
                        checkin.Leave_Date = dateToLeave;
                        checkin.KhungGio   = khunggio;
                        if (checkin.Leave_Date.Value.CompareTo(DateTime.Now.AddMinutes(10)) > 0)
                        {
                            return(JsonRs.create(-1, "Thời gian đi không thể quá thời gian hiện tại"));
                        }

                        if (checkin.Leave_Date.Value.CompareTo(checkin.Arrive_Date) <= 0)
                        {
                            return(JsonRs.create(-1, "Thời gian đi không thể nhỏ hơn hoặc bằng thời gian đến"));
                        }


                        //khi doi phong capnhat trang thai cu ve5 va them 1 thằng mới Stt=2
                        var itemcheckin = GetCustomerRoomByCheckInID(checkinID);
                        var usingroom   = db.Single <tbl_RoomUsing>(x => x.CheckInID == checkinID);
                        usingroom.status = 3;

                        //cập nhật bảng đặt phòng là đã checkout
                        /**/
                        var reservation = db.Select <tbl_Reservation_Room>().Where(e => e.BookingCode == itemcheckin.BookingCode).FirstOrDefault();
                        if (reservation != null)
                        {
                            reservation.ReservationStatus = ReservationStatus.CHECKOUT;
                            var reservationRel = db.Select <tbl_Reservation_Customer_Rel>().Where(e => e.reservationID == reservation.Id).FirstOrDefault();
                            if (reservationRel != null)
                            {
                                reservationRel.status = ReservationStatus.CHECKOUT;
                                db.Update(reservationRel);
                            }
                            db.Update(reservation);
                        }

                        #region lay lai chinh sach gia
                        var Estimate = new EstimatePrice1Service().caculatePrice(itemcheckin.SysHotelID.GetValueOrDefault(), khunggio,// itemcheckin.KhungGio.GetValueOrDefault(0),
                                                                                 itemcheckin.roomid.GetValueOrDefault(0), itemcheckin.Room_Type_ID.GetValueOrDefault(0), itemcheckin.Arrive_Date.GetValueOrDefault(),
                                                                                 dateToLeave, -1, itemcheckin.Number_People, itemcheckin.Number_Children);


                        #endregion
                        var room = db.Single <tbl_Room>(x => x.Id == usingroom.roomid);
                        room.status = 0;
                        checkin.ReservationStatus = 3;
                        if (itemcheckin == null)
                        {
                            rs.Status  = "00";
                            rs.Message = "Lỗi không tìm thấy khách hàng đang ở!";
                            return(rs);
                        }

                        //đến việc xử lý các dịch vụ
                        var product = GetListCustomerServices(checkinID);

                        ////trừ tồn kho tương ứng
                        ////tìm minibar ứng với phòng đang ở
                        //var queryStore=db.From<tbl_Store>().Where(e=>e.roomid==usingroom.roomid);
                        //var firstStore=db.Select(queryStore).FirstOrDefault();//nếu có kho

                        //if (firstStore == null)//nếu ko có kho thì trừ từ kho tổng
                        //{
                        //    firstStore = db.Select(db.From<tbl_Store>()
                        //        .Where(e => e.typeStore==1 && e.SysHotelID==comm.GetHotelId()))
                        //        .FirstOrDefault();
                        //}
                        //if (firstStore != null)//nếu  có kho thì tiến hành trừ
                        //{
                        //    //duyệt qua các sản phẩm
                        //    foreach (var oProduct in product)
                        //    {
                        //        var queryProductStore = db.From<tbl_StoreProduct>()
                        //            .Where(e => e.productid == oProduct.productid && e.storeid == firstStore.Id)
                        //            .OrderBy(e => e.quantity);
                        //        var firstProductInStore = db.Select(queryProductStore).FirstOrDefault();
                        //        if (firstProductInStore != null)
                        //        {
                        //            firstProductInStore.quantity = oProduct.Quantity - 1;
                        //            db.Update(firstProductInStore);
                        //        }
                        //    }
                        //}

                        var total     = product.Sum(x => x.TotalSale);
                        var totalroom = Estimate.Sum(x => x.price);
                        //var totalquantity = product.Sum(x => x.Quantity);

                        var saleorder = new tbl_SaleOrder()
                        {
                            SysHotelID    = itemcheckin.SysHotelID,
                            DateCreated   = DateTime.Now,
                            DatePayment   = DateTime.Now,
                            PaymentTypeID = 1,
                            TotalAmount   = total + totalroom,
                            CheckInID     = itemcheckin.CheckInID,
                            CustomerID    = itemcheckin.Id,
                            CreatorID     = 0,
                            CustomerCode  = "",
                            TypeOrder     = 1,
                            Deposit       = itemcheckin.Deposit.GetValueOrDefault(0),
                            Deduction     = itemcheckin.Deduction.GetValueOrDefault(0),
                            Discount      = itemcheckin.Discount.GetValueOrDefault(0),
                            Tax           = 0
                        };
                        db.Save(saleorder, true);
                        var list = product.Select(t => new tbl_SaleOrderDetail()
                        {
                            SysHotelID    = t.SysHotelID,
                            DateCreated   = DateTime.Now,
                            DatePayment   = DateTime.Now,
                            PaymentTypeID = 1,
                            TotalAmount   = t.TotalSale,
                            Price         = 0,
                            AmountNoTax   = 0,
                            Tax           = 0,
                            item          = "",
                            catalogitem   = "",
                            CreatorID     = 0,
                            quantity      = t.Quantity,
                            itemid        = t.productid,
                            StoreID       = 0,
                            TypeOrder     = 2, // dịch vụ bằng 2 giá phòng bằng 1
                            catalogitemid = t.productid,
                            OrderID       = saleorder.Id
                        }).ToList();


                        foreach (var price in Estimate)
                        {
                            list.Add(new tbl_SaleOrderDetail()
                            {
                                SysHotelID    = itemcheckin.SysHotelID,
                                DateCreated   = DateTime.Now,
                                DatePayment   = DateTime.Now,
                                PaymentTypeID = 1,
                                TotalAmount   = price.price,
                                Price         = price.quantiy,
                                AmountNoTax   = 0,
                                Tax           = 0,
                                item          = price.dtFrom.ToString("dd/MM/yyyy hh:mm") + "|" + price.dtTo.ToString("dd/MM/yyyy hh:mm"),
                                catalogitem   = "",
                                CreatorID     = 0,
                                quantity      = price.quantiy,
                                itemid        = price.roomid,
                                StoreID       = 0,
                                TypeOrder     = 1, // dịch vụ bằng 2 giá phòng bằng 1
                                catalogitemid = price.pricePolicyId,
                                OrderID       = saleorder.Id,
                            });
                        }
                        if (list.Count > 0)
                        {
                            db.InsertAll(list);
                        }
                        db.Update(room);
                        db.Update(usingroom);
                        db.Update(checkin);
                        tran.Commit();
                        rs.Status  = "01";
                        rs.Message = "Thanh toán thành công cho khách hàng :" + itemcheckin.CustomerName;
                        return(rs);
                    }
                    catch (Exception e)
                    {
                        rs.Status  = "00";
                        rs.Message = "Thanh toán thất bại. Vui lòng thực hiện lại!";
                        return(rs);
                    }
                }
            }
        }
        public JsonRs PayBillCheckOut(int checkinID, string Tdate, int khunggio)
        {
            var rs = new JsonRs();

            using (var db = _connectionData.OpenDbConnection())
            {
                using (var tran = db.OpenTransaction())
                {
                    try
                    {
                        DateTime dateToLeave;
                        DateTime.TryParse(Tdate, CultureInfo.GetCultureInfo("vi-vn"), DateTimeStyles.None, out dateToLeave);
                        //danh sách giá phòng

                        var check = db.Select <tbl_SaleOrder>(x => x.CheckInID == checkinID).FirstOrDefault();
                        if (check != null)
                        {
                            rs.Status  = "00";
                            rs.Message = "Khách hàng này đã ghi nhận thanh toán lúc : " + check.DateCreated.GetValueOrDefault().ToString("dd/MM/yyyy mm:ss");
                            return(rs);
                        }
                        //khi doi phong capnhat trang thai cu ve5 va them 1 thằng mới Stt=2
                        var itemcheckin = GetCustomerRoomByCheckInID(checkinID);

                        #region lay lai chinh sach gia
                        var Estimate = new EstimatePrice1Service().caculatePrice(itemcheckin.SysHotelID.GetValueOrDefault(), khunggio,// itemcheckin.KhungGio.GetValueOrDefault(0),
                                                                                 itemcheckin.roomid.GetValueOrDefault(0), itemcheckin.Room_Type_ID.GetValueOrDefault(0), itemcheckin.Arrive_Date.GetValueOrDefault(),
                                                                                 dateToLeave, -1, itemcheckin.Number_People, itemcheckin.Number_Children);
                        #endregion
                        var usingroom = db.Single <tbl_RoomUsing>(x => x.CheckInID == checkinID);
                        usingroom.status = 3;
                        var checkin = db.Single <tbl_CheckIn>(x => x.Id == checkinID);
                        checkin.Leave_Date = dateToLeave;
                        checkin.KhungGio   = khunggio;
                        var room = db.Single <tbl_Room>(x => x.Id == usingroom.roomid);
                        room.status = 0;
                        checkin.ReservationStatus = 3;
                        if (itemcheckin == null)
                        {
                            rs.Status  = "00";
                            rs.Message = "Lỗi không tìm thấy khách hàng đang ở!";
                            return(rs);
                        }
                        var product = GetListCustomerServices(checkinID);

                        var total     = product.Sum(x => x.TotalSale);
                        var totalroom = Estimate.Sum(x => x.price);
                        //var totalquantity = product.Sum(x => x.Quantity);
                        #region thêm hóa đơn theo dịch vụ và giá phòng

                        // giá phòng
                        var saleorderrom = new tbl_SaleOrder()
                        {
                            SysHotelID    = itemcheckin.SysHotelID,
                            DateCreated   = DateTime.Now,
                            DatePayment   = DateTime.Now,
                            PaymentTypeID = 1,
                            TotalAmount   = totalroom,
                            CheckInID     = itemcheckin.CheckInID,
                            CustomerID    = itemcheckin.Id,
                            CreatorID     = 0,
                            CustomerCode  = "",
                            TypeOrder     = 1,
                            Tax           = 0,
                            Deposit       = itemcheckin.Deposit.GetValueOrDefault(0),
                            Deduction     = itemcheckin.Deduction.GetValueOrDefault(0),
                            Discount      = itemcheckin.Discount.GetValueOrDefault(0),
                        };
                        // dịch vụ
                        var orderService = new tbl_SaleOrder()
                        {
                            SysHotelID    = itemcheckin.SysHotelID,
                            DateCreated   = DateTime.Now,
                            DatePayment   = DateTime.Now,
                            PaymentTypeID = 1,
                            TotalAmount   = total,
                            CheckInID     = itemcheckin.CheckInID,
                            CustomerID    = itemcheckin.Id,
                            CreatorID     = 0,
                            CustomerCode  = "",
                            TypeOrder     = 2,
                            Tax           = 0
                        };
                        // giá phòng
                        db.Save(saleorderrom, true);
                        // dịch vụ
                        db.Save(orderService, true);

                        #endregion
                        #region chi tiết hóa đơn theo dịch vụ và giá phòng
                        // giá phòng
                        var listroomdetail = Estimate.Select(t => new tbl_SaleOrderDetail()
                        {
                            SysHotelID    = itemcheckin.SysHotelID,
                            DateCreated   = DateTime.Now,
                            DatePayment   = DateTime.Now,
                            PaymentTypeID = 1,
                            TotalAmount   = t.price,
                            Price         = 0,
                            AmountNoTax   = 0,
                            Tax           = 0,
                            item          = "",
                            catalogitem   = "",
                            CreatorID     = 0,
                            quantity      = t.quantiy,
                            itemid        = t.roomid,
                            StoreID       = 0,
                            TypeOrder     = 2, // dịch vụ bằng 2 giá phòng bằng 1
                            catalogitemid = 0,
                            OrderID       = saleorderrom.Id
                        }).ToList();
                        // dịch vụ
                        var listservicedetail = product.Select(t => new tbl_SaleOrderDetail()
                        {
                            SysHotelID    = t.SysHotelID,
                            DateCreated   = DateTime.Now,
                            DatePayment   = DateTime.Now,
                            PaymentTypeID = 1,
                            TotalAmount   = t.TotalSale,
                            Price         = t.Quantity,
                            AmountNoTax   = 0,
                            Tax           = 0,
                            item          = "",
                            catalogitem   = "",
                            CreatorID     = 0,
                            quantity      = t.Quantity,
                            itemid        = t.productid,
                            StoreID       = 0,
                            TypeOrder     = 2, // dịch vụ bằng 2 giá phòng bằng 1
                            catalogitemid = t.productid,
                            OrderID       = orderService.Id
                        }).ToList();

                        // giá phòng
                        db.InsertAll(listroomdetail);
                        // dịch vụ
                        db.InsertAll(listservicedetail);
                        #endregion
                        db.Update(room);
                        db.Update(usingroom);
                        db.Update(checkin);
                        tran.Commit();
                        rs.Status  = "01";
                        rs.Message = "Thanh toán thành công cho khách hàng :" + itemcheckin.CustomerName;
                        return(rs);
                    }
                    catch (Exception e)
                    {
                        rs.Status  = "00";
                        rs.Message = "Thanh toán thất bại. Vui lòng thực hiện lại!";
                        return(rs);
                    }
                }
            }
        }
Exemple #3
0
        public tbl_SaleOrder InsertOrder(ExportRetailModel model)
        {
            try
            {
                using (var db = _connectionData.OpenDbConnection())
                {
                    var sysHotel = comm.GetHotelId();
                    #region Khách hàng

                    var sysCustomer = new tbl_Customer
                    {
                        Name           = model.CustomerName,
                        Phone          = model.CustomerPhone,
                        CreateDate     = DateTime.Now,
                        Mobile         = model.CustomerPhone,
                        SysHotelID     = sysHotel,
                        CreateBy       = comm.GetUserId(),
                        IdentifyNumber = model.CustomerPhone,
                        Status         = true
                    };
                    var customerId = (int)db.Insert(sysCustomer, selectIdentity: true);
                    #endregion
                    #region Insert đơn
                    var objInsert = new tbl_SaleOrder
                    {
                        CustomerID    = customerId,
                        SysHotelID    = sysHotel,
                        AmountNoTax   = model.TotalAmount,
                        Tax           = model.SurchargeAmount,
                        TotalAmount   = (model.TotalAmount - model.SurchargeAmount),
                        DateCreated   = DateTime.Now,
                        DatePayment   = DateTime.ParseExact(model.SPayTime, "dd/MM/yyyy hh:mm:ss", null),
                        PaymentTypeID = null,
                        TypeOrder     = 1,
                        CreatorID     = comm.GetUserId()
                    };
                    var iTblSaleOrderId = (int)db.Insert(objInsert, selectIdentity: true);
                    #endregion
                    #region Chi tiết đơn
                    if (model.ListRetailDetail != null)
                    {
                        foreach (var detail in model.ListRetailDetail)
                        {
                            var objDetail = new tbl_SaleOrderDetail
                            {
                                SysHotelID    = sysHotel,
                                OrderID       = iTblSaleOrderId,
                                catalogitem   = detail.ProductName,
                                item          = detail.ProductName,
                                itemid        = detail.ProductId,
                                catalogitemid = detail.ProductId,
                                quantity      = detail.Quantity,
                                Price         = int.Parse(detail.Price + ""),
                                AmountNoTax   = detail.Price,
                                Tax           = 0,
                                TotalAmount   = (detail.Price * detail.Quantity),
                                DateCreated   = DateTime.Now,
                                DatePayment   = DateTime.ParseExact(model.SPayTime, "dd/MM/yyyy hh:mm:ss", null),
                                TypeOrder     = 1,
                                CreatorID     = comm.GetUserId(),
                                StoreID       = 0
                            };
                            db.Insert(objDetail, selectIdentity: true);
                        }
                    }
                    #endregion
                    return(objInsert);
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }