0
0

MateGoods.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. use think\Exception;
  7. class MateGoods extends Base
  8. {
  9. public function lists(){
  10. $page = input('page',1);
  11. $size = input('size',10);
  12. $title = input('title','','trim');
  13. if($title !=''){
  14. $map[] = ['title','like','%'.$title.'%'];
  15. }
  16. $map[] = ['org_id','=',$this->orgId];
  17. $map[] = ['del','=',0];
  18. $list = Db::name('mate_goods')
  19. ->field('id as goods_id,price,nums,type,title,unit,brand,spec,consume,create_time')
  20. ->where($map)
  21. ->page($page,$size)
  22. ->order('id desc')
  23. ->select();
  24. foreach ($list as $k=>$v){
  25. if($v['type'] == 1){
  26. $list[$k]['cate_name'] ='固定资产';
  27. }elseif ($v['type'] == 2){
  28. $list[$k]['cate_name'] ='消耗品';
  29. }else{
  30. $list[$k]['cate_name'] = '';
  31. }
  32. }
  33. HelpHander::success($list);
  34. }
  35. //入库/出库记录
  36. public function storageList(){
  37. $type = input('type/d',0);
  38. $length = input('size',10); //每页条数
  39. $page = input('page',1); //第几页
  40. $start = ($page - 1) * $length; //分页开始位置
  41. $map[] = ['org_id','=',$this->orgId];
  42. $map[] = ['del','=',0];
  43. $map[] = ['user_id','=',$this->userId];
  44. if($type == 0){
  45. $type = 1;
  46. }else if($type == 1){
  47. $type = 2;
  48. }
  49. $map[] = ['type','=',$type];
  50. $lists = Db::name('mate_apply')
  51. ->where($map)
  52. ->field('id,org_id,user_id,remark,name,phone,create_time')
  53. ->limit($start,$length)
  54. ->order('id','desc')
  55. ->select();
  56. foreach ($lists as $k=>$v){
  57. $lists[$k]['userName'] = Db::name('user')
  58. ->where('id',$v['user_id'])
  59. ->value('real_name');
  60. }
  61. HelpHander::success($lists);
  62. }
  63. public function storageDetail(){
  64. $id = input('id/d',0);
  65. $info = db('mate_apply')
  66. ->where('id',$id)
  67. ->where('del',0)
  68. ->field('id,org_id,user_id,remark,name,phone,create_time,sign')
  69. ->find();
  70. if(!$info){
  71. HelpHander::error('信息不存在');
  72. }
  73. $info['sign'] = $info['sign']?$info['sign']:'';
  74. $info['userName'] = Db::name('user')
  75. ->where('id',$info['user_id'])
  76. ->value('real_name');
  77. $info['goods'] = Db::name('mate_apply_goods')
  78. ->alias('a')
  79. ->join('mate_goods b','a.goods_id=b.id')
  80. ->where('a.apply_id',$info['id'])
  81. ->field('a.*,b.title,b.unit,b.brand,b.spec')
  82. ->select();
  83. HelpHander::success($info);
  84. }
  85. public function scanGoods(){
  86. $code = input('code','');
  87. if(empty($code)){
  88. HelpHander::error('二维码为空');
  89. }
  90. $info = get_qrcode_arr($code);
  91. if(!$info || $info['ucode'] != config('app.ucode') || $info['type'] != 'mate_goods'){
  92. HelpHander::error('二维码不正确');
  93. }
  94. $goods = Db::name('mate_goods')
  95. ->where('id',$info['id'])
  96. ->field('img,news_status,total_price,consume,buy_time,update_time,del,often',true)
  97. ->find();
  98. HelpHander::success($goods);
  99. }
  100. public function addStorage(){
  101. $name =input('name','');
  102. $phone = input('phone','');
  103. $remark = input('remark','');
  104. $sign = input('sign','');
  105. $options = input('options');
  106. $type = input('type/d',2);
  107. if(empty($name)){
  108. HelpHander::error($type==2?'入库':'出库'.'人不能为空');
  109. }
  110. if(empty($phone)){
  111. HelpHander::error('电话');
  112. }
  113. if(empty($options)){
  114. HelpHander::error('物品信息不能为空');
  115. }
  116. $option = json_decode($options,true);
  117. if(empty($option)){
  118. HelpHander::error('物品信息不能为空');
  119. }
  120. Db::startTrans();
  121. try{
  122. foreach($option as $k=>$v){
  123. $g = Db::name('mate_goods')
  124. ->where('id',$v['id'])
  125. ->find();
  126. if($type==2){
  127. // if(empty($v['price']) || $v['price'] <=0){
  128. // exception($g['title'].'价格不能为空');
  129. // }
  130. }else{
  131. $goods = db('mate_goods')
  132. ->where('id',$v['id'])
  133. ->find();
  134. if($goods['nums']<$v['nums']){
  135. exception($g['title'].'出库数量超过库存数量');
  136. }
  137. }
  138. if(empty($v['nums']) || $v['nums'] <=0){
  139. exception($g['title'].'数量不能为空');
  140. }
  141. }
  142. //$post['type']==2 入库单 else 调拨单
  143. $apply = [
  144. 'org_id'=>$this->orgId,
  145. 'name'=>$name,
  146. 'phone'=>$phone,
  147. 'remark'=>$remark,
  148. 'sign'=>$sign,
  149. 'type'=>$type==2?1:2,
  150. 'create_time'=>getTime(),
  151. 'user_id'=>$this->userId,
  152. 'sn'=>get_unique_id()
  153. ];
  154. $applyid = db('mate_apply')->insertGetId($apply);
  155. if(!$applyid){
  156. exception('添加入库单失败');
  157. }
  158. foreach($option as $k=>$v){
  159. $goods = db('mate_goods')
  160. ->where('id',$v['id'])
  161. ->find();
  162. $items =[
  163. 'apply_id'=>$applyid,
  164. 'goods_id'=>$v['id'],
  165. 'nums'=>$v['nums'],
  166. //'price'=>$type==2?$v['price']:$goods['price'],
  167. 'price'=>$type==1?$goods['price']:$goods['price'],
  168. ];
  169. $res = db('mate_apply_goods')
  170. ->insertGetId($items);
  171. if(!$res){
  172. exception($v['title'].'添加mate_apply_goods失败');
  173. }
  174. $old = $goods['nums']*$goods['price'];
  175. $new = $items['nums']*$items['price'];
  176. $nums = $goods['nums']+$items['nums'];
  177. $price = ($old+$new)/$nums;
  178. if($type==1){ //调拨单
  179. $u = [
  180. 'nums'=>$goods['nums']-$items['nums'],
  181. 'total_price'=>($goods['nums']-$items['nums'])*$goods['price'],
  182. 'update_time'=>getTime(),
  183. ];
  184. }else{ //入库单
  185. $u = [
  186. 'nums'=>$nums,
  187. 'price'=>$price,
  188. 'total_price'=>$nums*$price,
  189. 'update_time'=>getTime(),
  190. ];
  191. }
  192. $ret = db('mate_goods')
  193. ->where('id',$v['id'])->update($u);
  194. if(!$ret){
  195. exception($v['title'].'更新数量失败');
  196. }
  197. }
  198. Db::commit();
  199. HelpHander::success([],'操作成功');
  200. }catch (Exception $e) {
  201. Db::rollback();
  202. HelpHander::error($e->getmessage());
  203. }
  204. }
  205. public function goodsDetail(){
  206. $code = input('code','');
  207. if(empty($code)){
  208. HelpHander::error('二维码为空');
  209. }
  210. $info = get_qrcode_arr($code);
  211. if(!$info || $info['ucode'] != config('app.ucode') || $info['type'] != 'mate_goods'){
  212. HelpHander::error('二维码不正确');
  213. }
  214. $goods = Db::name('mate_goods')
  215. ->where('id',$info['id'])
  216. ->field('img,news_status,total_price,consume,buy_time,update_time,del,often',true)
  217. ->find();
  218. $rk = Db::name('mate_apply_goods')
  219. ->alias('a')
  220. ->join('mate_apply c','a.apply_id=c.id')
  221. ->join('mate_goods b','a.goods_id=b.id')
  222. ->where('c.type',1)
  223. ->where('c.del',0)
  224. ->where('c.org_id',$this->orgId)
  225. ->where('b.id',$goods['id'])
  226. ->field('a.*,b.title,b.unit,b.brand,b.spec,c.id as dispatch_id')
  227. ->select();
  228. foreach ($rk as $k=>$v){
  229. $dispatch = Db::name('mate_apply')
  230. ->where('id',$v['apply_id'])
  231. ->find();
  232. $rk[$k]['userName'] = Db::name('user')
  233. ->where('id',$dispatch['user_id'])
  234. ->value('real_name');
  235. $rk[$k]['create_time'] = $dispatch['create_time'];
  236. $rk[$k]['remark'] = $dispatch['remark'];
  237. $rk[$k]['name'] = $dispatch['name'];
  238. $rk[$k]['phone'] = $dispatch['phone'];
  239. }
  240. $goods['rk'] = $rk;
  241. $ck = Db::name('mate_apply_goods')
  242. ->alias('a')
  243. ->join('mate_apply c','a.apply_id=c.id')
  244. ->join('mate_goods b','a.goods_id=b.id')
  245. ->where('c.type',2)
  246. ->where('c.del',0)
  247. ->where('c.org_id',$this->orgId)
  248. ->where('b.id',$goods['id'])
  249. ->field('a.*,b.title,b.unit,b.brand,b.spec,c.id as dispatch_id')
  250. ->select();
  251. foreach ($ck as $k=>$v){
  252. $dispatch = Db::name('mate_apply')
  253. ->where('id',$v['apply_id'])
  254. ->find();
  255. $ck[$k]['userName'] = Db::name('user')
  256. ->where('id',$dispatch['user_id'])
  257. ->value('real_name');
  258. $ck[$k]['create_time'] = $dispatch['create_time'];
  259. $ck[$k]['remark'] = $dispatch['remark'];
  260. $ck[$k]['name'] = $dispatch['name'];
  261. $ck[$k]['phone'] = $dispatch['phone'];
  262. }
  263. $goods['ck'] = $ck;
  264. HelpHander::success($goods);
  265. }
  266. }