schangxiang@126.com
2025-09-09 3d8966ba2c81e7e0365c8b123e861d18ee4f94f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import BaseService from "../baseService";
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
import { StorageAdjust } from "../../entity/storage/adjust/storageAdjust";
import { StorageAdjustList } from "../../entity/storage/adjust/storageAdjustList";
 
/**
 * 其他入库单
 */
export default class adjustService extends BaseService {
  /**
   * 添加保存前事件
   */
  public async onAddSaveBefore(t: StorageAdjust) {
    let userInfo = await this.userInfo;
    this.info.result = true;
 
    // let dataInfo: any = t;
 
    // let detailList = dataInfo.Storage_AdjustList;
    let detailList = await this.dbRead.find(StorageAdjustList, {
      adjust_Id: t.adjust_Id
    });
 
    if (!detailList || !detailList.length) {
      return this.info;
    }
    // 当数据量大时,需要在这进行计算
    let totalProductStorage = 0; // 合计账面库存量
    let totalPurchaseMoney = 0.0; // 合计账面成本额
    let totalCheckQuantity = 0; // 合计盘点数量
    let totalProfitQuantity = 0; // 合计盘盈数量
    let totalProfitMoney = 0.0; // 合计盘盈金额
    let totalLossQuantity = 0; // 合计盘亏数量
    let totalLossMoney = 0.0; // 合计盘亏金额
 
    // 保存前循环判断明细货位是否存在
    for (let slist of detailList) {
      let contractInfo = await this.dbRead.findOne(BasePosition, {
        storageName: t.storageName,
        userProduct_Id: userInfo.userProduct_Id,
        positionName: slist.positionName
      });
      if (!contractInfo) {
        this.info.result = false;
        this.info.msg = `${t.storageName},仓库中没有,${slist.positionName}货位,不能保存!`;
        return this.info;
      }
      //对数据进行计算
      totalProductStorage += slist.productStorage || 0;
      totalPurchaseMoney += slist.purchaseMoney || 0;
      totalCheckQuantity += slist.checkQuantity || 0;
      if (slist.checkQuantity > slist.productStorage) {
        var profitQuantity = slist.checkQuantity - slist.productStorage;
        var ProfitMoney = profitQuantity * slist.purchasePrice;
        totalProfitQuantity += profitQuantity;
        totalProfitMoney += ProfitMoney;
      } else if (slist.checkQuantity < slist.productStorage) {
        var lossQuantity = slist.productStorage - slist.checkQuantity;
        var lossMoney = lossQuantity * slist.purchasePrice;
        totalLossQuantity += lossQuantity;
        totalLossMoney += lossMoney;
      } else {
        slist.profitQuantity = 0;
        slist.lossQuantity = 0;
        slist.lossMoney = 0;
      }
    }
    t.totalProductStorage = totalProductStorage; // 合计账面库存量
    t.totalPurchaseMoney = totalPurchaseMoney; // 合计账面成本额
    t.totalCheckQuantity = totalCheckQuantity; // 合计盘点数量
    t.totalProfitQuantity = totalProfitQuantity; // 合计盘盈数量
    t.totalProfitMoney = totalProfitMoney; // 合计盘盈金额
    t.totalLossQuantity = totalLossQuantity; // 合计盘亏数量
    t.totalLossMoney = totalLossMoney; // 合计盘亏金额
 
    return this.info;
  }
}