@developerx12/invoices-medusa 0.1.3 → 0.1.5
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/.medusa/server/src/admin/index.js +248 -43
- package/.medusa/server/src/admin/index.mjs +251 -46
- package/.medusa/server/src/api/admin/orders/[id]/credit-invoice/route.js +69 -0
- package/.medusa/server/src/api/admin/orders/[id]/invoice/[invoice_id]/upload/route.js +55 -0
- package/.medusa/server/src/api/admin/orders/[id]/invoice/route.js +43 -0
- package/.medusa/server/src/api/admin/orders/[id]/invoice/upload-new/route.js +119 -0
- package/.medusa/server/src/api/middlewares.js +22 -1
- package/.medusa/server/src/core/classes/pdf-generator/templates/credit-invoice-content.js +2 -2
- package/.medusa/server/src/core/classes/pdf-generator/templates/invoice-content.js +3 -3
- package/.medusa/server/src/subscribers/payment-refunded-invoice.js +14 -26
- package/.medusa/server/src/utils/order-invoices.js +65 -0
- package/.medusa/server/src/utils/store-invoice-file.js +37 -0
- package/.medusa/server/src/workflows/index.js +4 -2
- package/.medusa/server/src/workflows/steps/generate-invoice-pdf-step.js +2 -2
- package/.medusa/server/src/workflows/steps/replace-invoice-pdf-step.js +7 -12
- package/.medusa/server/src/workflows/steps/upload-invoice-pdf-step.js +4 -13
- package/.medusa/server/src/workflows/upload-invoice-pdf.js +14 -0
- package/README.md +47 -1
- package/package.json +2 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const jsxRuntime = require("react/jsx-runtime");
|
|
3
|
+
const react = require("react");
|
|
3
4
|
const ui = require("@medusajs/ui");
|
|
4
5
|
const adminSdk = require("@medusajs/admin-sdk");
|
|
5
6
|
const Medusa = require("@medusajs/js-sdk");
|
|
@@ -18,13 +19,15 @@ const sdk = new Medusa__default.default({
|
|
|
18
19
|
const OrderInvoiceWidget = ({ data: order }) => {
|
|
19
20
|
var _a;
|
|
20
21
|
const queryClient = reactQuery.useQueryClient();
|
|
22
|
+
const fileInputRef = react.useRef(null);
|
|
23
|
+
const uploadTargetRef = react.useRef(null);
|
|
21
24
|
const {
|
|
22
25
|
data: expandedOrder,
|
|
23
26
|
isLoading,
|
|
24
27
|
isError
|
|
25
28
|
} = reactQuery.useQuery({
|
|
26
29
|
queryFn: () => sdk.admin.order.retrieve(order.id, {
|
|
27
|
-
fields: "+invoices.*"
|
|
30
|
+
fields: "+invoices.*,+payment_collections.payments.refunds.*"
|
|
28
31
|
}),
|
|
29
32
|
queryKey: [order.id, "order_invoices"]
|
|
30
33
|
});
|
|
@@ -41,7 +44,11 @@ const OrderInvoiceWidget = ({ data: order }) => {
|
|
|
41
44
|
}
|
|
42
45
|
});
|
|
43
46
|
const downloadInvoice = reactQuery.useMutation({
|
|
44
|
-
mutationFn: async ({
|
|
47
|
+
mutationFn: async ({
|
|
48
|
+
invoiceId,
|
|
49
|
+
displayId,
|
|
50
|
+
type
|
|
51
|
+
}) => {
|
|
45
52
|
const response = await fetch(
|
|
46
53
|
`/admin/orders/${order.id}/invoice/${invoiceId}`,
|
|
47
54
|
{ credentials: "include" }
|
|
@@ -63,54 +70,252 @@ const OrderInvoiceWidget = ({ data: order }) => {
|
|
|
63
70
|
ui.toast.error("Failed to download invoice");
|
|
64
71
|
}
|
|
65
72
|
});
|
|
73
|
+
const uploadNewInvoice = reactQuery.useMutation({
|
|
74
|
+
mutationFn: async ({ file, type }) => {
|
|
75
|
+
const formData = new FormData();
|
|
76
|
+
formData.append("file", file);
|
|
77
|
+
formData.append("type", type);
|
|
78
|
+
const headers = {};
|
|
79
|
+
try {
|
|
80
|
+
const token = await sdk.client.getToken();
|
|
81
|
+
if (token) {
|
|
82
|
+
headers.Authorization = `Bearer ${token}`;
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
}
|
|
86
|
+
const response = await fetch(
|
|
87
|
+
`/admin/orders/${order.id}/invoice/upload-new`,
|
|
88
|
+
{
|
|
89
|
+
method: "POST",
|
|
90
|
+
body: formData,
|
|
91
|
+
credentials: "include",
|
|
92
|
+
headers
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
let message = "Failed to upload invoice";
|
|
97
|
+
try {
|
|
98
|
+
const body = await response.json();
|
|
99
|
+
message = (body == null ? void 0 : body.message) ?? message;
|
|
100
|
+
} catch {
|
|
101
|
+
}
|
|
102
|
+
throw new Error(message);
|
|
103
|
+
}
|
|
104
|
+
return response.json();
|
|
105
|
+
},
|
|
106
|
+
onSuccess: () => {
|
|
107
|
+
ui.toast.success("Invoice uploaded successfully");
|
|
108
|
+
queryClient.invalidateQueries({ queryKey: [order.id, "order_invoices"] });
|
|
109
|
+
},
|
|
110
|
+
onError: (error) => {
|
|
111
|
+
ui.toast.error((error == null ? void 0 : error.message) ?? "Failed to upload invoice");
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const uploadInvoice = reactQuery.useMutation({
|
|
115
|
+
mutationFn: async ({
|
|
116
|
+
invoiceId,
|
|
117
|
+
file
|
|
118
|
+
}) => {
|
|
119
|
+
const formData = new FormData();
|
|
120
|
+
formData.append("file", file);
|
|
121
|
+
const headers = {};
|
|
122
|
+
try {
|
|
123
|
+
const token = await sdk.client.getToken();
|
|
124
|
+
if (token) {
|
|
125
|
+
headers.Authorization = `Bearer ${token}`;
|
|
126
|
+
}
|
|
127
|
+
} catch {
|
|
128
|
+
}
|
|
129
|
+
const response = await fetch(
|
|
130
|
+
`/admin/orders/${order.id}/invoice/${invoiceId}/upload`,
|
|
131
|
+
{
|
|
132
|
+
method: "POST",
|
|
133
|
+
body: formData,
|
|
134
|
+
credentials: "include",
|
|
135
|
+
headers
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
if (!response.ok) {
|
|
139
|
+
let message = "Failed to upload invoice PDF";
|
|
140
|
+
try {
|
|
141
|
+
const body = await response.json();
|
|
142
|
+
message = (body == null ? void 0 : body.message) ?? message;
|
|
143
|
+
} catch {
|
|
144
|
+
}
|
|
145
|
+
throw new Error(message);
|
|
146
|
+
}
|
|
147
|
+
return response.json();
|
|
148
|
+
},
|
|
149
|
+
onSuccess: () => {
|
|
150
|
+
ui.toast.success("Invoice PDF uploaded");
|
|
151
|
+
queryClient.invalidateQueries({ queryKey: [order.id, "order_invoices"] });
|
|
152
|
+
},
|
|
153
|
+
onError: (error) => {
|
|
154
|
+
ui.toast.error((error == null ? void 0 : error.message) ?? "Failed to upload invoice PDF");
|
|
155
|
+
}
|
|
156
|
+
});
|
|
66
157
|
const orderWithInvoices = expandedOrder == null ? void 0 : expandedOrder.order;
|
|
67
|
-
const orderHasInvoices = ((_a = orderWithInvoices == null ? void 0 : orderWithInvoices.invoices) == null ? void 0 : _a.length) > 0;
|
|
158
|
+
const orderHasInvoices = (((_a = orderWithInvoices == null ? void 0 : orderWithInvoices.invoices) == null ? void 0 : _a.length) ?? 0) > 0;
|
|
68
159
|
const invoices = orderWithInvoices == null ? void 0 : orderWithInvoices.invoices;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
"
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
160
|
+
const hasDebitInvoice = (invoices == null ? void 0 : invoices.some((invoice) => invoice.type === "debit")) ?? false;
|
|
161
|
+
const refunds = ((orderWithInvoices == null ? void 0 : orderWithInvoices.payment_collections) ?? []).flatMap((pc) => pc.payments ?? []).flatMap((payment) => payment.refunds ?? []);
|
|
162
|
+
const formatAmount = (amount) => {
|
|
163
|
+
try {
|
|
164
|
+
const formatter = new Intl.NumberFormat(void 0, {
|
|
165
|
+
style: "currency",
|
|
166
|
+
currency: order.currency_code
|
|
167
|
+
});
|
|
168
|
+
const fractionDigits = formatter.resolvedOptions().maximumFractionDigits ?? 2;
|
|
169
|
+
return formatter.format(amount / Math.pow(10, fractionDigits));
|
|
170
|
+
} catch {
|
|
171
|
+
return (amount / 100).toFixed(2);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
const openFilePicker = (invoiceId, type) => {
|
|
175
|
+
uploadTargetRef.current = { invoiceId, type };
|
|
176
|
+
if (fileInputRef.current) {
|
|
177
|
+
fileInputRef.current.value = "";
|
|
178
|
+
fileInputRef.current.click();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
const handleFileSelected = (event) => {
|
|
182
|
+
var _a2;
|
|
183
|
+
const file = (_a2 = event.target.files) == null ? void 0 : _a2[0];
|
|
184
|
+
const target = uploadTargetRef.current;
|
|
185
|
+
if (!file || !target) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (file.type && file.type !== "application/pdf") {
|
|
189
|
+
ui.toast.error("Only PDF files are supported");
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (target.invoiceId) {
|
|
193
|
+
uploadInvoice.mutate({ invoiceId: target.invoiceId, file });
|
|
194
|
+
} else if (target.type) {
|
|
195
|
+
uploadNewInvoice.mutate({ file, type: target.type });
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
199
|
+
/* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "divide-y p-0", children: [
|
|
200
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between gap-2 px-6 py-4", children: [
|
|
201
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h1", children: "Invoices" }) }),
|
|
202
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
|
|
203
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
204
|
+
ui.Button,
|
|
205
|
+
{
|
|
206
|
+
size: "small",
|
|
207
|
+
variant: "secondary",
|
|
208
|
+
isLoading: uploadNewInvoice.isPending,
|
|
209
|
+
disabled: hasDebitInvoice,
|
|
210
|
+
onClick: () => openFilePicker(void 0, "debit"),
|
|
211
|
+
children: [
|
|
212
|
+
/* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpTray, {}),
|
|
213
|
+
"Upload invoice"
|
|
214
|
+
]
|
|
215
|
+
}
|
|
216
|
+
),
|
|
217
|
+
/* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
|
|
218
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
219
|
+
ui.Button,
|
|
80
220
|
{
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
invoiceId: invoice.id,
|
|
86
|
-
displayId: invoice.display_id,
|
|
87
|
-
type: invoice.type
|
|
88
|
-
}),
|
|
221
|
+
size: "small",
|
|
222
|
+
variant: "secondary",
|
|
223
|
+
isLoading: uploadNewInvoice.isPending,
|
|
224
|
+
disabled: !refunds.length,
|
|
89
225
|
children: [
|
|
90
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.
|
|
91
|
-
invoice
|
|
92
|
-
".pdf",
|
|
93
|
-
/* @__PURE__ */ jsxRuntime.jsx("span", { className: `txt-xsmall px-1.5 py-0.5 rounded-base ${invoice.type === "credit" ? "bg-ui-green-10 text-ui-green-700" : "bg-ui-bg-subtle text-ui-fg-muted"}`, children: invoice.type })
|
|
226
|
+
/* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpTray, {}),
|
|
227
|
+
"Upload credit invoice"
|
|
94
228
|
]
|
|
95
229
|
}
|
|
96
|
-
),
|
|
97
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
98
|
-
ui.
|
|
230
|
+
) }),
|
|
231
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Content, { align: "end", children: refunds.map((refund) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
232
|
+
ui.DropdownMenu.Item,
|
|
99
233
|
{
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
)
|
|
108
|
-
]
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
)
|
|
112
|
-
|
|
113
|
-
|
|
234
|
+
onClick: () => openFilePicker(void 0, "credit"),
|
|
235
|
+
children: [
|
|
236
|
+
formatAmount(refund.amount),
|
|
237
|
+
refund.note ? ` - ${refund.note}` : ""
|
|
238
|
+
]
|
|
239
|
+
},
|
|
240
|
+
refund.id
|
|
241
|
+
)) })
|
|
242
|
+
] })
|
|
243
|
+
] })
|
|
244
|
+
] }),
|
|
245
|
+
hasDebitInvoice || !refunds.length ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-muted", children: hasDebitInvoice ? "This order already has an invoice. Upload a PDF to replace its document." : "A credit invoice is created from a refund. This order has no refunds yet." }) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-muted", children: "Select a refund amount to upload a credit invoice." }) }),
|
|
246
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-ui-fg-subtle px-6 py-4", children: isLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-muted", children: "Loading..." }) : isError ? /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-error", children: "Failed to load invoices" }) : orderHasInvoices ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-3", children: invoices == null ? void 0 : invoices.map((invoice) => {
|
|
247
|
+
var _a2, _b;
|
|
248
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
249
|
+
"div",
|
|
250
|
+
{
|
|
251
|
+
className: "flex items-center justify-between",
|
|
252
|
+
children: [
|
|
253
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
254
|
+
"button",
|
|
255
|
+
{
|
|
256
|
+
type: "button",
|
|
257
|
+
className: "font-normal font-sans txt-compact-small text-ui-fg-subtle flex items-center gap-2 hover:text-ui-fg-base cursor-pointer bg-transparent border-none p-0",
|
|
258
|
+
disabled: downloadInvoice.isPending && ((_a2 = downloadInvoice.variables) == null ? void 0 : _a2.invoiceId) === invoice.id,
|
|
259
|
+
onClick: () => downloadInvoice.mutate({
|
|
260
|
+
invoiceId: invoice.id,
|
|
261
|
+
displayId: invoice.display_id,
|
|
262
|
+
type: invoice.type
|
|
263
|
+
}),
|
|
264
|
+
children: [
|
|
265
|
+
/* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownTray, {}),
|
|
266
|
+
invoice.display_id,
|
|
267
|
+
".pdf",
|
|
268
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
269
|
+
"span",
|
|
270
|
+
{
|
|
271
|
+
className: `txt-xsmall px-1.5 py-0.5 rounded-base ${invoice.type === "credit" ? "bg-ui-green-10 text-ui-green-700" : "bg-ui-bg-subtle text-ui-fg-muted"}`,
|
|
272
|
+
children: invoice.type
|
|
273
|
+
}
|
|
274
|
+
)
|
|
275
|
+
]
|
|
276
|
+
}
|
|
277
|
+
),
|
|
278
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1", children: [
|
|
279
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Tooltip, { content: "Upload PDF", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
280
|
+
ui.IconButton,
|
|
281
|
+
{
|
|
282
|
+
size: "small",
|
|
283
|
+
variant: "transparent",
|
|
284
|
+
"aria-label": "Upload invoice PDF",
|
|
285
|
+
disabled: uploadInvoice.isPending && ((_b = uploadInvoice.variables) == null ? void 0 : _b.invoiceId) === invoice.id,
|
|
286
|
+
onClick: () => openFilePicker(invoice.id),
|
|
287
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpTray, {})
|
|
288
|
+
}
|
|
289
|
+
) }),
|
|
290
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Tooltip, { content: "Regenerate invoice PDF", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
291
|
+
ui.IconButton,
|
|
292
|
+
{
|
|
293
|
+
size: "small",
|
|
294
|
+
variant: "transparent",
|
|
295
|
+
"aria-label": "Regenerate invoice PDF",
|
|
296
|
+
disabled: refreshInvoice.isPending && refreshInvoice.variables === invoice.id,
|
|
297
|
+
onClick: () => refreshInvoice.mutate(invoice.id),
|
|
298
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowPath, {})
|
|
299
|
+
}
|
|
300
|
+
) })
|
|
301
|
+
] })
|
|
302
|
+
]
|
|
303
|
+
},
|
|
304
|
+
invoice.id
|
|
305
|
+
);
|
|
306
|
+
}) }) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-muted", children: "No invoices found" }) })
|
|
307
|
+
] }),
|
|
308
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
309
|
+
"input",
|
|
310
|
+
{
|
|
311
|
+
ref: fileInputRef,
|
|
312
|
+
type: "file",
|
|
313
|
+
accept: "application/pdf,.pdf",
|
|
314
|
+
className: "hidden",
|
|
315
|
+
onChange: handleFileSelected
|
|
316
|
+
}
|
|
317
|
+
)
|
|
318
|
+
] });
|
|
114
319
|
};
|
|
115
320
|
adminSdk.defineWidgetConfig({
|
|
116
321
|
zone: "order.details.side.before"
|