From 1399835845a32942168dd8c684a6a9820e964788 Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Sun, 7 Apr 2024 08:57:27 +0800 Subject: [PATCH] feat: optimize inventory feature --- .env.development | 4 +- src/constants/error-code.constant.ts | 5 +- .../in_out/materials_in_out.entity.ts | 2 +- .../in_out/materials_in_out.service.ts | 98 +++++++++++-------- .../materials_inventory.service.ts | 2 +- .../param-config/param-config.controller.ts | 10 +- 6 files changed, 72 insertions(+), 49 deletions(-) diff --git a/.env.development b/.env.development index 673dab6..78a78df 100644 --- a/.env.development +++ b/.env.development @@ -13,7 +13,7 @@ SWAGGER_PATH = api-docs SWAGGER_VERSION = 1.0 # db -DB_HOST = 192.168.60.39 +DB_HOST = localhost DB_PORT = 13307 DB_DATABASE = hxoa DB_USERNAME = root @@ -23,7 +23,7 @@ DB_LOGGING = "all" # redis REDIS_PORT = 6379 -REDIS_HOST = 192.168.60.39 +REDIS_HOST = localhost REDIS_PASSWORD = 123456 REDIS_DB = 0 diff --git a/src/constants/error-code.constant.ts b/src/constants/error-code.constant.ts index 0b6be78..40cacd8 100644 --- a/src/constants/error-code.constant.ts +++ b/src/constants/error-code.constant.ts @@ -58,7 +58,8 @@ export enum ErrorEnum { CONTRACT_NUMBER_EXIST = '1407:存在相同的合同编号', // Inventory - INVENTORY_INSUFFICIENT = '1408:库存不足', + INVENTORY_INSUFFICIENT = '1408:库存数量不足。请检查库存或重新操作', MATERIALS_IN_OUT_NOT_FOUND = '1409:出入库信息不存在', - MATERIALS_IN_OUT_UNIT_PRICE_CANNOT_BE_MODIFIED = '1410:该价格的产品已经出库,单价不允许修改。若有疑问,请联系管理员' + MATERIALS_IN_OUT_UNIT_PRICE_CANNOT_BE_MODIFIED = '1410:该价格的产品已经出库,单价不允许修改。若有疑问,请联系管理员', + MATERIALS_IN_OUT_UNIT_PRICE_MUST_ZERO_WHEN_MODIFIED = '1411:只能修改初始单价为0的入库记录。 若有疑问,请联系管理员' } diff --git a/src/modules/materials_inventory/in_out/materials_in_out.entity.ts b/src/modules/materials_inventory/in_out/materials_in_out.entity.ts index 56c4057..6feef1f 100644 --- a/src/modules/materials_inventory/in_out/materials_in_out.entity.ts +++ b/src/modules/materials_inventory/in_out/materials_in_out.entity.ts @@ -57,7 +57,7 @@ export class MaterialsInOutEntity extends CommonEntity { @Column({ name: 'time', - type: 'date', + type: 'datetime', nullable: true, comment: '时间' }) diff --git a/src/modules/materials_inventory/in_out/materials_in_out.service.ts b/src/modules/materials_inventory/in_out/materials_in_out.service.ts index dfe9264..3b3c318 100644 --- a/src/modules/materials_inventory/in_out/materials_in_out.service.ts +++ b/src/modules/materials_inventory/in_out/materials_in_out.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm'; -import { EntityManager, Repository } from 'typeorm'; +import { EntityManager, In, Repository } from 'typeorm'; import { Pagination } from '~/helper/paginate/pagination'; import { BusinessException } from '~/common/exceptions/biz.exception'; import { ErrorEnum } from '~/constants/error-code.constant'; @@ -79,6 +79,7 @@ export class MaterialsInOutService { .addSelect([ 'inventory.id', 'inventory.position', + 'inventory.inventoryNumber', 'files.id', 'files.path', 'project.name', @@ -103,7 +104,7 @@ export class MaterialsInOutService { position, unitPrice, quantity, - productId + productId } = dto; inventoryInOutNumber = await this.generateInventoryInOutNumber(inOrOut); let newRecordId; @@ -131,47 +132,60 @@ export class MaterialsInOutService { async update(id: number, { fileIds, ...data }: Partial): Promise { await this.entityManager.transaction(async manager => { /* 暂时不允许更改金额和数量,以及不能影响库存变化, */ - // const entity = await manager.findOne(MaterialsInOutEntity, { - // where: { - // id - // }, - // lock: { mode: 'pessimistic_write' } - // }); - // - // 修改入库记录的价格,会直接更改库存实际价格. - // 1.如果有了出库记录,不允许修改入库价格。 - // if ( - // Object.is(data.inOrOut, MaterialsInOrOutEnum.In) && - // isDefined(data.unitPrice) && - // Math.abs(Number(data.unitPrice) - Number(entity.unitPrice)) !== 0 - // ) { - // const outEntity = await manager.findOne(MaterialsInOutEntity, { - // where: { - // unitPrice: entity.unitPrice, - // inOrOut: MaterialsInOrOutEnum.Out, - // projectId: entity.projectId, - // productId: entity.productId - // } - // }); - // if (isDefined(outEntity)) { - // throw new BusinessException(ErrorEnum.MATERIALS_IN_OUT_UNIT_PRICE_CANNOT_BE_MODIFIED); - // } - // await ( - // Object.is(data.inOrOut, MaterialsInOrOutEnum.In) - // ? this.materialsInventoryService.inInventory.bind(this.materialsInventoryService) - // : this.materialsInventoryService.outInventory.bind(this.materialsInventoryService) - // )( - // { - // productId: entity.productId, - // quantity: 0, - // unitPrice: entity.unitPrice, - // projectId: entity.projectId, - // changedUnitPrice: data.unitPrice - // }, - // manager - // ); - // } + const entity = await manager.findOne(MaterialsInOutEntity, { + where: { + id + }, + lock: { mode: 'pessimistic_write' } + }); + // 修改入库记录的价格 + // 1.会直接更改库存实际价格.(仅仅只能之前价格为0时可以修改) + // 2.会同步库存所有的出库记录,修改其单价和金额. + if ( + Object.is(data.inOrOut, MaterialsInOrOutEnum.In) && + isDefined(data.unitPrice) && + Math.abs(Number(data.unitPrice) - Number(entity.unitPrice)) !== 0 + ) { + if (entity.unitPrice != 0) { + throw new BusinessException( + ErrorEnum.MATERIALS_IN_OUT_UNIT_PRICE_MUST_ZERO_WHEN_MODIFIED + ); + } + const outEntities = await manager.find(MaterialsInOutEntity, { + where: { + inventoryId: entity.inventoryId, + inOrOut: MaterialsInOrOutEnum.Out + } + }); + if (outEntities?.length > 0) { + await manager.update( + MaterialsInOutEntity, + { + id: In(outEntities.map(item => item.id)) + }, + { + unitPrice: data.unitPrice, + amount: () => `quantity * ${data.unitPrice}` + } + ); + } + await manager.update(MaterialsInventoryEntity, entity.inventoryId, { + unitPrice: data.unitPrice + }); + } + // 修改入库时的项目,必须同步到库存项目中 + if ( + Object.is(data.inOrOut, MaterialsInOrOutEnum.In) && + isDefined(data.projectId) && + data.projectId != entity.projectId + ) { + await manager.update(MaterialsInventoryEntity, entity.inventoryId, { + projectId: data.projectId + }); + } + + // 暂时不允许修改数量 // let changedQuantity = 0; // if (isDefined(data.quantity) && entity.quantity !== data.quantity) { // if (entity.inOrOut === MaterialsInOrOutEnum.In) { diff --git a/src/modules/materials_inventory/materials_inventory.service.ts b/src/modules/materials_inventory/materials_inventory.service.ts index fd4a8d8..bcae7e7 100644 --- a/src/modules/materials_inventory/materials_inventory.service.ts +++ b/src/modules/materials_inventory/materials_inventory.service.ts @@ -477,7 +477,7 @@ export class MaterialsInventoryService { let { quantity, id } = inventory; const newQuantity = calcNumber(quantity || 0, outQuantity || 0, 'subtract'); if (isNaN(newQuantity)) { - throw new Error('库存数量不足。请检查库存或重新操作。'); + throw new BusinessException(ErrorEnum.INVENTORY_INSUFFICIENT); } await manager.update(MaterialsInventoryEntity, id, { quantity: newQuantity diff --git a/src/modules/system/param-config/param-config.controller.ts b/src/modules/system/param-config/param-config.controller.ts index baaddd7..b93fd70 100644 --- a/src/modules/system/param-config/param-config.controller.ts +++ b/src/modules/system/param-config/param-config.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Delete, Get, Post, Query } from '@nestjs/common'; +import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { ApiResult } from '~/common/decorators/api-result.decorator'; @@ -41,6 +41,14 @@ export class ParamConfigController { await this.paramConfigService.create(dto); } + @Get('key/:code') + @ApiOperation({ summary: '查询参数配置信息By key' }) + @ApiResult({ type: String }) + @Perm(permissions.READ) + async code(@Param('code') code: string): Promise { + return this.paramConfigService.findValueByKey(code); + } + @Get(':id') @ApiOperation({ summary: '查询参数配置信息' }) @ApiResult({ type: ParamConfigEntity })