0
0

AssetDisposal.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 updates(){
  10. $data = request()->post();
  11. $data['org_id'] = cur_org_id();
  12. $result = validate('AssetDisposal')->check($data,[],'');
  13. if(true !== $result){
  14. $this->error = validate('AssetDisposal')->getError();
  15. return false;
  16. }
  17. $data['agent_dep_id'] = Db::name('user_dep')->where('user_id',$data['agent_user_id'])->value('dep_id');
  18. $id = $data['id'];
  19. unset($data['id']);
  20. if($id > 0){
  21. $data['update_time'] = date('Y-m-d H:i:s');
  22. unset($data['items']);
  23. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  24. if(!$ret){
  25. $this->error = '操作失败';
  26. return false;
  27. }
  28. }else{
  29. $data['create_time'] = date('Y-m-d H:i:s');
  30. Db::startTrans();
  31. try{
  32. $data['sn'] = $this->getSn($data['org_id']);
  33. $items = $data['items'];
  34. unset($data['items']);
  35. $receiveId = Db::name('AssetDisposal')->insertGetId($data);
  36. if(!$receiveId){
  37. \exception('操作失败');
  38. }
  39. $d = [];
  40. $itemids = [];
  41. foreach ($items as $k=>$v){
  42. $iteminfo = Db::name('asset_items')
  43. ->where('id',$v['id'])
  44. ->field('id,user_id,dep_id')
  45. ->find();
  46. $d[] = [
  47. 'disposal_id' => $receiveId,
  48. 'items_id' => $v['id'],
  49. 'user_id' => $iteminfo['user_id'],
  50. 'dep_id' => $iteminfo['dep_id'],
  51. ];
  52. $itemids[] = $v['id'];
  53. }
  54. $res = Db::name('asset_disposal_items')->insertAll($d);
  55. if($res != count($d)){
  56. \exception('操作失败');
  57. }
  58. $sd = [
  59. 'enable' => 5,
  60. 'update_time' => date('Y-m-d H:i:s')
  61. ];
  62. $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
  63. if($res != count($itemids)){
  64. \exception('操作失败');
  65. }
  66. Db::commit();
  67. }catch (Exception $e){
  68. Db::rollback();
  69. $this->error = $e->getMessage();
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. public function getSn($orgId){
  76. $max = Db::name('asset_disposal')
  77. ->where('org_id',$orgId)
  78. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  79. ->order('id desc')
  80. ->value('sn');
  81. $maxinit = $max?$max:date('Ymd').'0000';
  82. return $maxinit + 1;
  83. }
  84. public function info($id){
  85. $info = $this->where('id',$id)->find();
  86. if(!$info){
  87. $this->error = '数据不存在';
  88. return false;
  89. }
  90. $data = $info->toArray();
  91. $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('title');
  92. $data['agentUserName'] = Db::name('user')->where('id',$info['agent_user_id'])->value('real_name');
  93. $data['way_name'] = Db::name('asset_way')->where('id',$info['way_id'])->value('title');
  94. $items = Db::name('asset_disposal_items')
  95. ->alias('ari')
  96. ->join('asset_items ai','ai.id = ari.items_id')
  97. ->join('asset_unit au','au.id = ai.unit_id')
  98. ->where('ari.disposal_id',$id)
  99. ->field('ai.*,au.title as unitName')
  100. ->select();
  101. $data['items'] = $items?$items:[];
  102. $items = Db::name('asset_disposal_items')
  103. ->alias('ari')
  104. ->join('asset_items ai','ai.id = ari.items_id')
  105. ->join('asset_unit au','au.id = ai.unit_id')
  106. ->where('ari.disposal_id',$id)
  107. ->field('ai.*,au.title as unitName,ari.user_id as old_user_id,ari.dep_id as old_dep_id')
  108. ->select();
  109. $total = 0;
  110. foreach ($items as $k=>$v){
  111. $items[$k]['userName'] = Db::name('user')->where('id',$v['old_user_id'])->value('real_name');
  112. $items[$k]['depName'] = Db::name('dep')->where('id',$v['old_dep_id'])->value('title');
  113. $total +=$v['price'];
  114. }
  115. $data['items'] = $items?$items:[];
  116. $data['total'] = $total;
  117. return $data;
  118. }
  119. }