Example #1
0
        public ActionResult Update(PreImportMasterModel model, List <PreImportDetailViewModel> detail, decimal TotalShippingWeight, decimal?GuestAmountPaid, DateTime ExchangeDate, int CreateReceipt = 1)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    #region  // Bước 1 : Modified Master
                    model.LastModifiedDate       = DateTime.Now;
                    model.LastModifiedAccount    = currentAccount.UserName;
                    model.LastModifiedEmployeeId = currentEmployee.EmployeeId;
                    model.Paid = GuestAmountPaid.HasValue ? GuestAmountPaid : 0;
                    //Thêm tổng công nợ cộng dồn = nợ cũ + nợ mới
                    decimal?SuplierOldDebt = _context.PreImportMasterModel
                                             .Where(p => p.SupplierId == model.SupplierId && p.PreImportMasterId < model.PreImportMasterId)
                                             .OrderByDescending(p => p.PreImportMasterId)
                                             .Select(p => p.RemainingAmountAccrued)
                                             .FirstOrDefault();
                    model.RemainingAmountAccrued = SuplierOldDebt == null ? 0 + model.RemainingAmount : SuplierOldDebt + model.RemainingAmount;

                    #region  //Cập nhật lại số nợ RemainingAmountAccrued của các đơn yêu hàng cầu sau
                    var lstModelNext = _context.PreImportMasterModel
                                       .Where(p => p.Actived == true && p.SupplierId == model.SupplierId && p.PreImportMasterId > model.PreImportMasterId)
                                       .OrderBy(p => p.PreImportMasterId)
                                       .ToList();
                    if (lstModelNext != null)
                    {
                        decimal?SaveRemainingPre = model.RemainingAmountAccrued.Value;  // lưu Nợ cộng dồn của record trc đó
                        foreach (var item in lstModelNext)
                        {
                            decimal?SuplierOldDebtNext = _context.PreImportMasterModel
                                                         .Where(p => p.SupplierId == model.SupplierId && p.PreImportMasterId == item.PreImportMasterId)
                                                         .OrderByDescending(p => p.PreImportMasterId)
                                                         .Select(p => p.RemainingAmountAccrued)
                                                         .FirstOrDefault();
                            item.RemainingAmountAccrued = item.RemainingAmount + SaveRemainingPre;
                            SaveRemainingPre            = SuplierOldDebtNext == null ? 0 + item.RemainingAmount : SuplierOldDebtNext + item.RemainingAmount;
                            _context.Entry(item).State  = System.Data.Entity.EntityState.Modified;
                            _context.SaveChanges();
                        }
                    }
                    #endregion

                    _context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    _context.SaveChanges();
                    #endregion

                    #region // Bước 2 : Xoá những sản phẩm cũ , insert sản phẩm mới
                    var lstPreImportDetai = _context.PreImportDetailModel.Where(p => p.PreImportMasterId == model.PreImportMasterId).ToList();
                    foreach (var item in lstPreImportDetai)
                    {
                        _context.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                        _context.SaveChanges();
                    }
                    #region Detail insert sản phẩm mới
                    if (detail != null)
                    {
                        if (detail.GroupBy(p => p.ProductId).ToList().Count < detail.Count)
                        {
                            //khong duoc trung san pham
                            return(Content("Vui lòng không chọn thông tin sản phẩm trùng nhau"));
                        }
                        foreach (var item in detail)
                        {
                            PreImportDetailModel detailmodel = new PreImportDetailModel()
                            {
                                PreImportMasterId = model.PreImportMasterId,
                                ProductId         = item.ProductId,
                                Qty   = item.Qty,
                                Price = item.Price,
                                UnitShippingWeight = item.UnitShippingWeight,
                                UnitPrice          = item.UnitPrice,
                                ShippingFee        = item.ShippingFee,
                                UnitCOGS           = item.UnitCOGS,
                                Note = item.Note
                            };
                            _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                        }
                        // đánh dấu Transaction hoàn tất
                        ts.Complete();
                        return(Content("success"));
                    }
                    else
                    {
                        //chua nhap tt san pham
                        return(Content("Vui lòng chọn thông tin sản phẩm"));
                    }
                    #endregion

                    #endregion
                }
            }
            catch
            {
                return(Content("Xảy ra lỗi trong quá trình Chỉnh sửa yêu cầu sản phẩm từ nhà cung cấp"));
            }
        }
