· 10 years ago · Sep 14, 2016, 01:38 PM
1'use strict';
2
3angular.module('elasticApp')
4 .controller('OfferController', function($scope, ResourceConst, RemoteService, SharedService, CookieService, VFRemotingService, VFRemotingConstant, toaster,
5 ngDialog, $timeout, $interval) {
6 var offer = this;
7 offer.SharedService = SharedService;
8 offer.RemoteService = RemoteService;
9 offer.CookieService = CookieService;
10 offer.VFRemotingService = VFRemotingService;
11 offer.VFRemotingConstant = VFRemotingConstant;
12
13 offer.saveStatus = true;
14 // Get sort cookie
15 // if (CookieService.checkCookie('sortType')){
16 // var orderCookie = CookieService.getCookie('sortType');
17 // SharedService.sortType = orderCookie.sortType;
18 // };
19 // if (CookieService.checkCookie('sortReverse')){
20 // var reverseCookie = CookieService.getCookie('sortReverse');
21 // SharedService.sortReverse = reverseCookie.sortReverse;
22 // };
23 // Get offer information
24 RemoteService.getOfferPublications();
25
26 RemoteService.getBaseConfig()
27 .then(function(response) {
28 // Existing offer
29 if (response) {
30 console.log("Existing Offer");
31 // Skip these process steps
32 SharedService.process.baseConfigurationSelected = true;
33 SharedService.process.clonedOffer = true;
34 SharedService.section.fullPage.loader = false;
35 SharedService.section.navbar.show = true;
36 RemoteService.getOffer()
37 .then(function(response) {
38 SharedService.activeOffer.name = response.Name
39 // If it exists, then the process has been at least started
40 if (response.Variant_Generation_Status__c) {
41 if (response.Variant_Generation_Status__c == 'Complete') {
42 SharedService.process.variantsGenerationStarted = false;
43 SharedService.activeTab = 0;
44 RemoteService.getOfferVariants()
45 .then(function(response) {
46 SharedService.process.variantsGenerated = true;
47 SharedService.section.generatedVariants.loader = false;
48 }, function(error) {
49
50 });
51 } else if (response.Variant_Generation_Status__c == 'Processing') {
52 // Generate getVariantScope parameter
53 SharedService.process.variantsGenerated = false;
54 var scopeParam = [];
55 if (!RemoteService.baseConfigVariants.length) {
56 // Because it doesn't have time to load it will get variant list from cookie
57 RemoteService.baseConfigVariants = CookieService.getCookie('baseConfigVariants');
58 }
59 for (var i = 0; i < RemoteService.baseConfigVariants.length; i++) {
60 scopeParam.push(RemoteService.baseConfigVariants[i].id);
61 };
62 // Force generating offers
63 offer.generateDefinitions();
64 SharedService.activeTab = 2;
65 // Get param
66 RemoteService.getVariantScope(scopeParam)
67 .then(function(response) {
68 SharedService.process.variantsGenerationStarted = true;
69 SharedService.section.generatedVariants.loader = true;
70 offer.startWatchingGenerateVariants(3000);
71 }, function(error) {
72
73 });
74 } else {
75 SharedService.activeTab = 0;
76 }
77 // Finally write it somewhere
78 SharedService.activeOffer.Variant_Generation_Message__c = response.Variant_Generation_Message__c;
79 SharedService.activeOffer.Variant_Generation_Status__c = response.Variant_Generation_Status__c;
80 // Else, process has not been started
81 } else {
82 console.warn("Variants have not been generated yet");
83 SharedService.activeOffer.Variant_Generation_Message__c = 'Not started';
84 SharedService.activeOffer.Variant_Generation_Status__c = 'Not started';
85 }
86
87 }, function(error) {
88
89 });
90 }
91 // New offer
92 else {
93 console.log("New Offer");
94 SharedService.activeTab = 3;
95 SharedService.section.fullPage.loader = false;
96 SharedService.section.navbar.show = true;
97 }
98 }, function(error) {
99 console.log("Cannot get offer configs");
100 toaster.pop('danger', 'Error!', 'Cannot retrieve offer configuration');
101 SharedService.section.fullPage.loader = false;
102 SharedService.section.navbar.show = true;
103 });
104
105
106 // If base configuration has not been selected
107 if (!SharedService.process.baseConfigurationSelected) {
108 RemoteService.getSourceConfigs();
109 } else {
110 SharedService.activeTab = 0;
111 };
112 // VARIANTS
113 offer.selectBaseOffer = function(configId) {
114 SharedService.section.baseConf.loader = true;
115
116 RemoteService.cloneSourceConfig(configId)
117 .then(function(response) {
118 toaster.pop('success', 'Success!', 'Configuration successfully created.');
119 // Go to Variants tab
120 RemoteService.getBaseConfig()
121 .then(function(response) {
122 SharedService.process.baseConfigurationSelected = true;
123 SharedService.process.clonedOffer = true;
124 SharedService.activeTab = 0;
125 SharedService.section.fullPage.loader = false;
126 SharedService.section.navbar.show = true;
127
128 RemoteService.getOffer()
129 .then(function(response) {
130 SharedService.activeOffer.name = response.Name
131
132 // If it exists, then the process has been at least started
133 if (response.Variant_Generation_Status__c) {
134 if (response.Variant_Generation_Status__c == 'Complete') {
135 SharedService.process.variantsGenerationStarted = false;
136 SharedService.activeTab = 0;
137 RemoteService.getOfferVariants()
138 .then(function(response) {
139 SharedService.process.variantsGenerated = true;
140 SharedService.section.generatedVariants.loader = false;
141 }, function(error) {
142
143 });
144 } else if (response.Variant_Generation_Status__c == 'Processing') {
145 // Generate getVariantScope parameter
146 var scopeParam = [];
147 if (!RemoteService.baseConfigVariants.length) {
148 // Because it doesn't have time to load it will get variant list from cookie
149 RemoteService.baseConfigVariants = CookieService.getCookie('baseConfigVariants');
150 }
151 for (var i = 0; i < RemoteService.baseConfigVariants.length; i++) {
152 scopeParam.push(RemoteService.baseConfigVariants[i].id);
153 };
154 // Force generating offers
155 offer.generateDefinitions();
156 SharedService.activeTab = 2;
157 // Get param
158 RemoteService.getVariantScope(scopeParam)
159 .then(function(response) {
160 SharedService.process.variantsGenerationStarted = true;
161 SharedService.section.generatedVariants.loader = true;
162 offer.startWatchingGenerateVariants(3000);
163 }, function(error) {
164
165 });
166 } else {
167 SharedService.activeTab = 0;
168 }
169 // Finally write it somewhere
170 SharedService.activeOffer.Variant_Generation_Message__c = response.Variant_Generation_Message__c;
171 SharedService.activeOffer.Variant_Generation_Status__c = response.Variant_Generation_Status__c;
172 // Else, process has not been started
173 } else {
174 console.warn("Variants have not been generated yet");
175 SharedService.activeOffer.Variant_Generation_Message__c = 'Not started';
176 SharedService.activeOffer.Variant_Generation_Status__c = 'Not started';
177 }
178
179 }, function(error) {
180
181 });
182
183 // Turn off the loader
184 setTimeout(function() {
185 SharedService.section.baseConf.loader = false;
186 }, 500);
187 }, function(error) {
188 toaster.pop('danger', 'Error!', 'Action could not be completed');
189 SharedService.section.baseConf.loader = false;
190 });
191
192 }, function(error) {
193 toaster.pop('danger', 'Error!', 'Action could not be completed');
194 SharedService.section.baseConf.loader = false;
195 });
196 };
197 offer.updateAttribute = function(attribute) {
198 var sendObj = {};
199 sendObj.attrDefId = attribute.attrDefId;
200 sendObj.isFacet = attribute.isFacet;
201 sendObj.isOverride = attribute.isOverride;
202 sendObj.isVariant = attribute.isVariant;
203
204 RemoteService.saveVariableAttribute(sendObj);
205 };
206 offer.variantChanged = function(index, attribute) {
207 offer.saveStatus = true;
208 var attributeName = attribute.attributeName;
209 var attrDefId = attribute.attrDefId;
210 var newVal = attribute.isVariant;
211
212 if (newVal) {
213 // If selecting variant, add it to indexing table
214 if (RemoteService.baseConfigVariants.length <= 2) {
215 offer.updateAttribute(attribute);
216 RemoteService.DefAttrMasterObj[SharedService.activeProductDefinition.id].attributes[index].isOverride = false;
217 RemoteService.baseConfigVariants.push({
218 'id': attrDefId,
219 'name': attributeName
220 });
221 } else {
222 toaster.pop('warning', 'Variant limit!', 'No more than 3 Variant Attributes allowed.');
223 RemoteService.DefAttrMasterObj[SharedService.activeProductDefinition.id].attributes[index].isVariant = false;
224 };
225 // If deselected, splice it from indexing array
226 } else {
227 offer.updateAttribute(attribute);
228 var deleteIndex = SharedService.getIndexById(RemoteService.baseConfigVariants, attrDefId);
229 RemoteService.baseConfigVariants.splice(deleteIndex, 1);
230 };
231 CookieService.setCookie('baseConfigVariants', RemoteService.baseConfigVariants);
232 };
233 offer.overrideChanged = function(attribute) {
234 offer.saveStatus = true;
235 offer.updateAttribute(attribute);
236 };
237 offer.facetChanged = function(attribute) {
238 offer.saveStatus = true;
239 offer.updateAttribute(attribute);
240 };
241 offer.handleSaveVariants = function() {
242 SharedService.section.variants.loader = true;
243
244 var generatedArr = [];
245 var generatedArr = offer.generateOfferAttribute(RemoteService.DefAttrMasterObj[SharedService.activeProductDefinition.id].attributes);
246 RemoteService.saveOfferAttributes(generatedArr)
247 .then(function(response) {
248 offer.saveStatus = false;
249 SharedService.section.variants.loader = false;
250 }, function(error) {
251
252 });
253 };
254 offer.generateOfferAttribute = function(generatedArr) {
255 var returnArr = [];
256 for (var key in RemoteService.DefAttrMasterObj) {
257 for (var i = 0; i < RemoteService.DefAttrMasterObj[key].attributes.length; i++) {
258 var attrObj = {
259 'attrDefId': '',
260 'isVariant': '',
261 'isOverride': '',
262 'isFacet': '',
263 };
264
265 attrObj.attrDefId = RemoteService.DefAttrMasterObj[key].attributes[i].attrDefId;
266 attrObj.isVariant = RemoteService.DefAttrMasterObj[key].attributes[i].isVariant;
267 attrObj.isOverride = RemoteService.DefAttrMasterObj[key].attributes[i].isOverride;
268 attrObj.isFacet = RemoteService.DefAttrMasterObj[key].attributes[i].isFacet;
269
270 if (attrObj.isVariant || attrObj.isOverride || attrObj.isFacet) {
271 returnArr.push(attrObj);
272 }
273 }
274 }
275 return returnArr;
276 };
277 offer.handlePillClick = function(key, index, name) {
278 SharedService.activeProductDefinition.id = key
279 SharedService.activeProductDefinition.name = name;
280 SharedService.activePre = index;
281 };
282 // RELATED PRODUCTS
283 offer.generateRelatedProductStructue = function(callback) {
284 // Saving the index - attr def for easier manipulation
285 console.log("Starting to generate structure...");
286 // Efficient way to delete array
287 RemoteService.RelatedProductsStructured.length = 0;
288
289 var OfferMap = {};
290 var OfferArr = [];
291
292 var rpGroupMap = {};
293 RemoteService.relatedProductOfferAttributes()
294 .then(function(response) {
295 var mapAttributes = response;
296
297 // Create toggle structure
298 // Cookie check here
299 // if (SharedService.objectLength(mapAttributes)) {
300 // for (var key in mapAttributes){
301 // mapAttributes[key].cscfga__Attributes__r
302 // SharedService.relatedProductToggle[key] = {};
303
304 // for (var i = 0; i < mapAttributes[key].cscfga__Attributes__r.length; i++) {
305 // var attrs = mapAttributes[key].cscfga__Attributes__r[i];
306 // SharedService.relatedProductToggle[key][attrs.cscfga__Attribute_Definition__c] = (!!attrs.cscfga__Value__c);
307 // }
308 // }
309 // };
310
311 RemoteService.relatedProductOfferMap()
312 .then(function(response) {
313 for (var i = 0; i < response.length; i++) {
314 var tempObj = {};
315 tempObj.Id = response[i].Id; //Attribute__c
316 tempObj.Attribute__c = response[i].Attribute__c; //Attribute__c
317 tempObj.Product_Sequence__c = response[i].Product_Sequence__c;
318 tempObj.Group_Sequence__c = response[i].Group_Sequence__c;
319 tempObj.Related_Product_Configuration__c = response[i].Related_Product_Configuration__c;
320
321 tempObj.Related_Product_Offer__c = response[i].Related_Product_Offer__c;
322 tempObj.Name = response[i].Related_Product_Offer__r.Name;
323
324 // Added RP should have attributes but just in case
325 if (mapAttributes[tempObj.Related_Product_Configuration__c]) {
326 tempObj.cscfga__Attributes__r = [];
327 var attributeArr = mapAttributes[tempObj.Related_Product_Configuration__c].cscfga__Attributes__r;
328
329 for (var j = 0; j < attributeArr.length; j++) {
330 tempObj.cscfga__Attributes__r.push({
331 'Name': attributeArr[j].Name,
332 'cscfga__Value__c': attributeArr[j].cscfga__Value__c || null,
333 'cscfga__Attribute_Definition__c': attributeArr[j].cscfga__Attribute_Definition__c,
334 'Visible': (!!attributeArr[j].cscfga__Value__c)
335 });
336 if (!SharedService.relatedProductToggle[attributeArr[j].cscfga__Attribute_Definition__c]) {
337 SharedService.relatedProductToggle[attributeArr[j].cscfga__Attribute_Definition__c] = (!!
338 attributeArr[i].cscfga__Value__c);
339 }
340 }
341 }
342
343 tempObj.Selected = true;
344
345 // var groupIndex = SharedService.getIndexById(RemoteService.RelatedProductsStructured, response[i].Attribute__c);
346
347 if (!OfferMap[tempObj.Attribute__c]) {
348 OfferMap[tempObj.Attribute__c] = [];
349 }
350 OfferMap[tempObj.Attribute__c].push(tempObj);
351 };
352
353 // When its done
354 // Call for groups data
355 RemoteService.relatedProductAttributes()
356 .then(function(response) {
357 if (!response.length) {
358 toaster.pop('error', "No related products found!",
359 "Product definition does not contain any related products!");
360 SharedService.section.relatedProducts.loader = false;
361 return false;
362 }
363 // relatedProductGroups
364 for (var i = 0; i < response.length; i++) {
365
366 var tempObj = {};
367 OfferArr.push(response[i].Id);
368
369 tempObj.Id = response[i].Id;
370 tempObj.Attribute__c = response[i].cscfga__Attribute_Definition__c; //???
371
372 tempObj.cscfga__Label__c = response[i].cscfga__Attribute_Definition__r.cscfga__Label__c;
373 tempObj.cscfga__Min__c = response[i].cscfga__Attribute_Definition__r.cscfga__Min__c;
374 tempObj.cscfga__Max__c = response[i].cscfga__Attribute_Definition__r.cscfga__Max__c;
375
376 tempObj.RelatedProductMap = OfferMap[tempObj.Id] || [];
377 tempObj.RelatedAddableMap = [];
378 // As per Ranko's model:
379 tempObj.Group_Sequence__c = i;
380
381
382 RemoteService.RelatedProductsStructured.push(tempObj);
383 }
384 var loopGroup = function(indexGroup) {
385 RemoteService.addableRelatedProducts(OfferArr[indexGroup])
386 .then(function(response) {
387
388 for (var i = 0; i < response.length; i++) {
389 RemoteService.RelatedProductsStructured[indexGroup].RelatedAddableMap
390 .push(offer.handleAddableResponse(response[i], OfferArr[indexGroup], false))
391 }
392
393 if (indexGroup + 1 < OfferArr.length) {
394 loopGroup(indexGroup + 1);
395 } else {
396 // groups finished
397 callback ? callback() : '';
398 }
399 }, function(error) {
400
401 });
402 }
403 loopGroup(0);
404
405 // relatedProductSelected
406 }, function(error) {
407
408 }); // End relatedProductAttributes
409
410 //callAddable(0);
411 }, function(error) {
412
413 }); // End relatedProductOfferMap
414 }, function(error) {
415
416 }); // End relatedProductOfferAttributes
417 };
418 offer.handleAddableResponse = function(response, attributeId, selected) {
419 var tempObj = {};
420 tempObj.Id = response.Id;
421 tempObj.Attribute__c = attributeId;
422 tempObj.Related_Product_Offer__c = response.cscfga__Configuration_Offer__c;
423 tempObj.Name = response.cscfga__Configuration_Offer__r.Name;
424 tempObj.Selected = selected;
425 tempObj.cscfga__Attributes__r = [];
426
427 for (var i = 0; i < response.cscfga__Attributes__r.length; i++) {
428 tempObj.cscfga__Attributes__r.push({
429 'Name': response.cscfga__Attributes__r[i].Name,
430 'cscfga__Value__c': response.cscfga__Attributes__r[i].cscfga__Value__c || null,
431 'cscfga__Attribute_Definition__c': response.cscfga__Attributes__r[i].cscfga__Attribute_Definition__c,
432 'Visible': (!!response.cscfga__Attributes__r[i].cscfga__Value__c)
433 });
434
435 if (!SharedService.relatedProductToggle[response.cscfga__Attributes__r[i].cscfga__Attribute_Definition__c]) {
436 SharedService.relatedProductToggle[response.cscfga__Attributes__r[i].cscfga__Attribute_Definition__c] = (!!response.cscfga__Attributes__r[
437 i].cscfga__Value__c);
438 };
439 }
440
441 return tempObj;
442 }
443 offer.removeRelatedProduct = function(product) {
444 SharedService.section.relatedProducts.microLoader = true;
445 var rpId = product.Id;
446 var attributeId = product.Attribute__c;
447 var rpConf = product.Related_Product_Configuration__c;
448
449 var cleanWithProperty = function(arrayOfObj, prop, status) {
450 for (var i = 0; i < arrayOfObj.length; i++) {
451 if (arrayOfObj[i][prop] == status) {
452 arrayOfObj.splice(i, 1);
453 }
454 }
455 };
456 RemoteService.deleteOfferRelatedProduct(rpId)
457 .then(function(response) {
458 var RPMap = RemoteService.RelatedProductsStructured[SharedService.activeRelatedProductGroup.index].RelatedProductMap;
459 var updateIndex = SharedService.getIndexById(RPMap, rpId);
460 RPMap.splice(updateIndex, 1);
461
462 var RAMap = RemoteService.RelatedProductsStructured[SharedService.activeRelatedProductGroup.index].RelatedAddableMap;
463
464 RemoteService.addableRelatedProducts(SharedService.activeRelatedProductGroup.id)
465 .then(function(response) {
466 // Clear the array of unselected
467 RAMap.length = 0;
468
469 for (var i = 0; i < response.length; i++) {
470 var tempObj = offer.handleAddableResponse(response[i], SharedService.activeRelatedProductGroup.id, false);
471 RAMap.push(tempObj);
472 };
473 SharedService.activeRelatedProductGroup.count--;
474 SharedService.section.relatedProducts.microLoader = false;
475 }, function(error) {
476
477 });
478
479 console.log("Removed related product with id: ", rpId);
480 }, function(error) {
481
482 });
483 };
484 offer.addRelatedProduct = function(product, index) {
485 var attributeId = product.Attribute__c;
486 var Id = product.Id;
487 var rpc = product.Related_Product_Offer__c;
488 var name = product.Name;
489 var attributes = product.cscfga__Attributes__r;
490
491 SharedService.section.relatedProducts.microLoader = true;
492 var RPMap = RemoteService.RelatedProductsStructured[SharedService.activeRelatedProductGroup.index].RelatedProductMap;
493 var RAMap = RemoteService.RelatedProductsStructured[SharedService.activeRelatedProductGroup.index].RelatedAddableMap;
494
495 // var attributeId = attributeId;
496 var configId = SharedService.activeConfig.id;
497 var offerId = SharedService.activeOffer.id;
498 var relConfigOfferMap = {};
499 relConfigOfferMap[Id] = rpc;
500 var groupSeq = SharedService.activeRelatedProductGroup.index;
501 var prodSeq = RPMap.length;
502
503 // Instead of deleting and inserting, simply emptying current addable RP and replacing it with return of create remote action
504 // var addIndex = SharedService.getIndexById(RPMap, Id);
505 // RPMap[addIndex] = {};
506
507 RAMap.splice(index, 1);
508
509 RemoteService.createOfferRelatedProducts(attributeId, configId, offerId, relConfigOfferMap, groupSeq, prodSeq)
510 .then(function(response) {
511 // Obsolete
512 delete response[0].Configuration__c;
513 delete response[0].Offer__c;
514
515 var tempObj = response[0];
516 tempObj.Product_Sequence__c = RPMap.length;
517 tempObj.Name = name;
518 tempObj.cscfga__Attributes__r = attributes;
519 tempObj.Selected = true;
520
521
522 // RPMap[addIndex] = response[0];
523 // RPMap[addIndex].Product_Sequence__c = RPMap.length;
524 // RPMap[addIndex].Name = name;
525 // RPMap[addIndex].Selected = true;
526
527 RPMap.push(tempObj);
528 SharedService.activeRelatedProductGroup.count++;
529 SharedService.section.relatedProducts.microLoader = false;
530 }, function(error) {
531
532 });
533 };
534 offer.handleRPExpand = function(index, id, count, isOpen) {
535
536 SharedService.activeRelatedProductGroup.index = '';
537 SharedService.activeRelatedProductGroup.id = '';
538 SharedService.activeRelatedProductGroup.count = '';
539
540 if (isOpen) {
541 SharedService.activeRelatedProductGroup.index = index;
542 SharedService.activeRelatedProductGroup.id = id;
543 SharedService.activeRelatedProductGroup.count = '';
544 }
545 };
546 offer.handleGroupClick = function(index, id, count) {
547 if (SharedService.activeRelatedProductGroup.id == id) {
548 SharedService.activeRelatedProductGroup.index = '';
549 SharedService.activeRelatedProductGroup.id = '';
550 SharedService.activeRelatedProductGroup.count = '';
551 } else {
552 SharedService.activeRelatedProductGroup.index = index;
553 SharedService.activeRelatedProductGroup.id = id;
554 SharedService.activeRelatedProductGroup.count = count;
555 }
556 };
557 offer.handleGroupCollapse = function(index, id, isOpen) {
558
559 SharedService.activeRelatedProductGroup.index = '';
560 SharedService.activeRelatedProductGroup.id = '';
561
562 if (isOpen) {
563 SharedService.activeRelatedProductGroup.index = index;
564 SharedService.activeRelatedProductGroup.id = id;
565 }
566 };
567 offer.startWatchingGenerateVariants = function(timeout) {
568 // Function that is gonna be called every interval
569 var checkStatus = function() {
570 RemoteService.checkGenerationStatus()
571 .then(function(response) {
572 // If this is false, the process has not been started
573 if (response.Variant_Generation_Status__c) {
574
575 switch (response.Variant_Generation_Status__c) {
576 case 'Processing':
577 // All good
578 if (response.Offer_Configuration_Variants__r) {
579 SharedService.variantProgress.current = response.Offer_Configuration_Variants__r.length;
580 } else {
581
582 }
583 break;
584 case 'Error':
585 toaster.pop('error', "Generator failed!", response.Variant_Generation_Message__c);
586 $interval.cancel($scope.check);
587 SharedService.section.generatedVariants.loader = false;
588 SharedService.process.variantsGenerationStarted = false;
589 SharedService.process.variantsGenerated = false;
590 $interval.cancel($scope.check);
591 SharedService.variantProgress.current = 0;
592 SharedService.variantProgress.max = '';
593 $interval.cancel($scope.check);
594 break;
595 case 'Complete':
596 SharedService.variantProgress.current = SharedService.variantProgress.max;
597 // Give enough time to see process has finished
598 setTimeout(function() {
599 SharedService.process.variantsGenerationStarted = false;
600 SharedService.variantProgress.current = 0;
601 SharedService.variantProgress.max = '';
602 }, 60);
603 toaster.pop('success', "Variants generated!", response.Variant_Generation_Message__c);
604 // Setting cookie
605 RemoteService.AttributeStructured = angular.copy(SharedService.AttributeStructured);
606 CookieService.setCookie('attrToggle', SharedService.AttributeStructured);
607 // Cancel interval
608 $interval.cancel($scope.check);
609 // get offer variants
610 RemoteService.getOfferVariants()
611 .then(function(response) {
612 SharedService.process.variantsGenerated = true;
613 SharedService.section.generatedVariants.loader = false;
614 }, function(error) {
615
616 });
617 break;
618 };
619 SharedService.activeOffer.Variant_Generation_Message__c = response.Variant_Generation_Message__c;
620 SharedService.activeOffer.Variant_Generation_Status__c = response.Variant_Generation_Status__c;
621 } else {
622
623 }
624 }, function(error) {
625 // Stop loader
626 });
627 };
628 $scope.check = $interval(checkStatus, timeout);
629 };
630 offer.generateVariants = function() {
631 // start loader
632 SharedService.variantProgress.current = 0;
633 SharedService.variantProgress.max = '';
634 // Generate getVariantScope parameter
635 var scopeParam = [];
636 for (var i = 0; i < RemoteService.baseConfigVariants.length; i++) {
637 scopeParam.push(RemoteService.baseConfigVariants[i].id);
638 };
639 // Get total number of variants to be generated
640 RemoteService.getVariantScope(scopeParam)
641 .then(function(response) {
642 SharedService.section.generatedVariants.loader = true;
643 SharedService.process.variantsGenerationStarted = true;
644
645 RemoteService.getOfferPublications()
646 .then(function(response) {
647 if (!response.length) {
648 // Empty the current list
649 RemoteService.generatedVariants = {};
650 RemoteService.createVariants()
651 .then(function(response) {
652 setTimeout(function() {
653 offer.startWatchingGenerateVariants(1000);
654 }, 600);
655 }, function(error) {
656
657 });
658 } else {
659 toaster.pop('error', "Offer is published!", "Cannot generate variants for published offer.");
660 }
661 }, function(error) {
662
663 });
664 }, function(error) {
665
666 });
667 };
668 offer.handleSortClick = function() {
669 //
670 RemoteService.sortRelateProductStructure = angular.copy(RemoteService.RelatedProductsStructured);
671
672 ngDialog.open({
673 template: ResourceConst.path + '/views/forms/sort.html',
674 className: 'ngdialog-theme-default width-800',
675 closeByEscape: true,
676 scope: $scope
677 });
678 };
679 offer.handleSortGroupsClick = function() {
680 //
681 RemoteService.sortRelateProductStructure = angular.copy(RemoteService.RelatedProductsStructured);
682 SharedService.sortScope = 'groups';
683 };
684 offer.handleSortProductClick = function() {
685 //
686 RemoteService.sortRelateProductStructure = angular.copy(RemoteService.RelatedProductsStructured);
687 SharedService.sortScope = 'products';
688 };
689 offer.handleSortVariants = function(attrName) {
690 SharedService.sortReverse = !SharedService.sortReverse;
691 SharedService.sortType = attrName;
692 CookieService.setCookie('sortType', {
693 'sortType': SharedService.sortType
694 });
695 CookieService.setCookie('sortReverse', {
696 'sortReverse': SharedService.sortReverse
697 });
698 };
699 offer.testToaster = function() {
700 console.log("toasted");
701 toaster.pop('success', "success", "Action Success");
702 toaster.pop('warning', "warning", "Action Success");
703 toaster.pop('info', "info", "Action Success");
704 toaster.pop('error', "error", "Action Success");
705 console.log("**************RemoteService.DefAttrMasterObj*******************");
706 console.warn(RemoteService.DefAttrMasterObj);
707 console.log("*********************************");
708 };
709 offer.closeDialog = function(ngDialogId) {
710 ngDialog.close(ngDialogId);
711 };
712 offer.generateDefinitions = function() {
713 RemoteService.getProductDefinitions()
714 .then(function(response) {
715 // Convert getProductDefinitions to object
716 RemoteService.DefAttrMasterObj[RemoteService.baseConfig.cscfga__Product_Definition__r.Id] = {};
717 for (var i = 0; i < response.length; i++) {
718 // Create attributes property to hold structures from getAttributeDefinitions
719 response[i].attributes = [];
720 RemoteService.DefAttrMasterObj[response[i].Id] = response[i];
721 }
722 // Create structure for attribute and product definitions
723 RemoteService.getAttributeDefinitions()
724 .then(function(response) {
725 // Structure object
726 var tempDefObj = {};
727 // Helper object filled with references (attrDefId: prodDefId)
728 var tempIndex = {};
729
730 for (var i = 0; i < response.definitions.length; i++) {
731 // Update reference object
732 tempIndex[response.definitions[i].attrDefId] = response.definitions[i].prodDefId;
733 // Default status as .variables is not returning one which have all negatives
734 response.definitions[i].isFacet = false;
735 response.definitions[i].isOverride = false;
736 response.definitions[i].isVariant = false;
737 // Convert to object
738 tempDefObj[response.definitions[i].attrDefId] = response.definitions[i];
739 }
740
741 // List variables
742 for (var i = 0; i < response.variables.length; i++) {
743 // Update status
744 tempDefObj[response.variables[i].attrDefId].isFacet = response.variables[i].isFacet;
745 tempDefObj[response.variables[i].attrDefId].isOverride = response.variables[i].isOverride;
746 tempDefObj[response.variables[i].attrDefId].isVariant = response.variables[i].isVariant;
747 }
748
749 // Apply the structure to RemoteService.DefAttrMasterObj
750 for (var key in tempIndex) {
751 RemoteService.DefAttrMasterObj[tempIndex[key]].attributes.push(tempDefObj[key]);
752 }
753
754 //RemoteService.DefinitionsStructured = tempDefObj;
755 SharedService.process.offerAttributes = true;
756 SharedService.section.variants.loader = false;
757
758 RemoteService.DefAttrMasterObj[RemoteService.baseConfig.cscfga__Product_Definition__r.Id]
759
760 SharedService.activeProductDefinition.id = RemoteService.baseConfig.cscfga__Product_Definition__r.Id;
761 SharedService.activeProductDefinition.name = RemoteService.DefAttrMasterObj[RemoteService.baseConfig
762 .cscfga__Product_Definition__r.Id].Name;
763 SharedService.activeProductDefinition.originalState = angular.copy(RemoteService.DefAttrMasterObj[
764 RemoteService.baseConfig
765 .cscfga__Product_Definition__r.Id].attributes);
766
767 // For variant indexing
768 var variantIndexArr = RemoteService.DefAttrMasterObj[RemoteService.baseConfig.cscfga__Product_Definition__r
769 .Id].attributes;
770 RemoteService.baseConfigVariants.length = 0;
771 // Convert into array
772 for (var i = 0; i < variantIndexArr.length; i++) {
773
774 // For cookies
775 //Only for base configuration
776 if (variantIndexArr[i].prodDefId == RemoteService.baseConfig.cscfga__Product_Definition__r.Id) {
777 var isSKU = false;
778 // SKU CHECK
779 if (variantIndexArr[i].attributeName === 'SKU' || variantIndexArr[i].attributeName === '_SKU_' ||
780 variantIndexArr[i].attributeName === '__SKU__') {
781 isSKU = true;
782 };
783 SharedService.AttributeStructured[variantIndexArr[i].attrDefId] = {};
784 SharedService.AttributeStructured[variantIndexArr[i].attrDefId].attrDefId = variantIndexArr[i].attrDefId;
785 SharedService.AttributeStructured[variantIndexArr[i].attrDefId].attributeName = variantIndexArr[i].attributeName;
786 SharedService.AttributeStructured[variantIndexArr[i].attrDefId].isVariant = variantIndexArr[i].isVariant;
787 SharedService.AttributeStructured[variantIndexArr[i].attrDefId].isVisible = variantIndexArr[i].isVariant ||
788 isSKU;
789
790 // make or break cookie
791 if (SharedService.activeOffer.Variant_Generation_Status__c == 'Complete') {
792 // Does cookie exist
793 if (CookieService.checkCookie('attrToggle')) {
794 // Get cookie
795 var cookie = CookieService.getCookie('attrToggle');
796 // Is cookie empty
797 if (SharedService.objectLength(cookie)) {
798 RemoteService.AttributeStructured = cookie;
799 } else {
800 RemoteService.AttributeStructured = angular.copy(SharedService.AttributeStructured);
801 }
802 } else {
803 RemoteService.AttributeStructured = angular.copy(SharedService.AttributeStructured);
804 }
805 } else {
806 RemoteService.AttributeStructured = angular.copy(SharedService.AttributeStructured);
807 }
808 };
809
810 //For variant indexing
811 if (variantIndexArr[i].isVariant) {
812 RemoteService.baseConfigVariants.push({
813 'id': variantIndexArr[i].attrDefId,
814 'name': variantIndexArr[i].attributeName
815 });
816 };
817 if (variantIndexArr[i].prodDefId != RemoteService.baseConfig.cscfga__Product_Definition__r.Id) {
818 console.error(
819 "Intruder in variant indexing table (One or more attribute definitions in indexing table does not belong to base configuration)!"
820 );
821 }
822 };
823 CookieService.setCookie('baseConfigVariants', RemoteService.baseConfigVariants);
824 SharedService.section.generatedVariants.loader = false;
825 }, function(error) {
826 SharedService.process.offerAttributes = false;
827 SharedService.section.variants.loader = false;
828 });
829 }, function(error) {
830 SharedService.process.offerAttributes = false;
831 SharedService.section.variants.loader = false;
832 });
833 };
834 //RemoteService.getOfferAttributes();
835 $scope.$watch(() => SharedService.activeTab, function(newValue) {
836 // $scope.$watch(function() { return SharedService.activeTab}, function(newValue) {
837 RemoteService.getOfferPublications();
838 switch (newValue) {
839 case 0:
840 SharedService.section.variants.loader = true;
841 // Only fetch attributes once
842 if (SharedService.process.offerAttributes) {
843 // If attributes were already received
844 SharedService.section.variants.loader = false;
845 } else {
846 // first time getting attributes
847 offer.generateDefinitions();
848 }
849
850 break;
851 case 1:
852 console.warn("Related Products");
853 SharedService.section.relatedProducts.loader = true;
854 // RemoteService.relatedProductAttributes(SharedService.activeConfig.id)
855 if (SharedService.process.relatedProductsGenerated) {
856 SharedService.section.relatedProducts.loader = false;
857 } else {
858 offer.generateRelatedProductStructue(function() {
859 // Callback
860 SharedService.section.relatedProducts.loader = false;
861 SharedService.process.relatedProductsGenerated = true;
862 });
863 }
864 break;
865 case 2:
866 console.warn("Offer tab");
867 SharedService.section.generatedVariants.loader = true;
868 if (SharedService.activeOffer.Variant_Generation_Status__c == 'Complete' || SharedService.activeOffer.Variant_Generation_Status__c ==
869 'Error' || SharedService.activeOffer.Variant_Generation_Status__c == 'Not started') {
870 SharedService.section.generatedVariants.loader = false;
871 }
872 break;
873 }
874 });
875 $scope.$watch(() => SharedService.sortType, function(newValue) {
876 function compare(a, b) {
877 if (a.cscfga__Attributes__r[attrIndex].cscfga__Value__c) {
878 if (a.cscfga__Attributes__r[attrIndex].cscfga__Value__c < b.cscfga__Attributes__r[attrIndex].cscfga__Value__c)
879 return -1;
880 if (a.cscfga__Attributes__r[attrIndex].cscfga__Value__c > b.cscfga__Attributes__r[attrIndex].cscfga__Value__c)
881 return 1;
882 return 0;
883 } else return 0;
884 };
885 var attrIndex = '';
886 if (RemoteService.generatedVariants[0]) {
887 attrIndex = SharedService.getIndexByAttr(RemoteService.generatedVariants[0].cscfga__Attributes__r, 'Name', SharedService.sortType);
888 }
889 if (RemoteService.generatedVariants) {
890 RemoteService.generatedVariants.sort(compare);
891 }
892 });
893 $scope.$watch(() => SharedService.sortReverse, function(newValue) {
894 RemoteService.generatedVariants.reverse();
895 });
896 // $scope.$watch(() => SharedService.activeOffer.Variant_Generation_Status__c, function(newValue) {
897 // if (newValue == 'Complete') {
898 // console.log("Status changed");
899 // SharedService.section.generatedVariants.loader = false;
900 // SharedService.process.variantsGenerationStarted = false;
901 // SharedService.process.variantsGenerated = true;
902 // }
903 // if (newValue == 'Processing') {
904 // SharedService.section.generatedVariants.loader = true;
905 // SharedService.process.variantsGenerationStarted = true;
906 // SharedService.process.variantsGenerated = false;
907 // }
908 // if (newValue == 'Error') {
909 // SharedService.section.generatedVariants.loader = false;
910 // SharedService.process.variantsGenerationStarted = false;
911 // SharedService.process.variantsGenerated = false;
912 // }
913 // });
914 $scope.$watch(() => SharedService.sortScope, function(newValue) {
915 switch (newValue) {
916 case 'none':
917
918 break;
919 case 'groups':
920
921 break;
922 case 'products':
923
924 break;
925 }
926 });
927
928 });