MateApplyUse.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\MateGoodsLog;
  4. use app\hander\HelpHander;
  5. use think\App;
  6. use think\Db;
  7. use think\Exception;
  8. class MateApplyUse extends Auth
  9. {
  10. public function __construct(App $app = null) {
  11. parent::__construct($app);
  12. $this->table = 'mate_apply_use';
  13. }
  14. //领取记录
  15. public function get(){
  16. if(request()->isAjax()){
  17. //分页参数
  18. $length = input('rows',10,'intval'); //每页条数
  19. $page = input('page',1,'intval'); //第几页
  20. $start = ($page - 1) * $length; //分页开始位置
  21. //排序
  22. $sortRow = input('sidx','sort','trim'); //排序列
  23. $sort = input('sord','asc','trim'); //排序方式
  24. $order = $sortRow.' '.$sort.' ,id desc';
  25. $title = input('title','','trim');
  26. if($title){
  27. $map[] = ['sn','like','%'.$title.'%'];
  28. }
  29. // $orgs = mate_user($this->userId);
  30. // if(!empty($orgs)){
  31. // $map[] = ['org_id','in',$orgs];
  32. // }else{
  33. // $map[] = ['org_id','=',$this->orgId];
  34. // }
  35. $map[] = ['type','=',1];
  36. $map[] = ['org_id','=',$this->orgId];
  37. $map= empty($map) ? true: $map;
  38. //数据查询
  39. $lists = Db::name('mate_apply_use')
  40. ->where($map)
  41. ->limit($start,$length)
  42. ->order($order)
  43. ->select();
  44. foreach ($lists as $k=>$v){
  45. $lists[$k]['userName'] = Db::name('user')
  46. ->where('id',$v['user_id'])
  47. ->value('real_name');
  48. $lists[$k]['approverName'] = Db::name('user')
  49. ->where('id',$v['user_id'])
  50. ->value('real_name');
  51. }
  52. //数据返回
  53. $totalCount = Db::name('mate_apply_use')->where($map)->count();
  54. $totalPage = ceil($totalCount/$length);
  55. $result['page'] = $page;
  56. $result['total'] = $totalPage;
  57. $result['records'] = $totalCount;
  58. $result['rows'] = $lists;
  59. return json($result);
  60. }else{
  61. // $isMateUser = is_mate_user($this->userId);
  62. // $this->assign('isMateUser',$isMateUser);
  63. $this->assign('meta_title','物品领取记录');
  64. return $this->fetch();
  65. }
  66. }
  67. //审核
  68. public function check($id=0){
  69. if(request()->isAjax()){
  70. $data = request()->post();
  71. $info = Db::name('mate_apply_use')->where('id',$id)->find();
  72. if(!$info){
  73. $this->error('记录不存在');
  74. }
  75. if($info['status'] != 0){
  76. $this->error('记录已处理');
  77. }
  78. $goods = Db::name('mate_apply_use_goods')->where('apply_id',$id)->select();
  79. if($data['status'] == 1){
  80. Db::startTrans();
  81. try{
  82. $data = [
  83. 'status'=>1,
  84. 'approver_user_id'=>$this->userId,
  85. 'approver_time'=>date('Y-m-d H:i:s')
  86. ];
  87. // 更改记录状态
  88. $cc = Db::name('mate_apply_use')->where('id',$info['id'])->update($data);
  89. if(!$cc){
  90. \exception('操作失败');
  91. }
  92. // 更新物品信息
  93. if($info['type'] == 1){ // 领取
  94. foreach ($goods as $k=>$v){
  95. $ginfo = Db::name('mate_apply_use')
  96. ->where('id',$v['goods_id'])
  97. ->where('org_id',cur_org_id())
  98. ->find();
  99. if($ginfo['consume'] == 2 && $ginfo['type'] == 2){ // 出库消耗
  100. $gs = [[
  101. 'goodsId' => $v['goods_id'],
  102. 'nums' => $v['nums'],
  103. 'price' => $v['price']
  104. ]];
  105. $ret = model('MateGoodsUse')->save1(3,$v['id'],$info['user_id'],cur_org_id(),$gs);
  106. if(!$ret){
  107. \exception('操作失败');
  108. }
  109. }else{ //出库到小包
  110. $uginfo = Db::name('mate_user_goods')
  111. ->where('user_id',$info['user_id'])
  112. ->where('org_id',$info['org_id'])
  113. ->where('goods_id',$v['goods_id'])
  114. ->find();
  115. if($uginfo){
  116. Db::name('mate_user_goods')
  117. ->where('id',$uginfo['id'])
  118. ->update([
  119. 'nums'=>$uginfo['nums'] + $v['nums'],
  120. 'price' => round(($v['nums']*$v['price'] + $uginfo['nums']*$uginfo['price'])/($uginfo['nums'] + $v['nums']),2)
  121. ]);
  122. }else{
  123. $dd = Db::name('mate_user_goods')
  124. ->insert([
  125. 'org_id'=>cur_org_id(),
  126. 'user_id'=>$info['user_id'],
  127. 'goods_id'=>$v['goods_id'],
  128. 'nums'=>$v['nums'],
  129. 'price'=>$v['price'],
  130. ]);
  131. if(!$dd){
  132. \exception('操作失败');
  133. }
  134. }
  135. }
  136. }
  137. }else{ //归还
  138. foreach ($goods as $k=>$v){
  139. $ginfo = Db::name('mate_goods')
  140. ->where('id',$v['goods_id'])
  141. ->where('org_id',cur_org_id())
  142. ->find();
  143. $cc = Db::name('mate_goods')
  144. ->where('id',$ginfo['id'])
  145. ->update([
  146. 'nums'=>$ginfo['nums'] + $v['nums'],
  147. 'price' => round(($v['nums']*$v['price'] + $ginfo['nums']*$ginfo['price'])/($ginfo['nums'] + $v['nums']),2)
  148. ]);
  149. if(!$cc){
  150. \exception('操作失败');
  151. }
  152. }
  153. }
  154. Db::commit();
  155. }catch (\Exception $e){
  156. $this->error($e->getMessage());
  157. Db::rollback();
  158. }
  159. $this->success('操作成功');
  160. }elseif ($data['status'] == 2){
  161. Db::startTrans();
  162. try{
  163. $aa = Db::name('mate_apply_use')
  164. ->where('id',$info['id'])
  165. ->update([
  166. 'status' =>2,
  167. 'approver_user_id'=>$this->userId,
  168. 'approver_time'=>date('Y-m-d H:i:s')
  169. ]);
  170. // 更改记录状态
  171. if(!$aa){
  172. \exception('操作失败');
  173. }
  174. // 更新物品信息
  175. if($info['type'] == 1){ // 领取反库
  176. foreach ($goods as $k=>$v){
  177. $ginfo = Db::name('mate_goods')
  178. ->where('org_id',cur_org_id())
  179. ->where('id',$v['goods_id'])
  180. ->find();
  181. $b = Db::name('mate_goods')
  182. ->where('id',$ginfo['id'])
  183. ->update([
  184. 'nums'=>$ginfo['nums'] + $v['nums'],
  185. 'price'=>round(($v['nums'] * $v['price'] + $ginfo['nums'] * $ginfo['price'])/($ginfo['nums'] + $v['nums']),2),
  186. ]);
  187. if(!$b){
  188. \exception('操作失败');
  189. }
  190. }
  191. }else{ //归还小包
  192. foreach ($goods as $k=>$v){
  193. $uginfo = Db::name('mate_user_goods')
  194. ->where('goods_id',$v['goods_id'])
  195. ->where('org_id',cur_org_id())
  196. ->where('user_id',$info['user_id'])
  197. ->find();
  198. $c = Db::name('mate_user_goods')
  199. ->where('id',$uginfo['id'])
  200. ->update([
  201. 'nums'=>$uginfo['nums'] + $v['nums'],
  202. 'price' => round(($v['nums']*$v['price'] + $uginfo['nums']*$uginfo['price'])/($uginfo['nums'] + $v['nums']),2)
  203. ]);
  204. if(!$c){
  205. \exception('操作失败');
  206. }
  207. }
  208. }
  209. Db::commit();
  210. }catch (Exception $e){
  211. $this->error($e->getMessage());
  212. Db::rollback();
  213. }
  214. $this->success('操作成功');
  215. }
  216. }else{
  217. $this->assign('id',$id);
  218. return $this->fetch();
  219. }
  220. }
  221. //归还
  222. public function back(){
  223. if(request()->isAjax()){
  224. //分页参数
  225. $length = input('rows',10,'intval'); //每页条数
  226. $page = input('page',1,'intval'); //第几页
  227. $start = ($page - 1) * $length; //分页开始位置
  228. //排序
  229. $sortRow = input('sidx','sort','trim'); //排序列
  230. $sort = input('sord','asc','trim'); //排序方式
  231. $order = $sortRow.' '.$sort.' ,id desc';
  232. $title = input('title','','trim');
  233. if($title){
  234. $map[] = ['sn','like','%'.$title.'%'];
  235. }
  236. $map[] = ['type','=',2];
  237. // $orgs = mate_user($this->userId);
  238. // if(!empty($orgs)){
  239. // $map[] = ['org_id','in',$orgs];
  240. // }else{
  241. // $map[] = ['org_id','=',$this->orgId];
  242. // }
  243. $map[] = ['org_id','=',$this->orgId];
  244. $map= empty($map) ? true: $map;
  245. //数据查询
  246. $lists = Db::name('mate_apply_use')
  247. ->where($map)
  248. ->limit($start,$length)
  249. ->order($order)
  250. ->select();
  251. foreach ($lists as $k=>$v){
  252. $lists[$k]['userName'] = Db::name('user')
  253. ->where('id',$v['user_id'])
  254. ->value('real_name');
  255. $lists[$k]['approverName'] = Db::name('user')
  256. ->where('id',$v['user_id'])
  257. ->value('real_name');
  258. }
  259. //数据返回
  260. $totalCount = Db::name('mate_apply_use')->where($map)->count();
  261. $totalPage = ceil($totalCount/$length);
  262. $result['page'] = $page;
  263. $result['total'] = $totalPage;
  264. $result['records'] = $totalCount;
  265. $result['rows'] = $lists;
  266. return json($result);
  267. }else{
  268. // $isMateUser = is_mate_user($this->userId);
  269. // $this->assign('isMateUser',$isMateUser);
  270. $this->assign('meta_title','物品归还记录');
  271. return $this->fetch();
  272. }
  273. }
  274. public function detail($id){
  275. $info = (new \app\common\model\MateApplyUse())->info($id);
  276. if(!$info) $this->error('记录不存在');
  277. $this->assign('info',$info);
  278. return $this->fetch();
  279. }
  280. }