43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
const BaseFinancialRepository = require('./BaseFinancialRepository');
|
|
|
|
/**
|
|
* Unit Monthly Fee Repository
|
|
*
|
|
* Manages monthly fee settings for units.
|
|
*/
|
|
class UnitMonthlyFeeRepository extends BaseFinancialRepository {
|
|
constructor() {
|
|
super('pg_fn_unit_monthly_fees');
|
|
}
|
|
|
|
async findByUnitId(unitId, siteId) {
|
|
return await this.findAll({ unit_id: unitId, site_id: siteId }, {
|
|
orderBy: 'effective_from',
|
|
orderDirection: 'desc'
|
|
});
|
|
}
|
|
|
|
async findActiveFee(unitId, siteId) {
|
|
const now = new Date().toISOString().split('T')[0];
|
|
const fees = await this.findWhere({
|
|
unit_id: { eq: unitId },
|
|
site_id: { eq: siteId },
|
|
effective_from: { lte: now }
|
|
});
|
|
|
|
// Return the most recent active fee (effective_to is NULL or in future)
|
|
return fees.find(fee => !fee.effective_to || fee.effective_to >= now) || null;
|
|
}
|
|
|
|
async createMonthlyFee(feeData) {
|
|
return await this.create({
|
|
...feeData,
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString()
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = UnitMonthlyFeeRepository;
|
|
|
|
|