function __veh_pickCustomerId(){ try { if (window.sale_items && window.sale_items.customer) { var c = toIntSafe(window.sale_items.customer); if (c) return c; } } catch(e){} var $sc = $('#searchbox_customers'); if ($sc.length) { var v = $sc.val(); var c2 = toIntSafe(v); if (!c2 && $sc.select2) { try { var d = $sc.select2('data'); if (d && d.length) c2 = toIntSafe(d[0].id); } catch(e){} } if (c2) return c2; } var $c = $('#customer'); if ($c.length) { var v2 = $c.val(); var c3 = toIntSafe(v2); if (!c3 && $c.select2) { try { var d2 = $c.select2('data'); if (d2 && d2.length) c3 = toIntSafe(d2[0].id); } catch(e){} } if (c3) return c3; } return 0; } function refreshVehicleHistory(v){ v = toIntSafe(v); var c = __veh_pickCustomerId(); // 0 allowed var $tb = $('#reading_history_table tbody'); var reqId = ++vehHistReqId; // take a token $tb.empty(); if (!v) { $('#prev_reading').text('—'); $('#prev_invoice').text('—'); $('#prev_date').text('—'); return; } // last 5 (with cache buster to avoid old results) $.getJSON("{% url 'sales:vehicle_reading_history' %}", { vehicle_id: v, customer_id: c, limit: 5, _ts: Date.now() }, function(r){ if (reqId !== vehHistReqId) return; // newer request already issued — ignore this var items = (r && r.items) ? r.items : []; if (!items.length){ $tb.append('No history found.'); return; } for (var i=0; i'+ ''+ (inv || '-') +''+ ''+ date +''+ ''+ rdg +''+ ''; $tb.append(row); } }); // single previous (for hints) $.getJSON("{% url 'sales:vehicle_last_reading' %}", { vehicle_id: v, customer_id: c, _ts: Date.now() }, function(r){ if (reqId !== vehHistReqId) return; $('#prev_reading').text((r && r.prev_reading!=null)? r.prev_reading : '—'); $('#prev_invoice').text((r && r.prev_invoice!=null)? r.prev_invoice : '—'); $('#prev_date').text((r && r.prev_date)? r.prev_date : '—'); }); } function loadCustomerVehicles(){ var cid = getCustomerId(); var st = getSaleType(); var $tbody = $('#cust_vehicle_table tbody'); var $hint = $('#custVehHint'); $tbody.empty(); // show only when Credit + customer selected if (!cid) { $hint.text('Select customer to see attached vehicles.').show(); return; } $hint.hide(); // customer mil gaya — list la do $.getJSON("{% url 'sales:options_customer_vehicles' %}", { customer_id: cid, limit: 300 }, function(resp){ var rows = (resp && resp.results) ? resp.results : []; if (!rows.length) { $tbody.append('No vehicles for this customer.'); return; } for (var i=0; i'+ safeText +''); $tbody.append(tr); } }); } $('#custVehSearch').on('input', function(){ var term = ($(this).val() || '').toLowerCase(); $('#cust_vehicle_table tbody tr').each(function(){ var txt = ($(this).text() || '').toLowerCase(); $(this).toggle(txt.indexOf(term) >= 0); }); }); $(document).on('click', '#cust_vehicle_table tbody tr.cust-veh-row', function(){ var id = toIntSafe($(this).attr('data-id') || 0); var label = $(this).attr('data-text') || ('ID ' + id); if (!id) return; var $sel = $('#vehicle_select'); // Set selected without firing another change handler var opt = new Option(label, id, true, true); $sel.append(opt).trigger('change.select2'); // select2 UI sync (fires change once) refreshVehicleHistory(id); // we still call once, guarded by reqId }); $(document).on('change', '#vehicle_select', function(){ var v = toIntSafe($(this).val() || 0); refreshVehicleHistory(v); }); $('#applyVehicle').on('click', function(){ var v = parseInt($('#vehicle_select').val() || 0); var cr = parseFloat($('#current_reading').val() || 0); if (!window.sale_items) window.sale_items = {}; window.sale_items.vehicle = v || 0; window.sale_items.vehicle_reading = isNaN(cr) ? 0 : cr; $('#veh_hidden_id').val(window.sale_items.vehicle); $('#veh_hidden_reading').val(window.sale_items.vehicle_reading); $('#vehicleModal').modal('hide'); }); $(document).on('change', '#searchbox_customers, #customer, #saletype, input[name="saletype"]', function(){ if ($('#vehicleModal').is(':visible')) loadCustomerVehicles(); }); $(document).on('select2:select select2:clear', '#searchbox_customers, #customer', function(){ if ($('#vehicleModal').is(':visible')) loadCustomerVehicles(); }); $(document).off('change', '#vehicle_select').on('change', '#vehicle_select', function(){ var v = toIntSafe($(this).val() || 0); refreshVehicleHistory(v); }); $(document).on('click', '#reading_history_table tbody tr.hist-row', function(){ var inv = toIntSafe($(this).data('invoice')); if (!inv) inv = toIntSafe($(this).find('td').eq(0).text()); if (!inv) return; window.open(INVOICE_URL_BASE + inv+"/view", '_blank'); }); var VEH_AJAX_URL = "{% url 'sales:options_vehicles' %}"; var VEH_BY_CUST = "{% url 'sales:options_customer_vehicles' %}"; var LAST_READING = "{% url 'sales:vehicle_last_reading' %}"; var HISTORY_URL = "{% url 'sales:vehicle_reading_history' %}"; var vehicleSelectInited = false; document.getElementById('btnVehicle').addEventListener('click', function(){ $('#vehicleModal').modal('show'); }); $('#vehicleModal').on('shown.bs.modal', function(){ var $sel = $('#vehicle_select'); if (!vehicleSelectInited) { $sel.select2({ ajax: { url: "{% url 'sales:options_vehicles' %}", dataType: 'json', delay: 250, data: function (params) { return { q: params.term || "" }; }, processResults: function (data) { return data; }, cache: true }, dropdownParent: $('#vehicleModal'), width: '100%', placeholder: 'Search vehicle', allowClear: true, minimumInputLength: 0 }); vehicleSelectInited = true; } // LEFT list refresh each open loadCustomerVehicles(); // auto-open + focus in the search box try { $sel.select2('open'); } catch(e) {} setTimeout(function(){ var $f = $('.select2-container--open .select2-search__field'); if ($f.length) $f.focus(); }, 0); });