All files / libs/payout-calculation/payments/views/commission-report/src/lib/split PaymentSplitModalForm.tsx

97.6% Statements 163/167
78.94% Branches 15/19
100% Functions 0/0
97.6% Lines 163/167

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 1681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 6x 6x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 1x 3x 3x 3x 5x 5x 5x 5x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x     5x     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 4x 4x 5x 5x 5x 5x 5x 5x 1x  
import { css } from '@emotion/react';
import { Form } from 'formik';
import { last, orderBy, sum, uniq } from 'lodash-es';
import { memo, useCallback, useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
 
import { FormFieldError, Modal, Stack, Typography, type SelectOption } from '@allshares/studio-design-system';
import { type PaymentContract } from '@amalia/core/types';
import { type PeriodsMap } from '@amalia/payout-definition/periods/types';
 
import { OriginalPaymentDataGrid } from './original-payment-datagrid/OriginalPaymentDataGrid';
import { SplitPaymentsDatagrid } from './split-payments-datagrid/SplitPaymentsDatagrid';
import { type PaymentSplitPartWithPercent } from './types';
 
export interface PaymentSplitFormValues {
  splitParts: PaymentSplitPartWithPercent[];
}
 
interface PaymentSplitModalFormProps {
  // Data
  readonly originalPayment: PaymentContract;
  readonly isSelectedPaymentSplit: boolean;
  readonly periodsMap: PeriodsMap;
  readonly readonly: boolean;
 
  // Controls
  readonly isOpen: boolean;
  readonly onClose: () => void;
 
  // Form
  readonly isValid: boolean;
  readonly values: PaymentSplitFormValues;
  readonly setFieldValue: (field: string, value: unknown) => void;
}
 
export const PaymentSplitModalForm = memo(function PaymentSplitModalForm({
  originalPayment,
  isSelectedPaymentSplit,
  periodsMap,
  readonly,
  isOpen,
  onClose,
  isValid,
  values,
  setFieldValue,
}: PaymentSplitModalFormProps) {
  const periodOptions: SelectOption<string>[] = useMemo(
    () =>
      orderBy(Object.values(periodsMap), 'startDate').map((elt) => ({
        label: elt.name,
        value: elt.id,
      })),
    [periodsMap],
  );
 
  const { originalPaymentPeriodName, originalPaymentTotalValue } = useMemo(() => {
    const totalValue = originalPayment.totalValue || originalPayment.value;
 
    return {
      originalPaymentPeriodName:
        !!originalPayment.paymentPeriodId && originalPayment.paymentPeriodId in periodsMap
          ? periodsMap[originalPayment.paymentPeriodId].name
          : null,
      originalPaymentTotalValue: totalValue,
    };
  }, [originalPayment, periodsMap]);
 
  const setSplitPartsValue = useCallback(
    (newParts: PaymentSplitPartWithPercent[]) => {
      setFieldValue('splitParts', newParts);
      const sumOfSplitParts = sum(newParts.map((p) => p.value));
      setFieldValue('newValue', originalPaymentTotalValue - sumOfSplitParts);
    },
    [setFieldValue, originalPaymentTotalValue],
  );
 
  const onAddPart = useCallback(() => {
    setFieldValue(
      'splitParts',
      values.splitParts.concat({
        value: 0,
        percent: 0,
        paymentPeriodId: last(periodOptions)!.value,
      }),
    );
  }, [values, setFieldValue, periodOptions]);
 
  const error = useMemo(() => {
    if (values.splitParts.some((p) => p.paymentPeriodId === originalPayment.paymentPeriodId)) {
      return <FormattedMessage defaultMessage="One payment has the same payment period than the original payment" />;
    }
    if (uniq(values.splitParts.map((p) => p.paymentPeriodId)).length !== values.splitParts.length) {
      return <FormattedMessage defaultMessage="Some parts have the same payment period" />;
    }
 
    return '';
  }, [values, originalPayment]);
 
  return (
    <Modal
      isOpen={!!isOpen}
      size="large"
      onClose={onClose}
    >
      <Form
        css={css`
          display: contents;
        `}
      >
        <Modal.Content>
          <Modal.Header>
            <Modal.Title>
              <FormattedMessage defaultMessage="Split a payment" />
            </Modal.Title>
          </Modal.Header>
 
          <Modal.Body>
            <Stack gap={16}>
              <Stack gap={16}>
                <Typography variant="bodyLargeMedium">
                  <FormattedMessage defaultMessage="Original payment" />
                </Typography>
 
                <OriginalPaymentDataGrid
                  originalPayment={originalPayment}
                  originalPaymentPeriodName={originalPaymentPeriodName}
                />
              </Stack>
 
              <Stack gap={16}>
                <Typography variant="bodyLargeMedium">
                  <FormattedMessage defaultMessage="Split payment" />
                </Typography>
 
                <SplitPaymentsDatagrid
                  originalPayment={originalPayment}
                  periodOptions={periodOptions}
                  periodsMap={periodsMap}
                  readonly={readonly}
                  splitParts={values.splitParts}
                  onAddPart={onAddPart}
                  onChange={setSplitPartsValue}
                />
              </Stack>
 
              {!!error && <FormFieldError>{error}</FormFieldError>}
            </Stack>
          </Modal.Body>
        </Modal.Content>
        <Modal.Actions>
          <Modal.CancelAction />
 
          <Modal.MainAction
            disabled={!isValid || !!error}
            type="submit"
          >
            {isSelectedPaymentSplit ? (
              <FormattedMessage defaultMessage="Merge" />
            ) : (
              <FormattedMessage defaultMessage="Split" />
            )}
          </Modal.MainAction>
        </Modal.Actions>
      </Form>
    </Modal>
  );
});