コード例 #1
0
        public ActionResult CreateImportDetail(int id)
        {
            var categories = _categoryRepository.Search("").ToList();
            var model      = new ImportDetailModel {
                Categories = categories, ImportTrackId = id
            };

            return(View("_CreateImportDetail", model));
        }
コード例 #2
0
 public static void UpdateImportDetail(this ImportDetail importDetail, ImportDetailModel importDetailModel)
 {
     importDetail.ProductId     = importDetailModel.ProductId;
     importDetail.ImportId      = importDetailModel.ImportId;
     importDetail.ProductName   = importDetailModel.ProductName;
     importDetail.Quantity      = importDetailModel.Quantity;
     importDetail.Price         = importDetailModel.Price;
     importDetail.Total         = importDetailModel.Total;
     importDetail.Total         = importDetailModel.Total;
     importDetail.WareHouseId   = importDetailModel.WareHouseId;
     importDetail.ColorCode     = importDetailModel.ColorCode;
     importDetail.SizeCode      = importDetailModel.SizeCode;
     importDetail.ColorId       = importDetailModel.ColorId;
     importDetail.SizeId        = importDetailModel.SizeId;
     importDetail.ComponentCode = importDetailModel.ComponentCode;
 }
コード例 #3
0
        public ImportViewModel()
        {
            loadUserCurrentLogin();
            LoadImportData();

            ListProduct            = new List <ProductTable> (DataProvider.Ins.Entities.ProductTable);
            CreateNewImportCommand = new RelayCommand <object>((p) =>
            {
                return(true);
            }, (p) =>
            {
                InputTable newInput = new InputTable();
                newInput.DateInput  = DateTime.Now;
                newInput.ID_User    = ID_CurrentUser;
                if (newInput == null)
                {
                    return;
                }

                DataProvider.Ins.Entities.InputTable.Add(newInput);
                DataProvider.Ins.Entities.SaveChanges();

                IsActiveSnackBar = true;
                Message          = "Thêm Mới Phiếu Nhập Thành Công!";

                LoadImportData();

                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Interval            = 3000;
                timer.Enabled             = true;
                timer.Elapsed            += ShowSnackBar;
                timer.Start();
            });

            LoadEditCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { LoadDialogAccountEdit(); });

            AddCommand = new RelayCommand <object>((p) =>
            {
                if (Amount == 0 || InputPrice == 0 || string.IsNullOrEmpty(Status))
                {
                    return(false);
                }


                var Product = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID_Product == SelectedProduct.ID && x.ID_Input == SelectedItemInput.input.ID);

                if (Product.Count() != 0 || Product == null)
                {
                    return(false);
                }

                return(true);
            },
                                                   (p) =>
            {
                InputDetailTable detailItem = new InputDetailTable();
                detailItem.ID_Input         = SelectedItemInput.input.ID;
                detailItem.ID_Product       = SelectedProduct.ID;
                detailItem.PriceInput       = InputPrice;
                detailItem.Status           = Status;
                detailItem.Amount           = Amount;
                detailItem.TotalPrice       = Amount * InputPrice;
                DataProvider.Ins.Entities.InputDetailTable.Add(detailItem);
                DataProvider.Ins.Entities.SaveChanges();

                ListInputDetail = new List <ImportDetailModel>();
                var List        = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID_Input == SelectedItemInput.input.ID);
                foreach (var item in List)
                {
                    ImportDetailModel detail = new ImportDetailModel();
                    ProductTable product     = new ProductTable();
                    product            = DataProvider.Ins.Entities.ProductTable.Where(x => x.ID == item.ID_Product).FirstOrDefault();
                    detail.InputDetail = item;
                    detail.Product     = product;
                    ListInputDetail.Add(detail);
                }

                SelectedItemInputDetail = null;
                InputPrice      = 0;
                Status          = null;
                Amount          = 0;
                SelectedProduct = null;

                IsActiveSnackBar          = true;
                Message                   = "Thêm Mới Chi Tiết Thành Công!";
                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Interval            = 5000;
                timer.Enabled             = true;
                timer.Elapsed            += ShowSnackBar;
                timer.Start();
            });

            EditCommand = new RelayCommand <object>((p) =>
            {
                if (Amount == 0 || InputPrice == 0 || string.IsNullOrEmpty(Status) || SelectedProduct == null)
                {
                    return(false);
                }

                var Product = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID != SelectedItemInputDetail.Product.ID && x.ID_Product == SelectedProduct.ID && x.ID_Input == SelectedItemInput.input.ID);

                if (Product.Count() != 0 || Product == null)
                {
                    return(false);
                }

                return(true);
            },
                                                    (p) =>
            {
                InputDetailTable detailItem = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID == SelectedItemInputDetail.InputDetail.ID).FirstOrDefault();
                detailItem.ID_Product       = SelectedProduct.ID;
                detailItem.PriceInput       = InputPrice;
                detailItem.Status           = Status;
                detailItem.Amount           = Amount;
                detailItem.TotalPrice       = Amount * InputPrice;
                DataProvider.Ins.Entities.SaveChanges();

                ListInputDetail = new List <ImportDetailModel>();
                var List        = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID_Input == SelectedItemInput.input.ID);
                foreach (var item in List)
                {
                    ImportDetailModel detail = new ImportDetailModel();
                    ProductTable product     = new ProductTable();
                    product            = DataProvider.Ins.Entities.ProductTable.Where(x => x.ID == item.ID_Product).FirstOrDefault();
                    detail.InputDetail = item;
                    detail.Product     = product;
                    ListInputDetail.Add(detail);
                }

                SelectedItemInputDetail = null;
                InputPrice      = 0;
                Status          = null;
                Amount          = 0;
                SelectedProduct = null;

                IsActiveSnackBar          = true;
                Message                   = "Sửa Chi Tiết Thành Công!";
                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Interval            = 5000;
                timer.Enabled             = true;
                timer.Elapsed            += ShowSnackBar;
                timer.Start();
            });

            DeleteCommand = new RelayCommand <object>((p) =>
            {
                if (Amount == 0 || InputPrice == 0 || string.IsNullOrEmpty(Status) || SelectedProduct == null)
                {
                    return(false);
                }

                return(true);
            }, (p) =>
            {
                DeleteNotificationMessage msg = new DeleteNotificationMessage();
                Message = "Xóa Thông Tin Có Thể Dẫn Đến Sai Lệch Dữ Liệu! Bạn Chắc Chắn Chứ?";
                DialogHost.Show(msg, "ImportDialog");
            });

            DeleteConfirmCommand = new RelayCommand <object>((p) =>
            {
                if (Amount == 0 || InputPrice == 0 || string.IsNullOrEmpty(Status) || SelectedProduct == null)
                {
                    return(false);
                }

                return(true);
            }, (p) =>
            {
                InputDetailTable detailItem = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID == SelectedItemInputDetail.InputDetail.ID).FirstOrDefault();
                DataProvider.Ins.Entities.InputDetailTable.Remove(detailItem);
                DataProvider.Ins.Entities.SaveChanges();

                ListInputDetail = new List <ImportDetailModel>();
                var List        = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID_Input == SelectedItemInput.input.ID);
                foreach (var item in List)
                {
                    ImportDetailModel detail = new ImportDetailModel();
                    ProductTable product     = new ProductTable();
                    product            = DataProvider.Ins.Entities.ProductTable.Where(x => x.ID == item.ID_Product).FirstOrDefault();
                    detail.InputDetail = item;
                    detail.Product     = product;
                    ListInputDetail.Add(detail);
                }
                DialogHost.CloseDialogCommand.Execute(null, null);

                SelectedItemInputDetail = null;
                InputPrice      = 0;
                Status          = null;
                Amount          = 0;
                SelectedProduct = null;

                IsActiveSnackBar          = true;
                Message                   = "Xóa Chi Tiết Thành Công!";
                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Interval            = 5000;
                timer.Enabled             = true;
                timer.Elapsed            += ShowSnackBar;
                timer.Start();
            });

            DeleteImportCommand = new RelayCommand <object>((p) =>
            {
                return(true);
            }, (p) =>
            {
                var List = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID_Input == SelectedItemInput.input.ID);
                if (List.Count() != 0)
                {
                    ErrorNotificationMessage msg = new ErrorNotificationMessage();
                    Message = "Phiếu Nhập Này Đang Có Chi Tiết! Bạn Vui Lòng Xóa Bỏ Các Thông Tin Liên Quan!";
                    DialogHost.Show(msg, "ImportDialog");
                    return;
                }

                DataProvider.Ins.Entities.InputTable.Remove(SelectedItemInput.input);
                DataProvider.Ins.Entities.SaveChanges();

                SelectedItemInput       = null;
                SelectedItemInputDetail = null;
                InputPrice      = 0;
                Status          = null;
                Amount          = 0;
                SelectedProduct = null;

                IsActiveSnackBar = true;
                Message          = "Xóa Phiếu Nhập Thành Công!";

                LoadImportData();

                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Interval            = 5000;
                timer.Enabled             = true;
                timer.Elapsed            += ShowSnackBar;
                timer.Start();
            });

            SearchCommand = new RelayCommand <object>((p) =>
            {
                return(true);
            },
                                                      (p) =>
            {
                if (SearchTermInput == null)
                {
                    return;
                }


                var ListInput             = DataProvider.Ins.Entities.InputTable;
                List <ImportModel> Result = new List <ImportModel>();
                foreach (var item in ListInput)
                {
                    ImportModel import = new ImportModel();
                    import.input       = item;
                    import.User        = DataProvider.Ins.Entities.UserTable.Where(x => x.ID == item.ID_User).FirstOrDefault();
                    Result.Add(import);
                }

                ListImport = new List <ImportModel>(Result.Where(
                                                        x => x.input.DateInput.ToString().ToLower().Contains(SearchTermInput) ||
                                                        x.User.DisplayName.ToLower().Contains(SearchTermInput)
                                                        ));
            });

            SearchDetailCommand = new RelayCommand <object>((p) =>
            {
                return(true);
            },
                                                            (p) =>
            {
                if (SearchTermInputDetail == null || SelectedItemInput == null)
                {
                    return;
                }
                List <ImportDetailModel> Result = new List <ImportDetailModel>();

                var List = DataProvider.Ins.Entities.InputDetailTable.Where(x => x.ID_Input == SelectedItemInput.input.ID);
                foreach (var item in List)
                {
                    ImportDetailModel detail = new ImportDetailModel();
                    ProductTable product     = new ProductTable();
                    product            = DataProvider.Ins.Entities.ProductTable.Where(x => x.ID == item.ID_Product).FirstOrDefault();
                    detail.InputDetail = item;
                    detail.Product     = product;
                    Result.Add(detail);
                }
                ListInputDetail = new List <ImportDetailModel>(Result.Where(
                                                                   x => x.Product.DisplayName.ToLower().Contains(SearchTermInputDetail) ||
                                                                   x.InputDetail.Amount.ToString().ToLower().Contains(SearchTermInputDetail) || x.InputDetail.PriceInput.ToString().ToLower().Contains(SearchTermInputDetail) ||
                                                                   x.InputDetail.Status.Contains(SearchTermInputDetail)
                                                                   ));
            });
        }
