BudgetItems.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class BudgetItems extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'title' => input('title','','trim'),
  12. 'org_id' => input('orgId/d',0),
  13. 'company_id' => input('companyId/d',0),
  14. 'remark' => input('remark','','trim'),
  15. 'sorts' => input('sorts/d',0)
  16. ];
  17. $result = validate('BudgetItems')->check($data,[],'');
  18. if(true !== $result){
  19. HelpHander::error(validate('BudgetItems')->getError());
  20. }
  21. $id = $data['id'];
  22. unset($data['id']);
  23. if($id > 0){
  24. $data['update_time'] = date('Y-m-d H:i:s');
  25. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  26. }else{
  27. $data['create_time'] = date('Y-m-d H:i:s');
  28. $ret = $this->allowField(true)->save($data);
  29. }
  30. if(!$ret){
  31. HelpHander::error('操作失败');
  32. }
  33. return true;
  34. }
  35. public function info($id){
  36. $info = $this->where('id',$id)->find();
  37. if(!$info){
  38. HelpHander::error('数据不存在');
  39. }
  40. return $info->toArray();
  41. }
  42. public function lists($page,$size,$companyId,$title,$orgId){
  43. $map[] = ['del','=',0];
  44. $map[] = ['org_id','=',$orgId];
  45. if($companyId > 0){
  46. $map[] = ['company_id','=',$companyId];
  47. }
  48. if($title){
  49. $map[] = ['title','like','%'.$title.'%'];
  50. }
  51. $lists = $this
  52. ->where($map)
  53. ->page($page,$size)
  54. // ->order('id desc')
  55. ->order('sorts asc,id asc')
  56. ->select();
  57. $lists = $lists?$lists->toArray():[];
  58. foreach ($lists as $k=>$v){
  59. $lists[$k]['companyName'] = Db::name('company')->where('id',$v['company_id'])->value('title');
  60. }
  61. $total = $this->where($map)->count();
  62. $data = [
  63. 'total' => $total,
  64. 'list' => $lists
  65. ];
  66. return $data;
  67. }
  68. public function newlists($companyId,$orgId){
  69. $curTime = date('Y-m-d');
  70. $budget = Db::name('budget')
  71. ->where('del',0)
  72. ->where('enable',1)
  73. ->where('org_id',$orgId)
  74. ->where('start_time','<=',$curTime)
  75. ->where('end_time','>=',$curTime)
  76. ->order('id desc')
  77. ->find();
  78. $data = [];
  79. if($budget){
  80. $map[] = ['budget_id','=',$budget['id']];
  81. if($companyId > 0){
  82. $map[] = ['company_id','=',$companyId];
  83. }
  84. $lists = $this
  85. ->where($map)
  86. ->order('sorts asc,id asc')
  87. ->select();
  88. $data = [
  89. 'title' => $budget['title'],
  90. 'items' => $lists?$lists->toArray():[]
  91. ];
  92. }
  93. return $data;
  94. }
  95. public function newbudget($orgId,$userId){
  96. $deps = model('Dep')->getUserDep($userId,$orgId);
  97. if(empty($deps)){
  98. HelpHander::error('当前时间无法提交预算申请');
  99. }
  100. $depids = [];
  101. foreach ($deps as $k=>$v){
  102. $depids[] = $v['id'];
  103. }
  104. $curTime = date('Y-m-d');
  105. // $budget = Db::name('budget_auth')
  106. // ->alias('bd')
  107. // ->join('budget b','b.id = bd.budget_id')
  108. // ->where('b.del',0)
  109. // ->where('b.enable',1)
  110. // ->where('b.org_id',$orgId)
  111. // ->where('b.start_time','<=',$curTime)
  112. // ->where('b.end_time','>=',$curTime)
  113. // ->where('bd.dep_id','in',$depids)
  114. // ->field('b.*')
  115. // ->order('b.id desc')
  116. // ->find();
  117. $budget = Db::name('budget')
  118. ->where('del',0)
  119. ->where('enable',1)
  120. ->where('org_id',$orgId)
  121. ->where('start_time','<=',$curTime)
  122. ->where('end_time','>=',$curTime)
  123. ->order('id desc')
  124. ->find();
  125. if(!$budget){
  126. HelpHander::error('当前时间无法提交预算申请');
  127. }
  128. // 检查是否有权限提交
  129. $baret = Db::name('budget_auth')
  130. ->where('type',2)
  131. ->where('dep_id',$userId)
  132. ->where('budget_id',$budget['id'])
  133. ->find();
  134. $badret = Db::name('budget_auth')
  135. ->where('type',0)
  136. ->where('dep_id','in',$depids)
  137. ->where('budget_id',$budget['id'])
  138. ->find();
  139. if(!$baret && !$badret){
  140. HelpHander::error('当前时间无权限提交预算申请');
  141. }
  142. $items = $budget['items']?explode(',',$budget['items']):[];
  143. $company = Db::name('company')->where('org_id',$orgId)->field('id,title')->select();
  144. foreach ($company as $k=>$v){
  145. $lists = Db::name('budget_items')
  146. ->where('id','in',$items)
  147. ->where('company_id',$v['id'])
  148. ->where('org_id',$orgId)
  149. // ->order('id desc')
  150. ->order('sorts asc,id asc')
  151. ->select();
  152. $lists = $lists?$lists:[];
  153. foreach ($lists as $kk=>$vv){
  154. $lists[$kk]['budget_id'] = $budget['id'];
  155. }
  156. $company[$k]['items'] = $lists?$lists:[];
  157. }
  158. $data = [
  159. 'title' => $budget['title'],
  160. 'year' => $budget['year'],
  161. 'id' => $budget['id'],
  162. 'company' => $company?$company:[]
  163. ];
  164. return $data;
  165. }
  166. public function del($id){
  167. $info = Db::name('budget_dep')->where('del',0)->where('items_id',$id)->find();
  168. if($info){
  169. HelpHander::error('已被使用无法删除');
  170. }
  171. $ret = Db::name('budget_items')->delete($id);
  172. if(!$ret){
  173. HelpHander::error('删除失败');
  174. }
  175. return true;
  176. }
  177. public function curLists($orgId){
  178. $curTime = date('Y-m-d H:i:s');
  179. $lists = Db::name('budget_items')
  180. ->alias('bi')
  181. ->join('budget b','b.id = bi.budget_id')
  182. ->where('b.del',0)
  183. ->where('b.start_time','elt',$curTime)
  184. ->where('b.end_time','egt',$curTime)
  185. ->where('b.org_id',$orgId)
  186. ->field('bi.id,bi.title,b.year')
  187. ->order('bi.sorts asc,bi.id asc')
  188. ->select();
  189. $lists = $lists?$lists:[];
  190. foreach ($lists as $k=>$v){
  191. $lists[$k]['title'] = '['.$v['year'].']'.$v['title'];
  192. unset($lists[$k]['year']);
  193. }
  194. return $lists;
  195. }
  196. public function all($orgId){
  197. $company = Db::name('company')->field('id,title')->select();
  198. foreach ($company as $k=>$v){
  199. $items = Db::name('budget_items')
  200. ->where('company_id',$v['id'])
  201. ->where('org_id',$orgId)
  202. ->where('del',0)
  203. ->field('id,title')
  204. ->order('sorts asc,id asc')
  205. ->select();
  206. $company[$k]['items'] = $items?$items:[];
  207. }
  208. return $company;
  209. }
  210. }