예제 #1
0
        public async Task Handle(CreateReceiptDetailCommand notification, CancellationToken cancellationToken)
        {
            // Lấy về thông tin tồn kho.
            // Tồn kho theo sản phẩm
            var openingStock = await _inventoryReportRepository.GetOpeningStock(notification.TenantId,
                                                                                notification.WarehouseId, notification.ProductId,
                                                                                notification.Date);

            // Tồn kho theo lô.
            var openingStockByLot = await _inventoryReportRepository.GetOpeningStockByLot(notification.TenantId,
                                                                                          notification.WarehouseId,
                                                                                          notification.ProductId, notification.LotId, notification.Date);

            var openingStockQuantity      = openingStock?.ClosingStockQuantity ?? 0;
            var openingStockValue         = openingStock?.ClosingStockValue ?? 0;
            var openingStockQuantityByLot = openingStockByLot?.ClosingStockQuantity ?? 0;
            var openingStockValueByLot    = openingStockByLot?.ClosingStockValue ?? 0;

            // Trường hợp chưa tồn tại tồn khi thêm mới tồn kho. Nếu đã tồn tại cập nhập lại thông tin tồn kho.
            var inventoryReport = await _inventoryReportRepository.GetInfo(notification.TenantId, notification.WarehouseId,
                                                                           notification.ReceiptId, notification.ProductId, notification.IsReceived, notification.Date);

            if (inventoryReport == null)
            {
                inventoryReport = new InventoryReport
                {
                    ProductId            = notification.ProductId,
                    TenantId             = notification.TenantId,
                    ReceiptNo            = notification.ReceiptNo,
                    WarehouseId          = notification.WarehouseId,
                    ReceiptId            = notification.ReceiptId,
                    Date                 = notification.Date,
                    OpeningStockQuantity = openingStockQuantity,
                    OpeningStockValue    = openingStockValue,
                    ClosingStockValue    = notification.IsReceived
                        ? openingStockValue + notification.Price * notification.Quantity
                        : openingStockValue - notification.Price * notification.Quantity,
                    ClosingStockQuantity = notification.IsReceived
                        ? openingStockQuantity + notification.Quantity
                        : openingStockQuantity - notification.Quantity,
                    TotalAmounts  = notification.Quantity * notification.Price,
                    Quantity      = notification.Quantity,
                    ProductUnitId = notification.ProductUnitId,
                    IsReceived    = notification.IsReceived,
                };
                var inventoryReportDetail = new InventoryReportDetail
                {
                    Quantity                   = notification.Quantity,
                    Price                      = notification.Price,
                    ProductId                  = notification.ProductId,
                    InventoryReportId          = inventoryReport.Id,
                    GoodsReceiptNoteDetailCode = notification.Code,
                    OpeningStockQuantity       = openingStockQuantityByLot,
                    OpeningStockValue          = openingStockValueByLot,
                    ClosingStockValue          = notification.IsReceived
                        ? openingStockValueByLot + notification.Price * notification.Quantity
                        : openingStockQuantityByLot - notification.Price * notification.Quantity,
                    ClosingStockQuantity = notification.IsReceived
                        ? openingStockQuantity + notification.Quantity
                        : openingStockQuantity - notification.Quantity,
                    LotId         = notification.LotId,
                    Note          = notification.Note,
                    ProductUnitId = notification.ProductUnitId
                };
                inventoryReportDetail.ExWarehousePrice =
                    await _inventoryReportService.GetExWarehousePrice(notification.TenantId,
                                                                      notification.WarehouseId, openingStockValue, openingStockValue, notification.Date,
                                                                      inventoryReportDetail);

                inventoryReport.InventoryReportDetails.Add(inventoryReportDetail);

                await _inventoryReportRepository.Insert(inventoryReport);
            }
            else
            {
                // Cập nhật lại tồn kho và tồn kho chi tiết.
                var inventoryReportDetails =
                    await _inventoryReportDetailRepository.GetsAll(inventoryReport.Id, notification.ProductId);

                // Trường hợp chưa có báo cáo tồn chi tiết. Thêm mới báo cáo tồn chi tiết sau đó cập nhật lại báo cáo tồn.
                if (inventoryReportDetails == null || !inventoryReportDetails.Any())
                {
                    // Thêm mới báo cáo tồn chi tiết.
                    var inventoryReportDetail = new InventoryReportDetail
                    {
                        Quantity                   = notification.Quantity,
                        Price                      = notification.Price,
                        ProductId                  = notification.ProductId,
                        InventoryReportId          = inventoryReport.Id,
                        GoodsReceiptNoteDetailCode = notification.Code,
                        OpeningStockQuantity       = openingStockQuantityByLot,
                        OpeningStockValue          = openingStockValueByLot,
                        ClosingStockValue          = notification.IsReceived
                            ? openingStockValueByLot + notification.Price * notification.Quantity
                            : openingStockQuantityByLot - notification.Price * notification.Quantity,
                        ClosingStockQuantity = notification.IsReceived
                            ? openingStockQuantity + notification.Quantity
                            : openingStockQuantity - notification.Quantity,
                        LotId         = notification.LotId,
                        Note          = notification.Note,
                        ProductUnitId = notification.ProductUnitId
                    };
                    inventoryReportDetail.ExWarehousePrice =
                        await _inventoryReportService.GetExWarehousePrice(notification.TenantId,
                                                                          notification.WarehouseId, openingStockValue, openingStockValue, notification.Date,
                                                                          inventoryReportDetail);

                    var result = await _inventoryReportDetailRepository.Insert(inventoryReportDetail);

                    if (result > 0)
                    {
                        var reportStats =
                            await _inventoryReportDetailRepository.GetStats(inventoryReport.Id);

                        var totalQuantity = reportStats.Sum(x => x.Quantity);
                        var totalValues   = reportStats.Sum(x => x.Value);

                        // Cập nhật lại tồn kho.
                        inventoryReport.ClosingStockQuantity = inventoryReport.IsReceived
                            ? openingStockQuantity + totalQuantity
                            : openingStockQuantity - totalQuantity;
                        inventoryReport.ClosingStockValue = inventoryReport.IsReceived
                            ? openingStockValue + totalValues
                            : openingStockValue - totalValues;
                        inventoryReport.TotalAmounts = totalValues;
                        inventoryReport.Quantity     = totalQuantity;
                    }
                }
                else
                {
                    var inventoryReportDetail =
                        inventoryReportDetails.FirstOrDefault(x => x.LotId == notification.LotId);
                    if (inventoryReportDetail != null)
                    {
                        // Cập nhật lại kết quả.
                        inventoryReportDetail.OpeningStockQuantity = openingStockQuantityByLot;
                        inventoryReportDetail.OpeningStockValue    = openingStockValueByLot;
                        inventoryReportDetail.Quantity             = notification.Quantity;
                        inventoryReportDetail.Price = notification.Price;
                    }
                }
            }
        }
