Budget.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 Budget extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'title' => input('title','','trim'),
  13. 'year' => input('year','','trim'),
  14. 'start_time' => input('startTime','','trim'),
  15. 'end_time' => input('endTime','','trim'),
  16. 'org_id' => input('orgId/d',0),
  17. 'content' => input('content','','trim'),
  18. 'items' => input('items','','trim'),
  19. 'auths' => input('auths','','trim'),
  20. ];
  21. $result = validate('Budget')->check($data,[],'');
  22. if(true !== $result){
  23. HelpHander::error(validate('Budget')->getError());
  24. }
  25. $id = $data['id'];
  26. unset($data['id']);
  27. Db::startTrans();
  28. try{
  29. $auths = $data['auths']?json_decode($data['auths'],true):[];
  30. unset($data['auths']);
  31. if($id > 0){
  32. $data['update_time'] = date('Y-m-d H:i:s');
  33. Db::name('budget_auth')->where('budget_id',$id)->delete();
  34. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  35. }else{
  36. $data['create_time'] = date('Y-m-d H:i:s');
  37. $ret = $this->allowField(true)->save($data);
  38. $id = $this->id;
  39. }
  40. if(!$ret){
  41. \exception('操作失败');
  42. }
  43. $arr = [];
  44. foreach ($auths as $k=>$v){
  45. $arr[] = [
  46. 'budget_id' => $id,
  47. 'dep_id' => $v['depId'],
  48. 'type' => $v['type']
  49. ];
  50. }
  51. $res = Db::name('budget_auth')->insertAll($arr);
  52. if($res != count($auths)){
  53. \exception('操作失败');
  54. }
  55. Db::commit();
  56. }catch (Exception $e){
  57. Db::rollback();
  58. HelpHander::error($e->getMessage());
  59. }
  60. return true;
  61. }
  62. public function info($id){
  63. $info = $this->where('id',$id)->find();
  64. if(!$info){
  65. HelpHander::error('数据不存在');
  66. }
  67. return $info->toArray();
  68. }
  69. public function lists($page,$size,$title,$orgId){
  70. $map[] = ['del','=',0];
  71. $map[] = ['org_id','=',$orgId];
  72. if($title != ''){
  73. $map[] = ['title','like','%'.$title.'%'];
  74. }
  75. $lists = $this
  76. ->where($map)
  77. ->page($page,$size)
  78. ->order('id desc')
  79. ->select();
  80. $lists = $lists?$lists->toArray():[];
  81. foreach ($lists as $k=>$v){
  82. /*$auths = Db::name('budget_auth')
  83. ->alias('ba')
  84. ->join('dep d','d.id = ba.dep_id')
  85. ->where('ba.budget_id',$v['id'])
  86. ->field('ba.dep_id,d.name')
  87. ->select();
  88. $auths = $auths?$auths:[];
  89. $depids = [];
  90. foreach ($auths as $kk=>$vv){
  91. $depids[] = $vv['dep_id'];
  92. }
  93. $lists[$k]['auths'] = implode(',',$depids);
  94. $lists[$k]['budget_auths'] = $auths;*/
  95. $auths = Db::name('budget_auth')
  96. ->where('budget_id',$v['id'])
  97. ->select();
  98. $nauth = [];
  99. foreach ($auths as $kk=>$vv){
  100. $nauth[] = [
  101. 'type' => $vv['type'],
  102. 'depId' => $vv['dep_id'],
  103. ];
  104. if($vv['type'] == 0){
  105. $auths[$kk]['name'] = Db::name('dep')->where('id',$vv['dep_id'])->value('name');
  106. }else{
  107. $auths[$kk]['name'] = Db::name('user_info')->where('user_id',$vv['dep_id'])->value('name');
  108. }
  109. }
  110. $lists[$k]['auths'] = $nauth?json_encode($nauth):'';
  111. $lists[$k]['budget_auths'] = $auths?$auths:[];
  112. }
  113. $total = $this->where($map)->count();
  114. $data = [
  115. 'total' => $total,
  116. 'list' => $lists
  117. ];
  118. return $data;
  119. }
  120. public function all($orgId){
  121. $map[] = ['del','=',0];
  122. $map[] = ['enable','=',1];
  123. $map[] = ['org_id','=',$orgId];
  124. $lists = $this
  125. ->where($map)
  126. ->field('id,title')
  127. ->order('id desc')
  128. ->select();
  129. return $lists?$lists->toArray():[];
  130. }
  131. public function allItems($orgId){
  132. $map[] = ['del','=',0];
  133. $map[] = ['enable','=',1];
  134. $map[] = ['org_id','=',$orgId];
  135. $lists = Db::name('budget')
  136. ->where($map)
  137. ->field('id,title,items')
  138. ->order('id desc')
  139. ->select();
  140. $lists = $lists?$lists:[];
  141. $company = Db::name('company')->select();
  142. foreach ($lists as $k=>$v){
  143. $itemList = [];
  144. if($v['items']){
  145. $items = explode(',',$v['items']);
  146. $itemList = Db::name('budget_items')->where('id','in',$items)->where('del',0)->field('id,title,company_id')->order('sorts asc')->select();
  147. }
  148. foreach ($company as $key=>$val){
  149. $itlist = [];
  150. foreach ($itemList as $kk=>$vv){
  151. if($vv['company_id'] == $val['id']){
  152. $itlist[] = $vv;
  153. }
  154. }
  155. $company[$key]['options'] = $itlist;
  156. }
  157. $lists[$k]['items'] = $company;
  158. }
  159. return $lists;
  160. }
  161. public function del($id){
  162. $info = Db::name('budget_apply')->where('status',1)->where('budget_id',$id)->find();
  163. if($info){
  164. HelpHander::error('已有申请,无法删除');
  165. }
  166. $ret = $this->where('id',$id)->setField('del',1);
  167. if(!$ret){
  168. HelpHander::error('删除失败');
  169. }
  170. return true;
  171. }
  172. public function changeEnable($id,$enable){
  173. $info = Db::name('budget')->where('id',$id)->where('del',0)->find();
  174. if(!$info){
  175. HelpHander::error('记录不存在');
  176. }
  177. if($enable == $info['enable']){
  178. HelpHander::error('已是当前状态,无需重复操作');
  179. }
  180. $ret = Db::name('budget')->where('id',$id)->setField('enable',$enable);
  181. if(!$ret){
  182. HelpHander::error('操作失败');
  183. }
  184. return true;
  185. }
  186. public function alllist($userId,$orgId){
  187. $deps = model('Dep')->getUserDep($userId,$orgId,1);
  188. if(!$deps){
  189. return [];
  190. }
  191. $depids = [];
  192. foreach ($deps as $k => $v) {
  193. $depids[] = $v['id'];
  194. }
  195. $depids = implode(',',$depids);
  196. $curYear = date('Y');
  197. $company = Db::name('company')->field('id,title')->select();
  198. foreach ($company as $k=>$v){
  199. $items = Db::name('budget_dep')
  200. ->alias('bd')
  201. ->join('budget b','b.id = bd.budget_id')
  202. ->join('budget_items bi','bi.id = bd.items_id')
  203. ->where('b.year',$curYear)
  204. ->where('bd.status',1)
  205. ->where('bd.del',0)
  206. ->where('bd.dep_id','in',$depids)
  207. ->where('bd.company_id',$v['id'])
  208. ->field('bi.title,bd.id,bd.dep_id')
  209. ->select();
  210. $company[$k]['items'] = $items?$items:[];
  211. }
  212. return $company;
  213. }
  214. }