/** * Could (should) tidy this up to move the Quote/Invoice etc Specific * functionality into seperate files. * * Will do this at some point. Today I just need to hammer this out. * * Karl - 20/5/2011 */ $(function() { // var config defined in global.js loadLineItems(); var docID = $('#documentID').html(); $("#flashMessage").hide(); $("#lineItemDetails").hide(); $( "#addLineItemModal" ).dialog({ autoOpen: false, height: 900, width: 600, modal: true, buttons: { "Add Line Item": function() { $('#LineItemDescription').ckeditor(function() { this.updateElement(); this.destroy(); }); var thisLineItemInputs = $('#LineItemAddForm').find('input,select,textarea'); $.post('/line_items/ajax_add', thisLineItemInputs, function(data) { if(data == 'SUCCESS') { loadLineItems(); $( "#addLineItemModal" ).dialog('close'); } else { alert("Line Item could not be saved") $('#LineItemDescription').ckeditor(config); } }); }, Cancel: function() { loadLineItems(); $( this ).dialog( "close" ); } } }); $( "#editLineItemModal" ).dialog({ autoOpen: false, height: 900, width: 600, modal: true, buttons: { "Edit Line Item": function() { $('#LineItemDescription').ckeditor(function() { this.updateElement(); this.destroy(); }); var thisLineItemInputs = $('#LineItemEditForm').find('input,select,textarea'); $.post('/line_items/ajax_edit', thisLineItemInputs, function(data) { if(data == 'SUCCESS') { $( "#editLineItemModal" ).dialog('close'); } else { alert("Line Item could not be saved") $('#LineItemDescription').ckeditor(config); } }); }, Cancel: function() { $( "#editLineItemModal" ).dialog('close'); } }, close: function() { $('#LineItemDescription').ckeditor(function() { this.destroy(); }); loadLineItems(); } }); $( "#QuoteDetails" ).dialog({ autoOpen: false, height: 900, width: 600, modal: true, buttons: { "Edit Quote Details": function() { $('#QuoteCommercialComments').ckeditor(function() { this.updateElement(); this.destroy(); }); var quoteInputs = $('#QuoteEditForm').find('input,select,textarea'); $.post('/quotes/ajax_edit', quoteInputs, function(data) { $( "#QuoteDetails" ).dialog('close'); }); }, Cancel: function() { $( this ).dialog( "close" ); } }, close: function() { loadLineItems(); } }); $("#pageContentFactory").hide(); //Add a new Page Element. $("#addPage").button().click(function(event) { event.preventDefault(); newPage(false); return false; }); //Open the LineItem dialog $("#addLineItem").button().click(function() { $('#LineItemDescription').ckeditor(function() { this.destroy(); }); $("#editLineItemModal").empty(); var nextItemNo = $(".lineItem").length; nextItemNo++; $.get('/line_items/add/'+docID, function(data) { $("#addLineItemModal").html(data); $("#LineItemItemNumber").val(nextItemNo); //Auto fill in the next Item No $("#productDetails").hide(); $('#LineItemDescription').ckeditor(config); $( "#addLineItemModal" ).dialog('open'); }); return false; }); $(".editLineItem").live('click', function() { $('#LineItemDescription').ckeditor(function() { this.destroy(); }); $("#addLineItemModal").empty(); var thisLineItemID = $(this).parent('td').attr('id'); $.get('/line_items/edit/'+thisLineItemID, function(data) { $("#editLineItemModal").html(data); $("#productDetails").hide(); $('#LineItemDescription').ckeditor(config); $( "#editLineItemModal" ).dialog('open'); }); }); $("#principleSelect").live('change',function() { var principleID = getSelectedID('#principleSelect'); $("#productDetails").hide(); $.get('/documents/getProducts/'+principleID, function(data) { $('#productsDiv').html(data); }); }); $("#productSelect").live('change',function() { var productID = getSelectedID('#productSelect'); $.get('/documents/getProductDetails/'+productID, function(data) { $("#lineItemDetails").show(); $("#LineItemProductId").val(data.id); $("#LineItemTitle").val(data.title); $("#LineItemDescription").val(data.description); }, "json"); }); // Initialize the editor. // Callback function can be passed and executed after full instance creation. $('.page').ckeditor(config); //Remove this Page $(".removePage").live('click',function() { $('.page').ckeditor(function() { this.destroy(); }); $(this).parents(".docPage").remove(); $('.page').ckeditor(config); }); $("#savePages").click(function() { savePages(); }); $(".quickpricing").live('change', function() { calculateQuickPrices(); }); $('.removeLineItem').live('click', function() { var thisLineItemID = $(this).parent('td').attr('id'); $.post('/line_items/ajax_delete/'+thisLineItemID, function(data) { loadLineItems(); }); }); $("#editQuoteDetails").click(function() { var quoteID = $("#quoteID").html(); $('#QuoteCommercialComments').ckeditor(function() { this.destroy(); }); $.get('/quotes/edit/'+quoteID, function(data) { $("#QuoteDetails").html(data); $('#QuoteCommercialComments').ckeditor(config); $("#QuoteDetails").dialog('open'); }); }); $("#generateFirstPage").click(function() { if($(".firstPage").length == 0) { newPage(true); } $.get('/documents/generateFirstPage/'+docID, function(data) { $(".firstPage").val(data); savePages(); }); }); }); $('.generateCommercialComments').live('click', function(event) { event.preventDefault(); var deliveryTime = $("#QuoteDeliveryTime").val(); var deliveryTF = $("#QuoteDeliveryTimeFrame").val(); var paymentTerms = $("#QuotePaymentTerms").val(); var daysValid = $("#QuoteDaysValid").val(); var deliveryPoint = $("#QuoteDeliveryPoint").val(); var exchangeRate = $("#QuoteExchangeRate").val(); var customsDuty = $("#QuoteCustomsDuty").val(); deliveryTime = deliveryTime.toUpperCase(); paymentTerms = paymentTerms.toUpperCase(); deliveryPoint = deliveryPoint.toUpperCase(); var commComments = $("#commCommentsInitialString").clone(); var commList = commComments.find('ol'); commList.append('
  • DELIVERY IS ESTIMATED AT '+deliveryTime+ ' '+deliveryTF+ ' FROM RECEIPT OF YOUR TECHNICALLY AND COMMERCIALLY CLEAR ORDER'); commList.append('
  • PAYMENT TERMS: '+paymentTerms+'
  • '); commList.append('
  • QUOTATION IS VALID FOR '+daysValid+' DAYS
  • '); commList.append('
  • ALL PRICES ARE '+deliveryPoint+'
  • '); commList.append('
  • EXCHANGE RATE: '+exchangeRate+'
  • '); commList.append('
  • CUSTOMS DUTY INCLUDED AT: '+customsDuty+'
  • '); commList.append('
  • GST 10% EXTRA
  • '); commList.append('
  • WHEN PAYMENTS ARE MADE INTO OUR BANK ACCOUNT, BANK CHARGES ARE YOUR RESPONSIBILITY
  • '); $('#QuoteCommercialComments').val(commComments.html()); }); function newPage(firstPage) { $('.page').ckeditor(function() { this.destroy(); }); var newPage = $('#pageContentFactory').clone(); newPage.removeAttr('id'); newPage.show(); var pageCount = $('.docPage').length; //alert(pageCount); pageCount++; var model = 'DocPage'; var field = 'content'; var ID = getCakeID(model,pageCount, field); var name = getCakeName(model, pageCount, field); newPage.find('label').attr('for', ID); newPage.find('textarea').attr('id', ID).attr('name', name); newPage.addClass('docPage'); if(firstPage == true) { newPage.find('textarea').addClass('firstPage'); } var hiddenName = getCakeName(model, pageCount, 'page_number'); newPage.append(''); $('.pages').append(newPage); $('.page').ckeditor(config); } function savePages() { var docPages = $('#DocumentEditForm').find('input,select,textarea'); $('.page').ckeditor(function() { this.updateElement(); }); $.post('/documents/ajax_edit', docPages, function(data) { if(data =='SUCCESS') { $("#flashMessage").html("Pages Saved Successfully"); } else { $("#flashMessage").html("Unable to Save Pages"); } $("#flashMessage").show(); loadLineItems(); }); } function loadLineItems() { var documentID = $("#documentID").html(); /*$.get('/line_items/getTable/'+documentID, function(data) { $("#lineItems").html(data); });*/ $.ajax({ url: '/line_items/getTable/'+documentID, cache: false, success: function(data) { $("#lineItems").html(data); } }); } function calculateQuickPrices() { var quantity = $('#LineItemQuantity').val(); var gross_unit_price = $("#LineItemGrossUnitPrice").val(); var net_unit_price = $("#LineItemNetUnitPrice").val(); var discount_percent = $("#LineItemDiscountPercent").val(); var discount_amount_unit = $("#LineItemDiscountAmountUnit").val(); var discount_amount_total = $("#LineItemDiscountAmountTotal").val(); var gross_price = $("#LineItemGrossPrice").val(); var net_price = $("#LineItemNetPrice").val(); gross_price = quantity * gross_unit_price; $("#LineItemGrossPrice").val(gross_price); discount_amount_unit = (discount_percent/100) * gross_unit_price; discount_amount_unit = discount_amount_unit.toFixed(2); discount_amount_total = (discount_percent/100) * gross_price; discount_amount_total = discount_amount_total.toFixed(2); $("#LineItemDiscountAmountTotal").val(discount_amount_total); net_price = gross_price - discount_amount_total; $("#LineItemNetPrice").val(net_price); $("#LineItemDiscountAmountUnit").val(discount_amount_unit); net_unit_price = gross_unit_price - discount_amount_unit; $("#LineItemNetUnitPrice").val(net_unit_price); } function calcNetPrice() { var discountPercent = $("#discountPercent").val(); var unitPrice = $('#unitPrice').val(); var quantity = $('#LineItemQuantity').val(); var grossSellPrice = quantity * unitPrice; //Calculate the Sale Discount amount. var UnitDiscountAmount = (discountPercent/100) * unitPrice; var TotalDiscountAmount = (discountPercent/100) * grossSellPrice; UnitDiscountAmount = UnitDiscountAmount.toFixed(2); TotalDiscountAmount = TotalDiscountAmount.toFixed(2); $('#total_discountAmount').val(TotalDiscountAmount); $('#discountAmountEach').val(UnitDiscountAmount); $('#net_price_each').val(unitPrice - UnitDiscountAmount); $('#grossPrice').val(grossSellPrice); var netPrice = grossSellPrice - TotalDiscountAmount; $('#netPrice').val(netPrice); } function checkNaN(value) { if( isNaN(value) == true) { return 0; } else { return value; } }