setGoods.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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="factory_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="factory_id" id="factory_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="setFactory()">确认选择</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:'p_price', 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="totalMoney">0.00</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. layui.config(
  83. {
  84. base: '../script/lib/layui/admin/' //静态资源所在路径
  85. ,version: true
  86. }).extend(
  87. {
  88. index: 'lib/index', //主入口模块
  89. formSelects: 'plugin/formselects/formselects'
  90. }).use(['index', 'contlist', 'table', 'form', 'formSelects','layer'], function()
  91. {
  92. var table = layui.table;
  93. form = layui.form;
  94. var formSelects = layui.formSelects;
  95. });
  96. $(function()
  97. {
  98. $("#settlement").click(function(){
  99. var url = '<{$submit}>';
  100. var factory_id = '<{$factory_id}>';
  101. layui.layer.confirm('确定进行此项设置吗?', function() {
  102. var goods_string = JSON.stringify(goods);
  103. var data = {};
  104. data = {factory_id:factory_id, goods:goods_string};
  105. $.post(url, data, function(t) {
  106. t = JSON.parse(t);
  107. if (t.status == 1) {
  108. layui.layer.alert('商品设置成功', function(index){
  109. location.href = t.data;
  110. layer.close(index);
  111. });
  112. } else {
  113. layui.layer.alert(t.msg);
  114. }
  115. });
  116. });
  117. })
  118. /*
  119. $("#goods").on("keyup", ".goods_num", function() {
  120. var num = parseInt($(this).val());
  121. if (num < 0) {
  122. num = 1;
  123. $(this).val(1);
  124. }
  125. var id = $(this).parent().attr('value');
  126. goods[id].num = num;
  127. setTotal();
  128. });
  129. */
  130. $("#goods").on("keyup", ".goods_set_price", function() {
  131. var num = parseFloat($(this).val());
  132. var id = $(this).parent().attr('value');
  133. goods[id].p_price = num;
  134. setTotal();
  135. })
  136. })
  137. var goods = {};
  138. mini.parse();
  139. var tree = mini.get("tree1");
  140. for (var i in tree.data) {
  141. if (typeof(tree.data[i].children) == 'object') {
  142. if (tree.data[i].children.length > 0) {
  143. for (var j in tree.data[i].children) {
  144. if(tree.data[i].children[j].select == 1) {
  145. setGoods(tree.data[i].children[j]);
  146. }
  147. }
  148. } else if(tree.data[i].select == 1) {
  149. setGoods(tree.data[i]);
  150. }
  151. }
  152. }
  153. function search()
  154. {
  155. var key = mini.get("key").getValue();
  156. if (key == "") {
  157. tree.clearFilter();
  158. } else {
  159. key = key.toLowerCase();
  160. tree.filter(function (node) {
  161. var name = node.name ? node.name.toLowerCase() : "";
  162. if (name.indexOf(key) != -1) {
  163. return true;
  164. }
  165. });
  166. }
  167. }
  168. function onKeyEnter(e)
  169. {
  170. search();
  171. }
  172. function onNodeClick()
  173. {
  174. var tree=mini.get("tree1");
  175. node=tree.getSelectedNode();
  176. if(node.end) {
  177. setGoods(node);
  178. }
  179. }
  180. function setVal(e)
  181. {
  182. mini.get(e).setValue('');
  183. }
  184. function searchGoods()
  185. {
  186. var key = mini.get("search_key").getValue();
  187. if (key == "") {
  188. $(".mytr").show();
  189. } else {
  190. key = key.toLowerCase();
  191. $(".mytr").show();
  192. $(".goods_name").each(function(t) {
  193. var name = $(this).html();
  194. name = name.toLowerCase();
  195. if (name.indexOf(key) == -1) {
  196. $(this).parent().hide();
  197. }
  198. });
  199. }
  200. }
  201. // 一键添加
  202. function addGoods()
  203. {
  204. var key = mini.get("key").getValue();
  205. if (key == '请输入商品名称') {
  206. key = '';
  207. }
  208. if (key) {
  209. key = key.toLowerCase();
  210. }
  211. for (var i in tree.data) {
  212. if (typeof(tree.data[i].children) == 'object') {
  213. if (tree.data[i].children.length > 0) {
  214. for (var j in tree.data[i].children) {
  215. if (key) {
  216. var name = tree.data[i].children[j].name;
  217. if (name.indexOf(key) != -1) {
  218. setGoods(tree.data[i].children[j]);
  219. }
  220. } else {
  221. setGoods(tree.data[i].children[j]);
  222. }
  223. }
  224. } else {
  225. if (key) {
  226. var name = tree.data[i].name;
  227. if (name.indexOf(key) != -1) {
  228. setGoods(tree.data[i]);
  229. }
  230. } else {
  231. setGoods(tree.data[i]);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. // 一键删除
  238. function delGoods()
  239. {
  240. if (confirm('确定删除吗?')) {
  241. var key = mini.get("key").getValue();
  242. if (key == '请输入商品名称') {
  243. key = '';
  244. }
  245. if (key) {
  246. key = key.toLowerCase();
  247. }
  248. for (var i in tree.data) {
  249. if (typeof(tree.data[i].children) == 'object') {
  250. if (tree.data[i].children.length > 0) {
  251. for (var j in tree.data[i].children) {
  252. if (key) {
  253. var name = tree.data[i].children[j].name;
  254. if (name.indexOf(key) != -1) {
  255. unsetGoods(tree.data[i].children[j]);
  256. }
  257. } else {
  258. unsetGoods(tree.data[i].children[j]);
  259. }
  260. }
  261. } else {
  262. if (key) {
  263. var name = tree.data[i].name;
  264. if (name.indexOf(key) != -1) {
  265. unsetGoods(tree.data[i]);
  266. }
  267. } else {
  268. unsetGoods(tree.data[i]);
  269. }
  270. }
  271. }
  272. }
  273. setTotal();
  274. }
  275. }
  276. function unsetGoods(node)
  277. {
  278. if (node.id) {
  279. if (goods[node.id]) {
  280. delete goods[node.id];
  281. get(node.id).remove();
  282. }
  283. }
  284. }
  285. function setGoods(node)
  286. {
  287. if (node.id) {
  288. var key = node.id;
  289. if (!goods[key]) {
  290. goods[key] = node;
  291. goods[key].price = parseFloat(goods[key].price);
  292. goods[key].p_price = parseFloat(goods[key].p_price);
  293. goods[key].num = 1;
  294. create(node);
  295. setTotal();
  296. } else {
  297. //add(key);
  298. }
  299. }
  300. }
  301. function get(key)
  302. {
  303. return $('#goods_' + key);
  304. }
  305. function create(node)
  306. {
  307. if (!get(node.id).length) {
  308. var addtr = '<tr class="mytr" id="goods_'+node.id+'">';
  309. addtr += '<td class="goods_id">'+node.id+'</td>';
  310. addtr += '<td class="goods_name">'+node.name+'</td>';
  311. addtr += '<td class="goods_price">'+node.price+'</td>';
  312. addtr += '<td class="goods_p_price" value='+node.id+'><input type="tel" class="layui-input goods_set_price" value="'+node.p_price+'" /></td>';
  313. //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>';
  314. addtr += '<td><button class="delete_btn" onclick="del(\''+node.id+'\')">删除</button></td>';
  315. addtr += '</tr>';
  316. $("#goods").append(addtr);
  317. }
  318. }
  319. function add(id)
  320. {
  321. goods[id].num += 1;
  322. get(id).find('.goods_num').val(goods[id].num);
  323. setTotal();
  324. }
  325. function dec(id)
  326. {
  327. goods[id].num -= 1;
  328. if (goods[id].num < 1) {
  329. goods[id].num = 1;
  330. }
  331. get(id).find('.goods_num').val(goods[id].num);
  332. setTotal();
  333. }
  334. function del(id)
  335. {
  336. delete goods[id];
  337. get(id).remove();
  338. setTotal();
  339. }
  340. /*
  341. //点击 - 单价 - 可编辑
  342. $("#goods").on("click","td.danjia",function(){
  343. if(!$(this).is('.bj')){
  344. var currentPrice = $(this).html();
  345. $(this).focus();
  346. $(this).addClass('bj').html('<input type="tel" class="layui-input" value="'+$(this).text()+'" />').find('input').focus().blur();
  347. }
  348. })
  349. */
  350. function setTotal()
  351. {
  352. var cash = 0;
  353. var num = 0;
  354. for (var i in goods) {
  355. num += goods[i].num;
  356. cash += goods[i].p_price * goods[i].num;
  357. }
  358. $(".totalQuantity").html(num);
  359. $(".totalMoney").html(cash.toFixed(2));
  360. }
  361. function setFactory()
  362. {
  363. var factory_id = $('.xm-select-parent[fs_id="factory_id"] span[fsw="xm-select"]').attr('value');
  364. if (!factory_id) {
  365. layui.layer.alert('请选择工厂');
  366. return;
  367. }
  368. layui.layer.confirm('确定切换工厂吗?切换后现在选择的商品都将清空', function() {
  369. location.href = '<{$host}>&id=' + factory_id;
  370. });
  371. }
  372. </script>
  373. </body>
  374. </html>