|
@@ -466,6 +466,84 @@ class BaseBudget extends TreeService {
|
|
|
const result = await this.db.queryOne(sql, [bid]);
|
|
|
return result.total_price;
|
|
|
}
|
|
|
+
|
|
|
+ async importYbpData(budget, ybpData, needGcl) {
|
|
|
+ const filterGcl = function(data, compareData, isXmj) {
|
|
|
+ return !isXmj;
|
|
|
+ };
|
|
|
+ const YbpTrees = require('../lib/ybp_tree');
|
|
|
+ const gatherTreeSetting = {
|
|
|
+ id: 'ledger_id', pid: 'ledger_pid', order: 'order', full_path: 'full_path', level: 'level', rootId: -1,
|
|
|
+ calcFields: ['total_price'],
|
|
|
+ calc(node, helper, decimal) {
|
|
|
+ node.quantity = helper.round(node.quantity, decimal.qty);
|
|
|
+ node.unit_price = helper.round(node.unit_price, decimal.up);
|
|
|
+ if ((!node.children || node.children.length === 0)) {
|
|
|
+ node.total_price = node.b_code ? helper.mul(node.quantity, node.unit_price, decimal.tp) : helper.round(node.total_fee, decimal.tp);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ };
|
|
|
+ const ybpTreeSetting = { id: 'ID', pid: 'parentID', order: 'seq', rootId: '-1' };
|
|
|
+ const helper = this.ctx.helper;
|
|
|
+
|
|
|
+ const mergeCompliation = ['5b52b027fd3bb0000b257cf8', '5c66649650da2d000d8d37ba'];
|
|
|
+ const ybpImportType = mergeCompliation.indexOf(ybpData.subjects[0].project.compilationID) >= 0 ? YbpTrees.YbpImportType.flow : YbpTrees.YbpImportType.merge;
|
|
|
+ const gatherTree = new YbpTrees.YbpImportTree(gatherTreeSetting, ybpImportType, helper, budget.decimal);
|
|
|
+ for (const subject of ybpData.subjects) {
|
|
|
+ if (!subject.bills || subject.bills.length === 0) continue;
|
|
|
+
|
|
|
+ const ybpTree = new YbpTrees.YbpTree(ybpTreeSetting);
|
|
|
+ ybpTree.loadDatas(subject.bills);
|
|
|
+ gatherTree.importTree(ybpTree, subject.project.name, null, needGcl ? null : filterGcl);
|
|
|
+ }
|
|
|
+ gatherTree.sort(false);
|
|
|
+ gatherTree.calculateAll();
|
|
|
+
|
|
|
+ const bills = [];
|
|
|
+ for (const n of gatherTree.nodes) {
|
|
|
+ const billsId = this.uuid.v4();
|
|
|
+ const isLeaf = !n.children || n.children.length === 0;
|
|
|
+ bills.push({
|
|
|
+ id: billsId,
|
|
|
+ bid: budget.id,
|
|
|
+ tree_id: n.ledger_id,
|
|
|
+ tree_pid: n.ledger_pid,
|
|
|
+ level: n.level,
|
|
|
+ order: n.order,
|
|
|
+ is_leaf: isLeaf,
|
|
|
+ full_path: n.full_path,
|
|
|
+ code: n.code || '',
|
|
|
+ b_code: n.b_code || '',
|
|
|
+ name: n.name || '',
|
|
|
+ unit: n.unit || '',
|
|
|
+ unit_price: isLeaf ? n.unit_price || 0 : 0,
|
|
|
+ dgn_qty1: n.dgn_qty1 || 0,
|
|
|
+ dgn_qty2: n.dgn_qty2 || 0,
|
|
|
+ memo: n.remark,
|
|
|
+ drawing_code: n.drawing_code,
|
|
|
+ quantity: isLeaf ? n.quantity || 0 : 0,
|
|
|
+ total_price: isLeaf ? n.total_price || 0 : 0,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ const conn = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ await conn.delete(this.tableName, { bid: budget.id });
|
|
|
+ await conn.insert(this.tableName, bills);
|
|
|
+ await conn.commit();
|
|
|
+ return bills;
|
|
|
+ } catch (err) {
|
|
|
+ await conn.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async importYbp(budget, ybp, needGcl) {
|
|
|
+ const YBP = require('../lib/ybp');
|
|
|
+ const ybpAnalysis = new YBP(this.ctx);
|
|
|
+ const ybpData = ybpAnalysis.decryptBuffer(ybp);
|
|
|
+
|
|
|
+ return await this.importYbpData(budget, ybpData, needGcl);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = BaseBudget;
|