import { __, _n, sprintf } from '@wordpress/i18n'; import { useState } from '@wordpress/element'; import { Button, Modal, Notice, Spinner } from '@wordpress/components'; import type { Claim } from '../types'; import { channelLabel, eventLabel, eventReason, formatDate, formatUtc, outcomeLabel, remedyLabel, reviewReasonLabel, stateLabel, } from '../labels'; import ActionForm from './ActionForm'; import type { Field } from './ActionForm'; import UnitsTable from './UnitsTable'; interface Props { claim: Claim | null; loading: boolean; /** Why the data was reloaded (null when it was not). */ staleNotice: string | null; onClose: () => void; onUpdated: ( updated: Claim ) => void; onReload: ( message?: string ) => void; } function reportedText( claim: Claim ): string { const date = formatDate( claim.reported_on ); if ( claim.state === 'closed' ) { return sprintf( /* translators: 1: date the defect was reported, 2: days until the claim was closed */ _n( 'Reported on %1$s, closed after %2$d day', 'Reported on %1$s, closed after %2$d days', claim.days_open, 'abcreate-warranty-desk' ), date, claim.days_open ); } if ( claim.days_open === 0 ) { /* translators: %s: date the defect was reported */ return sprintf( __( 'Reported today, %s', 'abcreate-warranty-desk' ), date ); } return sprintf( /* translators: 1: date the defect was reported, 2: days since then */ _n( 'Reported on %1$s, %2$d day ago', 'Reported on %1$s, %2$d days ago', claim.days_open, 'abcreate-warranty-desk' ), date, claim.days_open ); } const REFUND_REASONS = [ '@order_refunded', '@order_cancelled' ]; export default function ClaimDetail( { claim, loading, staleNotice, onClose, onUpdated, onReload, }: Props ) { const title = claim ? `#${ claim.id } · ${ claim.product_name || __( '(deleted product)', 'abcreate-warranty-desk' ) } · ${ sprintf( /* translators: %s: order number */ __( 'order #%s', 'abcreate-warranty-desk' ), claim.order_number ) }${ claim.customer_name ? ` · ${ claim.customer_name }` : '' }` : __( 'Claim', 'abcreate-warranty-desk' ); return ( { ! claim && loading && } { claim && ( <> { staleNotice !== null && ( { staleNotice || __( 'The claim changed meanwhile. The latest data is shown below.', 'abcreate-warranty-desk' ) } ) }
{ stateLabel( claim.state ) } { claim.outcome && ( { outcomeLabel( claim.outcome ) } ) } { reportedText( claim ) } { __( 'Open order', 'abcreate-warranty-desk' ) }
{ claim.overdue && ( { sprintf( /* translators: %d: target days set by the shop */ __( 'Open for longer than your target of %d days. The law asks for a remedy within a reasonable time.', 'abcreate-warranty-desk' ), claim.target_days ) } ) }

{ claim.defect }

{ claim.state === 'needs_review' && ( { __( 'Why it is in review', 'abcreate-warranty-desk' ) }
    { claim.review_reasons.map( ( reason ) => (
  • { reviewReasonLabel( reason ) }
  • ) ) }
) }

{ __( 'Units and dates', 'abcreate-warranty-desk' ) }

onReload( '' ) } onStale={ onReload } /> { window.abcreateWd.canManage && ( ) }

{ __( 'History', 'abcreate-warranty-desk' ) }

    { ( claim.history ?? [] ).map( ( event ) => (
  1. { formatUtc( event.occurred_at ) } { eventLabel( event ) } { event.actor_name && ( { ' ' } · { event.actor_name } ) } { event.external_ref && ! event.external_ref.startsWith( 'claim:' ) && ( { ' ' } · { event.external_ref } ) } { eventReason( event ) && (
    { eventReason( event ) }
    ) } { typeof event.after?.informativa_text === 'string' && event.after.informativa_text !== '' && (
    { event.after.informativa_to ? sprintf( /* translators: %s: email address */ __( 'Text given, to %s', 'abcreate-warranty-desk' ), String( event.after .informativa_to ) ) : __( 'Text given', 'abcreate-warranty-desk' ) }
    												{ String(
    													event.after.informativa_text
    												) }
    											
    ) }
  2. ) ) }
) }
); } function NextSteps( { claim, onUpdated, onReload, }: { claim: Claim; onUpdated: ( c: Claim ) => void; onReload: ( m?: string ) => void; } ) { const [ more, setMore ] = useState( false ); const [ changeChoice, setChangeChoice ] = useState( false ); const [ lending, setLending ] = useState( false ); const done = ( result: unknown ) => { setChangeChoice( false ); setLending( false ); onUpdated( result as Claim ); }; const common = { target: { claim }, onDone: done, onStale: onReload }; const refundOptions = ( claim.refunds ?? [] ).map( ( r ) => { let lines = ''; if ( r.lines.length > 0 ) { lines = r.this_line ? ` · ${ __( 'this line', 'abcreate-warranty-desk' ) }` : ` · ${ sprintf( /* translators: %s: names of other order lines */ __( 'other line: %s', 'abcreate-warranty-desk' ), r.lines.join( ', ' ) ) }`; } return { value: String( r.id ), label: `#${ r.id } · ${ r.amount } · ${ r.date }${ r.reason ? ` · ${ r.reason }` : '' }${ lines }`, }; } ); const noRefund = refundOptions.length === 0; const someOtherLine = ( claim.refunds ?? [] ).some( ( r ) => r.other_lines && ! r.this_line ); const refundForm = noRefund ? (

{ __( 'Close with a WooCommerce refund', 'abcreate-warranty-desk' ) }

{ __( 'Make the refund in WooCommerce first (order screen, Refund button), then come back: the claim keeps only its number.', 'abcreate-warranty-desk' ) }

) : ( { __( 'Price reduction or termination of the contract (art. 135-bis c. 4 and 5). The money moves only through the WooCommerce refund.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Close the claim', 'abcreate-warranty-desk' ) } secondary fields={ [ { name: 'outcome', label: __( 'Outcome', 'abcreate-warranty-desk' ), type: 'select', options: [ { value: 'price_reduced', label: outcomeLabel( 'price_reduced' ), }, { value: 'terminated', label: outcomeLabel( 'terminated' ), }, ], }, { name: 'refund_id', label: __( 'Refund', 'abcreate-warranty-desk' ), type: 'select', options: refundOptions, }, ...( someOtherLine ? [ { name: 'confirm_other_line', label: __( 'If the refund is for another line: I checked it is the right one', 'abcreate-warranty-desk' ), type: 'checkbox' as const, }, ] : [] ), { name: 'defect_not_minor', label: __( 'The defect is not minor (needed for a termination)', 'abcreate-warranty-desk' ), type: 'checkbox', }, { name: 'date', label: __( 'Date', 'abcreate-warranty-desk' ), type: 'date', required: true, }, { name: 'reason', label: __( 'Reason', 'abcreate-warranty-desk' ), type: 'text', required: true, }, ] } /> ); const withoutRemedy = ( outcomes: Field[ 'options' ] ) => ( { __( '"Out of warranty" needs a confirmed delivery and a defect that appeared after the end of the guarantee (record the day above). A late report alone is not enough. Prescription never closes a claim on its own.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Close the claim', 'abcreate-warranty-desk' ) } secondary fields={ [ { name: 'outcome', label: __( 'Outcome', 'abcreate-warranty-desk' ), type: 'select', options: outcomes, help: __( '"Request withdrawn" means the customer dropped this guarantee request. It is not the 14-day right of withdrawal from the purchase: handle that as a return in WooCommerce.', 'abcreate-warranty-desk' ), }, { name: 'date', label: __( 'Date', 'abcreate-warranty-desk' ), type: 'date', required: true, }, { name: 'reason', label: __( 'Reason given to the customer', 'abcreate-warranty-desk' ), type: 'textarea', required: true, }, ] } /> ); if ( claim.state === 'needs_review' ) { const settledByRefund = claim.outcome === null && claim.review_reasons.length > 0 && claim.review_reasons.every( ( r ) => REFUND_REASONS.includes( r ) ); return (

{ __( 'Next step', 'abcreate-warranty-desk' ) }

{ __( 'Fix what the reasons above say (on the units, or in WooCommerce), then reconcile.', 'abcreate-warranty-desk' ) }

} extra={ { seen_facts: claim.order_now?.token ?? '' } } submitLabel={ __( 'Back to work', 'abcreate-warranty-desk' ) } fields={ [ ...( claim.review_reasons.includes( '@order_refunded' ) ? [ { name: 'goods_kept', label: __( 'The refund left the goods with the customer (a discount): the units are still there', 'abcreate-warranty-desk' ), type: 'checkbox' as const, }, ] : [] ), { name: 'reason', label: __( 'What you checked', 'abcreate-warranty-desk' ), type: 'text', required: true, }, ] } /> { claim.review_reasons.length === 1 && claim.review_reasons[ 0 ] === '@informativa_uncertain' && ( ) } { settledByRefund && ( <> { refundForm } { withoutRemedy( [ { value: 'withdrawn', label: outcomeLabel( 'withdrawn' ), }, ] ) } ) }
); } if ( claim.state === 'closed' ) { return (

{ __( 'After closing', 'abcreate-warranty-desk' ) }

{ claim.loaner_out && ( ) } { claim.outcome !== 'terminated' && ( { __( 'The claim goes to review and the units are held again. A repaired unit gets no second extension.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Reopen for the defect', 'abcreate-warranty-desk' ) } secondary fields={ [ { name: 'reason', label: __( 'What the customer reported', 'abcreate-warranty-desk' ), type: 'text', required: true, }, ] } /> ) } { __( 'A repair or a termination recorded by mistake is revoked with a compensating event; the history keeps both.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Reopen', 'abcreate-warranty-desk' ) } secondary fields={ [ { name: 'reason', label: __( 'What was wrong', 'abcreate-warranty-desk' ), type: 'text', required: true, }, ] } />
); } // Open claim: the steps of art. 135-bis and 135-ter, in order. const informed = claim.informed_on !== null; return (

