setGoods.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0" />
  6. <meta name="author" content="siweiyong 2602812659@qq.com"/>
  7. <title>设置商品</title>
  8. <link rel="stylesheet" href="../script/lib/miniui/themes/default/miniui.css">
  9. <link rel="stylesheet" href="../script/lib/layui/css/layui.css" />
  10. <link rel="stylesheet" href="../script/lib/cashier/common.css">
  11. <link rel="stylesheet" href="../script/lib/layui/admin/modules/plugin/formselects/formselects.css" media="all" />
  12. <script src="../script/lib/jquery/jquery.min.js"></script>
  13. <script src="../script/lib/miniui/miniui.js"></script>
  14. <script src="../script/lib/layui/layui.js"></script>
  15. <script><{Dever::script()}></script>
  16. <script src="../script/dever/core.js"></script>
  17. </head>
  18. <body style="background: #F6F7F9;">
  19. <div class="main">
  20. <form class="layui-form" lay-filter="form">
  21. <div class="layui-row">
  22. <div class="main_left layui-col-xs12 layui-col-md3">
  23. <div class="layui-row">
  24. <div class="top">
  25. <i class="layui-icon layui-icon-app"></i>商品列表
  26. </div>
  27. <div class="left_main">
  28. <input id="key" class="mini-textbox" onenter="onKeyEnter" value="请输入商品名称" onclick="setVal('key')"/>
  29. <a class="mini-button" onclick="search()">查询</a>
  30. <button class="layui-btn layui-btn-button" type="button" style="vertical-align: middle;margin-bottom: 2px;" onclick="addGoods()">一键添加</button>
  31. <button class="layui-btn layui-btn-button" type="button" style="vertical-align: middle;margin-bottom: 2px;" onclick="delGoods()">一键删除</button>
  32. <ul id="tree1" class="mini-tree" url="<{$url}>" showTreeIcon="true" textField="name" idField="id" expandOnLoad="true" onNodeClick="onNodeClick">
  33. </ul>
  34. </div>
  35. </div>
  36. </div>
  37. <div class="main_right layui-col-xs12 layui-col-md9">
  38. <div class="layui-row">
  39. <div class="top">
  40. <label>选择门店:</label>
  41. <div class="layui-input-inline">
  42. <div><select xm-select="shop_id" xm-select-skin="normal" xm-select-search="<{$search}>" xm-select-search-type="dl" xm-select-radio="" class="update_value form-control layui-input layui-select" name="shop_id" id="shop_id" ></select></div>
  43. </div>
  44. <div class="layui-input-inline">
  45. <button class="layui-btn layui-btn-button" type="button" style="vertical-align: middle;margin-bottom: 2px;" onclick="setShop()">确认选择</button>
  46. </div>
  47. <div class="layui-input-inline" style="margin-left:20px">
  48. <input id="search_key" class="mini-textbox" value="请输入商品名称" onclick="setVal('search_key')"/>
  49. <a class="mini-button" onclick="searchGoods()">搜索</a>
  50. </div>
  51. </div>
  52. <div class="goods_list" style="height:645px;">
  53. <table class="layui-table">
  54. <thead>
  55. <tr>
  56. <th lay-data="{field:'id'}">商品编码</th>
  57. <th lay-data="{field:'name'}">商品名称</th>
  58. <th lay-data="{field:'price', edit: 'text'}">销售价(元)</th>
  59. <th lay-data="{field:'total', edit: 'text'}">当前库存</th>
  60. <th lay-data="{field:'number', edit: 'text'}">新增库存</th>
  61. <th lay-data="{field:'number', edit: 'text'}">起订数</th>
  62. <th lay-data="{field:'operation'}">操作</th>
  63. </tr>
  64. </thead>
  65. <tbody id="goods">
  66. </tbody>
  67. </table>
  68. </div>
  69. <div class="total ft16">
  70. 合计:【<span> 当前总库存:<i class="totalNum">0</i> </span>】【<span> 新增总库存:<i class="totalQuantity">0</i> </span>】
  71. </div>
  72. <div class="towbtn">
  73. <input type="button" id="settlement" value="确认设置">
  74. </div>
  75. </div>
  76. </div>
  77. </div>
  78. </div>
  79. </form>
  80. </div>
  81. <script>
  82. var form;
  83. var goods = {};
  84. var del_goods = {};
  85. var tree = {};
  86. layui.config(
  87. {
  88. base: '../script/lib/layui/admin/' //静态资源所在路径
  89. ,version: true
  90. }).extend(
  91. {
  92. index: 'lib/index', //主入口模块
  93. formSelects: 'plugin/formselects/formselects'
  94. }).use(['index', 'contlist', 'table', 'form', 'formSelects','layer'], function()
  95. {
  96. var table = layui.table;
  97. form = layui.form;
  98. var formSelects = layui.formSelects;
  99. mini.parse();
  100. tree = mini.get("tree1");
  101. for (var i in tree.data) {
  102. if (typeof(tree.data[i].children) == 'object') {
  103. if (tree.data[i].children.length > 0) {
  104. for (var j in tree.data[i].children) {
  105. if(tree.data[i].children[j].select == 1) {
  106. setGoods(tree.data[i].children[j]);
  107. }
  108. }
  109. } else if(tree.data[i].select == 1) {
  110. setGoods(tree.data[i]);
  111. }
  112. }
  113. }
  114. });
  115. $(function()
  116. {
  117. $("#settlement").click(function(){
  118. var url = '<{$submit}>';
  119. var shop_id = '<{$shop_id}>';
  120. layui.layer.confirm('确定进行此项设置吗?', function() {
  121. var goods_string = JSON.stringify(goods);
  122. var data = {};
  123. data = {shop_id:shop_id, goods:goods_string};
  124. $.post(url, data, function(t) {
  125. t = JSON.parse(t);
  126. if (t.status == 1) {
  127. layui.layer.alert('商品设置成功', function(index){
  128. if (t.data == 'refer') {
  129. history.back();
  130. } else {
  131. location.href = t.data;
  132. }
  133. layer.close(index);
  134. });
  135. } else {
  136. layui.layer.alert(t.msg);
  137. }
  138. });
  139. });
  140. })
  141. $("#goods").on("keyup", ".goods_num", function() {
  142. var num = parseFloat($(this).val());
  143. if (num < 0) {
  144. num = 0;
  145. $(this).val(0);
  146. }
  147. var id = $(this).parent().attr('value');
  148. goods[id].num = num;
  149. setTotal();
  150. });
  151. $("#goods").on("keyup", ".goods_min", function() {
  152. var min = parseFloat($(this).val());
  153. if (min < 0) {
  154. min = 1;
  155. $(this).val(1);
  156. }
  157. var id = $(this).parent().attr('value');
  158. goods[id].min = min;
  159. setTotal();
  160. });
  161. })
  162. function search()
  163. {
  164. var key = mini.get("key").getValue();
  165. if (key == "") {
  166. tree.clearFilter();
  167. } else {
  168. key = key.toLowerCase();
  169. tree.filter(function (node) {
  170. var name = node.name ? node.name.toLowerCase() : "";
  171. if (name.indexOf(key) != -1) {
  172. return true;
  173. }
  174. });
  175. }
  176. }
  177. function onKeyEnter(e)
  178. {
  179. search();
  180. }
  181. function onNodeClick()
  182. {
  183. var tree=mini.get("tree1");
  184. node=tree.getSelectedNode();
  185. if(node.end) {
  186. setGoods(node);
  187. }
  188. }
  189. function setVal(e)
  190. {
  191. mini.get(e).setValue('');
  192. }
  193. function searchGoods()
  194. {
  195. var key = mini.get("search_key").getValue();
  196. if (key == "") {
  197. $(".mytr").show();
  198. } else {
  199. key = key.toLowerCase();
  200. $(".mytr").show();
  201. $(".goods_name").each(function(t) {
  202. var name = $(this).html();
  203. name = name.toLowerCase();
  204. if (name.indexOf(key) == -1) {
  205. $(this).parent().hide();
  206. }
  207. });
  208. }
  209. }
  210. // 一键添加
  211. function addGoods()
  212. {
  213. var key = mini.get("key").getValue();
  214. if (key == '请输入商品名称') {
  215. key = '';
  216. }
  217. if (key) {
  218. key = key.toLowerCase();
  219. }
  220. for (var i in tree.data) {
  221. if (typeof(tree.data[i].children) == 'object') {
  222. if (tree.data[i].children.length > 0) {
  223. for (var j in tree.data[i].children) {
  224. if (key) {
  225. var name = tree.data[i].children[j].name;
  226. if (name.indexOf(key) != -1) {
  227. setGoods(tree.data[i].children[j]);
  228. }
  229. } else {
  230. setGoods(tree.data[i].children[j]);
  231. }
  232. }
  233. } else {
  234. if (key) {
  235. var name = tree.data[i].name;
  236. if (name.indexOf(key) != -1) {
  237. setGoods(tree.data[i]);
  238. }
  239. } else {
  240. setGoods(tree.data[i]);
  241. }
  242. }
  243. }
  244. }
  245. }
  246. // 一键删除
  247. function delGoods()
  248. {
  249. if (confirm('确定删除吗?')) {
  250. var key = mini.get("key").getValue();
  251. if (key == '请输入商品名称') {
  252. key = '';
  253. }
  254. if (key) {
  255. key = key.toLowerCase();
  256. }
  257. for (var i in tree.data) {
  258. if (typeof(tree.data[i].children) == 'object') {
  259. if (tree.data[i].children.length > 0) {
  260. for (var j in tree.data[i].children) {
  261. if (key) {
  262. var name = tree.data[i].children[j].name;
  263. if (name.indexOf(key) != -1) {
  264. unsetGoods(tree.data[i].children[j]);
  265. }
  266. } else {
  267. unsetGoods(tree.data[i].children[j]);
  268. }
  269. }
  270. } else {
  271. if (key) {
  272. var name = tree.data[i].name;
  273. if (name.indexOf(key) != -1) {
  274. unsetGoods(tree.data[i]);
  275. }
  276. } else {
  277. unsetGoods(tree.data[i]);
  278. }
  279. }
  280. }
  281. }
  282. setTotal();
  283. }
  284. }
  285. function unsetGoods(node)
  286. {
  287. if (node.id) {
  288. if (goods[node.id]) {
  289. goods[node.id].del = 2;
  290. goods[node.id].num = 0;
  291. goods[node.id].total = 0;
  292. get(node.id).remove();
  293. }
  294. }
  295. }
  296. function setGoods(node)
  297. {
  298. if (node.id) {
  299. var key = node.id;
  300. if (!goods[key] || (goods[key] && goods[key].del == 2)) {
  301. goods[key] = node;
  302. goods[key].price = parseFloat(goods[key].price);
  303. goods[key].price_type = parseInt(goods[key].price_type);
  304. goods[key].total = parseFloat(goods[key].total);
  305. goods[key].min = goods[key].min ? parseFloat(goods[key].min) : 1;
  306. goods[key].num = 0;
  307. goods[key].del = 1;
  308. create(node);
  309. setTotal();
  310. } else if(node.price_type < 4) {
  311. add(key);
  312. }
  313. }
  314. }
  315. function get(key)
  316. {
  317. return $('#goods_' + key);
  318. }
  319. function create(node)
  320. {
  321. if (!get(node.id).length) {
  322. var select = '';
  323. if (node.price_type < 3) {
  324. select = '<select name="goods_price_template[]" class="goods_price_template" style="width: 120px;margin: 10px;" onchange="setPrice($(this), \''+node.id+'\')" lay-ignore>';
  325. var l = node.price_template.length;
  326. var i = 0;
  327. select += '<option value="0_'+node.price+'_1" selected>默认价格模板</option>';
  328. var selected = '';
  329. for (i = 0; i < l; i=i+1) {
  330. var id = node.price_template[i].id;
  331. var price = node.price_template[i].price_sell;
  332. var price_num = node.price_template[i].price_num;
  333. if (node.price_template_sku && node.price_template_sku[id]) {
  334. price = node.price_template_sku[id].price_sell;
  335. price_num = node.price_template_sku[id].price_num;
  336. }
  337. if (price == 'null' || price == null || !price) {
  338. price = node.price;
  339. }
  340. selected = '';
  341. if (node.price_template_id == id) {
  342. selected = 'selected';
  343. }
  344. select += '<option value="'+id+'_'+price+'_'+price_num+'" '+selected+'>'+node.price_template[i].name+'</option>';
  345. }
  346. select += '</select>';
  347. }
  348. var addtr = '<tr class="mytr" id="goods_'+node.id+'">';
  349. addtr += '<td class="goods_id">'+node.id+'</td>';
  350. addtr += '<td class="goods_name">'+node.name+'</td>';
  351. addtr += '<td class="goods_price"><span>'+node.price+'元</span>'+select+'</td>';
  352. addtr += '<td class="goods_total">'+node.total+'</td>';
  353. if (node.price_type == 4) {
  354. addtr += '<td>组合商品无法设置库存</td>';
  355. } else {
  356. addtr += '<td><div class="jiajian" value='+node.id+'><span class="jian" onclick="dec(\''+node.id+'\')">-</span><input type="text" value="'+node.num+'" class="goods_num"><span class="jia" onclick="add(\''+node.id+'\')">+</span></div></td>';
  357. }
  358. addtr += '<td><div class="jiajian" value='+node.id+'><span class="jian" onclick="decMin(\''+node.id+'\')">-</span><input type="text" value="'+node.min+'" class="goods_min"><span class="jia" onclick="addMin(\''+node.id+'\')">+</span></div></td>';
  359. var shop_id = '<{$shop_id}>';
  360. if (node.total <= 0 || shop_id == 3) {
  361. addtr += '<td><a class="delete_btn" onclick="del(\''+node.id+'\')">删除</a></td>';
  362. } else {
  363. addtr += '<td></td>';
  364. }
  365. addtr += '</tr>';
  366. $("#goods").append(addtr);
  367. form.render();
  368. $('.goods_price_template').change();
  369. }
  370. }
  371. function setPrice(e, id)
  372. {
  373. var val = e.val().split('_');
  374. goods[id].price_template_id = val[0];
  375. e.parent().find('span').html(val[1] + '元');
  376. //goods[id].min = val[2];
  377. //e.parent().parent().find('.goods_min').val(val[2]);
  378. }
  379. function add(id)
  380. {
  381. goods[id].num += 1;
  382. get(id).find('.goods_num').val(goods[id].num);
  383. setTotal();
  384. }
  385. function dec(id)
  386. {
  387. goods[id].num -= 1;
  388. if (goods[id].num < 1) {
  389. goods[id].num = 0;
  390. }
  391. get(id).find('.goods_num').val(goods[id].num);
  392. setTotal();
  393. }
  394. function addMin(id)
  395. {
  396. goods[id].min += 1;
  397. get(id).find('.goods_min').val(goods[id].min);
  398. setTotal();
  399. }
  400. function decMin(id)
  401. {
  402. goods[id].min -= 1;
  403. if (goods[id].min < 1) {
  404. goods[id].min = 0;
  405. }
  406. get(id).find('.goods_min').val(goods[id].min);
  407. setTotal();
  408. }
  409. function del(id)
  410. {
  411. //if (confirm('确定删除吗?')) {
  412. goods[id].del = 2;
  413. goods[id].num = 0;
  414. get(id).remove();
  415. setTotal();
  416. //}
  417. }
  418. function setTotal()
  419. {
  420. var cash = 0;
  421. var num = 0;
  422. var total = 0;
  423. for (var i in goods) {
  424. num += goods[i].num;
  425. total += goods[i].total;
  426. }
  427. $(".totalQuantity").html(num);
  428. $(".totalNum").html(total);
  429. }
  430. function setShop()
  431. {
  432. var shop_id = $('.xm-select-parent[fs_id="shop_id"] span[fsw="xm-select"]').attr('value');
  433. if (!shop_id) {
  434. layui.layer.alert('请选择门店');
  435. return;
  436. }
  437. layui.layer.confirm('确定切换门店吗?切换后现在选择的商品都将清空', function() {
  438. location.href = '<{$host}>&id=' + shop_id;
  439. });
  440. }
  441. </script>
  442. </body>
  443. </html>