/** * 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@magentocommerce.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.magentocommerce.com for more information. * * @category Mage * @package Mage_Adminhtml * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.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 = {}; }, 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 = 'dataShow'; 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'); 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(/.*?([0-9\.,]+)/); 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'); }, sidebarApplyChanges : function(){ if($(this.getAreaId('sidebar'))){ var data = {}; var elems = $(this.getAreaId('sidebar')).select('input'); for(var i=0; i