@managesome/eposmatic-toolkit 0.4.89 → 0.4.92
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/esm/constants/ENUMS.js +1 -0
- package/esm/constants/enums/storage.js +23 -0
- package/esm/helpers/index.js +1 -0
- package/esm/helpers/order/generate-warehouse-internal-movement.js +3 -0
- package/esm/helpers/storage/index.js +1 -0
- package/esm/helpers/storage/upload-paths.js +384 -0
- package/esm/helpers/store/common-warehouse-internal-movement-actions.js +124 -4
- package/esm/helpers/utils/discount-amounts.js +64 -0
- package/esm/helpers/utils/index.js +2 -0
- package/esm/helpers/utils/internal-movement-items.js +30 -0
- package/esm/helpers/utils/tenant-purchase-order-engine.js +10 -18
- package/esm/helpers/utils/warehouse-purchase-order-engine.js +10 -18
- package/esm/schemas/models/index.js +1 -0
- package/esm/schemas/models/pending_uploads.js +102 -0
- package/esm/schemas/models/warehouse_internal_movements.js +54 -0
- package/esm/schemas/types/index.js +1 -0
- package/esm/schemas/types/pending_uploads.js +1 -0
- package/lib/constants/ENUMS.d.ts +1 -0
- package/lib/constants/ENUMS.js +1 -0
- package/lib/constants/enums/storage.d.ts +21 -0
- package/lib/constants/enums/storage.js +26 -0
- package/lib/helpers/index.d.ts +1 -0
- package/lib/helpers/index.js +1 -0
- package/lib/helpers/order/generate-warehouse-internal-movement.js +3 -0
- package/lib/helpers/storage/index.d.ts +1 -0
- package/lib/helpers/storage/index.js +17 -0
- package/lib/helpers/storage/upload-paths.d.ts +117 -0
- package/lib/helpers/storage/upload-paths.js +394 -0
- package/lib/helpers/store/common-warehouse-internal-movement-actions.d.ts +33 -1
- package/lib/helpers/store/common-warehouse-internal-movement-actions.js +126 -3
- package/lib/helpers/utils/discount-amounts.d.ts +50 -0
- package/lib/helpers/utils/discount-amounts.js +69 -0
- package/lib/helpers/utils/index.d.ts +2 -0
- package/lib/helpers/utils/index.js +2 -0
- package/lib/helpers/utils/internal-movement-items.d.ts +35 -0
- package/lib/helpers/utils/internal-movement-items.js +36 -0
- package/lib/helpers/utils/tenant-purchase-order-engine.js +9 -17
- package/lib/helpers/utils/warehouse-purchase-order-engine.js +9 -17
- package/lib/schemas/models/index.d.ts +1 -0
- package/lib/schemas/models/index.js +1 -0
- package/lib/schemas/models/inventories.d.ts +3 -3
- package/lib/schemas/models/inventory_transactions.d.ts +3 -3
- package/lib/schemas/models/item_transactions.d.ts +3 -3
- package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +3 -3
- package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +3 -3
- package/lib/schemas/models/orders.d.ts +120 -120
- package/lib/schemas/models/pending_uploads.d.ts +118 -0
- package/lib/schemas/models/pending_uploads.js +105 -0
- package/lib/schemas/models/tenant_hr_employees.d.ts +3 -3
- package/lib/schemas/models/tenant_inventory_department_transactions.d.ts +3 -3
- package/lib/schemas/models/tenant_inventory_department_utilities.d.ts +3 -3
- package/lib/schemas/models/warehouse_internal_movements.d.ts +570 -0
- package/lib/schemas/models/warehouse_internal_movements.js +54 -0
- package/lib/schemas/models/warehouse_inventory_transactions.d.ts +3 -3
- package/lib/schemas/models/warehouse_item_transactions.d.ts +3 -3
- package/lib/schemas/models/warehouse_items.d.ts +3 -3
- package/lib/schemas/types/index.d.ts +1 -0
- package/lib/schemas/types/index.js +1 -0
- package/lib/schemas/types/pending_uploads.d.ts +30 -0
- package/lib/schemas/types/pending_uploads.js +2 -0
- package/lib/schemas/types/warehouse_internal_movements.d.ts +17 -1
- package/lib/tests/pending-uploads-schema.test.d.ts +1 -0
- package/lib/tests/pending-uploads-schema.test.js +79 -0
- package/lib/tests/storage-upload-paths.test.d.ts +1 -0
- package/lib/tests/storage-upload-paths.test.js +198 -0
- package/package.json +1 -1
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Server-owned Cloud Storage upload path registry.
|
|
4
|
+
*
|
|
5
|
+
* The client sends a `kind` plus entity ids and file metadata. This module is
|
|
6
|
+
* the only place that turns those inputs into a bucket-relative object path.
|
|
7
|
+
* Nothing here performs I/O, so both backend services can import it and the
|
|
8
|
+
* toolkit build stays usable in browser contexts.
|
|
9
|
+
*
|
|
10
|
+
* Security invariants:
|
|
11
|
+
* - every kind is in a closed registry;
|
|
12
|
+
* - every client-supplied id is a single, validated path segment;
|
|
13
|
+
* - only `images/` and `documents/` roots are reachable;
|
|
14
|
+
* - the final path is re-validated after construction.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.StorageUploadValidationError = exports.STORAGE_UPLOAD_KIND = exports.DOCUMENT_MIME_EXTENSIONS = exports.IMAGE_MIME_EXTENSIONS = exports.DOCUMENT_MAX_BYTES = exports.IMAGE_MAX_BYTES = void 0;
|
|
18
|
+
exports.normalizeStorageMime = normalizeStorageMime;
|
|
19
|
+
exports.isStorageUploadKind = isStorageUploadKind;
|
|
20
|
+
exports.getStorageUploadDefinition = getStorageUploadDefinition;
|
|
21
|
+
exports.getStorageUploadKinds = getStorageUploadKinds;
|
|
22
|
+
exports.getStorageUploadKindsForRoute = getStorageUploadKindsForRoute;
|
|
23
|
+
exports.buildStorageUploadPath = buildStorageUploadPath;
|
|
24
|
+
const storage_1 = require("../../constants/enums/storage");
|
|
25
|
+
exports.IMAGE_MAX_BYTES = 5 * 1024 * 1024;
|
|
26
|
+
exports.DOCUMENT_MAX_BYTES = 10 * 1024 * 1024;
|
|
27
|
+
exports.IMAGE_MIME_EXTENSIONS = {
|
|
28
|
+
'image/avif': 'avif',
|
|
29
|
+
'image/gif': 'gif',
|
|
30
|
+
'image/heic': 'heic',
|
|
31
|
+
'image/heif': 'heif',
|
|
32
|
+
'image/jpeg': 'jpeg',
|
|
33
|
+
'image/jpg': 'jpeg',
|
|
34
|
+
'image/png': 'png',
|
|
35
|
+
'image/vnd.microsoft.icon': 'ico',
|
|
36
|
+
'image/webp': 'webp',
|
|
37
|
+
'image/x-icon': 'ico',
|
|
38
|
+
};
|
|
39
|
+
exports.DOCUMENT_MIME_EXTENSIONS = {
|
|
40
|
+
'application/msword': ['doc'],
|
|
41
|
+
'application/pdf': ['pdf'],
|
|
42
|
+
'application/vnd.ms-excel': ['xls'],
|
|
43
|
+
'application/vnd.ms-powerpoint': ['ppt'],
|
|
44
|
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation': ['pptx'],
|
|
45
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['xlsx'],
|
|
46
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['docx'],
|
|
47
|
+
'image/heic': ['heic'],
|
|
48
|
+
'image/jpeg': ['jpg', 'jpeg'],
|
|
49
|
+
'image/png': ['png'],
|
|
50
|
+
'image/webp': ['webp'],
|
|
51
|
+
'text/csv': ['csv'],
|
|
52
|
+
'text/plain': ['txt'],
|
|
53
|
+
};
|
|
54
|
+
exports.STORAGE_UPLOAD_KIND = {
|
|
55
|
+
BRAND_CATEGORY_BANNER: 'brand.category_banner',
|
|
56
|
+
BRAND_CATEGORY_IMAGE: 'brand.category',
|
|
57
|
+
BRAND_HALF_HALF_IMAGE: 'brand.halfhalf',
|
|
58
|
+
BRAND_IMAGE: 'brand.image',
|
|
59
|
+
BRAND_ITEM_IMAGE: 'brand.item',
|
|
60
|
+
BRAND_OFFER_IMAGE: 'brand.offer',
|
|
61
|
+
BRAND_SPECIAL_IMAGE: 'brand.special',
|
|
62
|
+
BRAND_WAREHOUSE_CATEGORY_IMAGE: 'brand.warehouse.category',
|
|
63
|
+
BRAND_WAREHOUSE_ITEM_IMAGE: 'brand.warehouse.item',
|
|
64
|
+
DELIVERY_PROOF: 'delivery.proof',
|
|
65
|
+
DOCUMENT_HR: 'document.hr',
|
|
66
|
+
DOCUMENT_POLICY: 'document.policy',
|
|
67
|
+
INTERNAL_EXPENSE_IMAGE: 'internal.expense',
|
|
68
|
+
INTERNAL_RESTAURANT_IMAGE: 'internal.restaurant',
|
|
69
|
+
RIDER_IMAGE: 'rider.image',
|
|
70
|
+
TENANT_DEPARTMENT_IMAGE: 'tenant.department',
|
|
71
|
+
TENANT_DIGITAL_MENU: 'tenant.digital_menu',
|
|
72
|
+
TENANT_EXPENSE_IMAGE: 'tenant.expense',
|
|
73
|
+
TENANT_IMAGE: 'tenant.image',
|
|
74
|
+
TENANT_INVENTORY_IMAGE: 'tenant.inventory',
|
|
75
|
+
TENANT_MARKETING_IMAGE: 'tenant.marketing',
|
|
76
|
+
TENANT_SLIDER: 'tenant.slider',
|
|
77
|
+
TENANT_STAFF_IMAGE: 'tenant.staff',
|
|
78
|
+
TENANT_WEB_COVER: 'tenant.web_cover',
|
|
79
|
+
TENANT_WEB_FAVICON: 'tenant.web_favicon',
|
|
80
|
+
WAREHOUSE_EXPENSE_IMAGE: 'warehouse.expense',
|
|
81
|
+
WAREHOUSE_IMAGE: 'warehouse.image',
|
|
82
|
+
WAREHOUSE_INVENTORY_IMAGE: 'warehouse.inventory',
|
|
83
|
+
WAREHOUSE_STAFF_IMAGE: 'warehouse.staff',
|
|
84
|
+
};
|
|
85
|
+
class StorageUploadValidationError extends Error {
|
|
86
|
+
constructor(code, message) {
|
|
87
|
+
super(message);
|
|
88
|
+
this.name = 'StorageUploadValidationError';
|
|
89
|
+
this.code = code;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.StorageUploadValidationError = StorageUploadValidationError;
|
|
93
|
+
const ID_FIELDS = [
|
|
94
|
+
'brand_id',
|
|
95
|
+
'tenant_id',
|
|
96
|
+
'warehouse_id',
|
|
97
|
+
'employee_id',
|
|
98
|
+
'delivery_id',
|
|
99
|
+
'record_id',
|
|
100
|
+
'resource_id',
|
|
101
|
+
];
|
|
102
|
+
const SEGMENT_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
103
|
+
const FINAL_PATH_PATTERN = /^[A-Za-z0-9._/-]+$/;
|
|
104
|
+
const UUID_V4_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
105
|
+
function fail(code, message) {
|
|
106
|
+
throw new StorageUploadValidationError(code, message);
|
|
107
|
+
}
|
|
108
|
+
function requireId(ids, field) {
|
|
109
|
+
const value = ids[field];
|
|
110
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
111
|
+
fail('MISSING_FIELD', `Missing required storage id: ${field}`);
|
|
112
|
+
}
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
function assertSegment(value, field) {
|
|
116
|
+
if (typeof value !== 'string' || !SEGMENT_PATTERN.test(value) || value.includes('..')) {
|
|
117
|
+
fail('INVALID_ID', `Invalid storage path segment for ${field}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function assertFinalPath(path) {
|
|
121
|
+
if (!path.startsWith('images/') && !path.startsWith('documents/')) {
|
|
122
|
+
fail('INVALID_PATH', 'Storage path must start with images/ or documents/');
|
|
123
|
+
}
|
|
124
|
+
if (path.length === 0 || path.length > 1024) {
|
|
125
|
+
fail('INVALID_PATH', 'Storage path length is out of bounds');
|
|
126
|
+
}
|
|
127
|
+
if (path.includes('//') || path.includes('..') || path.includes('?') || path.includes('#')) {
|
|
128
|
+
fail('INVALID_PATH', 'Storage path contains a forbidden sequence');
|
|
129
|
+
}
|
|
130
|
+
if (!FINAL_PATH_PATTERN.test(path)) {
|
|
131
|
+
fail('INVALID_PATH', 'Storage path contains forbidden characters');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function assertStage(stage) {
|
|
135
|
+
if (stage === storage_1.PENDING_UPLOAD_STAGE_ENUM.PICKUP || stage === storage_1.PENDING_UPLOAD_STAGE_ENUM.DROPOFF) {
|
|
136
|
+
return stage;
|
|
137
|
+
}
|
|
138
|
+
return fail('INVALID_STAGE', 'stage must be PICKUP or DROPOFF');
|
|
139
|
+
}
|
|
140
|
+
function assertUuid(uuid) {
|
|
141
|
+
if (typeof uuid !== 'string' || !UUID_V4_PATTERN.test(uuid)) {
|
|
142
|
+
fail('INVALID_UUID', 'A valid server-generated UUID is required for this kind');
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function normalizeStorageMime(mime) {
|
|
146
|
+
if (typeof mime !== 'string') {
|
|
147
|
+
fail('INVALID_MIME', 'mime must be a string');
|
|
148
|
+
}
|
|
149
|
+
return mime.split(';')[0].trim().toLowerCase();
|
|
150
|
+
}
|
|
151
|
+
function imageExtension(mime) {
|
|
152
|
+
const extension = exports.IMAGE_MIME_EXTENSIONS[mime];
|
|
153
|
+
if (!extension) {
|
|
154
|
+
fail('INVALID_MIME', `Unsupported image mime type: ${mime}`);
|
|
155
|
+
}
|
|
156
|
+
return extension;
|
|
157
|
+
}
|
|
158
|
+
function filenameExtension(filename, mime) {
|
|
159
|
+
const allowed = exports.DOCUMENT_MIME_EXTENSIONS[mime];
|
|
160
|
+
if (!allowed) {
|
|
161
|
+
fail('INVALID_MIME', `Unsupported document mime type: ${mime}`);
|
|
162
|
+
}
|
|
163
|
+
const cleaned = filename.split('?')[0].split('#')[0].replace(/\\/g, '/');
|
|
164
|
+
const base = cleaned.slice(cleaned.lastIndexOf('/') + 1);
|
|
165
|
+
if (!base || base.startsWith('.') || base.includes('..')) {
|
|
166
|
+
fail('INVALID_FILENAME', 'Invalid document filename');
|
|
167
|
+
}
|
|
168
|
+
const match = /\.([A-Za-z0-9]{1,8})$/.exec(base);
|
|
169
|
+
if (!match) {
|
|
170
|
+
fail('INVALID_FILENAME', 'Document filename has no usable extension');
|
|
171
|
+
}
|
|
172
|
+
const extension = match[1].toLowerCase();
|
|
173
|
+
if (!allowed.includes(extension)) {
|
|
174
|
+
fail('INVALID_FILENAME', `Filename extension .${extension} does not match ${mime}`);
|
|
175
|
+
}
|
|
176
|
+
return extension;
|
|
177
|
+
}
|
|
178
|
+
function resolveExtension(definition, mime, filename) {
|
|
179
|
+
if (definition.extension === 'mime') {
|
|
180
|
+
if (definition.media !== 'image') {
|
|
181
|
+
fail('INVALID_MIME', 'Mime-derived extensions are only allowed for images');
|
|
182
|
+
}
|
|
183
|
+
return `.${imageExtension(mime)}`;
|
|
184
|
+
}
|
|
185
|
+
if (typeof filename !== 'string' || filename.length === 0) {
|
|
186
|
+
fail('MISSING_FIELD', 'filename is required for this kind');
|
|
187
|
+
}
|
|
188
|
+
return `.${filenameExtension(filename, mime)}`;
|
|
189
|
+
}
|
|
190
|
+
function imageDefinition(kind, owner, identities, routes, requiredIds, build, options) {
|
|
191
|
+
return {
|
|
192
|
+
kind,
|
|
193
|
+
routes,
|
|
194
|
+
owner,
|
|
195
|
+
identities,
|
|
196
|
+
media: 'image',
|
|
197
|
+
maxBytes: exports.IMAGE_MAX_BYTES,
|
|
198
|
+
extension: 'mime',
|
|
199
|
+
...options,
|
|
200
|
+
requiredIds,
|
|
201
|
+
build,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function documentDefinition(kind, requiredIds, build) {
|
|
205
|
+
return {
|
|
206
|
+
kind,
|
|
207
|
+
routes: ['items'],
|
|
208
|
+
owner: 'document',
|
|
209
|
+
identities: ['tenant_staff_hr'],
|
|
210
|
+
media: 'document',
|
|
211
|
+
maxBytes: exports.DOCUMENT_MAX_BYTES,
|
|
212
|
+
extension: 'filename',
|
|
213
|
+
leaf: 'uuid',
|
|
214
|
+
requiredIds,
|
|
215
|
+
build,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const FIXED_IMAGE_NAME = 'image_url';
|
|
219
|
+
const recordBaseName = (ids) => requireId(ids, 'record_id');
|
|
220
|
+
const STORAGE_UPLOAD_DEFINITIONS = {
|
|
221
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_IMAGE, 'brand', ['tenant_staff', 'internal'], ['items'], ['brand_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), fileName].join('/'), { baseName: () => FIXED_IMAGE_NAME, leaf: 'fixed' }),
|
|
222
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_CATEGORY_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_CATEGORY_IMAGE, 'brand', ['tenant_staff', 'internal'], ['items'], ['brand_id', 'record_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), 'categories', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
223
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_CATEGORY_BANNER]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_CATEGORY_BANNER, 'brand', ['tenant_staff', 'internal'], ['items'], ['brand_id', 'record_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), 'categories-banner', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
224
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_ITEM_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_ITEM_IMAGE, 'brand', ['tenant_staff', 'internal'], ['items'], ['brand_id', 'record_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), 'items', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
225
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_OFFER_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_OFFER_IMAGE, 'brand', ['tenant_staff', 'internal'], ['items'], ['brand_id', 'record_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), 'offers', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
226
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_SPECIAL_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_SPECIAL_IMAGE, 'brand', ['tenant_staff', 'internal'], ['items'], ['brand_id', 'record_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), 'specials', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
227
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_HALF_HALF_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_HALF_HALF_IMAGE, 'brand', ['tenant_staff', 'internal'], ['items'], ['brand_id', 'record_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), 'halfhalf', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
228
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_WAREHOUSE_CATEGORY_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_WAREHOUSE_CATEGORY_IMAGE, 'brand', ['warehouse_staff', 'internal'], ['items', 'operations'], ['brand_id', 'record_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), 'warehouse-categories', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
229
|
+
[exports.STORAGE_UPLOAD_KIND.BRAND_WAREHOUSE_ITEM_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.BRAND_WAREHOUSE_ITEM_IMAGE, 'brand', ['warehouse_staff', 'internal'], ['items', 'operations'], ['brand_id', 'record_id'], (ids, fileName) => ['images', 'brands', requireId(ids, 'brand_id'), 'warehouse-items', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
230
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_IMAGE, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), fileName].join('/'), { baseName: () => FIXED_IMAGE_NAME, leaf: 'fixed' }),
|
|
231
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_WEB_COVER]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_WEB_COVER, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), fileName].join('/'), { baseName: () => 'web_cover', leaf: 'fixed' }),
|
|
232
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_WEB_FAVICON]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_WEB_FAVICON, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), fileName].join('/'), { baseName: () => 'web_favicon', leaf: 'fixed' }),
|
|
233
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_DIGITAL_MENU]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_DIGITAL_MENU, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), 'digital-menu', fileName].join('/'), { leaf: 'uuid' }),
|
|
234
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_SLIDER]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_SLIDER, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), 'sliders', fileName].join('/'), { leaf: 'uuid' }),
|
|
235
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_STAFF_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_STAFF_IMAGE, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id', 'record_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), 'staff', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
236
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_MARKETING_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_MARKETING_IMAGE, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id', 'record_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), 'marketing', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
237
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_EXPENSE_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_EXPENSE_IMAGE, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id', 'record_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), 'expense-details', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
238
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_INVENTORY_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_INVENTORY_IMAGE, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id', 'record_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), 'inventory', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
239
|
+
[exports.STORAGE_UPLOAD_KIND.TENANT_DEPARTMENT_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.TENANT_DEPARTMENT_IMAGE, 'tenant', ['tenant_staff', 'internal'], ['items'], ['tenant_id', 'record_id'], (ids, fileName) => ['images', 'tenants', requireId(ids, 'tenant_id'), 'departments', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
240
|
+
[exports.STORAGE_UPLOAD_KIND.WAREHOUSE_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.WAREHOUSE_IMAGE, 'warehouse', ['warehouse_staff', 'internal'], ['items', 'operations'], ['warehouse_id'], (ids, fileName) => ['images', 'warehouses', requireId(ids, 'warehouse_id'), fileName].join('/'), { baseName: () => FIXED_IMAGE_NAME, leaf: 'fixed' }),
|
|
241
|
+
[exports.STORAGE_UPLOAD_KIND.WAREHOUSE_STAFF_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.WAREHOUSE_STAFF_IMAGE, 'warehouse', ['warehouse_staff', 'internal'], ['items', 'operations'], ['warehouse_id', 'record_id'], (ids, fileName) => ['images', 'warehouses', requireId(ids, 'warehouse_id'), 'staff', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
242
|
+
[exports.STORAGE_UPLOAD_KIND.WAREHOUSE_INVENTORY_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.WAREHOUSE_INVENTORY_IMAGE, 'warehouse', ['warehouse_staff', 'internal'], ['items', 'operations'], ['warehouse_id', 'record_id'], (ids, fileName) => ['images', 'warehouses', requireId(ids, 'warehouse_id'), 'inventory', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
243
|
+
[exports.STORAGE_UPLOAD_KIND.WAREHOUSE_EXPENSE_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.WAREHOUSE_EXPENSE_IMAGE, 'warehouse', ['warehouse_staff', 'internal'], ['items', 'operations'], ['warehouse_id', 'record_id'], (ids, fileName) => ['images', 'warehouses', requireId(ids, 'warehouse_id'), 'warehouse-expense-details', fileName].join('/'), { baseName: recordBaseName, leaf: 'record' }),
|
|
244
|
+
[exports.STORAGE_UPLOAD_KIND.DOCUMENT_HR]: documentDefinition(exports.STORAGE_UPLOAD_KIND.DOCUMENT_HR, ['tenant_id', 'employee_id'], (ids, fileName) => [
|
|
245
|
+
'documents',
|
|
246
|
+
'tenants',
|
|
247
|
+
requireId(ids, 'tenant_id'),
|
|
248
|
+
'hr-documents',
|
|
249
|
+
requireId(ids, 'employee_id'),
|
|
250
|
+
fileName,
|
|
251
|
+
].join('/')),
|
|
252
|
+
[exports.STORAGE_UPLOAD_KIND.DOCUMENT_POLICY]: documentDefinition(exports.STORAGE_UPLOAD_KIND.DOCUMENT_POLICY, ['tenant_id'], (ids, fileName) => ['documents', 'tenants', requireId(ids, 'tenant_id'), 'hr-policies', fileName].join('/')),
|
|
253
|
+
[exports.STORAGE_UPLOAD_KIND.INTERNAL_RESTAURANT_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.INTERNAL_RESTAURANT_IMAGE, 'internal', ['internal'], ['items'], ['resource_id'], (ids, fileName) => ['images', 'restaurants', requireId(ids, 'resource_id'), fileName].join('/'), { baseName: () => FIXED_IMAGE_NAME, leaf: 'fixed' }),
|
|
254
|
+
[exports.STORAGE_UPLOAD_KIND.INTERNAL_EXPENSE_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.INTERNAL_EXPENSE_IMAGE, 'internal', ['internal'], ['items'], ['resource_id'], (ids, fileName) => ['images', 'expenses', requireId(ids, 'resource_id'), fileName].join('/'), { baseName: () => FIXED_IMAGE_NAME, leaf: 'fixed' }),
|
|
255
|
+
[exports.STORAGE_UPLOAD_KIND.RIDER_IMAGE]: imageDefinition(exports.STORAGE_UPLOAD_KIND.RIDER_IMAGE, 'rider', ['rider'], ['riders'], [], (_ids, fileName, uid) => ['images', 'riders', uid, fileName].join('/'), { baseName: () => FIXED_IMAGE_NAME, leaf: 'fixed', requiresUid: true }),
|
|
256
|
+
[exports.STORAGE_UPLOAD_KIND.DELIVERY_PROOF]: imageDefinition(exports.STORAGE_UPLOAD_KIND.DELIVERY_PROOF, 'delivery', ['delivery_rider'], ['riders'], ['delivery_id'], (ids, fileName) => ['images', 'deliveries', requireId(ids, 'delivery_id'), fileName].join('/'), {
|
|
257
|
+
baseName: (ids) => `${assertStage(ids.stage).toLowerCase()}_food_image_url`,
|
|
258
|
+
leaf: 'fixed',
|
|
259
|
+
requiredStage: true,
|
|
260
|
+
}),
|
|
261
|
+
};
|
|
262
|
+
function resolveStorageUploadRef(definition, ids, uid) {
|
|
263
|
+
switch (definition.owner) {
|
|
264
|
+
case 'brand':
|
|
265
|
+
return {
|
|
266
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.BRAND,
|
|
267
|
+
refId: requireId(ids, 'brand_id'),
|
|
268
|
+
};
|
|
269
|
+
case 'tenant':
|
|
270
|
+
return {
|
|
271
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.TENANT,
|
|
272
|
+
refId: requireId(ids, 'tenant_id'),
|
|
273
|
+
};
|
|
274
|
+
case 'warehouse':
|
|
275
|
+
return {
|
|
276
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.WAREHOUSE,
|
|
277
|
+
refId: requireId(ids, 'warehouse_id'),
|
|
278
|
+
};
|
|
279
|
+
case 'document':
|
|
280
|
+
return definition.kind === exports.STORAGE_UPLOAD_KIND.DOCUMENT_HR
|
|
281
|
+
? {
|
|
282
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.EMPLOYEE,
|
|
283
|
+
refId: requireId(ids, 'employee_id'),
|
|
284
|
+
}
|
|
285
|
+
: {
|
|
286
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.TENANT,
|
|
287
|
+
refId: requireId(ids, 'tenant_id'),
|
|
288
|
+
};
|
|
289
|
+
case 'internal':
|
|
290
|
+
return definition.kind === exports.STORAGE_UPLOAD_KIND.INTERNAL_RESTAURANT_IMAGE
|
|
291
|
+
? {
|
|
292
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.RESTAURANT,
|
|
293
|
+
refId: requireId(ids, 'resource_id'),
|
|
294
|
+
}
|
|
295
|
+
: {
|
|
296
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.EXPENSE,
|
|
297
|
+
refId: requireId(ids, 'resource_id'),
|
|
298
|
+
};
|
|
299
|
+
case 'rider':
|
|
300
|
+
return {
|
|
301
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.RIDER,
|
|
302
|
+
refId: uid,
|
|
303
|
+
};
|
|
304
|
+
case 'delivery':
|
|
305
|
+
return {
|
|
306
|
+
refType: storage_1.PENDING_UPLOAD_REF_TYPE_ENUM.DELIVERY,
|
|
307
|
+
refId: requireId(ids, 'delivery_id'),
|
|
308
|
+
};
|
|
309
|
+
default:
|
|
310
|
+
return fail('INVALID_KIND', `Kind ${definition.kind} has no storage reference`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function isStorageUploadKind(value) {
|
|
314
|
+
return typeof value === 'string' && Object.prototype.hasOwnProperty.call(STORAGE_UPLOAD_DEFINITIONS, value);
|
|
315
|
+
}
|
|
316
|
+
function getStorageUploadDefinition(kind) {
|
|
317
|
+
if (!isStorageUploadKind(kind)) {
|
|
318
|
+
fail('INVALID_KIND', `Unknown storage upload kind: ${String(kind)}`);
|
|
319
|
+
}
|
|
320
|
+
return STORAGE_UPLOAD_DEFINITIONS[kind];
|
|
321
|
+
}
|
|
322
|
+
function getStorageUploadKinds() {
|
|
323
|
+
return Object.values(exports.STORAGE_UPLOAD_KIND);
|
|
324
|
+
}
|
|
325
|
+
function getStorageUploadKindsForRoute(route) {
|
|
326
|
+
return getStorageUploadKinds().filter((kind) => STORAGE_UPLOAD_DEFINITIONS[kind].routes.includes(route));
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Builds the bucket-relative object path for a validated upload request.
|
|
330
|
+
*
|
|
331
|
+
* Throws `StorageUploadValidationError` for every caller error. The route
|
|
332
|
+
* should map that error to HTTP 400 (or 415 for an unsupported MIME).
|
|
333
|
+
*/
|
|
334
|
+
function buildStorageUploadPath(input) {
|
|
335
|
+
const definition = getStorageUploadDefinition(input.kind);
|
|
336
|
+
const ids = input.ids ?? {};
|
|
337
|
+
const mime = normalizeStorageMime(input.mime);
|
|
338
|
+
if (!mime) {
|
|
339
|
+
fail('INVALID_MIME', 'mime is required');
|
|
340
|
+
}
|
|
341
|
+
for (const field of ID_FIELDS) {
|
|
342
|
+
const value = ids[field];
|
|
343
|
+
if (value !== undefined) {
|
|
344
|
+
if (typeof value !== 'string') {
|
|
345
|
+
fail('INVALID_ID', `Invalid storage path segment for ${field}`);
|
|
346
|
+
}
|
|
347
|
+
assertSegment(value, field);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (ids.stage !== undefined) {
|
|
351
|
+
assertStage(ids.stage);
|
|
352
|
+
}
|
|
353
|
+
for (const field of definition.requiredIds) {
|
|
354
|
+
requireId(ids, field);
|
|
355
|
+
}
|
|
356
|
+
if (definition.requiredStage && ids.stage === undefined) {
|
|
357
|
+
fail('MISSING_FIELD', 'stage is required for this kind');
|
|
358
|
+
}
|
|
359
|
+
if (definition.requiresUid) {
|
|
360
|
+
if (!input.uid) {
|
|
361
|
+
fail('MISSING_FIELD', 'uid is required for this kind');
|
|
362
|
+
}
|
|
363
|
+
assertSegment(input.uid, 'uid');
|
|
364
|
+
}
|
|
365
|
+
const extension = resolveExtension(definition, mime, input.filename);
|
|
366
|
+
let base;
|
|
367
|
+
if (definition.leaf === 'uuid') {
|
|
368
|
+
if (!input.uuid) {
|
|
369
|
+
fail('MISSING_FIELD', 'uuid is required for this kind');
|
|
370
|
+
}
|
|
371
|
+
assertUuid(input.uuid);
|
|
372
|
+
base = input.uuid;
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
if (!definition.baseName) {
|
|
376
|
+
fail('INVALID_KIND', `Kind ${definition.kind} has no base name`);
|
|
377
|
+
}
|
|
378
|
+
base = definition.baseName(ids);
|
|
379
|
+
}
|
|
380
|
+
assertSegment(base, 'file name');
|
|
381
|
+
const fileName = `${base}${extension}`;
|
|
382
|
+
const ref = resolveStorageUploadRef(definition, ids, input.uid ?? '');
|
|
383
|
+
assertSegment(ref.refId, 'ref_id');
|
|
384
|
+
const path = definition.build(ids, fileName, input.uid ?? '');
|
|
385
|
+
assertFinalPath(path);
|
|
386
|
+
return {
|
|
387
|
+
path,
|
|
388
|
+
extension,
|
|
389
|
+
maxBytes: definition.maxBytes,
|
|
390
|
+
contentType: mime,
|
|
391
|
+
refType: ref.refType,
|
|
392
|
+
refId: ref.refId,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import { IWarehouseInternalMovement, IWarehouseInternalMovementItem, IWarehouseInternalMovementIteration } from '../../schemas';
|
|
1
|
+
import { IDiscountDetails, IWarehouseInternalMovement, IWarehouseInternalMovementDiscountDetails, IWarehouseInternalMovementItem, IWarehouseInternalMovementIteration } from '../../schemas';
|
|
2
|
+
/**
|
|
3
|
+
* Resolved money value of one movement line's discount.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors `getWarehousePurchaseOrderItemDiscountAmount`: a flat
|
|
6
|
+
* `special_discount` is PER UNIT (so it scales with quantity) and a percentage
|
|
7
|
+
* applies to the line's gross value. The result is clamped to the line total.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getWarehouseInternalMovementItemDiscountAmount(item: Pick<IWarehouseInternalMovementItem, 'unit_price' | 'quantity' | 'item_discount_details'>): number;
|
|
10
|
+
/**
|
|
11
|
+
* Refreshes `total_discount` on a line's discount record so it matches the
|
|
12
|
+
* line's current price/quantity, preserving every other field.
|
|
13
|
+
*/
|
|
14
|
+
export declare function withResolvedWarehouseInternalMovementItemDiscountTotal(item: Pick<IWarehouseInternalMovementItem, 'unit_price' | 'quantity' | 'item_discount_details'>): IDiscountDetails | null;
|
|
2
15
|
/**
|
|
3
16
|
* Convert unit_price on every internal movement item.
|
|
4
17
|
*/
|
|
@@ -14,9 +27,28 @@ export declare function getWarehouseInternalMovementIterationsWithPrice(iteratio
|
|
|
14
27
|
* - `writeable = false` → cents → decimal (after receiving from backend)
|
|
15
28
|
*/
|
|
16
29
|
export declare function getWarehouseInternalMovementWithPrice<T extends Partial<IWarehouseInternalMovement> | IWarehouseInternalMovement | IWarehouseInternalMovementIteration>(order: T, writeable?: boolean): T;
|
|
30
|
+
interface IInternalMovementOrderDiscountResult {
|
|
31
|
+
order_discount_details: IWarehouseInternalMovementDiscountDetails | null;
|
|
32
|
+
total_discount_applied: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Resolve the order-level discount of an internal movement against its items
|
|
36
|
+
* subtotal. Mirrors `getWarehousePurchaseOrderOrderDiscountDetails`.
|
|
37
|
+
*
|
|
38
|
+
* `special_discount` is a percentage when `is_special_discount_percentage` is
|
|
39
|
+
* true and a flat money amount otherwise. The resolved value is clamped to the
|
|
40
|
+
* subtotal, so a movement can never go negative.
|
|
41
|
+
*/
|
|
42
|
+
export declare function getWarehouseInternalMovementOrderDiscountDetails(discountDetails: Partial<IWarehouseInternalMovementDiscountDetails> | null | undefined, baseAmount: number): IInternalMovementOrderDiscountResult;
|
|
17
43
|
/**
|
|
18
44
|
* Recalculate financial totals for an internal movement — mirrors
|
|
19
45
|
* `calculateCartWarehousePurchaseOrderInfoAndSyncData` but for the flat
|
|
20
46
|
* `items` array instead of nested `categories`.
|
|
47
|
+
*
|
|
48
|
+
* Each line's item discount is taken off first, then the order-level discount
|
|
49
|
+
* is applied to the resulting subtotal. GST and percentage charges are
|
|
50
|
+
* calculated on the discounted value. When `exclude_discount_from_gst` is true
|
|
51
|
+
* the order discount is taken off after GST instead.
|
|
21
52
|
*/
|
|
22
53
|
export declare function calculateCartInternalMovementInfoAndSyncData<T extends Partial<IWarehouseInternalMovement> | IWarehouseInternalMovement>(data: T): T;
|
|
54
|
+
export {};
|
|
@@ -1,11 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getWarehouseInternalMovementItemDiscountAmount = getWarehouseInternalMovementItemDiscountAmount;
|
|
4
|
+
exports.withResolvedWarehouseInternalMovementItemDiscountTotal = withResolvedWarehouseInternalMovementItemDiscountTotal;
|
|
3
5
|
exports.getWarehouseInternalMovementItemsWithPrice = getWarehouseInternalMovementItemsWithPrice;
|
|
4
6
|
exports.getWarehouseInternalMovementIterationsWithPrice = getWarehouseInternalMovementIterationsWithPrice;
|
|
5
7
|
exports.getWarehouseInternalMovementWithPrice = getWarehouseInternalMovementWithPrice;
|
|
8
|
+
exports.getWarehouseInternalMovementOrderDiscountDetails = getWarehouseInternalMovementOrderDiscountDetails;
|
|
6
9
|
exports.calculateCartInternalMovementInfoAndSyncData = calculateCartInternalMovementInfoAndSyncData;
|
|
7
10
|
const utils_1 = require("../utils");
|
|
8
11
|
const common_order_actions_1 = require("./common-order-actions");
|
|
12
|
+
/**
|
|
13
|
+
* Resolved money value of one movement line's discount.
|
|
14
|
+
*
|
|
15
|
+
* Mirrors `getWarehousePurchaseOrderItemDiscountAmount`: a flat
|
|
16
|
+
* `special_discount` is PER UNIT (so it scales with quantity) and a percentage
|
|
17
|
+
* applies to the line's gross value. The result is clamped to the line total.
|
|
18
|
+
*/
|
|
19
|
+
function getWarehouseInternalMovementItemDiscountAmount(item) {
|
|
20
|
+
// Shared rule with warehouse/tenant POs; this wrapper keeps the movement
|
|
21
|
+
// cart's currency-decimal rounding.
|
|
22
|
+
return (0, utils_1.parseNum)((0, utils_1.getItemDiscountAmount)({
|
|
23
|
+
grossAmount: (0, utils_1.parseNum)((item.unit_price ?? 0) * (item.quantity ?? 0)),
|
|
24
|
+
quantity: item.quantity,
|
|
25
|
+
details: item.item_discount_details,
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Refreshes `total_discount` on a line's discount record so it matches the
|
|
30
|
+
* line's current price/quantity, preserving every other field.
|
|
31
|
+
*/
|
|
32
|
+
function withResolvedWarehouseInternalMovementItemDiscountTotal(item) {
|
|
33
|
+
const details = item.item_discount_details;
|
|
34
|
+
if (!details)
|
|
35
|
+
return null;
|
|
36
|
+
return (0, utils_1.withDiscountTotal)(details, getWarehouseInternalMovementItemDiscountAmount(item));
|
|
37
|
+
}
|
|
9
38
|
/**
|
|
10
39
|
* Convert unit_price on every internal movement item.
|
|
11
40
|
*/
|
|
@@ -15,8 +44,27 @@ function getWarehouseInternalMovementItemsWithPrice(items, convertToPrice) {
|
|
|
15
44
|
return items.map((item) => ({
|
|
16
45
|
...item,
|
|
17
46
|
unit_price: item.unit_price != null ? convertToPrice(item.unit_price) : undefined,
|
|
47
|
+
...(item.item_discount_details != null
|
|
48
|
+
? {
|
|
49
|
+
item_discount_details: getWarehouseInternalMovementItemDiscountDetailsWithPrice(item.item_discount_details, convertToPrice),
|
|
50
|
+
}
|
|
51
|
+
: {}),
|
|
18
52
|
}));
|
|
19
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Scale a line discount record. A percentage is not a price, so only the fixed
|
|
56
|
+
* per-unit amount and the resolved total are converted.
|
|
57
|
+
*/
|
|
58
|
+
function getWarehouseInternalMovementItemDiscountDetailsWithPrice(discount, convertToPrice) {
|
|
59
|
+
const { special_discount, is_special_discount_percentage, total_discount } = discount;
|
|
60
|
+
return {
|
|
61
|
+
...discount,
|
|
62
|
+
...(typeof special_discount === 'number' && !is_special_discount_percentage
|
|
63
|
+
? { special_discount: convertToPrice(special_discount) }
|
|
64
|
+
: {}),
|
|
65
|
+
total_discount: convertToPrice(total_discount ?? 0),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
20
68
|
/**
|
|
21
69
|
* Convert monetary fields on payments array.
|
|
22
70
|
*/
|
|
@@ -39,6 +87,22 @@ function getWarehouseInternalMovementIterationsWithPrice(iterations, writeable)
|
|
|
39
87
|
return [];
|
|
40
88
|
return iterations.map((iteration) => getWarehouseInternalMovementWithPrice(iteration, writeable));
|
|
41
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Convert the order-level discount record. A percentage is not a price, so only
|
|
92
|
+
* the resolved money value (and a fixed `special_discount`) are scaled.
|
|
93
|
+
*/
|
|
94
|
+
function getWarehouseInternalMovementDiscountDetailsWithPrice(discount, convertToPrice) {
|
|
95
|
+
if (!discount)
|
|
96
|
+
return null;
|
|
97
|
+
const { special_discount, is_special_discount_percentage, total_discount } = discount;
|
|
98
|
+
return {
|
|
99
|
+
...discount,
|
|
100
|
+
...(typeof special_discount === 'number' && !is_special_discount_percentage
|
|
101
|
+
? { special_discount: convertToPrice(special_discount) }
|
|
102
|
+
: {}),
|
|
103
|
+
total_discount: convertToPrice(total_discount ?? 0),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
42
106
|
/**
|
|
43
107
|
* Convert all monetary fields on an internal movement.
|
|
44
108
|
*
|
|
@@ -59,6 +123,14 @@ function getWarehouseInternalMovementWithPrice(order, writeable) {
|
|
|
59
123
|
...(order.amount_collected != null ? { amount_collected: convertToPrice(order.amount_collected) } : {}),
|
|
60
124
|
...(order.amount_earned != null ? { amount_earned: convertToPrice(order.amount_earned) } : {}),
|
|
61
125
|
...(order.gst != null ? { gst: convertToPrice(order.gst) } : {}),
|
|
126
|
+
...(order.order_discount_details != null
|
|
127
|
+
? {
|
|
128
|
+
order_discount_details: getWarehouseInternalMovementDiscountDetailsWithPrice(order.order_discount_details, convertToPrice),
|
|
129
|
+
}
|
|
130
|
+
: {}),
|
|
131
|
+
...(order.total_discount_applied != null
|
|
132
|
+
? { total_discount_applied: convertToPrice(order.total_discount_applied) }
|
|
133
|
+
: {}),
|
|
62
134
|
...(order.labor_charges != null && order.is_labor_charges_percentage !== true
|
|
63
135
|
? { labor_charges: convertToPrice(order.labor_charges) }
|
|
64
136
|
: {}),
|
|
@@ -81,25 +153,70 @@ function getWarehouseInternalMovementWithPrice(order, writeable) {
|
|
|
81
153
|
: {}),
|
|
82
154
|
};
|
|
83
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Resolve the order-level discount of an internal movement against its items
|
|
158
|
+
* subtotal. Mirrors `getWarehousePurchaseOrderOrderDiscountDetails`.
|
|
159
|
+
*
|
|
160
|
+
* `special_discount` is a percentage when `is_special_discount_percentage` is
|
|
161
|
+
* true and a flat money amount otherwise. The resolved value is clamped to the
|
|
162
|
+
* subtotal, so a movement can never go negative.
|
|
163
|
+
*/
|
|
164
|
+
function getWarehouseInternalMovementOrderDiscountDetails(discountDetails, baseAmount) {
|
|
165
|
+
if (!discountDetails) {
|
|
166
|
+
return { order_discount_details: null, total_discount_applied: 0 };
|
|
167
|
+
}
|
|
168
|
+
const totalDiscount = (0, utils_1.parseNum)((0, utils_1.getOrderDiscountAmount)({
|
|
169
|
+
baseAmount,
|
|
170
|
+
details: discountDetails,
|
|
171
|
+
}));
|
|
172
|
+
if (totalDiscount <= 0) {
|
|
173
|
+
return { order_discount_details: null, total_discount_applied: 0 };
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
order_discount_details: (0, utils_1.withDiscountTotal)(discountDetails, totalDiscount),
|
|
177
|
+
total_discount_applied: totalDiscount,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
84
180
|
/**
|
|
85
181
|
* Recalculate financial totals for an internal movement — mirrors
|
|
86
182
|
* `calculateCartWarehousePurchaseOrderInfoAndSyncData` but for the flat
|
|
87
183
|
* `items` array instead of nested `categories`.
|
|
184
|
+
*
|
|
185
|
+
* Each line's item discount is taken off first, then the order-level discount
|
|
186
|
+
* is applied to the resulting subtotal. GST and percentage charges are
|
|
187
|
+
* calculated on the discounted value. When `exclude_discount_from_gst` is true
|
|
188
|
+
* the order discount is taken off after GST instead.
|
|
88
189
|
*/
|
|
89
190
|
function calculateCartInternalMovementInfoAndSyncData(data) {
|
|
90
|
-
|
|
191
|
+
let subtotal = 0;
|
|
192
|
+
const items = (data.items ?? []).map((item) => {
|
|
193
|
+
const grossAmount = (0, utils_1.parseNum)((item.unit_price ?? 0) * (item.quantity ?? 0));
|
|
194
|
+
const itemDiscountAmount = getWarehouseInternalMovementItemDiscountAmount(item);
|
|
195
|
+
subtotal += (0, utils_1.parseNum)(Math.max(0, grossAmount - itemDiscountAmount));
|
|
196
|
+
return {
|
|
197
|
+
...item,
|
|
198
|
+
item_discount_details: withResolvedWarehouseInternalMovementItemDiscountTotal(item),
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
subtotal = (0, utils_1.parseNum)(subtotal);
|
|
202
|
+
const { order_discount_details, total_discount_applied } = getWarehouseInternalMovementOrderDiscountDetails(data.order_discount_details, subtotal);
|
|
203
|
+
const applyGSTOnOriginalPrice = data.exclude_discount_from_gst === true;
|
|
91
204
|
let grand_total = subtotal;
|
|
205
|
+
if (!applyGSTOnOriginalPrice) {
|
|
206
|
+
grand_total -= total_discount_applied;
|
|
207
|
+
}
|
|
208
|
+
const postDiscountSubtotal = (0, utils_1.parseNum)(Math.max(0, subtotal - total_discount_applied));
|
|
92
209
|
let gst_amount = 0;
|
|
93
210
|
const gst = data.gst ?? 0;
|
|
94
211
|
// Order-level charges (labor, freight): entered per movement, either a fixed
|
|
95
|
-
// amount or a percentage of the items subtotal. `is_*_charges_taxable`
|
|
212
|
+
// amount or a percentage of the post-discount items subtotal. `is_*_charges_taxable`
|
|
96
213
|
// (snapshot of the warehouse setting) decides the placement: taxable => added
|
|
97
214
|
// BEFORE GST, untaxed => added after it.
|
|
98
215
|
const getOrderCharge = (value, isPercentage) => {
|
|
99
216
|
const entered = (0, utils_1.parseNum)(Math.max(0, value ?? 0));
|
|
100
217
|
return {
|
|
101
218
|
entered,
|
|
102
|
-
amount: isPercentage ? (0, utils_1.parseNum)((0, utils_1.getPercentageAmount)(
|
|
219
|
+
amount: isPercentage ? (0, utils_1.parseNum)((0, utils_1.getPercentageAmount)(postDiscountSubtotal, entered)) : entered,
|
|
103
220
|
};
|
|
104
221
|
};
|
|
105
222
|
const labor = getOrderCharge(data.labor_charges, data.is_labor_charges_percentage);
|
|
@@ -121,6 +238,9 @@ function calculateCartInternalMovementInfoAndSyncData(data) {
|
|
|
121
238
|
gst_amount = (0, utils_1.getPercentageAmount)(grand_total, gst);
|
|
122
239
|
grand_total += gst_amount;
|
|
123
240
|
}
|
|
241
|
+
if (applyGSTOnOriginalPrice) {
|
|
242
|
+
grand_total -= total_discount_applied;
|
|
243
|
+
}
|
|
124
244
|
const untaxedCharges = (isLaborTaxable ? 0 : labor_charges_amount) + (isFreightTaxable ? 0 : freight_charges_amount);
|
|
125
245
|
if (untaxedCharges > 0) {
|
|
126
246
|
grand_total += untaxedCharges;
|
|
@@ -130,8 +250,11 @@ function calculateCartInternalMovementInfoAndSyncData(data) {
|
|
|
130
250
|
grand_total = (0, utils_1.parseNum)(grand_total);
|
|
131
251
|
return {
|
|
132
252
|
...data,
|
|
253
|
+
items,
|
|
133
254
|
grand_total,
|
|
134
255
|
gst_amount,
|
|
256
|
+
order_discount_details,
|
|
257
|
+
total_discount_applied,
|
|
135
258
|
labor_charges,
|
|
136
259
|
labor_charges_amount,
|
|
137
260
|
freight_charges,
|