Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <% url = Rails.application.routes.url_helpers %>
- <% environment.context_class.instance_eval { include DateHelper } %>
- $(document).ready(function(){
- var clipboard = new Clipboard('#clipboard');
- clipboard.on('success', function(e) {
- $('.clipboard_message').html('Copied!').show().delay(2500).fadeOut('slow');
- });
- initialize_select_2();
- // show spinner on AJAX start
- $(document).ajaxStart(function(){
- $(".js-loader").removeClass('l-hidden');
- });
- // hide spinner on AJAX stop
- $(document).ajaxStop(function(){
- $(".js-loader").addClass('l-hidden');
- });
- if($('.sold_vehicles_entry_table').length >0)
- {
- populate_grid_row();
- initialize_select_2();
- }
- $(document.body).on('click', '.sold_vehicles_entry_table tr .add_row' ,function(){
- $('.sold_vehicles_entry_table tr:last .add_row input').remove()
- populate_grid_row();
- populate_values_from_preceding_row($(this).closest('tr'));
- initialize_select_2();
- });
- $(document.body).on('click', '.sold_vehicles_entry_table tr .delete_row' ,function(){
- if($('.sold_vehicles_entry_table tr').length==2){
- alert("Minimum one row is needed!!!");
- return false;
- }
- if(confirm("Are you sure?") == true) {
- $(this).closest('tr').remove();
- if($('.sold_vehicles_entry_table tr:last .add_row input').length == 0){
- $(".sold_vehicles_entry_table tr:last .add_remove_icon:first").append("<input accesskey='+' name='button' style='width:30px;height: 31px;' type='button' value='+'>")
- }
- }
- });
- $(document.body).on('change', '.upload_docs', function(e){
- $(this).find('#document').addClass('file-loaded');
- });
- $(document.body).on('change', '.purchased_at', function(e){
- var today = new Date();
- var new_date = new Date($(this).val());
- date_diff = daydiff(today,new_date)
- if( date_diff > 0){
- $(this).addClass('future_date');
- $(this).removeClass('obsolete');
- }else if(date_diff < -19){
- $(this).addClass('obsolete');
- $(this).removeClass('future_date');
- }else{
- $(this).removeClass('obsolete');
- $(this).removeClass('future_date');
- }
- });
- $(document.body).on('submit', '.external_vehicles_load_form, .external_vehicles_form', function(e){
- show_warnings_before_submit(e);
- });
- $(document.body).on('click', '.save_row_button', function(e){
- if($(this).closest('form').find('.no_dealer').length >0){
- e.preventDefault();
- alert('Please correct dealer details');
- return false;
- }
- });
- $(document.body).on('change', '.vehicle_purchase_price, .buy_fee', function(e){
- var vehicle_purchase_price = parseInt($(this).closest('tr').find('.vehicle_purchase_price').val()) || 0;
- var buy_fee = parseInt($(this).closest('tr').find('.buy_fee').val()) || 0;
- $(this).closest('tr').find('.vehicle_total').val(vehicle_purchase_price + buy_fee);
- });
- $(document.body).on('change', '.vehicle_purchase_price', function(e){
- var vehicle_purchase_price = parseInt($(this).closest('span').find('.vehicle_purchase_price').val()) || 0;
- if ($(this).attr('id') != null) {
- splits = $(this).attr('id').split('_');
- the_id = splits[splits.length - 1]
- var buy_fee = parseInt($("#buy_fee_"+the_id).val())
- $("#vehicle_total_"+the_id).val(vehicle_purchase_price + buy_fee)
- }
- });
- $(document.body).on('change', '.buy_fee', function(e){
- var buy_fee = parseInt($(this).closest('span').find('.buy_fee').val()) || 0;
- if ($(this).attr('id') != null) {
- splits = $(this).attr('id').split('_');
- the_id = splits[splits.length - 1]
- var vehicle_purchase_price = parseInt($("#vehicle_purchase_price_"+the_id).val())
- $("#vehicle_total_"+the_id).val(vehicle_purchase_price + buy_fee)
- }
- });
- $(document.body).on('change', '.sold_vehicles_entry_table .vin', function(e){
- decode_vin(this, e);
- copy_data_to_clipboard_text($('.vin'))
- });
- $(document.body).on('change', '.sold_vehicles_entry_table .dealer_no', function(e){
- dealer_no = $(this).val();
- dealer_name = $(this).closest('tr').find('.dealer_name')
- decode_dealer_name(dealer_no, dealer_name, e);
- });
- $(document.body).on('change', '.sold_vehicles_approver_table .dealer_no', function(e){
- dealer_no = $(this).val();
- dealer_name = $(this).closest('.tr').find('.dealer_name')
- decode_dealer_name(dealer_no, dealer_name, e);
- });
- $(document.body).on('click', '.decode_vin' ,function(e){
- decode_vin(this, e);
- copy_data_to_clipboard_text($('.vin'));
- });
- $('body').on('focus',".sold_vehicles_entry_table .network-plus-datetimepicker", function(){
- $(this).datetimepicker({ format: 'm/d/y H:i' })
- });
- });
- decode_vin = function($that,e){
- e.preventDefault();
- vin = $($that).closest('tr').find('.vin').val().trim();
- if(vin==''){ return false}
- decode_url = "<%= url.decode_sold_vehicle_path('vin')%>"
- decode_url = decode_url.replace("vin", vin);
- $.ajax({
- url: decode_url,
- dataType: "json",
- cache: false
- }).complete(function(data, status) {
- if (status === 'success') {
- response = data.responseJSON;
- if (response['errors'] === void 0 || response['errors'] === '') {
- $($that).closest('tr').find('.modal_year_select').val(response['year']);
- $($that).closest('tr').find('.make').val(response['make']);
- $($that).closest('tr').find('.model').val(response['model']);
- } else {
- $($that).closest('tr').find('.error_message').html('Error fetching info').show().delay(2500).fadeOut('slow');
- }
- } else {
- $($that).closest('tr').find('.error_message').html('Error fetching info').show().delay(2500).fadeOut('slow');
- }
- });
- }
- decode_dealer_name = function(dealer_no, dealer_name, e){
- e.preventDefault();
- dealer_no = dealer_no.trim()
- if(dealer_no==''){ return false}
- decode_url = "<%= url.dealer_information_path('dealer_no')%>"
- decode_url = decode_url.replace("dealer_no", dealer_no);
- $.ajax({
- url: decode_url,
- dataType: "json",
- cache: false
- }).complete(function(data, status) {
- if (status === 'success') {
- response = data.responseJSON;
- if(response != null){
- dealer_name.val(response.dealership_name)
- dealer_name.removeClass('no_dealer')
- dealer_name.css("background-color", "white");
- }else{
- dealer_name.val('Dealer not found!!!')
- dealer_name.addClass('no_dealer')
- dealer_name.css("background-color", "red");
- }
- }
- });
- }
- populate_grid_row = function(){
- $(".sold_vehicles_entry_table tr:last").after(get_new_row());
- populate_location_initial_select();
- populate_modal_year();
- populate_sale_no_text();
- populate_inital_dates();
- }
- populate_values_from_preceding_row = function(preceding_row){
- var new_row = $(".sold_vehicles_entry_table tr:last");
- new_row.find('.dealer_no').val(preceding_row.find('.dealer_no').val());
- new_row.find('.buy_fee').val(preceding_row.find('.buy_fee').val());
- new_row.find('.location_initials').val(preceding_row.find('.location_initials').val());
- new_row.find('.purchased_at').val(preceding_row.find('.purchased_at').val());
- new_row.find('.automatic_purchase_email').val(preceding_row.find('.automatic_purchase_email').val());
- new_row.find('.dealer_name').val(preceding_row.find('.dealer_name').val());
- }
- populate_location_initial_select = function(){
- var location_initials = <%= FtbLocation.all %>
- $(".location_initials_select:not(.option_filled)").append("<option value> -- </option>")
- // var location_initials = ['FTB']
- $.each(location_initials, function(index) {
- $(".location_initials_select:not(.option_filled)").append(new Option(location_initials[index], location_initials[index]));
- });
- // $(".location_initials_select").addClass('option_filled')
- }
- populate_modal_year = function(){
- for (i = new Date().getFullYear(); i > 1996; i--)
- {
- $('.modal_year_select:not(.option_filled)').append($('<option />').val(i).html(i));
- }
- $(".modal_year_select").addClass('option_filled')
- }
- populate_sale_no_text = function(){
- sale_no = <%= Date.current.cweek %>
- $(".sale_no_text:not(.option_filled)").val(sale_no);
- $(".sale_no_text").addClass('option_filled')
- }
- populate_inital_dates = function(){
- var currentdate = new Date();
- var datetime = formated_datetime(currentdate);
- $(".purchased_at:not(.option_filled)").val(datetime);
- $(".purchased_at").addClass('option_filled')
- }
- initialize_select_2 = function(){
- $(".location_initials.select2").select2({ placeholder: "Enter Auction...", allowClear: true });
- }
- get_new_row = function(){
- return "<tr class='sold_vehicle_to_submit' id='adding_row' valign='top'><td><input class='vin' name='external_vehicles_load[][vin]' required type='text' pattern='^[a-zA-Z0-9]{17}$' title='VIN must be 17 characters alphanumeric'><span class='error_message alert_msg' style='display:none;'></span></td><td><select class='modal_year_select model_year' name='external_vehicles_load[][year]' required></select></td><td><input class='make' name='external_vehicles_load[][make]' required type='text'></td><td><input class='model' name='external_vehicles_load[][model]' type='text' required></td><td><input class='odometer_reading' name='external_vehicles_load[][odometer_reading]' type='number' required min='0' max='999999'></td><td><input class='dealer_no' name='external_vehicles_load[][dealer_no]' type='text' pattern='^5[0-9]{1,6}$' required></td><td><input class='dealer_name' type='text' value='' disabled/></td><td><input class='vehicle_purchase_price' name='external_vehicles_load[][vehicle_purchase_price]' required type='number' min='0' max='99999'></td><td><input class='buy_fee' name='external_vehicles_load[][buy_fee]' type='number' required min='0' max='9999'></td><td><input class='vehicle_total' name='external_vehicles_load[][vehicle_total]' type='number' min='0' max='999999'></td><td><select class='location_initials location_initials_select select2' name='external_vehicles_load[][location_initials]' data-placeholder= 'Choose a location...' required></select></td><td><input class='automatic_purchase_email' name='external_vehicles_load[][automatic_purchase_email]' type='email' multiple required></td><td><input class='network-plus-datetimepicker purchased_at' name='external_vehicles_load[][purchased_at]' required type='text' value=''></td><td><input class='seller_name' name='external_vehicles_load[][seller_name]' type='text'></td><td><span class='add_remove_icon add_row'><input accesskey='+' name='button' style='width:30px;height: 31px;' type='button' value='+'></span></td><td><span class='add_remove_icon delete_row'><input accesskey='-' name='button' style='width:30px;height: 31px;' type='button' value='-'></span></td></tr>"
- }
- parseDate = function(str) {
- var mdy = str.split('/');
- return new Date(mdy[2], mdy[0]-1, mdy[1]);
- }
- daydiff = function(first, second) {
- return Math.round((second-first)/(1000*60*60*24));
- }
Add Comment
Please, Sign In to add comment