import BaseEntity from '../../common/entities/BaseEntity'; import ValidableInterface from '../../common/interfaces/ValidableInterface'; import PassengerType from '../../PassengerType/entity/PassengerType' import Money from '../../common/entities/Money'; import DiscountPercentage from '../../common/entities/DiscountPercentage'; export default class Concession extends BaseEntity implements ValidableInterface { static TYPE_FIXED = 'fixed'; static TYPE_PERCENTAGE = 'percentage'; private _passenger_type: PassengerType; private _type: string; private _amount: Money; private _percentage: DiscountPercentage; private _value: number; get passenger_type(): PassengerType { return this._passenger_type; } set passenger_type(value: PassengerType) { this._passenger_type = value; } get type(): string { return this._type; } set type(value: string) { this._type = value; } get amount(): Money { return this._amount; } set amount(value: Money) { this._amount = value; } get percentage(): DiscountPercentage { return this._percentage; } set percentage(value: DiscountPercentage) { this._percentage = value; } private isFixedAmount() { return this.type === Concession.TYPE_FIXED; } private isPercentageDiscount() { return this.type === Concession.TYPE_PERCENTAGE; } get value() { if(this.isFixedAmount()) { this._value = this.amount.amount; } else if(this.isPercentageDiscount()) { this._value = this.percentage.value; } return this._value; } set value(value: number) { this._value = value; if(this.isFixedAmount()) { this.amount.amount = value; } else if(this.isPercentageDiscount()) { this.percentage.value = value; } } isValid() { return this.invalidFields().length === 0; } invalidFields() { let fields = []; !this.passenger_type.name && fields.push('passenger_type'); !this.hasValue(this.amount) && fields.push('amount'); !this.hasValue(this.percentage) && fields.push('percentage'); !this.hasValue(this.type) && fields.push('type'); return fields; } }