feat: optimize inventory feature
This commit is contained in:
parent
072777b5e5
commit
1399835845
|
@ -13,7 +13,7 @@ SWAGGER_PATH = api-docs
|
||||||
SWAGGER_VERSION = 1.0
|
SWAGGER_VERSION = 1.0
|
||||||
|
|
||||||
# db
|
# db
|
||||||
DB_HOST = 192.168.60.39
|
DB_HOST = localhost
|
||||||
DB_PORT = 13307
|
DB_PORT = 13307
|
||||||
DB_DATABASE = hxoa
|
DB_DATABASE = hxoa
|
||||||
DB_USERNAME = root
|
DB_USERNAME = root
|
||||||
|
@ -23,7 +23,7 @@ DB_LOGGING = "all"
|
||||||
|
|
||||||
# redis
|
# redis
|
||||||
REDIS_PORT = 6379
|
REDIS_PORT = 6379
|
||||||
REDIS_HOST = 192.168.60.39
|
REDIS_HOST = localhost
|
||||||
REDIS_PASSWORD = 123456
|
REDIS_PASSWORD = 123456
|
||||||
REDIS_DB = 0
|
REDIS_DB = 0
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,8 @@ export enum ErrorEnum {
|
||||||
CONTRACT_NUMBER_EXIST = '1407:存在相同的合同编号',
|
CONTRACT_NUMBER_EXIST = '1407:存在相同的合同编号',
|
||||||
|
|
||||||
// Inventory
|
// Inventory
|
||||||
INVENTORY_INSUFFICIENT = '1408:库存不足',
|
INVENTORY_INSUFFICIENT = '1408:库存数量不足。请检查库存或重新操作',
|
||||||
MATERIALS_IN_OUT_NOT_FOUND = '1409:出入库信息不存在',
|
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的入库记录。 若有疑问,请联系管理员'
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ export class MaterialsInOutEntity extends CommonEntity {
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
name: 'time',
|
name: 'time',
|
||||||
type: 'date',
|
type: 'datetime',
|
||||||
nullable: true,
|
nullable: true,
|
||||||
comment: '时间'
|
comment: '时间'
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
|
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
|
||||||
|
|
||||||
import { EntityManager, Repository } from 'typeorm';
|
import { EntityManager, In, Repository } from 'typeorm';
|
||||||
import { Pagination } from '~/helper/paginate/pagination';
|
import { Pagination } from '~/helper/paginate/pagination';
|
||||||
import { BusinessException } from '~/common/exceptions/biz.exception';
|
import { BusinessException } from '~/common/exceptions/biz.exception';
|
||||||
import { ErrorEnum } from '~/constants/error-code.constant';
|
import { ErrorEnum } from '~/constants/error-code.constant';
|
||||||
|
@ -79,6 +79,7 @@ export class MaterialsInOutService {
|
||||||
.addSelect([
|
.addSelect([
|
||||||
'inventory.id',
|
'inventory.id',
|
||||||
'inventory.position',
|
'inventory.position',
|
||||||
|
'inventory.inventoryNumber',
|
||||||
'files.id',
|
'files.id',
|
||||||
'files.path',
|
'files.path',
|
||||||
'project.name',
|
'project.name',
|
||||||
|
@ -103,7 +104,7 @@ export class MaterialsInOutService {
|
||||||
position,
|
position,
|
||||||
unitPrice,
|
unitPrice,
|
||||||
quantity,
|
quantity,
|
||||||
productId
|
productId
|
||||||
} = dto;
|
} = dto;
|
||||||
inventoryInOutNumber = await this.generateInventoryInOutNumber(inOrOut);
|
inventoryInOutNumber = await this.generateInventoryInOutNumber(inOrOut);
|
||||||
let newRecordId;
|
let newRecordId;
|
||||||
|
@ -131,47 +132,60 @@ export class MaterialsInOutService {
|
||||||
async update(id: number, { fileIds, ...data }: Partial<MaterialsInOutUpdateDto>): Promise<void> {
|
async update(id: number, { fileIds, ...data }: Partial<MaterialsInOutUpdateDto>): Promise<void> {
|
||||||
await this.entityManager.transaction(async manager => {
|
await this.entityManager.transaction(async manager => {
|
||||||
/* 暂时不允许更改金额和数量,以及不能影响库存变化, */
|
/* 暂时不允许更改金额和数量,以及不能影响库存变化, */
|
||||||
// const entity = await manager.findOne(MaterialsInOutEntity, {
|
const entity = await manager.findOne(MaterialsInOutEntity, {
|
||||||
// where: {
|
where: {
|
||||||
// id
|
id
|
||||||
// },
|
},
|
||||||
// lock: { mode: 'pessimistic_write' }
|
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
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
// 修改入库记录的价格
|
||||||
|
// 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;
|
// let changedQuantity = 0;
|
||||||
// if (isDefined(data.quantity) && entity.quantity !== data.quantity) {
|
// if (isDefined(data.quantity) && entity.quantity !== data.quantity) {
|
||||||
// if (entity.inOrOut === MaterialsInOrOutEnum.In) {
|
// if (entity.inOrOut === MaterialsInOrOutEnum.In) {
|
||||||
|
|
|
@ -477,7 +477,7 @@ export class MaterialsInventoryService {
|
||||||
let { quantity, id } = inventory;
|
let { quantity, id } = inventory;
|
||||||
const newQuantity = calcNumber(quantity || 0, outQuantity || 0, 'subtract');
|
const newQuantity = calcNumber(quantity || 0, outQuantity || 0, 'subtract');
|
||||||
if (isNaN(newQuantity)) {
|
if (isNaN(newQuantity)) {
|
||||||
throw new Error('库存数量不足。请检查库存或重新操作。');
|
throw new BusinessException(ErrorEnum.INVENTORY_INSUFFICIENT);
|
||||||
}
|
}
|
||||||
await manager.update(MaterialsInventoryEntity, id, {
|
await manager.update(MaterialsInventoryEntity, id, {
|
||||||
quantity: newQuantity
|
quantity: newQuantity
|
||||||
|
|
|
@ -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 { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
|
|
||||||
import { ApiResult } from '~/common/decorators/api-result.decorator';
|
import { ApiResult } from '~/common/decorators/api-result.decorator';
|
||||||
|
@ -41,6 +41,14 @@ export class ParamConfigController {
|
||||||
await this.paramConfigService.create(dto);
|
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')
|
@Get(':id')
|
||||||
@ApiOperation({ summary: '查询参数配置信息' })
|
@ApiOperation({ summary: '查询参数配置信息' })
|
||||||
@ApiResult({ type: ParamConfigEntity })
|
@ApiResult({ type: ParamConfigEntity })
|
||||||
|
|
Loading…
Reference in New Issue