海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. $(function () {
  2. $('.has-many-spec .add.btn')
  3. .after('<div class="btn btn-spec spec-sync btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;同步供应商最新规格</div><div class="btn btn-spec batch-stock btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置库存</div><div class="btn btn-spec batch-original-price btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置市场价</div><div class="btn btn-spec batch-price btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置销售价</div>')
  4. .remove();
  5. //同步最新规格
  6. var template = $('template.spec-tpl').html();
  7. var is_load = false;
  8. $('.spec-sync').click(function () {
  9. if (is_load) {
  10. Dcat.confirm('本次载入将清除上次载入的所有数据,是否继续?', null, function () {
  11. Dcat.loading();
  12. post();
  13. });
  14. } else {
  15. Dcat.loading();
  16. post();
  17. is_load = true;
  18. }
  19. function post() {
  20. $.ajax({
  21. url: '`{{url}}`',
  22. method: 'POST',
  23. data: {
  24. product_id: $('input[name="product_id"]').val(),
  25. agent_product_id: `{{agent_product_id}}`,
  26. _form_: '`{{class}}`',
  27. },
  28. success: function (res) {
  29. var fields = ['supplier_name', 'supplier_date', 'supplier_price', 'supplier_stock', 'supplier_cost_price',
  30. 'price', 'stock', 'original_price', 'product_spec_id'];
  31. var data = res.data;
  32. var forms = $('.has-many-spec-forms');
  33. //只remove掉上次载入的,原来数据库保存的记录(.field_id不为空)不处理
  34. forms.children().each(function () {
  35. if (!$(this).find('.field_id').val()) {
  36. $(this).remove();
  37. }
  38. //清理供应商已删除的规格
  39. else if ($(this).find('.field_supplier_stock').val() == 0 && $(this).find('.field_supplier_price').val() == 0) {
  40. $(this).find('.remove.btn').click();
  41. }
  42. });
  43. for (var key in data) {
  44. var row = $(template.replace(/new___LA_KEY__/g, key));
  45. for (var i=0; i<fields.length; i++) {
  46. var field = fields[i];
  47. if (['original_price', 'price', 'stock'].indexOf(field) !== -1) {
  48. var value = data[key]['supplier_' + field], rule = '';
  49. //不处理市场价
  50. if (field === 'original_price') {
  51. rule = 'if(!this.value || isNaN(this.value) || parseFloat(this.value)<' + value + ') this.value=' + value;
  52. }
  53. row.find('input.field_' + field).val(value).attr({onblur: rule});
  54. } else {
  55. row.find('input.field_' + field).val(data[key][field]);
  56. }
  57. }
  58. forms.append(row);
  59. }
  60. Dcat.loading(false);
  61. },
  62. error: function () {
  63. Dcat.loading(false);
  64. Dcat.error('服务器出现未知错误,获取供应商产品规格失败');
  65. }
  66. });
  67. }
  68. });
  69. //批量库存
  70. $('.batch-stock').click(function () {
  71. var num = prompt('请输入你的库存(注:库存不能大于供应商库存)');
  72. if (!/^\d+$/.test(num)) {
  73. return;
  74. }
  75. $('.has-many-spec input.field_stock').each(function () {
  76. var value = $(this).parents('tr').find('.field_supplier_stock').val();
  77. var result = parseFloat(num) > parseFloat(value) ? value : num;
  78. $(this).val(result);
  79. });
  80. });
  81. //批量设置市场价
  82. $('.batch-original-price').click(function () {
  83. var num = prompt('请输入加价金额(在供应商销售价基础上加)');
  84. if (!/^\d+$/.test(num)) {
  85. return;
  86. }
  87. $('.has-many-spec input.field_original_price').each(function () {
  88. var value = $(this).parents('tr').find('.field_supplier_price').val();
  89. var result = parseFloat(value) + parseFloat(num);
  90. $(this).val(result.toFixed(2));
  91. });
  92. });
  93. //批量销售价
  94. $('.batch-price').click(function () {
  95. var num = prompt('请输入加价金额(在供应商销售价基础上加)');
  96. if (!/^\d+$/.test(num)) {
  97. return;
  98. }
  99. $('.has-many-spec input.field_price').each(function () {
  100. var value = $(this).parents('tr').find('.field_supplier_price').val();
  101. var result = parseFloat(value) + parseFloat(num);
  102. $(this).val(result.toFixed(2));
  103. });
  104. });
  105. });