{ __( 'Next steps', 'abcreate-warranty-desk' ) }

{ claim.informativa_required ? __( '1. Inform the consumer', 'abcreate-warranty-desk' ) : __( '1. Informativa (not required for this contract)', 'abcreate-warranty-desk' ) }

{ ! claim.informativa_required && (

{ __( 'Not required for this claim: the contract is older than 31 July 2026, the buyer is not a consumer, or the defect appeared after the guarantee. A repair brings no extension.', 'abcreate-warranty-desk' ) }

) } { informed && (

{ sprintf( /* translators: 1: date, 2: channel (email, phone…), 3: version of the text */ __( 'Informed on %1$s (%2$s, text %3$s).', 'abcreate-warranty-desk' ), formatDate( claim.informed_on ), channelLabel( claim.informed_via ), claim.informativa_version ?? '' ) } { claim.informed_via === 'email' && ` ${ __( 'The email was accepted by the mail system; delivery cannot be confirmed.', 'abcreate-warranty-desk' ) }` }

) } { claim.informativa_required && ! informed && ( <> { sprintf( /* translators: %s: email address */ __( 'Preview of the email to %s', 'abcreate-warranty-desk' ), claim.informativa.to || __( '(no email on the order)', 'abcreate-warranty-desk' ) ) } { claim.informativa.subject }
{ claim.informativa.body }
) : null } submitLabel={ __( 'Send email', 'abcreate-warranty-desk' ) } fields={ [] } /> ) }

