|
@@ -82,6 +82,117 @@ module.exports = app => {
|
|
|
}
|
|
|
await this.db.update(this.tableName, data, {where: {tid: tenderId}});
|
|
|
}
|
|
|
+
|
|
|
+ async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
|
|
|
+ const changePrecision = [];
|
|
|
+ const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
|
|
|
+ const defUnits = this._.map(newPrecision, 'units');
|
|
|
+ const otherUnits = units.filter(function (u) {return defUnits.indexOf(u) === -1});
|
|
|
+ let changeUnits = [];
|
|
|
+ for (const prop in newPrecision) {
|
|
|
+ if (oldPrecision[prop]) {
|
|
|
+ if (newPrecision[prop].value < oldPrecision[prop].value) {
|
|
|
+ changePrecision.push(newPrecision[prop]);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ changePrecision.push(newPrecision[prop]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (const cp of changePrecision) {
|
|
|
+ if (cp.unit) {
|
|
|
+ changeUnits.push(cp.unit);
|
|
|
+ } else {
|
|
|
+ changeUnits.push(otherUnits);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ changeUnits = this._.flatten(changeUnits);
|
|
|
+
|
|
|
+ if (changeUnits.length > 0) {
|
|
|
+ const bills = await this.ctx.service.ledger.getAllDataByCondition({
|
|
|
+ columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
|
|
|
+ where: {tender_id: tenderId, unit: changeUnits, is_leaf: true}
|
|
|
+ });
|
|
|
+ const pos = changeUnits.length > 0 ? await this.ctx.service.pos.getPosDataByUnits(tenderId, changeUnits) : [];
|
|
|
+
|
|
|
+ for (const b of bills) {
|
|
|
+ const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
|
|
|
+ const bPos = this._.filter(pos, {lid: b.id});
|
|
|
+ if (bPos.length > 0) {
|
|
|
+ let sgfh_qty = 0, sjcl_qty = 0, qtcl_qty = 0, quantity = 0;
|
|
|
+ for (const p of bPos) {
|
|
|
+ this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
|
|
|
+ p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
|
|
|
+ sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
|
|
|
+ sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
|
|
|
+ qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
|
|
|
+ quantity = this.ctx.helper.add(quantity, p.quantity);
|
|
|
+ }
|
|
|
+ b.sgfh_qty = sgfh_qty;
|
|
|
+ b.sjcl_qty = sjcl_qty;
|
|
|
+ b.qtcl_qty = qtcl_qty;
|
|
|
+ b.quantity = quantity;
|
|
|
+ } else {
|
|
|
+ this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
|
|
|
+ }
|
|
|
+ b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
|
|
|
+ b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
|
|
|
+ b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
|
|
|
+ b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
|
|
|
+ b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
|
|
|
+ b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
|
|
|
+ }
|
|
|
+
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ await transaction.update(this.tableName,
|
|
|
+ {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
|
|
|
+ if (bills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, bills);
|
|
|
+ if (pos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, pos);
|
|
|
+ await transaction.commit();
|
|
|
+ } catch (err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ await this.db.update(this.tableName,
|
|
|
+ {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async saveDecimal(tenderId, newDecimal, oldDecimal) {
|
|
|
+ const changeBills = [], calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
|
|
|
+ if (calcUp || calcTp) {
|
|
|
+ const bills = await this.ctx.service.ledger.getAllDataByCondition({
|
|
|
+ columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
|
|
|
+ where: {tender_id: tenderId, is_leaf: true}
|
|
|
+ });
|
|
|
+ for (const b of bills) {
|
|
|
+ const cb = { id: b.id };
|
|
|
+ cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
|
|
|
+ cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
|
|
|
+ cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
|
|
|
+ cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
|
|
|
+ cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
|
|
|
+ cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
|
|
|
+ changeBills.push(cb);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (changeBills.length > 0) {
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ await transaction.update(this.tableName,
|
|
|
+ {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
|
|
|
+ await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
|
|
|
+ await transaction.commit();
|
|
|
+ } catch(error) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ await this.db.update(this.tableName,
|
|
|
+ {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return TenderInfo;
|