var MyAccountInvoiceController = function(params) { this.init(params); } MyAccountInvoiceController.prototype = { invoice_id: false, invoice: 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(after) { var that = this; that.refresh_invoice(function() { $('#invoice_table').html("

Getting invoice...

"); that.print(); if (after) after(); }); }, refresh_invoice: function(after) { var that = this; $.ajax({ url: '/my-account/invoices/' + that.invoice_id + '/json', success: function(invoice) { that.invoice = invoice; if (after) after(); } }); }, /******************************************************************************/ line_items_for_invoice_package: function(invoice_package_id) { var that = this; var line_items = []; $.each(that.invoice.line_items, function(i, li) { if (li.invoice_package_id == invoice_package_id) line_items.push(li); }); return line_items; }, print: function() { var that = this; if (that.invoice.line_items && that.invoice.line_items.length > 0) { var count_packaged = 0; var count_unpackaged = 0; var count_downloadable = 0; $.each(that.invoice.line_items, function(i, li) { if (li.invoice_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('invoice').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); $('#invoice_table').empty().append(table); } else { $('#overview_table').empty(); $('#invoice_table').empty(); $('#message').empty().html("This invoice is empty."); } }, overview_table: function() { var that = this; var requires_shipping = that.invoice_requires_shipping(); var fstatus = $('
').append($('

').html(capitalize_first_letter(that.invoice.financial_status))); if (that.invoice.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('invoice'); var tr = $('') .append($('') .append($('
').html('Customer')); if (requires_shipping) tr.append($('').html('Shipping Address')) //.append($('').html('Billing Address')) tr.append($('').html('Status')) .append($('').html('Payment Status')); table.append(tr); tr = $('
').attr('valign', 'top').attr('id', 'customer' ).append(that.noneditable_customer())); if (requires_shipping) tr.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())) tr.append($('').attr('valign', 'top').attr('align', 'center').append($('

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

').attr('valign', 'top').attr('id', 'financial_status' ).attr('align', 'center').append(fstatus)); table.append(tr); return table; }, noneditable_customer: function() { var that = this; c = that.invoice.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.invoice.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_invoice(function() { that.edit_shipping_address(); }); })); } else { div.append("This invoice does not require shipping."); } return div; }, edit_shipping_address: function() { var that = this; var sa = that.invoice.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_invoice(function() { $('#shipping_address').empty().append(that.noneditable_shipping_address()); }); })); new ModelBinder({ name: 'ShippingAddress', id: sa.id, update_url: '/my-account/invoices/' + that.invoice.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.invoice.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_invoice(function() { that.edit_billing_address(); }); // })); // return div; //}, // //edit_billing_address: function() //{ // var that = this; // var sa = that.invoice.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_invoice(function() { $('#billing_address').empty().append(that.noneditable_billing_address()); }); // })); // // new ModelBinder({ // name: 'BillingAddress', // id: sa.id, // update_url: '/my-account/invoices/' + that.invoice.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.invoice.invoice_packages, function(i, op) { var line_items = that.line_items_for_invoice_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; var count = 0; var line_items = []; $.each(that.invoice.line_items, function(i, li) { line_items.push(li); }); if (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.invoice.invoice_packages, function(i, op) { var line_items = that.line_items_for_invoice_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', requires_shipping ? '6' : '5').addClass('totals_header').html('Totals'))); table.append($('
').attr('colspan', requires_shipping ? '5' : '4').attr('align', 'right').html('Subtotal' )).append($('').attr('align', 'right').attr('id', 'subtotal').html(curr(that.invoice.subtotal )))); table.append($('
').attr('colspan', requires_shipping ? '5' : '4').attr('align', 'right').html('Tax' )).append($('').attr('align', 'right').attr('id', 'tax' ).html(curr(that.invoice.tax )))); table.append($('
').attr('colspan', requires_shipping ? '5' : '4').attr('align', 'right').html('Shipping' )).append($('').attr('align', 'right').attr('id', 'shipping').html(curr(that.invoice.shipping )))); table.append($('
').attr('colspan', requires_shipping ? '5' : '4').attr('align', 'right').html('Handling' )).append($('').attr('align', 'right').attr('id', 'handling').html(curr(that.invoice.handling )))); if (that.invoice.discounts) { $.each(that.invoice.discounts, function(i, d) { table.append($('
').attr('colspan', requires_shipping ? '5' : '4').attr('align', 'right').append(' "' + d.gift_card.code + '" Discount')) .append($('').attr('align', 'right').html(curr(d.amount))) ); }); } table.append($('
').attr('colspan', requires_shipping ? '5' : '4').attr('align', 'right').html('Discount')).append($('').attr('align', 'right').attr('id', 'custom_discount').html(curr(that.invoice.custom_discount)))); table.append($('
').attr('colspan', requires_shipping ? '5' : '4').attr('align', 'right').html('Total' )).append($('').attr('align', 'right').attr('id', 'total' ).html(curr(that.invoice.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/invoices/' + that.invoice.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) { var that = this; console.log('RELAY'); console.log(resp); if (resp.success == true) { $('#payment_form').empty(); $('#payment_message').empty().html("

Your payment has been processed successfully.

"); setTimeout(function() { $('#payment_message').empty() }, 5000); that.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.invoice.line_items, function(i, li) { if (li.variant.downloadable == false) needs_shipping = true; }); return needs_shipping; }, invoice_requires_shipping: function() { var that = this; var requires = false; $.each(that.invoice.line_items, function(i, li) { if (li.requires_shipping && !li.downloadable) requires = true; }); return requires; } }; function relay_handler(resp) { controller.payment_relay_handler(resp); }