public ActionResult Create(HistoryCommissionStaffViewModel model)
        {
            if (ModelState.IsValid)
            {
                var HistoryCommissionStaff = new HistoryCommissionStaff();
                AutoMapper.Mapper.Map(model, HistoryCommissionStaff);
                HistoryCommissionStaff.IsDeleted      = false;
                HistoryCommissionStaff.CreatedUserId  = WebSecurity.CurrentUserId;
                HistoryCommissionStaff.ModifiedUserId = WebSecurity.CurrentUserId;
                HistoryCommissionStaff.AssignedUserId = WebSecurity.CurrentUserId;
                HistoryCommissionStaff.CreatedDate    = DateTime.Now;
                HistoryCommissionStaff.ModifiedDate   = DateTime.Now;
                HistoryCommissionStaffRepository.InsertHistoryCommissionStaff(HistoryCommissionStaff);

                TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess;
                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
        public static void SyncCommissionStaff(ProductInvoice model, int?CurrentUserId)
        {
            try
            {
                ProductInvoiceRepository         productInvoiceRepository         = new ProductInvoiceRepository(new ErpSaleDbContext());
                StaffsRepository                 staffRepository                  = new StaffsRepository(new ErpStaffDbContext());
                HistoryCommissionStaffRepository historyCommissionStaffRepository = new HistoryCommissionStaffRepository(new ErpStaffDbContext());
                //lấy danh sách nhà thuốc ra để bắt đầu đồng bộ dữ liệu
                var list_staff = staffRepository.GetvwAllStaffs().Where(x => (x.PositionCode == "ASM" || x.PositionCode == "TDV")).ToList();
                list_staff = list_staff.Where(x => ("," + x.DrugStore + ",").Contains("," + model.BranchId.ToString() + ",") == true).ToList();
                //lấy toàn bộ danh sách hóa đơn trong 1 tháng của tất cả nhà thuốc ra để xử lý 1 lần.
                var list_productInvoice = productInvoiceRepository.GetAllvwProductInvoice().Where(x => x.Month == model.CreatedDate.Value.Month && x.Year == model.CreatedDate.Value.Year && x.IsArchive == true).ToList();
                //lấy list đã insert nếu có
                var list_old = historyCommissionStaffRepository.GetAllHistoryCommissionStaffFull().Where(x => x.Month == model.CreatedDate.Value.Month && x.Year == model.CreatedDate.Value.Year).ToList();
                foreach (var item in list_staff)
                {
                    //lấy danh sách đơn bán hàng của nhà thuốc trong tháng
                    var list_invoice_by_drugStore = list_productInvoice.Where(x => ("," + item.DrugStore + ",").Contains("," + x.BranchId.ToString() + ",") == true).ToList();

                    decimal RevenueDS         = list_invoice_by_drugStore.Sum(x => x.TotalAmount);
                    decimal CommissionPercent = item.CommissionPercent == null ? 0 : item.CommissionPercent.Value;
                    decimal MinimumRevenue    = item.MinimumRevenue == null ? 0 : item.MinimumRevenue.Value;
                    decimal AmountCommission  = (CommissionPercent * RevenueDS) / 100;
                    if (list_old.Where(x => x.StaffId == item.Id).Count() > 0)
                    {
                        var update = list_old.Where(x => x.StaffId == item.Id).FirstOrDefault();
                        update.IsDeleted         = MinimumRevenue > RevenueDS ? true : false;
                        update.ModifiedUserId    = CurrentUserId;
                        update.ModifiedDate      = DateTime.Now;
                        update.StaffId           = item.Id;
                        update.PositionName      = item.PositionName;
                        update.Month             = model.CreatedDate.Value.Month;
                        update.Year              = model.CreatedDate.Value.Year;
                        update.CommissionPercent = CommissionPercent;
                        update.MinimumRevenue    = MinimumRevenue;
                        update.RevenueDS         = RevenueDS;
                        update.AmountCommission  = AmountCommission;
                        update.StaffName         = item.Name;
                        update.StaffParentId     = item.StaffParentId;
                        historyCommissionStaffRepository.UpdateHistoryCommissionStaff(update);
                    }
                    else
                    {
                        //thêm mới vào data.
                        var add = new HistoryCommissionStaff();
                        add.IsDeleted         = MinimumRevenue > RevenueDS ? true : false;
                        add.CreatedUserId     = CurrentUserId;
                        add.ModifiedUserId    = CurrentUserId;
                        add.AssignedUserId    = CurrentUserId;
                        add.CreatedDate       = DateTime.Now;
                        add.ModifiedDate      = DateTime.Now;
                        add.StaffId           = item.Id;
                        add.PositionName      = item.PositionName;
                        add.Month             = model.CreatedDate.Value.Month;
                        add.Year              = model.CreatedDate.Value.Year;
                        add.CommissionPercent = item.CommissionPercent;
                        add.MinimumRevenue    = item.MinimumRevenue;
                        add.RevenueDS         = RevenueDS;
                        add.AmountCommission  = AmountCommission;
                        add.StaffName         = item.Name;
                        add.StaffParentId     = item.StaffParentId;
                        historyCommissionStaffRepository.InsertHistoryCommissionStaff(add);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public JsonResult Sync(int month, int year)
        {
            //lấy danh sách nhà thuốc ra để bắt đầu đồng bộ dữ liệu
            var list_staff = staffRepository.GetvwAllStaffs().Where(x => x.PositionCode == "ASM" || x.PositionCode == "TDV").ToList();
            //lấy toàn bộ danh sách hóa đơn trong 1 tháng của tất cả nhà thuốc ra để xử lý 1 lần.
            var list_productInvoice = productInvoiceRepository.GetAllvwProductInvoice().Where(x => x.Month == month && x.Year == year && x.IsArchive == true).ToList();
            //lấy list đã insert nếu có
            var list_old = HistoryCommissionStaffRepository.GetAllHistoryCommissionStaffFull().Where(x => x.Month == month && x.Year == year).ToList();

            foreach (var item in list_staff)
            {
                //lấy danh sách đơn bán hàng của nhà thuốc trong tháng
                var list_invoice_by_drugStore = list_productInvoice.Where(x => ("," + item.DrugStore + ",").Contains("," + x.BranchId.ToString() + ",") == true).ToList();

                decimal RevenueDS         = list_invoice_by_drugStore.Sum(x => x.TotalAmount);
                decimal CommissionPercent = item.CommissionPercent == null ? 0 : item.CommissionPercent.Value;
                decimal MinimumRevenue    = item.MinimumRevenue == null ? 0 : item.MinimumRevenue.Value;
                decimal AmountCommission  = (CommissionPercent * RevenueDS) / 100;
                if (list_old.Where(x => x.StaffId == item.Id).Count() > 0)
                {
                    var update = list_old.Where(x => x.StaffId == item.Id).FirstOrDefault();
                    update.IsDeleted         = MinimumRevenue > RevenueDS ? true : false;
                    update.ModifiedUserId    = WebSecurity.CurrentUserId;
                    update.ModifiedDate      = DateTime.Now;
                    update.StaffId           = item.Id;
                    update.PositionName      = item.PositionName;
                    update.Month             = month;
                    update.Year              = year;
                    update.CommissionPercent = CommissionPercent;
                    update.MinimumRevenue    = MinimumRevenue;
                    update.RevenueDS         = RevenueDS;
                    update.AmountCommission  = AmountCommission;
                    update.StaffName         = item.Name;
                    update.StaffParentId     = item.StaffParentId;
                    HistoryCommissionStaffRepository.UpdateHistoryCommissionStaff(update);
                }
                else
                {
                    //thêm mới vào data.
                    var add = new HistoryCommissionStaff();
                    add.IsDeleted         = MinimumRevenue > RevenueDS ? true : false;
                    add.CreatedUserId     = WebSecurity.CurrentUserId;
                    add.ModifiedUserId    = WebSecurity.CurrentUserId;
                    add.AssignedUserId    = WebSecurity.CurrentUserId;
                    add.CreatedDate       = DateTime.Now;
                    add.ModifiedDate      = DateTime.Now;
                    add.StaffId           = item.Id;
                    add.PositionName      = item.PositionName;
                    add.Month             = month;
                    add.Year              = year;
                    add.CommissionPercent = item.CommissionPercent;
                    add.MinimumRevenue    = item.MinimumRevenue;
                    add.RevenueDS         = RevenueDS;
                    add.AmountCommission  = AmountCommission;
                    add.StaffName         = item.Name;
                    add.StaffParentId     = item.StaffParentId;
                    HistoryCommissionStaffRepository.InsertHistoryCommissionStaff(add);
                }
            }
            return(Json(new { Result = "success", Message = App_GlobalResources.Wording.UpdateSuccess },
                        JsonRequestBehavior.AllowGet));
        }