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.
Files changed (157) hide show
  1. package/README.md +3 -0
  2. package/changelog.md +17 -0
  3. package/lib/actions/create_invoice.d.ts +26 -0
  4. package/lib/actions/create_invoice.js +311 -0
  5. package/lib/actions/create_payment.d.ts +5 -0
  6. package/lib/actions/create_payment.js +177 -0
  7. package/lib/actions/create_proforma.d.ts +5 -0
  8. package/lib/actions/create_proforma.js +241 -0
  9. package/lib/actions/create_return_invoice.d.ts +5 -0
  10. package/lib/actions/create_return_invoice.js +268 -0
  11. package/lib/actions/create_transfer.d.ts +5 -0
  12. package/lib/actions/create_transfer.js +171 -0
  13. package/lib/actions/index.d.ts +3 -0
  14. package/lib/actions/index.js +31 -0
  15. package/lib/commands/adjust_inventory.d.ts +6 -0
  16. package/lib/commands/adjust_inventory.js +303 -0
  17. package/lib/commands/bank.d.ts +7 -0
  18. package/lib/commands/bank.js +301 -0
  19. package/lib/commands/basic.d.ts +2 -0
  20. package/lib/commands/basic.js +73 -0
  21. package/lib/commands/category.d.ts +4 -0
  22. package/lib/commands/category.js +193 -0
  23. package/lib/commands/channel.d.ts +4 -0
  24. package/lib/commands/channel.js +213 -0
  25. package/lib/commands/client.d.ts +39 -0
  26. package/lib/commands/client.js +410 -0
  27. package/lib/commands/client_disabled.d.ts +4 -0
  28. package/lib/commands/client_disabled.js +183 -0
  29. package/lib/commands/index.d.ts +23 -0
  30. package/lib/commands/index.js +82 -0
  31. package/lib/commands/join.d.ts +2 -0
  32. package/lib/commands/join.js +134 -0
  33. package/lib/commands/measureunit.d.ts +23 -0
  34. package/lib/commands/measureunit.js +314 -0
  35. package/lib/commands/measureunit_family.d.ts +12 -0
  36. package/lib/commands/measureunit_family.js +263 -0
  37. package/lib/commands/payment_term.d.ts +4 -0
  38. package/lib/commands/payment_term.js +217 -0
  39. package/lib/commands/price_list.d.ts +18 -0
  40. package/lib/commands/price_list.js +608 -0
  41. package/lib/commands/product.d.ts +39 -0
  42. package/lib/commands/product.js +454 -0
  43. package/lib/commands/product_disabled.d.ts +4 -0
  44. package/lib/commands/product_disabled.js +178 -0
  45. package/lib/commands/rep.d.ts +2 -0
  46. package/lib/commands/rep.js +246 -0
  47. package/lib/commands/tag.d.ts +2 -0
  48. package/lib/commands/tag.js +195 -0
  49. package/lib/commands/tax.d.ts +2 -0
  50. package/lib/commands/tax.js +198 -0
  51. package/lib/commands/warehouse.d.ts +4 -0
  52. package/lib/commands/warehouse.js +221 -0
  53. package/lib/index.d.ts +29 -0
  54. package/lib/index.js +6 -0
  55. package/lib/test/actions/create_invoice.d.ts +1 -0
  56. package/lib/test/actions/create_invoice.js +214 -0
  57. package/lib/test/actions/create_payment.d.ts +1 -0
  58. package/lib/test/actions/create_payment.js +131 -0
  59. package/lib/test/actions/create_proforma.d.ts +1 -0
  60. package/lib/test/actions/create_proforma.js +242 -0
  61. package/lib/test/actions/create_return_invoice.d.ts +1 -0
  62. package/lib/test/actions/create_return_invoice.js +226 -0
  63. package/lib/test/actions/create_transfer.d.ts +1 -0
  64. package/lib/test/actions/create_transfer.js +142 -0
  65. package/lib/test/commands/adjust_inventory.d.ts +1 -0
  66. package/lib/test/commands/adjust_inventory.js +97 -0
  67. package/lib/test/commands/bank.d.ts +1 -0
  68. package/lib/test/commands/bank.js +97 -0
  69. package/lib/test/commands/basic.d.ts +1 -0
  70. package/lib/test/commands/basic.js +339 -0
  71. package/lib/test/commands/category.d.ts +1 -0
  72. package/lib/test/commands/category.js +97 -0
  73. package/lib/test/commands/channel.d.ts +1 -0
  74. package/lib/test/commands/channel.js +97 -0
  75. package/lib/test/commands/client.d.ts +1 -0
  76. package/lib/test/commands/client.js +97 -0
  77. package/lib/test/commands/client_disabled.d.ts +1 -0
  78. package/lib/test/commands/client_disabled.js +97 -0
  79. package/lib/test/commands/join.d.ts +1 -0
  80. package/lib/test/commands/join.js +339 -0
  81. package/lib/test/commands/measureunit.d.ts +1 -0
  82. package/lib/test/commands/measureunit.js +97 -0
  83. package/lib/test/commands/measureunit_family.d.ts +1 -0
  84. package/lib/test/commands/measureunit_family.js +97 -0
  85. package/lib/test/commands/payment_term.d.ts +1 -0
  86. package/lib/test/commands/payment_term.js +97 -0
  87. package/lib/test/commands/price_list.d.ts +1 -0
  88. package/lib/test/commands/price_list.js +97 -0
  89. package/lib/test/commands/product.d.ts +1 -0
  90. package/lib/test/commands/product.js +97 -0
  91. package/lib/test/commands/product_disabled.d.ts +1 -0
  92. package/lib/test/commands/product_disabled.js +97 -0
  93. package/lib/test/commands/rep.d.ts +1 -0
  94. package/lib/test/commands/rep.js +97 -0
  95. package/lib/test/commands/tag.d.ts +1 -0
  96. package/lib/test/commands/tag.js +97 -0
  97. package/lib/test/commands/tax.d.ts +1 -0
  98. package/lib/test/commands/tax.js +97 -0
  99. package/lib/test/commands/warehouse.d.ts +1 -0
  100. package/lib/test/commands/warehouse.js +97 -0
  101. package/lib/types.d.ts +77 -0
  102. package/lib/types.js +1 -0
  103. package/lib/util.d.ts +70 -0
  104. package/lib/util.js +164 -0
  105. package/package.json +51 -0
  106. package/src/actions/create_invoice.ts +275 -0
  107. package/src/actions/create_payment.ts +138 -0
  108. package/src/actions/create_proforma.ts +187 -0
  109. package/src/actions/create_return_invoice.ts +217 -0
  110. package/src/actions/create_transfer.ts +160 -0
  111. package/src/actions/index.ts +27 -0
  112. package/src/commands/adjust_inventory.ts +250 -0
  113. package/src/commands/bank.ts +243 -0
  114. package/src/commands/basic.ts +64 -0
  115. package/src/commands/category.ts +176 -0
  116. package/src/commands/channel.ts +219 -0
  117. package/src/commands/client.ts +368 -0
  118. package/src/commands/client_disabled.ts +158 -0
  119. package/src/commands/index.ts +87 -0
  120. package/src/commands/join.ts +80 -0
  121. package/src/commands/measureunit.ts +276 -0
  122. package/src/commands/measureunit_family.ts +209 -0
  123. package/src/commands/payment_term.ts +224 -0
  124. package/src/commands/price_list.ts +434 -0
  125. package/src/commands/product.ts +359 -0
  126. package/src/commands/product_disabled.ts +152 -0
  127. package/src/commands/rep.ts +209 -0
  128. package/src/commands/tag.ts +173 -0
  129. package/src/commands/tax.ts +178 -0
  130. package/src/commands/warehouse.ts +204 -0
  131. package/src/index.ts +7 -0
  132. package/src/test/actions/create_invoice.ts +214 -0
  133. package/src/test/actions/create_payment.ts +131 -0
  134. package/src/test/actions/create_proforma.ts +242 -0
  135. package/src/test/actions/create_return_invoice.ts +226 -0
  136. package/src/test/actions/create_transfer.ts +142 -0
  137. package/src/test/commands/adjust_inventory.ts +100 -0
  138. package/src/test/commands/bank.ts +100 -0
  139. package/src/test/commands/basic.ts +342 -0
  140. package/src/test/commands/category.ts +100 -0
  141. package/src/test/commands/channel.ts +100 -0
  142. package/src/test/commands/client.ts +100 -0
  143. package/src/test/commands/client_disabled.ts +100 -0
  144. package/src/test/commands/join.ts +342 -0
  145. package/src/test/commands/measureunit.ts +100 -0
  146. package/src/test/commands/measureunit_family.ts +100 -0
  147. package/src/test/commands/payment_term.ts +100 -0
  148. package/src/test/commands/price_list.ts +100 -0
  149. package/src/test/commands/product.ts +100 -0
  150. package/src/test/commands/product_disabled.ts +100 -0
  151. package/src/test/commands/rep.ts +100 -0
  152. package/src/test/commands/tag.ts +100 -0
  153. package/src/test/commands/tax.ts +100 -0
  154. package/src/test/commands/warehouse.ts +100 -0
  155. package/src/types.ts +76 -0
  156. package/src/util.ts +205 -0
  157. package/tsconfig.json +15 -0
