Files
weiguang-jewelry-ledger/新账系统/web/src/pages/PriceLocks/receipts.ts
T

51 lines
2.4 KiB
TypeScript

import { clearRecovered, detailTarget, record, type ReceiptHandler } from '../../lib/receipt';
export const lockDraftKey = (user: string) => 'mm-price-lock-draft-' + user;
export const lockEditKey = (user: string, id: string) => `mm-price-lock-edit-${user}-${id}`;
export const lockDeliveryKey = (user: string, id: string) => `mm-price-lock-delivery-${user}-${id}`;
// 池料草稿额外保存切回手动模式时的值,提交时只包含实际计价来源。
function lockInput(d: Record<string, unknown>) {
const input = { ...d };
for (const k of ['customer', 'salesperson']) if (typeof input[k] === 'string') input[k] = input[k].trim();
if (Array.isArray(input.lines))
input.lines = input.lines.map(line => {
if (!record(line) || !record(line.material) || line.material.source !== 'pool') return line;
const material = { ...line.material };
delete material.manual;
return { ...line, material };
});
return input;
}
export const lockReceipt: ReceiptHandler = {
accepts: url => /^\/api\/(?:price-locks|price-lock-changes)(?:\/|$)/.test(url),
finish: ({ job, result, state, storage }) => {
const path = job.url.split('/'),
action = path[4],
user = state.user.id,
data = record(job.data) ? job.data : {};
const id =
result.price_lock_id ||
(record(result.request) && result.request.price_lock_id) ||
(record(result.lock) && result.lock.id) ||
(path[2] === 'price-locks' && path[3] ? decodeURIComponent(path[3]) : result.id);
if (typeof id !== 'string' || !id) return {};
if (!action) clearRecovered(storage, lockDraftKey(user), job, lockInput);
else if (action === 'deliver') {
clearRecovered(storage, lockDeliveryKey(user, id), job, draft => draft);
return detailTarget('orders', 'order', result.order_id || result.id);
} else if (action === 'edit' || (action === 'change-requests' && data.action === 'edit')) {
clearRecovered(storage, lockEditKey(user, id), job, d =>
d.id === id && record(d.inputs)
? {
...lockInput(d.inputs),
revision: d.revision,
reason: typeof d.reason === 'string' ? d.reason.trim() : d.reason,
verified: true,
...(action === 'change-requests' ? { action: 'edit' } : {}),
}
: undefined,
);
}
return detailTarget('price-locks', 'lock', id);
},
};