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,217 @@
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
+ Return_Reason?: string | number;
19
+ }
20
+
21
+ interface SAPInvoice {
22
+ RefNum: string; // "INV-1021-4",
23
+ SalPersCode?: string; // "106", // Required
24
+ DocDate: string; // "20211229",
25
+ DocDueDate: string; // "20211229",
26
+ ClientCode?: string; // "C00041", // Required
27
+ DiscountPerc: string; // "0",
28
+ Note?: string; // "",
29
+ WarehouseCode?: string; // "VS21"; // Required
30
+ LinesDetails: SAPInvoiceItem[];
31
+ }
32
+
33
+ interface ReturnReason {
34
+ sap_id: number; // 1,
35
+ sap_name: string; // "Barcode Issue",
36
+ repzo_name: string; // "Barcode issue",
37
+ repzo_id: string; //"62b074972b332895edf114fc"
38
+ }
39
+
40
+ export const create_return_invoice = async (event: EVENT, options: Config) => {
41
+ const repzo = new Repzo(options.data?.repzoApiKey, { env: options.env });
42
+ const action_sync_id: string = event?.headers?.action_sync_id || uuid();
43
+ const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
44
+ let body: Service.FullInvoice.InvoiceSchema | any;
45
+ try {
46
+ // console.log("create_return_invoice");
47
+ await actionLog.load(action_sync_id);
48
+
49
+ body = event.body;
50
+ try {
51
+ if (body) body = JSON.parse(body);
52
+ } catch (e) {}
53
+
54
+ const repzo_serial_number = body?.serial_number?.formatted;
55
+
56
+ await actionLog
57
+ .addDetail(
58
+ `Repzo => SAP: Started Create Return Invoice - ${repzo_serial_number}`
59
+ )
60
+ .commit();
61
+
62
+ const SAP_HOST_URL = options.data?.sapHostUrl;
63
+ if (!SAP_HOST_URL)
64
+ throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
65
+
66
+ const repzo_invoice: Service.FullInvoice.InvoiceSchema = body;
67
+
68
+ // Get Return Reason
69
+ let return_reasons: ReturnReason[] = options.data?.return_reasons;
70
+ try {
71
+ if (typeof return_reasons == "string")
72
+ return_reasons = JSON.parse(return_reasons);
73
+ } catch (e) {}
74
+
75
+ // Get Repzo Rep
76
+ let repzo_rep;
77
+ if (repzo_invoice.creator.type == "rep") {
78
+ repzo_rep = await repzo.rep.get(repzo_invoice?.creator?._id);
79
+ if (!repzo_rep)
80
+ throw `Rep with _id: ${repzo_invoice.creator._id} not found in Repzo`;
81
+ }
82
+
83
+ // Get Repzo Client
84
+ const repzo_client = await repzo.client.get(repzo_invoice?.client_id);
85
+ if (!repzo_client)
86
+ throw `Client with _id: ${repzo_invoice.client_id} not found in Repzo`;
87
+
88
+ // Get Repzo Warehouse
89
+ const repzo_warehouse = await repzo.warehouse.get(
90
+ repzo_invoice.origin_warehouse
91
+ );
92
+ if (!repzo_warehouse)
93
+ throw `warehouse with _id: ${repzo_invoice.origin_warehouse} not found in Repzo`;
94
+
95
+ const repzo_tax_ids: { [id: string]: true } = {};
96
+ const repzo_measureunit_ids: { [id: string]: true } = {};
97
+ const repzo_product_ids: { [id: string]: true } = {};
98
+
99
+ repzo_invoice.return_items?.forEach((item: Service.Item.Schema) => {
100
+ if (item) {
101
+ repzo_tax_ids[item.tax?._id] = true;
102
+ repzo_measureunit_ids[item.measureunit?._id] = true;
103
+ repzo_product_ids[item.variant?.product_id] = true;
104
+ }
105
+ });
106
+
107
+ const repzo_taxes = await get_data(
108
+ repzo.tax,
109
+ "_id",
110
+ Object.keys(repzo_tax_ids)
111
+ );
112
+ const repzo_measureunits = await get_data(
113
+ repzo.measureunit,
114
+ "_id",
115
+ Object.keys(repzo_measureunit_ids)
116
+ );
117
+ const repzo_products = await get_data(
118
+ repzo.product,
119
+ "_id",
120
+ Object.keys(repzo_product_ids)
121
+ );
122
+
123
+ // Prepare SAP_invoice_items
124
+ const return_items = [];
125
+
126
+ for (let i = 0; i < repzo_invoice?.return_items?.length; i++) {
127
+ const item = repzo_invoice.return_items[i];
128
+
129
+ // Get Repzo Tax
130
+ const repzo_tax = repzo_taxes?.find(
131
+ (t) => t._id?.toString() == item.tax?._id?.toString()
132
+ );
133
+ if (!repzo_tax) throw `Tax with _id: ${item.tax._id} not found in Repzo`;
134
+
135
+ // Get Repzo UoM
136
+ const repzo_measureunit = repzo_measureunits?.find(
137
+ (m) => m._id?.toString() == item.measureunit?._id?.toString()
138
+ );
139
+ if (!repzo_measureunit)
140
+ throw `Uom with _id: ${item.measureunit?._id} not found in Repzo`;
141
+
142
+ // Get Repzo Product
143
+ const repzo_product = repzo_products?.find(
144
+ (p) => p._id?.toString() == item.variant?.product_id?.toString()
145
+ );
146
+ if (!repzo_product)
147
+ throw `Product with _id: ${item.measureunit._id} not found in Repzo`;
148
+
149
+ // Get SAP return_reason
150
+ const item_return_reason = return_reasons?.find(
151
+ (r) => r.repzo_id === item.return_reason
152
+ );
153
+
154
+ return_items.push({
155
+ ItemCode: item.variant.variant_name,
156
+ Quantity: -1 * item.qty,
157
+ TaxCode: repzo_tax.integration_meta.TaxCode,
158
+ UnitPrice: (item.price * repzo_measureunit.factor) / 1000,
159
+ DiscountPerc: "0",
160
+ //@ts-ignore
161
+ LineTotal: (-1 * item.total_before_tax) / 1000,
162
+ UomCode: repzo_measureunit.integration_meta.ALTUOMID,
163
+ Brand: repzo_product.integration_meta?.BRAND, // "B1",
164
+ Department:
165
+ repzo_rep?.integration_meta?.DEPARTMENTCODE ||
166
+ options.data?.DepartmentCode, // "D2"
167
+ Return_Reason: item_return_reason?.sap_id,
168
+ });
169
+ }
170
+
171
+ const sap_return_invoice: SAPInvoice = {
172
+ RefNum: repzo_invoice.serial_number.formatted,
173
+ SalPersCode: repzo_rep?.integration_id,
174
+ DocDate: moment(repzo_invoice.issue_date, "YYYY-MM-DD").format(
175
+ "YYYYMMDD"
176
+ ),
177
+ DocDueDate: moment(repzo_invoice.due_date, "YYYY-MM-DD").format(
178
+ "YYYYMMDD"
179
+ ),
180
+ ClientCode: repzo_client.client_code,
181
+ DiscountPerc: "0",
182
+ Note: repzo_invoice.comment,
183
+ WarehouseCode: repzo_warehouse.code,
184
+ LinesDetails: return_items,
185
+ };
186
+
187
+ // console.dir(sap_return_invoice, { depth: null });
188
+
189
+ await actionLog
190
+ .addDetail(
191
+ `Repzo => SAP: Invoice - ${repzo_serial_number}`,
192
+ sap_return_invoice
193
+ )
194
+ .commit();
195
+
196
+ const result = await _create(
197
+ SAP_HOST_URL,
198
+ "/AddCreditMemo",
199
+ sap_return_invoice
200
+ );
201
+
202
+ // console.log(result);
203
+
204
+ await actionLog
205
+ .addDetail(`SAP Responded with `, result)
206
+ .addDetail(`Repzo => SAP: Return Invoice - ${repzo_serial_number}`)
207
+ .setStatus("success")
208
+ .setBody(repzo_invoice)
209
+ .commit();
210
+ return result;
211
+ } catch (e: any) {
212
+ //@ts-ignore
213
+ console.error(e?.response || e);
214
+ await actionLog.setStatus("fail", e).setBody(body).commit();
215
+ throw e;
216
+ }
217
+ };
@@ -0,0 +1,160 @@
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 SAPTransferItem {
9
+ ItemCode: string; // "010-RAI-SC0002";
10
+ Quantity: string | number; // "1";
11
+ FromWarehouse: string | number; // "1";
12
+ ToWarehouse: string | number; // "12";
13
+ }
14
+
15
+ interface SAPTransfer {
16
+ StockTransferID: string; // "TestStockTransfer1";
17
+ FromWarehouse: string | number; // "1";
18
+ ToWarehouse: string | number; // "12";
19
+ SalesPersonCode: string | number; // "110";
20
+ RepzoStockTransferLines: SAPTransferItem[];
21
+ }
22
+
23
+ export const create_transfer = async (event: EVENT, options: Config) => {
24
+ const repzo = new Repzo(options.data?.repzoApiKey, { env: options.env });
25
+ const action_sync_id: string = event?.headers?.action_sync_id || uuid();
26
+ const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
27
+ let body: Service.Transfer.Schema | any;
28
+ try {
29
+ // console.log("create_transfer");
30
+ await actionLog.load(action_sync_id);
31
+
32
+ body = event.body;
33
+ try {
34
+ if (body) body = JSON.parse(body);
35
+ } catch (e) {}
36
+
37
+ const repzo_serial_number = body?.serial_number?.formatted;
38
+
39
+ await actionLog
40
+ .addDetail(
41
+ `Repzo => SAP: Started Create Transfer - ${repzo_serial_number}`
42
+ )
43
+ .commit();
44
+
45
+ const SAP_HOST_URL = options.data?.sapHostUrl;
46
+ if (!SAP_HOST_URL)
47
+ throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
48
+
49
+ const repzo_transfer: Service.Transfer.Schema = body;
50
+
51
+ // Get Repzo Rep
52
+ let repzo_rep;
53
+ if (repzo_transfer.creator.type == "rep") {
54
+ repzo_rep = await repzo.rep.get(repzo_transfer?.creator?._id);
55
+ if (!repzo_rep)
56
+ throw `Rep with _id: ${repzo_transfer.creator._id} not found in Repzo`;
57
+ }
58
+
59
+ // Get Repzo Products with its own Populated MeasureUnit
60
+
61
+ const repzo_product_ids: { [id: string]: true } = {};
62
+ repzo_transfer.variants?.forEach(
63
+ (item: Service.Transfer.VariantTransfer) => {
64
+ if (item?.product_id) {
65
+ repzo_product_ids[item.product_id.toString()] = true;
66
+ }
67
+ }
68
+ );
69
+ const repzo_products = await get_data(
70
+ repzo.product,
71
+ "_id",
72
+ Object.keys(repzo_product_ids),
73
+ { populatedKeys: ["measureunit"] }
74
+ );
75
+
76
+ // Prepare Transfer Items
77
+ const variants: SAPTransferItem[] = [];
78
+
79
+ for (let i = 0; i < repzo_transfer.variants?.length; i++) {
80
+ const repzo_transfer_item = repzo_transfer.variants[i];
81
+
82
+ const repzo_product = repzo_products.find(
83
+ //@ts-ignore
84
+ (p) => p._id.toString() == repzo_transfer_item.product_id?.toString()
85
+ );
86
+ if (!repzo_product) {
87
+ console.log(
88
+ `Product with _id: ${repzo_transfer_item.product_id} was not found In Repzo`
89
+ );
90
+ throw new Error(
91
+ `Product with _id: ${repzo_transfer_item.product_id} was not found in Repzo`
92
+ );
93
+ }
94
+
95
+ const repzo_measure_unit = repzo_product.sv_measureUnit;
96
+
97
+ if (!repzo_measure_unit?._id) {
98
+ console.log(
99
+ `Measureunit with _id: ${repzo_product.sv_measureUnit?.toString()} was not found`
100
+ );
101
+ throw new Error(
102
+ `Measureunit with _id: ${repzo_product.sv_measureUnit?.toString()} was not found`
103
+ );
104
+ }
105
+
106
+ variants.push({
107
+ //@ts-ignore
108
+ ItemCode: repzo_transfer_item.variant_name,
109
+ Quantity: repzo_transfer_item.qty / Number(repzo_measure_unit.factor),
110
+ //@ts-ignore
111
+ FromWarehouse: (
112
+ repzo_transfer.from as Service.Warehouse.WarehouseSchema
113
+ )?.code,
114
+ //@ts-ignore
115
+ ToWarehouse: (repzo_transfer.to as Service.Warehouse.WarehouseSchema)
116
+ ?.code,
117
+ });
118
+ }
119
+
120
+ const sap_transfer: SAPTransfer = {
121
+ StockTransferID: body.serial_number.formatted,
122
+ SalesPersonCode: repzo_rep
123
+ ? repzo_rep.integration_id
124
+ : options.data?.SalesPersonCode, // "111"
125
+ FromWarehouse: body.from.code,
126
+ ToWarehouse: body.to.code,
127
+ RepzoStockTransferLines: variants,
128
+ };
129
+
130
+ // console.dir(sap_transfer, { depth: null });
131
+
132
+ await actionLog
133
+ .addDetail(
134
+ `Repzo => SAP: Transfer - ${repzo_serial_number}`,
135
+ sap_transfer
136
+ )
137
+ .commit();
138
+
139
+ const result = await _create(
140
+ SAP_HOST_URL,
141
+ "/AddStockTransfer",
142
+ sap_transfer
143
+ );
144
+
145
+ // console.log(result);
146
+
147
+ await actionLog
148
+ .addDetail(`SAP Responded with `, result)
149
+ .addDetail(`Repzo => SAP: Transfer - ${repzo_serial_number}`)
150
+ .setStatus("success")
151
+ .setBody(body)
152
+ .commit();
153
+ return result;
154
+ } catch (e: any) {
155
+ //@ts-ignore
156
+ console.error(e?.response || e);
157
+ await actionLog.setStatus("fail", e).setBody(body).commit();
158
+ throw e;
159
+ }
160
+ };
@@ -0,0 +1,27 @@
1
+ import { Config, Action } from "../types";
2
+ import { EVENT } from "../types";
3
+
4
+ import { create_invoice } from "./create_invoice.js";
5
+ import { create_return_invoice } from "./create_return_invoice.js";
6
+ import { create_proforma } from "./create_proforma.js";
7
+ import { create_payment } from "./create_payment.js";
8
+ import { create_transfer } from "./create_transfer.js";
9
+
10
+ export const actions = async (event: any, options: Config) => {
11
+ switch (event.queryStringParameters?.action) {
12
+ case "create_invoice":
13
+ return await create_invoice(event, options);
14
+ case "create_return_invoice":
15
+ return await create_return_invoice(event, options);
16
+ case "create_proforma":
17
+ return await create_proforma(event, options);
18
+ case "create_payment":
19
+ return await create_payment(event, options);
20
+ case "create_transfer":
21
+ return await create_transfer(event, options);
22
+ default:
23
+ throw `Route: ${event.queryStringParameters?.action} not found`;
24
+ }
25
+ };
26
+
27
+ export const actionsList: Action[] = [];
@@ -0,0 +1,250 @@
1
+ import Repzo from "repzo";
2
+ import { Service } from "repzo/src/types";
3
+ import DataSet from "data-set-query";
4
+ import { v4 as uuid } from "uuid";
5
+ import { CommandEvent, Result, FailedDocsReport } from "../types";
6
+ import {
7
+ _fetch,
8
+ _create,
9
+ _update,
10
+ _delete,
11
+ update_bench_time,
12
+ date_formatting,
13
+ set_error,
14
+ } from "../util.js";
15
+
16
+ interface SAPStoresBalance {
17
+ ITEMID: string; // "020-REY-DI0002",
18
+ STOREID: string; // "COOPS10",
19
+ UNITID: number; // 3,
20
+ UNITNAME: string; // "CRTN",
21
+ QTY: number; // 102.0
22
+ }
23
+
24
+ interface SAPStoresBalances {
25
+ result: "Success";
26
+ StoresBalance: SAPStoresBalance[];
27
+ }
28
+
29
+ export const adjust_inventory = async (commandEvent: CommandEvent) => {
30
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
31
+ env: commandEvent.env,
32
+ });
33
+
34
+ const commandLog = new Repzo.CommandLog(
35
+ repzo,
36
+ commandEvent.app,
37
+ commandEvent.command
38
+ );
39
+ try {
40
+ console.log("adjust_inventory");
41
+
42
+ await commandLog.load(commandEvent.sync_id);
43
+ await commandLog
44
+ .addDetail("Repzo SAP: Started Adjusting Inventories")
45
+ .commit();
46
+
47
+ const nameSpace: string = commandEvent.nameSpace.join("_");
48
+ const result: Result & { items_failed: number } = {
49
+ sap_total: 0,
50
+ repzo_total: 0,
51
+ items_failed: 0,
52
+ created: 0,
53
+ updated: 0,
54
+ failed: 0,
55
+ };
56
+ const failed_docs_report: FailedDocsReport = [];
57
+
58
+ const sap_inventories_items: SAPStoresBalance[] = await get_sap_inventories(
59
+ commandEvent.app.formData.sapHostUrl,
60
+ {}
61
+ );
62
+ result.sap_total = sap_inventories_items?.length;
63
+
64
+ await commandLog
65
+ .addDetail(`${result.sap_total} Stores Balance in SAP`)
66
+ .commit();
67
+
68
+ // Get Repzo Variants
69
+ const repzo_variants = await repzo.variant.find({
70
+ per_page: 50000,
71
+ disabled: false,
72
+ withProduct: true,
73
+ });
74
+ await commandLog
75
+ .addDetail(`${repzo_variants?.data?.length} Variant(s) in Repzo`)
76
+ .commit();
77
+
78
+ // Get Repzo Warehouses
79
+ const repzo_warehouses = await repzo.warehouse.find({
80
+ per_page: 50000,
81
+ disabled: false,
82
+ });
83
+ await commandLog
84
+ .addDetail(`${repzo_warehouses?.data?.length} Warehouse(s) in Repzo`)
85
+ .commit();
86
+
87
+ // Get Repzo Measure Units
88
+ const repzo_measureunits = await repzo.measureunit.find({
89
+ per_page: 50000,
90
+ disabled: false,
91
+ });
92
+ await commandLog
93
+ .addDetail(`${repzo_measureunits?.data?.length} Measure Unit(s) in Repzo`)
94
+ .commit();
95
+
96
+ const repzo_tags = await repzo.tag.find({ type: "area", per_page: 50000 });
97
+ result.repzo_total = repzo_tags?.data?.length;
98
+ await commandLog
99
+ .addDetail(`${repzo_tags?.data?.length} Area Tags in Repzo`)
100
+ .commit();
101
+
102
+ const sap_stores: {
103
+ [sap_store_id: string]: { STOREID: string; items: SAPStoresBalance[] };
104
+ } = {};
105
+
106
+ sap_inventories_items.forEach((item) => {
107
+ if (!sap_stores[item.STOREID])
108
+ sap_stores[item.STOREID] = { STOREID: item.STOREID, items: [] };
109
+ sap_stores[item.STOREID].items.push(item);
110
+ });
111
+
112
+ const sap_inventories = Object.values(sap_stores);
113
+
114
+ for (let i = 0; i < sap_inventories.length; i++) {
115
+ try {
116
+ const sap_inventory = sap_inventories[i];
117
+ const repzo_warehouse = repzo_warehouses?.data?.find(
118
+ (wh) => wh.code == sap_inventory.STOREID
119
+ );
120
+ if (!repzo_warehouse) {
121
+ throw `Warehouse with code: ${sap_inventory.STOREID} was not found in Repzo`;
122
+ }
123
+
124
+ // Get Repzo Inventory
125
+ const repzo_inventory = await repzo.inventory.find({
126
+ warehouse_id: repzo_warehouse._id,
127
+ per_page: 50000,
128
+ });
129
+
130
+ const body: Service.AdjustInventory.Create.Body = {
131
+ time: Date.now(),
132
+ sync_id: uuid(),
133
+ to: repzo_warehouse._id,
134
+ //@ts-ignore
135
+ variants: sap_inventory.items
136
+ .map((sap_item) => {
137
+ try {
138
+ const variant = repzo_variants?.data?.find(
139
+ (variant) =>
140
+ variant.integration_meta?.ITEMCODE == sap_item.ITEMID
141
+ );
142
+ if (!variant) {
143
+ console.log(
144
+ `Variant with ITEMCODE: ${sap_item.ITEMID} was not found`
145
+ );
146
+ throw `Variant with ITEMCODE: ${sap_item.ITEMID} was not found`;
147
+ }
148
+
149
+ const measureUnit = repzo_measureunits?.data?.find(
150
+ (measure_unit) =>
151
+ measure_unit._id.toString() ==
152
+ (
153
+ variant.product as Service.Product.ProductSchema
154
+ )?.sv_measureUnit?.toString()
155
+ );
156
+ if (!measureUnit) {
157
+ console.log(
158
+ `MeasureUnit with UNITNAME: ${sap_item.UNITNAME} & ALTUOMID: ${sap_item.UNITID} was not found`
159
+ );
160
+ throw `MeasureUnit with UNITNAME: ${sap_item.UNITNAME} & ALTUOMID: ${sap_item.UNITID} was not found`;
161
+ }
162
+
163
+ const qty = measureUnit.factor * sap_item.QTY;
164
+
165
+ const match_item_in_repzo_inventory =
166
+ repzo_inventory?.data?.find(
167
+ (repzo_item) =>
168
+ repzo_item.variant_id.toString() == variant._id.toString()
169
+ );
170
+
171
+ if (match_item_in_repzo_inventory) {
172
+ //@ts-ignore
173
+ match_item_in_repzo_inventory.has_match_in_SAP = true;
174
+ }
175
+
176
+ const diff_qty = match_item_in_repzo_inventory
177
+ ? qty - match_item_in_repzo_inventory.qty
178
+ : qty;
179
+
180
+ return { variant: variant._id, qty: diff_qty };
181
+ } catch (e) {
182
+ console.log(e);
183
+ failed_docs_report.push({
184
+ method: "fetchingData",
185
+ doc_id: sap_item.UNITNAME,
186
+ doc: { ...sap_item, STOREID: sap_inventories[i]?.STOREID },
187
+ error_message: set_error(e),
188
+ });
189
+ result.items_failed++;
190
+ }
191
+ })
192
+ .concat(
193
+ ...repzo_inventory?.data
194
+ //@ts-ignore
195
+ ?.filter((item) => !item.has_match_in_SAP)
196
+ .map((item) => {
197
+ return { variant: item.variant_id, qty: -1 * item.qty };
198
+ })
199
+ )
200
+ .filter((item) => item && item.qty),
201
+ };
202
+ // console.log(body);
203
+ if (!body.variants.length) continue;
204
+
205
+ const res = await repzo.adjustInventory.create(body);
206
+ result.created++;
207
+ } catch (e) {
208
+ // console.log(e);
209
+ failed_docs_report.push({
210
+ method: "fetchingData",
211
+ doc_id: sap_inventories[i]?.STOREID,
212
+ doc: { STOREID: sap_inventories[i]?.STOREID },
213
+ error_message: set_error(e),
214
+ });
215
+ result.failed++;
216
+ }
217
+ }
218
+
219
+ // console.log(result);
220
+ await commandLog
221
+ .setStatus(
222
+ "success",
223
+ failed_docs_report.length ? failed_docs_report : null
224
+ )
225
+ .setBody(result)
226
+ .commit();
227
+ return result;
228
+ } catch (e: any) {
229
+ //@ts-ignore
230
+ console.error(e?.response?.data || e);
231
+ await commandLog.setStatus("fail", e).commit();
232
+ throw e;
233
+ }
234
+ };
235
+
236
+ const get_sap_inventories = async (
237
+ serviceEndPoint: string,
238
+ query?: { updateAt?: string }
239
+ ): Promise<SAPStoresBalance[]> => {
240
+ try {
241
+ const sap_inventories: SAPStoresBalances = await _create(
242
+ serviceEndPoint,
243
+ "/StoresBalance",
244
+ {}
245
+ );
246
+ return sap_inventories.StoresBalance;
247
+ } catch (e: any) {
248
+ throw e;
249
+ }
250
+ };