repzo-sap-absjo 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/changelog.md +17 -0
- package/lib/actions/create_invoice.d.ts +26 -0
- package/lib/actions/create_invoice.js +311 -0
- package/lib/actions/create_payment.d.ts +5 -0
- package/lib/actions/create_payment.js +177 -0
- package/lib/actions/create_proforma.d.ts +5 -0
- package/lib/actions/create_proforma.js +241 -0
- package/lib/actions/create_return_invoice.d.ts +5 -0
- package/lib/actions/create_return_invoice.js +268 -0
- package/lib/actions/create_transfer.d.ts +5 -0
- package/lib/actions/create_transfer.js +171 -0
- package/lib/actions/index.d.ts +3 -0
- package/lib/actions/index.js +31 -0
- package/lib/commands/adjust_inventory.d.ts +6 -0
- package/lib/commands/adjust_inventory.js +303 -0
- package/lib/commands/bank.d.ts +7 -0
- package/lib/commands/bank.js +301 -0
- package/lib/commands/basic.d.ts +2 -0
- package/lib/commands/basic.js +73 -0
- package/lib/commands/category.d.ts +4 -0
- package/lib/commands/category.js +193 -0
- package/lib/commands/channel.d.ts +4 -0
- package/lib/commands/channel.js +213 -0
- package/lib/commands/client.d.ts +39 -0
- package/lib/commands/client.js +410 -0
- package/lib/commands/client_disabled.d.ts +4 -0
- package/lib/commands/client_disabled.js +183 -0
- package/lib/commands/index.d.ts +23 -0
- package/lib/commands/index.js +82 -0
- package/lib/commands/join.d.ts +2 -0
- package/lib/commands/join.js +134 -0
- package/lib/commands/measureunit.d.ts +23 -0
- package/lib/commands/measureunit.js +314 -0
- package/lib/commands/measureunit_family.d.ts +12 -0
- package/lib/commands/measureunit_family.js +263 -0
- package/lib/commands/payment_term.d.ts +4 -0
- package/lib/commands/payment_term.js +217 -0
- package/lib/commands/price_list.d.ts +18 -0
- package/lib/commands/price_list.js +608 -0
- package/lib/commands/product.d.ts +39 -0
- package/lib/commands/product.js +454 -0
- package/lib/commands/product_disabled.d.ts +4 -0
- package/lib/commands/product_disabled.js +178 -0
- package/lib/commands/rep.d.ts +2 -0
- package/lib/commands/rep.js +246 -0
- package/lib/commands/tag.d.ts +2 -0
- package/lib/commands/tag.js +195 -0
- package/lib/commands/tax.d.ts +2 -0
- package/lib/commands/tax.js +198 -0
- package/lib/commands/warehouse.d.ts +4 -0
- package/lib/commands/warehouse.js +221 -0
- package/lib/index.d.ts +29 -0
- package/lib/index.js +6 -0
- package/lib/test/actions/create_invoice.d.ts +1 -0
- package/lib/test/actions/create_invoice.js +214 -0
- package/lib/test/actions/create_payment.d.ts +1 -0
- package/lib/test/actions/create_payment.js +131 -0
- package/lib/test/actions/create_proforma.d.ts +1 -0
- package/lib/test/actions/create_proforma.js +242 -0
- package/lib/test/actions/create_return_invoice.d.ts +1 -0
- package/lib/test/actions/create_return_invoice.js +226 -0
- package/lib/test/actions/create_transfer.d.ts +1 -0
- package/lib/test/actions/create_transfer.js +142 -0
- package/lib/test/commands/adjust_inventory.d.ts +1 -0
- package/lib/test/commands/adjust_inventory.js +97 -0
- package/lib/test/commands/bank.d.ts +1 -0
- package/lib/test/commands/bank.js +97 -0
- package/lib/test/commands/basic.d.ts +1 -0
- package/lib/test/commands/basic.js +339 -0
- package/lib/test/commands/category.d.ts +1 -0
- package/lib/test/commands/category.js +97 -0
- package/lib/test/commands/channel.d.ts +1 -0
- package/lib/test/commands/channel.js +97 -0
- package/lib/test/commands/client.d.ts +1 -0
- package/lib/test/commands/client.js +97 -0
- package/lib/test/commands/client_disabled.d.ts +1 -0
- package/lib/test/commands/client_disabled.js +97 -0
- package/lib/test/commands/join.d.ts +1 -0
- package/lib/test/commands/join.js +339 -0
- package/lib/test/commands/measureunit.d.ts +1 -0
- package/lib/test/commands/measureunit.js +97 -0
- package/lib/test/commands/measureunit_family.d.ts +1 -0
- package/lib/test/commands/measureunit_family.js +97 -0
- package/lib/test/commands/payment_term.d.ts +1 -0
- package/lib/test/commands/payment_term.js +97 -0
- package/lib/test/commands/price_list.d.ts +1 -0
- package/lib/test/commands/price_list.js +97 -0
- package/lib/test/commands/product.d.ts +1 -0
- package/lib/test/commands/product.js +97 -0
- package/lib/test/commands/product_disabled.d.ts +1 -0
- package/lib/test/commands/product_disabled.js +97 -0
- package/lib/test/commands/rep.d.ts +1 -0
- package/lib/test/commands/rep.js +97 -0
- package/lib/test/commands/tag.d.ts +1 -0
- package/lib/test/commands/tag.js +97 -0
- package/lib/test/commands/tax.d.ts +1 -0
- package/lib/test/commands/tax.js +97 -0
- package/lib/test/commands/warehouse.d.ts +1 -0
- package/lib/test/commands/warehouse.js +97 -0
- package/lib/types.d.ts +77 -0
- package/lib/types.js +1 -0
- package/lib/util.d.ts +70 -0
- package/lib/util.js +164 -0
- package/package.json +51 -0
- package/src/actions/create_invoice.ts +275 -0
- package/src/actions/create_payment.ts +138 -0
- package/src/actions/create_proforma.ts +187 -0
- package/src/actions/create_return_invoice.ts +217 -0
- package/src/actions/create_transfer.ts +160 -0
- package/src/actions/index.ts +27 -0
- package/src/commands/adjust_inventory.ts +250 -0
- package/src/commands/bank.ts +243 -0
- package/src/commands/basic.ts +64 -0
- package/src/commands/category.ts +176 -0
- package/src/commands/channel.ts +219 -0
- package/src/commands/client.ts +368 -0
- package/src/commands/client_disabled.ts +158 -0
- package/src/commands/index.ts +87 -0
- package/src/commands/join.ts +80 -0
- package/src/commands/measureunit.ts +276 -0
- package/src/commands/measureunit_family.ts +209 -0
- package/src/commands/payment_term.ts +224 -0
- package/src/commands/price_list.ts +434 -0
- package/src/commands/product.ts +359 -0
- package/src/commands/product_disabled.ts +152 -0
- package/src/commands/rep.ts +209 -0
- package/src/commands/tag.ts +173 -0
- package/src/commands/tax.ts +178 -0
- package/src/commands/warehouse.ts +204 -0
- package/src/index.ts +7 -0
- package/src/test/actions/create_invoice.ts +214 -0
- package/src/test/actions/create_payment.ts +131 -0
- package/src/test/actions/create_proforma.ts +242 -0
- package/src/test/actions/create_return_invoice.ts +226 -0
- package/src/test/actions/create_transfer.ts +142 -0
- package/src/test/commands/adjust_inventory.ts +100 -0
- package/src/test/commands/bank.ts +100 -0
- package/src/test/commands/basic.ts +342 -0
- package/src/test/commands/category.ts +100 -0
- package/src/test/commands/channel.ts +100 -0
- package/src/test/commands/client.ts +100 -0
- package/src/test/commands/client_disabled.ts +100 -0
- package/src/test/commands/join.ts +342 -0
- package/src/test/commands/measureunit.ts +100 -0
- package/src/test/commands/measureunit_family.ts +100 -0
- package/src/test/commands/payment_term.ts +100 -0
- package/src/test/commands/price_list.ts +100 -0
- package/src/test/commands/product.ts +100 -0
- package/src/test/commands/product_disabled.ts +100 -0
- package/src/test/commands/rep.ts +100 -0
- package/src/test/commands/tag.ts +100 -0
- package/src/test/commands/tax.ts +100 -0
- package/src/test/commands/warehouse.ts +100 -0
- package/src/types.ts +76 -0
- package/src/util.ts +205 -0
- package/tsconfig.json +15 -0
package/README.md
ADDED
package/changelog.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Release Notes
|
|
2
|
+
|
|
3
|
+
## [v1.0.0 (2022-04-18)](https://github.com/Repzo/repzo-sap-absjo.git)
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Prepare repo @maramalshen
|
|
8
|
+
- New Commands: [warehouse, rep, tag, tax, measureunit, measureunit-family, category, channel, paymentTerm, bank, priceList, priceListItem, product, disabled_product, client, disabled_client, adjust_inventory] @maramalshen
|
|
9
|
+
- New Actions: [invoice, return_invoice, proforma, payment, transfer] @maramalshen
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
### Removed
|
|
16
|
+
|
|
17
|
+
## [unreleased (date)](path)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { EVENT, Config } from "../types";
|
|
2
|
+
interface SAPOpenInvoice {
|
|
3
|
+
CustomerNumber: string;
|
|
4
|
+
FatherCode: string;
|
|
5
|
+
DocDate: string;
|
|
6
|
+
DocDueDate: string;
|
|
7
|
+
InvoiceID: number;
|
|
8
|
+
InvoiceNumber: string;
|
|
9
|
+
InvoiceClientID: string;
|
|
10
|
+
InvoiceFinalAmount: number;
|
|
11
|
+
InvoiceRemainingAmount: number;
|
|
12
|
+
InvoiceStatus: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const create_invoice: (
|
|
15
|
+
event: EVENT,
|
|
16
|
+
options: Config
|
|
17
|
+
) => Promise<any>;
|
|
18
|
+
export declare const get_invoice_from_sap: (
|
|
19
|
+
serviceEndPoint: string,
|
|
20
|
+
query?: {
|
|
21
|
+
updatedAt: string;
|
|
22
|
+
Status: string;
|
|
23
|
+
InvoiceId: string;
|
|
24
|
+
}
|
|
25
|
+
) => Promise<SAPOpenInvoice[]>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import Repzo from "repzo";
|
|
2
|
+
import { _create, get_data } from "../util.js";
|
|
3
|
+
import { v4 as uuid } from "uuid";
|
|
4
|
+
import moment from "moment-timezone";
|
|
5
|
+
export const create_invoice = async (event, options) => {
|
|
6
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
7
|
+
const repzo = new Repzo(
|
|
8
|
+
(_a = options.data) === null || _a === void 0 ? void 0 : _a.repzoApiKey,
|
|
9
|
+
{ env: options.env }
|
|
10
|
+
);
|
|
11
|
+
const action_sync_id =
|
|
12
|
+
((_b = event === null || event === void 0 ? void 0 : event.headers) ===
|
|
13
|
+
null || _b === void 0
|
|
14
|
+
? void 0
|
|
15
|
+
: _b.action_sync_id) || uuid();
|
|
16
|
+
const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
|
|
17
|
+
let body;
|
|
18
|
+
try {
|
|
19
|
+
// console.log("create_invoice");
|
|
20
|
+
await actionLog.load(action_sync_id);
|
|
21
|
+
body = event.body;
|
|
22
|
+
try {
|
|
23
|
+
if (body) body = JSON.parse(body);
|
|
24
|
+
} catch (e) {}
|
|
25
|
+
const repzo_serial_number =
|
|
26
|
+
(_c = body === null || body === void 0 ? void 0 : body.serial_number) ===
|
|
27
|
+
null || _c === void 0
|
|
28
|
+
? void 0
|
|
29
|
+
: _c.formatted;
|
|
30
|
+
await actionLog
|
|
31
|
+
.addDetail(
|
|
32
|
+
`Repzo => SAP: Started Create Invoice - ${repzo_serial_number}`
|
|
33
|
+
)
|
|
34
|
+
.commit();
|
|
35
|
+
const SAP_HOST_URL =
|
|
36
|
+
(_d = options.data) === null || _d === void 0 ? void 0 : _d.sapHostUrl;
|
|
37
|
+
if (!SAP_HOST_URL)
|
|
38
|
+
throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
|
|
39
|
+
const repzo_invoice = body;
|
|
40
|
+
// Check if it is already exist in SAP
|
|
41
|
+
const sap_open_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
|
|
42
|
+
updatedAt: "",
|
|
43
|
+
Status: "",
|
|
44
|
+
InvoiceId: repzo_serial_number,
|
|
45
|
+
});
|
|
46
|
+
const open_invoice =
|
|
47
|
+
sap_open_invoices === null || sap_open_invoices === void 0
|
|
48
|
+
? void 0
|
|
49
|
+
: sap_open_invoices.find(
|
|
50
|
+
(inv) =>
|
|
51
|
+
(inv === null || inv === void 0 ? void 0 : inv.InvoiceNumber) ===
|
|
52
|
+
repzo_serial_number
|
|
53
|
+
);
|
|
54
|
+
if (open_invoice) {
|
|
55
|
+
await actionLog
|
|
56
|
+
.addDetail(`Checked Already in SAP `, open_invoice)
|
|
57
|
+
.addDetail(`Invoice - ${repzo_serial_number} Checked Already in SAP`)
|
|
58
|
+
.setStatus("success")
|
|
59
|
+
.setBody(repzo_invoice)
|
|
60
|
+
.commit();
|
|
61
|
+
return {
|
|
62
|
+
message: "Checked Already in SAP",
|
|
63
|
+
result: open_invoice,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
// Check closed invoice in SAP
|
|
67
|
+
const sap_closed_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
|
|
68
|
+
updatedAt: "",
|
|
69
|
+
Status: "closed",
|
|
70
|
+
InvoiceId: repzo_serial_number,
|
|
71
|
+
});
|
|
72
|
+
const closed_invoice =
|
|
73
|
+
sap_closed_invoices === null || sap_closed_invoices === void 0
|
|
74
|
+
? void 0
|
|
75
|
+
: sap_closed_invoices.find(
|
|
76
|
+
(inv) =>
|
|
77
|
+
(inv === null || inv === void 0 ? void 0 : inv.InvoiceNumber) ===
|
|
78
|
+
repzo_serial_number
|
|
79
|
+
);
|
|
80
|
+
if (closed_invoice) {
|
|
81
|
+
await actionLog
|
|
82
|
+
.addDetail(`Checked Closed Already in SAP `, closed_invoice)
|
|
83
|
+
.addDetail(
|
|
84
|
+
`Invoice - ${repzo_serial_number} Checked Closed Already in SAP`
|
|
85
|
+
)
|
|
86
|
+
.setStatus("success")
|
|
87
|
+
.setBody(repzo_invoice)
|
|
88
|
+
.commit();
|
|
89
|
+
return {
|
|
90
|
+
message: "Checked Closed Already in SAP",
|
|
91
|
+
result: closed_invoice,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
// Get Repzo Rep
|
|
95
|
+
let repzo_rep;
|
|
96
|
+
if (repzo_invoice.creator.type == "rep") {
|
|
97
|
+
repzo_rep = await repzo.rep.get(
|
|
98
|
+
(_e =
|
|
99
|
+
repzo_invoice === null || repzo_invoice === void 0
|
|
100
|
+
? void 0
|
|
101
|
+
: repzo_invoice.creator) === null || _e === void 0
|
|
102
|
+
? void 0
|
|
103
|
+
: _e._id
|
|
104
|
+
);
|
|
105
|
+
if (!repzo_rep)
|
|
106
|
+
throw `Rep with _id: ${repzo_invoice.creator._id} not found in Repzo`;
|
|
107
|
+
}
|
|
108
|
+
// Get Repzo Client
|
|
109
|
+
const repzo_client = await repzo.client.get(
|
|
110
|
+
repzo_invoice === null || repzo_invoice === void 0
|
|
111
|
+
? void 0
|
|
112
|
+
: repzo_invoice.client_id
|
|
113
|
+
);
|
|
114
|
+
if (!repzo_client)
|
|
115
|
+
throw `Client with _id: ${repzo_invoice.client_id} not found in Repzo`;
|
|
116
|
+
// Get Repzo Warehouse
|
|
117
|
+
const repzo_warehouse = await repzo.warehouse.get(
|
|
118
|
+
repzo_invoice.origin_warehouse
|
|
119
|
+
);
|
|
120
|
+
if (!repzo_warehouse)
|
|
121
|
+
throw `warehouse with _id: ${repzo_invoice.origin_warehouse} not found in Repzo`;
|
|
122
|
+
const repzo_tax_ids = {};
|
|
123
|
+
const repzo_measureunit_ids = {};
|
|
124
|
+
const repzo_product_ids = {};
|
|
125
|
+
(_f = repzo_invoice.items) === null || _f === void 0
|
|
126
|
+
? void 0
|
|
127
|
+
: _f.forEach((item) => {
|
|
128
|
+
var _a, _b, _c;
|
|
129
|
+
if (item) {
|
|
130
|
+
repzo_tax_ids[
|
|
131
|
+
(_a = item.tax) === null || _a === void 0 ? void 0 : _a._id
|
|
132
|
+
] = true;
|
|
133
|
+
repzo_measureunit_ids[
|
|
134
|
+
(_b = item.measureunit) === null || _b === void 0
|
|
135
|
+
? void 0
|
|
136
|
+
: _b._id
|
|
137
|
+
] = true;
|
|
138
|
+
repzo_product_ids[
|
|
139
|
+
(_c = item.variant) === null || _c === void 0
|
|
140
|
+
? void 0
|
|
141
|
+
: _c.product_id
|
|
142
|
+
] = true;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
const repzo_taxes = await get_data(
|
|
146
|
+
repzo.tax,
|
|
147
|
+
"_id",
|
|
148
|
+
Object.keys(repzo_tax_ids)
|
|
149
|
+
);
|
|
150
|
+
const repzo_measureunits = await get_data(
|
|
151
|
+
repzo.measureunit,
|
|
152
|
+
"_id",
|
|
153
|
+
Object.keys(repzo_measureunit_ids)
|
|
154
|
+
);
|
|
155
|
+
const repzo_products = await get_data(
|
|
156
|
+
repzo.product,
|
|
157
|
+
"_id",
|
|
158
|
+
Object.keys(repzo_product_ids)
|
|
159
|
+
);
|
|
160
|
+
// Prepare SAP_invoice_items
|
|
161
|
+
const items = [];
|
|
162
|
+
for (
|
|
163
|
+
let i = 0;
|
|
164
|
+
i <
|
|
165
|
+
((_g =
|
|
166
|
+
repzo_invoice === null || repzo_invoice === void 0
|
|
167
|
+
? void 0
|
|
168
|
+
: repzo_invoice.items) === null || _g === void 0
|
|
169
|
+
? void 0
|
|
170
|
+
: _g.length);
|
|
171
|
+
i++
|
|
172
|
+
) {
|
|
173
|
+
const item = repzo_invoice.items[i];
|
|
174
|
+
// Get Repzo Tax
|
|
175
|
+
const repzo_tax =
|
|
176
|
+
repzo_taxes === null || repzo_taxes === void 0
|
|
177
|
+
? void 0
|
|
178
|
+
: repzo_taxes.find((t) => {
|
|
179
|
+
var _a, _b, _c;
|
|
180
|
+
return (
|
|
181
|
+
((_a = t._id) === null || _a === void 0
|
|
182
|
+
? void 0
|
|
183
|
+
: _a.toString()) ==
|
|
184
|
+
((_c =
|
|
185
|
+
(_b = item.tax) === null || _b === void 0
|
|
186
|
+
? void 0
|
|
187
|
+
: _b._id) === null || _c === void 0
|
|
188
|
+
? void 0
|
|
189
|
+
: _c.toString())
|
|
190
|
+
);
|
|
191
|
+
});
|
|
192
|
+
if (!repzo_tax) throw `Tax with _id: ${item.tax._id} not found in Repzo`;
|
|
193
|
+
// Get Repzo UoM
|
|
194
|
+
const repzo_measureunit =
|
|
195
|
+
repzo_measureunits === null || repzo_measureunits === void 0
|
|
196
|
+
? void 0
|
|
197
|
+
: repzo_measureunits.find((m) => {
|
|
198
|
+
var _a, _b, _c;
|
|
199
|
+
return (
|
|
200
|
+
((_a = m._id) === null || _a === void 0
|
|
201
|
+
? void 0
|
|
202
|
+
: _a.toString()) ==
|
|
203
|
+
((_c =
|
|
204
|
+
(_b = item.measureunit) === null || _b === void 0
|
|
205
|
+
? void 0
|
|
206
|
+
: _b._id) === null || _c === void 0
|
|
207
|
+
? void 0
|
|
208
|
+
: _c.toString())
|
|
209
|
+
);
|
|
210
|
+
});
|
|
211
|
+
if (!repzo_measureunit)
|
|
212
|
+
throw `Uom with _id: ${
|
|
213
|
+
(_h = item.measureunit) === null || _h === void 0 ? void 0 : _h._id
|
|
214
|
+
} not found in Repzo`;
|
|
215
|
+
// Get Repzo Product
|
|
216
|
+
const repzo_product =
|
|
217
|
+
repzo_products === null || repzo_products === void 0
|
|
218
|
+
? void 0
|
|
219
|
+
: repzo_products.find((p) => {
|
|
220
|
+
var _a, _b, _c;
|
|
221
|
+
return (
|
|
222
|
+
((_a = p._id) === null || _a === void 0
|
|
223
|
+
? void 0
|
|
224
|
+
: _a.toString()) ==
|
|
225
|
+
((_c =
|
|
226
|
+
(_b = item.variant) === null || _b === void 0
|
|
227
|
+
? void 0
|
|
228
|
+
: _b.product_id) === null || _c === void 0
|
|
229
|
+
? void 0
|
|
230
|
+
: _c.toString())
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
if (!repzo_product)
|
|
234
|
+
throw `Product with _id: ${item.measureunit._id} not found in Repzo`;
|
|
235
|
+
items.push({
|
|
236
|
+
ItemCode: item.variant.variant_name,
|
|
237
|
+
Quantity: item.qty,
|
|
238
|
+
TaxCode: repzo_tax.integration_meta.TaxCode,
|
|
239
|
+
UnitPrice: (item.price * repzo_measureunit.factor) / 1000,
|
|
240
|
+
DiscountPerc: "0",
|
|
241
|
+
//@ts-ignore
|
|
242
|
+
LineTotal: item.total_before_tax / 1000,
|
|
243
|
+
UomCode: repzo_measureunit.integration_meta.ALTUOMID,
|
|
244
|
+
Brand:
|
|
245
|
+
(_j = repzo_product.integration_meta) === null || _j === void 0
|
|
246
|
+
? void 0
|
|
247
|
+
: _j.BRAND,
|
|
248
|
+
Department:
|
|
249
|
+
((_k =
|
|
250
|
+
repzo_rep === null || repzo_rep === void 0
|
|
251
|
+
? void 0
|
|
252
|
+
: repzo_rep.integration_meta) === null || _k === void 0
|
|
253
|
+
? void 0
|
|
254
|
+
: _k.DEPARTMENTCODE) ||
|
|
255
|
+
((_l = options.data) === null || _l === void 0
|
|
256
|
+
? void 0
|
|
257
|
+
: _l.DepartmentCode), // "D2",
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
const sap_invoice = {
|
|
261
|
+
RefNum: repzo_invoice.serial_number.formatted,
|
|
262
|
+
SalPersCode:
|
|
263
|
+
repzo_rep === null || repzo_rep === void 0
|
|
264
|
+
? void 0
|
|
265
|
+
: repzo_rep.integration_id,
|
|
266
|
+
DocDate: moment(repzo_invoice.issue_date, "YYYY-MM-DD").format(
|
|
267
|
+
"YYYYMMDD"
|
|
268
|
+
),
|
|
269
|
+
DocDueDate: moment(repzo_invoice.due_date, "YYYY-MM-DD").format(
|
|
270
|
+
"YYYYMMDD"
|
|
271
|
+
),
|
|
272
|
+
ClientCode: repzo_client.client_code,
|
|
273
|
+
DiscountPerc: "0",
|
|
274
|
+
Note: repzo_invoice.comment,
|
|
275
|
+
WarehouseCode: repzo_warehouse.code,
|
|
276
|
+
LinesDetails: items,
|
|
277
|
+
};
|
|
278
|
+
// console.dir(sap_invoice, { depth: null });
|
|
279
|
+
await actionLog
|
|
280
|
+
.addDetail(`Repzo => SAP: Invoice - ${repzo_serial_number}`, sap_invoice)
|
|
281
|
+
.commit();
|
|
282
|
+
const result = await _create(SAP_HOST_URL, "/AddInvoice", sap_invoice);
|
|
283
|
+
// console.log(result);
|
|
284
|
+
await actionLog
|
|
285
|
+
.addDetail(`SAP Responded with `, result)
|
|
286
|
+
.addDetail(`Repzo => SAP: Invoice - ${repzo_serial_number}`)
|
|
287
|
+
.setStatus("success")
|
|
288
|
+
.setBody(repzo_invoice)
|
|
289
|
+
.commit();
|
|
290
|
+
return result;
|
|
291
|
+
} catch (e) {
|
|
292
|
+
//@ts-ignore
|
|
293
|
+
console.error((e === null || e === void 0 ? void 0 : e.response) || e);
|
|
294
|
+
await actionLog.setStatus("fail", e).setBody(body).commit();
|
|
295
|
+
throw e;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
export const get_invoice_from_sap = async (serviceEndPoint, query) => {
|
|
299
|
+
try {
|
|
300
|
+
const sap_openInvoices = await _create(serviceEndPoint, "/OpenInvoices", {
|
|
301
|
+
updatedAt: query === null || query === void 0 ? void 0 : query.updatedAt,
|
|
302
|
+
Status: query === null || query === void 0 ? void 0 : query.Status,
|
|
303
|
+
InvoiceId: query === null || query === void 0 ? void 0 : query.InvoiceId,
|
|
304
|
+
});
|
|
305
|
+
return sap_openInvoices === null || sap_openInvoices === void 0
|
|
306
|
+
? void 0
|
|
307
|
+
: sap_openInvoices.OpenInvoices;
|
|
308
|
+
} catch (e) {
|
|
309
|
+
throw e;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import Repzo from "repzo";
|
|
2
|
+
import { _create } from "../util.js";
|
|
3
|
+
import { v4 as uuid } from "uuid";
|
|
4
|
+
import moment from "moment-timezone";
|
|
5
|
+
import { get_invoice_from_sap } from "./create_invoice.js";
|
|
6
|
+
export const create_payment = async (event, options) => {
|
|
7
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
8
|
+
const repzo = new Repzo(
|
|
9
|
+
(_a = options.data) === null || _a === void 0 ? void 0 : _a.repzoApiKey,
|
|
10
|
+
{ env: options.env }
|
|
11
|
+
);
|
|
12
|
+
const action_sync_id =
|
|
13
|
+
((_b = event === null || event === void 0 ? void 0 : event.headers) ===
|
|
14
|
+
null || _b === void 0
|
|
15
|
+
? void 0
|
|
16
|
+
: _b.action_sync_id) || uuid();
|
|
17
|
+
const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
|
|
18
|
+
let body;
|
|
19
|
+
try {
|
|
20
|
+
// console.log("create_payment");
|
|
21
|
+
await actionLog.load(action_sync_id);
|
|
22
|
+
body = event.body;
|
|
23
|
+
try {
|
|
24
|
+
if (body) body = JSON.parse(body);
|
|
25
|
+
} catch (e) {}
|
|
26
|
+
const repzo_serial_number =
|
|
27
|
+
(_c = body === null || body === void 0 ? void 0 : body.serial_number) ===
|
|
28
|
+
null || _c === void 0
|
|
29
|
+
? void 0
|
|
30
|
+
: _c.formatted;
|
|
31
|
+
await actionLog
|
|
32
|
+
.addDetail(
|
|
33
|
+
`Repzo => SAP: Started Create Payment - ${repzo_serial_number}`
|
|
34
|
+
)
|
|
35
|
+
.commit();
|
|
36
|
+
const SAP_HOST_URL =
|
|
37
|
+
(_d = options.data) === null || _d === void 0 ? void 0 : _d.sapHostUrl;
|
|
38
|
+
if (!SAP_HOST_URL)
|
|
39
|
+
throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
|
|
40
|
+
const repzo_payment = body;
|
|
41
|
+
// Get Repzo Rep
|
|
42
|
+
let repzo_rep;
|
|
43
|
+
if (repzo_payment.creator.type == "rep") {
|
|
44
|
+
repzo_rep = await repzo.rep.get(
|
|
45
|
+
(_e =
|
|
46
|
+
repzo_payment === null || repzo_payment === void 0
|
|
47
|
+
? void 0
|
|
48
|
+
: repzo_payment.creator) === null || _e === void 0
|
|
49
|
+
? void 0
|
|
50
|
+
: _e._id
|
|
51
|
+
);
|
|
52
|
+
if (!repzo_rep)
|
|
53
|
+
throw `Rep with _id: ${repzo_payment.creator._id} not found in Repzo`;
|
|
54
|
+
}
|
|
55
|
+
// Get Repzo Client
|
|
56
|
+
const repzo_client = await repzo.client.get(
|
|
57
|
+
repzo_payment === null || repzo_payment === void 0
|
|
58
|
+
? void 0
|
|
59
|
+
: repzo_payment.client_id
|
|
60
|
+
);
|
|
61
|
+
if (!repzo_client)
|
|
62
|
+
throw `Client with _id: ${repzo_payment.client_id} not found in Repzo`;
|
|
63
|
+
const sap_payment = {
|
|
64
|
+
PaymentID: repzo_serial_number,
|
|
65
|
+
ClientCode:
|
|
66
|
+
repzo_client === null || repzo_client === void 0
|
|
67
|
+
? void 0
|
|
68
|
+
: repzo_client.client_code,
|
|
69
|
+
PaymentDate: moment(repzo_payment.paytime, "YYYY-MM-DD").format(
|
|
70
|
+
"YYYYMMDD"
|
|
71
|
+
),
|
|
72
|
+
Amount: repzo_payment.amount / 1000,
|
|
73
|
+
};
|
|
74
|
+
if (
|
|
75
|
+
repzo_payment === null || repzo_payment === void 0
|
|
76
|
+
? void 0
|
|
77
|
+
: repzo_payment.LinkedTxn
|
|
78
|
+
) {
|
|
79
|
+
const sap_open_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
|
|
80
|
+
updatedAt: "",
|
|
81
|
+
Status: "",
|
|
82
|
+
InvoiceId:
|
|
83
|
+
(_g =
|
|
84
|
+
(_f =
|
|
85
|
+
repzo_payment === null || repzo_payment === void 0
|
|
86
|
+
? void 0
|
|
87
|
+
: repzo_payment.LinkedTxn) === null || _f === void 0
|
|
88
|
+
? void 0
|
|
89
|
+
: _f.Txn_serial_number) === null || _g === void 0
|
|
90
|
+
? void 0
|
|
91
|
+
: _g.formatted,
|
|
92
|
+
});
|
|
93
|
+
if (
|
|
94
|
+
!(sap_open_invoices === null || sap_open_invoices === void 0
|
|
95
|
+
? void 0
|
|
96
|
+
: sap_open_invoices.length)
|
|
97
|
+
) {
|
|
98
|
+
throw `Invoice with serial number: ${repzo_payment.LinkedTxn.Txn_serial_number.formatted} was not found on SAP or was closed`;
|
|
99
|
+
}
|
|
100
|
+
sap_payment.InvoiceID = sap_open_invoices[0].InvoiceID;
|
|
101
|
+
sap_payment.InvoiceTotal =
|
|
102
|
+
((_h =
|
|
103
|
+
repzo_payment === null || repzo_payment === void 0
|
|
104
|
+
? void 0
|
|
105
|
+
: repzo_payment.LinkedTxn) === null || _h === void 0
|
|
106
|
+
? void 0
|
|
107
|
+
: _h.Txn_invoice_total) / 1000;
|
|
108
|
+
} else {
|
|
109
|
+
sap_payment.InvoiceID = "";
|
|
110
|
+
sap_payment.InvoiceTotal = "";
|
|
111
|
+
}
|
|
112
|
+
if (repzo_payment.payment_type == "check") {
|
|
113
|
+
// bank
|
|
114
|
+
//@ts-ignore
|
|
115
|
+
const repzo_bank = await repzo.bank.get(
|
|
116
|
+
(_j = repzo_payment.check) === null || _j === void 0 ? void 0 : _j.bank
|
|
117
|
+
);
|
|
118
|
+
if (!repzo_bank) {
|
|
119
|
+
//@ts-ignore
|
|
120
|
+
throw `Bank with _id: ${repzo_payment.check.bank} not found in Repzo`;
|
|
121
|
+
}
|
|
122
|
+
sap_payment.PaymentType = "2";
|
|
123
|
+
sap_payment.ChequeAccount =
|
|
124
|
+
(_k =
|
|
125
|
+
repzo_rep === null || repzo_rep === void 0
|
|
126
|
+
? void 0
|
|
127
|
+
: repzo_rep.integration_meta) === null || _k === void 0
|
|
128
|
+
? void 0
|
|
129
|
+
: _k.USERCHECKACCTCODE;
|
|
130
|
+
sap_payment.ChequeDate = moment(
|
|
131
|
+
(_l = repzo_payment.check) === null || _l === void 0
|
|
132
|
+
? void 0
|
|
133
|
+
: _l.check_date,
|
|
134
|
+
"YYYY-MM-DD"
|
|
135
|
+
).format("YYYYMMDD");
|
|
136
|
+
sap_payment.ChequeNumber =
|
|
137
|
+
(_m = repzo_payment.check) === null || _m === void 0
|
|
138
|
+
? void 0
|
|
139
|
+
: _m.check_number;
|
|
140
|
+
sap_payment.BankCode =
|
|
141
|
+
(_o = repzo_bank.integration_meta) === null || _o === void 0
|
|
142
|
+
? void 0
|
|
143
|
+
: _o.BANKCODE;
|
|
144
|
+
sap_payment.CountryCode =
|
|
145
|
+
(_p = repzo_bank.integration_meta) === null || _p === void 0
|
|
146
|
+
? void 0
|
|
147
|
+
: _p.COUNTRY;
|
|
148
|
+
} else if (repzo_payment.payment_type == "cash") {
|
|
149
|
+
sap_payment.PaymentType = "1";
|
|
150
|
+
sap_payment.CashAccount =
|
|
151
|
+
(_q =
|
|
152
|
+
repzo_rep === null || repzo_rep === void 0
|
|
153
|
+
? void 0
|
|
154
|
+
: repzo_rep.integration_meta) === null || _q === void 0
|
|
155
|
+
? void 0
|
|
156
|
+
: _q.USERCASHACCOUNT;
|
|
157
|
+
}
|
|
158
|
+
// console.dir(sap_payment, { depth: null });
|
|
159
|
+
await actionLog
|
|
160
|
+
.addDetail(`Repzo => SAP: Payment - ${repzo_serial_number}`, sap_payment)
|
|
161
|
+
.commit();
|
|
162
|
+
const result = await _create(SAP_HOST_URL, "/AddPayment", sap_payment);
|
|
163
|
+
// console.log(result);
|
|
164
|
+
await actionLog
|
|
165
|
+
.addDetail(`SAP Responded with `, result)
|
|
166
|
+
.addDetail(`Repzo => SAP: Payment - ${repzo_serial_number}`)
|
|
167
|
+
.setStatus("success")
|
|
168
|
+
.setBody(repzo_payment)
|
|
169
|
+
.commit();
|
|
170
|
+
return result;
|
|
171
|
+
} catch (e) {
|
|
172
|
+
//@ts-ignore
|
|
173
|
+
console.error((e === null || e === void 0 ? void 0 : e.response) || e);
|
|
174
|
+
await actionLog.setStatus("fail", e).setBody(body).commit();
|
|
175
|
+
throw e;
|
|
176
|
+
}
|
|
177
|
+
};
|