Ejemplo n.º 1
0
        public async Task <ActionResultResponse> Delete(string tenantId, string id)
        {
            var unitInfo = await _unitRepository.GetInfo(tenantId, id);

            if (unitInfo == null)
            {
                return(new ActionResultResponse(-1, _resourceService.GetString("Unit does not exists.")));
            }

            var isExistsProductUnit = await _productUnitRepository.CheckExists(id, tenantId);

            if (isExistsProductUnit)
            {
                return(new ActionResultResponse(-1, _resourceService.GetString("Unit already use.")));
            }

            var result = await _unitRepository.Delete(tenantId, id);

            var unitTranslations = await _unitTranslationRepository.GetAllUnitTranslation(tenantId, id);

            foreach (var unitTranslation in unitTranslations)
            {
                await _unitTranslationRepository.Delete(unitTranslation.UnitId, unitTranslation.LanguageId, tenantId);
            }

            return(new ActionResultResponse(result, result <= 0 ? _resourceService.GetString("Something went wrong. Please contact with administrator.")
                    : _resourceService.GetString("Delete Unit successful.")));
        }
Ejemplo n.º 2
0
        public async Task <ActionResultResponse> Insert(ProductUnit productUnit)
        {
            var productInfo = await _productRepository.CheckExists(productUnit.ProductId, productUnit.TenantId);

            if (!productInfo)
            {
                return(new ActionResultResponse(-1, _sharedResourceService.GetString(ErrorMessage.NotExists, _resourceService.GetString("product"))));
            }

            // Kiểm tra đơn vị đã tồn tại chưa.
            var isExists = await _productUnitRepository.CheckExists(productUnit.TenantId, productUnit.ProductId, productUnit.UnitId);

            if (isExists)
            {
                return(new ActionResultResponse(-2, _sharedResourceService.GetString(ErrorMessage.AlreadyExists, _resourceService.GetString("unit"))));
            }

            if (productUnit.IsDefault)
            {
                // Kiểm tra đã tồn tại đơn vị mặc định chưa.
                var isDefaultUnitExists = await _productUnitRepository.CheckDefaultUnitExists(productUnit.ProductId, productUnit.TenantId);

                if (isDefaultUnitExists)
                {
                    return(new ActionResultResponse(-3, _sharedResourceService.GetString(ErrorMessage.AlreadyExists, _resourceService.GetString("default unit"))));
                }
            }

            var result = await _productUnitRepository.Insert(productUnit);

            return(new ActionResultResponse(result, result <= 0
                                                    ? _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong)
                    : _sharedResourceService.GetString(SuccessMessage.AddSuccessful, _resourceService.GetString("Product unit"))));
        }
