setGoodsNum.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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="store_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="store_id" id="store_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="setStore()">确认选择</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:'operation'}">操作</th>
  62. </tr>
  63. </thead>
  64. <tbody id="goods">
  65. </tbody>
  66. </table>
  67. </div>
  68. <div class="total ft16">
  69. 合计:【<span> 当前总库存:<i class="totalNum">0</i> </span>】【<span> 新增总库存:<i class="totalQuantity">0</i> </span>】
  70. </div>
  71. <div class="towbtn">
  72. <input type="button" id="settlement" value="确认设置">
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. </div>
  78. </form>
  79. </div>
  80. <script>
  81. var form;
  82. var goods = {};
  83. var tree = {};
  84. layui.config(
  85. {
  86. base: '../script/lib/layui/admin/' //静态资源所在路径
  87. ,version: true
  88. }).extend(
  89. {
  90. index: 'lib/index', //主入口模块
  91. formSelects: 'plugin/formselects/formselects'
  92. }).use(['index', 'contlist', 'table', 'form', 'formSelects','layer'], function()
  93. {
  94. var table = layui.table;
  95. form = layui.form;
  96. var formSelects = layui.formSelects;
  97. mini.parse();
  98. tree = mini.get("tree1");
  99. for (var i in tree.data) {
  100. if (typeof(tree.data[i].children) == 'object') {
  101. if (tree.data[i].children.length > 0) {
  102. for (var j in tree.data[i].children) {
  103. if(tree.data[i].children[j].select == 1) {
  104. setGoods(tree.data[i].children[j]);
  105. }
  106. }
  107. } else if(tree.data[i].select == 1) {
  108. setGoods(tree.data[i]);
  109. }
  110. }
  111. }
  112. });
  113. $(function()
  114. {
  115. $("#settlement").click(function(){
  116. var url = '<{$submit}>';
  117. var store_id = '<{$store_id}>';
  118. layui.layer.confirm('确定进行此项设置吗?', function() {
  119. var goods_string = JSON.stringify(goods);
  120. var data = {};
  121. data = {store_id:store_id, goods:goods_string};
  122. $.post(url, data, function(t) {
  123. t = JSON.parse(t);
  124. if (t.status == 1) {
  125. layui.layer.alert('商品设置成功', function(index){
  126. location.href = t.data;
  127. layer.close(index);
  128. });
  129. } else {
  130. layui.layer.alert(t.msg);
  131. }
  132. });
  133. });
  134. })
  135. $("#goods").on("keyup", ".goods_num", function() {
  136. var num = parseFloat($(this).val());
  137. if (num < 0) {
  138. num = 0;
  139. $(this).val(0);
  140. }
  141. var id = $(this).parent().attr('value');
  142. goods[id].num = num;
  143. setTotal();
  144. });
  145. })
  146. function search()
  147. {
  148. var key = mini.get("key").getValue();
  149. if (key == "") {
  150. tree.clearFilter();
  151. } else {
  152. key = key.toLowerCase();
  153. tree.filter(function (node) {
  154. var name = node.name ? node.name.toLowerCase() : "";
  155. if (name.indexOf(key) != -1) {
  156. return true;
  157. }
  158. });
  159. }
  160. }
  161. function onKeyEnter(e)
  162. {
  163. search();
  164. }
  165. function onNodeClick()
  166. {
  167. var tree=mini.get("tree1");
  168. node=tree.getSelectedNode();
  169. if(node.end) {
  170. setGoods(node);
  171. }
  172. }
  173. function setVal(e)
  174. {
  175. mini.get(e).setValue('');
  176. }
  177. function searchGoods()
  178. {
  179. var key = mini.get("search_key").getValue();
  180. if (key == "") {
  181. $(".mytr").show();
  182. } else {
  183. key = key.toLowerCase();
  184. $(".mytr").show();
  185. $(".goods_name").each(function(t) {
  186. var name = $(this).html();
  187. name = name.toLowerCase();
  188. if (name.indexOf(key) == -1) {
  189. $(this).parent().hide();
  190. }
  191. });
  192. }
  193. }
  194. // 一键添加
  195. function addGoods()
  196. {
  197. var key = mini.get("key").getValue();
  198. if (key == '请输入商品名称') {
  199. key = '';
  200. }
  201. if (key) {
  202. key = key.toLowerCase();
  203. }
  204. for (var i in tree.data) {
  205. if (typeof(tree.data[i].children) == 'object') {
  206. if (tree.data[i].children.length > 0) {
  207. for (var j in tree.data[i].children) {
  208. if (key) {
  209. var name = tree.data[i].children[j].name;
  210. if (name.indexOf(key) != -1) {
  211. setGoods(tree.data[i].children[j]);
  212. }
  213. } else {
  214. setGoods(tree.data[i].children[j]);
  215. }
  216. }
  217. } else {
  218. if (key) {
  219. var name = tree.data[i].name;
  220. if (name.indexOf(key) != -1) {
  221. setGoods(tree.data[i]);
  222. }
  223. } else {
  224. setGoods(tree.data[i]);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. // 一键删除
  231. function delGoods()
  232. {
  233. if (confirm('确定删除吗?')) {
  234. var key = mini.get("key").getValue();
  235. if (key == '请输入商品名称') {
  236. key = '';
  237. }
  238. if (key) {
  239. key = key.toLowerCase();
  240. }
  241. for (var i in tree.data) {
  242. if (typeof(tree.data[i].children) == 'object') {
  243. if (tree.data[i].children.length > 0) {
  244. for (var j in tree.data[i].children) {
  245. if (key) {
  246. var name = tree.data[i].children[j].name;
  247. if (name.indexOf(key) != -1) {
  248. unsetGoods(tree.data[i].children[j]);
  249. }
  250. } else {
  251. unsetGoods(tree.data[i].children[j]);
  252. }
  253. }
  254. } else {
  255. if (key) {
  256. var name = tree.data[i].name;
  257. if (name.indexOf(key) != -1) {
  258. unsetGoods(tree.data[i]);
  259. }
  260. } else {
  261. unsetGoods(tree.data[i]);
  262. }
  263. }
  264. }
  265. }
  266. setTotal();
  267. }
  268. }
  269. function unsetGoods(node)
  270. {
  271. if (node.id) {
  272. if (goods[node.id]) {
  273. goods[node.id].del = 2;
  274. goods[node.id].num = 0;
  275. goods[node.id].total = 0;
  276. get(node.id).remove();
  277. }
  278. }
  279. }
  280. function setGoods(node)
  281. {
  282. if (node.id) {
  283. var key = node.id;
  284. if (!goods[key] || (goods[key] && goods[key].del == 2)) {
  285. goods[key] = node;
  286. goods[key].price = parseFloat(goods[key].price);
  287. goods[key].price_type = parseInt(goods[key].price_type);
  288. goods[key].total = parseFloat(goods[key].total);
  289. goods[key].num = 0;
  290. goods[key].del = 1;
  291. create(node);
  292. setTotal();
  293. } else if(node.price_type < 4) {
  294. add(key);
  295. }
  296. }
  297. }
  298. function get(key)
  299. {
  300. return $('#goods_' + key);
  301. }
  302. function create(node)
  303. {
  304. if (!get(node.id).length) {
  305. var select = '';
  306. /*
  307. if (node.price_type < 3) {
  308. select = '<select name="goods_price_template[]" class="goods_price_template" style="width: 120px;margin: 10px;" onchange="setPrice($(this), \''+node.id+'\')" lay-ignore>';
  309. var l = node.price_template.length;
  310. var i = 0;
  311. select += '<option value="0_'+node.price+'" selected>默认价格模板</option>';
  312. var selected = '';
  313. for (i = 0; i < l; i=i+1) {
  314. var id = node.price_template[i].id;
  315. var price = node.price_template[i].price_buy;
  316. if (node.price_template_sku && node.price_template_sku[id]) {
  317. price = node.price_template_sku[id].price_buy;
  318. }
  319. selected = '';
  320. if (node.price_template_id == id) {
  321. selected = 'selected';
  322. }
  323. select += '<option value="'+id+'_'+price+'" '+selected+'>'+node.price_template[i].name+'</option>';
  324. }
  325. select += '</select>';
  326. }*/
  327. var addtr = '<tr class="mytr" id="goods_'+node.id+'">';
  328. addtr += '<td class="goods_id">'+node.id+'</td>';
  329. addtr += '<td class="goods_name">'+node.name+'</td>';
  330. addtr += '<td class="goods_price"><span>'+node.price+'元</span>'+select+'</td>';
  331. addtr += '<td class="goods_price">'+node.total+'</td>';
  332. if (node.price_type == 4) {
  333. addtr += '<td>组合商品无法设置库存</td>';
  334. } else {
  335. 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>';
  336. }
  337. if (node.total <= 0) {
  338. addtr += '<td><a class="delete_btn" onclick="del(\''+node.id+'\')">删除</a></td>';
  339. } else {
  340. addtr += '<td></td>';
  341. }
  342. addtr += '</tr>';
  343. $("#goods").append(addtr);
  344. form.render();
  345. //$('.goods_price_template').change();
  346. }
  347. }
  348. function setPrice(e, id)
  349. {
  350. var val = e.val().split('_');
  351. goods[id].price_template_id = val[0];
  352. e.parent().find('span').html(val[1] + '元');
  353. }
  354. function add(id)
  355. {
  356. goods[id].num += 1;
  357. get(id).find('.goods_num').val(goods[id].num);
  358. setTotal();
  359. }
  360. function dec(id)
  361. {
  362. goods[id].num -= 1;
  363. if (goods[id].num < 1) {
  364. goods[id].num = 0;
  365. }
  366. get(id).find('.goods_num').val(goods[id].num);
  367. setTotal();
  368. }
  369. function del(id)
  370. {
  371. //if (confirm('确定删除吗?')) {
  372. goods[id].del = 2;
  373. goods[id].num = 0;
  374. get(id).remove();
  375. setTotal();
  376. //}
  377. }
  378. function setTotal()
  379. {
  380. var cash = 0;
  381. var num = 0;
  382. var total = 0;
  383. for (var i in goods) {
  384. num += goods[i].num;
  385. total += goods[i].total;
  386. }
  387. num = parseFloat(num).toFixed(2);
  388. total = parseFloat(total).toFixed(2);
  389. $(".totalQuantity").html(num);
  390. $(".totalNum").html(total);
  391. }
  392. function setStore()
  393. {
  394. var store_id = $('.xm-select-parent[fs_id="store_id"] span[fsw="xm-select"]').attr('value');
  395. if (!store_id) {
  396. layui.layer.alert('请选择仓库');
  397. return;
  398. }
  399. layui.layer.confirm('确定切换仓库吗?切换后现在选择的商品都将清空', function() {
  400. location.href = '<{$host}>&id=' + store_id;
  401. });
  402. }
  403. </script>
  404. </body>
  405. </html>