Example #1
0
        public ActionResult Create(TotalDiscountMoneyNTViewModel model)
        {
            if (ModelState.IsValid)
            {
                var TotalDiscountMoneyNT = new TotalDiscountMoneyNT();
                AutoMapper.Mapper.Map(model, TotalDiscountMoneyNT);
                TotalDiscountMoneyNT.IsDeleted      = false;
                TotalDiscountMoneyNT.CreatedUserId  = WebSecurity.CurrentUserId;
                TotalDiscountMoneyNT.ModifiedUserId = WebSecurity.CurrentUserId;
                TotalDiscountMoneyNT.AssignedUserId = WebSecurity.CurrentUserId;
                TotalDiscountMoneyNT.CreatedDate    = DateTime.Now;
                TotalDiscountMoneyNT.ModifiedDate   = DateTime.Now;
                TotalDiscountMoneyNTRepository.InsertTotalDiscountMoneyNT(TotalDiscountMoneyNT);

                TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess;
                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
Example #2
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;
            }
        }
Example #3
0
        public JsonResult Sync(int month, int year)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    //tìm ngày đầu tháng
                    DateTime aDateTime = new DateTime(year, 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 danh sách nhà thuốc ra để bắt đầu đồng bộ dữ liệu
                    var list_drugStore = branchRepository.GetAllBranch().Where(x => x.ParentId != null && x.ParentId.Value > 0).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);
                    //lấy list đã insert nếu có
                    var list_old = TotalDiscountMoneyNTRepository.GetAllTotalDiscountMoneyNT().Where(x => x.Month == month && x.Year == year).ToList();

                    var date_percent     = Helpers.Common.GetSetting("date_percent_decrease_NT");
                    var percent_decrease = Helpers.Common.GetSetting("percent_decrease_NT");
                    foreach (var item in list_drugStore)
                    {
                        //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 => x.BranchId == item.Id).ToList();
                        //đế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 amount         = 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;
                            //  amount = percent * DiscountAmount / 100;
                        }

                        var     ds_             = list_invoice_by_drugStore.Sum(x => x.TotalAmount);
                        var     ds_tru          = ds_ * percent / 100;
                        decimal RemainingAmount = DiscountAmount - ds_tru;
                        if (list_old.Where(x => x.DrugStoreId == item.Id).Count() > 0)
                        {
                            var add = list_old.Where(x => x.DrugStoreId == item.Id).FirstOrDefault();
                            add.ModifiedUserId  = WebSecurity.CurrentUserId;
                            add.ModifiedDate    = DateTime.Now;
                            add.DrugStoreId     = item.Id;
                            add.BranchId        = item.ParentId;
                            add.UserManagerId   = WebSecurity.CurrentUserId;
                            add.Month           = month;
                            add.Year            = 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   = WebSecurity.CurrentUserId;
                            add.ModifiedUserId  = WebSecurity.CurrentUserId;
                            add.AssignedUserId  = WebSecurity.CurrentUserId;
                            add.CreatedDate     = DateTime.Now;
                            add.ModifiedDate    = DateTime.Now;
                            add.DrugStoreId     = item.Id;
                            add.BranchId        = item.ParentId;
                            add.UserManagerId   = WebSecurity.CurrentUserId;
                            add.Month           = month;
                            add.Year            = 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);
                        }
                    }
                    scope.Complete();
                    return(Json(new { Result = "success", Message = App_GlobalResources.Wording.UpdateSuccess },
                                JsonRequestBehavior.AllowGet));
                }
                catch (DbUpdateException)
                {
                    return(Json(new { Result = "error", Message = App_GlobalResources.Error.UpdateUnsuccess },
                                JsonRequestBehavior.AllowGet));
                }
            }
        }