public IEnumerable <int> FetchListStaffLabourContractType(string LabourContractName, bool?IsSeniority, string BranchId)
        {
            //tìm ds các nhân viên thuộc khu vực và phòng ban
            StaffsRepository staffRepository = new StaffsRepository(new ErpStaffDbContext());
            var listStaffSeniority           = new List <int?>();



            var staff = new List <int>();
            IEnumerable <vwStaffs> listStaffs;

            if (string.IsNullOrEmpty(LabourContractName))
            {
                listStaffs = staffRepository.GetvwAllStaffs().ToList();
            }
            else
            {
                listStaffs = staffRepository.GetvwAllStaffs().ToList();
            }

            if (BranchId != null)
            {
                listStaffs = listStaffs.Where(n => n.DrugStore == BranchId).ToList();
            }
            //listStaffs = staffRepository.GetvwAllStaffs().Where(u => u.LabourContractName == LabourContractName).ToList();

            //if (IsSeniority != null && IsSeniority.Value == true)
            //{
            //    DateTime dateNow = DateTime.Now;
            //    int monthsetup = 5;
            //    if (dateNow.Month <= monthsetup)
            //        listStaffSeniority = salarySeniority.GetAllSalarySeniority()
            //            .Where(n => n.NgayTinhHuong.Value.Month <= monthsetup && n.NgayTinhHuong.Value.Year == dateNow.Year).Select(n => n.StaffId).Distinct().ToList();
            //    else
            //        listStaffSeniority = salarySeniority.GetAllSalarySeniority()
            //            .Where(n => n.NgayTinhHuong.Value.Month > monthsetup && n.NgayTinhHuong.Value.Year == dateNow.Year).Select(n => n.StaffId).Distinct().ToList();

            //    //setup list staff
            //    listStaffs = listStaffs.Where(n => listStaffSeniority.Contains(n.Id));
            //}

            foreach (var item in listStaffs)
            {
                staff.Add(item.Id);
            }
            //staff.Position = Erp.BackOffice.Helpers.Common.GetCategoryByValueCodeOrId("value", staff.Position, "position").Name.ToString();

            return(staff);
        }
        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;
            }
        }