stockin.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <base-page>
  3. <view class="content-wrap" @click="goodsShow = false">
  4. <view class="title">添加入库单</view>
  5. <view class="table-wrap">
  6. <view class="table-head">
  7. <view class="table-tr">
  8. <view class="table-th" style="flex: 3;">产品名称/规格/编码</view>
  9. <view class="table-th" style="flex: 1;">当前库存</view>
  10. <view class="table-th" style="flex: 1;">单位</view>
  11. <view class="table-th" style="flex: 2;">成本价</view>
  12. <view class="table-th" style="flex: 2;">数量</view>
  13. <view class="table-th" style="flex: 1;">总金额</view>
  14. <view class="table-th" style="flex: 1;">操作</view>
  15. </view>
  16. </view>
  17. <view class="table-body">
  18. <view class="table-tr">
  19. <view class="table-td select-goods-input" style="flex: 3;" @click.stop="goodsShow = true">
  20. <input type="text" v-model="name" @confirm="getGoodsData" placeholder="请输入产品名称/规格/编码" />
  21. <scroll-view class="select-goods-frame" :scroll-y="true" v-show="goodsShow">
  22. <view :class="['goods-item', { select: selectGoodsId == item.sku_id }]" v-for="(item, index) in searchGoodsList" @click.stop="selectGoods(item)">
  23. {{ item.sku_name }}
  24. </view>
  25. <view class="goods-item-empty" v-if="!searchGoodsList.length">暂无商品</view>
  26. </scroll-view>
  27. </view>
  28. <view class="table-td" style="flex: 1;"></view>
  29. <view class="table-td" style="flex: 1;"></view>
  30. <view class="table-td" style="flex: 2;"></view>
  31. <view class="table-td" style="flex: 2;"></view>
  32. <view class="table-td" style="flex: 1;"></view>
  33. <view class="table-td" style="flex: 1;"></view>
  34. </view>
  35. <block v-for="(item, index) in goodsList" :key="index">
  36. <view class="table-tr" v-if="goodsIdArr.includes(item.sku_id)">
  37. <view class="table-td goods-name" style="flex: 3;">
  38. <image :src="$util.img(item.sku_image, { size: 'small' })" mode="aspectFill"></image>
  39. <text class="name multi-hidden">{{ item.sku_name }}</text>
  40. </view>
  41. <view class="table-td" style="flex: 1;">{{ item.real_stock }}</view>
  42. <view class="table-td" style="flex: 1;">{{ item.unit }}</view>
  43. <view class="table-td" style="flex: 2;">
  44. <input type="number" class="goods-num" v-model="item.cost_price" placeholder="请输入成本价" @input="calcTotalData" />
  45. </view>
  46. <view class="table-td" style="flex: 2;">
  47. <input type="number" class="goods-num" v-model="item.goods_num" placeholder="请输入数量" @input="calcTotalData" />
  48. </view>
  49. <view class="table-td" style="flex: 1;">{{ (item.goods_num * item.cost_price || 0).toFixed(2) }}</view>
  50. <view class="table-td" style="flex: 1;"><button type="default" class="delete" @click="delGoods(item.sku_id)">删除</button></view>
  51. </view>
  52. </block>
  53. <view class="table-tr table-empty" v-if="!goodsIdArr.length">暂无数据,请选择商品数据</view>
  54. </view>
  55. </view>
  56. <view class="action-wrap">
  57. <view class="table-total">合计:共{{ totalData.kindsNum }}种{{ totalData.countNum }}{{ totalData.unit }}产品,合计金额{{ totalData.price.toFixed(2) }}</view>
  58. <view class="btn-wrap">
  59. <button type="default" class="stockout-btn" @click="stockOutFn">入库</button>
  60. <button type="default" @click="backFn">返回</button>
  61. </view>
  62. </view>
  63. </view>
  64. </base-page>
  65. </template>
  66. <script>
  67. export default {
  68. data() {
  69. return {
  70. name: '', //产品名称
  71. goodsList:[],
  72. goodsIdArr:[],
  73. selectGoodsId: 0,
  74. goodsShow: false,
  75. totalData:{
  76. kindsNum:0,
  77. countNum:0,
  78. price: 0
  79. },
  80. isSubmit: false,
  81. scanCode: {
  82. code: '',
  83. lastTime: 0
  84. },
  85. searchGoodsList: []
  86. };
  87. },
  88. onLoad(option) {
  89. uni.hideTabBar();
  90. },
  91. onShow() {
  92. this.getGoodsData();
  93. // #ifdef APP-PLUS
  94. plus.key.addEventListener('keyup', this.listenerScancode, true);
  95. // #endif
  96. // #ifdef H5
  97. window.addEventListener('keypress', this.listenerScancode, true);
  98. // #endif
  99. },
  100. onHide() {
  101. // #ifdef APP-PLUS
  102. plus.key.removeEventListener('keyup', this.listenerScancode, true);
  103. // #endif
  104. // #ifdef H5
  105. window.removeEventListener('keypress', this.listenerScancode, true);
  106. // #endif
  107. },
  108. watch: {
  109. goodsIdArr(data){
  110. this.calcTotalData();
  111. },
  112. name(){
  113. this.getGoodsData();
  114. }
  115. },
  116. methods: {
  117. getGoodsData() {
  118. this.$api.sendRequest({
  119. url: '/stock/storeapi/manage/getskulist',
  120. data:{search: this.name},
  121. success: res => {
  122. this.searchGoodsList = [];
  123. if (res.code >= 0 && res.data.length != 0) {
  124. this.searchGoodsList = res.data;
  125. } else{
  126. this.$util.showToast({
  127. title:res.message
  128. });
  129. }
  130. }
  131. });
  132. },
  133. selectGoods(data){
  134. this.selectGoodsId = data.sku_id;
  135. if(!this.goodsIdArr.includes(data.sku_id)) {
  136. this.goodsIdArr.push(data.sku_id);
  137. this.goodsList.push(data);
  138. }
  139. this.goodsShow = false;
  140. },
  141. delGoods(id){
  142. this.goodsList.splice(this.goodsIdArr.indexOf(id),1);
  143. this.goodsIdArr.splice(this.goodsIdArr.indexOf(id),1);
  144. this.$forceUpdate();
  145. },
  146. stockOutFn(){
  147. if(!this.goodsIdArr.length){
  148. this.$util.showToast({
  149. title: "请选择导出数据"
  150. });
  151. return false;
  152. }
  153. // 检测库存是否填写,且提取数据
  154. let isStock = false;
  155. let saveData = [];
  156. try{
  157. this.goodsList.forEach((item,index)=>{
  158. if(this.goodsIdArr.includes(item.sku_id)){
  159. if(!parseFloat(item.goods_num || 0)){
  160. isStock = true;
  161. let toast = "请输入" + item.sku_name + "的入库数量";
  162. this.$util.showToast({
  163. title: toast
  164. });
  165. throw new Error('end');
  166. }
  167. var obj = {};
  168. obj.goods_num = item.goods_num;
  169. obj.goods_price = item.cost_price;
  170. obj.goods_sku_id = item.sku_id;
  171. saveData.push(obj);
  172. }
  173. })
  174. }catch(e){
  175. if(e.message != "end") throw e;
  176. }
  177. if(isStock) return false;
  178. if(this.isSubmit) return false;
  179. this.isSubmit = true;
  180. this.$api.sendRequest({
  181. url: '/stock/storeapi/storage/stockin',
  182. data:{stock_json: JSON.stringify(saveData)},
  183. success: res => {
  184. this.isSubmit = false;
  185. this.$util.showToast({
  186. title:res.message
  187. });
  188. if (res.code >= 0) {
  189. setTimeout(()=>{
  190. this.backFn();
  191. },500);
  192. this.resetFn();
  193. }
  194. }
  195. });
  196. },
  197. backFn(){
  198. this.$util.redirectTo('/pages/stock/storage');
  199. },
  200. calcTotalData(){
  201. this.totalData.price = 0;
  202. this.totalData.countNum = 0;
  203. this.totalData.kindsNum = 0;
  204. this.goodsList.forEach((item,index)=>{
  205. if(this.goodsIdArr.includes(item.sku_id)){
  206. this.totalData.price += parseFloat(item.cost_price) * parseFloat(item.goods_num || 1);
  207. this.totalData.countNum += parseFloat(item.goods_num || 0);
  208. this.totalData.unit = item.unit;
  209. }
  210. })
  211. this.totalData.kindsNum = this.goodsIdArr.length;
  212. },
  213. resetFn(){
  214. this.goodsIdArr = [];
  215. this.selectGoodsId = this.goodsList[0].sku_id;
  216. this.goodsShow = false;
  217. this.totalData.kindsNum = 0;
  218. this.totalData.countNum = 0;
  219. this.totalData.price = 0;
  220. },
  221. /**
  222. * 监听扫码事件
  223. * @param {Object} e
  224. */
  225. listenerScancode(e){
  226. const clearBarCode = ()=> {
  227. this.scanCode = { lastTime: 0, code: '' }
  228. }
  229. // #ifdef H5
  230. var currCode = e.keyCode || e.which || e.charCode;
  231. // #endif
  232. // #ifdef APP-PLUS
  233. const keycode = ['keycode_7': 0, 'keycode_8': 1, 'keycode_9': 2, 'keycode_10': 3, 'keycode_11': 4, 'keycode_12':
  234. 5, 'keycode_13': 6, 'keycode_14': 7, 'keycode_15': 8, 'keycode_16': 9
  235. ];
  236. var currCode = keyArr['keycode_' + e.keyCode] || '';
  237. // #endif
  238. var currTime = new Date().getTime();
  239. if (this.scanCode.lastTime > 0) {
  240. if (currTime - this.scanCode.lastTime <= 100) {
  241. this.scanCode.code += String.fromCharCode(currCode);
  242. } else if (currTime - this.scanCode.lastTime > 500) {
  243. // 输入间隔500毫秒清空
  244. clearBarCode();
  245. }
  246. } else {
  247. this.scanCode.code = String.fromCharCode(currCode);
  248. }
  249. this.scanCode.lastTime = currTime;
  250. // #ifdef H5
  251. if (currCode == 13) {
  252. // #endif
  253. // #ifdef APP-PLUS
  254. if (e.keyCode == 66) {
  255. // #endif
  256. if (this.scanCode.code && this.scanCode.code.length >= 8) this.getSkuBycode(this.scanCode.code)
  257. // 回车输入后清空
  258. clearBarCode();
  259. }
  260. },
  261. /**
  262. * 获取商品信息通过条形码
  263. */
  264. getSkuBycode(code){
  265. this.$api.sendRequest({
  266. url: '/cashier/storeapi/goods/skuinfo',
  267. data: {
  268. sku_no: code.trim()
  269. },
  270. success: res => {
  271. if (res.code == 0) {
  272. if (res.data) {
  273. res.data.goods_num = 0;
  274. this.selectGoods(res.data)
  275. } else {
  276. this.$util.showToast({
  277. title: '未找到该商品!'
  278. })
  279. }
  280. } else {
  281. this.$util.showToast({
  282. title: res.message,
  283. })
  284. }
  285. }
  286. })
  287. }
  288. }
  289. };
  290. </script>
  291. <style lang="scss">
  292. .content-wrap {
  293. position: relative;
  294. padding: 0.15rem;
  295. background-color: #fff;
  296. @extend %body-overhide;
  297. box-sizing: border-box;
  298. .title {
  299. font-size: 0.18rem;
  300. margin-bottom: 0.2rem;
  301. text-align: center;
  302. }
  303. .table-wrap {
  304. position: relative;
  305. margin-top: 40rpx;
  306. border: 1rpx solid #ccc;
  307. .table-head {
  308. background-color: #f7f7f7;
  309. }
  310. .table-body {
  311. @extend %body-overhide;
  312. max-height: 6rem;
  313. .table-tr {
  314. &:nth-child(1) {
  315. position: absolute;
  316. left: 0;
  317. right: 0;
  318. background: #fff;
  319. z-index: 2;
  320. }
  321. &:nth-child(2) {
  322. margin-top: 0.51rem;
  323. }
  324. &:last-of-type .table-td {
  325. border-bottom: 0;
  326. }
  327. }
  328. }
  329. .table-tr {
  330. display: flex;
  331. }
  332. .table-th,
  333. .table-td {
  334. display: flex;
  335. align-items: center;
  336. justify-content: center;
  337. padding: 0.15rem 0.3rem;
  338. border-bottom: 0.01rem solid #ccc;
  339. border-right: 0.01rem solid #ccc;
  340. text-align: center;
  341. &:last-of-type {
  342. border-right: 0;
  343. justify-content: flex-end;
  344. }
  345. &.goods-name {
  346. justify-content: flex-start;
  347. image {
  348. width: 0.45rem;
  349. height: 0.45rem;
  350. flex-shrink: 0;
  351. }
  352. .name {
  353. margin-left: 0.1rem;
  354. }
  355. }
  356. }
  357. .goods-num {
  358. border: 0.01rem solid #eee;
  359. height: 0.35rem;
  360. line-height: 0.35rem;
  361. border-radius: 0.05rem;
  362. }
  363. input {
  364. font-size: $uni-font-size-base;
  365. }
  366. .delete {
  367. margin: 0;
  368. font-size: $uni-font-size-base;
  369. background-color: $uni-color-primary;
  370. color: #fff;
  371. }
  372. .table-empty {
  373. justify-content: center;
  374. padding: 0.3rem;
  375. color: #999;
  376. }
  377. }
  378. .action-wrap {
  379. position: absolute;
  380. bottom: 0;
  381. left: 0;
  382. right: 0;
  383. display: flex;
  384. justify-content: space-between;
  385. padding: 0.1rem 0.15rem 0.2rem;
  386. align-items: center;
  387. border-top: 0.01rem solid #ccc;
  388. .btn-wrap {
  389. display: flex;
  390. align-items: center;
  391. justify-content: center;
  392. button {
  393. margin: 0;
  394. min-width: 2.75rem;
  395. height: 0.4rem;
  396. line-height: 0.4rem;
  397. font-size: $uni-font-size-base;
  398. &.stockout-btn {
  399. margin-right: 0.15rem;
  400. background-color: $uni-color-primary;
  401. color: #fff;
  402. }
  403. }
  404. }
  405. }
  406. .select-goods-input {
  407. position: relative;
  408. input {
  409. flex: 1;
  410. font-size: $uni-font-size-base;
  411. }
  412. }
  413. .select-goods-frame {
  414. position: absolute;
  415. z-index: 2;
  416. padding: 0.05rem 0;
  417. top: 0.45rem;
  418. left: 0;
  419. width: 5rem;
  420. height: 5rem;
  421. background-color: #fff;
  422. box-shadow: 0 0 36rpx rgba(0, 0, 0, 0.4);
  423. .goods-item {
  424. padding: 10rpx 30rpx;
  425. line-height: 1.5;
  426. text-align: left;
  427. &.select {
  428. background-color: $uni-color-primary;
  429. color: #fff;
  430. }
  431. }
  432. .goods-item-empty {
  433. padding: 10rpx 30rpx;
  434. line-height: 1.5;
  435. text-align: center;
  436. }
  437. }
  438. }
  439. </style>