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,241 @@
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_proforma = async (event, options) => {
6
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
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_proforma");
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 SalesOrder - ${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_proforma = body;
40
+ // Get Repzo Rep
41
+ let repzo_rep;
42
+ if (repzo_proforma.creator.type == "rep") {
43
+ repzo_rep = await repzo.rep.get(
44
+ (_e =
45
+ repzo_proforma === null || repzo_proforma === void 0
46
+ ? void 0
47
+ : repzo_proforma.creator) === null || _e === void 0
48
+ ? void 0
49
+ : _e._id
50
+ );
51
+ if (!repzo_rep)
52
+ throw `Rep with _id: ${repzo_proforma.creator._id} not found in Repzo`;
53
+ }
54
+ // Get Repzo Client
55
+ const repzo_client = await repzo.client.get(
56
+ repzo_proforma === null || repzo_proforma === void 0
57
+ ? void 0
58
+ : repzo_proforma.client_id
59
+ );
60
+ if (!repzo_client)
61
+ throw `Client with _id: ${repzo_proforma.client_id} not found in Repzo`;
62
+ const repzo_tax_ids = {};
63
+ const repzo_measureunit_ids = {};
64
+ const repzo_product_ids = {};
65
+ (_f = repzo_proforma.items) === null || _f === void 0
66
+ ? void 0
67
+ : _f.forEach((item) => {
68
+ var _a, _b, _c;
69
+ if (item) {
70
+ repzo_tax_ids[
71
+ (_a = item.tax) === null || _a === void 0 ? void 0 : _a._id
72
+ ] = true;
73
+ repzo_measureunit_ids[
74
+ (_b = item.measureunit) === null || _b === void 0
75
+ ? void 0
76
+ : _b._id
77
+ ] = true;
78
+ repzo_product_ids[
79
+ (_c = item.variant) === null || _c === void 0
80
+ ? void 0
81
+ : _c.product_id
82
+ ] = true;
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
+ // Prepare SAP_invoice_items
101
+ const items = [];
102
+ for (
103
+ let i = 0;
104
+ i <
105
+ ((_g =
106
+ repzo_proforma === null || repzo_proforma === void 0
107
+ ? void 0
108
+ : repzo_proforma.items) === null || _g === void 0
109
+ ? void 0
110
+ : _g.length);
111
+ i++
112
+ ) {
113
+ const item = repzo_proforma.items[i];
114
+ // Get Repzo Tax
115
+ const repzo_tax =
116
+ repzo_taxes === null || repzo_taxes === void 0
117
+ ? void 0
118
+ : repzo_taxes.find((t) => {
119
+ var _a, _b, _c;
120
+ return (
121
+ ((_a = t._id) === null || _a === void 0
122
+ ? void 0
123
+ : _a.toString()) ==
124
+ ((_c =
125
+ (_b = item.tax) === null || _b === void 0
126
+ ? void 0
127
+ : _b._id) === null || _c === void 0
128
+ ? void 0
129
+ : _c.toString())
130
+ );
131
+ });
132
+ if (!repzo_tax) throw `Tax with _id: ${item.tax._id} not found in Repzo`;
133
+ // Get Repzo UoM
134
+ const repzo_measureunit =
135
+ repzo_measureunits === null || repzo_measureunits === void 0
136
+ ? void 0
137
+ : repzo_measureunits.find((m) => {
138
+ var _a, _b, _c;
139
+ return (
140
+ ((_a = m._id) === null || _a === void 0
141
+ ? void 0
142
+ : _a.toString()) ==
143
+ ((_c =
144
+ (_b = item.measureunit) === null || _b === void 0
145
+ ? void 0
146
+ : _b._id) === null || _c === void 0
147
+ ? void 0
148
+ : _c.toString())
149
+ );
150
+ });
151
+ if (!repzo_measureunit)
152
+ throw `Uom with _id: ${
153
+ (_h = item.measureunit) === null || _h === void 0 ? void 0 : _h._id
154
+ } not found in Repzo`;
155
+ // Get Repzo Product
156
+ const repzo_product =
157
+ repzo_products === null || repzo_products === void 0
158
+ ? void 0
159
+ : repzo_products.find((p) => {
160
+ var _a, _b, _c;
161
+ return (
162
+ ((_a = p._id) === null || _a === void 0
163
+ ? void 0
164
+ : _a.toString()) ==
165
+ ((_c =
166
+ (_b = item.variant) === null || _b === void 0
167
+ ? void 0
168
+ : _b.product_id) === null || _c === void 0
169
+ ? void 0
170
+ : _c.toString())
171
+ );
172
+ });
173
+ if (!repzo_product)
174
+ throw `Product with _id: ${item.measureunit._id} not found in Repzo`;
175
+ items.push({
176
+ ItemCode: item.variant.variant_name,
177
+ Quantity: item.qty,
178
+ TaxCode: repzo_tax.integration_meta.TaxCode,
179
+ UnitPrice: (item.price * repzo_measureunit.factor) / 1000,
180
+ DiscountPerc: "0",
181
+ //@ts-ignore
182
+ LineTotal: item.total_before_tax / 1000,
183
+ UomCode: repzo_measureunit.integration_meta.ALTUOMID,
184
+ Brand:
185
+ (_j = repzo_product.integration_meta) === null || _j === void 0
186
+ ? void 0
187
+ : _j.BRAND,
188
+ Department:
189
+ ((_k =
190
+ repzo_rep === null || repzo_rep === void 0
191
+ ? void 0
192
+ : repzo_rep.integration_meta) === null || _k === void 0
193
+ ? void 0
194
+ : _k.DEPARTMENTCODE) ||
195
+ ((_l = options.data) === null || _l === void 0
196
+ ? void 0
197
+ : _l.DepartmentCode), // "D2"
198
+ });
199
+ }
200
+ const sap_invoice = {
201
+ RefNum: repzo_proforma.serial_number.formatted,
202
+ SalPersCode: repzo_rep
203
+ ? repzo_rep.integration_id
204
+ : (_m = options.data) === null || _m === void 0
205
+ ? void 0
206
+ : _m.SalPersCode,
207
+ DocDate: moment(repzo_proforma.issue_date, "YYYY-MM-DD").format(
208
+ "YYYYMMDD"
209
+ ),
210
+ DocDueDate: moment(repzo_proforma.issue_date, "YYYY-MM-DD").format(
211
+ "YYYYMMDD"
212
+ ),
213
+ ClientCode: repzo_client.client_code,
214
+ DiscountPerc: "0",
215
+ Note: repzo_proforma.comment,
216
+ // WarehouseCode: "1",
217
+ LinesDetails: items,
218
+ };
219
+ // console.dir(sap_invoice, { depth: null });
220
+ await actionLog
221
+ .addDetail(
222
+ `Repzo => SAP: SalesOrder - ${repzo_serial_number}`,
223
+ sap_invoice
224
+ )
225
+ .commit();
226
+ const result = await _create(SAP_HOST_URL, "/AddOrder", sap_invoice);
227
+ // console.log(result);
228
+ await actionLog
229
+ .addDetail(`SAP Responded with `, result)
230
+ .addDetail(`Repzo => SAP: SalesOrder - ${repzo_serial_number}`)
231
+ .setStatus("success")
232
+ .setBody(repzo_proforma)
233
+ .commit();
234
+ return result;
235
+ } catch (e) {
236
+ //@ts-ignore
237
+ console.error((e === null || e === void 0 ? void 0 : e.response) || e);
238
+ await actionLog.setStatus("fail", e).setBody(body).commit();
239
+ throw e;
240
+ }
241
+ };
@@ -0,0 +1,5 @@
1
+ import { EVENT, Config } from "../types";
2
+ export declare const create_return_invoice: (
3
+ event: EVENT,
4
+ options: Config
5
+ ) => Promise<any>;
@@ -0,0 +1,268 @@
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_return_invoice = async (event, options) => {
6
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
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_return_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 Return 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
+ // Get Return Reason
41
+ let return_reasons =
42
+ (_e = options.data) === null || _e === void 0
43
+ ? void 0
44
+ : _e.return_reasons;
45
+ try {
46
+ if (typeof return_reasons == "string")
47
+ return_reasons = JSON.parse(return_reasons);
48
+ } catch (e) {}
49
+ // Get Repzo Rep
50
+ let repzo_rep;
51
+ if (repzo_invoice.creator.type == "rep") {
52
+ repzo_rep = await repzo.rep.get(
53
+ (_f =
54
+ repzo_invoice === null || repzo_invoice === void 0
55
+ ? void 0
56
+ : repzo_invoice.creator) === null || _f === void 0
57
+ ? void 0
58
+ : _f._id
59
+ );
60
+ if (!repzo_rep)
61
+ throw `Rep with _id: ${repzo_invoice.creator._id} not found in Repzo`;
62
+ }
63
+ // Get Repzo Client
64
+ const repzo_client = await repzo.client.get(
65
+ repzo_invoice === null || repzo_invoice === void 0
66
+ ? void 0
67
+ : repzo_invoice.client_id
68
+ );
69
+ if (!repzo_client)
70
+ throw `Client with _id: ${repzo_invoice.client_id} not found in Repzo`;
71
+ // Get Repzo Warehouse
72
+ const repzo_warehouse = await repzo.warehouse.get(
73
+ repzo_invoice.origin_warehouse
74
+ );
75
+ if (!repzo_warehouse)
76
+ throw `warehouse with _id: ${repzo_invoice.origin_warehouse} not found in Repzo`;
77
+ const repzo_tax_ids = {};
78
+ const repzo_measureunit_ids = {};
79
+ const repzo_product_ids = {};
80
+ (_g = repzo_invoice.return_items) === null || _g === void 0
81
+ ? void 0
82
+ : _g.forEach((item) => {
83
+ var _a, _b, _c;
84
+ if (item) {
85
+ repzo_tax_ids[
86
+ (_a = item.tax) === null || _a === void 0 ? void 0 : _a._id
87
+ ] = true;
88
+ repzo_measureunit_ids[
89
+ (_b = item.measureunit) === null || _b === void 0
90
+ ? void 0
91
+ : _b._id
92
+ ] = true;
93
+ repzo_product_ids[
94
+ (_c = item.variant) === null || _c === void 0
95
+ ? void 0
96
+ : _c.product_id
97
+ ] = true;
98
+ }
99
+ });
100
+ const repzo_taxes = await get_data(
101
+ repzo.tax,
102
+ "_id",
103
+ Object.keys(repzo_tax_ids)
104
+ );
105
+ const repzo_measureunits = await get_data(
106
+ repzo.measureunit,
107
+ "_id",
108
+ Object.keys(repzo_measureunit_ids)
109
+ );
110
+ const repzo_products = await get_data(
111
+ repzo.product,
112
+ "_id",
113
+ Object.keys(repzo_product_ids)
114
+ );
115
+ // Prepare SAP_invoice_items
116
+ const return_items = [];
117
+ for (
118
+ let i = 0;
119
+ i <
120
+ ((_h =
121
+ repzo_invoice === null || repzo_invoice === void 0
122
+ ? void 0
123
+ : repzo_invoice.return_items) === null || _h === void 0
124
+ ? void 0
125
+ : _h.length);
126
+ i++
127
+ ) {
128
+ const item = repzo_invoice.return_items[i];
129
+ // Get Repzo Tax
130
+ const repzo_tax =
131
+ repzo_taxes === null || repzo_taxes === void 0
132
+ ? void 0
133
+ : repzo_taxes.find((t) => {
134
+ var _a, _b, _c;
135
+ return (
136
+ ((_a = t._id) === null || _a === void 0
137
+ ? void 0
138
+ : _a.toString()) ==
139
+ ((_c =
140
+ (_b = item.tax) === null || _b === void 0
141
+ ? void 0
142
+ : _b._id) === null || _c === void 0
143
+ ? void 0
144
+ : _c.toString())
145
+ );
146
+ });
147
+ if (!repzo_tax) throw `Tax with _id: ${item.tax._id} not found in Repzo`;
148
+ // Get Repzo UoM
149
+ const repzo_measureunit =
150
+ repzo_measureunits === null || repzo_measureunits === void 0
151
+ ? void 0
152
+ : repzo_measureunits.find((m) => {
153
+ var _a, _b, _c;
154
+ return (
155
+ ((_a = m._id) === null || _a === void 0
156
+ ? void 0
157
+ : _a.toString()) ==
158
+ ((_c =
159
+ (_b = item.measureunit) === null || _b === void 0
160
+ ? void 0
161
+ : _b._id) === null || _c === void 0
162
+ ? void 0
163
+ : _c.toString())
164
+ );
165
+ });
166
+ if (!repzo_measureunit)
167
+ throw `Uom with _id: ${
168
+ (_j = item.measureunit) === null || _j === void 0 ? void 0 : _j._id
169
+ } not found in Repzo`;
170
+ // Get Repzo Product
171
+ const repzo_product =
172
+ repzo_products === null || repzo_products === void 0
173
+ ? void 0
174
+ : repzo_products.find((p) => {
175
+ var _a, _b, _c;
176
+ return (
177
+ ((_a = p._id) === null || _a === void 0
178
+ ? void 0
179
+ : _a.toString()) ==
180
+ ((_c =
181
+ (_b = item.variant) === null || _b === void 0
182
+ ? void 0
183
+ : _b.product_id) === null || _c === void 0
184
+ ? void 0
185
+ : _c.toString())
186
+ );
187
+ });
188
+ if (!repzo_product)
189
+ throw `Product with _id: ${item.measureunit._id} not found in Repzo`;
190
+ // Get SAP return_reason
191
+ const item_return_reason =
192
+ return_reasons === null || return_reasons === void 0
193
+ ? void 0
194
+ : return_reasons.find((r) => r.repzo_id === item.return_reason);
195
+ return_items.push({
196
+ ItemCode: item.variant.variant_name,
197
+ Quantity: -1 * item.qty,
198
+ TaxCode: repzo_tax.integration_meta.TaxCode,
199
+ UnitPrice: (item.price * repzo_measureunit.factor) / 1000,
200
+ DiscountPerc: "0",
201
+ //@ts-ignore
202
+ LineTotal: (-1 * item.total_before_tax) / 1000,
203
+ UomCode: repzo_measureunit.integration_meta.ALTUOMID,
204
+ Brand:
205
+ (_k = repzo_product.integration_meta) === null || _k === void 0
206
+ ? void 0
207
+ : _k.BRAND,
208
+ Department:
209
+ ((_l =
210
+ repzo_rep === null || repzo_rep === void 0
211
+ ? void 0
212
+ : repzo_rep.integration_meta) === null || _l === void 0
213
+ ? void 0
214
+ : _l.DEPARTMENTCODE) ||
215
+ ((_m = options.data) === null || _m === void 0
216
+ ? void 0
217
+ : _m.DepartmentCode),
218
+ Return_Reason:
219
+ item_return_reason === null || item_return_reason === void 0
220
+ ? void 0
221
+ : item_return_reason.sap_id,
222
+ });
223
+ }
224
+ const sap_return_invoice = {
225
+ RefNum: repzo_invoice.serial_number.formatted,
226
+ SalPersCode:
227
+ repzo_rep === null || repzo_rep === void 0
228
+ ? void 0
229
+ : repzo_rep.integration_id,
230
+ DocDate: moment(repzo_invoice.issue_date, "YYYY-MM-DD").format(
231
+ "YYYYMMDD"
232
+ ),
233
+ DocDueDate: moment(repzo_invoice.due_date, "YYYY-MM-DD").format(
234
+ "YYYYMMDD"
235
+ ),
236
+ ClientCode: repzo_client.client_code,
237
+ DiscountPerc: "0",
238
+ Note: repzo_invoice.comment,
239
+ WarehouseCode: repzo_warehouse.code,
240
+ LinesDetails: return_items,
241
+ };
242
+ // console.dir(sap_return_invoice, { depth: null });
243
+ await actionLog
244
+ .addDetail(
245
+ `Repzo => SAP: Invoice - ${repzo_serial_number}`,
246
+ sap_return_invoice
247
+ )
248
+ .commit();
249
+ const result = await _create(
250
+ SAP_HOST_URL,
251
+ "/AddCreditMemo",
252
+ sap_return_invoice
253
+ );
254
+ // console.log(result);
255
+ await actionLog
256
+ .addDetail(`SAP Responded with `, result)
257
+ .addDetail(`Repzo => SAP: Return Invoice - ${repzo_serial_number}`)
258
+ .setStatus("success")
259
+ .setBody(repzo_invoice)
260
+ .commit();
261
+ return result;
262
+ } catch (e) {
263
+ //@ts-ignore
264
+ console.error((e === null || e === void 0 ? void 0 : e.response) || e);
265
+ await actionLog.setStatus("fail", e).setBody(body).commit();
266
+ throw e;
267
+ }
268
+ };
@@ -0,0 +1,5 @@
1
+ import { EVENT, Config } from "../types";
2
+ export declare const create_transfer: (
3
+ event: EVENT,
4
+ options: Config
5
+ ) => Promise<any>;
@@ -0,0 +1,171 @@
1
+ import Repzo from "repzo";
2
+ import { _create, get_data } from "../util.js";
3
+ import { v4 as uuid } from "uuid";
4
+ export const create_transfer = async (event, options) => {
5
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
6
+ const repzo = new Repzo(
7
+ (_a = options.data) === null || _a === void 0 ? void 0 : _a.repzoApiKey,
8
+ { env: options.env }
9
+ );
10
+ const action_sync_id =
11
+ ((_b = event === null || event === void 0 ? void 0 : event.headers) ===
12
+ null || _b === void 0
13
+ ? void 0
14
+ : _b.action_sync_id) || uuid();
15
+ const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
16
+ let body;
17
+ try {
18
+ // console.log("create_transfer");
19
+ await actionLog.load(action_sync_id);
20
+ body = event.body;
21
+ try {
22
+ if (body) body = JSON.parse(body);
23
+ } catch (e) {}
24
+ const repzo_serial_number =
25
+ (_c = body === null || body === void 0 ? void 0 : body.serial_number) ===
26
+ null || _c === void 0
27
+ ? void 0
28
+ : _c.formatted;
29
+ await actionLog
30
+ .addDetail(
31
+ `Repzo => SAP: Started Create Transfer - ${repzo_serial_number}`
32
+ )
33
+ .commit();
34
+ const SAP_HOST_URL =
35
+ (_d = options.data) === null || _d === void 0 ? void 0 : _d.sapHostUrl;
36
+ if (!SAP_HOST_URL)
37
+ throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
38
+ const repzo_transfer = body;
39
+ // Get Repzo Rep
40
+ let repzo_rep;
41
+ if (repzo_transfer.creator.type == "rep") {
42
+ repzo_rep = await repzo.rep.get(
43
+ (_e =
44
+ repzo_transfer === null || repzo_transfer === void 0
45
+ ? void 0
46
+ : repzo_transfer.creator) === null || _e === void 0
47
+ ? void 0
48
+ : _e._id
49
+ );
50
+ if (!repzo_rep)
51
+ throw `Rep with _id: ${repzo_transfer.creator._id} not found in Repzo`;
52
+ }
53
+ // Get Repzo Products with its own Populated MeasureUnit
54
+ const repzo_product_ids = {};
55
+ (_f = repzo_transfer.variants) === null || _f === void 0
56
+ ? void 0
57
+ : _f.forEach((item) => {
58
+ if (item === null || item === void 0 ? void 0 : item.product_id) {
59
+ repzo_product_ids[item.product_id.toString()] = true;
60
+ }
61
+ });
62
+ const repzo_products = await get_data(
63
+ repzo.product,
64
+ "_id",
65
+ Object.keys(repzo_product_ids),
66
+ { populatedKeys: ["measureunit"] }
67
+ );
68
+ // Prepare Transfer Items
69
+ const variants = [];
70
+ for (
71
+ let i = 0;
72
+ i <
73
+ ((_g = repzo_transfer.variants) === null || _g === void 0
74
+ ? void 0
75
+ : _g.length);
76
+ i++
77
+ ) {
78
+ const repzo_transfer_item = repzo_transfer.variants[i];
79
+ const repzo_product = repzo_products.find(
80
+ //@ts-ignore
81
+ (p) => {
82
+ var _a;
83
+ return (
84
+ p._id.toString() ==
85
+ ((_a = repzo_transfer_item.product_id) === null || _a === void 0
86
+ ? void 0
87
+ : _a.toString())
88
+ );
89
+ }
90
+ );
91
+ if (!repzo_product) {
92
+ console.log(
93
+ `Product with _id: ${repzo_transfer_item.product_id} was not found In Repzo`
94
+ );
95
+ throw new Error(
96
+ `Product with _id: ${repzo_transfer_item.product_id} was not found in Repzo`
97
+ );
98
+ }
99
+ const repzo_measure_unit = repzo_product.sv_measureUnit;
100
+ if (
101
+ !(repzo_measure_unit === null || repzo_measure_unit === void 0
102
+ ? void 0
103
+ : repzo_measure_unit._id)
104
+ ) {
105
+ console.log(
106
+ `Measureunit with _id: ${
107
+ (_h = repzo_product.sv_measureUnit) === null || _h === void 0
108
+ ? void 0
109
+ : _h.toString()
110
+ } was not found`
111
+ );
112
+ throw new Error(
113
+ `Measureunit with _id: ${
114
+ (_j = repzo_product.sv_measureUnit) === null || _j === void 0
115
+ ? void 0
116
+ : _j.toString()
117
+ } was not found`
118
+ );
119
+ }
120
+ variants.push({
121
+ //@ts-ignore
122
+ ItemCode: repzo_transfer_item.variant_name,
123
+ Quantity: repzo_transfer_item.qty / Number(repzo_measure_unit.factor),
124
+ //@ts-ignore
125
+ FromWarehouse:
126
+ (_k = repzo_transfer.from) === null || _k === void 0
127
+ ? void 0
128
+ : _k.code,
129
+ //@ts-ignore
130
+ ToWarehouse:
131
+ (_l = repzo_transfer.to) === null || _l === void 0 ? void 0 : _l.code,
132
+ });
133
+ }
134
+ const sap_transfer = {
135
+ StockTransferID: body.serial_number.formatted,
136
+ SalesPersonCode: repzo_rep
137
+ ? repzo_rep.integration_id
138
+ : (_m = options.data) === null || _m === void 0
139
+ ? void 0
140
+ : _m.SalesPersonCode,
141
+ FromWarehouse: body.from.code,
142
+ ToWarehouse: body.to.code,
143
+ RepzoStockTransferLines: variants,
144
+ };
145
+ // console.dir(sap_transfer, { depth: null });
146
+ await actionLog
147
+ .addDetail(
148
+ `Repzo => SAP: Transfer - ${repzo_serial_number}`,
149
+ sap_transfer
150
+ )
151
+ .commit();
152
+ const result = await _create(
153
+ SAP_HOST_URL,
154
+ "/AddStockTransfer",
155
+ sap_transfer
156
+ );
157
+ // console.log(result);
158
+ await actionLog
159
+ .addDetail(`SAP Responded with `, result)
160
+ .addDetail(`Repzo => SAP: Transfer - ${repzo_serial_number}`)
161
+ .setStatus("success")
162
+ .setBody(body)
163
+ .commit();
164
+ return result;
165
+ } catch (e) {
166
+ //@ts-ignore
167
+ console.error((e === null || e === void 0 ? void 0 : e.response) || e);
168
+ await actionLog.setStatus("fail", e).setBody(body).commit();
169
+ throw e;
170
+ }
171
+ };
@@ -0,0 +1,3 @@
1
+ import { Config, Action } from "../types";
2
+ export declare const actions: (event: any, options: Config) => Promise<any>;
3
+ export declare const actionsList: Action[];