import { __, sprintf } from '@wordpress/i18n'; import { Button } from '@wordpress/components'; import { useState } from '@wordpress/element'; import type { Unit } from '../types'; import { consumerLabel, deliveryLabel, formatDate } from '../labels'; import ActionForm from './ActionForm'; interface Props { units: Unit[]; /** Show the operator's actions on tracked units. */ editable?: boolean; onChanged?: () => void; onStale?: ( message?: string ) => void; } /** * The dates of each unit: the heart of the legal guarantee (spec section 2). * @param root0 * @param root0.units * @param root0.editable * @param root0.onChanged * @param root0.onStale */ export default function UnitsTable( { units, editable, onChanged, onStale, }: Props ) { const [ editing, setEditing ] = useState< { id: number; what: string; } | null >( null ); const canEdit = editable && window.abcreateWd.canManage; return ( { units.map( ( unit ) => { const dates = unit.dates; const isEditing = canEdit && unit.id !== null && editing?.id === unit.id; return [ , isEditing ? ( ) : null, ]; } ) }
{ __( 'Unit', 'abcreate-warranty-desk' ) } { __( 'Delivery', 'abcreate-warranty-desk' ) } { __( 'Buyer', 'abcreate-warranty-desk' ) } { __( 'Covered until', 'abcreate-warranty-desk' ) } { __( 'Presumption until', 'abcreate-warranty-desk' ) } { __( 'Prescription', 'abcreate-warranty-desk' ) }
{ unit.unit_index } { unit.terminated_by_claim_id && (
{ __( 'returned', 'abcreate-warranty-desk' ) }
) }
{ unit.delivered_on ? formatDate( unit.delivered_on ) : '' }{ ' ' } ({ deliveryLabel( unit.delivery_source ) }) { canEdit && unit.id !== null && (
) }
{ consumerLabel( unit.consumer ) } { canEdit && unit.id !== null && (
) }
{ dates ? ( { formatDate( dates.guarantee_end ) } ) : ( { __( 'hidden until delivery and buyer are known', 'abcreate-warranty-desk' ) } ) } { dates?.extended && (
{ __( '+12 months after a repair', 'abcreate-warranty-desk' ) }
) } { unit.operator_end_on && (
{ __( 'lengthened by the shop', 'abcreate-warranty-desk' ) }
) } { unit.guarantee_months < 24 && (
{ sprintf( /* translators: %d: months of guarantee for used goods */ __( 'used goods, %d months', 'abcreate-warranty-desk' ), unit.guarantee_months ) }
) }
{ sprintf( /* translators: %s: date the contract was concluded */ __( 'contract of %s', 'abcreate-warranty-desk' ), formatDate( unit.contract_date ) ) } { ! unit.new_rules && ` · ${ __( 'before 31/07/2026', 'abcreate-warranty-desk' ) }` }
{ ( unit.ambiguous_repair_claims ?? [] ) .length > 0 && (
{ sprintf( /* translators: %s: claim numbers */ __( 'A repair (claim %s) was recorded with the report date only, after the guarantee, so it gave no extension. If the defect appeared in time it should have: ask the customer, then reopen that claim as closed by mistake, record the day and the repair again.', 'abcreate-warranty-desk' ), unit.ambiguous_repair_claims .map( ( id ) => `#${ id }` ) .join( ', ' ) ) }
) } { unit.repair_count > 0 && (
{ sprintf( /* translators: %d: number of repairs */ __( 'repairs: %d', 'abcreate-warranty-desk' ), unit.repair_count ) }
) } { canEdit && unit.id !== null && dates && (
) } { canEdit && unit.id !== null && (
) }
{ dates ? formatDate( dates.presumption_end ) : '' } { dates ? formatDate( dates.prescription_end ) : '' } { dates?.prescription_before_guarantee_end && (
{ __( 'Falls before the end of the guarantee: the claim is never closed for this.', 'abcreate-warranty-desk' ) }
) }
{ setEditing( null ); onChanged?.(); } } onStale={ ( m ) => { setEditing( null ); onStale?.( m ); } } />
); } function UnitForm( { unit, what, onDone, onStale, }: { unit: Unit; what: string; onDone: () => void; onStale: ( m?: string ) => void; } ) { if ( what === 'set_delivery' ) { return ( ); } if ( what === 'set_consumer' ) { return ( ); } if ( what === 'set_contract' ) { return ( <> { __( 'Taken from the order date. Correct it for imported orders or orders created before the sale: the new rules apply to contracts from 31 July 2026.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Save', 'abcreate-warranty-desk' ) } fields={ [ { name: 'date', label: __( 'Contract date', 'abcreate-warranty-desk' ), type: 'date', required: true, initial: unit.contract_date, help: __( 'The day the sale was agreed.', 'abcreate-warranty-desk' ), }, { name: 'reason', label: __( 'Proof (signed order, import, receipt…)', 'abcreate-warranty-desk' ), type: 'text', required: true, }, ] } onDone={ onDone } onStale={ onStale } /> { __( '24 months. For used goods it can be shorter, down to 12 months, only if agreed with the buyer (art. 133 c. 4): write where the agreement is.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Save', 'abcreate-warranty-desk' ) } secondary fields={ [ { name: 'months', label: __( 'Months', 'abcreate-warranty-desk' ), type: 'select', initial: String( unit.guarantee_months ), options: [ 24, 18, 12 ] .concat( [ 24, 18, 12 ].includes( unit.guarantee_months ) ? [] : [ unit.guarantee_months ] ) .map( ( m ) => ( { value: String( m ), label: String( m ), } ) ), }, { name: 'reason', label: __( 'Where the agreement is', 'abcreate-warranty-desk' ), type: 'text', required: true, }, ] } onDone={ onDone } onStale={ onStale } /> ); } return ( { __( 'For example after a replacement. The shop can lengthen the legal guarantee, never shorten it. Leave the date empty to remove it.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Save', 'abcreate-warranty-desk' ) } fields={ [ { name: 'date', label: __( 'Covered until', 'abcreate-warranty-desk' ), type: 'date', allowFuture: true, initial: unit.operator_end_on ?? '', }, { name: 'reason', label: __( 'Reason', 'abcreate-warranty-desk' ), type: 'text', required: true, }, ] } onDone={ onDone } onStale={ onStale } /> ); }