Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. // Copyright (c) 2016, Rootstack and contributors
  2. // For license information, please see license.txt
  3.  
  4. (function() {
  5. var reasons = [];
  6. var edited_item_qtys = [];
  7. frappe.ui.form.on("reinventario", "refresh", function(frm) {
  8. toggleButtonVisivility(frm);
  9. getProductList(frm);
  10. frm.set_value('author', frm.doc.owner);
  11. reasons = frm.fields_dict.reason_list.df.options.split( "\n" );
  12. });
  13.  
  14. frappe.ui.form.on("reinventario", "before_submit", function(frm) {
  15. frm.set_value("reinventario_status", "Aprobado");
  16. })
  17.  
  18. frappe.ui.form.on("reinventario", "before_save", function(frm) {
  19. console.log(frm);
  20. validate = false;
  21. })
  22.  
  23. frappe.ui.form.on("reinventario", "after_save", function(frm) {
  24. frm.assign_to.add();
  25. frm.assign_to.dialog.hide();
  26. frm.assign_to.dialog.set_values({assign_to:frm.doc.quien_aprueba});
  27. frm.assign_to.dialog.primary_action();
  28. })
  29.  
  30. frappe.ui.form.on("reinventario", "reinventario_status", function(frm) {
  31. toggleButtonVisivility(frm);
  32. });
  33.  
  34. frappe.ui.form.on("reinventario", "warehouse", function(frm) {
  35. getProductList(frm);
  36. });
  37.  
  38. frappe.ui.form.on("reinventario", "revert_button", function(frm) {
  39. clearTable();
  40. });
  41.  
  42. function toggleButtonVisivility(frm) {
  43. if(frm.doc.reinventario_status == "Aprobado") {
  44. $(frm.fields_dict['revert_button'].wrapper).hide();
  45. } else {
  46. $(frm.fields_dict['revert_button'].wrapper).show();
  47. }
  48. }
  49.  
  50. function clearTable() {
  51. $('.productos-body').html('');
  52. }
  53.  
  54. function getProductList(frm) {
  55.  
  56. frm.call({
  57. method: 'erpnext.stock.doctype.stock_reconciliation.stock_reconciliation.get_items',
  58. args: {
  59. warehouse:frm.doc.warehouse || "",
  60. posting_date: getDate(),
  61. posting_time: "23:59:59"
  62. },
  63. callback: function(r) {
  64. console.log(r)
  65. edited_item_qtys = [];
  66. $('.productos-body').html('');
  67. $.each(r.message, function(index) {
  68. var item = r.message[index];
  69. item.parsed_id = item.item_code.replace(' ', '_').toLowerCase();
  70. //appendTableRow(frm, item);
  71. var resource = getDetails(frm, r.message[index]);
  72. resource.success(function(a) {
  73. console.log('a',a)
  74. item = $.extend(item, a.message);
  75. appendTableRow(frm, item);
  76. });
  77. });
  78. }
  79. })
  80. }
  81.  
  82. function getDate() {
  83. var today = new Date();
  84. var dd = today.getDate();
  85. var mm = today.getMonth()+1; //January is 0!
  86. var yyyy = today.getFullYear();
  87.  
  88. if(dd<10) {
  89. dd='0'+dd
  90. }
  91.  
  92. if(mm<10) {
  93. mm='0'+mm
  94. }
  95.  
  96. return mm+'-'+dd+'-'+yyyy;
  97. }
  98.  
  99. function getDetails(frm, item) {
  100. if(item.item_code) {
  101. return frm.call({
  102. method: "erpnext.stock.get_item_details.get_available_qty",
  103. args: {
  104. item_code: item.item_code,
  105. warehouse: item.warehouse
  106. }
  107. });
  108. }
  109. }
  110.  
  111. function addTmpRecord(frm, item) {
  112. var tmp_obj = {
  113. id : item.parsed_id,
  114. reinventario_id : frm.doc.name,
  115. wharehouse : item.warehouse,
  116. item_code : item.item_code,
  117. prev_qty : item.actual_qty || 0,
  118. next_qty : item.qty || 0,
  119. reason : item.reason | ""
  120. };
  121. edited_item_qtys.push(tmp_obj);
  122. return tmp_obj;
  123. }
  124.  
  125. function createReasonSelect(item) {
  126. var reason_select = $("<select class=\"form-control reason-select\" " +
  127. "id=\""+item.parsed_id+"_reason\" />");
  128.  
  129. for(var r_index in reasons) {
  130. $("<option />", {value: reasons[r_index], text: reasons[r_index]}).appendTo(reason_select);
  131. }
  132. reason_select.appendTo("#tempselect");
  133. return $("#tempselect").html();
  134. }
  135.  
  136. function createNumberFieldQty(item) {
  137. return '<input ' +
  138. 'id="'+item.parsed_id+'_qty" ' +
  139. 'type="number"' +
  140. 'min="0"' +
  141. 'class="form-control"' +
  142. 'value="' + item.actual_qty + '" ' +
  143. '></input>';
  144. }
  145.  
  146. function appendTableRow(frm, item) {
  147. console.log(item);
  148. $('.productos-body').append(
  149. '<tr><td>' + item.item_code + '</td>' +
  150. '<td class="producto-cantidad-actual">' + item.actual_qty + '</td>' +
  151. '<td> ' + createNumberFieldQty(item) + '</td>' +
  152. '<td> ' + createReasonSelect(item) + '</td></tr>'
  153. );
  154.  
  155. $("#tempselect").html('');
  156.  
  157. var tmp_item = addTmpRecord(frm, item);
  158.  
  159. $("input#" + item.parsed_id+ "_qty" ).on("change", function() {
  160. var el = $(this);
  161. if(el.val() > 0) {
  162. tmp_item.qty = parseInt(el.val(), 10);
  163. console.log(el.val(), tmp_item, edited_item_qtys);
  164. }
  165. });
  166. }
  167.  
  168.  
  169. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement