AssetDisposal.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 AssetDisposal extends Model
  8. {
  9. public function advancedStartAssetDisposal($id,$orgId,$userId,$formJson,$extra){
  10. $formJson = json_decode($formJson,true);
  11. $data = [
  12. 'org_id' => $orgId,
  13. 'user_id' => $userId,
  14. 'apply_id' => $id,
  15. 'dep_id' => $extra['depId'],
  16. 'create_time' => date('Y-m-d H:i:s'),
  17. 'status' => 0,
  18. ];
  19. $items = [];
  20. foreach ($formJson as $k=>$v){
  21. if($v['componentName'] == 'ddassetdisposalfield'){
  22. foreach ($v['components'] as $key=>$val){
  23. switch ($val['idx']){
  24. case '0':
  25. $items = json_decode($val['values'],true);
  26. break;
  27. case '1':
  28. $data['remark'] = $val['values'];
  29. break;
  30. }
  31. }
  32. break;
  33. }
  34. }
  35. $amount = 0;
  36. foreach ($items as $k=>$v){
  37. $amount += $v['snums']*$v['price'];
  38. }
  39. $data['amount'] = $amount;
  40. $receiveId = Db::name('asset_disposal')->insertGetId($data);
  41. $sitems = [];
  42. foreach ($items as $k=>$v){
  43. $sitems[] = [
  44. 'disposal_id' => $receiveId,
  45. 'items_id' => $v['id'],
  46. 'nums' => $v['snums']
  47. ];
  48. }
  49. $count = Db::name('asset_disposal_items')->insertAll($sitems);
  50. if($count != count($sitems)){
  51. return false;
  52. }
  53. return true;
  54. }
  55. public function advancedEndAssetDisposal($id,$orgId,$userId,$formJson){
  56. $data = [
  57. 'update_time' => date('Y-m-d H:i:s'),
  58. 'status' => 1
  59. ];
  60. $info = Db::name('asset_disposal')->where('apply_id',$id)->find();
  61. if(!$info || $info['status'] != 0){
  62. return false;
  63. }
  64. $ret = Db::name('asset_disposal')->where('apply_id',$id)->update($data);
  65. if(!$ret){
  66. return false;
  67. }
  68. // 删除使用者和使用部门
  69. $itemsids = Db::name('asset_disposal_items')->where('disposal_id',$info['id'])->column('items_id');
  70. Db::name('asset_items')->where('id','in',$itemsids)->update([
  71. 'enable' => 5,
  72. 'update_time' => date('Y-m-d H:i:s')
  73. ]);
  74. return $ret?true:false;
  75. }
  76. public function advancedDisagreeAssetDisposal($id){
  77. $data = [
  78. 'update_time' => date('Y-m-d H:i:s'),
  79. 'status' => 2
  80. ];
  81. $ret = Db::name('asset_disposal')->where('apply_id',$id)->update($data);
  82. if(!$ret){
  83. return false;
  84. }
  85. return true;
  86. }
  87. public function lists($page,$size,$title,$start,$end,$orgId){
  88. $map[] = ['or.org_id','=',$orgId];
  89. if($title){
  90. $map[] = ['ui.name','like','%'.$title.'%'];
  91. }
  92. if($start&&$end){
  93. $map[] = ['or.create_time','egt',$start];
  94. $map[] = ['or.create_time','elt',$end];
  95. }
  96. $lists = Db::name('asset_disposal')
  97. ->alias('or')
  98. ->join('user_info ui','ui.user_id = or.agent_user_id')
  99. ->field('or.*,ui.name as user_name')
  100. ->where($map)
  101. ->page($page,$size)
  102. ->order('or.id desc')
  103. ->select();
  104. $lists = $lists?$lists:[];
  105. foreach ($lists as $k=>$v){
  106. $lists[$k]['agent_dep_name'] = Db::name('dep')->where('id',$v['agent_dep_id'])->value('name');
  107. $lists[$k]['way_name'] = Db::name('asset_way')->where('id',$v['way_id'])->value('title');
  108. }
  109. $total = Db::name('asset_disposal')
  110. ->alias('or')
  111. ->join('user_info ui','ui.user_id = or.agent_user_id')
  112. ->where($map)->count();
  113. $data = [
  114. 'total' => $total,
  115. 'list' => $lists
  116. ];
  117. return $data;
  118. }
  119. public function add(){
  120. $data = [
  121. 'id' => input('id/d',0),
  122. 'org_id' => input('orgId/d',0),
  123. 'agent_user_id' => input('agentUserId/d',0),
  124. 'agent_dep_id' => input('agentDepId/d',0),
  125. 'way_id' => input('wayId/d',0),
  126. 'reason' => input('reason','','trim'),
  127. 'result' => input('result','','trim'),
  128. 'remark' => input('remark','','trim'),
  129. 'files' => input('files','','trim'),
  130. 'items' => input('items','','trim')
  131. ];
  132. $result = validate('AssetDisposal')->check($data,[],'');
  133. if(true !== $result){
  134. HelpHander::error(validate('AssetDisposal')->getError());
  135. }
  136. $id = $data['id'];
  137. unset($data['id']);
  138. if($id > 0){
  139. $data['update_time'] = date('Y-m-d H:i:s');
  140. unset($data['items']);
  141. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  142. if(!$ret){
  143. HelpHander::error('操作失败');
  144. }
  145. }else{
  146. $data['create_time'] = date('Y-m-d H:i:s');
  147. Db::startTrans();
  148. try{
  149. $data['sn'] = $this->getSn($data['org_id']);
  150. $items = json_decode($data['items'],true);
  151. unset($data['items']);
  152. $receiveId = Db::name('AssetDisposal')->insertGetId($data);
  153. if(!$receiveId){
  154. \exception('操作失败');
  155. }
  156. $d = [];
  157. $itemids = [];
  158. foreach ($items as $k=>$v){
  159. $iteminfo = Db::name('asset_items')
  160. ->where('id',$v['id'])
  161. ->field('id,user_id,dep_id')
  162. ->find();
  163. $d[] = [
  164. 'disposal_id' => $receiveId,
  165. 'items_id' => $v['id'],
  166. 'user_id' => $iteminfo['user_id'],
  167. 'dep_id' => $iteminfo['dep_id'],
  168. ];
  169. $itemids[] = $v['id'];
  170. }
  171. $res = Db::name('asset_disposal_items')->insertAll($d);
  172. if($res != count($d)){
  173. \exception('操作失败');
  174. }
  175. $sd = [
  176. 'enable' => 5,
  177. 'update_time' => date('Y-m-d H:i:s')
  178. ];
  179. $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
  180. if($res != count($itemids)){
  181. \exception('操作失败');
  182. }
  183. Db::commit();
  184. }catch (Exception $e){
  185. Db::rollback();
  186. HelpHander::error($e->getMessage());
  187. }
  188. }
  189. return true;
  190. }
  191. public function getSn($orgId){
  192. $max = Db::name('asset_disposal')
  193. ->where('org_id',$orgId)
  194. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  195. ->order('id desc')
  196. ->value('sn');
  197. $maxinit = $max?$max:date('Ymd').'0000';
  198. return $maxinit + 1;
  199. }
  200. public function info($id){
  201. $info = $this->where('id',$id)->find();
  202. if(!$info){
  203. HelpHander::error('数据不存在');
  204. }
  205. $data = $info->toArray();
  206. $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('name');
  207. $data['agentUserName'] = Db::name('user_info')->where('user_id',$info['agent_user_id'])->value('name');
  208. $data['way_name'] = Db::name('asset_way')->where('id',$info['way_id'])->value('title');
  209. $items = Db::name('asset_disposal_items')
  210. ->alias('ari')
  211. ->join('asset_items ai','ai.id = ari.items_id')
  212. ->join('asset_unit au','au.id = ai.unit_id')
  213. ->where('ari.disposal_id',$id)
  214. ->field('ai.*,au.title as unitName')
  215. ->select();
  216. $data['items'] = $items?$items:[];
  217. $items = Db::name('asset_disposal_items')
  218. ->alias('ari')
  219. ->join('asset_items ai','ai.id = ari.items_id')
  220. ->join('asset_unit au','au.id = ai.unit_id')
  221. ->where('ari.disposal_id',$id)
  222. ->field('ai.*,au.title as unitName,ari.user_id as old_user_id,ari.dep_id as old_dep_id')
  223. ->select();
  224. foreach ($items as $k=>$v){
  225. $items[$k]['userName'] = Db::name('user_info')->where('user_id',$v['old_user_id'])->value('name');
  226. $items[$k]['depName'] = Db::name('dep')->where('id',$v['old_dep_id'])->value('name');
  227. }
  228. $data['items'] = $items?$items:[];
  229. return $data;
  230. }
  231. }