· 8 years ago · Dec 19, 2017, 08:54 AM
1/*
2 *
3 * BX.Sale.Admin.OrderBasket
4 *
5 */
6
7BX.namespace("BX.Sale.Admin.OrderBasket");
8
9BX.Sale.Admin.OrderBasket = function (params)
10{
11 var i,l;
12
13 this.tableId = params.tableId;
14 this.objName = params.objName;
15 this.idPrefix = params.idPrefix;
16 this.productMenu = params.productMenu;
17 this.columnsCount = params.columnsCount;
18 this.visibleColumns = params.visibleColumns;
19 this.isShowXmlId = params.isShowXmlId;
20 this.weightUnit = params.weightUnit;
21
22 this.productsCount = 0;
23 this.customPrices = {};
24 this.customDiscount = {};
25 this.createBasketBottom = params.createBasketBottom || false;
26 this.createProductBasement = params.createProductBasement || false;
27 this.discounts = params.discounts || false;
28 this.iblocksSkuParams = params.iblocksSkuParams || {};
29 this.iblocksSkuParamsOrder = params.iblocksSkuParamsOrder || {};
30 this.mode = params.mode || "edit";
31 this.unRemovableFields = params.unRemovableFields || [];
32 this.formatQuantity = params.formatQuantity;
33 this.qantityUpdaterTimeout = 0;
34 this.qantityUpdaterDelay = 750;
35 this.canSendUpdateQuantityRequest = true;
36
37 if(params.iblocksSkuParams)
38 {
39 for(i in params.iblocksSkuParams)
40 {
41 if(params.iblocksSkuParams.hasOwnProperty(i))
42 this.setIblocksSkuParams(i, params.iblocksSkuParams[i]);
43 }
44 }
45
46 this.fieldsUpdaters = {};
47 if (!this.products)
48 this.products = {};
49
50 if(params.productsOrder && params.productsOrder.length)
51 for(i=0, l=params.productsOrder.length-1; i<=l; i++)
52 if (!!params.products[params.productsOrder[i]])
53 this.productSet(params.products[params.productsOrder[i]], true);
54
55 if(this.createBasketBottom)
56 this.createBottomRow();
57
58 if(this.discounts)
59 this.setDiscounts(this.discounts);
60
61 if(params.totalBlockFields)
62 {
63 this.totalBlock = new BX.Sale.Admin.OrderBasketEditTotal(
64 {
65 fields: params.totalBlockFields,
66 weightUnit: this.weightUnit
67 }
68 );
69 }
70};
71
72BX.Sale.Admin.OrderBasket.prototype.getFieldsUpdaters = function()
73{
74 return {
75 "SUM_PAID": {context: this, callback: this.setSumPaid},
76 "PAYABLE": {context: this, callback: this.setSumUnPaid},
77 "TOTAL_PRICES": {context: this, callback: this.setTotalPrices},
78 "DELIVERY_PRICE": {context: this, callback: this.setDeliveryPrice},
79 "DELIVERY_PRICE_DISCOUNT": {context: this, callback: this.setDeliveryPriceDiscount},
80 "BASKET": {context: this, callback: this.setBasket},
81 "DISCOUNTS_LIST": {context: this, callback: this.setDiscounts}
82 };
83};
84
85BX.Sale.Admin.OrderBasket.prototype.setBasket = function(basket)
86{
87 if(!basket)
88 return;
89
90 var i, l;
91
92 if(basket.IBLOCKS_SKU_PARAMS)
93 for(i in basket.IBLOCKS_SKU_PARAMS)
94 if(basket.IBLOCKS_SKU_PARAMS.hasOwnProperty(i))
95 this.setIblocksSkuParams(i, basket.IBLOCKS_SKU_PARAMS[i]);
96
97 if(basket.IBLOCKS_SKU_PARAMS_ORDER)
98 for(i in basket.IBLOCKS_SKU_PARAMS_ORDER)
99 if(basket.IBLOCKS_SKU_PARAMS_ORDER.hasOwnProperty(i))
100 this.setIblocksSkuParamsOrder(i, basket.IBLOCKS_SKU_PARAMS_ORDER[i]);
101
102 if(basket.ITEMS && basket.ITEMS_ORDER && basket.ITEMS_ORDER.length)
103 {
104 for(i=0, l=basket.ITEMS_ORDER.length-1; i<=l; i++)
105 if (!!basket.ITEMS[basket.ITEMS_ORDER[i]])
106 this.productSet(basket.ITEMS[basket.ITEMS_ORDER[i]], true);
107 }
108 else
109 {
110 this.showEmptyRow();
111 }
112
113 this.hideLoadingRow();
114};
115
116BX.Sale.Admin.OrderBasket.prototype.setTotalPrices = function(prices)
117{
118 this.totalBlock.setFieldValue("SUM_PAID", prices.SUM_PAID);
119 this.totalBlock.setFieldValue("SUM_UNPAID", prices.SUM_UNPAID);
120};
121BX.Sale.Admin.OrderBasket.prototype.setDeliveryPrice = function(price)
122{
123 this.totalBlock.setFieldValue("PRICE_DELIVERY", price);
124};
125
126BX.Sale.Admin.OrderBasket.prototype.setDeliveryPriceDiscount = function(price)
127{
128 this.totalBlock.setFieldValue("PRICE_DELIVERY_DISCOUNTED", price);
129};
130
131BX.Sale.Admin.OrderBasket.prototype.setSumPaid = function(summ)
132{
133 this.totalBlock.setFieldValue("SUM_PAID", summ);
134};
135
136BX.Sale.Admin.OrderBasket.prototype.setSumUnPaid = function(summ)
137{
138 this.totalBlock.setFieldValue("SUM_UNPAID", summ);
139};
140
141BX.Sale.Admin.OrderBasket.prototype.showEmptyRow = function ()
142{
143 var row = BX(this.idPrefix+"sale-adm-order-edit-basket-empty-row");
144
145 if(row)
146 row.style.display = "";
147};
148
149BX.Sale.Admin.OrderBasket.prototype.hideEmptyRow = function ()
150{
151 var row = BX(this.idPrefix+"sale-adm-order-edit-basket-empty-row");
152
153 if(row)
154 row.style.display = "none";
155};
156
157BX.Sale.Admin.OrderBasket.prototype.hideLoadingRow = function ()
158{
159 var row = BX(this.idPrefix+"sale-adm-order-basket-loading-row");
160
161 if(row)
162 row.style.display = "none";
163};
164
165BX.Sale.Admin.OrderBasket.prototype.createBottomRow = function(products)
166{
167 if(!this.createBasketBottom)
168 return;
169
170 var table = BX(this.tableId),
171 td1 = BX.create('td',{
172 html: '<strong style="margin-left: 10px;">'+
173 BX.message('SALE_ORDER_BASKET_PROD_COUNT')+': '+
174 '<span id="'+this.idPrefix+'sale-order-basket-products-count">'+this.productsCount+'</span>'+
175 '</strong>'
176 }),
177 td2 = BX.create('td',{
178 props: {
179 id: this.idPrefix+"sale-order-basket-bottom-discounts"
180 }
181 }),
182 tbody = BX.create('tbody', {props:{
183 "id": this.idPrefix+"sale-order-basket-bottom"
184 },
185 "style": {
186 "textAlign": "left",
187 "borderBottom": "1px solid #DDD"
188 },
189 children: [
190 BX.create('tr', {
191 children: [
192 td1,
193 td2
194 ]
195 })
196 ]
197 });
198
199
200 td1.colSpan = this.mode == "view" ? 1 : 2;
201 td2.colSpan = this.columnsCount-1;
202 table.appendChild(tbody);
203};
204
205BX.Sale.Admin.OrderBasket.prototype.setDiscounts = function(discounts)
206{
207 this.discounts = discounts;
208 var basketDiscounts = BX(this.idPrefix+"sale-order-basket-bottom-discounts"),
209 filteredDiscounts = [];
210
211 if(!discounts || !discounts.ORDER || !discounts.ORDER.DISCOUNT_LIST)
212 return;
213
214 for(var i=0, l=discounts.ORDER.DISCOUNT_LIST.length; i<l; i++)
215 {
216 var deliveryId = discounts.ORDER.DISCOUNT_LIST[i];
217
218 if(discounts.DISCOUNT_LIST
219 && discounts.DISCOUNT_LIST[deliveryId]
220 && discounts.DISCOUNT_LIST[deliveryId].ACTIONS_DESCR
221 && discounts.DISCOUNT_LIST[deliveryId].ACTIONS_DESCR.BASKET
222 )
223 {
224 filteredDiscounts[i] = discounts.DISCOUNT_LIST[deliveryId];
225 filteredDiscounts[i].DESCR = discounts.DISCOUNT_LIST[deliveryId].ACTIONS_DESCR.BASKET;
226 }
227 }
228
229 if(basketDiscounts && filteredDiscounts)
230 {
231 basketDiscounts = BX.cleanNode(basketDiscounts, false);
232 basketDiscounts.appendChild(
233 this.createDiscountsNodeBasket(filteredDiscounts)
234 );
235 }
236};
237
238BX.Sale.Admin.OrderBasket.prototype.createDiscountsNodeBasket = function(discounts)
239{
240 return BX.Sale.Admin.OrderEditPage.createDiscountsNode(
241 "",
242 "DISCOUNT_LIST",
243 discounts,
244 this.discounts,
245 "VIEW"
246 );
247};
248
249BX.Sale.Admin.OrderBasket.prototype.getProductBasketCode = function(product)
250{
251 if(!product.BASKET_CODE)
252 {
253 if(product.IS_SET_ITEM == "Y")
254 {
255 var d = new Date();
256 product.BASKET_CODE = "set_"+d.getTime()+Math.floor(Math.random()*1000);
257 }
258 else
259 {
260 BX.debug("product.BASKET_CODE is undefined!");
261 }
262 }
263
264 return product.BASKET_CODE;
265};
266
267BX.Sale.Admin.OrderBasket.prototype.setProductsCount = function(count)
268{
269 if(!this.createBasketBottom)
270 return;
271
272 var node = BX(this.idPrefix+'sale-order-basket-products-count');
273
274 if(node)
275 node.innerHTML = count;
276};
277
278BX.Sale.Admin.OrderBasket.prototype.productAdd = function(product)
279{
280 var basketCode = this.getProductBasketCode(product),
281 productRow = BX(this.createProductRowId(basketCode, product));
282
283 if(productRow)
284 return;
285
286 if(parseFloat(product.PRICE) != parseFloat(product.BASE_PRICE) && product.CUSTOM_PRICE && product.CUSTOM_PRICE == 'Y')
287 this.customPrices[basketCode] = product.PRICE;
288
289 productRow = this.createProductRow(basketCode, product);
290 var table = BX(this.tableId);
291
292 if(this.createBasketBottom && (bottom=BX(this.idPrefix+"sale-order-basket-bottom")))
293 table.insertBefore(productRow, bottom);
294 else
295 table.appendChild(productRow);
296};
297
298BX.Sale.Admin.OrderBasket.prototype.productSet = function(product, isReplaceExisting)
299{
300 var basketCode = this.getProductBasketCode(product);
301
302 if(this.productsCount <= 0)
303 this.hideEmptyRow();
304
305 if(BX(this.createProductRowId(basketCode, product))) // product already exist
306 {
307 if(isReplaceExisting)
308 {
309 this.productReplace(product, basketCode);
310 }
311 else
312 {
313 this.setProductQuantity(
314 basketCode,
315 this.roundQuantity(parseFloat(this.getProductQuantity(basketCode)) + parseFloat(product.QUANTITY))
316 );
317 }
318 }
319 else
320 {
321 this.productAdd(product);
322
323 if(product.IS_SET_ITEM != "Y")
324 this.setProductsCount(++this.productsCount);
325 }
326
327 //sets
328 if(product.SET_ITEMS && product.SET_ITEMS.length)
329 {
330 for(var i = product.SET_ITEMS.length-1; i>=0; i--)
331 {
332 product.SET_ITEMS[i].BASKET_CODE = "set_" + product.BASKET_CODE + "_"+product.SET_ITEMS[i].OFFER_ID;
333 this.productSet(product.SET_ITEMS[i], true);
334 }
335 }
336
337 this.setRowNumbers();
338};
339
340BX.Sale.Admin.OrderBasket.prototype.productReplace = function(product, oldProductBasketCode)
341{
342 var oldProductRow = BX(this.createProductRowId(oldProductBasketCode, product));
343
344 if(!oldProductRow)
345 return;
346
347 this.onProductDelete(oldProductBasketCode);
348
349 var basketCode = this.getProductBasketCode(product),
350 newProductRow = this.createProductRow(basketCode, product);
351
352 if(!newProductRow)
353 return;
354
355 if(!BX.hasClass(oldProductRow, "basket-bundle-child-hidden"))
356 BX.removeClass(newProductRow, "basket-bundle-child-hidden");
357
358 oldProductRow.parentNode.replaceChild(newProductRow, oldProductRow);
359 this.setRowNumbers();
360};
361
362BX.Sale.Admin.OrderBasket.prototype.onProductDelete = function(basketCode)
363{
364 BX.Sale.Admin.OrderEditPage.unRegisterProductFieldsUpdaters(basketCode);
365};
366
367BX.Sale.Admin.OrderBasket.prototype.roundQuantity = function(quantity)
368{
369 if (this.formatQuantity == 'AUTO' || parseInt(this.formatQuantity) <= 0)
370 quantity = Math.round(quantity*10000)/10000;
371 else
372 quantity = Math.round(quantity*Math.pow(10, this.formatQuantity)) / Math.pow(10, this.formatQuantity);
373
374 return quantity;
375};
376
377BX.Sale.Admin.OrderBasket.prototype.getDiscountCellId = function(basketCode)
378{
379 return this.idPrefix+"sale-order-basket-product-"+basketCode+"-discount-cell";
380};
381
382BX.Sale.Admin.OrderBasket.prototype.createDiscountCell = function(basketCode, product)
383{
384 var discountsNode = BX.create("text",{html: " "}),
385 isDiscountsExist = false,
386 skuPropsExist = product.SKU_PROPS && Object.keys(product.SKU_PROPS).length > 0;
387
388 if(this.discounts && this.discounts.RESULT && this.discounts.RESULT.BASKET)
389 {
390 discountsNode = BX.Sale.Admin.OrderEditPage.createDiscountsNode(
391 basketCode,
392 "BASKET",
393 this.discounts.RESULT.BASKET[basketCode] ? this.discounts.RESULT.BASKET[basketCode] : {},
394 this.discounts,
395 "VIEW"
396 );
397
398 isDiscountsExist = this.discounts.RESULT.BASKET[basketCode] ? true : false;
399 }
400
401 var td = BX.create('td',{
402 props:{
403 id: this.getDiscountCellId()
404 },
405 children: [
406 BX.create('div',{
407 children: [discountsNode]
408 })
409 ]
410 });
411
412 td.colSpan = this.columnsCount-1;
413
414 if(skuPropsExist || isDiscountsExist)
415 td.style.borderTop = "1px solid #ddd";
416
417 return td;
418};
419
420BX.Sale.Admin.OrderBasket.prototype.setIblocksSkuParamsOrder = function(iBlockId, iblocksSkuParamOrder)
421{
422 if(!this.iblocksSkuParamsOrder)
423 this.iblocksSkuParamsOrder = {};
424
425 if(!this.iblocksSkuParamsOrder[iBlockId])
426 this.iblocksSkuParamsOrder[iBlockId] = [];
427
428 if(iblocksSkuParamOrder)
429 {
430 for(var i in iblocksSkuParamOrder)
431 {
432 if(!iblocksSkuParamOrder.hasOwnProperty(i))
433 continue;
434
435 var exist = false;
436
437 for(var j in this.iblocksSkuParamsOrder[iBlockId])
438 {
439 if(!this.iblocksSkuParamsOrder[iBlockId].hasOwnProperty(j))
440 continue;
441
442 if(this.iblocksSkuParamsOrder[iBlockId][j] == iblocksSkuParamOrder[i])
443 {
444 exist = true;
445 break;
446 }
447 }
448
449 if(!exist)
450 this.iblocksSkuParamsOrder[iBlockId][i] = iblocksSkuParamOrder[i];
451 }
452 }
453};
454
455BX.Sale.Admin.OrderBasket.prototype.setIblocksSkuParams = function(iBlockId, iblocksSkuParam)
456{
457 if(!this.iblocksSkuParams)
458 this.iblocksSkuParams = {};
459
460 if(!this.iblocksSkuParams[iBlockId])
461 this.iblocksSkuParams[iBlockId] = {};
462
463 if(iblocksSkuParam)
464 {
465 for(var i in iblocksSkuParam)
466 {
467 if(!iblocksSkuParam.hasOwnProperty(i))
468 continue;
469
470 if(this.iblocksSkuParams[iBlockId][i] !== undefined && this.iblocksSkuParams[iBlockId][i]['VALUES'] !== undefined && iblocksSkuParam[i]['VALUES'] !== undefined)
471 {
472 for(var j in iblocksSkuParam[i]['VALUES'])
473 if(iblocksSkuParam[i]['VALUES'].hasOwnProperty(j))
474 this.iblocksSkuParams[iBlockId][i]['VALUES'][j] = iblocksSkuParam[i]['VALUES'][j];
475 }
476 else
477 {
478 this.iblocksSkuParams[iBlockId][i] = iblocksSkuParam[i];
479 }
480 }
481 }
482};
483
484BX.Sale.Admin.OrderBasket.prototype.createProductRowBasement = function(basketCode, product)
485{
486 if(!this.createProductBasement)
487 return;
488
489 var result = null,
490 tr = BX.create('tr', {props:{
491 id: this.idPrefix+"sale-order-basket-product-"+basketCode+"-basement",
492 className: "bdb-line"
493 }
494 }),
495 propsTd = this.createProductBasementSkuCell(basketCode, product),
496 discountTd = this.createDiscountCell(basketCode, product);
497
498 if(propsTd && discountTd)
499 {
500 tr.appendChild(propsTd);
501 tr.appendChild(discountTd);
502 result = tr;
503 }
504
505 return result;
506};
507
508BX.Sale.Admin.OrderBasket.prototype.createProductBasementSkuCell = function(basketCode, product)
509{
510 var tbl = this.createSkuPropsTable(basketCode, product),
511 result = null;
512
513 if(tbl)
514 {
515 result = BX.create('td',{
516 children: [ tbl ]
517 });
518 }
519
520 return result;
521};
522
523BX.Sale.Admin.OrderBasket.prototype.createProductRowId = function(basketCode, product)
524{
525 var id = this.idPrefix+"sale-order-basket-product-"+basketCode;
526
527 if(product)
528 {
529 if(product.IS_SET_ITEM == "Y" && product.PARENT_OFFER_ID)
530 id += "-"+product.PARENT_OFFER_ID;
531 }
532
533 return id;
534};
535
536/**
537 * @param {string} basketCode
538 * @param {object} product
539 * @return {node} product table row Dom node
540 */
541BX.Sale.Admin.OrderBasket.prototype.createProductRow = function(basketCode, product)
542{
543 var cellContent,
544 tbody = BX.create('tbody', {props:{
545 "id": this.createProductRowId(basketCode, product)
546 },
547 "style": {
548 "textAlign": "left",
549 "borderBottom": "1px solid #DDD"
550 }
551 }),
552 menuCell = this.createMenuCell(basketCode, product),
553 tr = BX.create('tr');
554
555 if(menuCell)
556 {
557 if(this.createProductBasement)
558 menuCell.rowSpan = 2;
559
560 tr.appendChild(menuCell);
561 }
562
563 var field,
564 hiddenFields = [];
565
566 if(product.IS_SET_ITEM != "Y")
567 {
568 hiddenFields = this.createHiddenFields(basketCode, product);
569 this.products[basketCode] = product;
570 tbody.setAttribute("data-basket-code", basketCode);
571
572 if(product.IS_SET_PARENT && product.OLD_PARENT_ID)
573 tbody.setAttribute("data-old-parent-id-parent", product.OLD_PARENT_ID);
574 }
575 else
576 {
577 BX.addClass(tbody,"bundle-child-"+product.OLD_PARENT_ID);
578 BX.addClass(tbody,"basket-bundle-child-hidden");
579 BX.addClass(tbody,"bundle-child");
580 }
581
582 for(var fieldId in this.visibleColumns)
583 {
584 if(!this.visibleColumns.hasOwnProperty(fieldId))
585 continue;
586
587 cellContent = this.createProductCell(basketCode, product, fieldId);
588
589 if(cellContent)
590 {
591 if(hiddenFields)
592 while(field = hiddenFields.pop())
593 cellContent.appendChild(field);
594
595 tr.appendChild(cellContent);
596 }
597 }
598
599 tr.setAttribute('onmouseover', this.objName+'.onProductRowMouseOver(this);');
600 tr.setAttribute('onmouseout', this.objName+'.onProductRowMouseOut(this);');
601 tbody.appendChild(tr);
602
603 if((this.createProductBasement && ((product.SKU_PROPS || (product.PROPS && product.PROPS.length > 0 && this.mode == "view")) && product.IS_SET_ITEM != "Y")) || product.DISCOUNTS)
604 {
605 var rowBasement = this.createProductRowBasement(basketCode, product);
606
607 if(rowBasement)
608 tbody.appendChild(rowBasement);
609 }
610
611 return tbody;
612};
613
614BX.Sale.Admin.OrderBasket.prototype.createHiddenFields = function(basketCode, product)
615{
616 return [];
617};
618
619BX.Sale.Admin.OrderBasket.prototype.createMenuCell = function(basketCode, product)
620{
621 var menuSpan,
622 menuContent = this.createProductMenuContent(basketCode, product);
623
624 if(menuContent.length <= 0)
625 return false;
626
627 if(product.IS_SET_ITEM != "Y")
628 {
629
630 menuSpan = BX.create('span', {
631 props:{
632 className: "adm-s-order-item-title-icon"
633 }
634 });
635
636 BX.bind(
637 menuSpan,
638 "click",
639 BX.proxy( function(e){
640 menuSpan.blur();
641 BX.adminList.ShowMenu(menuSpan, menuContent);
642 },
643 this
644 )
645 );
646 }
647 else
648 {
649 menuSpan = BX.create('span', {html: " "});
650 }
651
652 return BX.create(
653 'td',
654 {
655 props:{
656 className: 'tac bdb-line',
657 id: this.idPrefix+"sale-order-basket-product-"+basketCode+"-menu"
658 },
659 children:[
660 menuSpan
661 ]
662 }
663 );
664};
665
666BX.Sale.Admin.OrderBasket.prototype.onHeadMenu = function(node)
667{
668 BX.adminList.ShowMenu(node,
669 [{
670 "ICON": "view",
671 "TEXT": BX.message("SALE_ORDER_BASKET_ROW_SETTINGS"),
672 "ACTION": this.objName+'.onSettings()',
673 "DEFAULT":true
674 }
675 ]);
676};
677
678BX.Sale.Admin.OrderBasket.prototype.setRowNumbers = function()
679{
680 if(!this.visibleColumns["NUMBER"])
681 return;
682
683 var table = BX(this.tableId),
684 basketCode = "",
685 span = null,
686 counter = 1;
687
688
689 for(var i=0, l=table.tBodies.length; i<l; i++)
690 {
691 if(BX.hasClass(table.tBodies[i], "bundle-child"))
692 continue;
693
694 basketCode = table.tBodies[i].getAttribute("data-basket-code");
695
696 if(!basketCode)
697 continue;
698
699 span = BX(this.idPrefix+"sale_order_product_"+basketCode+"_number");
700
701 if(span)
702 span.innerHTML = counter++;
703 else
704 BX.debug("BX.Sale.Admin.OrderBasket.prototype.setRowNumbers can't find: "+basketCode);
705 }
706};
707
708BX.Sale.Admin.OrderBasket.prototype.getPriceCellId = function(basketCode)
709{
710 return this.idPrefix+"sale-order-basket-product-"+basketCode+"-price-cell";
711};
712
713BX.Sale.Admin.OrderBasket.prototype.getProductSummCellId = function(basketCode)
714{
715 return this.idPrefix+'sale_order_edit_product_'+basketCode+'_summ';
716};
717
718BX.Sale.Admin.OrderBasket.prototype.getQuantityCellId = function(basketCode)
719{
720 return this.idPrefix+"sale-order-basket-product-"+basketCode+"-quantity-cell";
721};
722
723BX.Sale.Admin.OrderBasket.prototype.createProductCell = function(basketCode, product, fieldId)
724{
725 var result = null,
726 cellNodes = [],
727 fieldValue = product[fieldId],
728 tdClass = "",
729 _this = this;
730
731 switch(fieldId)
732 {
733 case "NUMBER":
734 cellNodes.push(
735 BX.create(
736 'span',
737 {
738 props:{
739 id: product.IS_SET_ITEM != "Y" ? this.idPrefix+"sale_order_product_"+basketCode+"_number" : " "
740 },
741 html: " "
742 }
743 )
744 );
745 break;
746
747 case "NAME":
748
749 if(product.IS_SET_PARENT == "Y" && product.SET_ITEMS)
750 {
751 var bundleShow = BX.create('a',{
752 props:{
753 href:"javascript:void(0);",
754 className: "dashed-link show-set-link"
755 },
756 html: BX.message("SALE_ORDER_BASKET_EXPAND")
757 });
758
759 BX.bind(bundleShow, "click", function(e){
760 var source = e.target || e.srcElement;
761 _this.onToggleBundleChildren(product.OLD_PARENT_ID, source);
762 });
763
764 cellNodes.push(
765 BX.create('div', {
766 children: [bundleShow]
767 })
768 );
769 }
770
771 if(product.EDIT_PAGE_URL)
772 {
773 if(!fieldValue)
774 fieldValue = BX.message('SALE_ORDER_BASKET_NO_NAME');
775
776 node = BX.create('a',{
777 props:{
778 href:product.EDIT_PAGE_URL,
779 target:"_blank"
780 },
781 html: BX.util.htmlspecialchars(fieldValue)
782 });
783 }
784 else
785 {
786 var name = product[fieldId] ? product[fieldId] : BX.message('SALE_ORDER_BASKET_NO_NAME');
787 node = BX.create('span', {
788 style: {fontWeight: "bold"},
789 html: BX.util.htmlspecialchars(name)
790 });
791 }
792
793 cellNodes.push(node);
794
795 if(product.RECOMMENDATION && product.RECOMMENDATION.length > 0)
796 cellNodes.push(BX.create('div',{props: {className: 'bx-adm-bigdata-icon-medium-inner'}}));
797
798 break;
799
800 case "QUANTITY":
801 if(typeof product.MEASURE_TEXT != "undefined")
802 cellNodes.push(document.createTextNode(" "+product.MEASURE_TEXT+" "));
803
804 product['QUANTITY'] = this.roundQuantity(parseFloat(product['QUANTITY']));
805
806 if(product.IS_SET_ITEM == "Y")
807 {
808 node = BX.Sale.Admin.OrderBasket.prototype.createFieldQuantity(basketCode, product, fieldId);
809 node.id = this.getQuantityCellId(basketCode);
810 cellNodes.push(node);
811 }
812 else
813 {
814 cellNodes.push(this.createFieldQuantity(basketCode, product, fieldId));
815 }
816
817 break;
818
819 case "AVAILABLE":
820 cellNodes.push(this.createTextField(basketCode, product, fieldId));
821 break;
822
823 case "PRICE":
824 if(product.IS_SET_ITEM == "Y")
825 {
826 cellNodes.push(BX.Sale.Admin.OrderBasket.prototype.createFieldPrice(basketCode, product, fieldId));
827 }
828 else
829 {
830 var priceNode = this.createFieldPrice(basketCode, product, fieldId);
831 priceNode.id = this.getPriceCellId(basketCode);
832 cellNodes.push(priceNode);
833 }
834 break;
835
836 case "SUM":
837 var price;
838 if(typeof(this.customPrices[basketCode]) == "undefined")
839 price = product.PRICE;
840 else
841 price = this.customPrices[basketCode];
842
843 var sum = price*product.QUANTITY;
844
845 cellNodes.push(
846 BX.create('strong', {
847 props:{
848 id: this.getProductSummCellId(basketCode)
849 },
850 html: BX.Sale.Admin.OrderEditPage.currencyFormat(sum),
851 style: {whiteSpace: 'nowrap'}
852 })
853 );
854 break;
855
856 case "IMAGE":
857 cellNodes.push(this.createFieldImage(basketCode, product, fieldId));
858 tdClass = "adm-s-order-table-ddi-table-img";
859 break;
860
861 case "PROPS":
862 var node = this.createFieldSkuProps(basketCode, product, fieldId);
863
864 if(node)
865 cellNodes.push(node);
866 break;
867 }
868
869
870 if(fieldId.indexOf("PROPERTY_") === 0)
871 {
872 var html = "",
873 propCode = fieldId.substr("PROPERTY_".length);
874
875 if(product.PRODUCT_PROPS_VALUES && product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"])
876 {
877 var iblocks = [product.OFFERS_IBLOCK_ID, product.IBLOCK_ID];
878
879 for(var idx in iblocks)
880 {
881 if(!iblocks.hasOwnProperty(idx))
882 continue;
883
884 var iblockId = iblocks[idx];
885
886 if(!iblockId)
887 continue;
888
889 if(this.iblocksSkuParams[iblockId])
890 {
891 for(var i in this.iblocksSkuParams[iblockId])
892 {
893 if(!this.iblocksSkuParams[iblockId].hasOwnProperty(i))
894 continue;
895
896 if(this.iblocksSkuParams[iblockId][i].CODE != propCode)
897 continue;
898
899 if(!this.iblocksSkuParams[iblockId][i].VALUES[product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"]])
900 continue;
901/*
902 if(this.iblocksSkuParams[iblockId][i].VALUES[product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"]]["PICT"]
903 && this.iblocksSkuParams[iblockId][i].VALUES[product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"]]["PICT"].length > 0
904 )
905 {
906 html = '<img src="'+this.iblocksSkuParams[iblockId][i].VALUES[product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"]]["PICT"]+'">';
907 }*/
908 if(this.iblocksSkuParams[iblockId][i].VALUES[product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"]]["NAME"]
909 && this.iblocksSkuParams[iblockId][i].VALUES[product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"]]["NAME"].length > 0
910 )
911 {
912 html = this.iblocksSkuParams[iblockId][i].VALUES[product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"]]["NAME"];
913 }
914 else
915 {
916 html = product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"];
917 }
918
919 if(html)
920 html = BX.util.htmlspecialchars(html);
921
922 break;
923 }
924
925 if(html.length > 0)
926 break;
927 }
928 }
929
930 if(html.length <= 0)
931 html = product.PRODUCT_PROPS_VALUES[fieldId+"_VALUE"];
932 }
933 else
934 {
935 html = "";
936 }
937
938 cellNodes.push(BX.create('span', {html: html}));
939 }
940
941 if(cellNodes.length > 0)
942 {
943 result = BX.create('td');
944
945 if(tdClass)
946 BX.addClass(result, tdClass);
947
948 while(cellNodes.length > 0)
949 result.appendChild(cellNodes.pop());
950
951 if(fieldId == "NAME")
952 {
953 result.style.minWidth = "250px";
954
955 if(product.IS_SET_ITEM == "Y")
956 {
957 result.style.fontStyle = "italic";
958 result.style.paddingLeft = "40px";
959 }
960 }
961 }
962
963 return result;
964};
965
966BX.Sale.Admin.OrderBasket.prototype.onToggleBundleChildren = function(oldParentId, anchor)
967{
968 if(anchor.innerHTML == BX.message("SALE_ORDER_BASKET_TURN"))
969 anchor.innerHTML = BX.message("SALE_ORDER_BASKET_EXPAND");
970 else
971 anchor.innerHTML = BX.message("SALE_ORDER_BASKET_TURN");
972
973 var bundleChildren = BX.findChildren(BX(this.tableId), {className: "bundle-child-"+oldParentId}, true);
974
975 for(var i in bundleChildren)
976 if(bundleChildren.hasOwnProperty(i))
977 BX.toggleClass(bundleChildren[i], "basket-bundle-child-hidden");
978};
979
980BX.Sale.Admin.OrderBasket.prototype.createFieldSkuProps = function(basketCode, product, fieldId)
981{
982 return this.createSkuPropsTable(basketCode, product);
983};
984
985BX.Sale.Admin.OrderBasket.prototype.createSkuPropsTable = function(basketCode, product)
986{
987 var table = BX.create('table'),
988 html,
989 propIdx = 0,
990 skuCodes = [];
991
992 if(product.SKU_PROPS)
993 {
994 for(var skuId in product.SKU_PROPS)
995 {
996 if(!product.SKU_PROPS.hasOwnProperty(skuId))
997 continue;
998
999 if(!product.SKU_PROPS[skuId]["VALUE"])
1000 continue;
1001
1002 if(product.SKU_PROPS[skuId]["VALUE"]["ID"] == "-")
1003 continue;
1004
1005 if(product.SKU_PROPS[skuId]["VALUE"] && product.SKU_PROPS[skuId]["VALUE"]["PICT"])
1006 {
1007 html = '<div style="width: 17px; height: 17px; text-align: center; border: 1px solid gray;">'+
1008 '<img width="17" height="17" src="'+product.SKU_PROPS[skuId]["VALUE"]["PICT"]+'">'+
1009 '</div>';
1010 }
1011 else
1012 {
1013 if(!product.SKU_PROPS[skuId]["VALUE"]["NAME"])
1014 product.SKU_PROPS[skuId]["VALUE"]["NAME"] = skuId;
1015
1016 html = '<div style="font-size: 9px; padding: 2px 5px; text-align: center; border: 1px solid gray;">'+BX.util.htmlspecialchars(product.SKU_PROPS[skuId]["VALUE"]["NAME"])+'</div>';
1017 }
1018
1019 // Unlinked property of type E
1020 if(!product.SKU_PROPS[skuId]["NAME"])
1021 product.SKU_PROPS[skuId]["NAME"] = skuId;
1022
1023 table.appendChild(
1024 BX.create('tr',{
1025 children: [
1026 BX.create('td',{
1027 html: '<span style="color: gray; font-size: 11px">'+BX.util.htmlspecialchars(product.SKU_PROPS[skuId]["NAME"])+': </span>'
1028 }),
1029 BX.create('td',{html: html})
1030 ]
1031 })
1032 );
1033
1034 skuCodes.push(product.SKU_PROPS[skuId]["CODE"]);
1035 propIdx++;
1036 }
1037 }
1038
1039 if(product.PROPS)
1040 {
1041 for(var i in product.PROPS)
1042 {
1043 if(!product.PROPS.hasOwnProperty(i))
1044 continue;
1045
1046 if(!product.PROPS[i] || skuCodes.indexOf(product.PROPS[i]["CODE"]) != -1)
1047 continue;
1048
1049 if(!product.PROPS[i]["NAME"]) product.PROPS[i]["NAME"] = "";
1050 if(!product.PROPS[i]["VALUE"]) product.PROPS[i]["VALUE"] = "";
1051
1052 var tr = BX.create('tr',{
1053 children: [
1054 BX.create('td',{
1055 html: '<span style="color: gray; font-size: 11px">'+BX.util.htmlspecialchars(product.PROPS[i]["NAME"])+': </span>'
1056 }),
1057 BX.create('td',{
1058 html: '<div style="font-size: 9px; padding: 2px 5px; text-align: center;">'+BX.util.htmlspecialchars(product.PROPS[i]["VALUE"])+'</div>'
1059 })
1060 ]
1061 });
1062
1063 if(!this.isShowXmlId && (product.PROPS[i]["CODE"] == "PRODUCT.XML_ID" || product.PROPS[i]["CODE"] == "CATALOG.XML_ID"))
1064 tr.style.display = "none";
1065
1066 table.appendChild(tr);
1067 propIdx++;
1068 }
1069 }
1070
1071 return table;
1072};
1073
1074BX.Sale.Admin.OrderBasket.prototype.createFieldQuantity = function(basketCode, product, fieldId)
1075{
1076 return this.createTextField(basketCode, product, fieldId);
1077};
1078
1079BX.Sale.Admin.OrderBasket.prototype.createFieldImage = function(basketCode, product, fieldId)
1080{
1081 var pictureNode, resultNode;
1082
1083 if(product.PICTURE_URL)
1084 {
1085 pictureNode = BX.create('img',{
1086 props:{src: product.PICTURE_URL}
1087 });
1088 }
1089 else
1090 {
1091 pictureNode = BX.create('div',{
1092 props:{
1093 className: "no_foto"
1094 },
1095 text: BX.message("SALE_ORDER_BASKET_NO_PICTURE")
1096 });
1097 }
1098
1099 if(typeof product.EDIT_PAGE_URL != "undefined")
1100 {
1101 resultNode = BX.create('a',{
1102 props: {
1103 href: product.EDIT_PAGE_URL ? product.EDIT_PAGE_URL : "",
1104 target:"_blank"
1105 },
1106 children: [pictureNode]
1107 });
1108
1109 resultNode.style.textAlign = (this.mode == "edit" ? "left" : "center");
1110 }
1111 else
1112 {
1113 resultNode = BX.create('div',{
1114 style: {
1115 width: '150px',
1116 textAlign: this.mode == "edit" ? "left" : "center"
1117 },
1118 children: [pictureNode]
1119 });
1120 }
1121
1122 return resultNode;
1123};
1124
1125BX.Sale.Admin.OrderBasket.prototype.createFieldPrice = function(basketCode, product, fieldId)
1126{
1127 var resultNode = BX.create('span',{props:{className: "view_price"}}),
1128 price = "";
1129
1130 if(typeof product.PRICE != "undefined")
1131 {
1132 price = BX.Sale.Admin.OrderEditPage.currencyFormat(product.PRICE);
1133 }
1134
1135 resultNode.appendChild(BX.create('div', {
1136 html: price,
1137 style: {whiteSpace: 'nowrap'}
1138 }
1139 ));
1140
1141 if(price != "")
1142 {
1143 var display = "none";
1144
1145 if(parseFloat(product.BASE_PRICE) > 0 && parseFloat(product.PRICE) != parseFloat(product.BASE_PRICE))
1146 display = "";
1147
1148 var basePrice = BX.create('div',{
1149 props: {className: "base_price"},
1150 style: {display: display},
1151 children: [
1152 BX.create('span',{
1153 html: BX.Sale.Admin.OrderEditPage.currencyFormat(product.BASE_PRICE)
1154 })
1155 ]
1156 }),
1157 priceNotes = BX.create('div',{
1158 props: {className: "base_price_title"},
1159 style: {display: (((product.CUSTOM_PRICE && product.CUSTOM_PRICE == "Y") || product.NOTES) ? "": "none")},
1160 text: ((product.CUSTOM_PRICE && product.CUSTOM_PRICE == "Y") ? BX.message("SALE_ORDER_BASKET_BASE_CATALOG_PRICE") : product.NOTES)
1161 });
1162
1163 resultNode.appendChild(basePrice);
1164 resultNode.appendChild(priceNotes);
1165 }
1166
1167 return resultNode;
1168};
1169
1170BX.Sale.Admin.OrderBasket.prototype.createTextField = function(basketCode, product, fieldId)
1171{
1172 var text = typeof product[fieldId] != "undefined" ? product[fieldId] : "";
1173 return BX.create('span', {text: text+" "});
1174};
1175
1176BX.Sale.Admin.OrderBasket.prototype.onSettings = function()
1177{
1178 this.settingsDialog.show();
1179};
1180
1181BX.Sale.Admin.OrderBasket.prototype.createProductMenuContent = function(basketCode, product)
1182{
1183 return [];
1184};
1185
1186BX.Sale.Admin.OrderBasket.prototype.onProductRowMouseOver = function(rowNode)
1187{
1188 BX.addClass(rowNode, "tr_hover");
1189};
1190
1191BX.Sale.Admin.OrderBasket.prototype.onProductRowMouseOut = function(rowNode)
1192{
1193 BX.removeClass(rowNode, "tr_hover");
1194};
1195
1196/*
1197 *
1198 * OrderBasketEdit
1199 *
1200 */
1201
1202BX.Sale.Admin.OrderBasketEdit = function(params)
1203{
1204 this.productEditDialog = new BX.Sale.Admin.OrderBasketProductEditDialog(this);
1205 BX.Sale.Admin.OrderBasket.call(this, params);
1206
1207 this.settingsDialog = new BX.Sale.Admin.OrderBasket.SettingsDialog({
1208 basket: this
1209 });
1210};
1211
1212BX.Sale.Admin.OrderBasketEdit.prototype = Object.create(BX.Sale.Admin.OrderBasket.prototype);
1213
1214BX.Sale.Admin.OrderBasketEdit.prototype.getProductIdBySkuProps = function(params)
1215{
1216 BX.Sale.Admin.OrderAjaxer.sendRequest(
1217 BX.Sale.Admin.OrderEditPage.ajaxRequests.getProductIdBySkuProps(params)
1218 );
1219};
1220
1221BX.Sale.Admin.OrderBasketEdit.prototype.setBasket = function(basket)
1222{
1223 if(!basket)
1224 return;
1225
1226 var i, l;
1227
1228 if(basket.IBLOCKS_SKU_PARAMS)
1229 for(i in basket.IBLOCKS_SKU_PARAMS)
1230 if(basket.IBLOCKS_SKU_PARAMS.hasOwnProperty(i))
1231 this.setIblocksSkuParams(i, basket.IBLOCKS_SKU_PARAMS[i]);
1232
1233 if(basket.IBLOCKS_SKU_PARAMS_ORDER)
1234 for(i in basket.IBLOCKS_SKU_PARAMS_ORDER)
1235 if(basket.IBLOCKS_SKU_PARAMS_ORDER.hasOwnProperty(i))
1236 this.setIblocksSkuParamsOrder(i, basket.IBLOCKS_SKU_PARAMS_ORDER[i]);
1237
1238 if(typeof basket.WEIGHT_FOR_HUMAN !== "undefined")
1239 this.totalBlock.setFieldValue('WEIGHT', basket.WEIGHT_FOR_HUMAN);
1240
1241 //just update some fields
1242 if(basket.PRICES_UPDATED && basket.PRICES_UPDATED.length > 0)
1243 {
1244 //update price fields if price was changed
1245 for(i in basket.PRICES_UPDATED)
1246 if(basket.PRICES_UPDATED.hasOwnProperty(i))
1247 this.updateProductPriceCell(basket.ITEMS[i]);
1248
1249 //update discounts.... always
1250 for(i in basket.ITEMS)
1251 if(basket.ITEMS.hasOwnProperty(i))
1252 this.updateProductDiscountsCell(basket.ITEMS[i]);
1253
1254 //add new if it exists
1255 if(basket.NEW_ITEM_BASKET_CODE && basket.ITEMS[basket.NEW_ITEM_BASKET_CODE])
1256 this.productSet(basket.ITEMS[basket.NEW_ITEM_BASKET_CODE], true);
1257 }
1258 else if(basket.LIGHT && basket.LIGHT == 'Y')
1259 {
1260 for(i in this.products)
1261 {
1262 if(!this.products.hasOwnProperty(i))
1263 continue;
1264
1265 if(typeof basket.ITEMS[i] == 'undefined')
1266 this.productDelete(i);
1267 }
1268
1269 if(basket.ADDED_PRODUCTS)
1270 {
1271 for(i in basket.ADDED_PRODUCTS)
1272 {
1273 if(basket.ADDED_PRODUCTS.hasOwnProperty(i))
1274 {
1275 this.productSet(basket.ITEMS[basket.ADDED_PRODUCTS[i]], true);
1276 delete(basket.ITEMS[basket.ADDED_PRODUCTS[i]]);
1277 }
1278 }
1279 }
1280
1281 if(basket.ITEMS)
1282 {
1283 for(i in basket.ITEMS)
1284 if(basket.ITEMS.hasOwnProperty(i))
1285 this.productUpdateLight(basket.ITEMS[i]);
1286 }
1287 }
1288 else if(basket.ITEMS_ORDER && basket.ITEMS_ORDER.length)
1289 {
1290 for(i in this.products)
1291 if(this.products.hasOwnProperty(i))
1292 this.productDelete(i);
1293
1294 for(i = 0, l=basket.ITEMS_ORDER.length-1; i <= l; i++)
1295 this.productSet(basket.ITEMS[basket.ITEMS_ORDER[i]], true);
1296 }
1297
1298 if(!basket.ITEMS || !basket.ITEMS_ORDER || !basket.ITEMS_ORDER.length)
1299 {
1300 this.showEmptyRow();
1301 }
1302
1303 this.hideLoadingRow();
1304};
1305
1306BX.Sale.Admin.OrderBasketEdit.prototype.updateProductPriceCell = function(product)
1307{
1308 var basketCode = this.getProductBasketCode(product),
1309 id = this.getPriceCellId(basketCode),
1310 oldPriceCell = BX(id);
1311
1312 if(!oldPriceCell)
1313 return;
1314
1315 /*
1316 if(this.customPrices[basketCode])
1317 delete this.customPrices[basketCode];
1318 */
1319 var newPriceCell = this.createFieldPrice(basketCode, product),
1320 priceParent = oldPriceCell.parentNode;
1321
1322 priceParent.removeChild(oldPriceCell);
1323 priceParent.appendChild(newPriceCell);
1324 newPriceCell.id = id;
1325 this.updateProductSumm(basketCode);
1326};
1327
1328BX.Sale.Admin.OrderBasketEdit.prototype.updateProductDiscountsCell = function(product)
1329{
1330 var basketCode = this.getProductBasketCode(product),
1331 id = this.getDiscountCellId(basketCode),
1332 oldDiscountCell = BX(id);
1333
1334 if(!oldDiscountCell)
1335 return;
1336
1337 var newDiscountCell = this.createDiscountCell(basketCode, product),
1338 discountParent = oldDiscountCell.parentNode;
1339
1340 discountParent.removeChild(oldDiscountCell);
1341 discountParent.appendChild(newDiscountCell);
1342 newDiscountCell.id = id;
1343};
1344
1345BX.Sale.Admin.OrderBasketEdit.prototype.productUpdateLight = function(product)
1346{
1347 var basketCode = this.getProductBasketCode(product),i;
1348
1349 product["NOTES"] = this.products[basketCode]["NOTES"];
1350 this.updateProductPriceCell(product);
1351 this.updateProductDiscountsCell(product);
1352
1353 //don't send requests to refresh data
1354 this.canSendUpdateQuantityRequest = false;
1355
1356 if(this.getProductQuantity(basketCode) != product["QUANTITY"])
1357 this.setProductQuantity(basketCode, product["QUANTITY"]);
1358
1359 if(this.getCustomPrice(basketCode) != product["CUSTOM_PRICE"])
1360 this.setCustomPrice(basketCode, product["CUSTOM_PRICE"]);
1361
1362 for(i in product)
1363 if(product.hasOwnProperty(i))
1364 this.products[basketCode][i] = product[i];
1365
1366 if(product.SET_ITEMS)
1367 for(i in product.SET_ITEMS)
1368 if(product.SET_ITEMS.hasOwnProperty(i))
1369 this.updateSetQuantity(product.SET_ITEMS[i], basketCode);
1370
1371 this.canSendUpdateQuantityRequest = true;
1372};
1373
1374BX.Sale.Admin.OrderBasketEdit.prototype.updateSetQuantity = function(product, parentBasketCode)
1375{
1376 var basketCode = "set_" + parentBasketCode + "_" + product.OFFER_ID,
1377 qSpqn = BX(this.getQuantityCellId(basketCode)),
1378 summCell = BX(this.getProductSummCellId(basketCode));
1379
1380 if(qSpqn)
1381 qSpqn.innerHTML = product.QUANTITY;
1382
1383 if(summCell)
1384 summCell.innerHTML = BX.Sale.Admin.OrderEditPage.currencyFormat(product.QUANTITY*product.PRICE);
1385};
1386
1387BX.Sale.Admin.OrderBasketEdit.prototype.setCustomPrice = function(basketCode, value)
1388{
1389 var form = BX.Sale.Admin.OrderEditPage.getForm();
1390 form.elements[this.getFieldName(basketCode, "CUSTOM_PRICE")].value = value;
1391};
1392
1393BX.Sale.Admin.OrderBasketEdit.prototype.getCustomPrice = function(basketCode)
1394{
1395 var form = BX.Sale.Admin.OrderEditPage.getForm();
1396 return form.elements[this.getFieldName(basketCode, "CUSTOM_PRICE")].value;
1397};
1398
1399BX.Sale.Admin.OrderBasketEdit.prototype.getParamsBySkuProps = function(params)
1400{
1401 var customPrice = false;
1402
1403 if(this.customPrices[params.replaceBasketCode] !== undefined)
1404 customPrice = this.customPrices[params.replaceBasketCode];
1405
1406 BX.Sale.Admin.OrderAjaxer.sendRequest(
1407 BX.Sale.Admin.OrderEditPage.ajaxRequests.addProductToBasketBySkuProps({
1408 oldProductId: params.oldProductId,
1409 oldProductIblock: params.oldProductIblock,
1410 quantity: params.quantity,
1411 replaceBasketCode: params.replaceBasketCode,
1412 skuPropsVal: params.skuPropsVal,
1413 columns: this.visibleColumns,
1414 customPrice: customPrice
1415 })
1416 );
1417};
1418
1419BX.Sale.Admin.OrderBasketEdit.prototype.getParamsByProductId = function(params, iBlockId, callback)
1420{
1421 var customPrice = false;
1422
1423 if(this.customPrices[params.replaceBasketCode] !== undefined)
1424 customPrice = this.customPrices[params.replaceBasketCode];
1425
1426 BX.Sale.Admin.OrderAjaxer.sendRequest(
1427 BX.Sale.Admin.OrderEditPage.ajaxRequests.addProductToBasket(
1428 params.id,
1429 params.quantity,
1430 params.replaceBasketCode,
1431 this.visibleColumns,
1432 customPrice
1433 )
1434 );
1435};
1436
1437BX.Sale.Admin.OrderBasketEdit.prototype.createProductBasementSkuCell = function(basketCode, product)
1438{
1439 var td = BX.create('td',{props:{id: this.idPrefix+"sale-order-basket-product-"+basketCode+"-basement-sku"}}),
1440 divSku = BX.create('div',{props:{className: 'adm-s-order-table-ddi-table-sku'}}),
1441 possibleSkuProps = product.SKU_PROPS_POSSIBLE_VALUES,
1442 title, currentSkuId;
1443
1444 for(var i in this.iblocksSkuParamsOrder[product.OFFERS_IBLOCK_ID])
1445 {
1446 if(!this.iblocksSkuParamsOrder[product.OFFERS_IBLOCK_ID].hasOwnProperty(i))
1447 continue;
1448
1449 var idx = this.iblocksSkuParamsOrder[product.OFFERS_IBLOCK_ID][i];
1450
1451 if(!possibleSkuProps[idx])
1452 continue;
1453
1454 title = this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][idx]["NAME"];
1455 currentSkuId = product.SKU_PROPS[idx]["VALUE"]["ID"];
1456
1457 divSku.appendChild(
1458 this.createSkuSelector(basketCode, idx, title, currentSkuId, possibleSkuProps[idx], product)
1459 );
1460 }
1461
1462 td.appendChild(divSku);
1463 return td;
1464};
1465
1466BX.Sale.Admin.OrderBasketEdit.prototype.createSkuSelector = function(basketCode, skuId, title, activeItemId, items, product)
1467{
1468 var ul = BX.create('ul', {
1469 attrs: {
1470 "data-sku-id": skuId
1471 }}),
1472 styleType = 'sku',
1473 variantsCount = 0,
1474 activeVariant = 0;
1475
1476 for(var idx in this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][skuId]["ORDER"])
1477 {
1478 if(!this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][skuId]["ORDER"].hasOwnProperty(idx))
1479 continue;
1480
1481 var item = this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][skuId]["ORDER"][idx],
1482 found = false;
1483
1484 for(var i in items)
1485 {
1486 if(!items.hasOwnProperty(i))
1487 continue;
1488
1489 if(items[i] == this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][skuId]['VALUES'][item]['ID'])
1490 {
1491 found = true;
1492 break;
1493 }
1494 }
1495
1496 if(!found)
1497 continue;
1498
1499 var html,
1500 itemId = this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][skuId]["VALUES"][item]["ID"];
1501
1502 if(this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][skuId]["VALUES"][item]["PICT"])
1503 {
1504 html = '<img src="'+this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][skuId]["VALUES"][item]["PICT"]+'">';
1505 }
1506 else
1507 {
1508 styleType = 'size';
1509 html = BX.util.htmlspecialchars(this.iblocksSkuParams[product.OFFERS_IBLOCK_ID][skuId]["VALUES"][item]["NAME"]);
1510 }
1511
1512 var span = BX.create('span',{
1513 props: {
1514 className: 'cnt'
1515 },
1516 html: html
1517 }),
1518 li = BX.create('li',{
1519 attrs: {
1520 "data-value": BX.util.htmlspecialchars(item),
1521 "data-id": itemId
1522 },
1523 children:[span]
1524 });
1525
1526 if(itemId == activeItemId)
1527 {
1528 BX.addClass(li,'bx-active');
1529 activeVariant = variantsCount;
1530 }
1531
1532 ul.appendChild(li);
1533 BX.bind(span, "click", BX.delegate(
1534 function(e) {
1535
1536 var span = e.target || e.srcElement,
1537 activeValue = span.parentNode.getAttribute("data-value") || span.parentNode.parentNode.getAttribute("data-value"),
1538 activeId = span.parentNode.getAttribute("data-id") || span.parentNode.parentNode.getAttribute("data-id"),
1539 propsVal = this.getSkuProps(basketCode, product.SKU_PROPS);
1540
1541 activeValue = BX.util.htmlspecialcharsback(activeValue);
1542
1543 if(activeId == activeItemId)
1544 return;
1545
1546 propsVal[skuId] = activeId;
1547
1548 this.getProductIdBySkuProps({
1549 productId: product.PRODUCT_ID,
1550 iBlockId: product.OFFERS_IBLOCK_ID,
1551 skuProps: propsVal,
1552 skuOrder: this.iblocksSkuParamsOrder[product.OFFERS_IBLOCK_ID],
1553 changedSkuId: skuId,
1554 callback: BX.delegate(
1555 function(result){
1556
1557 if(!result.OFFER_ID)
1558 {
1559 BX.debug("can't find product id for set of sku props");
1560 return;
1561 }
1562
1563 var offerId = result.OFFER_ID;
1564
1565 for(var i in this.products)
1566 {
1567 if(!this.products.hasOwnProperty(i))
1568 continue;
1569
1570 if(i == basketCode)
1571 continue;
1572
1573 if(this.products[i].OFFER_ID == offerId)
1574 if(!confirm(BX.message("SALE_ORDER_BASKET_POSITION_EXISTS").replace("#NAME#", this.products[i].NAME)))
1575 return;
1576 }
1577
1578 this.onSkuSelectorClick(basketCode, ul, skuId, activeValue, activeId);
1579 this.onSkuPropSelect(basketCode, offerId); },
1580 this
1581 )}
1582 );
1583 },
1584 this
1585 ));
1586
1587 variantsCount++;
1588 }
1589
1590 var leftArrow = BX.create('div', {
1591 props:{
1592 className: "adm-s-item-detail-"+styleType+"-box-arrow left"
1593 },
1594 events:{
1595 click: BX.delegate( function(){ this.skuSelectorScrollLeft(ul, variantsCount); }, this)
1596 }
1597 }),
1598 rightArrow = BX.create('div', {
1599 props:{
1600 className: "adm-s-item-detail-"+styleType+"-box-arrow right"
1601 },
1602 events:{
1603 click: BX.delegate( function(){ this.skuSelectorScrollRight(ul, variantsCount); }, this)
1604 }
1605 });
1606
1607 var result = BX.create('div',{
1608 props: {
1609 className: "adm-s-item-detail-sku"
1610 },
1611 children: [
1612 BX.create('div', {props: {className: "adm-s-item-detail-"+styleType+"-title"}, text: title}),
1613 leftArrow,
1614 rightArrow,
1615 BX.create('div', {
1616 props:{
1617 className: "adm-s-item-detail-"+styleType+"-box"
1618 },
1619 children: [
1620 BX.create('div', {
1621 props:{
1622 className: "adm-s-item-detail-"+styleType+"-box-container"
1623 },
1624 children: [
1625 ul,
1626 BX.create('input',{
1627 props: {
1628 type: 'hidden',
1629 name: this.getFieldName(basketCode, "SKU")+"["+skuId+"]",
1630 value: activeItemId
1631 }
1632 }
1633 )
1634 ]
1635 })
1636 ]
1637 })
1638 ]
1639 });
1640
1641 var currentOffset = (-1) * this.skuSelectorCountScrollOffset(activeVariant, variantsCount);
1642 this.skuSelectorSetScroll(ul, currentOffset);
1643 this.skuSelectorSetArrowsVisibility (ul, currentOffset, variantsCount);
1644
1645 return result;
1646};
1647
1648BX.Sale.Admin.OrderBasketEdit.prototype.getFieldsUpdaters = function()
1649{
1650 return {
1651 "TOTAL_PRICES": {context: this, callback: this.setTotalPrices},
1652 "BASKET": {context: this, callback: this.setBasket},
1653 "SUM_PAID": {context: this, callback: this.setSumPaid},
1654 "PAYABLE": {context: this, callback: this.setSumUnPaid},
1655 "TAX_VALUE": {context: this, callback: this.setTaxValue},
1656 "DELIVERY_PRICE": {context: this, callback: this.setDeliveryPrice},
1657 "DELIVERY_PRICE_DISCOUNT": {context: this, callback: this.setDeliveryPriceDiscount},
1658 "SHIPMENT[1][PRICE_DELIVERY]": {context: this, callback: this.setDeliveryPrice},
1659 "COUPONS_LIST": {context: this, callback: this.setCoupons},
1660 "DISCOUNTS_LIST": {context: this, callback: this.setDiscounts}
1661 };
1662};
1663
1664BX.Sale.Admin.OrderBasketEdit.prototype.getProductPrice = function(basketCode)
1665{
1666 var form = BX.Sale.Admin.OrderEditPage.getForm();
1667 return form.elements[this.getFieldName(basketCode, "PRICE")].value;
1668};
1669
1670BX.Sale.Admin.OrderBasketEdit.prototype.setCoupons = function(coupons)
1671{
1672 return BX.Sale.Admin.OrderBasketCoupons.setCoupons(coupons);
1673};
1674
1675BX.Sale.Admin.OrderBasketEdit.prototype.getProductQuantity = function(basketCode)
1676{
1677 var form = BX.Sale.Admin.OrderEditPage.getForm();
1678 return form.elements[this.getFieldName(basketCode, "QUANTITY")].value;
1679};
1680
1681BX.Sale.Admin.OrderBasketEdit.prototype.setProductQuantity = function(basketCode, quantity)
1682{
1683 var form = BX.Sale.Admin.OrderEditPage.getForm(),
1684 qInput = form.elements[this.getFieldName(basketCode, "QUANTITY")];
1685
1686 if(!quantity || quantity < 0)
1687 quantity = 0;
1688 else
1689 quantity = this.roundQuantity(quantity);
1690
1691 if(qInput)
1692 qInput.value = quantity;
1693
1694 this.onProductQuantityChange({productId: basketCode});
1695 return quantity;
1696};
1697
1698BX.Sale.Admin.OrderBasketEdit.prototype.setProductPrice = function(basketCode, price)
1699{
1700 var form = BX.Sale.Admin.OrderEditPage.getForm(),
1701 pInput = form.elements[this.getFieldName(basketCode, "PRICE")],
1702 bpInput = form.elements[this.getFieldName(basketCode, "BASE_PRICE")],
1703 bpDiv = BX(this.idPrefix+"sale-order-basket-product-"+basketCode+"-base_price"),
1704 fpDiv = BX(this.idPrefix+"sale-order-basket-product-"+basketCode+"-formatted_price");
1705
1706 price = parseFloat(price);
1707
1708 if(!price || price < 0)
1709 price = 0;
1710 else
1711 price = parseFloat(price);
1712
1713 if(pInput)
1714 pInput.value = price;
1715
1716 if(fpDiv)
1717 fpDiv.innerHTML = BX.Sale.Admin.OrderEditPage.currencyFormat(price);
1718
1719 if(this.products[basketCode].CUSTOM_PRICE == 'Y')
1720 this.customPrices[basketCode] = price;
1721 else
1722 delete(this.customPrices[basketCode]);
1723
1724 if(bpInput && bpDiv)
1725 {
1726 var basePrice = parseFloat(bpInput.value);
1727
1728 if(basePrice > 0 && basePrice != price)
1729 {
1730 bpDiv.style.display = "";
1731 }
1732 else
1733 {
1734 bpDiv.style.display = "none";
1735 }
1736 }
1737
1738 this.onProductPriceChange({productId: basketCode});
1739 return price;
1740};
1741
1742BX.Sale.Admin.OrderBasketEdit.prototype.onProductQuantityChange = function(params)
1743{
1744 var fieldName = this.getFieldName(params.productId, "QUANTITY"),
1745 _this = this,
1746 canSendUpdateQuantityRequest = this.canSendUpdateQuantityRequest;
1747
1748 clearTimeout(this.qantityUpdaterTimeout);
1749
1750 this.qantityUpdaterTimeout = setTimeout( function(){
1751
1752 var tmp = _this.canSendUpdateQuantityRequest;
1753 _this.canSendUpdateQuantityRequest = canSendUpdateQuantityRequest;
1754 BX.Sale.Admin.OrderEditPage.callConcreteFieldUpdater(fieldName, params.productId);
1755 _this.canSendUpdateQuantityRequest = tmp;
1756
1757 },
1758 this.qantityUpdaterDelay
1759 );
1760};
1761
1762BX.Sale.Admin.OrderBasketEdit.prototype.onProductPriceChange = function(params)
1763{
1764 BX.Sale.Admin.OrderEditPage.callConcreteFieldUpdater(this.getFieldName(params.productId, "PRICE"), params.productId);
1765};
1766
1767BX.Sale.Admin.OrderBasketEdit.prototype.updateProductQuantity = function(productId)
1768{
1769 this.updateProductSumm(productId);
1770
1771 if(this.canSendUpdateQuantityRequest)
1772 {
1773 BX.Sale.Admin.OrderAjaxer.sendRequest(
1774 BX.Sale.Admin.OrderEditPage.ajaxRequests.refreshOrderData({
1775 operation: "FIELD_UPDATE",
1776 updatedFieldId: "QUANTITY",
1777 productId: productId
1778 })
1779 );
1780 }
1781};
1782
1783BX.Sale.Admin.OrderBasketEdit.prototype.updateProductPrice = function(productId)
1784{
1785 this.updateProductSumm(productId);
1786
1787 BX.Sale.Admin.OrderAjaxer.sendRequest(
1788 BX.Sale.Admin.OrderEditPage.ajaxRequests.refreshOrderData({
1789 operation: "FIELD_UPDATE",
1790 updatedFieldId: "PRICE",
1791 productId: productId
1792 })
1793 );
1794};
1795
1796BX.Sale.Admin.OrderBasketEdit.prototype.updateProductSumm = function(productId)
1797{
1798 var summ = BX(this.getProductSummCellId(productId));
1799
1800 if(!summ)
1801 return;
1802
1803 summ.innerHTML = BX.Sale.Admin.OrderEditPage.currencyFormat(this.getProductPrice(productId)*this.getProductQuantity(productId));
1804};
1805
1806BX.Sale.Admin.OrderBasketEdit.prototype.setTotalPrices = function(prices)
1807{
1808 this.totalBlock.setFieldValue("PRICE_BASKET", prices.PRICE_BASKET);
1809 this.totalBlock.setFieldValue("PRICE_BASKET_DISCOUNTED", prices.PRICE_BASKET_DISCOUNTED);
1810 this.totalBlock.setFieldValue("PRICE_DELIVERY", prices.PRICE_DELIVERY);
1811 this.totalBlock.setFieldValue("PRICE_DELIVERY_DISCOUNTED", prices.PRICE_DELIVERY_DISCOUNTED);
1812 this.totalBlock.setFieldValue("TAX_VALUE", prices.TAX_VALUE);
1813 this.totalBlock.setFieldValue("SUM_PAID", prices.SUM_PAID);
1814 this.totalBlock.setFieldValue("SUM_UNPAID", prices.SUM_UNPAID);
1815};
1816
1817BX.Sale.Admin.OrderBasketEdit.prototype.setDeliveryPrice = function(price)
1818{
1819 this.totalBlock.setFieldValue("PRICE_DELIVERY", price);
1820};
1821
1822BX.Sale.Admin.OrderBasketEdit.prototype.setDeliveryPriceDiscount = function(price)
1823{
1824 this.totalBlock.setFieldValue("PRICE_DELIVERY_DISCOUNTED", price);
1825};
1826
1827BX.Sale.Admin.OrderBasketEdit.prototype.setTaxValue = function(tax)
1828{
1829 this.totalBlock.setFieldValue("TAX_VALUE", tax);
1830};
1831
1832BX.Sale.Admin.OrderBasketEdit.prototype.addProductSearch = function(params)
1833{
1834 var funcName = this.objName+'.getParamsByProductId';
1835 window[funcName] = BX.proxy(function(params, iblockId){this.getParamsByProductId(params, iblockId);}, this);
1836
1837 var popup = new BX.CDialog({
1838 content_url: '/bitrix/tools/sale/product_search_dialog.php?'+
1839 'lang='+BX.Sale.Admin.OrderEditPage.languageId+
1840 '&LID='+BX.Sale.Admin.OrderEditPage.siteId+
1841 '&caller=order_edit'+
1842 '&func_name='+funcName+
1843 '&STORE_FROM_ID=0',
1844 height: Math.max(500, window.innerHeight-400),
1845 width: Math.max(800, window.innerWidth-400),
1846 draggable: true,
1847 resizable: true,
1848 min_height: 500,
1849 min_width: 800
1850 });
1851 BX.addCustomEvent(popup, 'onWindowRegister', BX.defer(function(){
1852 popup.Get().style.position = 'fixed';
1853 popup.Get().style.top = (parseInt(popup.Get().style.top) - BX.GetWindowScrollPos().scrollTop) + 'px';
1854 }));
1855
1856 popup.Show();
1857};
1858
1859BX.Sale.Admin.OrderBasketEdit.prototype.getCoupons = function()
1860{
1861 var form = BX.Sale.Admin.OrderEditPage.getForm();
1862 return form.elements['COUPONS'].value;
1863};
1864
1865BX.Sale.Admin.OrderBasketEdit.prototype.onSkuPropSelect = function(basketCode, newOfferId)
1866{
1867 this.getParamsByProductId(
1868 {
1869 id: newOfferId,
1870 quantity: this.getProductQuantity(basketCode),
1871 replaceBasketCode: basketCode
1872 },
1873 0
1874 );
1875};
1876
1877BX.Sale.Admin.OrderBasketEdit.prototype.getSkuProps = function(basketCode, skuProps)
1878{
1879 var result = {},
1880 form = BX.Sale.Admin.OrderEditPage.getForm();
1881
1882 for(var propId in skuProps)
1883 {
1884 if(!skuProps.hasOwnProperty(propId))
1885 continue;
1886
1887 var fieldName = this.getFieldName(basketCode, "SKU")+"["+propId+"]";
1888
1889 if(typeof form.elements[fieldName] != "undefined")
1890 result[propId] = form.elements[fieldName].value;
1891 }
1892
1893 return result;
1894};
1895
1896BX.Sale.Admin.OrderBasketEdit.prototype.getFieldName = function(basketCode, type)
1897{
1898 return "PRODUCT["+basketCode+"]["+type+"]";
1899};
1900
1901BX.Sale.Admin.OrderBasketEdit.prototype.productDeleteClick = function(basketCode)
1902{
1903 //this.productDelete(basketCode);
1904
1905 BX.Sale.Admin.OrderAjaxer.sendRequest(
1906 BX.Sale.Admin.OrderEditPage.ajaxRequests.refreshOrderData({
1907 operation: "PRODUCT_DELETE",
1908 basketCode: basketCode
1909 })
1910 );
1911};
1912
1913BX.Sale.Admin.OrderBasketEdit.prototype.productDelete = function(basketCode)
1914{
1915 var productRaw = BX(this.createProductRowId(basketCode));
1916
1917 if(!productRaw)
1918 return;
1919
1920 var oldParentId = productRaw.getAttribute('data-old-parent-id-parent');
1921
1922 //set
1923 if(oldParentId)
1924 {
1925 var bundleChildren = BX.findChildren(productRaw.parentNode, {className: "bundle-child-"+oldParentId}, false);
1926
1927 for(var i in bundleChildren)
1928 if(bundleChildren.hasOwnProperty(i))
1929 bundleChildren[i].parentNode.removeChild(bundleChildren[i]);
1930 }
1931
1932 this.onProductDelete(basketCode);
1933
1934 productRaw.parentNode.removeChild(productRaw);
1935
1936 this.setRowNumbers();
1937 this.setProductsCount(--this.productsCount);
1938
1939 if(this.productsCount <= 0)
1940 this.showEmptyRow();
1941
1942 if(this.customPrices[basketCode])
1943 delete this.customPrices[basketCode];
1944
1945 delete(this.products[basketCode]);
1946};
1947
1948BX.Sale.Admin.OrderBasketEdit.prototype.onSkuSelectorClick = function(basketCode, ul, skuId, activeValue, activeId)
1949{
1950 for(var i=0,l=ul.children.length; i<l; i++)
1951 {
1952 var li = ul.children[i];
1953
1954 if(li.getAttribute('data-value') != BX.util.htmlspecialchars(activeValue))
1955 BX.removeClass(li,'bx-active');
1956 else
1957 BX.addClass(li,'bx-active');
1958 }
1959
1960 this.setSkuInput(basketCode, skuId, activeId);
1961};
1962
1963BX.Sale.Admin.OrderBasketEdit.prototype.setSkuInput = function(basketCode, skuId, value)
1964{
1965 var inputName = this.getFieldName(basketCode, "SKU")+"["+skuId+"]",
1966 form = BX.Sale.Admin.OrderEditPage.getForm();
1967
1968 form.elements[inputName].value = value;
1969};
1970
1971BX.Sale.Admin.OrderBasketEdit.prototype.skuSelectorSetArrowsVisibility = function(ul, currentOffset, variantsCount)
1972{
1973 var leftArrow = ul.parentNode.parentNode.parentNode.childNodes[1];
1974
1975 if(variantsCount <= 5 || currentOffset >= 0)
1976 leftArrow.style.display = 'none';
1977 else
1978 leftArrow.style.display = '';
1979
1980 var rightArrow = ul.parentNode.parentNode.parentNode.childNodes[2];
1981
1982 if(variantsCount <= 5 || currentOffset <= 5 - variantsCount)
1983 rightArrow.style.display = 'none';
1984 else
1985 rightArrow.style.display = '';
1986
1987};
1988
1989BX.Sale.Admin.OrderBasketEdit.prototype.skuSelectorScrollLeft = function(ul, variantsCount)
1990{
1991 var currentOffset = this.skuSelectorGetScrollOffset(ul);
1992
1993 if(currentOffset >= 0)
1994 return false;
1995
1996 this.skuSelectorSetScroll(ul, ++currentOffset);
1997 this.skuSelectorSetArrowsVisibility (ul, currentOffset);
1998 return true;
1999};
2000
2001BX.Sale.Admin.OrderBasketEdit.prototype.skuSelectorScrollRight = function(ul, variantsCount)
2002{
2003 var currentOffset = this.skuSelectorGetScrollOffset(ul);
2004
2005 if(currentOffset <= 5 - variantsCount)
2006 return false;
2007
2008 this.skuSelectorSetScroll(ul, --currentOffset);
2009 this.skuSelectorSetArrowsVisibility (ul, currentOffset, variantsCount);
2010 return true;
2011};
2012
2013BX.Sale.Admin.OrderBasketEdit.prototype.createDiscountCell = function(basketCode, product)
2014{
2015 var discountsNode = BX.create("text",{html: " "}),
2016 isDiscountsExist = false,
2017 skuPropsExist = product.SKU_PROPS && Object.keys(product.SKU_PROPS).length > 0;
2018
2019
2020 if(this.discounts && this.discounts.RESULT && this.discounts.RESULT.BASKET)
2021 {
2022 discountsNode = BX.Sale.Admin.OrderEditPage.createDiscountsNode(
2023 basketCode,
2024 "BASKET",
2025 this.discounts.RESULT.BASKET[basketCode] ? this.discounts.RESULT.BASKET[basketCode] : {},
2026 this.discounts,
2027 "EDIT"
2028 );
2029
2030 isDiscountsExist = this.discounts.RESULT.BASKET[basketCode] ? true : false;
2031 }
2032
2033 var td = BX.create('td',{
2034 props:{
2035 id: this.getDiscountCellId(basketCode)
2036 },
2037 children: [
2038 BX.create('div',{
2039 children: [discountsNode]
2040 })
2041 ]
2042 });
2043
2044 td.colSpan = this.columnsCount-1;
2045
2046
2047 if(skuPropsExist || isDiscountsExist)
2048 td.style.borderTop = "1px solid #ddd";
2049
2050 return td;
2051};
2052
2053BX.Sale.Admin.OrderBasketEdit.prototype.skuSelectorCountScrollOffset = function(activeSkuOrder, skuCount)
2054{
2055 var result = 0;
2056 activeSkuOrder = activeSkuOrder+1;
2057
2058 if(skuCount > 5 && activeSkuOrder > 2)
2059 {
2060 var diff = skuCount - activeSkuOrder;
2061
2062 result = activeSkuOrder - 3;
2063
2064 if(diff < 2)
2065 result = result - (2 - diff);
2066 }
2067
2068 return result;
2069};
2070
2071BX.Sale.Admin.OrderBasketEdit.prototype.skuSelectorGetStepSize = function()
2072{
2073 return 30; //px width of sku square
2074};
2075
2076BX.Sale.Admin.OrderBasketEdit.prototype.skuSelectorGetScrollOffset = function(el)
2077{
2078 if (!el)
2079 return false;
2080
2081 var result = parseInt(el.style.marginLeft, 10);
2082
2083 if(!result)
2084 result = 0;
2085 else
2086 result = parseInt(result/this.skuSelectorGetStepSize(), 10);
2087
2088 return result;
2089};
2090
2091BX.Sale.Admin.OrderBasketEdit.prototype.skuSelectorSetScroll = function(el, stepsCount)
2092{
2093 el.style.marginLeft = stepsCount * this.skuSelectorGetStepSize() + 'px';
2094};
2095
2096BX.Sale.Admin.OrderBasketEdit.prototype.onCouponsRecount = function()
2097{
2098 BX.Sale.Admin.OrderAjaxer.sendRequest(
2099 BX.Sale.Admin.OrderEditPage.ajaxRequests.refreshOrderData({
2100 operation: "COUPONS_RECOUNT"
2101 })
2102 );
2103};
2104
2105BX.Sale.Admin.OrderBasketEdit.prototype.createProductMenuContent = function(basketCode)
2106{
2107 return [{
2108 "ICON": "view",
2109 "TEXT": BX.message("SALE_ORDER_BASKET_PROD_MENU_EDIT"),
2110 "ACTION": this.objName+'.productEdit("'+basketCode+'")',
2111 "DEFAULT":true
2112 },
2113 {
2114 "ICON": "delete",
2115 "TEXT": BX.message("SALE_ORDER_BASKET_PROD_MENU_DELETE"),
2116 "ACTION": this.objName+'.productDeleteClick("'+basketCode+'")'
2117 }
2118 ];
2119};
2120
2121BX.Sale.Admin.OrderBasketEdit.prototype.productEdit = function(basketCode)
2122{
2123 this.productEditDialog.show(basketCode);
2124};
2125
2126BX.Sale.Admin.OrderBasketEdit.prototype.createMeasureRatioNode = function(basketCode, quantityInputNode, ratio)
2127{
2128 if(!quantityInputNode || typeof quantityInputNode.value == "undefined")
2129 return null;
2130
2131 if(!ratio || ratio == 1)
2132 return null;
2133
2134 var upArrow = BX.create('a', {
2135 props:{
2136 href: "javascript:void(0);",
2137 title: BX.message("SALE_ORDER_BASKET_UP_RATIO").replace("#RATIO#", ratio),
2138 className: "plus"
2139 }
2140 }),
2141 downArrow = BX.create('a', {
2142 props:{
2143 href: "javascript:void(0);",
2144 title: BX.message("SALE_ORDER_BASKET_DOWN_RATIO").replace("#RATIO#", ratio),
2145 className: "minus"
2146 }
2147 }),
2148 _this = this;
2149
2150 BX.bind(upArrow, "click", function(e){
2151 quantityInputNode.value = _this.roundQuantity(parseFloat(quantityInputNode.value) + parseFloat(ratio));
2152 _this.onProductQuantityChange({productId: basketCode});
2153 });
2154
2155 BX.bind(downArrow, "click", function(e){
2156 quantityInputNode.value = _this.roundQuantity(parseFloat(quantityInputNode.value) - parseFloat(ratio));
2157 _this.onProductQuantityChange({productId: basketCode});
2158 });
2159
2160 return BX.create('div', {
2161 props:{
2162 className: "quantity_control"
2163 },
2164 children:[
2165 upArrow,
2166 downArrow
2167 ],
2168 style: {
2169 position: 'inherit',
2170 marginLeft: '3px'
2171 }
2172 });
2173};
2174
2175BX.Sale.Admin.OrderBasketEdit.prototype.createFieldQuantity = function(basketCode, product, fieldId)
2176{
2177 var ratio = typeof product.MEASURE_RATIO != "undefined" ? product.MEASURE_RATIO : 1,
2178 updater = {},
2179 result,
2180 _this = this,
2181 input = BX.create('input', {
2182 props:{
2183 type: "text",
2184 name: this.getFieldName(basketCode, "QUANTITY"),
2185 value: this.roundQuantity(product.QUANTITY),
2186 className: "tac"
2187 },
2188 style: { width: '60px' }
2189 }),
2190 ratioNode = this.createMeasureRatioNode(basketCode, input, ratio);
2191
2192 BX.bind(
2193 input,
2194 "change",
2195 function(e){
2196 _this.setProductQuantity( basketCode, input.value);
2197 }
2198 );
2199
2200 BX.bind(input, "keydown", function(e){
2201 if(!e) e = window.event;
2202 if(!e) return;
2203 if(e.keyCode == 13) input.blur();
2204 }
2205 );
2206
2207 updater[this.getFieldName(basketCode, "QUANTITY")] = {
2208 callback: _this.updateProductQuantity,
2209 context: _this
2210 };
2211
2212 BX.Sale.Admin.OrderEditPage.registerFieldsUpdaters(updater);
2213
2214 if(ratioNode)
2215 {
2216 result = BX.create('span', {
2217 children: [input, ratioNode],
2218 style: { display: 'inline-flex' }
2219 });
2220 }
2221 else
2222 {
2223 result = input;
2224 }
2225
2226 return result;
2227};
2228
2229BX.Sale.Admin.OrderBasketEdit.prototype.createFieldPrice = function(basketCode, product, fieldId)
2230{
2231 var price;
2232 var discount;
2233
2234 if(typeof(this.customPrices[basketCode]) == "undefined")
2235 price = product.PRICE;
2236 else
2237 price = this.customPrices[basketCode];
2238
2239 if(typeof(product.DISCOUNT) == "undefined")
2240 discount = 0;
2241 else
2242 discount = product.DISCOUNT;
2243
2244
2245 console.log(product);
2246 var updater = {},
2247 _this = this,
2248 inputP = BX.create('input', {
2249 props:{
2250 type: "text",
2251 name: this.getFieldName(basketCode, "PRICE"),
2252 value: price,
2253 maxlength: "9",
2254 size: 5
2255 }
2256 }),
2257 inputD = BX.create('input', {
2258 props:{
2259 type: "text",
2260 name: this.getFieldName(basketCode, "DISCOUNT"),
2261 value: discount,
2262 maxlength: "9",
2263 size: 5
2264 }
2265 }),
2266 spanInput = BX.create('span', {
2267 props:{
2268 className: "edit_price_product"
2269 },
2270 children: [
2271 inputP
2272 ]
2273 }),
2274 spanInputDiscount = BX.create('span', {
2275 props:{
2276 className: "edit_discount_product"
2277 },
2278 children: [
2279 inputD
2280 ]
2281 }),
2282 spanFormattedPrice = BX.create('span', {
2283 props:{
2284 className: "formated_price",
2285 id: this.idPrefix+"sale-order-basket-product-"+basketCode+"-formatted_price"
2286 },
2287 html: BX.Sale.Admin.OrderEditPage.currencyFormat(price),
2288 style: {whiteSpace: 'nowrap'}
2289 }),
2290 spanFormattedDiscount = BX.create('span', {
2291 props:{
2292 className: "formated_discount",
2293 id: this.idPrefix+"sale-order-basket-product-"+basketCode+"-formatted_discount"
2294 },
2295 style: {whiteSpace: 'nowrap'}
2296 }),
2297 spanView = BX.create('span', {
2298 props:{
2299 className: "default_price_product"
2300 },
2301 children: [
2302 spanFormattedPrice
2303 ]
2304 }),
2305 spanViewDiscount = BX.create('span', {
2306 props:{
2307 className: "default_discount_product"
2308 },
2309 children: [
2310 spanFormattedDiscount
2311 ]
2312 }),
2313 currencySpan = BX.create('span', {
2314 text: ""
2315 }),
2316 apencil = BX.create('a', {
2317 props:{
2318 href: "javascript:void(0);"
2319 },
2320 children: [
2321 BX.create('span', {
2322 props:{
2323 className: "pencil"
2324 }
2325 })
2326 ]
2327 }),
2328 basePrice = BX.create('div',{
2329 props: {
2330 className: "base_price",
2331 id: this.idPrefix+"sale-order-basket-product-"+basketCode+"-base_price"
2332 },
2333 style: {display: ((parseFloat(product.BASE_PRICE) > 0 && parseFloat(product.PRICE) != parseFloat(product.BASE_PRICE)) ? "": "none")},
2334 children: [
2335 BX.create('span',{
2336 html: BX.Sale.Admin.OrderEditPage.currencyFormat(product.BASE_PRICE)
2337 })
2338 ]
2339 }),
2340 priceNotes = BX.create('div',{
2341 props: {className: "base_price_title"},
2342 style: {display: (((product.CUSTOM_PRICE && product.CUSTOM_PRICE == "Y") || product.NOTES) ? "": "none")},
2343 text: ((product.CUSTOM_PRICE && product.CUSTOM_PRICE == "Y") ? BX.message("SALE_ORDER_BASKET_BASE_CATALOG_PRICE") : product.NOTES)
2344 }),
2345 containerDiv = BX.create('span', {
2346 props:{
2347 className: "edit_price"
2348 },
2349 children: [
2350 spanFormattedDiscount,
2351 spanView,
2352 spanInput,
2353 spanInputDiscount,
2354 currencySpan,
2355 apencil,
2356 basePrice,
2357 priceNotes
2358 ]
2359 });
2360
2361 inputP.style.width = "60px";
2362
2363 BX.bind(apencil, "click", function(e){
2364 _this.onPriceEditEnable(containerDiv, inputP);
2365 });
2366
2367 BX.bind(spanFormattedPrice, "click", function(e){
2368 _this.onPriceEditEnable(containerDiv, inputP);
2369 });
2370
2371 BX.bind(spanFormattedDiscount, "click", function(e){
2372 _this.onPriceEditEnable(containerDiv, inputD);
2373 });
2374
2375 BX.bind(inputP, "change", function(e){
2376 var form = BX.Sale.Admin.OrderEditPage.getForm();
2377 form.elements[_this.getFieldName(basketCode, "CUSTOM_PRICE")].value = "Y";
2378 var price = _this.setProductPrice(basketCode, inputP.value);
2379 spanFormattedPrice.innerHTML = BX.Sale.Admin.OrderEditPage.currencyFormat(price);
2380
2381 console.log(inputP.value);
2382 console.log(form);
2383 console.log(basketCode);
2384 console.log(price);
2385 console.log(spanFormattedPrice.innerHTML);
2386 });
2387
2388 BX.bind(inputP, "keydown", function(e){
2389 if(!e) e = window.event;
2390 if(!e) return;
2391 if(e.keyCode == 13) inputP.blur();
2392 }
2393 );
2394
2395 BX.bind(inputP, "blur", function(e){
2396 _this.onPriceEditDisable(containerDiv);
2397 });
2398
2399 updater[this.getFieldName(basketCode, "PRICE")] = {
2400 callback: _this.updateProductPrice,
2401 context: _this
2402 };
2403
2404 BX.Sale.Admin.OrderEditPage.registerFieldsUpdaters(updater);
2405
2406 return containerDiv;
2407};
2408
2409BX.Sale.Admin.OrderBasketEdit.prototype.createDiscountsNodeBasket = function(discounts)
2410{
2411 return BX.Sale.Admin.OrderEditPage.createDiscountsNode(
2412 "",
2413 "DISCOUNT_LIST",
2414 discounts,
2415 this.discounts,
2416 "EDIT"
2417 );
2418};
2419
2420BX.Sale.Admin.OrderBasketEdit.prototype.getHiddenFieldsNames = function()
2421{
2422 return ["CURRENCY", "PRODUCT_PROVIDER_CLASS", "NAME", "DETAIL_PAGE_URL", "WEIGHT",
2423 "CATALOG_XML_ID", "NOTES", "PRODUCT_XML_ID", "OFFER_ID", "MODULE", "CUSTOM_PRICE", "IS_SET_ITEM", "IS_SET_PARENT", "MEASURE_CODE"];
2424};
2425
2426BX.Sale.Admin.OrderBasketEdit.prototype.createHiddenFields = function(basketCode, product)
2427{
2428 var result = [];
2429
2430 if(typeof product["CUSTOM_PRICE"] == "undefined")
2431 product["CUSTOM_PRICE"] = "N";
2432
2433 for(var i in product)
2434 {
2435 if(!product.hasOwnProperty(i))
2436 continue;
2437
2438 if(!product[i] && i != "CATALOG_XML_ID")
2439 continue;
2440
2441 if(i == "PROPS")
2442 {
2443 result.push(
2444 BX.create('span', {
2445 html: this.createSkuPropsHiddenHtml(basketCode, product["PROPS"], product.IS_SET_ITEM, 'PROPS')
2446 })
2447 );
2448
2449 continue;
2450 }
2451
2452 if(i == "SKU_PROPS")
2453 {
2454 result.push(
2455 BX.create('span', {
2456 html: this.createSkuPropsHiddenHtml(basketCode, product["SKU_PROPS"], product.IS_SET_ITEM, 'SKU_PROPS')
2457 })
2458 );
2459
2460 continue;
2461 }
2462
2463 if(typeof product[i] == "object")
2464 continue;
2465
2466 if(i == "PRICE" || i == "QUANTITY")
2467 continue;
2468
2469 result.push(
2470 BX.create('input', {
2471 props:{
2472 type: "hidden",
2473 name: this.getFieldName(basketCode, i),
2474 value: product[i]
2475 }
2476 })
2477 );
2478 }
2479
2480 return result;
2481};
2482
2483BX.Sale.Admin.OrderBasketEdit.prototype.createSkuPropsHiddenHtml = function(basketCode, props, isSetItem, propsName)
2484{
2485 if(!props)
2486 return "";
2487
2488 var propIdx = 0,
2489 propsFieldName = this.getFieldName(basketCode, propsName),
2490 skuCodes = [],
2491 hiddenFieldsHtml = '';
2492
2493 if(props)
2494 {
2495 for(var i in props)
2496 {
2497 if(!props.hasOwnProperty(i))
2498 continue;
2499
2500 if(!props[i] || skuCodes.indexOf(props[i]["CODE"]) != -1)
2501 continue;
2502
2503 if(isSetItem != "Y")
2504 {
2505 var name = props[i]["NAME"] ? BX.util.htmlspecialchars(props[i]["NAME"]) : "",
2506 value = props[i]["VALUE"] ? BX.util.htmlspecialchars(props[i]["VALUE"]) : "",
2507 code = props[i]["CODE"] ? BX.util.htmlspecialchars(props[i]["CODE"]) : "",
2508 sort = props[i]["SORT"] ? BX.util.htmlspecialchars(props[i]["SORT"]) : 100,
2509 id = props[i]["ID"] ? BX.util.htmlspecialchars(props[i]["ID"]) : 0;
2510
2511 hiddenFieldsHtml += '<input type="hidden" name="'+propsFieldName+'['+propIdx+'][NAME]" value="'+name+'">'+
2512 '<input type="hidden" name="'+propsFieldName+'['+propIdx+'][VALUE]" value="'+value+'">'+
2513 '<input type="hidden" name="'+propsFieldName+'['+propIdx+'][CODE]" value="'+code+'">'+
2514 '<input type="hidden" name="'+propsFieldName+'['+propIdx+'][ID]" value="'+id+'">'+
2515 '<input type="hidden" name="'+propsFieldName+'['+propIdx+'][SORT]" value="'+sort+'">';
2516 }
2517
2518 propIdx++;
2519 }
2520 }
2521
2522 return hiddenFieldsHtml;
2523};
2524
2525BX.Sale.Admin.OrderBasketEdit.prototype.onPriceEditEnable = function(containerNode, inputNode)
2526{
2527 BX.addClass(containerNode, "edit_enable");
2528 inputNode.focus();
2529};
2530
2531BX.Sale.Admin.OrderBasketEdit.prototype.onPriceEditDisable = function(containerNode)
2532{
2533 BX.removeClass(containerNode, "edit_enable");
2534};
2535
2536BX.Sale.Admin.OrderBasketEdit.prototype.addFieldUpdater = function(basketCode, fieldName, callback)
2537{
2538 if(!this.fieldsUpdaters[basketCode])
2539 this.fieldsUpdaters[basketCode] = {};
2540
2541 if(!this.fieldsUpdaters[basketCode][fieldName])
2542 this.fieldsUpdaters[basketCode][fieldName] = [];
2543
2544 if(typeof callback == "function")
2545 this.fieldsUpdaters[basketCode][fieldName].push(callback);
2546 else
2547 BX.debug("BX.Sale.Admin.OrderBasketEdit.prototype.addFieldUpdater() callback is not a function!");
2548};
2549
2550BX.Sale.Admin.OrderBasketEdit.prototype.callFieldUpdaters = function(basketCode, fieldName, fieldValue)
2551{
2552 if(!this.fieldsUpdaters[basketCode])
2553 return false;
2554
2555 if(!this.fieldsUpdaters[basketCode][fieldName])
2556 return false;
2557
2558 var updaters = this.fieldsUpdaters[basketCode][fieldName];
2559
2560 for(var i in updaters)
2561 {
2562 if(!updaters.hasOwnProperty(i))
2563 continue;
2564
2565 if(typeof updaters[i] != "function")
2566 continue;
2567
2568 updaters[i].call(this, fieldValue);
2569 }
2570};
2571
2572BX.Sale.Admin.OrderBasketEdit.prototype.setProductFieldValue = function(basketCode, fieldName, value)
2573{
2574 var form = BX.Sale.Admin.OrderEditPage.getForm(),
2575 formField = form.elements[this.getFieldName(basketCode, fieldName)];
2576
2577 if(formField && formField.value)
2578 formField.value = value;
2579
2580
2581 if(this.fieldsUpdaters[basketCode]
2582 && this.fieldsUpdaters[basketCode][fieldName]
2583 && typeof (this.fieldsUpdaters[basketCode][fieldName]) == "function"
2584 )
2585 {
2586 this.fieldsUpdaters[basketCode][fieldName].call(this, value);
2587 }
2588};
2589
2590BX.Sale.Admin.OrderBasketEdit.prototype.getProductFieldValue = function(basketCode, fieldName)
2591{
2592 var form = BX.Sale.Admin.OrderEditPage.getForm();
2593
2594 if(!form)
2595 return "";
2596
2597 var result = "",
2598 formField = form.elements[this.getFieldName(basketCode, fieldName)];
2599
2600 if(formField && formField.value)
2601 result = formField.value;
2602
2603 return result;
2604};
2605
2606/*
2607 *
2608 * OrderBasketEditTotal
2609 *
2610 */
2611
2612BX.Sale.Admin.OrderBasketEditTotal = function(params)
2613{
2614 if(!params.fields)
2615 {
2616 BX.debug("OrderBasketEditTotal:constructor() params.fields not defined!");
2617 return;
2618 }
2619
2620 this.fields = {};
2621 this.formulas = {};
2622
2623 var _this = this;
2624
2625 var getUnitOfMesure = function(fieldId)
2626 {
2627 var type = _this.fields[fieldId]["type"],
2628 result = "";
2629
2630 if(type == "currency")
2631 result = BX.Sale.Admin.OrderEditPage.currencyLang;
2632 else if(type == "weight")
2633 result = params.weightUnit;
2634
2635 return result;
2636 };
2637
2638 var getFormattedValue = function(fieldId)
2639 {
2640 var value = _this.fields[fieldId]["value"],
2641 type = _this.fields[fieldId]["type"],
2642 result = value;
2643
2644 if(type == "currency")
2645 result = BX.Sale.Admin.OrderEditPage.currencyFormat(value, false);
2646 else if(type == "weight")
2647 result = value;
2648
2649 return result;
2650 };
2651
2652 var setFieldView = function(fieldId)
2653 {
2654 var field = BX(_this.fields[fieldId].id);
2655
2656 if(!field)
2657 {
2658 BX.debug("OrderBasketEditTotal:setFieldView() can't find field with id: \""+_this.fields[fieldId].id+"\"");
2659 return false;
2660 }
2661
2662 if(_this.fields[fieldId]["edit"])
2663 {
2664 var span = BX.findChild(field, {className: 'formated_field_view'}, true, false);
2665
2666 if(span)
2667 span.innerHTML = getFormattedValue(fieldId);
2668 }
2669 else
2670 {
2671 field.innerHTML = getFormattedValue(fieldId);
2672
2673 if(_this.fields[fieldId]["type"] != "currency")
2674 field.innerHTML = field.innerHTML + " " + getUnitOfMesure(fieldId)
2675 }
2676
2677 return true;
2678 };
2679
2680 var setFieldEditable = function(fieldId)
2681 {
2682 var field = BX(_this.fields[fieldId].id);
2683
2684 if(!field)
2685 {
2686 BX.debug("OrderBasketEditTotal:setFieldView() can't find field with id: \""+_this.fields[fieldId].id+"\"");
2687 return false;
2688 }
2689
2690 var input = BX.create('input', {
2691 props:{
2692 type: "text",
2693 name: fieldId,
2694 value: _this.fields[fieldId].value,
2695 maxlength: "9",
2696 size: 5
2697 }
2698 }),
2699 spanInput = BX.create('span', {
2700 props:{
2701 className: "edit_field"
2702 },
2703 children: [
2704 input
2705 ]
2706 }),
2707 spanFormattedPrice = BX.create('span', {
2708 props:{
2709 className: "formated_field_view"
2710 },
2711 html: getFormattedValue(fieldId)
2712 }),
2713 spanView = BX.create('span', {
2714 props:{
2715 className: "view_field"
2716 },
2717 children: [
2718 spanFormattedPrice
2719 ]
2720 }),
2721 unitOfMesure = BX.create('span', {
2722 text: " "+getUnitOfMesure(fieldId)+" "
2723 }),
2724 apencil = BX.create('a', {
2725 props:{
2726 href: "javascript:void(0);"
2727 },
2728 children: [
2729 BX.create('span', {
2730 props:{
2731 className: "pencil"
2732 }
2733 })
2734 ]
2735 }),
2736 containerDiv = BX.create('span', {
2737 props:{
2738 className: "edit_field_container"
2739 },
2740 children: [
2741 spanView,
2742 spanInput,
2743 unitOfMesure,
2744 apencil
2745 ]
2746 });
2747
2748 input.style.width = "40px";
2749
2750 //on change value
2751 BX.bind(input, "change", function(e){
2752 _this.setFieldValue(fieldId, this.value);
2753 });
2754
2755 //show pensil
2756 BX.bind(field, "mouseover", function(e){
2757 BX.addClass(field, "edit_field_hover");
2758 });
2759
2760 //hide pensil
2761 BX.bind(field, "mouseout", function(e){
2762 BX.removeClass(field, "edit_field_hover");
2763 });
2764
2765 //shown input
2766 BX.bind(field, "click", function(e){
2767 BX.addClass(field, "edit_enabled");
2768 input.focus();
2769 });
2770
2771 //hide input
2772 BX.bind(input, "blur", function(e){
2773 BX.removeClass(field, "edit_enabled");
2774 });
2775
2776 field = BX.cleanNode(field, false);
2777 field.appendChild(containerDiv);
2778 };
2779
2780 var setFormulas = function(fieldId)
2781 {
2782 _this.formulas[fieldId] = _this.fields[fieldId]["formula"];
2783 };
2784
2785 var getFormulas = function(fieldId)
2786 {
2787 var result = [];
2788
2789 for(var i in _this.formulas) //fieldId's
2790 {
2791 if(!_this.formulas.hasOwnProperty(i))
2792 continue;
2793
2794 var re = new RegExp("\\W*"+fieldId+"\\W*", 'i');
2795
2796 if(_this.formulas[i].search(re) != -1)
2797 result.push(i);
2798 }
2799
2800 return result;
2801 };
2802
2803 var recountFormulasFieldsValues = function(fieldsIds)
2804 {
2805 for(var i in fieldsIds)
2806 {
2807 if(!fieldsIds.hasOwnProperty(i))
2808 continue;
2809
2810 var formula = _this.formulas[fieldsIds[i]],
2811 re = new RegExp("[\\w_]+", "ig"),
2812 newFormula = formula.replace(re, function(fieldId){
2813 var result = _this.getFieldValue(fieldId);
2814 return result;
2815 });
2816
2817 _this.setFieldValue(fieldsIds[i], eval(newFormula));
2818 }
2819 };
2820
2821 var setField = function(fieldId, fieldParams)
2822 {
2823 var re = new RegExp("[^\\w_]+", "ig");
2824
2825 if(fieldId.search(re) != -1)
2826 BX.debug("OrderBasketEditTotal:setField() wrong field id!");
2827
2828 _this.fields[fieldId] = fieldParams;
2829
2830 if(fieldParams.edit)
2831 setFieldEditable(fieldId);
2832
2833 if(fieldParams.formula)
2834 setFormulas(fieldId);
2835 };
2836
2837 for(var fieldId in params.fields)
2838 if(params.fields.hasOwnProperty(fieldId))
2839 setField(fieldId, params.fields[fieldId]);
2840
2841 /* --- public methods --- */
2842
2843 this.setFieldValue = function(fieldId, value)
2844 {
2845 if(typeof(_this.fields[fieldId]) != "undefined")
2846 {
2847 _this.fields[fieldId].value = value;
2848
2849 if(typeof(_this.fields[fieldId]["type"]["formula"]) != "undefined")
2850 {
2851 var formulasFields = getFormulas(fieldId);
2852
2853 if(formulasFields)
2854 recountFormulasFieldsValues(formulasFields);
2855 }
2856
2857 if(_this.fields[fieldId]["type"] != "hidden")
2858 setFieldView(fieldId, value);
2859 }
2860 else
2861 {
2862 BX.debug("OrderBasketEditTotal:setFieldValue() unknown field id: \""+fieldId+"\"");
2863 }
2864 };
2865
2866 this.getFieldValue= function(fieldId)
2867 {
2868 var result = false;
2869
2870 if(typeof(this.fields[fieldId]) != "undefined")
2871 result = this.fields[fieldId].value;
2872 else
2873 BX.debug("OrderBasketEditTotal: unknown field id: \""+fieldId+"\"");
2874
2875 return result;
2876 };
2877};
2878
2879
2880/*
2881 *
2882 * OrderBasketProductEditDialog
2883 *
2884 */
2885
2886BX.Sale.Admin.OrderBasketProductEditDialog = function(basketObj)
2887{
2888 var dialog = null,
2889 basketCode = 0,
2890 propCount = 0,
2891 isNewProduct = true,
2892 basket = basketObj,
2893 usedBasketFields = ["CURRENCY", "PRODUCT_PROVIDER_CLASS", "NAME", "DETAIL_PAGE_URL", "WEIGHT",
2894 "CATALOG_XML_ID", "NOTES", "PRODUCT_XML_ID", "OFFER_ID", "PRICE", "QUANTITY", "PROPS", "MEASURE_CODE",
2895 "MEASURE_TEXT", "CUSTOM_PRICE", "BASKET_CODE"],
2896 _this = this;
2897
2898
2899 /* private methods */
2900
2901 var setBasketCode = function(code)
2902 {
2903 if(!code)
2904 code = getFreeBasketOfferId();
2905
2906 basketCode = code;
2907 };
2908
2909 var getBasketCode = function()
2910 {
2911 return basketCode;
2912 };
2913
2914 var getPropsTable = function()
2915 {
2916 var table = BX("BASKET_PROP_TABLE");
2917
2918 if(!table)
2919 BX.debug("BX.Sale.Admin.OrderBasketProductEditDialog:addPropRow() can't find props table!");
2920
2921 return table;
2922 };
2923
2924 var createPropFieldName = function(fieldId, id)
2925 {
2926 return 'FORM_PROD_PROP_' + getBasketCode() + '_'+fieldId+'_' + id;
2927 };
2928
2929 var getDialogField = function(fieldName)
2930 {
2931 return BX('FORM_PROD_BASKET_'+fieldName);
2932 };
2933
2934 var getFreeBasketOfferId = function()
2935 {
2936 var i = 0;
2937
2938 do
2939 {
2940 i++;
2941 }
2942 while(typeof (basket.products[('n'+i)]) != "undefined");
2943
2944 return 'n'+i;
2945 };
2946
2947 var getProductPropsFromDialog = function()
2948 {
2949 var result = [],
2950 props = ["NAME", "VALUE", "CODE", "SORT"];
2951
2952 for(var i = 0; i < propCount; i++)
2953 {
2954 var property = {};
2955
2956 for(var j=0; j< props.length; j++)
2957 {
2958 var fieldName = createPropFieldName(props[j], i),
2959 propField = BX(fieldName);
2960
2961 if(propField && typeof (propField.value) != "undefined")
2962 property[props[j]] = propField.value;
2963 }
2964
2965 result.unshift(property);
2966 }
2967
2968 return result;
2969 };
2970
2971 var setProps = function(product)
2972 {
2973 var props = getProductPropsFromDialog();
2974 product.PROPS = [];
2975
2976 for(var i in props)
2977 if(props.hasOwnProperty(i))
2978 if(props[i].NAME)
2979 product.PROPS.push(props[i]);
2980
2981 return product;
2982 };
2983
2984 var setProductParams = function()
2985 {
2986 var basketCode = getBasketCode(),
2987 dialogField,
2988 product = basket.products[basketCode] ? basket.products[basketCode] : {MODULE: "", OFFER_ID: 1, BASKET_CODE: basketCode},
2989 customedPrice = false;
2990
2991 for(var i in usedBasketFields)
2992 {
2993 if(!usedBasketFields.hasOwnProperty(i))
2994 continue;
2995
2996 if(dialogField = getDialogField(usedBasketFields[i]))
2997 {
2998 if(usedBasketFields[i] == "PRICE")
2999 {
3000 var productPrice = Math.round(parseFloat(product[usedBasketFields[i]])*10000),
3001 dialogPrice = Math.round(parseFloat(dialogField.value)*10000);
3002
3003 if(productPrice != dialogPrice)
3004 {
3005 product["CUSTOM_PRICE"] = "Y";
3006 customedPrice = true;
3007 basket.customPrices[basketCode] = dialogField.value;
3008
3009 if(!product.MODULE)
3010 product["BASE_PRICE"] = product["PRICE_BASE"] = dialogField.value;
3011 }
3012 }
3013 else if(usedBasketFields[i] == "CUSTOM_PRICE")
3014 {
3015 if(customedPrice)
3016 continue;
3017 }
3018
3019 if(dialogField.value || (usedBasketFields[i] != 'OFFER_ID' && usedBasketFields[i] != 'BASKET_CODE'))
3020 product[usedBasketFields[i]] = dialogField.value;
3021 }
3022 }
3023
3024 product['MANUALLY_EDITED'] = 'Y';
3025
3026 if(product.BASKET_CODE != basketCode)
3027 BX.debug('setProductParams: product.BASKET_CODE != basketCode "'+product.BASKET_CODE+'" != "'+basketCode+'"');
3028
3029 if(!product.BASKET_CODE || product.BASKET_CODE != basketCode)
3030 product.BASKET_CODE = basketCode;
3031
3032 if(isNewProduct && !product.OFFER_ID)
3033 product.OFFER_ID = 1;
3034
3035 product = setProps(product);
3036
3037 basket.productSet(product, !isNewProduct);
3038
3039 BX.Sale.Admin.OrderAjaxer.sendRequest(
3040 BX.Sale.Admin.OrderEditPage.ajaxRequests.refreshOrderData({
3041 operation: "PRODUCT_ADD",
3042 productId: product.OFFER_ID
3043 })
3044 );
3045 };
3046
3047 var getProps = function()
3048 {
3049 var product = basket.products[getBasketCode()];
3050
3051 if(!product)
3052 {
3053 BX.debug("BX.Sale.Admin.OrderBasketProductEditDialog.getProps() can't find product with basket code: \""+getBasketCode()+"\"");
3054 return [];
3055 }
3056
3057 return basket.products[getBasketCode()]["PROPS"];
3058 };
3059
3060 var getProductParams = function()
3061 {
3062 var dialogField;
3063 for(var i in usedBasketFields)
3064 {
3065 if(!usedBasketFields.hasOwnProperty(i))
3066 continue;
3067
3068 if(usedBasketFields[i] == "PROPS")
3069 {
3070 var props = getProps();
3071
3072 for(var pi in props)
3073 if(props.hasOwnProperty(pi))
3074 _this.addPropRow(pi, props[pi]);
3075 }
3076 else if(dialogField = getDialogField(usedBasketFields[i]))
3077 {
3078 dialogField.value = basket.getProductFieldValue(getBasketCode(), usedBasketFields[i]);
3079 }
3080 }
3081
3082 BX("FORM_PROD_BASKET_MEASURE_CODE").disabled = (basket.getProductFieldValue(getBasketCode(), "PRODUCT_PROVIDER_CLASS") != "");
3083 };
3084
3085 var clearProductParams = function()
3086 {
3087 var dialogField;
3088
3089 for(var i in usedBasketFields)
3090 if(usedBasketFields.hasOwnProperty(i))
3091 if(dialogField = getDialogField(usedBasketFields[i]))
3092 dialogField.value = "";
3093
3094 var table = getPropsTable();
3095
3096 for(i = table.rows.length-1; i > 1 ; i--)
3097 table.deleteRow(i);
3098
3099 BX("FORM_PROD_BASKET_EMPTY_PROP_ROW").style.display = "";
3100
3101 };
3102
3103 var loadHtml = function()
3104 {
3105 var params = {
3106 action: "getProductEditDialogHtml",
3107 currency: BX.Sale.Admin.OrderEditPage.currencyLang,
3108 objName: basket.objName,
3109 callback: function(result)
3110 {
3111 if(result && result.DIALOG_CONTENT && !result.ERROR)
3112 createDialog(result.DIALOG_CONTENT);
3113 else if(result && result.ERROR)
3114 BX.debug("Error receiving dialog content: " + result.ERROR);
3115 else
3116 BX.debug("Error receiving dialog content!");
3117 }
3118 };
3119
3120 BX.Sale.Admin.OrderAjaxer.sendRequest(params);
3121 };
3122
3123 var createDialog = function(content)
3124 {
3125 dialog = new BX.CDialog({
3126 'content': content,
3127 'title': !isNewProduct ? BX.message("SALE_ORDER_BASKET_PROD_EDIT") : BX.message("SALE_ORDER_BASKET_PROD_CREATE"),
3128 'width': 820,
3129 'height': 470
3130// 'resizable': false
3131 });
3132
3133 dialog.ClearButtons();
3134 dialog.SetButtons([
3135 {
3136 'title': BX.message("SALE_ORDER_BASKET_PROD_EDIT_ITEM_SAVE"),
3137 'id': 'save_custom_product',
3138 'action': function() {
3139 setProductParams();
3140 this.parentWindow.Close();
3141 }
3142 },
3143 BX.CDialog.prototype.btnCancel
3144 ]);
3145
3146 if(!isNewProduct)
3147 getProductParams();
3148
3149 basket.productEditDialog.disableButton();
3150 dialog.Show();
3151 BX.Sale.Admin.OrderEditPage.unBlockForm();
3152 };
3153
3154 /* public methods */
3155
3156 this.show = function(basketCode)
3157 {
3158 propCount = 0;
3159
3160 if(basketCode)
3161 isNewProduct = false;
3162 else
3163 isNewProduct = true;
3164
3165 setBasketCode(basketCode);
3166
3167 if(dialog == null)
3168 {
3169 dialog = loadHtml();
3170 }
3171 else
3172 {
3173 var title;
3174
3175 if(!isNewProduct)
3176 {
3177 clearProductParams();
3178 getProductParams();
3179 title = BX.message("SALE_ORDER_BASKET_PROD_EDIT");
3180 }
3181 else
3182 {
3183 clearProductParams();
3184 title = BX.message("SALE_ORDER_BASKET_PROD_CREATE");
3185 }
3186
3187 dialog.SetTitle(title);
3188 this.disableButton();
3189
3190 dialog.Show();
3191 dialog.adjustSize();
3192 }
3193 };
3194
3195 this.setMeasureText = function()
3196 {
3197
3198 var code = BX('FORM_PROD_BASKET_MEASURE_CODE'),
3199 text = BX('FORM_PROD_BASKET_MEASURE_TEXT');
3200
3201 for(var o = 0; o < code.options.length; o++)
3202 {
3203 if(code.options[o].selected != true)
3204 continue;
3205
3206 text.value = code.options[o].innerHTML;
3207 break;
3208 }
3209 };
3210
3211 this.disableButton = function()
3212 {
3213 var button = BX("save_custom_product");
3214
3215 if(!button)
3216 return;
3217
3218 if(this.checkRequiredFields())
3219 button.disabled = false;
3220 else
3221 button.disabled = true;
3222 };
3223
3224 this.checkRequiredFields = function()
3225 {
3226 var name = BX("FORM_PROD_BASKET_NAME"),
3227 price = BX("FORM_PROD_BASKET_PRICE"),
3228 quantity = BX("FORM_PROD_BASKET_QUANTITY");
3229
3230 if(quantity.value.length > 0 && /\D\./.test(quantity.value))
3231 quantity.value = parseFloat(quantity.value) || "0";
3232
3233 if(price.value.length > 0 && /\D\./.test(price.value))
3234 price.value = parseFloat(price.value) || "0";
3235
3236 if(name.value.length <= 0)
3237 return false;
3238
3239 if(price.value.length <= 0)
3240 return false;
3241
3242 if(quantity.value.length <= 0)
3243 return false;
3244
3245 return true;
3246 };
3247
3248 this.addPropRow = function(id, props)
3249 {
3250 var table = getPropsTable();
3251
3252 if(!id)
3253 id = propCount;
3254
3255 if(propCount <=0)
3256 BX("FORM_PROD_BASKET_EMPTY_PROP_ROW").style.display = "none";
3257
3258 var sizes = { NAME: 20, VALUE: 20, CODE: 3, SORT: 2 };
3259
3260 if(!props)
3261 props = { NAME: "", VALUE: "", CODE: "", SORT: "" };
3262
3263 var row = table.insertRow(-1),
3264 cell = row.insertCell(-1),
3265 name = props["NAME"] ? BX.util.htmlspecialchars(props["NAME"]) : "",
3266 value = props["VALUE"] ? BX.util.htmlspecialchars(props["VALUE"]) : "",
3267 code = props["CODE"] ? BX.util.htmlspecialchars(props["CODE"]) : "",
3268 sort = props["SORT"] ? BX.util.htmlspecialchars(props["SORT"]) : 100;
3269
3270 cell.innerHTML = '<input type="text" maxlength="250" size="'+sizes["NAME"]+'" name="'+createPropFieldName("NAME", id)+'" id="'+createPropFieldName("NAME", id)+'" value="'+name+'" />';
3271 cell = row.insertCell(-1);
3272 cell.innerHTML = '<input type="text" maxlength="250" size="'+sizes["VALUE"]+'" name="'+createPropFieldName("VALUE", id)+'" id="'+createPropFieldName("VALUE", id)+'" value="'+value+'" />';
3273 cell = row.insertCell(-1);
3274 cell.innerHTML = '<input type="text" maxlength="250" size="'+sizes["CODE"]+'" name="'+createPropFieldName("CODE", id)+'" id="'+createPropFieldName("CODE", id)+'" value="'+code+'" />';
3275 cell = row.insertCell(-1);
3276 cell.innerHTML = '<input type="text" maxlength="250" size="'+sizes["SORT"]+'" name="'+createPropFieldName("SORT", id)+'" id="'+createPropFieldName("SORT", id)+'" value="'+sort+'" />';
3277
3278 propCount++;
3279 };
3280};
3281
3282BX.Sale.Admin.OrderBasketCoupons =
3283{
3284 MODES_LIST: {
3285 CREATE: 0,
3286 EDIT: 1,
3287 VIEW: 2
3288 },
3289
3290 statusCouponApplyed: null,
3291 mode: null,
3292
3293 getCouponsContainerNode: function()
3294 {
3295 return BX("sale-admin-order-coupons-container");
3296 },
3297
3298 onSetCoupon: function(e)
3299 {
3300 BX.Sale.Admin.OrderEditPage.setDiscountCheckbox(e);
3301 BX.Sale.Admin.OrderEditPage.refreshDiscounts();
3302 },
3303
3304 onDeleteCoupon: function(coupon)
3305 {
3306 BX.Sale.Admin.OrderAjaxer.sendRequest(
3307 {
3308 action: "deleteCoupon",
3309 coupon: coupon,
3310 orderId: BX.Sale.Admin.OrderEditPage.orderId,
3311 userId: BX.Sale.Admin.OrderBuyer.getBuyerId(),
3312 callback: function(result){
3313 if(result && result.ERROR)
3314 BX.Sale.Admin.OrderEditPage.showDialog("Error: can't delete coupon");
3315 }
3316 },
3317 false,
3318 true
3319 );
3320 },
3321
3322 onAddCoupons: function()
3323 {
3324 var coupons = BX("sale-admin-order-coupons");
3325
3326 if(!coupons || !coupons.value)
3327 return;
3328
3329 BX.Sale.Admin.OrderAjaxer.sendRequest(
3330 {
3331 action: "addCoupons",
3332 coupon: coupons.value,
3333 orderId: BX.Sale.Admin.OrderEditPage.orderId,
3334 userId: BX.Sale.Admin.OrderBuyer.getBuyerId(),
3335 callback: function(result){
3336 if(result && result.ERROR)
3337 BX.Sale.Admin.OrderEditPage.showDialog(BX.message('SALE_ORDER_BASKET_ADD_COUPON_ERROR'));
3338 }
3339 },
3340 false,
3341 true
3342 );
3343
3344 coupons.value = "";
3345 },
3346
3347 addCouponRow: function(coupon) //for order creation page
3348 {
3349 if(!coupon)
3350 return;
3351
3352 var container = this.getCouponsContainerNode(),
3353 params;
3354
3355 if(!container)
3356 return;
3357
3358 params = {
3359 toUse: (coupon.JS_STATUS === 'APPLYED'),
3360 coupon: coupon.COUPON,
3361 title: coupon.JS_CHECK_CODE ? coupon.JS_CHECK_CODE : '',
3362 discountId: coupon.ORDER_DISCOUNT_ID,
3363 description: '['+coupon.COUPON+']'
3364 };
3365
3366 if(coupon.DISCOUNT_NAME != "")
3367 params.description += " "+coupon.DISCOUNT_NAME;
3368
3369 if (coupon.JS_STATUS === 'BAD')
3370 params.color = 'red';
3371 else if (coupon.JS_STATUS === 'APPLYED')
3372 params.color = 'green';
3373 else
3374 params.color = 'gray';
3375
3376 if (coupon.DISCOUNT_SIZE)
3377 params.discountSize = coupon.DISCOUNT_SIZE;
3378 else
3379 params.discountSize = "0 %";
3380
3381 if (coupon.APPLY)
3382 params.APPLY = coupon.APPLY;
3383 else
3384 params.APPLY = "N";
3385
3386 params.NEW_COUPON = coupon.SAVED == 'N';
3387
3388 container.appendChild(
3389 this.createCouponRowNode(params)
3390 );
3391 },
3392
3393 clearCoupons: function()
3394 {
3395 var couponsContainer = this.getCouponsContainerNode();
3396
3397 if(!couponsContainer)
3398 return;
3399
3400 BX.cleanNode(couponsContainer, false);
3401 },
3402
3403 createCouponRowNode: function(params)
3404 {
3405 var colorClass = "bx-bg-"+params.color,
3406 setCheckbox = (this.mode === this.MODES_LIST.EDIT),
3407 setRemover = (this.mode === this.MODES_LIST.CREATE || (this.mode === this.MODES_LIST.EDIT && params.NEW_COUPON)),
3408 discountSize = params.discountSize ? params.discountSize : "",
3409 description = params.description ? BX.util.htmlspecialchars(params.description) : "",
3410 coupon = params.coupon ? BX.util.htmlspecialchars(params.coupon) : "",
3411 toUse = params.toUse,
3412 apply = params.APPLY,
3413 discountId = params.discountId,
3414 title = '';
3415
3416 if(params.title)
3417 title = BX.message("SALE_ORDER_BASKET_COUPON_STATUS")+": "+BX.util.htmlspecialchars(params.title);
3418
3419 return BX.create('li',{
3420 props: {
3421 className: "bx-adm-pc-sale-item "+colorClass
3422 },
3423 html: (setCheckbox ? '<input type="hidden" value="N" name="DISCOUNTS[COUPON_LIST]['+coupon+']">'+
3424 '<input type="checkbox" data-discount-id="'+discountId+'" data-coupon="Y" data-discount-coupon="'+coupon+'" class="bx-adm-pc-input-checkbox" name="DISCOUNTS[COUPON_LIST]['+coupon+']" value="Y" onclick="BX.Sale.Admin.OrderBasketCoupons.onSetCoupon(event);"'+(apply == "Y" ? ' checked' : '')+' title="'+BX.message("SALE_ORDER_BASKET_COUPON_APPLY")+'">' : '')+
3425 '<div class="bx-adm-pc-sale-item-block'+(setCheckbox ? "" : " bx-amd-l0")+(setRemover ? "" : " bx-adm-r0")+'" title="'+title+'">'+
3426 '<div class="bx-adm-pc-sale-overname"><div class="bx-adm-pc-sale-cost">'+(discountSize.length > 0 ? discountSize : '0')+'</div>'+description+'</div>'+
3427 '</div>'+
3428 (setRemover ? '<div class="bx-adm-pc-sale-item-remover" onclick="BX.Sale.Admin.OrderBasketCoupons.onDeleteCoupon(\''+coupon+'\');" title="'+BX.message("SALE_ORDER_BASKET_COUPON_DELETE")+'"></div>' : '')
3429 });
3430 },
3431
3432 setCoupons: function(coupons)
3433 {
3434 this.clearCoupons();
3435
3436 if(!coupons)
3437 return;
3438
3439 for(var i in coupons)
3440 if(coupons.hasOwnProperty(i))
3441 this.addCouponRow(coupons[i]);
3442 }
3443};