海南旅游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.

230 lines
7.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. $(function () {
  2. $('.has-many-spec .add.btn').parent().attr('class', 'col-md-12')
  3. .after(`
  4. <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>
  5. <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>
  6. <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>
  7. <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>
  8. `);
  9. $('.has-many-spec_self .add.btn').parent().attr('class', 'col-md-12')
  10. .after(`
  11. <div class="btn btn-spec batch-add btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;</div>
  12. <div class="btn btn-spec batch-delete btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-minus"></i>&nbsp;</div>
  13. `);
  14. //切换类型
  15. $('input[name="type"]').change(function () {
  16. if ($(this).val() == 3) {
  17. $('#btn-group1').hide();
  18. $('#btn-group2').show();
  19. } else {
  20. $('#btn-group1').show();
  21. $('#btn-group2').hide();
  22. }
  23. $('.has-many-spec-forms').html('');
  24. $('.has-many-spec_self-form').html('');
  25. }).change();
  26. //批量新增
  27. $('.has-many-spec_self .batch-add').click(function () {
  28. var name = prompt('请输入规格名称');
  29. if (!name) {
  30. return;
  31. }
  32. var date = new Date();
  33. date = prompt('请输入起始日期', date.format('yyyy-MM-dd'));
  34. if (!date || isNaN(new Date(date).getTime())) {
  35. return;
  36. }
  37. var num = parseInt(prompt('请输入新增数量,最多不超过180', '30'));
  38. if (!num || isNaN(num)) {
  39. return;
  40. }
  41. if (num > 180) {
  42. num = 180;
  43. }
  44. var stock = parseInt(prompt('请输入默认库存', '9999'));
  45. if (!stock || isNaN(stock)) {
  46. return;
  47. }
  48. var original_price = parseFloat(prompt('请输入默认市场价'));
  49. if (!original_price || isNaN(original_price)) {
  50. return;
  51. }
  52. var price = parseFloat(prompt('请输入默认销售价'));
  53. if (!price || isNaN(price)) {
  54. return;
  55. }
  56. $('template.spec-tpl .field_name').prop('value', name);
  57. var html = $('template.spec_self-tpl').html();
  58. var fields = {
  59. 'name': name,
  60. 'date': date,
  61. 'stock': stock,
  62. 'original_price': original_price,
  63. 'price': price,
  64. };
  65. for (var key in fields) {
  66. html = html.replace('name="spec_self[new___LA_KEY__][' + key + ']" value=""', 'name="spec_self[new___LA_KEY__][' + key + ']" value="' + fields[key] + '"');
  67. }
  68. var nestedIndex = $('.has-many-spec-forms tr').length - 1;
  69. function replaceNestedFormIndex(value) {
  70. return String(value).replace(/__LA_KEY__/g, nestedIndex);
  71. }
  72. for (var i = 0; i < num; i++) {
  73. nestedIndex++;
  74. var d = new Date(date);
  75. d.setDate(d.getDate() + i);
  76. var date_template = replaceNestedFormIndex(html.replace(date, d.format('yyyy-MM-dd')));
  77. $('.has-many-spec_self-forms').append(date_template);
  78. }
  79. });
  80. //删除所有
  81. $('.has-many-spec_self .batch-delete').click(function () {
  82. $('.has-many-spec_self-forms .remove.btn').click();
  83. });
  84. //同步最新规格
  85. var template = $('template.spec-tpl').html();
  86. var is_load = false;
  87. $('.spec-sync').click(function () {
  88. if (is_load) {
  89. Dcat.confirm('本次载入将清除上次载入的所有数据,是否继续?', null, function () {
  90. Dcat.loading();
  91. post();
  92. });
  93. } else {
  94. Dcat.loading();
  95. post();
  96. is_load = true;
  97. }
  98. function post() {
  99. $.ajax({
  100. url: '`{{url}}`',
  101. method: 'POST',
  102. data: {
  103. product_id: $('input[name="product_id"]').val(),
  104. agent_product_id: `{{agent_product_id}}`,
  105. _form_: '`{{class}}`',
  106. },
  107. success: function (res) {
  108. var fields = ['supplier_name', 'supplier_date', 'supplier_price', 'supplier_stock', 'supplier_cost_price',
  109. 'price', 'stock', 'original_price', 'product_spec_id'];
  110. var data = res.data;
  111. var forms = $('.has-many-spec-forms');
  112. //只remove掉上次载入的,原来数据库保存的记录(.field_id不为空)不处理
  113. forms.children().each(function () {
  114. if (!$(this).find('.field_id').val()) {
  115. $(this).remove();
  116. }
  117. //清理供应商已删除的规格
  118. else if ($(this).find('.field_supplier_stock').val() == 0 && $(this).find('.field_supplier_price').val() == 0) {
  119. $(this).find('.remove.btn').click();
  120. }
  121. });
  122. for (var key in data) {
  123. var row = $(template.replace(/new___LA_KEY__/g, key));
  124. for (var i=0; i<fields.length; i++) {
  125. var field = fields[i];
  126. if (['original_price', 'price', 'stock'].indexOf(field) !== -1) {
  127. var value = data[key]['supplier_' + field], rule = '';
  128. //不处理市场价
  129. if (field === 'original_price') {
  130. rule = 'if(!this.value || isNaN(this.value) || parseFloat(this.value)<' + value + ') this.value=' + value;
  131. }
  132. row.find('input.field_' + field).val(value).attr({onblur: rule});
  133. } else {
  134. row.find('input.field_' + field).val(data[key][field]);
  135. }
  136. }
  137. forms.append(row);
  138. }
  139. Dcat.loading(false);
  140. },
  141. error: function () {
  142. Dcat.loading(false);
  143. Dcat.error('服务器出现未知错误,获取供应商产品规格失败');
  144. }
  145. });
  146. }
  147. });
  148. //批量库存
  149. $('.batch-stock').click(function () {
  150. var num = prompt('请输入你的库存(注:库存不能大于供应商库存)');
  151. if (!/^\d+$/.test(num)) {
  152. return;
  153. }
  154. $('.has-many-spec input.field_stock').each(function () {
  155. var value = $(this).parents('tr').find('.field_supplier_stock').val();
  156. var result = parseFloat(num) > parseFloat(value) ? value : num;
  157. $(this).val(result);
  158. });
  159. });
  160. //批量设置市场价
  161. $('.batch-original-price').click(function () {
  162. var num = prompt('请输入加价金额(在供应商销售价基础上加)');
  163. if (!/^\d+$/.test(num)) {
  164. return;
  165. }
  166. $('.has-many-spec input.field_original_price').each(function () {
  167. var value = $(this).parents('tr').find('.field_supplier_price').val();
  168. var result = parseFloat(value) + parseFloat(num);
  169. $(this).val(result.toFixed(2));
  170. });
  171. });
  172. //批量销售价
  173. $('.batch-price').click(function () {
  174. var num = prompt('请输入加价金额(在供应商销售价基础上加)');
  175. if (!/^\d+$/.test(num)) {
  176. return;
  177. }
  178. $('.has-many-spec input.field_price').each(function () {
  179. var value = $(this).parents('tr').find('.field_supplier_price').val();
  180. var result = parseFloat(value) + parseFloat(num);
  181. $(this).val(result.toFixed(2));
  182. });
  183. });
  184. });
  185. Date.prototype.format = function (fmt) {
  186. var o = {
  187. 'M+': this.getMonth() + 1, //月份
  188. 'd+': this.getDate(), //日
  189. 'h+': this.getHours(), //小时
  190. 'm+': this.getMinutes(), //分
  191. 's+': this.getSeconds(), //秒
  192. 'q+': Math.floor((this.getMonth() + 3) / 3), //季度
  193. 'S': this.getMilliseconds() //毫秒
  194. };
  195. if (/(y+)/.test(fmt)) {
  196. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  197. }
  198. for (var k in o) {
  199. if (new RegExp('(' + k + ')').test(fmt)) {
  200. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
  201. }
  202. }
  203. return fmt;
  204. };