コード例 #1
0
        public ActionResult Exchange(int Id)
        {
            var PhysicalInventory = PhysicalInventoryRepository.GetPhysicalInventoryById(Id);

            if (PhysicalInventory == null)
            {
                TempData[Globals.FailedMessageKey] = App_GlobalResources.Wording.NotfoundObject;
                if (PhysicalInventory.Type == PhysicalInventoryType.Product)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(RedirectToAction("ListForCard"));
                }
            }

            List <ProductOutboundDetail> outboundDetails = new List <ProductOutboundDetail>();
            List <ProductInboundDetail>  inboundDetails  = new List <ProductInboundDetail>();

            var listDetail = PhysicalInventoryRepository.GetAllPhysicalInventoryDetail(Id).Where(x => x.QuantityInInventory != x.QuantityRemaining).ToList();

            foreach (var item in listDetail)
            {
                var product = ProductRepository.GetProductById(item.ProductId);

                if (item.QuantityDiff < 0) //Chênh lệch dương thì thuộc về xuất
                {
                    outboundDetails.Add(
                        new ProductOutboundDetail
                    {
                        IsDeleted      = false,
                        CreatedDate    = DateTime.Now,
                        CreatedUserId  = Helpers.Common.CurrentUser.Id,
                        ModifiedDate   = DateTime.Now,
                        ModifiedUserId = Helpers.Common.CurrentUser.Id,
                        Price          = product.PriceOutbound,
                        ProductId      = product.Id,
                        Quantity       = Math.Abs(item.QuantityDiff),
                    }
                        );
                }
                else if (item.QuantityDiff > 0) //Chênh lệch âm thì thuộc về nhập
                {
                    inboundDetails.Add(
                        new ProductInboundDetail
                    {
                        IsDeleted      = false,
                        CreatedDate    = DateTime.Now,
                        CreatedUserId  = Helpers.Common.CurrentUser.Id,
                        ModifiedDate   = DateTime.Now,
                        ModifiedUserId = Helpers.Common.CurrentUser.Id,
                        Price          = product.PriceInbound.Value,
                        ProductId      = product.Id,
                        Quantity       = Math.Abs(item.QuantityDiff),
                    }
                        );
                }
            }

            if (outboundDetails.Count != 0)
            {
                var outbound = new ProductOutbound
                {
                    IsDeleted           = false,
                    CreatedDate         = DateTime.Now,
                    CreatedUserId       = Helpers.Common.CurrentUser.Id,
                    ModifiedDate        = DateTime.Now,
                    ModifiedUserId      = Helpers.Common.CurrentUser.Id,
                    BranchId            = Helpers.Common.CurrentUser.BranchId,
                    IsDone              = true,
                    Type                = PhysicalInventory.Type == PhysicalInventoryType.Product ? ProductOutboundType.Physical : ProductOutboundType.PhysicalCard,
                    TotalAmount         = outboundDetails.Sum(x => x.Quantity * x.Price),
                    WarehouseSourceId   = PhysicalInventory.WarehouseId,
                    PhysicalInventoryId = PhysicalInventory.Id,
                    Note                = "Xuất kho kiểm kê"
                };

                productOutboundRepository.InsertProductOutbound(outbound);

                foreach (var item in outboundDetails)
                {
                    item.ProductOutboundId = outbound.Id;
                    item.IsDeleted         = false;
                    item.CreatedUserId     = WebSecurity.CurrentUserId;
                    item.ModifiedUserId    = WebSecurity.CurrentUserId;
                    item.CreatedDate       = DateTime.Now;
                    item.ModifiedDate      = DateTime.Now;
                    productOutboundRepository.InsertProductOutboundDetail(item);
                }

                //cập nhật lại mã xuất kho
                outbound.Code = Erp.BackOffice.Helpers.Common.GetOrderNo("ProductOutbound");
                productOutboundRepository.UpdateProductOutbound(outbound);
                Erp.BackOffice.Helpers.Common.SetOrderNo("ProductOutbound");
                foreach (var item in listDetail.Where(x => x.QuantityDiff < 0))
                {
                    item.ModifiedDate     = DateTime.Now;
                    item.ModifiedUserId   = Helpers.Common.CurrentUser.Id;
                    item.ReferenceVoucher = outbound.Code;
                    PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item);
                }
                ProductOutboundController.Archive(productOutboundRepository, outbound, TempData);
            }

            if (inboundDetails.Count != 0)
            {
                var inbound = new ProductInbound
                {
                    IsDeleted              = false,
                    CreatedDate            = DateTime.Now,
                    CreatedUserId          = Helpers.Common.CurrentUser.Id,
                    ModifiedDate           = DateTime.Now,
                    ModifiedUserId         = Helpers.Common.CurrentUser.Id,
                    BranchId               = Helpers.Common.CurrentUser.BranchId,
                    IsDone                 = true,
                    Type                   = PhysicalInventory.Type == PhysicalInventoryType.Product ? ProductInboundType.Physical : ProductInboundType.PhysicalCard,
                    TotalAmount            = inboundDetails.Sum(x => x.Quantity * x.Price),
                    WarehouseDestinationId = PhysicalInventory.WarehouseId,
                    PhysicalInventoryId    = PhysicalInventory.Id,
                    Note                   = "Nhập kho kiểm kê"
                };

                productInboundRepository.InsertProductInbound(inbound);

                //Thêm chi tiết phiếu nhập
                foreach (var item in inboundDetails)
                {
                    item.ProductInboundId = inbound.Id;
                    item.IsDeleted        = false;
                    item.CreatedUserId    = WebSecurity.CurrentUserId;
                    item.ModifiedUserId   = WebSecurity.CurrentUserId;
                    item.CreatedDate      = DateTime.Now;
                    item.ModifiedDate     = DateTime.Now;
                    productInboundRepository.InsertProductInboundDetail(item);
                }

                //cập nhật lại mã xuất kho
                inbound.Code = Erp.BackOffice.Helpers.Common.GetOrderNo("ProductInbound");
                productInboundRepository.UpdateProductInbound(inbound);
                Erp.BackOffice.Helpers.Common.SetOrderNo("ProductInbound");
                foreach (var item in listDetail.Where(x => x.QuantityDiff > 0))
                {
                    item.ModifiedDate     = DateTime.Now;
                    item.ModifiedUserId   = Helpers.Common.CurrentUser.Id;
                    item.ReferenceVoucher = inbound.Code;
                    PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item);
                }
                ProductInboundController.Archive(productInboundRepository, inbound, TempData);
            }

            PhysicalInventory.IsExchange     = true;
            PhysicalInventory.ModifiedDate   = DateTime.Now;
            PhysicalInventory.ModifiedUserId = Helpers.Common.CurrentUser.Id;
            PhysicalInventoryRepository.UpdatePhysicalInventory(PhysicalInventory);
            TempData[Globals.SuccessMessageKey] += App_GlobalResources.Wording.Success;
            return(RedirectToAction("Detail", new { Id = PhysicalInventory.Id }));
        }
