(function(){u8.Crm.ServiceManagement=function(){this.commandBindings={"ServiceManagement/CreateOfferForTicket":this._onCommandCreateOfferForTicket.bind(this)}};$.extend(u8.Crm.ServiceManagement.prototype,{getTicketsRepIds:function(e,n,r){if(!e){$error("ticketUids must not be null!")}if(!u8.tools.isArray(e)){e=[e]}if(n&&!u8.tools.isArray(n)){n=[n]}var t=this;var i={options:{repIds:n,ticketRecordUids:e}};u8.request("ServiceManagementChannel.GetTicketsRepIds",i,function(e,n){n=n.error?n:{repIds:n.response&&n.response.repIds};return r&&r(t,n)})},getRepSkills:function(e,n,r){u8.request("ServiceManagementChannel.GetRepSkills",{repId:e,options:n},function(e,n){n=n.error?n:{skills:n.response&&n.response.skills};return r&&r(this,n)}.bind(this))},getRepSets:function(e){u8.request("RepsChannel.GetRepSets",{},function(n,r){r=r.error?r:{repSets:r.response&&r.response.repSets};return e&&e(this,r)},true)},createOfferFromServiceContract:function(e){u8.request("ServiceManagementChannel.CreateOfferFromServiceContract",{serviceContractUid:e.serviceContractUid,ticketUid:e.ticketUid},function(e,n){var r=n.response&&n.response.offerUid;if(u8.isValidRecordUid(r)){u8.services.actions.executeDefault(r)}}.bind(this))},getLinkedDataForOffer:function(e,n){u8.services.crud.read({uid:e,options:{linkName:"$Link[KP:1;FI:1]"}},function(e,r){var t=r.businessObject&&r.businessObject.uid;n(e,{uid:t})})},onServiceContractsOfTicketFormCreate:function(e,n){var r=e,t=r.page.process,i=t.step,a=i.get("$in","uid");var o=new u8.Crm.List({dataProvider:{reader:{arguments:{uid:a},endPoint:{Get:{endPoint:"ServiceManagementChannel.Bb.GetServiceContractsFor"}}}}});o.update(null,function(e,n){var t=o.resultSet;if(t.getNumberOfRows()==1){var a=t.getUidsAt(0,0);i.proceed({command:"Event1",commandArguments:a})}var c=r.getWidget("listViewPlaceholder");var u=r.page.$("<div />").appendTo(r.page.$(c.element));var d=s.bind(this);var m=new u8.Crm.Widgets.ListView(u,{model:o,parent:c,defaultAction:d,canEdit:false,menu:{model:{menuName:"M_WizardSelectWR"}},commandBindings:{"Action/A_SelectWR":d}})}.bind(this));function s(e,n,r){var t=r.uid||n.uid;i.proceed({command:"Event1",commandArguments:t})}},_onCommandCreateOfferForTicket:function(e,n,r){r.pending=true;var t=r.process,i=t.get("$in","uid");if(r.commandArguments&&r.commandArguments.skipCheck){this.getLinkedDataForOffer(i,function(e,n){r.output={command:"Event1",commandArguments:n.uid};r.proceed()});return}u8.request("ServiceManagementChannel.Bb.HasServiceContract",{uid:i},function(e,n){var t=n.response||{};if(u8.isValidRecordUid(t.contractUid)){r.output={command:"Event2",commandArguments:t.productUid};r.proceed()}else{this.getLinkedDataForOffer(i,function(e,n){r.output={command:"Event1",commandArguments:n.uid};r.proceed()})}}.bind(this))},createOrderFromOffer:function(e){var n=$.Deferred();u8.request("ServiceManagementChannel.CreateOrderFromOffer",{offerUid:e},function(e,r){var t={};if(r.response&&u8.isValidRecordUid(r.response.orderUid)){t.command="Created";t.newUid=r.response.orderUid}else{t.command="Error";t.error=r.error}n.resolve(t)});return n},getDurationInSeconds:function(e,n){if(isNaN(e)||isNaN(n)){return undefined}switch(n){case u8.Crm.ServiceManagement.Unit.Year:e*=12;case u8.Crm.ServiceManagement.Unit.Month:e*=4;case u8.Crm.ServiceManagement.Unit.Week:e*=7;case u8.Crm.ServiceManagement.Unit.Day:e*=24;case u8.Crm.ServiceManagement.Unit.Hour:e*=60;case u8.Crm.ServiceManagement.Unit.Minute:e*=60;break;default:return undefined}return e}});u8.services.add({name:"serviceManagement",scope:"Application",ctor:u8.Crm.ServiceManagement});u8.Crm.ServiceManagement.Unit={NoValue:0,Minute:1,Hour:2,Day:3,Week:4,Month:5,Year:6}})();