All files / libs/payout-calculation/statements/views/list/src/lib/hooks use-user-statements-total.ts

82.35% Statements 28/34
28.57% Branches 2/7
100% Functions 1/1
82.35% Lines 28/34

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 351x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x             3x 3x 3x 3x 3x 3x 3x  
import { type PaymentAggregatedItem, type UserStatements } from '@amalia/core/types';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
import { getTotalFromStatementPlanVariable } from '@amalia/payout-calculation/statements/shared';
 
export const useUserStatementsTotal = ({
  userStatements,
  userPayments,
  isForecast = false,
  forceUseCommission = false,
}: {
  userStatements: UserStatements;
  userPayments?: PaymentAggregatedItem[];
  isForecast?: boolean;
  forceUseCommission?: boolean;
}) => {
  const totalFromPlan =
    !isForecast && !forceUseCommission && userStatements.lines.length === 1
      ? getTotalFromStatementPlanVariable(userStatements.lines[0])
      : undefined;
 
  return totalFromPlan
    ? {
        achievedValue: totalFromPlan.value || 0,
        forecastedValue: undefined,
        format: totalFromPlan.format,
        currencySymbol: userPayments?.at(0)?.currency || userStatements.currency,
      }
    : {
        achievedValue: (userPayments ?? []).reduce((acc, p) => acc + p.sum, 0),
        forecastedValue: userStatements.lines.reduce((acc, t) => acc + (t.forecast?.total ?? 0), 0),
        currencySymbol: userPayments?.at(0)?.currency || userStatements.currency,
        format: FormatsEnum.currency,
      };
};