equipment/equipment.js

/**
 * Creates a new equipment class
 * @class
 */
class Equipment {
    /**
     * @param {struct} skv_config
     */
    constructor(skv_config) {
        this.name = skv_config.name;
        this.id = skv_config.id;
        this.original_net = skv_config.original_net ? skv_config.original_net : 0;
        this.cost_net = skv_config.cost_net ? skv_config.cost_net : 0;
        this.cost_gross = skv_config.cost_gross ? skv_config.cost_gross : 0;
        this.change_battery_range = skv_config.change_battery_range;
        this.change_co2 = skv_config.change_co2;
        this.type = skv_config.type;
        this.is_selected = this.type === 'standard' ? true : skv_config.is_init_selected;
        this.is_init_selected = skv_config.is_init_selected;
        this.is_enabled = skv_config.is_init_enabled;
        this.is_taxable = skv_config.is_taxable;
        this.is_packed = skv_config.is_packed;
        this.is_parent_packed = skv_config.is_parent_packed;
        this.user_selected = false;
        this.user_deselected = false;
        this.panel_group = skv_config.panel_group;
        this.equipment_group = skv_config.equipment_group;
        this.display_cost = skv_config.display_cost;
        this.info_txt = skv_config.info_txt;
        this.dependency_txt = skv_config.dependency_txt;
        this.image = skv_config.image;
        this.matchtype_name = skv_config.matchtype_name;
        // options that cannot be selected by the user because of packing will not show a cost
        this.show_cost = this.type !== 'standard';

        this.taxable_cost = this.cost_gross;

        this.is_swatch = false;

        // if (
        //     this.is_packed
        //     || this.is_parent_packed
        // ) {
        //     this.cost_net = 0;
        //     this.cost_gross = 0;
        //     this.display_cost = 0;
        // }

        // only half the vat is taken off salsac amounts so add this back on
        // if (skv_config.is_salsac) {
        //     // used to get the net cost
        //     const vat_decimal = 1 + (skv_config.vat_percentage / 100);
        //     // used to add half the vat back on
        //     const salsac_vat_decimal = 1 + ((skv_config.vat_percentage / 100) / 2);
        //     // work out the net cost
        //     this.cost_net = this.cost_gross / vat_decimal;

        //     // most companies can reclaim vat but insurance companies like Allianz
        //     // cannot reclaim vat so check before adding half the vat back on
        //     if (skv_config.salsac_employer_can_reclaim_vat) {
        //         // work out the net cost
        //         this.cost_net = this.cost_gross / vat_decimal;
        //         this.cost_net *= salsac_vat_decimal;
        //     } else {
        //         // no vat can be reclaimed so just set the net cost to the gross cost
        //         this.cost_net = this.cost_gross;
        //     }
        // }

        // see if the swatch is required based on the "show_as_swatch" struct
        // passed in
        if (
            Object.prototype.hasOwnProperty.call(skv_config.show_as_swatch, skv_config.panel_group)
            && skv_config.show_as_swatch
        ) {
            this.is_swatch = true;
        }

        // apply any adjustment to each of the costs
        const cost_keys = ['cost_net', 'cost_gross', 'taxable_cost'];

        Object.keys(cost_keys).forEach((i) => {
            const cost_key = cost_keys[i];
            const unadjusted_value = this[cost_key];
            // work out how much the adjustment takes off
            const adjusted_by_value = unadjusted_value * (skv_config.adjustment_percentage / 100);

            // update the cost with adjustment applied
            this[cost_key] = unadjusted_value - adjusted_by_value;
            // add keys to show how much the costs have been adjusted by
            const adjusted_key = `adjusted_by_${cost_key}`;
            this[adjusted_key] = 0 - adjusted_by_value;
        });

        this.affects = [];
        this.affected_by = [];
        this.resolved = [];

        if (typeof skv_config.affects !== 'undefined') {
            const cnt_affects = skv_config.affects.length;

            for (let i = 0; i < cnt_affects; i += 1) {
                const affects = skv_config.affects[i];
                this.affects.push({
                    id: affects.id,
                    name: affects.name,
                    relationship: affects.relationship,
                });
            }
        }

        if (typeof skv_config.affected_by !== 'undefined') {
            const cnt_affected_by = skv_config.affected_by.length;

            for (let i = 0; i < cnt_affected_by; i += 1) {
                const affects = skv_config.affected_by[i];
                this.affected_by.push({
                    id: affects.id,
                    name: affects.name,
                    relationship: affects.relationship,
                });
            }
        }

        // set resolved dependencies
        const dependency_letters = ['A', 'B', 'C', 'D'];
        const cnt_dependency_letters = dependency_letters.length;

        for (let i = 0; i < cnt_dependency_letters; i += 1) {
            const letter = dependency_letters[i];

            this.resolved.push({
                name: `require1of${letter}`,
                resolved: false,
                resolved_by: 0,
            });
        }
    }

    /**
     * @param {string} name
     * @returns {boolean}
     */
    isDependencyResolved(name) {
        const cnt_resolved = this.resolved.length;

        for (let i = 0; i < cnt_resolved; i += 1) {
            const ResolvedDependency = this.resolved[i];

            if (ResolvedDependency.name === name) {
                return ResolvedDependency.resolved;
            }
        }
        return false;
    }

    /**
     * @param {string} name
     * @param {string} resolved_id
     * @returns {boolean}
     */
    isResolvedBy(name, resolved_id) {
        const cnt_resolved = this.resolved.length;

        for (let i = 0; i < cnt_resolved; i += 1) {
            const ResolvedDependency = this.resolved[i];

            if (ResolvedDependency.name === name) {
                return ResolvedDependency.resolved
                    && ResolvedDependency.resolved_by === resolved_id;
            }
        }

        return false;
    }

    /**
     * @param {string} name
     * @param {string} value
     * @param {string} resolved_id
     */
    resolveDependency(name, value, resolved_id) {
        const cnt_resolved = this.resolved.length;

        for (let i = 0; i < cnt_resolved; i += 1) {
            const ResolvedDependency = this.resolved[i];

            if (ResolvedDependency.name === name) {
                ResolvedDependency.resolved = value;
                ResolvedDependency.resolved_by = value ? resolved_id : 0;
                break;
            }
        }
    }

    /**
     * @param val
     */
    set displayCost(val) {
        this.display_cost = val;
    }

    get displayCost() {
        return this.display_cost;
    }
}

export {
    Equipment,
};