diff --git a/src/leases/components/leaseSections/rent/BasicInfo.tsx b/src/leases/components/leaseSections/rent/BasicInfo.tsx index bf44e0d7..3ed24512 100644 --- a/src/leases/components/leaseSections/rent/BasicInfo.tsx +++ b/src/leases/components/leaseSections/rent/BasicInfo.tsx @@ -6,11 +6,11 @@ import FormText from "@/components/form/FormText"; import FormTextTitle from "@/components/form/FormTextTitle"; import { LeaseRentDueDatesFieldPaths, LeaseRentDueDatesFieldTitles, LeaseRentsFieldPaths, LeaseRentsFieldTitles, RentCycles, RentTypes, RentDueDateTypes, LeaseFieldPaths } from "@/leases/enums"; import { formatDueDates, formatSeasonalDate, sortDueDates } from "@/leases/helpers"; +import { showOverrideReceivableTypeField } from "@/leases/helpers"; import { getUiDataLeaseKey } from "@/uiData/helpers"; import { formatDate, formatNumber, getFieldOptions, getLabelOfOption, isEmptyValue, isFieldAllowedToRead } from "@/util/helpers"; import { getAttributes as getLeaseAttributes } from "@/leases/selectors"; import type { Attributes } from "types"; -import { getReceivableTypes } from "@/leaseCreateCharge/selectors"; type SeasonalDatesProps = { leaseAttributes: Attributes; rent: Record; @@ -36,17 +36,19 @@ const SeasonalDates = ({ type Props = { leaseAttributes: Attributes; - receivableTypes?: Array | null | undefined; - receivableTypeOptions?: Array | null | undefined; rent: Record; rentType: string | null | undefined; }; +type PropsWithServiceUnitId = Props & { + serviceUnitId: number; +} + const BasicInfoIndexOrManual = ({ leaseAttributes, - receivableTypeOptions, - rent -}: Props) => { + rent, + serviceUnitId, +}: PropsWithServiceUnitId) => { const areOldInfoVisible = () => { return !isEmptyValue(rent.elementary_index) || !isEmptyValue(rent.index_rounding) || !isEmptyValue(rent.x_value) || !isEmptyValue(rent.y_value) || !isEmptyValue(rent.y_value_start) || !isEmptyValue(rent.equalization_start_date) || !isEmptyValue(rent.equalization_end_date); }; @@ -55,6 +57,7 @@ const BasicInfoIndexOrManual = ({ const cycleOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.CYCLE); const indexTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.INDEX_TYPE); const dueDatesTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.DUE_DATES_TYPE); + const receivableTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE); const oldValuesVisible = areOldInfoVisible(); return @@ -191,17 +194,18 @@ const BasicInfoIndexOrManual = ({ - + { showOverrideReceivableTypeField(serviceUnitId) && {LeaseRentsFieldTitles.OVERRIDE_RECEIVABLE_TYPE} - {getLabelOfOption(receivableTypeOptions, rent?.override_receivable_type) || '-'} + {getLabelOfOption(receivableTypeOptions, rent.override_receivable_type) || '-'} - + + } {oldValuesVisible && @@ -335,12 +339,13 @@ const BasicInfoOneTime = ({ const BasicInfoFixed = ({ leaseAttributes, - receivableTypeOptions, - rent -}: Props) => { + rent, + serviceUnitId, +}: PropsWithServiceUnitId) => { const dueDatesTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.DUE_DATES_TYPE); const typeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.TYPE); - + const receivableTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE); + return @@ -433,16 +438,19 @@ const BasicInfoFixed = ({ + + { showOverrideReceivableTypeField(serviceUnitId) && {LeaseRentsFieldTitles.OVERRIDE_RECEIVABLE_TYPE} - {getLabelOfOption(receivableTypeOptions, rent?.override_receivable_type) || '-'} + {getLabelOfOption(receivableTypeOptions, rent.override_receivable_type) || '-'} + } ; }; @@ -498,18 +506,18 @@ const BasicInfoFree = ({ ; }; + const BasicInfo = ({ leaseAttributes, - receivableTypes, rent, - rentType -}: Props) => { - const receivableTypeOptions = receivableTypes?.map((rt) => ({ value: rt.id, label: rt.name })) || []; + rentType, + serviceUnitId, +}: PropsWithServiceUnitId) => { return {!rentType && Vuokralajia ei ole valittu} - {(rentType === RentTypes.INDEX || rentType === RentTypes.INDEX2022 || rentType === RentTypes.MANUAL) && } - {rentType === RentTypes.ONE_TIME && } - {rentType === RentTypes.FIXED && } + {(rentType === RentTypes.INDEX || rentType === RentTypes.INDEX2022 || rentType === RentTypes.MANUAL) && } + {rentType === RentTypes.ONE_TIME && } + {rentType === RentTypes.FIXED && } {rentType === RentTypes.FREE && } ; }; @@ -517,6 +525,5 @@ const BasicInfo = ({ export default connect(state => { return { leaseAttributes: getLeaseAttributes(state), - receivableTypes: getReceivableTypes(state) }; -})(BasicInfo); \ No newline at end of file +})(BasicInfo); diff --git a/src/leases/components/leaseSections/rent/BasicInfoEdit.tsx b/src/leases/components/leaseSections/rent/BasicInfoEdit.tsx index 6f3f3425..778143d4 100644 --- a/src/leases/components/leaseSections/rent/BasicInfoEdit.tsx +++ b/src/leases/components/leaseSections/rent/BasicInfoEdit.tsx @@ -15,10 +15,12 @@ import { rentCustomDateOptions } from "@/leases/constants"; import { FieldTypes, FormNames } from "@/enums"; import { DueDatesPositions, FixedDueDates, LeaseRentDueDatesFieldPaths, LeaseRentDueDatesFieldTitles, LeaseRentsFieldPaths, LeaseRentsFieldTitles, RentCycles, RentTypes, RentDueDateTypes } from "@/leases/enums"; import { UsersPermissions } from "@/usersPermissions/enums"; -import { formatDueDates, formatSeasonalDate } from "@/leases/helpers"; +import { isServiceUnitAkvOrKuva } from "@/serviceUnits/enums"; +import { formatDueDates, formatSeasonalDate, showOverrideReceivableTypeField } from "@/leases/helpers"; import { getUiDataLeaseKey } from "@/uiData/helpers"; import { getFieldAttributes, hasPermissions, isFieldAllowedToEdit, isFieldAllowedToRead, isFieldRequired } from "@/util/helpers"; import { getAttributes as getLeaseAttributes, getCurrentLease } from "@/leases/selectors"; +import { getReceivableTypes } from "@/leaseCreateCharge/selectors"; import { getLeaseTypeList } from "@/leaseType/selectors"; import { getUsersPermissions } from "@/usersPermissions/selectors"; import { PlotSearchFieldPaths } from "@/plotSearch/enums"; @@ -182,7 +184,7 @@ type BasicInfoEmptyProps = { const BasicInfoEmpty = ({ isSaveClicked, - leaseAttributes + leaseAttributes, }: BasicInfoEmptyProps) => { return @@ -203,6 +205,8 @@ type BasicInfoIndexOrManualProps = { rentType: string; isSaveClicked: boolean; leaseAttributes: Attributes; + receivableTypeOptions: Array>; + serviceUnitId: number; usersPermissions: UsersPermissionsType; yearlyDueDates: Array; }; @@ -215,6 +219,8 @@ const BasicInfoIndexOrManual = ({ rentType, isSaveClicked, leaseAttributes, + receivableTypeOptions, + serviceUnitId, usersPermissions, yearlyDueDates }: BasicInfoIndexOrManualProps) => { @@ -324,17 +330,24 @@ const BasicInfoIndexOrManual = ({ + + { showOverrideReceivableTypeField(serviceUnitId) && + } ; }; @@ -348,7 +361,7 @@ type BasicInfoOneTimeProps = { const BasicInfoOneTime = ({ isSaveClicked, - leaseAttributes + leaseAttributes, }: BasicInfoOneTimeProps) => { return @@ -381,7 +394,6 @@ const BasicInfoOneTime = ({ - @@ -391,17 +403,7 @@ const BasicInfoOneTime = ({ - - - - - - - + ; }; @@ -411,6 +413,8 @@ type BasicInfoFixedProps = { field: string; isSaveClicked: boolean; leaseAttributes: Attributes; + receivableTypeOptions: Array>; + serviceUnitId: number; usersPermissions: UsersPermissionsType; yearlyDueDates: Array; }; @@ -421,6 +425,8 @@ const BasicInfoFixed = ({ field, isSaveClicked, leaseAttributes, + receivableTypeOptions, + serviceUnitId, usersPermissions, yearlyDueDates }: BasicInfoFixedProps) => { @@ -496,18 +502,23 @@ const BasicInfoFixed = ({ + + { showOverrideReceivableTypeField(serviceUnitId) && - + } ; }; @@ -557,6 +568,24 @@ const BasicInfoFree = ({ ; }; +/** + * Get receivable type options for override receivable type select. + * ReceivableTypes must be fetched separately from API, because receivabletype + * choices from leaseAttributes are not filtered by service unit. + * + * @param receivableTypes Receivable types filtered by lease's service unit + * @returns Array Receivabletype options for select element + */ +const getOverrideReceivableTypeOptions = (receivableTypes: Array>) => { + const options = receivableTypes.map((rt) => ({ + label: rt.name, + value: rt.id + })); + const sortedOptions = options.sort((a, b) => a.label.localeCompare(b.label)); + const emptyItem = { label: "", value: "" }; + return [emptyItem, ...sortedOptions]; +} + type Props = { change: (...args: Array) => any; currentLease: Lease; @@ -568,6 +597,7 @@ type Props = { isSaveClicked: boolean; leaseAttributes: Attributes; leaseTypes: LeaseTypeList; + receivableTypes: Array>; rentType: string | null | undefined; usersPermissions: UsersPermissionsType; }; @@ -582,6 +612,7 @@ const BasicInfoEdit = ({ isSaveClicked, leaseAttributes, leaseTypes, + receivableTypes, rentType, usersPermissions }: Props) => { @@ -591,13 +622,13 @@ const BasicInfoEdit = ({ if (!dueDatesPerYear || !leaseType || dueDatesType !== RentDueDateTypes.FIXED) return []; return FixedDueDates[rentType === RentTypes.FIXED ? DueDatesPositions.START_OF_MONTH : leaseType.due_dates_position][dueDatesPerYear]; }; - const yearlyDueDates = getYearlyDueDates(); + const receivableTypeOptions=getOverrideReceivableTypeOptions(receivableTypes); return {!rentType && } - {(rentType === RentTypes.INDEX || rentType === RentTypes.INDEX2022 || rentType === RentTypes.MANUAL) && } + {(rentType === RentTypes.INDEX || rentType === RentTypes.INDEX2022 || rentType === RentTypes.MANUAL) && } {rentType === RentTypes.ONE_TIME && } - {rentType === RentTypes.FIXED && } + {rentType === RentTypes.FIXED && } {rentType === RentTypes.FREE && } ; }; @@ -617,8 +648,9 @@ export default connect((state, props: BasicInfoEditProps) => { dueDates: selector(state, `${props.field}.due_dates`), leaseAttributes: getLeaseAttributes(state), leaseTypes: getLeaseTypeList(state), + receivableTypes: getReceivableTypes(state), usersPermissions: getUsersPermissions(state) }; }, { change -})(BasicInfoEdit); \ No newline at end of file +})(BasicInfoEdit); diff --git a/src/leases/components/leaseSections/rent/RentItem.tsx b/src/leases/components/leaseSections/rent/RentItem.tsx index 00b09fe7..ca497ef9 100644 --- a/src/leases/components/leaseSections/rent/RentItem.tsx +++ b/src/leases/components/leaseSections/rent/RentItem.tsx @@ -32,6 +32,7 @@ type Props = { rents: Array>; rentAdjustmentsCollapseState: boolean; rentCollapseState: boolean; + serviceUnitId: number; }; const RentItem = ({ @@ -45,7 +46,8 @@ const RentItem = ({ rent, rents, rentAdjustmentsCollapseState, - rentCollapseState + rentCollapseState, + serviceUnitId, }: Props) => { const handleCollapseToggle = (key: string, val: boolean) => { receiveCollapseStates({ @@ -108,7 +110,7 @@ const RentItem = ({ } headerTitle={ {getLabelOfOption(typeOptions, rentType) || '-'} } onToggle={handleRentCollapseToggle}> - + {(rentTypeIsIndex || rentTypeIsIndex2022 || rentTypeIsManual) && @@ -162,4 +164,4 @@ export default connect((state, props) => { }; }, { receiveCollapseStates -})(RentItem); \ No newline at end of file +})(RentItem); diff --git a/src/leases/components/leaseSections/rent/Rents.tsx b/src/leases/components/leaseSections/rent/Rents.tsx index 1a1a0d78..19e83b29 100644 --- a/src/leases/components/leaseSections/rent/Rents.tsx +++ b/src/leases/components/leaseSections/rent/Rents.tsx @@ -67,14 +67,14 @@ const Rents = ({ <> {!rents || !rents.length && Ei vuokria} {rents && !!rents.length && rents.map(rent => { - return ; + return ; })} {!!rentsArchived.length &&

Arkisto

} - {!!rentsArchived.length && rentsArchived.map(rent => )} + {!!rentsArchived.length && rentsArchived.map(rent => )}
@@ -110,4 +110,4 @@ const mapStateToProps = (state: RootState) => { }; }; -export default flowRight(connect(mapStateToProps), withRouter)(Rents) as React.ComponentType; \ No newline at end of file +export default flowRight(connect(mapStateToProps), withRouter)(Rents) as React.ComponentType; diff --git a/src/leases/enums.ts b/src/leases/enums.ts index e77dd082..31631960 100644 --- a/src/leases/enums.ts +++ b/src/leases/enums.ts @@ -907,7 +907,7 @@ export const LeaseRentsFieldTitles = { Y_VALUE: 'Y-luku', Y_VALUE_START: 'Y-luku alkaen', YEARLY_DUE_DATES: 'Eräpäivät (pv.kk)', - OVERRIDE_RECEIVABLE_TYPE: 'Korvaava saamislaji' + OVERRIDE_RECEIVABLE_TYPE: 'Automaattinen saamislaji' }; /** @@ -1594,4 +1594,4 @@ export const calculatorTypeOptions = [{ }, { value: 'device cabinet', label: 'Laitekaappi' -}]; \ No newline at end of file +}]; diff --git a/src/leases/helpers.ts b/src/leases/helpers.ts index 1be20b7d..41f6d1af 100644 --- a/src/leases/helpers.ts +++ b/src/leases/helpers.ts @@ -10,6 +10,7 @@ import { FormNames, TableSortOrder } from "@/enums"; import { CollateralTypes, ConstructabilityType, DecisionTypeKinds, LeaseState, LeaseTenantRentSharesFieldPaths, LeaseStatus, RecipientOptions, RelationTypes, RentAdjustmentAmountTypes, RentCycles, RentDueDateTypes, RentTypes, SubventionTypes, TenantContactType } from "./enums"; import { CalculatorTypes } from "@/leases/enums"; import { LeaseAreaAttachmentTypes } from "@/leaseAreaAttachment/enums"; +import { ServiceUnitIds, isServiceUnitAkvOrKuva } from "@/serviceUnits/enums"; import { getContactFullName, getContentContact } from "@/contacts/helpers"; import { getContentLessor } from "@/lessor/helpers"; import { getContentPropertyIdentifiers } from "@/rentbasis/helpers"; @@ -1641,7 +1642,7 @@ export const getContentRents = (lease: Record): Array, formVa } } - // Patch seasonal dates, contract rents and rent adjustments data only if rent type is index, index2022, fixed or manual + // Patch some data only if rent type is index, index2022, fixed, or manual if (rent.type === RentTypes.INDEX || rent.type === RentTypes.INDEX2022 || rent.type === RentTypes.FIXED || rent.type === RentTypes.MANUAL) { rentData.seasonal_start_day = rent.seasonal_start_day || null; rentData.seasonal_start_month = rent.seasonal_start_month || null; @@ -2733,6 +2734,7 @@ export const addRentsFormValuesToPayload = (payload: Record, formVa rentData.seasonal_end_month = rent.seasonal_end_month || null; rentData.contract_rents = getPayloadContractRents(rent, rent.type); rentData.rent_adjustments = getPayloadRentAdjustments(rent); + rentData.override_receivable_type = get(rent, 'override_receivable_type.id') || rent.override_receivable_type; } return rentData; @@ -2872,7 +2874,7 @@ export const formatDueDates = (dates: Array): string => { /** * Sort due dates by month and day - * @param {Array} dueDates + * @param {Array} dueDates * @returns {Array} Sorted array of due date objects with day and month properties. */ export const sortDueDates = (dueDates: Array): Array => { @@ -2931,7 +2933,7 @@ export const restructureLease = (lease: Record): Record, b: Record { + return isServiceUnitAkvOrKuva(leaseServiceUnitId); +} diff --git a/src/serviceUnits/enums.ts b/src/serviceUnits/enums.ts index ad850cc5..a1004e31 100644 --- a/src/serviceUnits/enums.ts +++ b/src/serviceUnits/enums.ts @@ -18,3 +18,16 @@ export const ServiceUnitIds = { KUVA_UPA: 4, KUVA_NUP: 5 } + +/** + * Tells whether or not the service unit ID is for AKV or KuVa service units. + */ +export const isServiceUnitAkvOrKuva = (serviceUnitId: number): boolean => { + const akv_kuva_service_unit_ids = [ + ServiceUnitIds.AKV, + ServiceUnitIds.KUVA_LIPA, + ServiceUnitIds.KUVA_UPA, + ServiceUnitIds.KUVA_NUP, + ] + return akv_kuva_service_unit_ids.includes(serviceUnitId); +}