@fleetbase/fleetops-data
Version:
Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.
179 lines (149 loc) • 6.11 kB
JavaScript
import Model, { attr, hasMany, belongsTo } from '@ember-data/model';
import { computed, action } from '@ember/object';
import { getOwner } from '@ember/application';
import { format as formatDate, formatDistanceToNow } from 'date-fns';
export default class ServiceRate extends Model {
/** @ids */
public_id;
company_uuid;
service_area_uuid;
zone_uuid;
order_config_uuid;
/** @relationships */
rate_fees;
parcel_fees;
service_area;
order_config;
zone;
custom_field_values;
/** @attributes */
service_area_name;
zone_name;
service_name;
service_type;
base_fee;
per_meter_flat_rate_fee;
per_meter_unit;
max_distance_unit;
max_distance;
algorithm;
rate_calculation_method;
cod_calculation_method;
cod_flat_fee;
cod_percent;
peak_hours_calculation_method;
peak_hours_flat_fee;
peak_hours_percent;
peak_hours_start;
peak_hours_end;
currency;
duration_terms;
estimated_days;
has_cod_fee;
has_peak_hours_fee;
meta;
/** @dates */
deleted_at;
created_at;
updated_at;
/** @computed */
get updatedAgo() {
return formatDistanceToNow(this.updated_at);
}
get updatedAt() {
return formatDate(this.updated_at, 'yyyy-MM-dd HH:mm');
}
get updatedAtShort() {
return formatDate(this.updated_at, 'dd, MMM');
}
get createdAgo() {
return formatDistanceToNow(this.created_at);
}
get createdAt() {
return this.created_at ? formatDate(this.created_at, 'yyyy-MM-dd HH:mm') : null;
}
get createdAtShort() {
return this.created_at ? formatDate(this.created_at, 'dd, MMM') : null;
}
get isFixedMeter() {
return this.rate_calculation_method === 'fixed_meter' || this.rate_calculation_method === 'fixed_rate';
}
get isFixedRate() {
return this.rate_calculation_method === 'fixed_meter' || this.rate_calculation_method === 'fixed_rate';
}
get isPerMeter() {
return this.rate_calculation_method === 'per_meter';
}
get isPerDrop() {
return this.rate_calculation_method === 'per_drop';
}
get isAlgorithm() {
return this.rate_calculation_method === 'algo';
}
get isParcelService() {
return this.rate_calculation_method === 'parcel';
}
get hasPeakHoursFlatFee() {
return this.peak_hours_calculation_method === 'flat';
}
get hasPeakHoursPercentageFee() {
return this.peak_hours_calculation_method === 'percentage';
}
get hasCodFlatFee() {
return this.cod_calculation_method === 'flat';
}
get hasCodPercentageFee() {
return this.cod_calculation_method === 'percentage';
}
get rateFees() {
const n = Math.max(0, Number(this.max_distance) || 0);
const existing = (this.rate_fees?.toArray?.() ?? []).filter((r) => r.distance !== null && r.distance !== undefined && !r.isDeleted);
// Return existing fees sorted by distance, filtered by max_distance
return existing.filter((r) => r.distance >= 0 && r.distance < n).sort((a, b) => a.distance - b.distance);
}
/** @methods */
createDefaultPerDropFee(attributes = {}) {
const store = getOwner(this).lookup('service:store');
return store.createRecord('service-rate-fee', {
min: 1,
max: 5,
fee: 0,
unit: 'waypoint',
currency: this.currency,
...attributes,
});
}
addPerDropRateFee() {
const store = getOwner(this).lookup('service:store');
const existingFees = this.rate_fees?.toArray?.() ?? [];
const last = existingFees[existingFees.length - 1];
const min = last ? last.max + 1 : 1;
const max = min + 5;
const newFee = store.createRecord('service-rate-fee', {
min: min,
max: max,
unit: 'waypoint',
fee: 0,
currency: this.currency,
});
this.rate_fees.addObject(newFee);
}
removePerDropFee(fee) {
if (!fee || !fee.destroyRecord) return;
this.rate_fees.removeObject(fee);
fee.destroyRecord();
}
resetPerDropFees() {
// Remove all existing per-drop fees
const existingFees = this.rate_fees?.toArray?.() ?? [];
existingFees.forEach((fee) => {
if (fee.unit === 'waypoint') {
this.rate_fees.removeObject(fee);
fee.destroyRecord();
}
});
// Add a new default per-drop fee
const defaultFee = this.createDefaultPerDropFee();
this.rate_fees.addObject(defaultFee);
}
}