var InvoiceController = function(params) { this.init(params); } InvoiceController.prototype = { invoice_id: false, invoice: false, authenticity_token: false, store_config: false, init: function(params) { for (var i in params) this[i] = params[i]; var that = this; $(document).ready(function() { that.get_store_config(); that.refresh(); }); }, get_store_config: function() { var that = this; $.ajax({ url: '/admin/store/json', success: function(sc) { that.store_config = sc; }, async: false }); }, refresh: function(after) { var that = this; that.refresh_invoice(function() { $('#invoice_table').html("

Getting invoice...

"); that.print(); that.make_editable(); if (after) after(); }); }, refresh_invoice: function(after) { var that = this; $.ajax({ url: '/admin/invoices/' + that.invoice_id + '/json', success: function(invoice) { that.invoice = invoice; $.each(that.invoice.invoice_transactions, function(i, t) { t.amount = parseFloat(t.amount); t.amount_refunded = t.amount_refunded == null || isNaN(t.amount_refunded) ? 0.00 : parseFloat(t.amount_refunded); }); that.refresh_numbers(); if (after) after(); } }); }, refresh_numbers: function() { var that = this; $('#subtotal').html(curr(that.invoice.subtotal)); $('#shipping').html(curr(that.invoice.shipping)); $('#total' ).html(curr(that.invoice.total )); $.each(that.invoice.line_items, function(i, li) { $('#li_' + li.id + '_subtotal').html(curr(li.subtotal)); }); }, make_editable: function() { var that = this; $.each(that.invoice.invoice_packages, function(i, op) { new ModelBinder({ name: 'InvoicePackage', id: op.id, update_url: '/admin/invoices/' + op.invoice_id + '/packages/' + op.id, authenticity_token: that.authenticity_token, attributes: [ { name: 'instore_pickup' , nice_name: 'In-store Pickup' , type: 'checkbox' , value: op.instore_pickup , width: 300, fixed_placeholder: true }, { name: 'status' , nice_name: 'Status' , type: 'select' , value: op.status , width: 300, fixed_placeholder: true , options_url: '/admin/invoice-packages/status-options' }, { name: 'package_method' , nice_name: 'Package/Method' , type: 'select' , value: op.shipping_package_id + '_' + op.shipping_method_id , width: 300, fixed_placeholder: false, options_url: '/admin/shipping-packages/package-method-options' }, { name: 'tracking_number' , nice_name: 'Tracking Number' , type: 'text' , value: op.tracking_number , width: 300, fixed_placeholder: true, align: 'right' }, { name: 'total' , nice_name: 'Shipping Total' , type: 'text' , value: curr(op.total) , width: 300, fixed_placeholder: true, align: 'right' , after_update: function() { that.refresh_invoice(); }} ] }); }); $.each(that.invoice.line_items, function(i, li) { var arr = [ { name: 'status' , nice_name: 'Status' , type: 'select' , align: 'left' , value: li.status , text: li.status, width: 150, fixed_placeholder: false, options_url: '/admin/invoices/line-items/status-options' }, { name: 'tracking_number' , nice_name: 'Tracking Number' , type: 'text' , align: 'left' , value: li.tracking_number , width: 200, fixed_placeholder: false }, { name: 'unit_price' , nice_name: 'Unit Price' , type: 'text' , align: 'right', value: curr(li.unit_price) , width: 75, fixed_placeholder: false, after_update: function() { that.refresh_invoice(); } }, { name: 'quantity' , nice_name: 'Quantity' , type: 'text' , align: 'right', value: li.quantity , width: 75, fixed_placeholder: false, after_update: function() { that.refresh_invoice(); } } ]; if (li.subscription_id) { arr.push({ name: 'date_starts' , nice_name: 'Starts' , type: 'date' , align: 'left' , value: li.date_starts , width: 100, fixed_placeholder: false, date_format: 'Y-m-d' }); arr.push({ name: 'date_ends' , nice_name: 'Ends' , type: 'date' , align: 'left' , value: li.date_ends , width: 100, fixed_placeholder: false, date_format: 'Y-m-d' }); } new ModelBinder({ name: 'Lineitem', id: li.id, update_url: '/admin/invoices/' + li.invoice_id + '/line-items/' + li.id, authenticity_token: that.authenticity_token, attributes: arr }); }); new ModelBinder({ name: 'Invoice', id: that.invoice.id, update_url: '/admin/invoices/' + that.invoice.id, authenticity_token: that.authenticity_token, attributes: [ { name: 'status' , nice_name: 'Status' , type: 'select' , value: that.invoice.status , width: 100, fixed_placeholder: false, options_url: '/admin/invoices/status-options' }, { name: 'financial_status' , nice_name: 'Status' , type: 'select' , value: that.invoice.financial_status , width: 100, fixed_placeholder: true , width: 200, options_url: '/admin/invoices/financial-status-options' }, { name: 'payment_terms' , nice_name: 'Terms' , type: 'select' , value: that.invoice.payment_terms , width: 200, fixed_placeholder: true , width: 200, options_url: '/admin/invoices/payment-terms-options' }, { name: 'tax' , nice_name: 'Tax' , type: 'text' , value: curr(that.invoice.tax) , width: 100, fixed_placeholder: false, align: 'right' , after_update: function() { that.refresh_invoice(); }}, { name: 'handling' , nice_name: 'Handling' , type: 'text' , value: curr(that.invoice.handling) , width: 100, fixed_placeholder: false, align: 'right' , after_update: function() { that.refresh_invoice(); }}, { name: 'custom_discount' , nice_name: 'Discount' , type: 'text' , value: curr(that.invoice.custom_discount) , width: 100, fixed_placeholder: false, align: 'right' , after_update: function() { that.refresh_invoice(); }}, { name: 'notes' , nice_name: 'Notes (not public)' , type: 'textarea' , value: that.invoice.notes , width: 100, fixed_placeholder: false, align: 'left' , after_update: function() { that.refresh_invoice(); }, height: 50 }, { name: 'customer_notes' , nice_name: 'Customer Notes' , type: 'textarea' , value: that.invoice.notes , width: 100, fixed_placeholder: false, align: 'left' , after_update: function() { that.refresh_invoice(); }, height: 50 } ] }); }, assign_to_package_form: function(li_id) { var that = this; if (!that.invoice.invoice_packages) that.invoice.invoice_packages = []; if (that.invoice.invoice_packages.length == 0) { that.assign_to_new_package_form(li_id); return; } var select = $('') .attr('id', 'package_id') .css('width', '400px') .change(function(e) { // Create the new invoice package var arr = $(this).val().split('_'); $.ajax({ url: '/admin/invoices/' + that.invoice.id + '/packages', type: 'post', data: { shipping_package_id: arr[0], shipping_method_id: arr[1] }, success: function(resp) { that.assign_to_package(li_id, resp.new_id); } }); } ); select.append($('').attr('label', name); $.each(sp.shipping_methods, function(j, sm) { optgroup.append($('