示例#1
0
        public static void SyncTotalDisCountMoneyNT(ProductInvoice model, int?CreatedUserId)
        {
            try
            {
                ProductInvoiceRepository       productInvoiceRepository       = new ProductInvoiceRepository(new ErpSaleDbContext());
                BranchRepository               branchRepository               = new BranchRepository(new ErpStaffDbContext());
                TotalDiscountMoneyNTRepository totalDiscountMoneyNTRepository = new TotalDiscountMoneyNTRepository(new ErpSaleDbContext());
                //tìm ngày đầu tháng
                DateTime aDateTime = new DateTime(model.CreatedDate.Value.Year, model.CreatedDate.Value.Month, 1);
                //tìm ngày cuối tháng
                DateTime retDateTime = aDateTime.AddMonths(1).AddDays(-1);
                //tìm số ngày của 1 tháng
                var count_day_of_month = retDateTime.Day;
                //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_invoice_by_drugStore = productInvoiceRepository.GetAllvwProductInvoice().Where(x => x.BranchId == model.BranchId && 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 = totalDiscountMoneyNTRepository.GetAllTotalDiscountMoneyNT().Where(x => x.Month == model.CreatedDate.Value.Month && x.Year == model.CreatedDate.Value.Year).ToList();

                var date_percent     = Helpers.Common.GetSetting("date_percent_decrease_NT");
                var percent_decrease = Helpers.Common.GetSetting("percent_decrease_NT");

                //đếm số ngày tạo đơn thuốc trong 1 tháng
                var count_day = list_invoice_by_drugStore.GroupBy(z => z.Day).Count();

                //nếu số ngày đăng nhập lớn hơn cài đặt thì lấy tổng ngày đăng nhập theo cài đặt
                var count_day_setting = count_day > Convert.ToInt32(date_percent) ? Convert.ToInt32(date_percent) : count_day;
                //tính số ngày không tạo đơn hàng trong tháng theo cài đặt
                decimal count_day_off = Convert.ToInt32(date_percent) - count_day_setting;
                //tinh % trừ chiết khấu
                decimal percent        = 0;
                decimal DiscountAmount = list_invoice_by_drugStore.Sum(x => (x.FixedDiscount + x.IrregularDiscount));

                if (count_day_off > 0)
                {
                    decimal  pe_countday         = count_day_setting / 5;
                    var      p_last              = pe_countday / Convert.ToInt32(percent_decrease);
                    string[] arrVal_percent_last = p_last.ToString().IndexOf(".") > 0 ? p_last.ToString().Split('.') : p_last.ToString().Split(',');

                    var vl = int.Parse(arrVal_percent_last[0], CultureInfo.InstalledUICulture);
                    if (vl == 0)
                    {
                        vl = 1;
                    }
                    decimal  p_dayoff = count_day_off / 5;
                    string[] arrVal   = p_dayoff.ToString().IndexOf(".") > 0 ? p_dayoff.ToString().Split('.') : p_dayoff.ToString().Split(',');
                    var      value    = int.Parse(arrVal[0], CultureInfo.InstalledUICulture);
                    if (arrVal.Count() >= 2)
                    {
                        var aa = int.Parse(arrVal[1], CultureInfo.InstalledUICulture);
                        if (aa > 0)
                        {
                            value = value + 1;
                        }
                    }

                    var percent_off = value * Convert.ToInt32(percent_decrease) / vl;

                    percent = percent_off;
                }

                var     ds_             = list_invoice_by_drugStore.Sum(x => x.TotalAmount);
                var     ds_tru          = ds_ * percent / 100;
                decimal RemainingAmount = DiscountAmount - ds_tru;

                var branch_parent = branchRepository.GetBranchById(model.BranchId);
                if (list_old.Where(x => x.DrugStoreId == model.BranchId).Count() > 0)
                {
                    var add = list_old.Where(x => x.DrugStoreId == model.BranchId).FirstOrDefault();
                    add.ModifiedUserId  = CreatedUserId;
                    add.ModifiedDate    = DateTime.Now;
                    add.DrugStoreId     = model.BranchId;
                    add.BranchId        = branch_parent.ParentId;
                    add.UserManagerId   = CreatedUserId;
                    add.Month           = model.CreatedDate.Value.Month;
                    add.Year            = model.CreatedDate.Value.Year;
                    add.QuantityDay     = count_day;
                    add.PercentDecrease = Math.Round(percent, 2);
                    add.DiscountAmount  = DiscountAmount;
                    add.DecreaseAmount  = ds_tru;
                    add.RemainingAmount = RemainingAmount;
                    add.Status          = App_GlobalResources.Wording.New;
                    totalDiscountMoneyNTRepository.UpdateTotalDiscountMoneyNT(add);
                }
                else
                {
                    //thêm mới vào data.
                    var add = new TotalDiscountMoneyNT();
                    add.IsDeleted       = false;
                    add.CreatedUserId   = CreatedUserId;
                    add.ModifiedUserId  = CreatedUserId;
                    add.AssignedUserId  = CreatedUserId;
                    add.CreatedDate     = DateTime.Now;
                    add.ModifiedDate    = DateTime.Now;
                    add.DrugStoreId     = model.BranchId;
                    add.BranchId        = branch_parent.ParentId;
                    add.UserManagerId   = WebSecurity.CurrentUserId;
                    add.Month           = model.CreatedDate.Value.Month;
                    add.Year            = model.CreatedDate.Value.Year;
                    add.QuantityDay     = count_day;
                    add.PercentDecrease = Math.Round(percent, 2);
                    add.DiscountAmount  = DiscountAmount;
                    add.DecreaseAmount  = ds_tru;
                    add.RemainingAmount = RemainingAmount;
                    add.Status          = App_GlobalResources.Wording.New;
                    totalDiscountMoneyNTRepository.InsertTotalDiscountMoneyNT(add);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        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;
            }
        }