NTP_V2/web/src/features/refinery/variables/meta.ts

86 lines
3.8 KiB
TypeScript

import type { FieldMeta, FormMeta, GridMeta, L10n } from '@/components/meta/types';
import strings from './strings';
import { GRID_VARIABLES, type VariablesRow } from './api';
const s = strings;
const p2 = (n: number) => String(n).padStart(2, '0');
const iso = (d: Date) => `${d.getFullYear()}-${p2(d.getMonth() + 1)}-${p2(d.getDate())}`;
/** First and last day of the current month as the grid's startDate / endDate filters. */
const monthRange = () => {
const now = new Date();
return { startDate: iso(new Date(now.getFullYear(), now.getMonth(), 1)), endDate: iso(new Date(now.getFullYear(), now.getMonth() + 1, 0)) };
};
/** Legacy REFINERY_VARIABLESGRID: period, then the prices; the filters are the two period dates. */
export function variablesGrid(h: { onOpen: (r: VariablesRow) => void }): GridMeta<VariablesRow> {
return {
key: GRID_VARIABLES,
endpoint: '/refinery/variables',
rowKey: (r) => r.groupId,
onRowClick: h.onOpen,
pageSize: 16,
emptyAsGrid: true,
// opens on the periods that start in the current month (first day .. last day)
defaultFilters: monthRange(),
filters: [
{ name: 'startDate', type: 'date', label: s['rv.filter.startDate'] },
{ name: 'endDate', type: 'date', label: s['rv.filter.endDate'] },
],
columns: [
{ key: 'startDate', header: s['rv.startDate'], format: 'date', ltr: true },
{ key: 'endDate', header: s['rv.endDate'], format: 'date', ltr: true },
{ key: 'dieselLiterPrice', header: s['rv.dieselLiterPrice'], format: 'number', ltr: true },
{ key: 'tonWage', header: s['rv.tonWage'], format: 'number', ltr: true },
{ key: 'tonPrice', header: s['rv.tonPrice'], format: 'number', ltr: true },
{ key: 'driverTip', header: s['rv.driverTip'], format: 'number', ltr: true, hideBelow: 'sm' },
{ key: 'permitDispatch', header: s['rv.permitDispatch'], format: 'number', ltr: true, hideBelow: 'md' },
{ key: 'waybillFee', header: s['rv.waybillFee'], format: 'number', ltr: true, hideBelow: 'md' },
{ key: 'commission', header: s['rv.commission'], format: 'number', ltr: true, hideBelow: 'lg' },
{ key: 'tolerancePercentage', header: s['rv.tolerancePercentage'], format: 'number', ltr: true, hideBelow: 'lg' },
],
};
}
const money = (name: string, label: L10n, extra: Partial<FieldMeta> = {}): FieldMeta => ({
name, type: 'number', label, required: true, min: 0, max: 9999999, placeholder: { en: '0.000', ar: '0.000' }, ...extra,
});
/** Legacy REFINERY_VARIABLESMain.html: every field is required. Diesel price (0, 2) and ton wage (0, 100) are checked by the server too. */
export function variablesForm(): FormMeta {
return {
key: 'refinery.variables.form',
sections: [
{
key: 'period',
title: s['rv.basic'],
columns: 2,
fields: [
{ name: 'startDate', type: 'date', label: s['rv.startDate'], required: true },
{ name: 'endDate', type: 'date', label: s['rv.endDate'], required: true },
money('dieselLiterPrice', s['rv.dieselLiterPrice'], { min: 0.001, max: 1.999 }),
money('firstAdvancePayment', s['rv.firstAdvancePayment']),
money('tonWage', s['rv.tonWage'], { min: 0.001, max: 99.999 }),
money('tonPrice', s['rv.tonPrice']),
money('driverTip', s['rv.driverTip']),
money('permitDispatch', s['rv.permitDispatch']),
money('waybillFee', s['rv.waybillFee']),
money('commission', s['rv.commission']),
money('tolerancePercentage', s['rv.tolerancePercentage']),
],
},
],
};
}
export function tonPriceCorrectionForm(): FormMeta {
return {
key: 'refinery.variables.tonPriceCorrection',
sections: [{
key: 'correction',
columns: 2,
fields: [money('tonPriceCorrection', s['rv.tonPriceCorrection'], { min: 0.001 })],
}],
};
}