feat: optimize inventory feature

This commit is contained in:
louis 2024-04-07 08:57:27 +08:00
parent 072777b5e5
commit 1399835845
6 changed files with 72 additions and 49 deletions

View File

@ -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

View File

@ -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的入库记录。 若有疑问,请联系管理员'
}

View File

@ -57,7 +57,7 @@ export class MaterialsInOutEntity extends CommonEntity {
@Column({
name: 'time',
type: 'date',
type: 'datetime',
nullable: true,
comment: '时间'
})

View File

@ -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',
@ -131,47 +132,60 @@ export class MaterialsInOutService {
async update(id: number, { fileIds, ...data }: Partial<MaterialsInOutUpdateDto>): Promise<void> {
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) {

View File

@ -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

View File

@ -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<string> {
return this.paramConfigService.findValueByKey(code);
}
@Get(':id')
@ApiOperation({ summary: '查询参数配置信息' })
@ApiResult({ type: ParamConfigEntity })