_form.html.erb 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <%= form_for(@sale, :remote => true, :html => {:class=>"form-horizontal form-barcode", :id=> "sale_form"}) do |f| %>
  2. <div class="portlet-body form">
  3. <%= hidden_field_tag :barcode_for_sale %>
  4. <!-- este error explanation se usa para mostrar errores de presales e inventarios -->
  5. <!-- <div id="error_explanation"></div> -->
  6. <!-- este error explanation es cuando falla alguna validacion de la venta en sí -->
  7. <div class="alert alert-danger hidden" id="error_explanation_for_sale"></div>
  8. <div class="row">
  9. <!-- cuando no hay caja abierta -->
  10. <% if @opened_cash_registers.blank? %>
  11. <div class="alert alert-block alert-danger fade in">
  12. <h4 class="alert-heading">¡AVISO! no hay caja abierta</h4>
  13. <p> Para realizar una venta es necesario abrir una caja registradora. </p>
  14. <p>
  15. <%= link_to "Abrir caja", new_open_cash_register_path, :remote => true, :class => 'btn btn-primary' %>
  16. </p>
  17. </div>
  18. <% end %>
  19. <!-- boton para resetear datos -->
  20. <div class="col-md-offset-10 col-md-2">
  21. <button id="reset_pre_sales" type="button" class="btn btn-warning" <%= @disabled_button ? 'disabled' : '' %> onclick="deletePreSales()">Restaurar venta</button>
  22. </div>
  23. <div class="col-md-8">
  24. <h4 class="form-section"> Datos generales</h4>
  25. <!-- purchase code -->
  26. <div class="form-group">
  27. <%= f.label :sale_code, {:class=>"col-md-3 control-label"} do %> Código de compra
  28. <span class="required">*</span>
  29. <% end %>
  30. <div class="col-md-6 input-group">
  31. <span class="input-group-addon"><i class="fa fa-barcode"></i></span>
  32. <%= f.text_field :sale_code, {:class=>"form-control", :readonly => true} %>
  33. </div>
  34. </div>
  35. <!-- fecha -->
  36. <div class="form-group">
  37. <%= hidden_field_tag :tax_percent, @pos_config.tax_percent %>
  38. <%= f.label :date_sale, "Fecha", {:class=>"col-md-3 control-label"} do %> Fecha
  39. <span class="required">*</span>
  40. <% end %>
  41. <%= f.hidden_field :date_sale, {:value=>Date.today} %>
  42. <div class="col-sm-6" style="padding-left:0px;padding-right:0px;">
  43. <div class='input-group date' id='datetimepicker1'>
  44. <span class="input-group-addon">
  45. <span class="glyphicon glyphicon-calendar"></span>
  46. </span>
  47. <input type='text' class="form-control" disabled/>
  48. </div>
  49. </div>
  50. </div>
  51. <!-- open cash register -->
  52. <!-- < % if current_user.usertype == 'C' %>
  53. < %= f.hidden_field :open_cash_register_id, :value => @open_cash_register.id %>
  54. < % end %> -->
  55. <div class="form-group">
  56. <%= f.label :open_cash_register_id, "Caja registradora", {:class=>"col-md-3 control-label"} do %> Caja registradora
  57. <span class="required">*</span>
  58. <% end %>
  59. <div class="input-group col-md-6 select2-bootstrap-prepend">
  60. <% if @sale.open_cash_register.nil? %>
  61. <%= f.select :open_cash_register_id, Pointsale.find(current_user.pointsale_id).open_cash_registers.abiertas.map { |o| [o.cash_register.name, o.id] }, {:prompt => "Seleccione"}, { :class => 'form-control select2', :disabled => @disabled_select } %>
  62. <% else %>
  63. <%= f.select :open_cash_register_id, @opened_cash_registers.map{|o| [o.cash_register.name, o.id]}, {:include_blank => "Seleccione", :selected => @sale.open_cash_register.id}, {:class => "form-control", :disabled => true } %>
  64. <% end %>
  65. <%= f.hidden_field :open_cash_register_id, { :id => 'open_cash' } %>
  66. </div>
  67. </div>
  68. <!-- cliente -->
  69. <div class="form-group">
  70. <%= f.label :customer_id, "Cliente", {:class=>"col-md-3 control-label"} do %> Cliente
  71. <span class="required">*</span>
  72. <% end %>
  73. <div class="input-group col-md-6 select2-bootstrap-prepend" style="float:left">
  74. <%= f.collection_select :customer_id, Customer.vigentes, :id, :nick_name, { :prompt => "Seleccione", :selected => @sale.customer_id.present? ? @sale.customer_id : @general_public_id}, { :class => "form-control select2" } %>
  75. <%= f.hidden_field :customer_id, :value => @sale.customer_id.present? ? @sale.customer_id : @general_public_id, :id => 'customer' %>
  76. </div>
  77. <div class="col-md-2">
  78. <% if @pre_sales.count == 0 %>
  79. <%= link_to new_customer_path, remote: true, class: "btn btn-success", id: :customer_remote do %>
  80. <i class="fa fa-plus"></i>
  81. <% end %>
  82. <% end %>
  83. </div>
  84. </div>
  85. <!-- tipo de venta -->
  86. <div class="form-group">
  87. <%= f.label :saletype, {:class=>"col-md-3 control-label"} do %> ¿Tipo de venta?
  88. <span class="required">*</span><% end %>
  89. <div class="btn-group col-md-6" data-toggle="buttons" style="padding-left:0px;padding-right:0px">
  90. <label class="col-md-4 btn default <%= @sale.saletype == 'cash' ? 'active' : (@enable_radios ? '' : 'disabled') %>">
  91. <%= radio_button_tag 'types', "cash", (@sale.saletype == 'cash'), class: "toggle" %> Contado
  92. </label>
  93. <label class="col-md-4 btn default <%= @sale.saletype == 'credit' ? 'active' : (@enable_radios ? '' : 'disabled') %>">
  94. <%= radio_button_tag 'types', "credit", (@sale.saletype == 'credit'), class: "toggle" %> Crédito
  95. </label>
  96. <label class="col-md-4 btn default <%= @sale.saletype == 'reserved' ? 'active' : (@enable_radios ? '' : 'disabled') %>">
  97. <%= radio_button_tag 'types', "reserved", (@sale.saletype == 'reserved'), class: "toggle" %> Apartado
  98. </label>
  99. <%= f.hidden_field :saletype %>
  100. </div>
  101. </div>
  102. <!-- vale -->
  103. <div class="form-group hidden" id="credit_note_div">
  104. <%= f.label :credit_note, {:class=>"col-md-3 control-label"} do %> Folio de vale <% end %>
  105. <div class="col-md-6 input-group">
  106. <span class="input-group-addon"><i class="fa fa-ticket"></i></span>
  107. <%= f.text_field :credit_note, {:class=>"form-control"} %>
  108. </div>
  109. </div>
  110. <!-- vendedor -->
  111. <div class="form-group">
  112. <%= f.label :seller_id, "Vendedor", {:class=>"col-md-3 control-label"} do %> Vendedor
  113. <span class="required">*</span>
  114. <% end %>
  115. <div class="input-group col-md-6 select2-bootstrap-prepend">
  116. <%= f.collection_select :seller_id, Seller.where(pointsale_id: current_user.pointsale_id, status: '1'), :id, :full_name, { prompt: "Seleccione", selected: @seller.blank? ? '' : @seller.to_i }, { class: "form-control select2" } %>
  117. </div>
  118. </div>
  119. </div>
  120. <div class="col-md-4 hidden" id="customer_info">
  121. <h4 class="form-section"> Datos del cliente</h4>
  122. <div class="panel panel-info">
  123. <!-- Default panel contents -->
  124. <div class="panel-heading">
  125. <h3 class="panel-title" id="customer_name"></h3>
  126. </div>
  127. <!-- List group -->
  128. <ul class="list-group">
  129. <li class="list-group-item"> ¿Tiene crédito?
  130. <span class="badge badge-info badge-roundless" id="has_credit"></span>
  131. </li>
  132. <li class="list-group-item"> Adeudo del cliente
  133. <span class="badge badge-info badge-roundless" id="customer_debiting"></span>
  134. </li>
  135. <%= hidden_field_tag :available_credit, 0 %>
  136. <li class="list-group-item"> Límite de crédito
  137. <span class="badge badge-info badge-roundless" id="credit_limit"></span>
  138. </li>
  139. <li class="list-group-item"> Fecha de último abono
  140. <span class="badge badge-info badge-roundless" id="last_credit_payment"></span>
  141. </li>
  142. </ul>
  143. </div>
  144. <div class="from-group">
  145. <div id="error_explanation"></div>
  146. </div>
  147. </div>
  148. </div>
  149. <!-- agregar productos -->
  150. <h4 class="form-section"> Agregar producto</h4>
  151. <div class="row">
  152. <div class="col-md-12">
  153. <div class="note note-info">
  154. <h4 class="block">¡Nota!</h4>
  155. <p>
  156. Para hacer una búsqueda avanzada utiliza el siguiente formato: <br>
  157. Nombre del producto:(inicial del atributo a buscar)atributo a buscar<br>
  158. Ejemplo: <strong>Blusa:cverde:tch:elargo</strong> (Donde C es Color, T es Talla y E es Estilo, pueden tener cualquier orden, siempre y cuando tenga ese formato)
  159. </p>
  160. </div>
  161. </div>
  162. <div class='col-md-12'>
  163. <div class="form-group">
  164. <label class="col-md-2 control-label" for="typeahead"> Producto
  165. <span class="required">*</span>
  166. </label>
  167. <div class="col-md-4">
  168. <!-- -->
  169. <input class="form-control" type="text" id="typeahead">
  170. </div>
  171. <button id="products_new" disabled class="btn btn-success" type="button" onclick="addRow()"> Agregar <i class="fa fa-plus"></i> </button>
  172. </div>
  173. </div>
  174. </div>
  175. <!-- lista de productos -->
  176. <h4 class="form-section"> Lista de productos</h4>
  177. <div class="portlet-body">
  178. <table class="table table-striped table-bordered table-hover tableadvanced" id="products_table">
  179. <thead>
  180. <tr>
  181. <th>#</th>
  182. <th>SKU</th>
  183. <th>Imagen</th>
  184. <th>Producto</th>
  185. <th>Cantidad</th>
  186. <th>Precio unitario</th>
  187. <th>IVA</th>
  188. <th>Descuento</th>
  189. <th>Importe</th>
  190. <th width="15%">Acciones</th>
  191. </tr>
  192. </thead>
  193. <tbody>
  194. <%= render @pre_sales %>
  195. </tbody>
  196. </table>
  197. </div>
  198. <!-- totales -->
  199. <div class="row">
  200. <div class="col-md-offset-8 col-md-4">
  201. <div class="well">
  202. <div class="row static-info align-reverse">
  203. <div class="col-md-6 name"> Sub Total: </div>
  204. <div class="col-md-6 value ">
  205. <div class="input-group">
  206. <span class="input-group-addon">$ </span>
  207. <%= f.text_field :amount, {:class=>"form-control sub_total", :readonly => true, :id => "amount"} %>
  208. </div>
  209. </div>
  210. </div>
  211. <div class="row static-info align-reverse">
  212. <div class="col-md-6 name"> Descuento: </div>
  213. <div class="col-md-6 value ">
  214. <div class="input-group">
  215. <span class="input-group-addon">$ </span>
  216. <%= f.text_field :discount, {:class=>"form-control sub_total", :readonly => true, :id => "discount"} %>
  217. </div>
  218. </div>
  219. </div>
  220. <div class="row static-info align-reverse">
  221. <div class="col-md-6 name"> IVA: </div>
  222. <div class="col-md-6 value ">
  223. <div class="input-group">
  224. <span class="input-group-addon">$ </span>
  225. <%= f.text_field :tax, {:class=>"form-control descto", :readonly => true, :id => "tax"} %>
  226. </div>
  227. </div>
  228. </div>
  229. <div class="row static-info align-reverse">
  230. <div class="col-md-6 name"> Total: </div>
  231. <div class="col-md-6 value ">
  232. <div class="input-group">
  233. <span class="input-group-addon">$ </span>
  234. <%= f.text_field :total, {:class=>"form-control descto", :readonly => true, :id => "total"} %>
  235. </div>
  236. </div>
  237. </div>
  238. </div>
  239. </div>
  240. </div>
  241. <!-- acciones del form -->
  242. <div class="form-actions">
  243. <div class="row">
  244. <div class="col-md-9">
  245. <button type="button" class="btn green" onclick="addSale()" id="save_sale">Guardar venta</button>
  246. <%= link_to 'Cancelar', sales_path(:filter => @filter, :current_page => @current_page), {:class=>"btn default"} %>
  247. </div>
  248. </div>
  249. </div>
  250. </div>
  251. <% end %>
  252. <script type="text/javascript">
  253. var selectedProduct;
  254. var timeout = null;
  255. $(document).on("page:change", function() {
  256. // App.init();
  257. calculateTotals();
  258. fillCustomerInfo();
  259. enumeratePreSales();
  260. generateSaleCode();
  261. // si es publico en general, desactivar venta a credito y apartado
  262. if ($('#sale_customer_id').val() == '<%= @general_public_id %>') {
  263. $('#types_credit').attr('disabled', true);
  264. $('#types_credit').closest('label').addClass('disabled');
  265. $('#types_reserved').attr('disabled', true);
  266. $('#types_reserved').closest('label').addClass('disabled');
  267. }
  268. $('#datetimepicker1').datetimepicker({
  269. icons: {
  270. date: "fa fa-calendar"
  271. },
  272. format: "DD/MM/YYYY",
  273. defaultDate: new Date()
  274. });
  275. $("#new_pre_sale").bind('ajax:complete', function() {
  276. calculateTotals();
  277. });
  278. $('form#sale_form').bind('ajax:complete', function() {
  279. $('#save_sale').attr('disabled', false);
  280. });
  281. $('#sale_open_cash_register_id').on('change', function() {
  282. $(this).attr('disabled', true);
  283. $('#open_cash').val($(this).val());
  284. generateSaleCode();
  285. });
  286. $('input:radio[name=types]').change(function() {
  287. if (!$(this).closest('label').hasClass('disabled')) {
  288. <% if @pre_sales.count == 0 %>
  289. if (!$(this).closest('label').hasClass('disabled')) {
  290. $('#sale_saletype').val($(this).val());
  291. } else {
  292. var type = $('#sale_saletype').val();
  293. $(":radio[value=" + type + "]").closest('label').addClass('active');
  294. }
  295. <% else %>
  296. var type = '<%= @sale.saletype %>';
  297. $(":radio[value=" + type + "]").closest('label').addClass('active');
  298. <% end %>
  299. //Si es tipo crédito, aparecer input de vale
  300. if ($(this).val() == 'credit') {
  301. $('#credit_note_div').removeClass('hidden');
  302. } else {
  303. $('#credit_note_div').addClass('hidden');
  304. $('#sale_credit_note').val('');
  305. }
  306. }
  307. });
  308. $(document).on("keydown", function (e) {
  309. if (window.location.pathname == "/sales/new") {
  310. if (e.which === 8 && !$(e.target).is("input, textarea")) {
  311. e.preventDefault();
  312. }
  313. }
  314. });
  315. $(document).scannerDetection({
  316. timeBeforeScanTest: 200, // wait for the next character for upto 200ms
  317. startChar: [120], // Prefix character for the cabled scanner (OPL6845R)
  318. endChar: [13], // be sure the scan is complete if key 13 (enter) is detected
  319. avgTimeByChar: 40, // it's not a barcode if a character takes longer than 40ms
  320. onComplete: function(barcode, qty){ findProductByBarcode(barcode) } // main callback function
  321. });
  322. });
  323. $('#sale_customer_id').on('change', function() {
  324. // $(this).attr('disabled', true);
  325. $('#customer').val($(this).val());
  326. //bloquear boton para agregar nuevo cliente ya que se selecciono o agrego uno
  327. $('#customer_remote').remove();
  328. // $('#customer_remote').attr("disabled", true);
  329. // activar apartado y el credito se activa dependiendo si tiene o no,
  330. // en la funcion fillCustomer
  331. if ($('#sale_customer_id').val() != '<%= @general_public_id %>') {
  332. $('#types_reserved').attr('disabled', false);
  333. $('#types_reserved').closest('label').removeClass('disabled');
  334. }
  335. updatePreSale($(this).val());
  336. });
  337. $('#types_cash, #types_credit, #types_reserved').on('change', function() {
  338. $("#sale_saletype").val($(this).val());
  339. if($(this).val() == "cash"){
  340. $("#save_sale").attr("disabled", false);
  341. }
  342. $.ajax({
  343. type: "PUT",
  344. url: "/update_saletype/" + $(this).val(),
  345. dataType: "json",
  346. success: function(xhr, status, error) {
  347. toastr["warning"]("Se actualizó el tipo de venta");
  348. },
  349. error: function (xhr, status, errorThrown) {
  350. toastr["error"](xhr.responseText.substr(3, (xhr.responseText.length -6)));
  351. }
  352. });
  353. });
  354. function updatePreSale(customer_id) {
  355. $.ajax({
  356. type: "PUT",
  357. url: "/update_presale_customer/" + customer_id,
  358. dataType: "json",
  359. success: function(xhr, status, error) {
  360. toastr["warning"]("Se actualizó el cliente de venta");
  361. fillCustomerInfo();
  362. }
  363. });
  364. }
  365. // initialize bloodhound engine
  366. var bloodhound = new Bloodhound({
  367. datumTokenizer: function (d) {
  368. return Bloodhound.tokenizers.whitespace(d.value);
  369. },
  370. queryTokenizer: Bloodhound.tokenizers.whitespace,
  371. remote: {
  372. url: '/find_products_from_stock?query=%QUERY',
  373. wildcard: '%QUERY'
  374. }
  375. });
  376. bloodhound.initialize();
  377. $('#typeahead').typeahead(
  378. {
  379. minLength: 3
  380. },
  381. {
  382. displayKey: 'name',
  383. source: bloodhound.ttAdapter(),
  384. limit: Infinity,
  385. templates: {
  386. empty: [
  387. '<div class="empty-message">',
  388. 'No se encontró ningun producto. Favor de verificar',
  389. '</div>'
  390. ].join('\n'),
  391. suggestion: Handlebars.compile(
  392. '<div class="media">' +
  393. '<div class="pull-left">' +
  394. '<div class="media-object">' +
  395. '<img src="{{small_img}}" width="50" height="50"/>' +
  396. '</div>' +
  397. '</div>' +
  398. '<div class="media-body">' +
  399. '<h4 class="media-heading"><strong>{{sku}}</strong> | {{name}}</h4>' +
  400. '<p>{{barcode}}</p>' +
  401. '<p>{{description}}</p>' +
  402. '<p>{{display_attributes}}</p>' +
  403. '</div>' +
  404. '</div>')
  405. }
  406. });
  407. // this is the event that is fired when a user clicks on a suggestion
  408. $('#typeahead').bind('typeahead:selected', function(event, datum, name) {
  409. selectedProduct = datum;
  410. var openCash = $('#sale_open_cash_register_id').val();
  411. var customer = $('#sale_customer_id').val();
  412. var saleType = $('#sale_saletype').val();
  413. var seller = $('#sale_seller_id').val();
  414. if (openCash && customer && saleType && seller ) {
  415. $('#products_new').attr('disabled', false);
  416. } else {
  417. toastr["error"]("No ha ingresado toda la información requerida.");
  418. }
  419. });
  420. function addRow() {
  421. if(selectedProduct) {
  422. $('#pre_sale_customer_id').val($('#sale_customer_id').val());
  423. $('#pre_sale_open_cash_register_id').val($('#sale_open_cash_register_id').val());
  424. $('#pre_sale_sale_type').val($('#sale_saletype').val());
  425. $('#pre_sale_product_id').val(selectedProduct.id);
  426. $('#pre_sale_quantity').val(1);
  427. $('#new_pre_sale').submit();
  428. $('#typeahead').typeahead('val','');
  429. $('#products_new').attr('disabled', true);
  430. }
  431. }
  432. function deleteRow(input) {
  433. var table = $('#products_table').DataTable();
  434. var idText = input.closest('tr').attr('id');
  435. var preSaleId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
  436. $.ajax({
  437. type: "DELETE",
  438. url: "/pre_sales/" + preSaleId,
  439. dataType: "json",
  440. data: "",
  441. success: function(xhr, status, error) {
  442. table.row(input.closest('tr')).remove().draw();
  443. calculateTotals();
  444. }
  445. });
  446. }
  447. function calculatePrice(input) {
  448. if(input.val()) {
  449. clearTimeout(timeout);
  450. timeout = setTimeout(function () {
  451. tax = 0;
  452. quantity = parseInt(input.val());
  453. var idText = input.closest('tr').attr('id');
  454. var preSaleId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
  455. $('#save_sale').attr('disabled', true);
  456. $.ajax({
  457. type: "PUT",
  458. url: "/pre_sales/" + preSaleId,
  459. dataType: "json",
  460. data: { pre_sale: { quantity: input.val() } },
  461. success: function(xhr, status, error) {
  462. input.closest('tr').find('td:eq(6)').text(Math.round(xhr.tax * 100) / 100);
  463. input.closest('tr').find('td:eq(7)').text(Math.round(xhr.discount * 100) / 100);
  464. input.closest('tr').find('td:eq(8)').text(Math.round(xhr.amount * 100) / 100);
  465. calculateTotals();
  466. $('#save_sale').attr('disabled', false);
  467. },
  468. error: function (xhr, status, errorThrown) {
  469. input.val(1);
  470. toastr["error"](xhr.responseText.substr(3, (xhr.responseText.length -6)));
  471. }
  472. });
  473. }, 500);
  474. }
  475. }
  476. function calculateTotals() {
  477. var amount = 0;
  478. var discount = 0;
  479. var tax = 0;
  480. var total = 0;
  481. if($('#products_table').dataTable().fnGetData().length > 0) {
  482. $('#products_table tbody tr').each(function() {
  483. // quantity = parseFloat(($(this).find('td:nth-child(5) input').val()));
  484. // price = parseFloat($(this).find('td:nth-child(6)').text());
  485. // amount += (quantity * price);
  486. amount += parseFloat($(this).find('td:nth-child(9)').text()); // importe
  487. });
  488. }
  489. $('#products_table tbody tr td:nth-child(7)').each(function() {
  490. tax += parseFloat($(this).text());
  491. });
  492. $('#products_table tbody tr td:nth-child(8)').each(function() {
  493. discount += parseFloat($(this).text());
  494. });
  495. total = amount + tax;
  496. $('#tax').val(Math.round(tax * 100) / 100);
  497. $('#discount').val(Math.round(discount * 100) / 100);
  498. $('#amount').val(Math.round(amount * 100) / 100);
  499. $('#total').val(Math.round(total * 100) / 100);
  500. if(total < parseFloat($("#available_credit").val())) {
  501. $('#types_credit').attr('disabled', false);
  502. $('#types_credit').closest('label').removeClass('disabled');
  503. }
  504. }
  505. function deletePreSales() {
  506. $.ajax({
  507. type: "get",
  508. url: '/delete_pre_sales',
  509. dataType: 'json',
  510. success: function(xhr, status, error) {
  511. location.reload();
  512. }
  513. });
  514. }
  515. function addSale() {
  516. var $form = $('#sale_form');
  517. var table = $('#products_table').dataTable();
  518. if ($form[0].checkValidity()) {
  519. if(table.fnGetData().length > 0) {
  520. $('#sale_form').submit();
  521. $('#save_sale').attr('disabled', true);
  522. } else {
  523. toastr["error"]("Error, Se debe agregar al menos un producto.");
  524. }
  525. } else {
  526. toastr["error"]("La cantidad de los productos debe de ser mayor o igual a 1.");
  527. }
  528. }
  529. function fillCustomerInfo() {
  530. var customerId = $('#customer').val();
  531. var public_customer = "<%= @general_public_id %>";
  532. if(customerId) {
  533. $.ajax({
  534. type: "get",
  535. url: '/customers/' + customerId + ".json",
  536. dataType: 'json',
  537. success: function(data) {
  538. if(data.credit) {
  539. $('#customer_name').text(data.nick_name);
  540. $('#has_credit').text(data.credit ? 'SI' : 'NO');
  541. // $('#customer_debiting').text(formatter.format(data.customer_debiting.customer_debiting));
  542. // $('#credit_limit').text(formatter.format(data.credit_limit));
  543. $('#customer_debiting').text(accounting.formatMoney(data.customer_debiting.customer_debiting));
  544. $('#credit_limit').text(accounting.formatMoney(data.credit_limit));
  545. if(data.customer_payment) {
  546. $('#last_credit_payment').text(data.customer_payment.customer_payment);
  547. }
  548. $('#customer_info').removeClass('hidden');
  549. $("#available_credit").val(data.available_credit.available_credit)
  550. if(parseInt($("#total").val()) > parseInt(data.available_credit.available_credit)) {
  551. $('#types_cash').click();
  552. $('#types_credit').attr('disabled', true);
  553. $('#types_credit').closest('label').addClass('disabled');
  554. } else {
  555. $('#types_credit').attr('disabled', false);
  556. $('#types_credit').closest('label').removeClass('disabled');
  557. }
  558. } else {
  559. // DISABLEAR EL DE CREDITO
  560. $("#customer_info").addClass("hidden");
  561. $('#types_cash').click();
  562. $('#types_credit').attr('disabled', true);
  563. $('#types_credit').closest('label').addClass('disabled');
  564. }
  565. }
  566. });
  567. }
  568. }
  569. function enumeratePreSales() {
  570. if($('#reset_pre_sales').prop("disabled") == false) {
  571. var table = $('#products_table').dataTable();
  572. var counter = 1;
  573. $('#products_table tbody tr').each(function() {
  574. $(this).find('td:eq(0)').html($(this).find('td:eq(0)').html().replace('#', counter));
  575. counter++;
  576. });
  577. }
  578. }
  579. function generateSaleCode() {
  580. if($('#sale_open_cash_register_id').val()) {
  581. $.ajax({
  582. type: "get",
  583. url: '/get_next_sale_code/' + $('#sale_open_cash_register_id').val(),
  584. dataType: 'text',
  585. success: function(data) {
  586. $('#sale_sale_code').val(data);
  587. },
  588. });
  589. }
  590. }
  591. function findProductByBarcode(barcode) {
  592. var customer = $('#sale_customer_id').val();
  593. var openCash = $('#sale_open_cash_register_id').val();
  594. var saleType = $('form input[type=radio]:checked').val();
  595. var seller = $('#sale_seller_id').val();
  596. if(customer && openCash && saleType && seller) {
  597. $.ajax({
  598. type: "get",
  599. url: '/add_pre_sale_by_barcode/' + barcode + "/" + customer + "/" + openCash + "/" + saleType,
  600. dataType: 'script',
  601. success: function(data) {
  602. calculateTotals();
  603. },
  604. });
  605. } else {
  606. toastr["error"]("Falta seleccionar datos del formulario.");
  607. }
  608. }
  609. </script>