var MyAccountOrderController = function(params) { this.init(params); } MyAccountOrderController.prototype = { order_id: false, order: false, authenticity_token: false, init: function(params) { for (var i in params) this[i] = params[i]; var that = this; $('#payment_form').hide(); $(document).ready(function() { that.refresh(); }); }, refresh: function() { var that = this; that.refresh_order(function() { $('#order_table').html("

Getting order...

"); that.print(); }); }, refresh_order: function(after) { var that = this; $.ajax({ url: '/my-account/orders/' + that.order_id + '/json', success: function(order) { that.order = order; if (after) after(); } }); }, /******************************************************************************/ line_items_for_order_package: function(order_package_id) { var that = this; var line_items = []; $.each(that.order.line_items, function(i, li) { if (li.order_package_id == order_package_id) line_items.push(li); }); return line_items; }, print: function() { var that = this; if (that.order.line_items && that.order.line_items.length > 0) { var count_packaged = 0; var count_unpackaged = 0; var count_downloadable = 0; $.each(that.order.line_items, function(i, li) { if (li.order_package_id) count_packaged++; else if (li.variant.downloadable == true) count_downloadable++; else count_unpackaged++; }); var table = that.overview_table(); $('#overview_table').empty().append(table).append($('
')); table = $('').addClass('order').css('width', '100%'); if (count_packaged > 0) that.packaged_line_items_table(table); if (count_unpackaged > 0) that.unpackaged_line_items_table(table); if (count_downloadable > 0) that.downloadable_line_items_table(table); that.summary_table(table); $('#order_table').empty().append(table); } else { $('#overview_table').empty(); $('#order_table').empty(); $('#message').empty().html("This order is empty."); } }, overview_table: function() { var that = this; var fstatus = $('
').append($('

').html(capitalize_first_letter(that.order.financial_status))); if (that.order.financial_status == 'pending') { fstatus.append($('

').append($('').attr('type', 'button').addClass('btn').val('Pay now').click(function(e) { e.preventDefault(); that.payment_form(); }))); } var table = $('

').addClass('order'); table.append($('') .append($('') .append($('
').html('Customer')) .append($('').html('Shipping Address')) .append($('').html('Billing Address')) .append($('').html('Order Status')) .append($('').html('Payment Status')) ); table.append($('
').attr('valign', 'top').attr('id', 'customer' ).append(that.noneditable_customer())) .append($('').attr('valign', 'top').attr('id', 'shipping_address' ).append(that.noneditable_shipping_address())) .append($('').attr('valign', 'top').attr('id', 'billing_address' ).append(that.noneditable_billing_address())) .append($('').attr('valign', 'top').attr('align', 'center').append($('

').html(capitalize_first_letter(that.order.status)))) .append($('

').attr('valign', 'top').attr('align', 'center').append(fstatus)) ); return table; }, noneditable_customer: function() { var that = this; c = that.order.customer; str = ''; if (c) { str = c.first_name + ' ' + c.last_name; if (c.email) str += '
' + c.email + ''; if (c.phone) str += '
' + c.phone; } else str = '[Empty]'; return str; }, noneditable_shipping_address: function() { var that = this; var div = $('
'); if (that.has_shippable_items()) { var sa = that.order.shipping_address; str = ''; str += (sa.first_name ? sa.first_name : '[Empty first name]') + ' '; str += (sa.last_name ? sa.last_name : '[Empty last name]'); str += '
' + (sa.address1 ? sa.address1 : '[Empty address]'); if (sa.address2) str += "
" + sa.address2; str += '
' + (sa.city ? sa.city : '[Empty city]') + ", " + (sa.state ? sa.state : '[Empty state]') + " " + (sa.zip ? sa.zip : '[Empty zip]'); div.append($('
').attr('id', 'shipping_address').append(str)); div.append($('').attr('href', '#').html('Edit').click(function(e) { var a = $(this); that.refresh_order(function() { that.edit_shipping_address(); }); })); } else { div.append("This order does not require shipping."); } return div; }, edit_shipping_address: function() { var that = this; var sa = that.order.shipping_address; var table = $('').addClass('shipping_address') .append($('').append($('
').append($('').append($('') .append($('').append($('
').append($('
').attr('id', 'shippingaddress_' + sa.id + '_first_name'))) .append($('
').append($('
').attr('id', 'shippingaddress_' + sa.id + '_last_name'))) )))) .append($('
').append($('').append($('') .append($('').append($('
').append($('
').attr('id', 'shippingaddress_' + sa.id + '_address1'))) )))) .append($('
').append($('').append($('') .append($('').append($('
').append($('
').attr('id', 'shippingaddress_' + sa.id + '_address2'))) )))) .append($('
').append($('').append($('') .append($('
').append($('
').attr('id', 'shippingaddress_' + sa.id + '_city'))) .append($('
').append($('
').attr('id', 'shippingaddress_' + sa.id + '_state'))) .append($('
').append($('
').attr('id', 'shippingaddress_' + sa.id + '_zip'))) )))); $('#shipping_address').empty() .append(table) .append($('').attr('href', '#').html('Finished').click(function(e) { var a = $(this); that.refresh_order(function() { $('#shipping_address').empty().append(that.noneditable_shipping_address()); }); })); new ModelBinder({ name: 'ShippingAddress', id: sa.id, update_url: '/my-account/orders/' + that.order.id + '/shipping-address', authenticity_token: that.authenticity_token, attributes: [ { name: 'first_name' , nice_name: 'First Name' , type: 'text' , value: sa.first_name , width: 150, fixed_placeholder: false }, { name: 'last_name' , nice_name: 'Last Name' , type: 'text' , value: sa.last_name , width: 150, fixed_placeholder: false }, { name: 'address1' , nice_name: 'Address 1' , type: 'text' , value: sa.address1 , width: 320, fixed_placeholder: false }, { name: 'address2' , nice_name: 'Address 2' , type: 'text' , value: sa.address2 , width: 320, fixed_placeholder: false }, { name: 'city' , nice_name: 'City' , type: 'text' , value: sa.city , width: 180, fixed_placeholder: false }, { name: 'state' , nice_name: 'State' , type: 'text' , value: sa.state , width: 40, fixed_placeholder: false }, { name: 'zip' , nice_name: 'Zip' , type: 'text' , value: sa.zip , width: 60, fixed_placeholder: false } ] }); }, noneditable_billing_address: function() { var that = this; var sa = that.order.billing_address; if (!sa) sa = {}; var str = ''; str += (sa.first_name ? sa.first_name : '[Empty first name]') + ' '; str += (sa.last_name ? sa.last_name : '[Empty last name]'); str += '
' + (sa.address1 ? sa.address1 : '[Empty address]'); if (sa.address2) str += "
" + sa.address2; str += '
' + (sa.city ? sa.city : '[Empty city]') + ", " + (sa.state ? sa.state : '[Empty state]') + " " + (sa.zip ? sa.zip : '[Empty zip]'); var div = $('
') .append(str) .append("
") .append($('
').attr('href', '#').html('Edit').click(function(e) { var a = $(this); that.refresh_order(function() { that.edit_billing_address(); }); })); return div; }, edit_billing_address: function() { var that = this; var sa = that.order.billing_address; if (!sa) sa = { id: 1 }; var table = $('').addClass('billing_address') .append($('').append($('
').append($('').append($('') .append($('').append($('
').append($('
').attr('id', 'billingaddress_' + sa.id + '_first_name'))) .append($('
').append($('
').attr('id', 'billingaddress_' + sa.id + '_last_name'))) )))) .append($('
').append($('').append($('') .append($('').append($('
').append($('
').attr('id', 'billingaddress_' + sa.id + '_address1'))) )))) .append($('
').append($('').append($('') .append($('').append($('
').append($('
').attr('id', 'billingaddress_' + sa.id + '_address2'))) )))) .append($('
').append($('').append($('') .append($('').append($('') .append($(''); tr.append($('').append($('') .append($(''); tr.append($('').append($('') .append($(''); tr.append($('').append($('').addClass('totals_row').append($('').addClass('totals_row').append($('').addClass('totals_row').append($('').addClass('totals_row').append($('').addClass('totals_row') .append($('').addClass('totals_row').append($('').addClass('totals_row').append($('
').append($('
').attr('id', 'billingaddress_' + sa.id + '_city'))) .append($('
').append($('
').attr('id', 'billingaddress_' + sa.id + '_state'))) .append($('
').append($('
').attr('id', 'billingaddress_' + sa.id + '_zip'))) )))); $('#billing_address').empty() .append(table) .append($('').attr('href', '#').html('Finished').click(function(e) { var a = $(this); that.refresh_order(function() { $('#billing_address').empty().append(that.noneditable_billing_address()); }); })); new ModelBinder({ name: 'BillingAddress', id: sa.id, update_url: '/my-account/orders/' + that.order.id + '/billing-address', authenticity_token: that.authenticity_token, attributes: [ { name: 'first_name' , nice_name: 'First Name' , type: 'text' , value: sa.first_name , width: 150, fixed_placeholder: false }, { name: 'last_name' , nice_name: 'Last Name' , type: 'text' , value: sa.last_name , width: 150, fixed_placeholder: false }, { name: 'address1' , nice_name: 'Address 1' , type: 'text' , value: sa.address1 , width: 320, fixed_placeholder: false }, { name: 'address2' , nice_name: 'Address 2' , type: 'text' , value: sa.address2 , width: 320, fixed_placeholder: false }, { name: 'city' , nice_name: 'City' , type: 'text' , value: sa.city , width: 180, fixed_placeholder: false }, { name: 'state' , nice_name: 'State' , type: 'text' , value: sa.state , width: 40, fixed_placeholder: false }, { name: 'zip' , nice_name: 'Zip' , type: 'text' , value: sa.zip , width: 60, fixed_placeholder: false } ] }); }, // Show all the packages and the line items in each package packaged_line_items_table: function(table) { var that = this; $.each(that.order.order_packages, function(i, op) { var line_items = that.line_items_for_order_package(op.id); if (line_items && line_items.length > 0) { table.append($('
').attr('colspan', '5').addClass('package_header').html("Package " + (i+1) + ": " + op.shipping_method.service_name + "
" + op.status))); table.append($('
').html('Item')) .append($('').html('Status')) .append($('').html('Unit Price')) .append($('').html('Quantity')) .append($('').html('Subtotal')) ); $.each(line_items, function(j, li) { var tr = $('
').addClass('line_item_details' ).append(that.line_item_details(li))); tr.append($('').addClass('line_item_status' ).attr('align', 'center').html(li.status)); tr.append($('').addClass('line_item_unit_price').attr('align', 'right' ).html(curr(li.unit_price))); tr.append($('').addClass('line_item_quantity' ).attr('align', 'right' ).html(li.quantity)); tr.append($('').addClass('line_item_subtotal' ).attr('align', 'right' ).html(curr(li.subtotal))); table.append(tr); }); } }); }, // Show all the packages and the line items in each package unpackaged_line_items_table: function(table) { var that = this; $.each(that.order.order_packages, function(i, op) { var line_items = that.line_items_for_order_package(op.id); if (line_items && line_items.length > 0) { table.append($('
').attr('colspan', '5').addClass('package_header').html("Unpackaged Items"))); table.append($('
').html('Item')) .append($('').html('Status')) .append($('').html('Unit Price')) .append($('').html('Quantity')) .append($('').html('Subtotal')) ); $.each(line_items, function(j, li) { var tr = $('
').addClass('line_item_details' ).append(that.line_item_details(li))); tr.append($('').addClass('line_item_status' ).attr('align', 'center').html(li.status)); tr.append($('').addClass('line_item_unit_price').attr('align', 'right' ).html(curr(li.unit_price))); tr.append($('').addClass('line_item_quantity' ).attr('align', 'right' ).html(li.quantity)); tr.append($('').addClass('line_item_subtotal' ).attr('align', 'right' ).html(curr(li.subtotal))); table.append(tr); }); } }); }, downloadable_line_items_table: function(table) { var that = this; $.each(that.order.order_packages, function(i, op) { var line_items = that.line_items_for_order_package(op.id); if (line_items && line_items.length > 0) { table.append($('
').attr('colspan', '5').addClass('package_header').html("Downloadable Items"))); table.append($('
').html('Item')) .append($('').html('Status')) .append($('').html('Unit Price')) .append($('').html('Quantity')) .append($('').html('Subtotal')) ); $.each(line_items, function(j, li) { var tr = $('
').addClass('line_item_details' ).append(that.line_item_details(li))); tr.append($('').addClass('line_item_status' ).attr('align', 'center').html(li.status)); tr.append($('').addClass('line_item_unit_price').attr('align', 'right' ).html(curr(li.unit_price))); tr.append($('').addClass('line_item_quantity' ).attr('align', 'right' ).html(li.quantity)); tr.append($('').addClass('line_item_subtotal' ).attr('align', 'right' ).html(curr(li.subtotal))); table.append(tr); }); } }); }, line_item_details: function(li) { var that = this; var v = li.variant; var name = '' if (!v || !v.product) name = v ? v.sku : 'Unknown variant'; else { name = v.product.title; if (v.sku && v.sku.length > 0) name += '
' + v.sku; if (v.title && v.title.length > 0) name += '
' + v.title; } var div = $('
').append($('

').append($('').attr('href', '/products/' + li.variant.product_id).html(name))); if (li.is_gift) { div.append($('

').attr('colspan', '5').addClass('totals_header').html('Totals'))); table.append($('
').attr('colspan', '4').attr('align', 'right').html('Subtotal' )).append($('').attr('align', 'right').attr('id', 'subtotal').html(curr(that.order.subtotal )))); table.append($('
').attr('colspan', '4').attr('align', 'right').html('Tax' )).append($('').attr('align', 'right').attr('id', 'tax' ).html(curr(that.order.tax )))); table.append($('
').attr('colspan', '4').attr('align', 'right').html('Shipping' )).append($('').attr('align', 'right').attr('id', 'shipping').html(curr(that.order.shipping )))); table.append($('
').attr('colspan', '4').attr('align', 'right').html('Handling' )).append($('').attr('align', 'right').attr('id', 'handling').html(curr(that.order.handling )))); if (that.order.discounts) { $.each(that.order.discounts, function(i, d) { table.append($('
').attr('colspan', '4').attr('align', 'right').append(' "' + d.gift_card.code + '" Discount')) .append($('').attr('align', 'right').html(curr(d.amount))) ); }); } table.append($('
').attr('colspan', '4').attr('align', 'right').html('Discount')).append($('').attr('align', 'right').attr('id', 'custom_discount').html(curr(that.order.custom_discount)))); table.append($('
').attr('colspan', '4').attr('align', 'right').html('Total' )).append($('').attr('align', 'right').attr('id', 'total' ).html(curr(that.order.total)))); }, /****************************************************************************/ payment_form: function() { var that = this; var form = $('#payment_form'); if (form.is(':visible')) { form.slideUp(function() { form.empty(); }); $('#payment_message').empty(); return; } $('#payment_message').empty().html("

Getting payment form...

"); $.ajax({ url: '/my-account/orders/' + that.order.id + '/payment-form', type: 'get', success: function(html) { form.empty().append(html); form.slideDown(); $('#payment_message').empty(); $('#payment_confirm').click(function(e) { $('#expiration').val($('#month').val() + $('#year').val()); $('#payment_message').empty().html("

Processing payment...

"); $('#payment_form').slideUp(); $('#payment').submit(); }); } }); }, payment_relay_handler: function(resp) { console.log('RELAY'); console.log(resp); if (resp.success == true) controller.refresh(); else if (resp.error) $('#payment_message').html("

" + resp.error + "

"); else $('#payment_message').html("

There was an error processing your payment.

"); }, has_shippable_items: function() { var that = this; var needs_shipping = false; $.each(that.order.line_items, function(i, li) { if (li.variant.downloadable == false) needs_shipping = true; }); return needs_shipping; }, }; function relay_handler(resp) { controller.payment_relay_handler(resp); }