Guest User

Untitled

a guest
Nov 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.71 KB | None | 0 0
  1. <% url = Rails.application.routes.url_helpers %>
  2. <% environment.context_class.instance_eval { include DateHelper } %>
  3.  
  4. $(document).ready(function(){
  5. var clipboard = new Clipboard('#clipboard');
  6. clipboard.on('success', function(e) {
  7. $('.clipboard_message').html('Copied!').show().delay(2500).fadeOut('slow');
  8. });
  9. initialize_select_2();
  10. // show spinner on AJAX start
  11. $(document).ajaxStart(function(){
  12. $(".js-loader").removeClass('l-hidden');
  13. });
  14.  
  15. // hide spinner on AJAX stop
  16. $(document).ajaxStop(function(){
  17. $(".js-loader").addClass('l-hidden');
  18. });
  19.  
  20. if($('.sold_vehicles_entry_table').length >0)
  21. {
  22. populate_grid_row();
  23. initialize_select_2();
  24. }
  25.  
  26. $(document.body).on('click', '.sold_vehicles_entry_table tr .add_row' ,function(){
  27. $('.sold_vehicles_entry_table tr:last .add_row input').remove()
  28. populate_grid_row();
  29. populate_values_from_preceding_row($(this).closest('tr'));
  30. initialize_select_2();
  31. });
  32.  
  33. $(document.body).on('click', '.sold_vehicles_entry_table tr .delete_row' ,function(){
  34. if($('.sold_vehicles_entry_table tr').length==2){
  35. alert("Minimum one row is needed!!!");
  36. return false;
  37. }
  38.  
  39. if(confirm("Are you sure?") == true) {
  40. $(this).closest('tr').remove();
  41. if($('.sold_vehicles_entry_table tr:last .add_row input').length == 0){
  42. $(".sold_vehicles_entry_table tr:last .add_remove_icon:first").append("<input accesskey='+' name='button' style='width:30px;height: 31px;' type='button' value='+'>")
  43. }
  44. }
  45. });
  46.  
  47. $(document.body).on('change', '.upload_docs', function(e){
  48. $(this).find('#document').addClass('file-loaded');
  49. });
  50.  
  51. $(document.body).on('change', '.purchased_at', function(e){
  52. var today = new Date();
  53. var new_date = new Date($(this).val());
  54. date_diff = daydiff(today,new_date)
  55. if( date_diff > 0){
  56. $(this).addClass('future_date');
  57. $(this).removeClass('obsolete');
  58. }else if(date_diff < -19){
  59. $(this).addClass('obsolete');
  60. $(this).removeClass('future_date');
  61. }else{
  62. $(this).removeClass('obsolete');
  63. $(this).removeClass('future_date');
  64. }
  65. });
  66.  
  67. $(document.body).on('submit', '.external_vehicles_load_form, .external_vehicles_form', function(e){
  68. show_warnings_before_submit(e);
  69. });
  70.  
  71. $(document.body).on('click', '.save_row_button', function(e){
  72. if($(this).closest('form').find('.no_dealer').length >0){
  73. e.preventDefault();
  74. alert('Please correct dealer details');
  75. return false;
  76. }
  77. });
  78.  
  79. $(document.body).on('change', '.vehicle_purchase_price, .buy_fee', function(e){
  80. var vehicle_purchase_price = parseInt($(this).closest('tr').find('.vehicle_purchase_price').val()) || 0;
  81. var buy_fee = parseInt($(this).closest('tr').find('.buy_fee').val()) || 0;
  82. $(this).closest('tr').find('.vehicle_total').val(vehicle_purchase_price + buy_fee);
  83. });
  84.  
  85. $(document.body).on('change', '.vehicle_purchase_price', function(e){
  86. var vehicle_purchase_price = parseInt($(this).closest('span').find('.vehicle_purchase_price').val()) || 0;
  87. if ($(this).attr('id') != null) {
  88. splits = $(this).attr('id').split('_');
  89. the_id = splits[splits.length - 1]
  90. var buy_fee = parseInt($("#buy_fee_"+the_id).val())
  91. $("#vehicle_total_"+the_id).val(vehicle_purchase_price + buy_fee)
  92. }
  93. });
  94.  
  95. $(document.body).on('change', '.buy_fee', function(e){
  96. var buy_fee = parseInt($(this).closest('span').find('.buy_fee').val()) || 0;
  97. if ($(this).attr('id') != null) {
  98. splits = $(this).attr('id').split('_');
  99. the_id = splits[splits.length - 1]
  100. var vehicle_purchase_price = parseInt($("#vehicle_purchase_price_"+the_id).val())
  101. $("#vehicle_total_"+the_id).val(vehicle_purchase_price + buy_fee)
  102. }
  103. });
  104.  
  105. $(document.body).on('change', '.sold_vehicles_entry_table .vin', function(e){
  106. decode_vin(this, e);
  107. copy_data_to_clipboard_text($('.vin'))
  108. });
  109.  
  110. $(document.body).on('change', '.sold_vehicles_entry_table .dealer_no', function(e){
  111. dealer_no = $(this).val();
  112. dealer_name = $(this).closest('tr').find('.dealer_name')
  113. decode_dealer_name(dealer_no, dealer_name, e);
  114. });
  115.  
  116. $(document.body).on('change', '.sold_vehicles_approver_table .dealer_no', function(e){
  117. dealer_no = $(this).val();
  118. dealer_name = $(this).closest('.tr').find('.dealer_name')
  119. decode_dealer_name(dealer_no, dealer_name, e);
  120. });
  121.  
  122. $(document.body).on('click', '.decode_vin' ,function(e){
  123. decode_vin(this, e);
  124. copy_data_to_clipboard_text($('.vin'));
  125. });
  126.  
  127. $('body').on('focus',".sold_vehicles_entry_table .network-plus-datetimepicker", function(){
  128. $(this).datetimepicker({ format: 'm/d/y H:i' })
  129. });
  130. });
  131.  
  132. decode_vin = function($that,e){
  133. e.preventDefault();
  134. vin = $($that).closest('tr').find('.vin').val().trim();
  135. if(vin==''){ return false}
  136.  
  137. decode_url = "<%= url.decode_sold_vehicle_path('vin')%>"
  138. decode_url = decode_url.replace("vin", vin);
  139.  
  140. $.ajax({
  141. url: decode_url,
  142. dataType: "json",
  143. cache: false
  144. }).complete(function(data, status) {
  145. if (status === 'success') {
  146. response = data.responseJSON;
  147. if (response['errors'] === void 0 || response['errors'] === '') {
  148. $($that).closest('tr').find('.modal_year_select').val(response['year']);
  149. $($that).closest('tr').find('.make').val(response['make']);
  150. $($that).closest('tr').find('.model').val(response['model']);
  151. } else {
  152. $($that).closest('tr').find('.error_message').html('Error fetching info').show().delay(2500).fadeOut('slow');
  153. }
  154. } else {
  155. $($that).closest('tr').find('.error_message').html('Error fetching info').show().delay(2500).fadeOut('slow');
  156. }
  157. });
  158. }
  159.  
  160. decode_dealer_name = function(dealer_no, dealer_name, e){
  161. e.preventDefault();
  162. dealer_no = dealer_no.trim()
  163. if(dealer_no==''){ return false}
  164.  
  165. decode_url = "<%= url.dealer_information_path('dealer_no')%>"
  166. decode_url = decode_url.replace("dealer_no", dealer_no);
  167.  
  168. $.ajax({
  169. url: decode_url,
  170. dataType: "json",
  171. cache: false
  172. }).complete(function(data, status) {
  173. if (status === 'success') {
  174. response = data.responseJSON;
  175. if(response != null){
  176. dealer_name.val(response.dealership_name)
  177. dealer_name.removeClass('no_dealer')
  178. dealer_name.css("background-color", "white");
  179. }else{
  180. dealer_name.val('Dealer not found!!!')
  181. dealer_name.addClass('no_dealer')
  182. dealer_name.css("background-color", "red");
  183. }
  184. }
  185. });
  186. }
  187.  
  188.  
  189. populate_grid_row = function(){
  190. $(".sold_vehicles_entry_table tr:last").after(get_new_row());
  191. populate_location_initial_select();
  192. populate_modal_year();
  193. populate_sale_no_text();
  194. populate_inital_dates();
  195. }
  196.  
  197. populate_values_from_preceding_row = function(preceding_row){
  198. var new_row = $(".sold_vehicles_entry_table tr:last");
  199. new_row.find('.dealer_no').val(preceding_row.find('.dealer_no').val());
  200. new_row.find('.buy_fee').val(preceding_row.find('.buy_fee').val());
  201. new_row.find('.location_initials').val(preceding_row.find('.location_initials').val());
  202. new_row.find('.purchased_at').val(preceding_row.find('.purchased_at').val());
  203. new_row.find('.automatic_purchase_email').val(preceding_row.find('.automatic_purchase_email').val());
  204. new_row.find('.dealer_name').val(preceding_row.find('.dealer_name').val());
  205. }
  206.  
  207. populate_location_initial_select = function(){
  208. var location_initials = <%= FtbLocation.all %>
  209. $(".location_initials_select:not(.option_filled)").append("<option value> -- </option>")
  210. // var location_initials = ['FTB']
  211. $.each(location_initials, function(index) {
  212. $(".location_initials_select:not(.option_filled)").append(new Option(location_initials[index], location_initials[index]));
  213. });
  214. // $(".location_initials_select").addClass('option_filled')
  215. }
  216.  
  217. populate_modal_year = function(){
  218. for (i = new Date().getFullYear(); i > 1996; i--)
  219. {
  220. $('.modal_year_select:not(.option_filled)').append($('<option />').val(i).html(i));
  221. }
  222. $(".modal_year_select").addClass('option_filled')
  223. }
  224.  
  225. populate_sale_no_text = function(){
  226. sale_no = <%= Date.current.cweek %>
  227. $(".sale_no_text:not(.option_filled)").val(sale_no);
  228. $(".sale_no_text").addClass('option_filled')
  229. }
  230.  
  231. populate_inital_dates = function(){
  232. var currentdate = new Date();
  233. var datetime = formated_datetime(currentdate);
  234.  
  235. $(".purchased_at:not(.option_filled)").val(datetime);
  236. $(".purchased_at").addClass('option_filled')
  237. }
  238.  
  239. initialize_select_2 = function(){
  240. $(".location_initials.select2").select2({ placeholder: "Enter Auction...", allowClear: true });
  241. }
  242.  
  243. get_new_row = function(){
  244. 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>"
  245. }
  246.  
  247. parseDate = function(str) {
  248. var mdy = str.split('/');
  249. return new Date(mdy[2], mdy[0]-1, mdy[1]);
  250. }
  251.  
  252. daydiff = function(first, second) {
  253. return Math.round((second-first)/(1000*60*60*24));
  254. }
Add Comment
Please, Sign In to add comment