コード例 #4
0
        public string Save(ImportMasterModel model, List <ImportDetailViewModel> detail, decimal TotalShippingWeight, decimal?GuestAmountPaid, DateTime ExchangeDate, int CreateReceipt, string UserName, int EmployeeId)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    var currentTime = DateTime.Now;

                    #region ImportMaster
                    model.CreatedDate       = currentTime;
                    model.CreatedAccount    = UserName;
                    model.CreatedEmployeeId = EmployeeId;
                    model.InventoryTypeId   = EnumInventoryType.NC;
                    model.Paid             = GuestAmountPaid.HasValue ? GuestAmountPaid : 0;
                    model.ImportMasterCode = GetImportMasterCode();
                    //Thêm tổng công nợ cộng dồn = nợ cũ + nợ mới
                    //decimal? SuplierOldDebt = _context.ImportMasterModel
                    //                                  .Where(p => p.SupplierId == model.SupplierId)
                    //                                  .OrderByDescending(p => p.ImportMasterId)
                    //                                  .Select(p => p.RemainingAmountAccrued)
                    //                                  .FirstOrDefault();
                    decimal?SuplierOldDebt = _context.AM_DebtModel
                                             .Where(p => p.SupplierId == model.SupplierId)
                                             .OrderByDescending(p => p.TimeOfDebt)
                                             .Select(p => p.RemainingAmountAccrued)
                                             .FirstOrDefault();
                    SuplierOldDebt               = (SuplierOldDebt == null) ? 0 : SuplierOldDebt.Value;
                    model.RemainingAmount        = (model.RemainingAmount == null) ? 0 : model.RemainingAmount.Value;
                    model.RemainingAmountAccrued = SuplierOldDebt.Value + model.RemainingAmount.Value;

                    _context.Entry(model).State = System.Data.Entity.EntityState.Added;
                    _context.SaveChanges(); // LƯU TẠM ĐỂ LẤY IMPORTMASTERID (SẼ BỊ SCROLLBACK KHI XẢY RA LỖI)
                    #endregion

                    #region Kế toán
                    if (CreateReceipt == 1)
                    {
                        #region Thêm vào giao dịch kế toán
                        AM_TransactionModel AMmodel;

                        #region TH1 : Trả đủ

                        if (model.TotalPrice == GuestAmountPaid)
                        {
                            AMmodel = new AM_TransactionModel()
                            {
                                StoreId             = model.StoreId,
                                AMAccountId         = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.TM && p.AMAccountTypeCode == EnumAM_AccountType.TIENMAT && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(),
                                TransactionTypeCode = EnumTransactionType.NXNHAP,
                                ContactItemTypeCode = EnumContactType.NCC,
                                CustomerId          = null,
                                SupplierId          = model.SupplierId,
                                EmployeeId          = null,
                                OtherId             = null,
                                Amount                 = GuestAmountPaid,
                                OrderId                = null,
                                ImportMasterId         = model.ImportMasterId,
                                IEOtherMasterId        = null,
                                Note                   = model.Note,
                                CreateDate             = currentTime,
                                CreateEmpId            = EmployeeId,
                                RemainingAmountAccrued = model.RemainingAmountAccrued
                            };
                            _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                        }
                        #endregion

                        #region TH2 : Không trả lưu vào công nợ
                        else if (GuestAmountPaid == 0 || GuestAmountPaid == null)
                        {
                            AMmodel = new AM_TransactionModel()
                            {
                                StoreId             = model.StoreId,
                                AMAccountId         = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.PTNCC && p.AMAccountTypeCode == EnumAM_AccountType.CONGNO && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(),
                                TransactionTypeCode = EnumTransactionType.NXNHAP,
                                ContactItemTypeCode = EnumContactType.NCC,
                                CustomerId          = null,
                                SupplierId          = model.SupplierId,
                                EmployeeId          = null,
                                OtherId             = null,
                                Amount                 = model.TotalPrice,
                                OrderId                = null,
                                ImportMasterId         = model.ImportMasterId,
                                IEOtherMasterId        = null,
                                Note                   = model.Note,
                                CreateDate             = currentTime,
                                CreateEmpId            = EmployeeId,
                                RemainingAmountAccrued = model.RemainingAmountAccrued
                            };
                            _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                        }
                        #endregion

                        #region TH3 : Trả 1 phần
                        else
                        {
                            #region 1 phần (Tiền mặt hoặc chuyển khoản)
                            AMmodel = new AM_TransactionModel()
                            {
                                StoreId             = model.StoreId,
                                AMAccountId         = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.TM && p.AMAccountTypeCode == EnumAM_AccountType.TIENMAT && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(),
                                TransactionTypeCode = EnumTransactionType.NXNHAP,
                                ContactItemTypeCode = EnumContactType.NCC,
                                CustomerId          = null,
                                SupplierId          = model.SupplierId,
                                EmployeeId          = null,
                                OtherId             = null,
                                Amount                 = GuestAmountPaid,
                                OrderId                = null,
                                ImportMasterId         = model.ImportMasterId,
                                IEOtherMasterId        = null,
                                Note                   = model.Note,
                                CreateDate             = currentTime,
                                CreateEmpId            = EmployeeId,
                                RemainingAmountAccrued = model.RemainingAmountAccrued
                            };
                            _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                            #endregion

                            #region 1 phần đưa vào công nợ
                            AMmodel = new AM_TransactionModel()
                            {
                                StoreId             = model.StoreId,
                                AMAccountId         = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.PTNCC && p.AMAccountTypeCode == EnumAM_AccountType.CONGNO && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(),
                                TransactionTypeCode = EnumTransactionType.NXNHAP,
                                ContactItemTypeCode = EnumContactType.NCC,
                                CustomerId          = null,
                                SupplierId          = model.SupplierId,
                                EmployeeId          = null,
                                OtherId             = null,
                                Amount                 = model.TotalPrice - GuestAmountPaid,
                                OrderId                = null,
                                ImportMasterId         = model.ImportMasterId,
                                IEOtherMasterId        = null,
                                Note                   = model.Note,
                                CreateDate             = currentTime,
                                CreateEmpId            = EmployeeId,
                                RemainingAmountAccrued = model.RemainingAmountAccrued
                            };
                            _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                            #endregion
                        }
                        #endregion

                        #endregion
                    }
                    #endregion

                    #region Thêm AM_DebtModel (Số nợ còn lại)
                    if (model.RemainingAmount > 0)
                    {
                        var AMDebModel = new AM_DebtModel()
                        {
                            SupplierId             = model.SupplierId,
                            TimeOfDebt             = currentTime,
                            RemainingAmountAccrued = model.RemainingAmountAccrued,
                            ImportId            = model.ImportMasterId,
                            TransactionTypeCode = EnumTransactionType.NXNHAP
                        };
                        _context.Entry(AMDebModel).State = System.Data.Entity.EntityState.Added;
                        _context.SaveChanges();
                    }
                    #endregion

                    #region InventoryMaster
                    InventoryMasterModel InvenMaster = new InventoryMasterModel();
                    InvenMaster.WarehouseModelId      = model.WarehouseId;
                    InvenMaster.InventoryCode         = model.ImportMasterCode;
                    InvenMaster.InventoryTypeId       = EnumInventoryType.NC;
                    InvenMaster.CreatedDate           = model.CreatedDate;
                    InvenMaster.CreatedAccount        = model.CreatedAccount;
                    InvenMaster.CreatedEmployeeId     = model.CreatedEmployeeId;
                    InvenMaster.Actived               = true;
                    InvenMaster.BusinessId            = model.ImportMasterId;     // Id nghiệp vụ
                    InvenMaster.BusinessName          = "ImportMasterModel";      // Tên bảng nghiệp vụ
                    InvenMaster.ActionUrl             = "/ImportMaster/Details/"; // Đường dẫn ( cộng ID cho truy xuất)
                    InvenMaster.StoreId               = model.StoreId;
                    _context.Entry(InvenMaster).State = System.Data.Entity.EntityState.Added;
                    _context.SaveChanges(); // insert tạm để lấy InvenMasterID
                    #endregion

                    #region ExchangeRate
                    // 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 Lst Product
                    if (detail != null)
                    {
                        //if (detail.GroupBy(p => p.ProductId).ToList().Count < detail.Count)
                        //{
                        //    //khong duoc trung san pham
                        //    return "Vui lòng không chọn thông tin sản phẩm trùng nhau !";
                        //}
                        foreach (var item in detail)
                        {
                            item.UnitCOGS = (item.Price * model.ExchangeRate) + item.ShippingFee;

                            #region Import Detail
                            ImportDetailModel detailmodel = new ImportDetailModel()
                            {
                                ImportMasterId     = model.ImportMasterId,
                                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();
                            #endregion

                            #region  update bảng Product
                            var productmodel = _context.ProductModel.Where(p => p.ProductId == item.ProductId).FirstOrDefault();
                            //productmodel.ImportPrice = item.Price;
                            //productmodel.ShippingFee = item.ShippingFee;
                            //productmodel.COGS = item.UnitCOGS;
                            //productmodel.CurrencyId = model.CurrencyId;
                            //productmodel.ExchangeRate = model.ExchangeRate;
                            productmodel.ImportDate            = DateTime.Now;
                            _context.Entry(productmodel).State = System.Data.Entity.EntityState.Modified;
                            _context.SaveChanges();
                            #endregion
                        }

                        #region Insert InventoryDetail

                        #region groupby Importdetail
                        var detailgruoppd =
                            (from c in detail
                             group c by new
                        {
                            c.ProductId
                        } into gcs
                             select new ImportDetailViewModel()
                        {
                            ProductId = gcs.Key.ProductId,
                            Qty = gcs.Sum(p => p.Qty),
                            Price = gcs.Sum(p => p.Price),
                            UnitShippingWeight = gcs.Sum(p => p.UnitShippingWeight),
                            UnitPrice = gcs.Sum(p => p.UnitPrice),
                            ShippingFee = gcs.Sum(p => p.ShippingFee),
                            UnitCOGS = gcs.Sum(p => p.UnitCOGS)
                        }).ToList();
                        #endregion

                        foreach (var item in detailgruoppd)
                        {
                            item.UnitCOGS = (item.Price * model.ExchangeRate) + item.ShippingFee;
                            #region Insert
                            // Insert InventoryDetail
                            EndInventoryRepository EndInventoryRepo = new EndInventoryRepository(_context);
                            decimal tondau = EndInventoryRepo.GetQty(item.ProductId.Value);
                            var     tempt2 = _context.ProductModel.Where(p => p.ProductId == item.ProductId).FirstOrDefault();
                            decimal GiaVon = tempt2.COGS.HasValue ? tempt2.COGS.Value : 0;
                            InventoryDetailModel InvenDetail = new InventoryDetailModel()
                            {
                                InventoryMasterId = InvenMaster.InventoryMasterId,
                                ProductId         = item.ProductId,
                                BeginInventoryQty = tondau,
                                COGS = GiaVon,
                                //Price = item.Price,
                                ImportQty = item.Qty,
                                //ExportQty = 0,
                                UnitCOGS = GiaVon * item.Qty,
                                //UnitPrice = 0,
                                EndInventoryQty = tondau + item.Qty
                            };
                            _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                            #endregion
                        }

                        #endregion
                        // 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();

                        #region Cập nhật Tỉ giá và giá nhập,phí vận chuyển,giá vốn trên StoreProcedure

                        #endregion
                        return("success");
                    }
                    else
                    {
                        //chua nhap tt san pham
                        return("Vui lòng chọn thông tin sản phẩm");
                    }
                    #endregion
                }
            }
            catch
            {
                return("Xảy ra lỗi trong quá trình thêm mới sản phẩm từ nhà cung cấp");
            }
        }
