public void RecordPoint(RePayPoints RePayPoints)
 {
     //Lưu lịch sử điểm
     HistoryPointController.CreatePoint(RePayPoints.CustomerId, RePayPoints.Id, "RePayPoints", 0, RePayPoints.TotalPoint);
     //Ghi nhận điểm
     MemberCardController.SynchUpdateTypeCrad(RePayPoints.CustomerId.Value, 0, RePayPoints.TotalPoint, null);
 }
 public void DeletePoint(RePayPoints RePayPoints)
 {
     //xóa lịch sử điểm
     HistoryPointController.DeletedPoint(RePayPoints.Id, "RePayPoints");
     //Ghi nhận điểm lại
     MemberCardController.SynchUpdateTypeCrad(RePayPoints.CustomerId.Value, 0, RePayPoints.TotalPoint, null, false);
 }
        public ActionResult Create(RePayPointsViewModel model)
        {
            if (ModelState.IsValid && model.DetailList.Count != 0)
            {
                RePayPoints rePayPoints = null;
                if (model.Id > 0)
                {
                    rePayPoints = RePayPointsService.Get(model.Id);
                }

                if (rePayPoints != null)
                {
                    #region Cập nhật
                    //Nếu đã ghi sổ rồi thì không được sửa
                    if (rePayPoints.IsArchive == true)
                    {
                        TempData[Globals.FailedMessageKey] = "Đã ghi sổ. không được chỉnh sửa";
                        return(RedirectToAction("Detail", new { Id = rePayPoints.Id }));
                    }

                    //Kiểm tra xem nếu có xuất kho rồi thì return
                    var checkProductOutbound = ProductOutboundRepository.GetAllProductOutbound()
                                               .Where(item => item.Type == ProductOutboundType.Gift && item.PayPointId == rePayPoints.Id).FirstOrDefault();
                    if (checkProductOutbound != null)
                    {
                        TempData[Globals.FailedMessageKey] = "Phiếu xuất kho đã có!";
                        return(RedirectToAction("Detail", new { Id = rePayPoints.Id }));
                    }

                    AutoMapper.Mapper.Map(model, rePayPoints);
                    rePayPoints.ModifiedUserId = WebSecurity.CurrentUserId;
                    rePayPoints.ModifiedDate   = DateTime.Now;
                    rePayPoints.Status         = RePayPointsStatus.Pending;
                    rePayPoints.BranchId       = Helpers.Common.CurrentUser.BranchId;
                    rePayPoints.IsArchive      = false;
                    RePayPointsService.Update(rePayPoints);

                    //Xóa chi tiết cũ và thêm chi tiết mới
                    var listDetail_old = RePayPointsDetailService.GetRePayPointsDetailByPayPointId(rePayPoints.Id).ToList();
                    foreach (var item in listDetail_old)
                    {
                        RePayPointsDetailService.Delete(item);
                    }

                    foreach (var item in model.DetailList)
                    {
                        RePayPointsDetail rePayPointsDetail = new RePayPointsDetail();
                        AutoMapper.Mapper.Map(item, rePayPointsDetail);
                        rePayPointsDetail.RePayPointId   = rePayPoints.Id;
                        rePayPointsDetail.IsDeleted      = false;
                        rePayPointsDetail.CreatedUserId  = WebSecurity.CurrentUserId;
                        rePayPointsDetail.ModifiedUserId = WebSecurity.CurrentUserId;
                        rePayPointsDetail.CreatedDate    = DateTime.Now;
                        rePayPointsDetail.ModifiedDate   = DateTime.Now;
                        RePayPointsDetailService.Create(rePayPointsDetail);
                    }

                    #endregion
                }
                else// Thêm mới
                {
                    #region MyRegion
                    rePayPoints = new RePayPoints();
                    AutoMapper.Mapper.Map(model, rePayPoints);
                    rePayPoints.IsDeleted      = false;
                    rePayPoints.CreatedUserId  = WebSecurity.CurrentUserId;
                    rePayPoints.ModifiedUserId = WebSecurity.CurrentUserId;
                    rePayPoints.CreatedDate    = DateTime.Now;
                    rePayPoints.ModifiedDate   = DateTime.Now;
                    rePayPoints.Status         = RePayPointsStatus.Pending;
                    rePayPoints.BranchId       = Helpers.Common.CurrentUser.BranchId;
                    rePayPoints.IsArchive      = false;
                    RePayPointsService.Create(rePayPoints);
                    //Cập nhật lại mã xuất kho
                    rePayPoints.Code = Erp.BackOffice.Helpers.Common.GetOrderNo("RepayPoints", model.Code);
                    RePayPointsService.Update(rePayPoints);
                    Erp.BackOffice.Helpers.Common.SetOrderNo("RepayPoints");

                    foreach (var item in model.DetailList)
                    {
                        RePayPointsDetail rePayPointsDetail = new RePayPointsDetail();
                        AutoMapper.Mapper.Map(item, rePayPointsDetail);
                        rePayPointsDetail.IsDeleted      = false;
                        rePayPointsDetail.CreatedUserId  = WebSecurity.CurrentUserId;
                        rePayPointsDetail.ModifiedUserId = WebSecurity.CurrentUserId;
                        rePayPointsDetail.CreatedDate    = DateTime.Now;
                        rePayPointsDetail.ModifiedDate   = DateTime.Now;
                        rePayPointsDetail.RePayPointId   = rePayPoints.Id;
                        RePayPointsDetailService.Create(rePayPointsDetail);
                    }

                    //Thêm vào quản lý chứng từ
                    TransactionController.Create(new TransactionViewModel
                    {
                        TransactionModule = "RePayPoints",
                        TransactionCode   = rePayPoints.Code,
                        TransactionName   = "Trả điểm"
                    });
                    #endregion
                }
                //Tạo phiếu nhập, nếu là tự động
                string sale_tu_dong_tao_chung_tu = Erp.BackOffice.Helpers.Common.GetSetting("sale_auto_outbound");
                if (sale_tu_dong_tao_chung_tu == "true")
                {
                    ProductOutboundViewModel productOutboundViewModel = new ProductOutboundViewModel();
                    // var warehouseDefault = WarehouseRepository.GetAllWarehouse().Where(x => x.BranchId == Helpers.Common.CurrentUser.BranchId && x.IsSale == true).FirstOrDefault();

                    //Nếu trong đơn hàng có sản phẩm thì xuất kho
                    if (model.WarehouseSourceId != null)
                    {
                        productOutboundViewModel.PayPointId        = rePayPoints.Id;
                        productOutboundViewModel.PayPointCode      = rePayPoints.Code;
                        productOutboundViewModel.WarehouseSourceId = model.WarehouseSourceId;
                        productOutboundViewModel.Note = "Xuất kho cho đơn trả điểm " + rePayPoints.Code;
                        // lấy các danh sách không Xuất kho
                        var productNoInbound = ProductRepository.GetAllProduct().Where(x => x.Type == ProductType.Gift && x.NoInbound == true).ToList();
                        //Lấy dữ liệu cho chi tiết
                        productOutboundViewModel.DetailList = model.DetailList.Where(x => !productNoInbound.Any(y => y.Id == x.GiftId)).Select(x => new ProductOutboundDetailViewModel
                        {
                            ProductId = x.GiftId,
                            Quantity  = x.Quantity,
                            Price     = 0,
                            Unit      = x.Unit,
                        }).ToList();

                        var productOutbound = ProductOutboundController.CreateFromPayPoint(ProductOutboundRepository, productOutboundViewModel, rePayPoints.Code, TempData);
                        PostController.SavePost(rePayPoints.Id, "RePayPoints", "Xuất kho trả điểm (" + productOutbound.Code + ")");
                    }
                    //Ghi sổ chứng từ bán hàng
                    model.Id = rePayPoints.Id;
                    Archive(model);
                }
                return(RedirectToAction("Detail", new { Id = rePayPoints.Id }));
            }
            return(View(model));
        }