Ejemplo n.º 3
0
        public async Task <ActionResultResponse <dynamic> > Insert(string tenantId, string userId, string receiptId,
                                                                   GoodsReceiptNoteDetailMeta goodsReceiptNoteDetailMeta)
        {
            var goodsReceiptNote = await _goodsReceiptNoteRepository.GetInfo(tenantId, receiptId);

            if (goodsReceiptNote == null)
            {
                return(new ActionResultResponse <dynamic>(-1, _sharedResourceService.GetString(ErrorMessage.NotExists, _resourceService.GetString("Goods receipt note"))));
            }

            // Kiểm tra lô sản phẩm.
            var isManagementByLot = await _productRepository.CheckIsManageByLot(tenantId, goodsReceiptNoteDetailMeta.ProductId);

            if (isManagementByLot && string.IsNullOrEmpty(goodsReceiptNoteDetailMeta.LotId))
            {
                return(new ActionResultResponse <dynamic>(-2, _sharedResourceService.GetString(ValidatorMessage.PleaseEnter, _resourceService.GetString("Lot number"))));
            }

            // Kiểm tra sản phẩm có tồn tại không.
            var isProductExists = await _productRepository.CheckExists(goodsReceiptNoteDetailMeta.ProductId, tenantId);

            if (!isProductExists)
            {
                return(new ActionResultResponse <dynamic>(-3, _resourceService.GetString("Product does not exists. Please try again.")));
            }

            // Kiểm tra đơn vị tính có tồn tại không.
            var isUnitExists = await _productUnitRepository.CheckExists(tenantId, goodsReceiptNoteDetailMeta.ProductId,
                                                                        goodsReceiptNoteDetailMeta.UnitId);

            if (!isUnitExists)
            {
                return(new ActionResultResponse <dynamic>(-4, _resourceService.GetString("Invalid unit. Please check again.")));
            }

            var defaultUnit = await _productUnitRepository.GetDefaultUnit(goodsReceiptNote.TenantId, goodsReceiptNoteDetailMeta.ProductId);

            if (defaultUnit == null)
            {
                return(new ActionResultResponse <dynamic>(-5,
                                                          _sharedResourceService.GetString(ErrorMessage.NotExists, _resourceService.GetString("product unit"))));
            }

            decimal conversionValue = 1;

            if (defaultUnit.UnitId != goodsReceiptNoteDetailMeta.UnitId)
            {
                // Lấy về đơn vị chuyển đổi.
                var productConversionUnit = await _productConversionUnitRepository.GetConversion(goodsReceiptNote.TenantId,
                                                                                                 goodsReceiptNoteDetailMeta.ProductId, goodsReceiptNoteDetailMeta.UnitId, defaultUnit.UnitId);

                if (productConversionUnit == null)
                {
                    return(new ActionResultResponse <dynamic>(-6,
                                                              _sharedResourceService.GetString(ErrorMessage.NotExists, _resourceService.GetString("Product conversion unit"))));
                }

                conversionValue = productConversionUnit.Value;
            }

            var goodsReceiptNoteDetail = new GoodsReceiptNoteDetail
            {
                Code               = await GetCode(tenantId, goodsReceiptNote.Id, goodsReceiptNoteDetailMeta.ProductId, goodsReceiptNoteDetailMeta.LotId),
                ChargeableWeight   = goodsReceiptNoteDetailMeta.ChargeableWeight,
                BrandId            = goodsReceiptNoteDetailMeta.BrandId,
                ExpiryDate         = goodsReceiptNoteDetailMeta.ExpiryDate,
                GoodsReceiptNoteId = receiptId,
                Height             = goodsReceiptNoteDetailMeta.Height,
                InvoiceQuantity    = goodsReceiptNoteDetailMeta.InvoiceQuantity ?? goodsReceiptNoteDetailMeta.Quantity,
                Quantity           = goodsReceiptNoteDetailMeta.Quantity,
                Length             = goodsReceiptNoteDetailMeta.Length,
                LotId              = await GetLotId(tenantId, goodsReceiptNoteDetailMeta.LotId),
                ManufactureDate    = goodsReceiptNoteDetailMeta.ManufacturingDate,
                Price              = goodsReceiptNoteDetailMeta.Price,
                ProductId          = goodsReceiptNoteDetailMeta.ProductId,
                SKU                   = goodsReceiptNoteDetailMeta.SKU,
                UPC                   = goodsReceiptNoteDetailMeta.UPC,
                VAT                   = goodsReceiptNoteDetailMeta.VAT,
                UnitId                = goodsReceiptNoteDetailMeta.UnitId,
                VolumnWidth           = goodsReceiptNoteDetailMeta.VolumnWidth,
                WarehouseId           = goodsReceiptNote.WarehouseId,
                Weight                = goodsReceiptNoteDetailMeta.Weight,
                Width                 = goodsReceiptNoteDetailMeta.Width,
                TotalBeforeTaxes      = goodsReceiptNoteDetailMeta.Quantity * goodsReceiptNoteDetailMeta.Price,
                TenantId              = tenantId,
                ConversionUnitGroupId = await _productConversionUnitRepository.GetCurrentConversionUnitGroupId(tenantId,
                                                                                                               goodsReceiptNoteDetailMeta.ProductId, goodsReceiptNote.EntryDate),
                ConversionValue = conversionValue
            };

            if (goodsReceiptNoteDetailMeta.Tax.HasValue)
            {
                goodsReceiptNoteDetail.Tax          = goodsReceiptNoteDetailMeta.Tax;
                goodsReceiptNoteDetail.Taxes        = goodsReceiptNoteDetail.Tax.Value * goodsReceiptNoteDetail.TotalBeforeTaxes / 100;
                goodsReceiptNoteDetail.TotalAmounts = goodsReceiptNoteDetail.TotalBeforeTaxes + (goodsReceiptNoteDetail.Taxes ?? 0);
            }
            else
            {
                goodsReceiptNoteDetail.TotalAmounts = goodsReceiptNoteDetail.TotalBeforeTaxes;
            }

            var result = await _goodsReceiptNoteDetailRepository.Insert(goodsReceiptNoteDetail);

            if (result <= 0)
            {
                return(new ActionResultResponse <dynamic>(-5, _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong)));
            }

            // Cập nhật lại giá trị đơn nhập
            await UpdateGoodsReceiptNoteTotalAmount(goodsReceiptNoteDetail.GoodsReceiptNoteId);

            // Cập nhật lại tổng số item trong phiếu nhập.
            await UpdateGoodsReceiptNoteTotalItem(goodsReceiptNoteDetail.GoodsReceiptNoteId);

            // Cập nhật báo cáo tồn kho.
            await UpdateInventoryReport();

            return(new ActionResultResponse <dynamic>(result, _resourceService.GetString("Add product successful."), "", new
            {
                goodsReceiptNoteDetail.Id,
                goodsReceiptNoteDetail.ConcurrencyStamp
            }));

            async Task UpdateInventoryReport()
            {
                await _mediator.Publish(new CreateReceiptNoteDetailCommand
                {
                    TenantId                   = goodsReceiptNoteDetail.TenantId,
                    ReceiptId                  = receiptId,
                    Date                       = goodsReceiptNote.EntryDate,
                    IsReceived                 = true,
                    ProductId                  = goodsReceiptNoteDetail.ProductId,
                    WarehouseId                = goodsReceiptNoteDetail.WarehouseId,
                    Note                       = goodsReceiptNoteDetail.Note,
                    ReceiptDetailId            = goodsReceiptNoteDetail.Id,
                    LotId                      = goodsReceiptNoteDetail.LotId,
                    GoodsReceiptNoteDetailCode = goodsReceiptNoteDetail.Code,
                    ProductUnitId              = defaultUnit.Id,
                    ReceiptNo                  = goodsReceiptNote.ReceiptNo,
                });
            }
        }