@@ -0,0 +1,275 @@
1
+ import Repzo from "repzo";
2
+ import { EVENT, Config } from "../types";
3
+ import { _fetch, _create, _update, _delete, get_data } from "../util.js";
4
+ import { Service } from "repzo/src/types";
5
+ import { v4 as uuid } from "uuid";
6
+ import moment from "moment-timezone";
7
+
8
+ interface SAPInvoiceItem {
9
+ ItemCode: string; // "010-LAG-PO0002";
10
+ Quantity: number; // 10;
11
+ TaxCode: string; // "S16";
12
+ UnitPrice: number; // 383.824;
13
+ DiscountPerc: string; // "0";
14
+ LineTotal: number; // 3838.24;
15
+ UomCode: number; // 3;
16
+ Brand: null;
17
+ Department: string; // "D6";
18
+ }
19
+
20
+ interface SAPInvoice {
21
+ RefNum: string; // "INV-1021-4",
22
+ SalPersCode?: string; // "106", // Required
23
+ DocDate: string; // "20211229",
24
+ DocDueDate: string; // "20211229",
25
+ ClientCode?: string; // "C00041", // Required
26
+ DiscountPerc: string; // "0",
27
+ Note?: string; // "",
28
+ WarehouseCode?: string; // "VS21"; // Required
29
+ LinesDetails: SAPInvoiceItem[];
30
+ }
31
+
32
+ interface SAPOpenInvoice {
33
+ CustomerNumber: string; // "400065-53";
34
+ FatherCode: string; // "400065";
35
+ DocDate: string; // "2022-12-14T21:00:00Z";
36
+ DocDueDate: string; // "2023-02-12T21:00:00Z";
37
+ InvoiceID: number; // 91210;
38
+ InvoiceNumber: string; // "INV-1028-1109";
39
+ InvoiceClientID: string; // "400065-53";
40
+ InvoiceFinalAmount: number; // 125.328;
41
+ InvoiceRemainingAmount: number; // 125.328;
42
+ InvoiceStatus: string; // "O";
43
+ }
44
+
45
+ interface SAPOpenInvoices {
46
+ result: "Success";
47
+ OpenInvoices: SAPOpenInvoice[];
48
+ }
49
+
50
+ export const create_invoice = async (event: EVENT, options: Config) => {
51
+ const repzo = new Repzo(options.data?.repzoApiKey, { env: options.env });
52
+ const action_sync_id: string = event?.headers?.action_sync_id || uuid();
53
+ const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
54
+ let body: Service.FullInvoice.InvoiceSchema | any;
55
+ try {
56
+ // console.log("create_invoice");
57
+ await actionLog.load(action_sync_id);
58
+
59
+ body = event.body;
60
+ try {
61
+ if (body) body = JSON.parse(body);
62
+ } catch (e) {}
63
+
64
+ const repzo_serial_number = body?.serial_number?.formatted;
65
+
66
+ await actionLog
67
+ .addDetail(
68
+ `Repzo => SAP: Started Create Invoice - ${repzo_serial_number}`
69
+ )
70
+ .commit();
71
+
72
+ const SAP_HOST_URL = options.data?.sapHostUrl;
73
+ if (!SAP_HOST_URL)
74
+ throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
75
+
76
+ const repzo_invoice: Service.FullInvoice.InvoiceSchema = body;
77
+
78
+ // Check if it is already exist in SAP
79
+ const sap_open_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
80
+ updatedAt: "",
81
+ Status: "",
82
+ InvoiceId: repzo_serial_number,
83
+ });
84
+
85
+ const open_invoice = sap_open_invoices?.find(
86
+ (inv) => inv?.InvoiceNumber === repzo_serial_number
87
+ );
88
+ if (open_invoice) {
89
+ await actionLog
90
+ .addDetail(`Checked Already in SAP `, open_invoice)
91
+ .addDetail(`Invoice - ${repzo_serial_number} Checked Already in SAP`)
92
+ .setStatus("success")
93
+ .setBody(repzo_invoice)
94
+ .commit();
95
+ return {
96
+ message: "Checked Already in SAP",
97
+ result: open_invoice,
98
+ };
99
+ }
100
+
101
+ // Check closed invoice in SAP
102
+ const sap_closed_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
103
+ updatedAt: "",
104
+ Status: "closed",
105
+ InvoiceId: repzo_serial_number,
106
+ });
107
+
108
+ const closed_invoice = sap_closed_invoices?.find(
109
+ (inv) => inv?.InvoiceNumber === repzo_serial_number
110
+ );
111
+ if (closed_invoice) {
112
+ await actionLog
113
+ .addDetail(`Checked Closed Already in SAP `, closed_invoice)
114
+ .addDetail(
115
+ `Invoice - ${repzo_serial_number} Checked Closed Already in SAP`
116
+ )
117
+ .setStatus("success")
118
+ .setBody(repzo_invoice)
119
+ .commit();
120
+ return {
121
+ message: "Checked Closed Already in SAP",
122
+ result: closed_invoice,
123
+ };
124
+ }
125
+
126
+ // Get Repzo Rep
127
+ let repzo_rep;
128
+ if (repzo_invoice.creator.type == "rep") {
129
+ repzo_rep = await repzo.rep.get(repzo_invoice?.creator?._id);
130
+ if (!repzo_rep)
131
+ throw `Rep with _id: ${repzo_invoice.creator._id} not found in Repzo`;
132
+ }
133
+
134
+ // Get Repzo Client
135
+ const repzo_client = await repzo.client.get(repzo_invoice?.client_id);
136
+ if (!repzo_client)
137
+ throw `Client with _id: ${repzo_invoice.client_id} not found in Repzo`;
138
+
139
+ // Get Repzo Warehouse
140
+ const repzo_warehouse = await repzo.warehouse.get(
141
+ repzo_invoice.origin_warehouse
142
+ );
143
+ if (!repzo_warehouse)
144
+ throw `warehouse with _id: ${repzo_invoice.origin_warehouse} not found in Repzo`;
145
+
146
+ const repzo_tax_ids: { [id: string]: true } = {};
147
+ const repzo_measureunit_ids: { [id: string]: true } = {};
148
+ const repzo_product_ids: { [id: string]: true } = {};
149
+
150
+ repzo_invoice.items?.forEach((item: Service.Item.Schema) => {
151
+ if (item) {
152
+ repzo_tax_ids[item.tax?._id] = true;
153
+ repzo_measureunit_ids[item.measureunit?._id] = true;
154
+ repzo_product_ids[item.variant?.product_id] = true;
155
+ }
156
+ });
157
+
158
+ const repzo_taxes = await get_data(
159
+ repzo.tax,
160
+ "_id",
161
+ Object.keys(repzo_tax_ids)
162
+ );
163
+ const repzo_measureunits = await get_data(
164
+ repzo.measureunit,
165
+ "_id",
166
+ Object.keys(repzo_measureunit_ids)
167
+ );
168
+ const repzo_products = await get_data(
169
+ repzo.product,
170
+ "_id",
171
+ Object.keys(repzo_product_ids)
172
+ );
173
+
174
+ // Prepare SAP_invoice_items
175
+ const items = [];
176
+
177
+ for (let i = 0; i < repzo_invoice?.items?.length; i++) {
178
+ const item = repzo_invoice.items[i];
179
+
180
+ // Get Repzo Tax
181
+ const repzo_tax = repzo_taxes?.find(
182
+ (t) => t._id?.toString() == item.tax?._id?.toString()
183
+ );
184
+ if (!repzo_tax) throw `Tax with _id: ${item.tax._id} not found in Repzo`;
185
+
186
+ // Get Repzo UoM
187
+ const repzo_measureunit = repzo_measureunits?.find(
188
+ (m) => m._id?.toString() == item.measureunit?._id?.toString()
189
+ );
190
+ if (!repzo_measureunit)
191
+ throw `Uom with _id: ${item.measureunit?._id} not found in Repzo`;
192
+
193
+ // Get Repzo Product
194
+ const repzo_product = repzo_products?.find(
195
+ (p) => p._id?.toString() == item.variant?.product_id?.toString()
196
+ );
197
+ if (!repzo_product)
198
+ throw `Product with _id: ${item.measureunit._id} not found in Repzo`;
199
+
200
+ items.push({
201
+ ItemCode: item.variant.variant_name,
202
+ Quantity: item.qty,
203
+ TaxCode: repzo_tax.integration_meta.TaxCode,
204
+ UnitPrice: (item.price * repzo_measureunit.factor) / 1000,
205
+ DiscountPerc: "0", // ??
206
+ //@ts-ignore
207
+ LineTotal: item.total_before_tax / 1000,
208
+ UomCode: repzo_measureunit.integration_meta.ALTUOMID,
209
+ Brand: repzo_product.integration_meta?.BRAND, // "B1", // ??
210
+ Department:
211
+ repzo_rep?.integration_meta?.DEPARTMENTCODE ||
212
+ options.data?.DepartmentCode, // "D2",
213
+ });
214
+ }
215
+
216
+ const sap_invoice: SAPInvoice = {
217
+ RefNum: repzo_invoice.serial_number.formatted,
218
+ SalPersCode: repzo_rep?.integration_id,
219
+ DocDate: moment(repzo_invoice.issue_date, "YYYY-MM-DD").format(
220
+ "YYYYMMDD"
221
+ ),
222
+ DocDueDate: moment(repzo_invoice.due_date, "YYYY-MM-DD").format(
223
+ "YYYYMMDD"
224
+ ),
225
+ ClientCode: repzo_client.client_code,
226
+ DiscountPerc: "0",
227
+ Note: repzo_invoice.comment,
228
+ WarehouseCode: repzo_warehouse.code,
229
+ LinesDetails: items,
230
+ };
231
+
232
+ // console.dir(sap_invoice, { depth: null });
233
+
234
+ await actionLog
235
+ .addDetail(`Repzo => SAP: Invoice - ${repzo_serial_number}`, sap_invoice)
236
+ .commit();
237
+
238
+ const result = await _create(SAP_HOST_URL, "/AddInvoice", sap_invoice);
239
+
240
+ // console.log(result);
241
+
242
+ await actionLog
243
+ .addDetail(`SAP Responded with `, result)
244
+ .addDetail(`Repzo => SAP: Invoice - ${repzo_serial_number}`)
245
+ .setStatus("success")
246
+ .setBody(repzo_invoice)
247
+ .commit();
248
+ return result;
249
+ } catch (e: any) {
250
+ //@ts-ignore
251
+ console.error(e?.response || e);
252
+ await actionLog.setStatus("fail", e).setBody(body).commit();
253
+ throw e;
254
+ }
255
+ };
256
+
257
+ export const get_invoice_from_sap = async (
258
+ serviceEndPoint: string,
259
+ query?: { updatedAt: string; Status: string; InvoiceId: string }
260
+ ): Promise<SAPOpenInvoice[]> => {
261
+ try {
262
+ const sap_openInvoices: SAPOpenInvoices = await _create(
263
+ serviceEndPoint,
264
+ "/OpenInvoices",
265
+ {
266
+ updatedAt: query?.updatedAt,
267
+ Status: query?.Status,
268
+ InvoiceId: query?.InvoiceId,
269
+ }
270
+ );
271
+ return sap_openInvoices?.OpenInvoices;
272
+ } catch (e: any) {
273
+ throw e;
274
+ }
275
+ };
@@ -0,0 +1,138 @@
1
+ import Repzo from "repzo";
2
+ import { EVENT, Config } from "../types";
3
+ import { _fetch, _create, _update, _delete, get_data } from "../util.js";
4
+ import { Service } from "repzo/src/types";
5
+ import { v4 as uuid } from "uuid";
6
+ import moment from "moment-timezone";
7
+ import { get_invoice_from_sap } from "./create_invoice.js";
8
+
9
+ interface SAPPayment {
10
+ PaymentID: string; // "PAY-1041-3";
11
+ ClientCode?: string; // "C00767"; => required
12
+ PaymentDate: string; // "20211124";
13
+ Amount: string | number; // "3.625";
14
+ InvoiceID?: string | number; // "92560";
15
+ InvoiceTotal?: string | number; // "3.625";
16
+ PaymentType?: "1" | "2"; // 1 => CashAccount, 2 => ChequeAccount;
17
+ CashAccount?: string; // "124020012";
18
+ ChequeAccount?: string;
19
+ ChequeDate?: string;
20
+ ChequeNumber?: string | number;
21
+ BankCode?: string;
22
+ CountryCode?: string; // "JO";
23
+ }
24
+
25
+ export const create_payment = async (event: EVENT, options: Config) => {
26
+ const repzo = new Repzo(options.data?.repzoApiKey, { env: options.env });
27
+ const action_sync_id: string = event?.headers?.action_sync_id || uuid();
28
+ const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
29
+ let body: Service.Payment.PaymentSchema | any;
30
+ try {
31
+ // console.log("create_payment");
32
+ await actionLog.load(action_sync_id);
33
+
34
+ body = event.body;
35
+ try {
36
+ if (body) body = JSON.parse(body);
37
+ } catch (e) {}
38
+
39
+ const repzo_serial_number = body?.serial_number?.formatted;
40
+
41
+ await actionLog
42
+ .addDetail(
43
+ `Repzo => SAP: Started Create Payment - ${repzo_serial_number}`
44
+ )
45
+ .commit();
46
+
47
+ const SAP_HOST_URL = options.data?.sapHostUrl;
48
+ if (!SAP_HOST_URL)
49
+ throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
50
+
51
+ const repzo_payment: Service.Payment.PaymentSchema = body;
52
+
53
+ // Get Repzo Rep
54
+ let repzo_rep;
55
+ if (repzo_payment.creator.type == "rep") {
56
+ repzo_rep = await repzo.rep.get(repzo_payment?.creator?._id);
57
+ if (!repzo_rep)
58
+ throw `Rep with _id: ${repzo_payment.creator._id} not found in Repzo`;
59
+ }
60
+
61
+ // Get Repzo Client
62
+ const repzo_client = await repzo.client.get(repzo_payment?.client_id);
63
+ if (!repzo_client)
64
+ throw `Client with _id: ${repzo_payment.client_id} not found in Repzo`;
65
+
66
+ const sap_payment: SAPPayment = {
67
+ PaymentID: repzo_serial_number,
68
+ ClientCode: 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
+
75
+ if (repzo_payment?.LinkedTxn) {
76
+ const sap_open_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
77
+ updatedAt: "",
78
+ Status: "",
79
+ InvoiceId: repzo_payment?.LinkedTxn?.Txn_serial_number?.formatted,
80
+ });
81
+ if (!sap_open_invoices?.length) {
82
+ throw `Invoice with serial number: ${repzo_payment.LinkedTxn.Txn_serial_number.formatted} was not found on SAP or was closed`;
83
+ }
84
+ sap_payment.InvoiceID = sap_open_invoices[0].InvoiceID;
85
+ sap_payment.InvoiceTotal =
86
+ repzo_payment?.LinkedTxn?.Txn_invoice_total / 1000;
87
+ } else {
88
+ sap_payment.InvoiceID = "";
89
+ sap_payment.InvoiceTotal = "";
90
+ }
91
+
92
+ if (repzo_payment.payment_type == "check") {
93
+ // bank
94
+ //@ts-ignore
95
+ const repzo_bank = await repzo.bank.get(repzo_payment.check?.bank);
96
+ if (!repzo_bank) {
97
+ //@ts-ignore
98
+ throw `Bank with _id: ${repzo_payment.check.bank} not found in Repzo`;
99
+ }
100
+ sap_payment.PaymentType = "2";
101
+ sap_payment.ChequeAccount =
102
+ repzo_rep?.integration_meta?.USERCHECKACCTCODE;
103
+ sap_payment.ChequeDate = moment(
104
+ repzo_payment.check?.check_date,
105
+ "YYYY-MM-DD"
106
+ ).format("YYYYMMDD");
107
+ sap_payment.ChequeNumber = repzo_payment.check?.check_number;
108
+ sap_payment.BankCode = repzo_bank.integration_meta?.BANKCODE;
109
+ sap_payment.CountryCode = repzo_bank.integration_meta?.COUNTRY;
110
+ } else if (repzo_payment.payment_type == "cash") {
111
+ sap_payment.PaymentType = "1";
112
+ sap_payment.CashAccount = repzo_rep?.integration_meta?.USERCASHACCOUNT;
113
+ }
114
+
115
+ // console.dir(sap_payment, { depth: null });
116
+
117
+ await actionLog
118
+ .addDetail(`Repzo => SAP: Payment - ${repzo_serial_number}`, sap_payment)
119
+ .commit();
120
+
121
+ const result = await _create(SAP_HOST_URL, "/AddPayment", sap_payment);
122
+
123
+ // console.log(result);
124
+
125
+ await actionLog
126
+ .addDetail(`SAP Responded with `, result)
127
+ .addDetail(`Repzo => SAP: Payment - ${repzo_serial_number}`)
128
+ .setStatus("success")
129
+ .setBody(repzo_payment)
130
+ .commit();
131
+ return result;
132
+ } catch (e: any) {
133
+ //@ts-ignore
134
+ console.error(e?.response || e);
135
+ await actionLog.setStatus("fail", e).setBody(body).commit();
136
+ throw e;
137
+ }
138
+ };
@@ -0,0 +1,187 @@
1
+ import Repzo from "repzo";
2
+ import { EVENT, Config } from "../types";
3
+ import { _fetch, _create, _update, _delete, get_data } from "../util.js";
4
+ import { Service } from "repzo/src/types";
5
+ import { v4 as uuid } from "uuid";
6
+ import moment from "moment-timezone";
7
+
8
+ interface SAPProformaItem {
9
+ ItemCode: string; // "010-LAG-PO0002";
10
+ Quantity: number; // 10;
11
+ TaxCode: string; // "S16";
12
+ UnitPrice: number; // 383.824;
13
+ DiscountPerc: string; // "0";
14
+ LineTotal: number; // 3838.24;
15
+ UomCode: number; // 3;
16
+ Brand: null;
17
+ Department: string; // "D6";
18
+ }
19
+
20
+ interface SAPProforma {
21
+ RefNum: string; // "INV-1021-4",
22
+ SalPersCode: string; // "106",
23
+ DocDate: string; // "20211229",
24
+ DocDueDate: string; // "20211229",
25
+ ClientCode?: string; // "C00041", Required
26
+ DiscountPerc: string; // "0",
27
+ Note?: string; // "",
28
+ WarehouseCode?: string; // "VS21";
29
+ LinesDetails: SAPProformaItem[];
30
+ }
31
+
32
+ export const create_proforma = async (event: EVENT, options: Config) => {
33
+ const repzo = new Repzo(options.data?.repzoApiKey, { env: options.env });
34
+ const action_sync_id: string = event?.headers?.action_sync_id || uuid();
35
+ const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
36
+ let body: Service.Proforma.ProformaSchema | any;
37
+ try {
38
+ // console.log("create_proforma");
39
+ await actionLog.load(action_sync_id);
40
+
41
+ body = event.body;
42
+ try {
43
+ if (body) body = JSON.parse(body);
44
+ } catch (e) {}
45
+
46
+ const repzo_serial_number = body?.serial_number?.formatted;
47
+
48
+ await actionLog
49
+ .addDetail(
50
+ `Repzo => SAP: Started Create SalesOrder - ${repzo_serial_number}`
51
+ )
52
+ .commit();
53
+
54
+ const SAP_HOST_URL = options.data?.sapHostUrl;
55
+ if (!SAP_HOST_URL)
56
+ throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
57
+
58
+ const repzo_proforma: Service.Proforma.ProformaSchema = body;
59
+
60
+ // Get Repzo Rep
61
+ let repzo_rep;
62
+ if (repzo_proforma.creator.type == "rep") {
63
+ repzo_rep = await repzo.rep.get(repzo_proforma?.creator?._id);
64
+ if (!repzo_rep)
65
+ throw `Rep with _id: ${repzo_proforma.creator._id} not found in Repzo`;
66
+ }
67
+
68
+ // Get Repzo Client
69
+ const repzo_client = await repzo.client.get(repzo_proforma?.client_id);
70
+ if (!repzo_client)
71
+ throw `Client with _id: ${repzo_proforma.client_id} not found in Repzo`;
72
+
73
+ const repzo_tax_ids: { [id: string]: true } = {};
74
+ const repzo_measureunit_ids: { [id: string]: true } = {};
75
+ const repzo_product_ids: { [id: string]: true } = {};
76
+
77
+ repzo_proforma.items?.forEach((item: Service.Item.Schema) => {
78
+ if (item) {
79
+ repzo_tax_ids[item.tax?._id] = true;
80
+ repzo_measureunit_ids[item.measureunit?._id] = true;
81
+ repzo_product_ids[item.variant?.product_id] = true;
82
+ }
83
+ });
84
+
85
+ const repzo_taxes = await get_data(
86
+ repzo.tax,
87
+ "_id",
88
+ Object.keys(repzo_tax_ids)
89
+ );
90
+ const repzo_measureunits = await get_data(
91
+ repzo.measureunit,
92
+ "_id",
93
+ Object.keys(repzo_measureunit_ids)
94
+ );
95
+ const repzo_products = await get_data(
96
+ repzo.product,
97
+ "_id",
98
+ Object.keys(repzo_product_ids)
99
+ );
100
+
101
+ // Prepare SAP_invoice_items
102
+ const items: SAPProformaItem[] = [];
103
+
104
+ for (let i = 0; i < repzo_proforma?.items?.length; i++) {
105
+ const item = repzo_proforma.items[i];
106
+
107
+ // Get Repzo Tax
108
+ const repzo_tax = repzo_taxes?.find(
109
+ (t) => t._id?.toString() == item.tax?._id?.toString()
110
+ );
111
+ if (!repzo_tax) throw `Tax with _id: ${item.tax._id} not found in Repzo`;
112
+
113
+ // Get Repzo UoM
114
+ const repzo_measureunit = repzo_measureunits?.find(
115
+ (m) => m._id?.toString() == item.measureunit?._id?.toString()
116
+ );
117
+ if (!repzo_measureunit)
118
+ throw `Uom with _id: ${item.measureunit?._id} not found in Repzo`;
119
+
120
+ // Get Repzo Product
121
+ const repzo_product = repzo_products?.find(
122
+ (p) => p._id?.toString() == item.variant?.product_id?.toString()
123
+ );
124
+ if (!repzo_product)
125
+ throw `Product with _id: ${item.measureunit._id} not found in Repzo`;
126
+
127
+ items.push({
128
+ ItemCode: item.variant.variant_name,
129
+ Quantity: item.qty,
130
+ TaxCode: repzo_tax.integration_meta.TaxCode,
131
+ UnitPrice: (item.price * repzo_measureunit.factor) / 1000,
132
+ DiscountPerc: "0",
133
+ //@ts-ignore
134
+ LineTotal: item.total_before_tax / 1000,
135
+ UomCode: repzo_measureunit.integration_meta.ALTUOMID,
136
+ Brand: repzo_product.integration_meta?.BRAND,
137
+ Department:
138
+ repzo_rep?.integration_meta?.DEPARTMENTCODE ||
139
+ options.data?.DepartmentCode, // "D2"
140
+ });
141
+ }
142
+
143
+ const sap_invoice: SAPProforma = {
144
+ RefNum: repzo_proforma.serial_number.formatted,
145
+ SalPersCode: repzo_rep
146
+ ? repzo_rep.integration_id
147
+ : options.data?.SalPersCode, // "111",
148
+ DocDate: moment(repzo_proforma.issue_date, "YYYY-MM-DD").format(
149
+ "YYYYMMDD"
150
+ ),
151
+ DocDueDate: moment(repzo_proforma.issue_date, "YYYY-MM-DD").format(
152
+ "YYYYMMDD"
153
+ ),
154
+ ClientCode: repzo_client.client_code,
155
+ DiscountPerc: "0",
156
+ Note: repzo_proforma.comment,
157
+ // WarehouseCode: "1",
158
+ LinesDetails: items,
159
+ };
160
+
161
+ // console.dir(sap_invoice, { depth: null });
162
+
163
+ await actionLog
164
+ .addDetail(
165
+ `Repzo => SAP: SalesOrder - ${repzo_serial_number}`,
166
+ sap_invoice
167
+ )
168
+ .commit();
169
+
170
+ const result = await _create(SAP_HOST_URL, "/AddOrder", sap_invoice);
171
+
172
+ // console.log(result);
173
+
174
+ await actionLog
175
+ .addDetail(`SAP Responded with `, result)
176
+ .addDetail(`Repzo => SAP: SalesOrder - ${repzo_serial_number}`)
177
+ .setStatus("success")
178
+ .setBody(repzo_proforma)
179
+ .commit();
180
+ return result;
181
+ } catch (e: any) {
182
+ //@ts-ignore
183
+ console.error(e?.response || e);
184
+ await actionLog.setStatus("fail", e).setBody(body).commit();
185
+ throw e;
186
+ }
187
+ };