text: define(["text!sulusalescore/components/item-table/item.form.html","text!sulusalescore/components/item-table/item.row.html","text!sulusalescore/components/item-table/item.row-head.html","text!sulusalescore/components/item-table/item.overlay.html","config","suluproduct/util/price-calculation-util"],function(a,b,c,d,e,f){"use strict";var g={addressKey:"deliveryAddress",allowDuplicatedProducts:!0,columnCallbacks:{},columns:["name","number","settings","quantity","quantityUnit","price","discount","totalPrice"],data:[],defaultData:{},displayToolbars:!0,formId:"item-table-form",hasNestedItems:!1,isEditable:!0,rowCallback:null,settings:!1,showItemCount:!0,urlFilter:{}},h={products:"/admin/api/products{?filter*}",product:"/admin/api/products/"},i={listClass:".item-table-list",formSelector:".item-table-list-form",productSearchClass:".product-search",rowIdPrefix:"item-table-row-",rowClass:".item-table-row",quantityRowClass:".item-quantity",quantityInput:".item-quantity input",priceRowClass:".item-price",priceInput:".item-price input",discountRowClass:".item-discount",discountInput:".item-discount input",globalPriceTableClass:".global-price-table",overallEmptyString:"-",loaderSelector:".item-table-loader",loaderClass:"item-table-loader",overlayClassSelector:".settings-overlay",autocompleteLimit:20},j={defaultAddress:"salescore.use-main-delivery-address"},k={rowClass:null,id:null,rowNumber:null,address:null,addressObject:null,description:"",rowId:"",name:"",number:"",quantity:"",quantityUnit:"",price:"",discount:null,overallPrice:"",currency:"EUR",useProductsPrice:!1,tax:0,supplierName:""},l={priceRow:function(a,b){return["<tr>","   <td>",a,"   </td>","   <td>",b,"   </td>","</tr>"].join("")},loader:function(a){return'<div style="display:hidden" class="grid-row '+a+'"></div>'}},m="sulu.item-table.",n=function(){return u.call(this,"initialized")},o=function(){return m+"changed"},p=function(){return u.call(this,"set-default-data")},q=function(){return u.call(this,"reset-item-addresses")},r=function(){return u.call(this,"change-currency")},s=function(){return u.call(this,"set-addresses")},t=function(){return u.call(this,"get-data")},u=function(a){return m+this.options.instanceName+"."+a},v=function(){return{rowClass:"header",name:this.sandbox.translate("salescore.item.product"),number:this.sandbox.translate("salescore.item.number"),account:this.sandbox.translate("public.company"),customer:this.sandbox.translate("salescore.customer"),supplier:this.sandbox.translate("salescore.supplier"),address:this.sandbox.translate("address.delivery"),description:this.sandbox.translate("salescore.item.description"),quantity:this.sandbox.translate("salescore.item.quantity"),quantityUnit:this.sandbox.translate("salescore.item.unit"),price:this.sandbox.translate("salescore.item.price"),discount:this.sandbox.translate("salescore.item.discount"),overallPrice:this.sandbox.translate("salescore.item.overall-value")}},w=function(){this.sandbox.on(p.call(this),J.bind(this)),this.sandbox.on(r.call(this),B.bind(this)),this.sandbox.on(s.call(this),y.bind(this)),this.sandbox.on(t.call(this),z.bind(this)),this.sandbox.on(q.call(this),A.bind(this))},x=function(){this.sandbox.dom.on(this.$el,"click",aa.bind(this),".add-row"),this.sandbox.dom.on(this.$el,"click",Z.bind(this),".remove-row"),this.sandbox.dom.on(this.$el,"click",K.bind(this),".item-table-row"),this.sandbox.dom.on(this.$el,"click",L.bind(this),".item-table-row td"),this.sandbox.dom.on(this.$el,"data-changed",function(a){var b=a.items||[];ia.call(this,b)}.bind(this)),this.sandbox.dom.on(this.$el,"change",M.bind(this),i.quantityInput),this.sandbox.dom.on(this.$el,"change",N.bind(this),i.priceInput),this.sandbox.dom.on(this.$el,"change",O.bind(this),i.discountInput)},y=function(a){this.options.settings&&(this.options.settings.addresses=a)},z=function(a){"function"==typeof a&&a(this.getItems())},A=function(a){var b,c;J.call(this,"address",a);for(b in this.items)this.items.hasOwnProperty(b)&&(c=this.items[b],c.address=a)},B=function(a){var b,c,d=new this.sandbox.data.deferred;this.currency=a,b=I.call(this,this.items),b&&b.length>0&&(G.call(this,d),c=E.call(this,b),this.sandbox.dom.when(c,d).done(function(a){C.call(this,a),R.call(this),F.call(this)}.bind(this)).fail(function(a,b,c){this.sandbox.emit("sulu.labels.error.show",this.sandbox.translate("salescore.item-table.error.changing-currency"),"labels.error",""),this.sandbox.logger.error(a,b,c)}.bind(this)))},C=function(a){var b,c,d,e=D.call(this,a._embedded.products);for(d in this.items)this.items.hasOwnProperty(d)&&(c=this.items[d],e[c.product.id]&&e[c.product.id][this.currency]?c.price=e[c.product.id][this.currency]:c.price=0,b=this.sandbox.dom.find(i.priceInput,this.sandbox.dom.find("#"+d,this.$list)),this.sandbox.dom.val(b,this.sandbox.numberFormat(c.price,"n")),Q.call(this,d))},D=function(a){var b={};return this.sandbox.util.foreach(a,function(a){b[a.id]={},this.sandbox.util.foreach(a.prices,function(c){b[a.id][c.currency.code]=c.price||0}.bind(this))}.bind(this)),b},E=function(a){var b=this.sandbox.uritemplate.parse(h.products).expand({filter:{ids:a.join(",")}});return this.sandbox.util.load(b)},F=function(){this.sandbox.stop(this.$loader),this.sandbox.dom.show(this.$list)},G=function(a){H.call(this),this.sandbox.start([{name:"loader@husky",options:{el:this.$loader,size:"40px",hidden:!1}}]).done(function(){a.resolve()}.bind(this))},H=function(){var a=this.sandbox.dom.height(this.$el);this.$loader=this.sandbox.dom.createElement(l.loader.call(this,i.loaderClass)),this.$list=this.sandbox.dom.find(i.formSelector,this.$el),this.sandbox.dom.append(this.$el,this.$loader),this.sandbox.dom.height(this.$loader,a),this.sandbox.dom.hide(this.$list),this.sandbox.dom.show(this.$loader)},I=function(a){var b,c=[];for(b in a)a[b].hasOwnProperty("product")&&c.push(a[b].product.id);return c},J=function(a,b){this.options.defaultData[a]=b},K=function(a){if("INPUT"!==a.target.tagName.toUpperCase()&&"A"!==a.target.tagName.toUpperCase()&&this.options.isEditable){var b=this.sandbox.dom.attr(a.currentTarget,"id"),c=this.sandbox.dom.data(a.currentTarget,"id");this.options.rowCallback&&this.options.rowCallback.call(this,b,this.items[b]),this.options.settings&&"false"!==this.options.settings&&(c||0===c)&&la.call(this,this.items[b],this.options.settings,b)}},L=function(a){var b=this.sandbox.dom.data(a.currentTarget,"name"),c=this.sandbox.dom.data(this.sandbox.dom.parent(),"id");b&&this.options.columnCallbacks.hasOwnProperty(b)&&this.options.columnCallbacks[b].call(this,a.currentTarget,c)},M=function(a){var b=P.call(this,a).id;this.items[b].quantity=this.sandbox.parseFloat(this.sandbox.dom.val(a.target)),oa.call(this),Q.call(this,b),R.call(this),this.sandbox.emit(o.call(this))},N=function(a){var b=P.call(this,a).id;this.items[b].price=this.sandbox.parseFloat(this.sandbox.dom.val(a.target)),oa.call(this),Q.call(this,b),R.call(this),this.sandbox.emit(o.call(this))},O=function(a){var b=P.call(this,a).id;this.items[b].discount=this.sandbox.parseFloat(this.sandbox.dom.val(a.target)),oa.call(this),Q.call(this,b),R.call(this),this.sandbox.emit(o.call(this))},P=function(a){var b=this.sandbox.dom.closest(a.target,".item-table-row"),c=this.sandbox.dom.attr(b,"id");return{row:b,id:c}},Q=function(a){var b=this.$find("#"+a),c=this.items[a],d=this.sandbox.dom.find(".item-overall-price span",b);this.sandbox.dom.html(d,T.call(this,c))},R=function(){var a,b,c,d=this.getItems();if(d&&d.length>0&&d[0].price&&(a=f.getTotalPricesAndTaxes(this.sandbox,this.items),b=this.$find(i.globalPriceTableClass),this.sandbox.dom.empty(b),a)){S.call(this,b,this.sandbox.translate("salescore.item.net-price"),f.getFormattedAmountAndUnit(this.sandbox,a.netPrice,this.currency));for(c in a.taxes)S.call(this,b,this.sandbox.translate("salescore.item.vat")+".("+c+"%)",f.getFormattedAmountAndUnit(this.sandbox,a.taxes[c],this.currency));S.call(this,b,this.sandbox.translate("salescore.item.overall-price"),f.getFormattedAmountAndUnit(this.sandbox,a.grossPrice,this.currency))}},S=function(a,b,c){var d=this.sandbox.dom.createElement(l.priceRow.call(this,b,c));this.sandbox.dom.append(a,d)},T=function(a){return U(a),f.getTotalPrice(this.sandbox,a.price,V.call(this,a),a.discount,a.quantity,a.tax,!0)},U=function(a){a.price=a.price||0,a.discount=a.discount||0,a.quantity=a.quantity||0,a.tax=a.tax||0},V=function(a){return a.currency?a.currency:this.currency},W=function(a,b){var c=this.sandbox.dom.closest(b.currentTarget,i.rowClass),d=this.sandbox.dom.attr(c,"id"),e={};return X.call(this,a.id)?void this.sandbox.emit("sulu.labels.warning.show",this.sandbox.translate("salescore.item-table.warning.product-already-added"),"labels.warning",""):(this.addedProductIds.push(a.id),this.sandbox.start([{name:"loader@husky",options:{el:this.sandbox.dom.find(i.productSearchClass,c),size:"15px"}}]),void this.sandbox.util.load(h.product+a.id).then(function(a){e=ka.call(this,a),ea.call(this,d,e)}.bind(this)).fail(function(a,b,c){this.sandbox.emit("sulu.labels.error.show",this.sandbox.translate("salescore.item-table.error.loading-product"),"labels.error",""),this.sandbox.logger.error(a,b,c)}.bind(this)))},X=function(a){return this.options.allowDuplicatedProducts||-1===this.addedProductIds.indexOf(a)?!1:!0},Y=function(a){var b=e.get("suluproduct.components.autocomplete.default"),c=b.remoteUrl+"{&filter*}{&limit*}";b.remoteUrl=this.sandbox.uritemplate.parse(c).expand({filter:this.options.urlFilter,limit:i.autocompleteLimit}),b.el=this.sandbox.dom.find(i.productSearchClass,a),b.selectCallback=W.bind(this),b.limit=i.autocompleteLimit,b.instanceName+=this.rowCount,this.sandbox.start([{name:"auto-complete@husky",options:b}]).then(function(){if(this.$lastAddedRow){var a=this.sandbox.dom.find("input",this.$lastAddedRow)[0];this.sandbox.dom.focus(a),this.$lastAddedRow=null}}.bind(this))},Z=function(a){a.preventDefault(),a.stopPropagation();var b=this.sandbox.dom.closest(a.currentTarget,".item-table-row"),c=this.sandbox.dom.attr(b,"id");ba.call(this,c,b)},$=function(a){delete this.items[a],oa.call(this)},_=function(a,b){this.items[a]=b,oa.call(this)},aa=function(a){a.preventDefault(),ha.call(this)},ba=function(a,b){if(this.sandbox.dom.remove(b),this.items[a]&&this.items[a].product){var c=this.addedProductIds.indexOf(this.items[a].product.id);this.addedProductIds.splice(c,1)}$.call(this,a),ga.call(this,b),R.call(this),this.sandbox.emit(o.call(this))},ca=function(a,c){c||this.rowCount++;var d,e,f=this.sandbox.util.extend({},k,this.options.defaultData,a,{isEditable:this.options.isEditable,displayToolbars:this.options.displayToolbars,columns:this.options.columns,rowId:c?c:i.rowIdPrefix+this.rowCount,rowNumber:this.rowCount,showItemCount:this.options.showItemCount});return f.address&&"object"==typeof f.address&&(f.addressObject=f.address,f.address=this.sandbox.sulu.createAddressString(f.address)),f.currency=this.currency,f.overallPrice=this.sandbox.numberFormat(T.call(this,f)),f.discount=this.sandbox.numberFormat(f.discount,"n"),f.price=this.sandbox.numberFormat(f.price,"n"),f.quantity=this.sandbox.numberFormat(f.quantity,"n"),d=this.sandbox.util.template(b,f),e=this.sandbox.dom.createElement(d)},da=function(a){var b,c;return this.options.hasNestedItems&&(c=a,a=this.sandbox.util.extend({},a.item,c),delete a.item),b=ca.call(this,a),this.$lastAddedRow=b,this.sandbox.dom.append(this.$find(i.listClass),b),b},ea=function(a,b){var c=ca.call(this,b,a);return this.sandbox.dom.replaceWith(this.$find("#"+a),c),_.call(this,a,b),fa.call(this,c),this.sandbox.emit(o.call(this)),c},fa=function(a){this.options.columns.indexOf("quantity")>0&&this.sandbox.form.addField(this.selectorFormId,this.sandbox.dom.find(i.quantityInput,a)),this.options.columns.indexOf("price")>0&&this.sandbox.form.addField(this.selectorFormId,this.sandbox.dom.find(i.priceInput,a)),this.options.columns.indexOf("discount")>0&&this.sandbox.form.addField(this.selectorFormId,this.sandbox.dom.find(i.discountInput,a))},ga=function(a){this.options.columns.indexOf("quantity")>0&&this.sandbox.form.removeField(this.selectorFormId,this.sandbox.dom.find(i.quantityInput,a)),this.options.columns.indexOf("price")>0&&this.sandbox.form.removeField(this.selectorFormId,this.sandbox.dom.find(i.priceInput,a)),this.options.columns.indexOf("discount")>0&&this.sandbox.form.removeField(this.selectorFormId,this.sandbox.dom.find(i.discountInput,a))},ha=function(){var a,b={rowClass:"new"};a=da.call(this,b),Y.call(this,a)},ia=function(a){this.items={},this.sandbox.dom.empty(this.$find(i.listClass)),ja.call(this,a)},ja=function(a){var b,c,d,e,f;for(b=-1,c=a.length;++b<c;)d=a[b],e=da.call(this,a[b]),f=this.sandbox.dom.attr(e,"id"),this.items[f]=d;oa.call(this)},ka=function(a){var b,c,d=this.sandbox.util.extend({},k,this.options.defaultData,{name:a.name,number:a.number,description:a.shortDescription,product:a,quantityUnit:a.orderUnit?a.orderUnit.name:""});if(a.prices&&a.prices.length>0)for(b=-1,c=a.prices.length;++b<c;)a.prices[b].currency.code===this.currency&&(d.price=a.prices[b].price);return a.supplierName&&(d.supplierName=a.supplierName),d},la=function(a,b,c){var e,f,g,h,k=this.sandbox.translate(j.defaultAddress);b=this.sandbox.util.extend({columns:[],addresses:[]},b),a[this.options.addressKey]&&(k=this.sandbox.sulu.createAddressString(a[this.options.addressKey])),a=this.sandbox.util.extend({settings:b,defaultAddressLabel:k,createAddressString:this.sandbox.sulu.createAddressString,translate:this.sandbox.translate,deliveryDate:null,costCenter:null,discount:null,numberFormat:this.sandbox.numberFormat},a),a.hasOwnProperty(this.options.addressKey)&&a[this.options.addressKey]||(a[this.options.addressKey]={id:null}),this.sandbox.stop(this.sandbox.dom.find(i.overlayClassSelector,this.$el)),this.sandbox.dom.remove(this.sandbox.dom.find(i.overlayClassSelector,this.$el)),f=this.sandbox.util.template(d,a),e=this.sandbox.dom.createElement('<div class="'+i.overlayClass+'"></div>'),this.sandbox.dom.append(this.$el,e),g=a.name,h="#"+a.number,a.supplierName&&""!==a.supplierName&&(h+="<br/>"+a.supplierName),this.sandbox.start([{name:"overlay@husky",options:{el:e,title:g,subTitle:h,instanceName:"settings",openOnStart:!0,removeOnClose:!1,skin:"wide",data:f,okCallback:function(){var a=this.sandbox.dom.val(i.overlayClassSelector+' *[data-mapper-property="deliveryAddress"]'),b=this.sandbox.dom.val(i.overlayClassSelector+' *[data-mapper-property="deliveryDate"] input'),d=this.sandbox.dom.val(i.overlayClassSelector+' *[data-mapper-property="costCenter"]');this.items[c].description=this.sandbox.dom.val(i.overlayClassSelector+' *[data-mapper-property="description"]'),this.items[c].quantity=this.sandbox.parseFloat(this.sandbox.dom.val(i.overlayClassSelector+' *[data-mapper-property="quantity"]')),this.items[c].price=this.sandbox.parseFloat(this.sandbox.dom.val(i.overlayClassSelector+' *[data-mapper-property="price"]')),this.items[c].discount=this.sandbox.parseFloat(this.sandbox.dom.val(i.overlayClassSelector+' *[data-mapper-property="discount"]')),"-1"!==a&&(this.items[c][this.options.addressKey]=ma.call(this,a)),this.items[c].deliveryDate=""!==b?b:null,this.items[c].costCenter=""!==d?d:null,ea.call(this,c,this.items[c]),R.call(this,c),oa.call(this)}.bind(this)}},{name:"input@husky",options:{el:"#delivery-date",skin:"date",instanceName:"settings-delivery-date",inputId:"settings-delivery-date"}}])},ma=function(a){var b,c,d=this.options.settings.addresses;for(b=-1,c=d.length;++b<c;)if(d[b].id.toString()===a.toString())return d[b];return null},na=function(){var a=this.sandbox.util.extend({},k,this.options,{header:v.call(this)}),b=this.sandbox.util.template(c,a);this.sandbox.dom.append(this.$find(i.listClass),b)},oa=function(){this.sandbox.dom.data(this.$el,"items",this.getItems())},pa=function(){this.sandbox.form.create(this.selectorFormId)};return{initialize:function(){this.options=this.sandbox.util.extend({},g,this.options),this.selectorFormId="#"+this.options.formId,this.items={},this.rowCount=0,this.addedProductIds=[],this.table=null,this.currency=this.options.currency||k.currency,this.isEmpty=this.items.length;var a=this.sandbox.dom.data(this.$el,"items");0===this.options.data.length&&a&&a.length>0&&(this.options.data=a),this.render(),w.call(this),x.call(this)},render:function(){var b=this.sandbox.util.extend({},{formId:this.options.formId,addText:this.sandbox.translate("salescore.item.add"),isEditable:this.options.isEditable,displayToolbars:this.options.displayToolbars,columns:this.options.columns,showItemCount:this.options.showItemCount});this.table=this.sandbox.util.template(a,b),this.html(this.table),na.call(this),ja.call(this,this.options.data),pa.call(this),R.call(this),this.sandbox.emit(n.call(this))},getItems:function(){var a,b=[];for(a in this.items)b.push(this.items[a]);return b}}});