Skip to content

Instantly share code, notes, and snippets.

@geersch
Last active Aug 29, 2015
Embed
What would you like to do?
SellableFactory
function sellableFactory() {
this.deserialize = function (properties) {
var factory = new window[properties.type + 'SellableFactory']();
var sellable = Object.create(factory.sellableType.prototype);
angular.copy(properties, sellable);
return sellable;
}
};
function glasscurtainSellableFactory() {
var Sellable = function (settings) {
var self = this;
self.quantity = 1;
self.settings = settings;
self.totalMeters = 0;
self.productionCost = 0;
self.fabricCost = 0;
self.total = 0;
self.type = 'glasscurtain';
self.options = {
twoParts: false,
tunnel: false,
tunnelPosition: 'top',
leadwire: false,
leadbars: false,
ribbonTrim: false,
stitchedTopAndBottom: false
}
}
Sellable.prototype = {
determineFabricHeight: function () {
var height = this.height ? this.height : 0;
height += this.settings.marginHeight;
var fabricHeights = this.curtain ? this.curtain.fabricHeights : [];
var fabric;
for (var i = 0; i < fabricHeights.length; i++) {
if (fabricHeights[i].height >= height) {
fabric = fabricHeights[i];
break;
}
}
if (!fabric)
fabric = fabricHeights.length > 0 ? fabricHeights[fabricHeights.length - 1] : { height: 0, price: 0 };
this.fabric = fabric;
},
calculateTotals: function () {
this.determineFabricHeight();
var hem = this.options.twoParts ? this.settings.hem * 2 : this.settings.hem;
var width = this.width ? this.width * 3 + hem : 0;
if (this.options.ribbonTrim)
width *= this.options.widthMultiplier ? this.options.widthMultiplier : 1;
var quantity = this.quantity && this.quantity > 0 ? this.quantity : 1;
this.totalMeters = (width / 100);
var cost = this.options.stitchedTopAndBottom ? this.settings.cost * 2 : this.settings.cost;
this.productionCost = this.totalMeters * cost;
this.fabricCost = (this.totalMeters * this.fabric.price);
this.total = (this.productionCost + this.fabricCost) * quantity;
},
description: function () {
var description = this.curtain ? this.curtain.description : undefined;
if (description && this.color) {
description += ' (' + this.color.toLowerCase() + ')';
}
if (description && this.location) {
description += ' voor ' + this.location.toLowerCase();
}
return description;
}
}
this.sellableType = Sellable;
this.create = function (settings) {
return new Sellable(settings);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment