0
0

AssetCheck.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class AssetCheck extends Model
  8. {
  9. public function updates(){
  10. $data = request()->post();
  11. $data['org_id'] = cur_org_id();
  12. $data['user_id'] = is_login();
  13. $data['create_time'] = date('Y-m-d H:i:s');
  14. $data['status'] = 0;
  15. $result = validate('AssetCheck')->check($data,[],'');
  16. if(true !== $result){
  17. $this->error = validate('AssetCheck')->getError();
  18. return false;
  19. }
  20. $itemsIds = [];
  21. foreach ($data['items'] as $k=>$v){
  22. $itemsIds[] = $v['id'];
  23. }
  24. $items = $itemsIds;
  25. unset($data['items']);
  26. $assets = Db::name('asset_items')
  27. ->where('id','in',$items)
  28. ->where('del',0)
  29. ->where('enable','in','1,2')
  30. ->field('id as items_id,user_id,dep_id,nums,sn,title,spec,price,unit_id')
  31. ->select();
  32. $assets = $assets?$assets:[];
  33. if(count($assets) != count($items)){
  34. $this->error = '有部分资产状态不可盘库';
  35. return false;
  36. }
  37. Db::startTrans();
  38. try{
  39. $checkId = Db::name('asset_check')->insertGetId($data);
  40. if(!$checkId){
  41. \exception('操作失败');
  42. }
  43. $arr = [];
  44. foreach ($assets as $k=>$v){
  45. $v['check_id'] = $checkId;
  46. $arr[] = $v;
  47. if(count($arr) == 300){
  48. $res = Db::name('asset_check_items')->insertAll($arr);
  49. if(count($arr) != $res){
  50. \exception('操作失败');
  51. }
  52. }
  53. }
  54. if($arr){
  55. $res = Db::name('asset_check_items')->insertAll($arr);
  56. if(count($arr) != $res){
  57. \exception('操作失败');
  58. }
  59. }
  60. Db::commit();
  61. }catch (Exception $e){
  62. Db::rollback();
  63. $this->error = $e->getMessage();
  64. return false;
  65. }
  66. return true;
  67. }
  68. public function info($id){
  69. $info = Db::name('asset_check')->where('id',$id)->where('del',0)->find();
  70. if(!$info){
  71. $this->error ='数据不存在';
  72. return false;
  73. }
  74. $info['userName'] = Db::name('user')->where('id',$info['user_id'])->value('real_name');
  75. $info['totalNums'] = Db::name('asset_check_items')->where('check_id',$id)->where('del',0)->sum('nums');
  76. $info['totalCheckNums'] = Db::name('asset_check_items')->where('check_id',$id)->where('del',0)->sum('check_nums');
  77. $info['totalDiffNums'] = $info['totalCheckNums'] - $info['totalNums'];
  78. return $info;
  79. }
  80. public function finish($id){
  81. $info = Db::name('asset_check')->where('id',$id)->where('del',0)->find();
  82. if(!$info){
  83. $this->error ='记录不存在';
  84. return false;
  85. }
  86. if($info['status'] == 1){
  87. $this->error ='已完成无需重复操作';
  88. return false;
  89. }
  90. $ret = Db::name('asset_check')->where('id',$id)->update(['status' => 1,'finish_time'=>date('Y-m-d H:i:s')]);
  91. if(!$ret){
  92. $this->error ='操作失败';
  93. return false;
  94. }
  95. return true;
  96. }
  97. public function allItems($checkId){
  98. $map[] = ['check_id','=',$checkId];
  99. $map[] = ['del','=',0];
  100. $lists = Db::name('asset_check_items')
  101. ->where($map)
  102. ->order('id asc')
  103. ->select();
  104. foreach ($lists as $k=>$v){
  105. $lists[$k]['depName'] = '';
  106. $lists[$k]['userName'] = '';
  107. if($v['dep_id']){
  108. $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
  109. $lists[$k]['userName'] = Db::name('user')->where('user_id',$v['dep_id'])->value('real_name');
  110. }
  111. $lists[$k]['diffNums'] = $v['check_nums'] - $v['nums'];
  112. }
  113. $check = $this->info($checkId);
  114. $data = [
  115. 'check' => $check,
  116. 'list' => $lists?$lists:[]
  117. ];
  118. return $data;
  119. }
  120. public function delItems($id){
  121. $info = Db::name('asset_check_items')->where('id',$id)->where('del',0)->find();
  122. if(!$info){
  123. $this->error = '记录不存在';
  124. return false;
  125. }
  126. $check = $this->info($info['check_id']);
  127. if($check['status'] == 1){
  128. $this->error = '盘点已完成,无法删除';
  129. return false;
  130. }
  131. $ret = Db::name('asset_check_items')->where('id',$id)->setField('del',1);
  132. if(!$ret){
  133. $this->error = '操作失败';
  134. return false;
  135. }
  136. return true;
  137. }
  138. public function finishItems($id){
  139. $info = Db::name('asset_check_items')->where('id',$id)->where('del',0)->find();
  140. if(!$info){
  141. $this->error = '记录不存在';
  142. return false;
  143. }
  144. if($info['status'] != 0){
  145. $this->error = '已盘点';
  146. return false;
  147. }
  148. $check = $this->info($info['check_id']);
  149. if($check['status'] == 1){
  150. $this->error = '盘点已完成,无法操作';
  151. return false;
  152. }
  153. $ret = Db::name('asset_check_items')->where('id',$id)->update([
  154. 'status' => 1,
  155. 'check_nums' => 1,
  156. 'check_time' => date('Y-m-d H:i:s'),
  157. 'remark' => '手动盘点'
  158. ]);
  159. if(!$ret){
  160. $this->error = '操作失败';
  161. return false;
  162. }
  163. return true;
  164. }
  165. }