{ __( "2. The consumer's choice", 'abcreate-warranty-desk' ) }

{ claim.choice && (

{ sprintf( /* translators: %s: repair or replacement */ __( 'Chosen: %s.', 'abcreate-warranty-desk' ), remedyLabel( claim.choice ) ) } { claim.remedy_refused && ` ${ __( 'Both remedies were refused by the shop.', 'abcreate-warranty-desk' ) }` }

) } { claim.choice && ! claim.remedy_refused && ! changeChoice && ( ) } { ( ! claim.choice || claim.remedy_refused || changeChoice ) && ( ) } { claim.choice === 'replacement' && ! claim.refurbished_requested && ( ) }

{ __( '3. Goods and loan (optional)', 'abcreate-warranty-desk' ) }

{ claim.goods_received_on ? (

{ sprintf( /* translators: %s: date */ __( 'Goods received on %s.', 'abcreate-warranty-desk' ), formatDate( claim.goods_received_on ) ) }

) : ( ) } { claim.loaner_out && ( ) } { ! claim.loaner_out && ! lending && (
) } { ! claim.loaner_out && lending && ( ) }

{ __( '4. Remedy done', 'abcreate-warranty-desk' ) }

{ ! claim.choice && (

{ __( "Record the consumer's choice first.", 'abcreate-warranty-desk' ) }

) } { claim.choice === 'repair' && ( u.new_rules ) ? (

{ __( 'The guarantee of the units is extended by 12 months, once, if the buyer is a consumer and the defect appeared within the guarantee.', 'abcreate-warranty-desk' ) }

) : null } submitLabel={ __( 'Record repair', 'abcreate-warranty-desk' ) } fields={ [ { name: 'date', label: __( 'Date', 'abcreate-warranty-desk' ), type: 'date', required: true, }, { name: 'reason', label: __( 'What was done', 'abcreate-warranty-desk' ), type: 'text', }, ] } /> ) } { claim.choice === 'replacement' && ( { __( 'A replacement brings no 12-month extension. Whether the guarantee starts again for the new good is not settled by the law: the plugin does not decide it. If you grant a new end, write it below; leave it empty to keep the current end.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Record replacement', 'abcreate-warranty-desk' ) } fields={ [ { name: 'date', label: __( 'Date', 'abcreate-warranty-desk' ), type: 'date', required: true, }, ...( claim.refurbished_requested ? [ { name: 'refurbished', label: __( 'The replacement is refurbished', 'abcreate-warranty-desk' ), type: 'checkbox' as const, }, ] : [] ), { name: 'new_end', label: __( 'New end of the guarantee (optional)', 'abcreate-warranty-desk' ), type: 'date', allowFuture: true, help: __( 'Your decision, for the replaced units.', 'abcreate-warranty-desk' ), }, { name: 'reason', label: __( 'Note', 'abcreate-warranty-desk' ), type: 'text', }, ] } /> ) }
{ more && ( <> { ! claim.remedy_refused && ( { __( 'Only when both are impossible or disproportionately expensive (art. 135-bis c. 3). The consumer can then have a price reduction or terminate the contract.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Record refusal', 'abcreate-warranty-desk' ) } secondary fields={ [ { name: 'reason', label: __( 'Written reason', 'abcreate-warranty-desk' ), type: 'textarea', required: true, }, ] } /> ) } { refundForm } { withoutRemedy( [ { value: 'rejected_not_covered', label: outcomeLabel( 'rejected_not_covered' ), }, { value: 'rejected_out_of_warranty', label: outcomeLabel( 'rejected_out_of_warranty' ), }, { value: 'rejected_not_consumer', label: outcomeLabel( 'rejected_not_consumer' ), }, { value: 'withdrawn', label: outcomeLabel( 'withdrawn' ), }, ] ) } ) }
); } /** * When the defect appeared (art. 133 c. 1): the guarantee looks at this day. * @param root0 * @param root0.claim * @param root0.onUpdated * @param root0.onReload */ function Manifestation( { claim, onUpdated, onReload, }: { claim: Claim; onUpdated: ( c: Claim ) => void; onReload: ( m?: string ) => void; } ) { const needed = claim.review_reasons.includes( '@manifestation_unknown' ); const [ editing, setEditing ] = useState( false ); const canEdit = window.abcreateWd.canManage && claim.state !== 'closed'; return (
{ claim.manifested_on ? sprintf( /* translators: %s: date the defect appeared */ __( 'The defect appeared on %s.', 'abcreate-warranty-desk' ), formatDate( claim.manifested_on ) ) : __( 'When the defect appeared: not recorded.', 'abcreate-warranty-desk' ) } { ' ' } { canEdit && ! editing && ! needed && ( ) } { canEdit && ( editing || needed ) && ( { __( 'As the customer declares it. It can be before the report, never after. It decides "out of warranty" and the extension.', 'abcreate-warranty-desk' ) }

} submitLabel={ __( 'Save', 'abcreate-warranty-desk' ) } secondary={ ! needed } fields={ [ { name: 'date', label: __( 'Appeared on', 'abcreate-warranty-desk' ), type: 'date', required: true, initial: claim.manifested_on ?? '', }, { name: 'reason', label: __( 'Source (what the customer said, when)', 'abcreate-warranty-desk' ), type: 'text', required: true, }, ] } onDone={ ( result ) => { setEditing( false ); onUpdated( result as Claim ); } } onStale={ onReload } /> ) }
); } /** * The order as WooCommerce has it now: reconciling accepts exactly this. * @param root0 * @param root0.claim */ function OrderNow( { claim }: { claim: Claim } ) { const now = claim.order_now; if ( ! now ) { return null; } let line: string = sprintf( /* translators: 1: product name, 2: quantity */ __( '%1$s × %2$d', 'abcreate-warranty-desk' ), now.product, now.quantity ); if ( now.order_deleted ) { line = __( 'order deleted', 'abcreate-warranty-desk' ); } else if ( now.line_removed ) { line = __( 'line removed from the order', 'abcreate-warranty-desk' ); } return (

{ sprintf( /* translators: 1: order status, 2: product and quantity of the line */ __( 'WooCommerce now: %1$s, %2$s. Reconciling accepts the order as shown here; if it changes meanwhile you will be asked to check again.', 'abcreate-warranty-desk' ), now.status || '-', line ) }

); }