/** * Magento * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE_AFL.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@magento.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade Magento to newer * versions in the future. If you wish to customize Magento for your * needs please refer to http://www.magento.com for more information. * * @category Mage * @package Mage_Adminhtml * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com) * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) */ var AdminOrder = new Class.create(); AdminOrder.prototype = { initialize : function(data){ if(!data) data = {}; this.loadBaseUrl = false; this.customerId = data.customer_id ? data.customer_id : false; this.storeId = data.store_id ? data.store_id : false; this.currencyId = false; this.currencySymbol = data.currency_symbol ? data.currency_symbol : ''; this.addresses = data.addresses ? data.addresses : $H({}); this.shippingAsBilling = data.shippingAsBilling ? data.shippingAsBilling : false; this.gridProducts = $H({}); this.gridProductsGift = $H({}); this.billingAddressContainer = ''; this.shippingAddressContainer= ''; this.isShippingMethodReseted = data.shipping_method_reseted ? data.shipping_method_reseted : false; this.overlayData = $H({}); this.giftMessageDataChanged = false; this.productConfigureAddFields = {}; this.productPriceBase = {}; this.collectElementsValue = true; Event.observe(window, 'load', (function(){ this.dataArea = new OrderFormArea('data', $(this.getAreaId('data')), this); this.itemsArea = Object.extend(new OrderFormArea('items', $(this.getAreaId('items')), this), { addControlButton: function(button){ var controlButtonArea = $(this.node).select('.form-buttons')[0]; if (typeof controlButtonArea != 'undefined') { var buttons = controlButtonArea.childElements(); for (var i = 0; i < buttons.length; i++) { if (buttons[i].innerHTML.include(button.label)) { return ; } } button.insertIn(controlButtonArea, 'top'); } } }); var searchButton = new ControlButton(Translator.translate('Add Products')), searchAreaId = this.getAreaId('search'); searchButton.onClick = function() { $(searchAreaId).show(); var el = this; window.setTimeout(function () { el.remove(); }, 10); } this.dataArea.onLoad = this.dataArea.onLoad.wrap(function(proceed) { proceed(); this._parent.itemsArea.setNode($(this._parent.getAreaId('items'))); this._parent.itemsArea.onLoad(); }); this.itemsArea.onLoad = this.itemsArea.onLoad.wrap(function(proceed) { proceed(); if (!$(searchAreaId).visible()) { this.addControlButton(searchButton); } }); this.areasLoaded(); this.itemsArea.onLoad(); }).bind(this)); }, areasLoaded: function(){ }, itemsLoaded: function(){ }, dataLoaded: function(){ this.dataShow(); }, setLoadBaseUrl : function(url){ this.loadBaseUrl = url; }, setAddresses : function(addresses){ this.addresses = addresses; }, setCustomerId : function(id){ this.customerId = id; this.loadArea('header', true); $(this.getAreaId('header')).callback = 'setCustomerAfter'; $('back_order_top_button').hide(); $('reset_order_top_button').show(); }, setCustomerAfter : function () { this.customerSelectorHide(); if (this.storeId) { $(this.getAreaId('data')).callback = 'dataLoaded'; this.loadArea(['data'], true); } else { this.storeSelectorShow(); } }, setStoreId : function(id){ this.storeId = id; this.storeSelectorHide(); this.sidebarShow(); //this.loadArea(['header', 'sidebar','data'], true); this.dataShow(); this.loadArea(['header', 'data'], true); }, setCurrencyId : function(id){ this.currencyId = id; //this.loadArea(['sidebar', 'data'], true); this.loadArea(['data'], true); }, setCurrencySymbol : function(symbol){ this.currencySymbol = symbol; }, selectAddress : function(el, container){ id = el.value; if (id.length == 0) { id = '0'; } if(this.addresses[id]){ this.fillAddressFields(container, this.addresses[id]); } else{ this.fillAddressFields(container, {}); } var data = this.serializeData(container); data[el.name] = id; if(this.isShippingField(container) && !this.isShippingMethodReseted){ this.resetShippingMethod(data); } else{ this.saveData(data); } }, isShippingField : function(fieldId){ if(this.shippingAsBilling){ return fieldId.include('billing'); } return fieldId.include('shipping'); }, isBillingField : function(fieldId){ return fieldId.include('billing'); }, bindAddressFields : function(container) { var fields = $(container).select('input', 'select', 'textarea'); for(var i=0;i 0) { checkbox.inputElements = inputs; for (var i = 0; i < inputs.length; i++) { var input = inputs[i]; input.checkboxElement = checkbox; var product = this.gridProducts.get(checkbox.value); if (product) { var defaultValue = product[input.name]; if (defaultValue) { if (input.name == 'giftmessage') { input.checked = true; } else { input.value = defaultValue; } } } input.disabled = !checkbox.checked || input.hasClassName('input-inactive'); Event.observe(input,'keyup', this.productGridRowInputChange.bind(this)); Event.observe(input,'change',this.productGridRowInputChange.bind(this)); } } }, productGridRowInputChange : function(event){ var element = Event.element(event); if (element && element.checkboxElement && element.checkboxElement.checked){ if (element.name!='giftmessage' || element.checked) { this.gridProducts.get(element.checkboxElement.value)[element.name] = element.value; } else if (element.name=='giftmessage' && this.gridProducts.get(element.checkboxElement.value)[element.name]) { delete(this.gridProducts.get(element.checkboxElement.value)[element.name]); } } }, productGridRowClick : function(grid, event){ var trElement = Event.findElement(event, 'tr'); var qtyElement = trElement.select('input[name="qty"]')[0]; var eventElement = Event.element(event); var isInputCheckbox = eventElement.tagName == 'INPUT' && eventElement.type == 'checkbox'; var isInputQty = eventElement.tagName == 'INPUT' && eventElement.name == 'qty'; if (trElement && !isInputQty) { var checkbox = Element.select(trElement, 'input[type="checkbox"]')[0]; var confLink = Element.select(trElement, 'a')[0]; var priceColl = Element.select(trElement, '.price')[0]; if (checkbox) { // processing non composite product if (confLink.readAttribute('disabled')) { var checked = isInputCheckbox ? checkbox.checked : !checkbox.checked; grid.setCheckboxChecked(checkbox, checked); // processing composite product } else if (isInputCheckbox && !checkbox.checked) { grid.setCheckboxChecked(checkbox, false); // processing composite product } else if (!isInputCheckbox || (isInputCheckbox && checkbox.checked)) { var listType = confLink.readAttribute('list_type'); var productId = confLink.readAttribute('product_id'); if (typeof this.productPriceBase[productId] == 'undefined') { var priceBase = priceColl.innerHTML.match(/.*?([\d,]+\.?\d*)/); if (!priceBase) { this.productPriceBase[productId] = 0; } else { this.productPriceBase[productId] = parseFloat(priceBase[1].replace(/,/g,'')); } } productConfigure.setConfirmCallback(listType, function() { // sync qty of popup and qty of grid var confirmedCurrentQty = productConfigure.getCurrentConfirmedQtyElement(); if (qtyElement && confirmedCurrentQty && !isNaN(confirmedCurrentQty.value)) { qtyElement.value = confirmedCurrentQty.value; } // calc and set product price var productPrice = parseFloat(this._calcProductPrice() + this.productPriceBase[productId]); priceColl.innerHTML = this.currencySymbol + productPrice.toFixed(2); // and set checkbox checked grid.setCheckboxChecked(checkbox, true); }.bind(this)); productConfigure.setCancelCallback(listType, function() { if (!$(productConfigure.confirmedCurrentId) || !$(productConfigure.confirmedCurrentId).innerHTML) { grid.setCheckboxChecked(checkbox, false); } }); productConfigure.setShowWindowCallback(listType, function() { // sync qty of grid and qty of popup var formCurrentQty = productConfigure.getCurrentFormQtyElement(); if (formCurrentQty && qtyElement && !isNaN(qtyElement.value)) { formCurrentQty.value = qtyElement.value; } }.bind(this)); productConfigure.showItemConfiguration(listType, productId); } } } }, /** * Calc product price through its options */ _calcProductPrice: function () { var productPrice = 0; var getPriceFields = function (elms) { var productPrice = 0; var getPrice = function (elm) { var optQty = 1; if (elm.hasAttribute('qtyId')) { if (!$(elm.getAttribute('qtyId')).value) { return 0; } else { optQty = parseFloat($(elm.getAttribute('qtyId')).value); } } if (elm.hasAttribute('price') && !elm.disabled) { return parseFloat(elm.readAttribute('price')) * optQty; } return 0; }; for(var i = 0; i < elms.length; i++) { if (elms[i].type == 'select-one' || elms[i].type == 'select-multiple') { for(var ii = 0; ii < elms[i].options.length; ii++) { if (elms[i].options[ii].selected) { productPrice += getPrice(elms[i].options[ii]); } } } else if (((elms[i].type == 'checkbox' || elms[i].type == 'radio') && elms[i].checked) || ((elms[i].type == 'file' || elms[i].type == 'text' || elms[i].type == 'textarea' || elms[i].type == 'hidden') && Form.Element.getValue(elms[i])) ) { productPrice += getPrice(elms[i]); } } return productPrice; }.bind(this); productPrice += getPriceFields($(productConfigure.confirmedCurrentId).getElementsByTagName('input')); productPrice += getPriceFields($(productConfigure.confirmedCurrentId).getElementsByTagName('select')); productPrice += getPriceFields($(productConfigure.confirmedCurrentId).getElementsByTagName('textarea')); return productPrice; }, productGridCheckboxCheck : function(grid, element, checked){ if (checked) { if(element.inputElements) { this.gridProducts.set(element.value, {}); var product = this.gridProducts.get(element.value); for (var i = 0; i < element.inputElements.length; i++) { var input = element.inputElements[i]; if (!input.hasClassName('input-inactive')) { input.disabled = false; if (input.name == 'qty' && !input.value) { input.value = 1; } } if (input.checked || input.name != 'giftmessage') { product[input.name] = input.value; } else if (product[input.name]) { delete(product[input.name]); } } } } else { if(element.inputElements){ for(var i = 0; i < element.inputElements.length; i++) { element.inputElements[i].disabled = true; } } this.gridProducts.unset(element.value); } grid.reloadParams = {'products[]':this.gridProducts.keys()}; }, /** * Submit configured products to quote */ productGridAddSelected : function(){ if(this.productGridShowButton) Element.show(this.productGridShowButton); var area = ['search', 'items', 'shipping_method', 'totals', 'giftmessage','billing_method']; // prepare additional fields and filtered items of products var fieldsPrepare = {}; var itemsFilter = []; var products = this.gridProducts.toObject(); for (var productId in products) { itemsFilter.push(productId); var paramKey = 'item['+productId+']'; for (var productParamKey in products[productId]) { paramKey += '['+productParamKey+']'; fieldsPrepare[paramKey] = products[productId][productParamKey]; } } this.productConfigureSubmit('product_to_add', area, fieldsPrepare, itemsFilter); productConfigure.clean('quote_items'); this.hideArea('search'); this.gridProducts = $H({}); }, selectCustomer : function(grid, event){ var element = Event.findElement(event, 'tr'); if (element.title){ this.setCustomerId(element.title); } }, customerSelectorHide : function(){ this.hideArea('customer-selector'); }, customerSelectorShow : function(){ this.showArea('customer-selector'); }, storeSelectorHide : function(){ this.hideArea('store-selector'); }, storeSelectorShow : function(){ this.showArea('store-selector'); }, dataHide : function(){ this.hideArea('data'); }, dataShow : function(){ if ($('submit_order_top_button')) { $('submit_order_top_button').show(); } this.showArea('data'); }, clearShoppingCart : function(confirmMessage){ if (confirm(confirmMessage)) { this.collectElementsValue = false; order.sidebarApplyChanges({'sidebar[empty_customer_cart]': 1}); } }, sidebarApplyChanges : function(auxiliaryParams) { if ($(this.getAreaId('sidebar'))) { var data = {}; if (this.collectElementsValue) { var elems = $(this.getAreaId('sidebar')).select('input'); for (var i=0; i < elems.length; i++) { if (elems[i].getValue()) { data[elems[i].name] = elems[i].getValue(); } } } if (auxiliaryParams instanceof Object) { for (var paramName in auxiliaryParams) { data[paramName] = String(auxiliaryParams[paramName]); } } data.reset_shipping = true; this.loadArea(['sidebar', 'items', 'shipping_method', 'billing_method','totals', 'giftmessage'], true, data); } }, sidebarHide : function(){ if(this.storeId === false && $('page:left') && $('page:container')){ $('page:left').hide(); $('page:container').removeClassName('container'); $('page:container').addClassName('container-collapsed'); } }, sidebarShow : function(){ if($('page:left') && $('page:container')){ $('page:left').show(); $('page:container').removeClassName('container-collapsed'); $('page:container').addClassName('container'); } }, /** * Show configuration of product and add handlers on submit form * * @param productId */ sidebarConfigureProduct: function (listType, productId, itemId) { // create additional fields var params = {}; params.reset_shipping = true; params.add_product = productId; this.prepareParams(params); for (var i in params) { if (params[i] === null) { unset(params[i]); } else if (typeof(params[i]) == 'boolean') { params[i] = params[i] ? 1 : 0; } } var fields = []; for (var name in params) { fields.push(new Element('input', {type: 'hidden', name: name, value: params[name]})); } // add additional fields before triggered submit productConfigure.setBeforeSubmitCallback(listType, function() { productConfigure.addFields(fields); }.bind(this)); // response handler productConfigure.setOnLoadIFrameCallback(listType, function(response) { if (!response.ok) { return; } this.loadArea(['items', 'shipping_method', 'billing_method','totals', 'giftmessage'], true); }.bind(this)); // show item configuration itemId = itemId ? itemId : productId; productConfigure.showItemConfiguration(listType, itemId); return false; }, removeSidebarItem : function(id, from){ this.loadArea(['sidebar_'+from], 'sidebar_data_'+from, {remove_item:id, from:from}); }, itemsUpdate : function(){ var area = ['sidebar', 'items', 'shipping_method', 'billing_method','totals', 'giftmessage']; // prepare additional fields var fieldsPrepare = {update_items: 1}; var info = $('order-items_grid').select('input', 'select', 'textarea'); for(var i=0; i option[value=' + currentCustomerGroupId + ']')[0].text; var customerGroupOption = $$('#' + groupIdHtmlId + ' > option[value=' + groupId + ']')[0]; var confirmText = message.replace(/%s/, customerGroupOption.text); confirmText = confirmText.replace(/%s/, currentCustomerGroupTitle); if (confirm(confirmText)) { $$('#' + groupIdHtmlId + ' option').each(function(o) { o.selected = o.readAttribute('value') == groupId; }); this.accountGroupChange(); } } }; var OrderFormArea = Class.create(); OrderFormArea.prototype = { _name: null, _node: null, _parent: null, _callbackName: null, initialize: function(name, node, parent){ this._name = name; this._parent = parent; this._callbackName = node.callback; if (typeof this._callbackName == 'undefined') { this._callbackName = name + 'Loaded'; node.callback = this._callbackName; } parent[this._callbackName] = parent[this._callbackName].wrap((function (proceed){ proceed(); this.onLoad(); }).bind(this)); this.setNode(node); }, setNode: function(node){ if (!node.callback) { node.callback = this._callbackName; } this.node = node; }, onLoad: function(){ } }; var ControlButton = Class.create(); ControlButton.prototype = { _label: '', _node: null, initialize: function(label){ this._label = label; this._node = new Element('button', { 'class': 'scalable add', 'type': 'button' }); }, onClick: function(){ }, insertIn: function(element, position){ var node = Object.extend(this._node), content = {}; node.observe('click', this.onClick); node.update('' + this._label + ''); content[position] = node; Element.insert(element, content); } };