Пример #1
0
        public ActionResult ToggleState(string id)
        {
            var response = new JsonResultBody();

            try
            {
                Branch country = _repository.GetBranchById(id);
                response.Data = _repository.Delete(country);
            }
            catch (DbEntityValidationException ex)
            {
                response.Status = System.Net.HttpStatusCode.InternalServerError;
                foreach (DbEntityValidationResult result in ex.EntityValidationErrors)
                {
                    response.Errors = (from ve in result.ValidationErrors select ve.ErrorMessage).ToList();
                }
            }
            catch (Exception exApp)
            {
                response.Status = System.Net.HttpStatusCode.InternalServerError;
                response.Errors.Add(exApp.Message);
            }

            return(Json(response));
        }
Пример #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;
            }
        }