コード例 #5
0
        public ActionResult Update(ImportMasterModel model, List <ImportDetailViewModel> detail, decimal TotalShippingWeight)
        {
            try
            {
                //_context.Entry(model).State = System.Data.Entity.EntityState.Modified; Chỉ Modified những filed cần thiết
                ImportMasterModel ImportMastermodel = _context.ImportMasterModel
                                                      .Include(p => p.ImportDetailModel)
                                                      .Where(p => p.ImportMasterId == model.ImportMasterId).FirstOrDefault();
                // Xoá những Detail cũ
                if (ImportMastermodel.ImportDetailModel != null && ImportMastermodel.ImportDetailModel.Count > 0)
                {
                    while (ImportMastermodel.ImportDetailModel.Count > 0)
                    {
                        _context.Entry(ImportMastermodel.ImportDetailModel.First()).State = System.Data.Entity.EntityState.Deleted;
                    }
                }

                #region // Update lại những filed cần Modified cho Model Master
                ImportMastermodel.ImportMasterId     = model.ImportMasterId;
                ImportMastermodel.SupplierId         = model.SupplierId;
                ImportMastermodel.SalemanName        = model.SalemanName;
                ImportMastermodel.SenderName         = model.SenderName;
                ImportMastermodel.ReceiverName       = model.ReceiverName;
                ImportMastermodel.Note               = model.Note;
                ImportMastermodel.CurrencyId         = model.CurrencyId;
                ImportMastermodel.ExchangeRate       = model.ExchangeRate;
                ImportMastermodel.WarehouseId        = model.WarehouseId;
                ImportMastermodel.VATType            = model.VATType;
                ImportMastermodel.VATValue           = model.VATValue;
                ImportMastermodel.TAXBillCode        = model.TAXBillCode;
                ImportMastermodel.TAXBillDate        = model.TAXBillDate;
                ImportMastermodel.ManualDiscountType = model.ManualDiscountType;
                ImportMastermodel.ManualDiscount     = model.ManualDiscount;
                ImportMastermodel.Paid               = model.Paid;
                ImportMastermodel.MoneyTransfer      = model.MoneyTransfer;
                ImportMastermodel.DebtDueDate        = model.DebtDueDate;
                ImportMastermodel.LastModifiedDate   = DateTime.Now;
                ImportMastermodel.TotalQty           = model.TotalQty;
                ImportMastermodel.TotalPrice         = model.TotalPrice;
                #endregion
                // đánh dấu sửa ImportMasterModel
                _context.Entry(ImportMastermodel).State = System.Data.Entity.EntityState.Modified;

                if (detail != null)
                {
                    foreach (var item in detail)
                    {
                        ImportDetailModel detailmodel = new ImportDetailModel()
                        {
                            ImportMasterId     = model.ImportMasterId,
                            ProductId          = item.ProductId,
                            Qty                = item.Qty,
                            Price              = item.Price,
                            UnitShippingWeight = item.UnitShippingWeight,
                            UnitPrice          = item.UnitPrice
                        };
                        _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added;
                    }
                }
                _context.SaveChanges();
                return(Content("success"));
            }
            catch
            {
                return(Content("Xảy ra lỗi trong quá trình thêm mới nhà cung cấp"));
            }
        }