@fleetbase/fleetops-data
Version:
Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.
171 lines (136 loc) • 5.15 kB
JavaScript
import Model, { attr, hasMany, belongsTo } from '@ember-data/model';
import { computed } from '@ember/object';
import { getOwner } from '@ember/application';
import { isArray } from '@ember/array';
import { format as formatDate, formatDistanceToNow } from 'date-fns';
import isModel from '@fleetbase/ember-core/utils/is-model';
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;
zone;
/** @attributes */
service_area_name;
zone_name;
service_name;
service_type;
base_fee;
per_meter_flat_rate_fee;
per_meter_unit;
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, 'PPP p');
}
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, 'PPP p') : null;
}
get createdAtShort() {
return this.created_at ? formatDate(this.created_at, 'dd, MMM') : null;
}
get isFixedMeter() {
return this.rate_calculation_method === 'fixed_meter';
}
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.service_type === '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';
}
/** @methods */
setServiceRateFees(fees = []) {
if (!isArray(fees)) {
return this;
}
let serviceRateFees = [];
let owner = getOwner(this);
let store = owner.lookup('service:store');
for (let i = 0; i < fees.length; i++) {
let rateFee = fees.objectAt(i);
rateFee.currency = this.currency;
if (isModel(rateFee)) {
serviceRateFees.pushObject(rateFee);
} else {
serviceRateFees.pushObject(store.createRecord('service-rate-fee', rateFee));
}
}
this.rate_fees.pushObjects(serviceRateFees);
return this;
}
clearServiceRateFees() {
this.rate_fees.clear();
return this;
}
setServiceRateParcelFees(fees = []) {
if (!isArray(fees)) {
return this;
}
let serviceRateParcelFees = [];
let owner = getOwner(this);
let store = owner.lookup('service:store');
for (let i = 0; i < fees.length; i++) {
let parcelFee = fees.objectAt(i);
parcelFee.currency = this.currency;
if (isModel(parcelFee)) {
serviceRateParcelFees.pushObject(parcelFee);
} else {
serviceRateParcelFees.pushObject(store.createRecord('service-rate-parcel-fee', parcelFee));
}
}
this.parcel_fees.pushObjects(serviceRateParcelFees);
return this;
}
}