-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto-helpers.js
45 lines (39 loc) · 2.25 KB
/
proto-helpers.js
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
const JSONbig = require('json-bigint');
module.exports = {
convertReceiptRequestToProto: function (str, cashboxId) {
let receiptRequest = JSONbig.parse(str);
receiptRequest.ftCashBoxID = cashboxId;
receiptRequest.ftReceiptCase = receiptRequest.ftReceiptCase.toString();
receiptRequest.cbReceiptMoment = this.convertJsDatetimeToProto(receiptRequest.cbReceiptMoment);
receiptRequest.cbReceiptAmount = this.convertJsDecimalToProto(receiptRequest.cbReceiptAmount);
receiptRequest.cbChargeItems.forEach(chargeItem => {
chargeItem.Quantity = this.convertJsDecimalToProto(chargeItem.Quantity);
chargeItem.Amount = this.convertJsDecimalToProto(chargeItem.Amount);
chargeItem.VATRate = this.convertJsDecimalToProto(chargeItem.VATRate);
chargeItem.VATAmount = this.convertJsDecimalToProto(chargeItem.VATAmount);
chargeItem.UnitQuantity = this.convertJsDecimalToProto(chargeItem.UnitQuantity);
chargeItem.Moment = this.convertJsDatetimeToProto(chargeItem.Moment);
chargeItem.ftChargeItemCase = chargeItem.ftChargeItemCase.toString();
});
receiptRequest.cbPayItems.forEach(payItem => {
payItem.Quantity = this.convertJsDecimalToProto(payItem.Quantity);
payItem.Amount = this.convertJsDecimalToProto(payItem.Amount);
payItem.Moment = this.convertJsDatetimeToProto(payItem.Moment);
payItem.ftPayItemCase = payItem.ftPayItemCase.toString();
});
return receiptRequest;
},
convertJsDatetimeToProto: (datetime) => {
if (!datetime) return undefined;
return { value: new Date(datetime).getTime(), scale: 4, kind: 2 };
},
convertJsDecimalToProto: (dec) => {
if (!dec) return undefined;
// TODO: Replace this workaround with a proper serialization and also include the HI bits, instead of just cutting after 15 characters
let decStr = dec.toString().replace("-", "").substring(0, 15);
let split = decStr.split(".");
let precision = split.length == 2 ? split[1].length || 0 : 0;
let shift = dec.toString().startsWith("-") ? 0 : 1;
return { lo: decStr.replace('.', ''), hi: 0, signScale: precision << shift };
}
};