Example #2
0
        public ActionResult Save(PreImportMasterModel model, List <PreImportDetailViewModel> detail, decimal TotalShippingWeight, decimal?GuestAmountPaid, DateTime ExchangeDate)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    #region Master
                    model.CreatedDate       = DateTime.Now;
                    model.CreatedAccount    = currentAccount.UserName;
                    model.CreatedEmployeeId = currentEmployee.EmployeeId;
                    model.InventoryTypeId   = EnumInventoryType.NC;
                    model.Paid = GuestAmountPaid.HasValue ? GuestAmountPaid : 0;
                    model.PreImportMasterCode = GetPreImportMasterCode();
                    model.StatusCode          = "TAOMOI";

                    //Thêm tổng công nợ cộng dồn = nợ cũ + nợ mới
                    decimal?SuplierOldDebt = _context.PreImportMasterModel
                                             .Where(p => p.SupplierId == model.SupplierId)
                                             .OrderByDescending(p => p.PreImportMasterId)
                                             .Select(p => p.RemainingAmountAccrued)
                                             .FirstOrDefault();
                    model.RemainingAmountAccrued = SuplierOldDebt == null ? 0 + model.RemainingAmount : SuplierOldDebt + model.RemainingAmount;
                    _context.Entry(model).State  = System.Data.Entity.EntityState.Added;
                    _context.SaveChanges(); // LƯU TẠM ĐỂ LẤY PREIMPORTMASTERID (SẼ BỊ SCROLLBACK KHI XẢY RA LỖI)
                    #endregion

                    #region Update ExchangeRate
                    var Exchangerate = _context.ExchangeRateModel
                                       .OrderByDescending(p => p.ExchangeDate)
                                       .Where(p => p.CurrencyId == model.CurrencyId &&
                                              p.ExchangeDate.Value.CompareTo(DateTime.Now) <= 0
                                              )
                                       .FirstOrDefault();
                    string DateDB  = string.Format("{0}-{1}-{2}", Exchangerate.ExchangeDate.Value.Year, Exchangerate.ExchangeDate.Value.Month, Exchangerate.ExchangeDate.Value.Day);
                    string DateNow = string.Format("{0}-{1}-{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

                    if (DateDB == DateNow)
                    {
                        // update
                        Exchangerate.ExchangeRate          = (float)model.ExchangeRate.Value;
                        Exchangerate.ExchangeDate          = DateTime.Now;
                        _context.Entry(Exchangerate).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        ExchangeRateModel Exchangeratemodel = new ExchangeRateModel()
                        {
                            CurrencyId   = model.CurrencyId,
                            ExchangeRate = (float)model.ExchangeRate.Value,
                            ExchangeDate = DateTime.Now,
                        };
                        // add
                        _context.Entry(Exchangeratemodel).State = System.Data.Entity.EntityState.Added;
                    }
                    _context.SaveChanges();
                    #endregion

                    #region Detail
                    if (detail != null)
                    {
                        if (detail.GroupBy(p => p.ProductId).ToList().Count < detail.Count)
                        {
                            //khong duoc trung san pham
                            return(Content("Vui lòng không chọn thông tin sản phẩm trùng nhau"));
                        }
                        foreach (var item in detail)
                        {
                            PreImportDetailModel detailmodel = new PreImportDetailModel()
                            {
                                PreImportMasterId = model.PreImportMasterId,
                                ProductId         = item.ProductId,
                                Qty   = item.Qty,
                                Price = item.Price,
                                UnitShippingWeight = item.UnitShippingWeight,
                                UnitPrice          = item.UnitPrice,
                                ShippingFee        = item.ShippingFee,
                                UnitCOGS           = item.UnitCOGS,
                                Note = item.Note
                            };
                            _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                        }
                        // Cập nhật lại Tổng giá vốn
                        model.SumCOGSOfOrderDetail  = detail.Sum(p => p.UnitCOGS * p.Qty);
                        _context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                        _context.SaveChanges();

                        // đánh dấu Transaction hoàn tất
                        ts.Complete();
                        return(Content("success"));
                    }
                    else
                    {
                        //chua nhap tt san pham
                        return(Content("Vui lòng chọn thông tin sản phẩm"));
                    }
                    #endregion
                }
            }
            catch
            {
                return(Content("Xảy ra lỗi trong quá trình thêm mới yêu cầu sản phẩm từ nhà cung cấp"));
            }
        }