コード例 #2
0
        public ActionResult Exchange(int Id)
        {
            var PhysicalInventory = PhysicalInventoryRepository.GetvwPhysicalInventoryById(Id);
            var model             = new PhysicalInventory();

            if (PhysicalInventory == null)
            {
                TempData[Globals.FailedMessageKey] = App_GlobalResources.Wording.NotfoundObject;
                return(RedirectToAction("Index"));
            }
            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    AutoMapper.Mapper.Map(PhysicalInventory, model);
                    List <ProductOutboundDetail> outboundDetails = new List <ProductOutboundDetail>();
                    List <ProductInboundDetail>  inboundDetails  = new List <ProductInboundDetail>();
                    var outbounds = productOutboundRepository.GetAllProductOutbound().Where(item => item.Code == PhysicalInventory.ProductOutboundCode);
                    if (outbounds.Count() > 0)
                    {
                        //Xóa chi tiết xuất cũ
                        var outboundDetails_old = productOutboundRepository.GetAllProductOutboundDetailByOutboundId(outbounds.FirstOrDefault().Id).Select(item => item.Id).ToList();
                        foreach (var item in outboundDetails_old)
                        {
                            productOutboundRepository.DeleteProductOutboundDetail(item);
                        }
                        productOutboundRepository.DeleteProductOutbound(outbounds.FirstOrDefault().Id);
                    }
                    var inbounds = productInboundRepository.GetAllProductInbound().Where(item => item.Code == PhysicalInventory.ProductInboundCode);
                    if (inbounds.Count() > 0)
                    {
                        //Xóa chi tiết nhập cũ
                        var inboundDetails_old = productInboundRepository.GetAllProductInboundDetailByInboundId(inbounds.FirstOrDefault().Id).Select(item => item.Id).ToList();
                        foreach (var item in inboundDetails_old)
                        {
                            productInboundRepository.DeleteProductInboundDetail(item);
                        }
                        productInboundRepository.DeleteProductInbound(inbounds.FirstOrDefault().Id);
                    }

                    var listDetail = PhysicalInventoryRepository.GetAllPhysicalInventoryDetail(Id).Where(x => x.QuantityInInventory != x.QuantityRemaining).ToList();
                    foreach (var item in listDetail)
                    {
                        var product = ProductRepository.GetProductById(item.ProductId);

                        if (item.QuantityDiff < 0) //Chênh lệch dương thì thuộc về xuất
                        {
                            outboundDetails.Add(
                                new ProductOutboundDetail
                            {
                                IsDeleted      = false,
                                CreatedDate    = DateTime.Now,
                                CreatedUserId  = Helpers.Common.CurrentUser.Id,
                                ModifiedDate   = DateTime.Now,
                                ModifiedUserId = Helpers.Common.CurrentUser.Id,
                                Price          = product.PriceOutbound,
                                ProductId      = product.Id,
                                Quantity       = Math.Abs(item.QuantityDiff),
                                ExpiryDate     = item.ExpiryDate,
                                LoCode         = item.LoCode
                            }
                                );
                        }
                        else if (item.QuantityDiff > 0) //Chênh lệch âm thì thuộc về nhập
                        {
                            inboundDetails.Add(
                                new ProductInboundDetail
                            {
                                IsDeleted      = false,
                                CreatedDate    = DateTime.Now,
                                CreatedUserId  = Helpers.Common.CurrentUser.Id,
                                ModifiedDate   = DateTime.Now,
                                ModifiedUserId = Helpers.Common.CurrentUser.Id,
                                Price          = product.PriceInbound,
                                ProductId      = product.Id,
                                Quantity       = Math.Abs(item.QuantityDiff),
                                LoCode         = item.LoCode,
                                ExpiryDate     = item.ExpiryDate
                            }
                                );
                        }
                    }

                    if (outboundDetails.Count != 0)
                    {
                        var outbound = new ProductOutbound
                        {
                            IsDeleted           = false,
                            CreatedDate         = DateTime.Now,
                            CreatedUserId       = Helpers.Common.CurrentUser.Id,
                            ModifiedDate        = DateTime.Now,
                            ModifiedUserId      = Helpers.Common.CurrentUser.Id,
                            BranchId            = model.BranchId,
                            IsDone              = true,
                            Type                = "PhysicalInventory",
                            TotalAmount         = outboundDetails.Sum(x => x.Quantity * x.Price),
                            WarehouseSourceId   = PhysicalInventory.WarehouseId,
                            PhysicalInventoryId = PhysicalInventory.Id,
                            Note                = "Xuất kho kiểm kê"
                        };

                        productOutboundRepository.InsertProductOutbound(outbound);

                        foreach (var item in outboundDetails)
                        {
                            item.ProductOutboundId = outbound.Id;
                            item.IsDeleted         = false;
                            item.CreatedUserId     = WebSecurity.CurrentUserId;
                            item.ModifiedUserId    = WebSecurity.CurrentUserId;
                            item.CreatedDate       = DateTime.Now;
                            item.ModifiedDate      = DateTime.Now;
                            productOutboundRepository.InsertProductOutboundDetail(item);
                        }

                        //cập nhật lại mã xuất kho
                        string prefix = Erp.BackOffice.Helpers.Common.GetSetting("prefixOrderNo_Outbound");
                        outbound.Code = Erp.BackOffice.Helpers.Common.GetCode(prefix, outbound.Id);
                        productOutboundRepository.UpdateProductOutbound(outbound);

                        foreach (var item in listDetail.Where(x => x.QuantityDiff < 0))
                        {
                            item.ModifiedDate     = DateTime.Now;
                            item.ModifiedUserId   = Helpers.Common.CurrentUser.Id;
                            item.ReferenceVoucher = outbound.Code;
                            PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item);
                        }
                    }

                    if (inboundDetails.Count != 0)
                    {
                        var inbound = new ProductInbound
                        {
                            IsDeleted              = false,
                            CreatedDate            = DateTime.Now,
                            CreatedUserId          = Helpers.Common.CurrentUser.Id,
                            ModifiedDate           = DateTime.Now,
                            ModifiedUserId         = Helpers.Common.CurrentUser.Id,
                            BranchId               = model.BranchId,
                            IsDone                 = true,
                            Type                   = "PhysicalInventory",
                            TotalAmount            = inboundDetails.Sum(x => x.Quantity * x.Price),
                            WarehouseDestinationId = PhysicalInventory.WarehouseId,
                            PhysicalInventoryId    = PhysicalInventory.Id,
                            Note                   = "Nhập kho kiểm kê"
                        };

                        productInboundRepository.InsertProductInbound(inbound);

                        //Thêm chi tiết phiếu nhập
                        foreach (var item in inboundDetails)
                        {
                            item.ProductInboundId = inbound.Id;
                            item.IsDeleted        = false;
                            item.CreatedUserId    = WebSecurity.CurrentUserId;
                            item.ModifiedUserId   = WebSecurity.CurrentUserId;
                            item.CreatedDate      = DateTime.Now;
                            item.ModifiedDate     = DateTime.Now;
                            productInboundRepository.InsertProductInboundDetail(item);
                        }

                        //cập nhật lại mã xuất kho
                        string prefix = Erp.BackOffice.Helpers.Common.GetSetting("prefixOrderNo_Inbound");
                        inbound.Code = Erp.BackOffice.Helpers.Common.GetCode(prefix, inbound.Id);
                        productInboundRepository.UpdateProductInbound(inbound);

                        foreach (var item in listDetail.Where(x => x.QuantityDiff > 0))
                        {
                            item.ModifiedDate     = DateTime.Now;
                            item.ModifiedUserId   = Helpers.Common.CurrentUser.Id;
                            item.ReferenceVoucher = inbound.Code;
                            PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item);
                        }
                    }

                    model.IsExchange     = true;
                    model.ModifiedDate   = DateTime.Now;
                    model.ModifiedUserId = Helpers.Common.CurrentUser.Id;
                    PhysicalInventoryRepository.UpdatePhysicalInventory(model);
                    scope.Complete();
                }
                catch (DbUpdateException)
                {
                    return(Content("Fail"));
                }
            }
            TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.Success;
            return(RedirectToAction("Detail", new { Id = PhysicalInventory.Id }));
        }