예제 #2
0
        public async Task <ActionResultResponse <ProductInfoViewModel> > GetProductInfoByCode(string tenantId, string userId, string code,
                                                                                              string warehouseId, GoodsDeliveryNoteType type, DateTime deliveryDate)
        {
            // Kiểm tra người dùng có quyền trên kho hiện tại hay không.
            var isWarehouseManagement = await _warehouseManagerConfigRepository.CheckExists(
                warehouseId, userId, tenantId);

            if (!isWarehouseManagement)
            {
                return(new ActionResultResponse <ProductInfoViewModel>(-403, _sharedResourceService.GetString(ErrorMessage.NotHavePermission)));
            }

            var productInfo = await _goodsReceiptNoteDetailRepository.GetProductInfoByCode(tenantId, warehouseId, code);

            if (productInfo == null)
            {
                return(new ActionResultResponse <ProductInfoViewModel>(-1, _resourceService.GetString("Product you are looking for does not exits.",
                                                                                                      _sharedResourceService.GetString(ErrorMessage.Sorry))));
            }

            // Lấy về số lượng tồn kho của sản phẩm.
            var openingStock = !string.IsNullOrEmpty(productInfo.LotId)
                ? await _inventoryReportRepository.GetOpeningStockByLot(tenantId, warehouseId, productInfo.ProductId, productInfo.LotId,
                                                                        deliveryDate)
                    : await _inventoryReportRepository.GetOpeningStock(tenantId, warehouseId, productInfo.ProductId,
                                                                       deliveryDate);

            if (openingStock == null || openingStock.ClosingStockValue <= 0)
            {
                return(new ActionResultResponse <ProductInfoViewModel>(-2, _resourceService.GetString("Out of stock.",
                                                                                                      _sharedResourceService.GetString(ErrorMessage.Sorry))));
            }

            productInfo.InventoryQuantity = openingStock.ClosingStockQuantity;
            productInfo.Price             = openingStock.ExWarehousePrice;

            productInfo.Price = !string.IsNullOrEmpty(productInfo.LotId)
                ? await _inventoryReportRepository.GetExWarehousePriceByCode(tenantId, warehouseId, code)
                : openingStock.ExWarehousePrice;

            productInfo.InventoryQuantity = openingStock.ClosingStockQuantity;
            productInfo.Units             = await _productUnitRepository.GetByProductId(tenantId, productInfo.ProductId);

            var conversionUnit = productInfo.Units.FirstOrDefault(x => x.UnitId == productInfo.UnitId);

            if (conversionUnit == null)
            {
                return(new ActionResultResponse <ProductInfoViewModel>(-4, _resourceService.GetString("Product unit does not exists. Please contact with administrator.")));
            }

            // Lấy về tổng sản phẩm theo phiếu nhập.
            var receivedQuantities = await _goodsReceiptNoteDetailRepository.GetQuantitiesByCode(tenantId,
                                                                                                 warehouseId, code);

            // Tổng xuất theo mã sản phẩm thuộc phiếu nhập.
            var deliveryQuantities = await _goodsDeliveryNoteDetailsRepository.GetQuantiesByCode(tenantId,
                                                                                                 warehouseId, code, string.Empty);

            productInfo.RealInventoryQuantity = receivedQuantities - deliveryQuantities;
            return(new ActionResultResponse <ProductInfoViewModel>
            {
                Data = productInfo
            });
        }