t

mail@pastecode.io avatar
unknown
typescript
7 months ago
4.1 kB
2
Indexable
Never
static computeVorList(estimateList: EstimateListItemBase[], rulesMap: Map<string, ComplianceRule>, loadingFn: TLoadingFn) {
    loadingFn(true);
    const esTree = createTreeSortEsList(estimateList) as EstimateListItemBase[];
    const worktypesMap = new Map<string, EstimateListItemBase>();
    const mapLastParendNode = new Map<string, EstimateListItemBase>();
    let whileIdx = 0;
    for (let i = 0; i < esTree.length; i++) {
      const arrHandle: EstimateListItemBase[] = [];
      let lastParendNode: EstimateListItemBase = esTree[i];
      esTree[i]?.children?.forEach((ch) => {
        arrHandle.push(ch);
      });
      while (arrHandle.length > 0) {
        const item = arrHandle.pop();
        if (item?.tree_type === 'node' && Array.isArray(item.children)) {
          lastParendNode = item;
          item.children.forEach((ch) => arrHandle.push(ch));
        }
        if (item?.tree_type === 'item') {
          const isExistRule = rulesMap.get(item.code);
          const existNotIncludedNode = mapLastParendNode.get(item.parent_id);
          if (existNotIncludedNode === undefined && isExistRule === undefined) {
            const node = this.createWithoutRulesNode(item.parent_id, lastParendNode);
            mapLastParendNode.set(item.parent_id, node);
            lastParendNode?.children?.push(node);
            item.parent_id = node.id;
            node?.children?.unshift(item);
          } else if (existNotIncludedNode instanceof EstimateListItemBase && isExistRule === undefined) {
            item.parent_id = existNotIncludedNode.id;
            existNotIncludedNode?.children?.unshift(item);
          }
          if (isExistRule) {
            const existedWorktype = worktypesMap.get(`${isExistRule.worktype?._id}.${item.parent_id}`);
            if (existedWorktype === undefined) {
              const worktype = this.addWorktype(isExistRule.worktype, item.parent_id, item, whileIdx);
              lastParendNode.children?.push(worktype);
              worktypesMap.set(`${isExistRule.worktype?._id}.${item.parent_id}`, worktype);
            } else {
              existedWorktype.cost = stringToNumberOr0(existedWorktype.cost) + stringToNumberOr0(item.cost);
              existedWorktype.labour_plan = stringToNumberOr0(existedWorktype.labour_plan) + stringToNumberOr0(item.labour_plan);
              existedWorktype.MT = stringToNumberOr0(existedWorktype.MT) + stringToNumberOr0(item.MT);
              existedWorktype.OZ = stringToNumberOr0(existedWorktype.OZ) + stringToNumberOr0(item.OZ);
              existedWorktype.ZM = stringToNumberOr0(existedWorktype.ZM) + stringToNumberOr0(item.ZM);
              existedWorktype.EM = stringToNumberOr0(existedWorktype.EM) + stringToNumberOr0(item.EM);
              existedWorktype.NR = stringToNumberOr0(existedWorktype.NR) + stringToNumberOr0(item.NR);
              existedWorktype.SP = stringToNumberOr0(existedWorktype.SP) + stringToNumberOr0(item.SP);
              existedWorktype.cost_fixed = stringToNumberOr0(existedWorktype.cost_fixed) + stringToNumberOr0(item.cost_fixed);
            }
            if (existNotIncludedNode) {
              existNotIncludedNode.cost = stringToNumberOr0(existNotIncludedNode.cost) - stringToNumberOr0(item.cost);
              existNotIncludedNode.cost_fixed = stringToNumberOr0(existNotIncludedNode.cost_fixed) - stringToNumberOr0(item.cost_fixed);
              existNotIncludedNode.labour_plan = stringToNumberOr0(existNotIncludedNode.labour_plan) - stringToNumberOr0(item.labour_plan);
            }
          }
          const idx = lastParendNode?.children?.findIndex(
            (ch) => ch.id === item.id,
          );
          if (idx !== undefined) {
            lastParendNode?.children?.splice(idx, 1);
          }
          lastParendNode?.children?.sort((a, b) => a.row_number - b.row_number);
        }
        whileIdx++;
      }
    }
    createMaterializedPath(esTree, 'materialized_code_path', []);
    mapLastParendNode.clear();
    loadingFn(false);
    return flattenTree(esTree).rows;
  }