All files / src/pages/flowsheet-page/economics/cost-items-panel/depreciation CapitalDepreciationControls.tsx

63.88% Statements 23/36
48.97% Branches 24/49
20% Functions 1/5
67.85% Lines 19/28

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                                  141x                               141x 141x 141x         150x                                                                               516x   9x 141x 150x 9x                   141x 641x 9x 141x   159x 516x 125x                                                                     1141x 391x      
import { Label } from "@/ahuora-design-system/ui/label";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/ahuora-design-system/ui/select";
import {
  DepreciationModeEnum,
  type PatchedCapitalCostLine,
} from "@/api/apiStore.gen";
import { ValueWithFixedUnitField } from "../../shared/ui/EconomicsFormFields";
import type { CapitalDepreciationDraft } from "../model/capitalDepreciationDraft";
import type { FieldMessages } from "../model/capitalLineTypes";
import { FieldError } from "../ui/CapitalLineUi";
 
export function CapitalDepreciationControls({
  idPrefix,
  draft,
  disabled,
  fieldMessages,
  onPatch,
}: {
  idPrefix: string;
  draft: CapitalDepreciationDraft;
  disabled?: boolean;
  fieldMessages: FieldMessages;
  onPatch: (
    patch: PatchedCapitalCostLine,
    draftPatch: Partial<CapitalDepreciationDraft>,
  ) => void;
}) {
  const mode = draft.depreciation_mode;
  const custom = mode === DepreciationModeEnum.Custom;
  const modeId = `${idPrefix}-depreciation-mode`;
 
  return (
    <div className="contents">
      <div className="space-y-2">
        <Label htmlFor={modeId}>Depreciation treatment</Label>
        <Select
          value={mode}
          disabled={disabled}
          onValueChange={(value) => {
            const depreciationMode = value as DepreciationModeEnum;
            if (depreciationMode === DepreciationModeEnum.Custom) {
              const draftLifeYears = Number(draft.depreciation_life_years);
              const lifeYears =
                Number.isFinite(draftLifeYears) && draftLifeYears > 0
                  ? draftLifeYears
                  : 10;
              onPatch(
                {
                  depreciation_mode: depreciationMode,
                  depreciation_life_years: lifeYears,
                  depreciation_salvage_percent:
                    draft.depreciation_salvage_percent || "0.0000",
                },
                {
                  depreciation_mode: depreciationMode,
                  depreciation_life_years: String(lifeYears),
                  depreciation_salvage_percent:
                    draft.depreciation_salvage_percent || "0.0000",
                },
              );
              return;
            }
            onPatch(
              {
                depreciation_mode: depreciationMode,
                depreciation_life_years: null,
                depreciation_salvage_percent: null,
              },
              {
                depreciation_mode: depreciationMode,
                depreciation_life_years: "",
                depreciation_salvage_percent: "",
              },
            );
          }}
        >
          <SelectTrigger id={modeId}>
            <SelectValue />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value={DepreciationModeEnum.StudyDefault}>
              Use study default
            </SelectItem>
            <SelectItem value={DepreciationModeEnum.Custom}>
              Custom equipment life
            </SelectItem>
            <SelectItem value={DepreciationModeEnum.Excluded}>
              Exclude from depreciation
            </SelectItem>
          </SelectContent>
        </Select>
        <FieldError
          id={`${modeId}-error`}
          message={fieldMessages.depreciation_mode}
        />
      </div>
      {custom ? (
        <>
          <ValueWithFixedUnitField
            id={`${idPrefix}-depreciation-life`}
            label="Equipment life"
            value={draft.depreciation_life_years}
            unit="years"
            unitDisplay="compact-field"
            disabled={disabled}
            error={fieldMessages.depreciation_life_years}
            errorTone="destructive"
            onValueChange={(value) =>
              onPatch(
                { depreciation_life_years: value ? Number(value) : null },
                { depreciation_life_years: value },
              )
            }
          />
          <ValueWithFixedUnitField
            id={`${idPrefix}-depreciation-salvage`}
            label="Residual value"
            value={draft.depreciation_salvage_percent}
            unit="%"
            unitDisplay="suffix"
            disabled={disabled}
            error={fieldMessages.depreciation_salvage_percent}
            errorTone="destructive"
            onValueChange={(value) =>
              onPatch(
                { depreciation_salvage_percent: value || null },
                { depreciation_salvage_percent: value },
              )
            }
          />
        </>
      ) : null}
    </div>
  );
}