BudgetDep.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 BudgetDep extends Model
  8. {
  9. public function add(){ // 弃用
  10. $data = [
  11. 'items_id' => input('itemsId/d',0),
  12. 'money' => input('money/f',0),
  13. 'dep_id' => input('depId/d',0),
  14. 'org_id' => input('orgId/d',0),
  15. 'budget_id' => input('budgetId/d',0),
  16. 'intro' => input('intro','','trim')
  17. ];
  18. $userId = input('userId/d',0);
  19. $result = validate('BudgetDep')->check($data,[],'');
  20. if(true !== $result){
  21. HelpHander::error(validate('BudgetDep')->getError());
  22. }
  23. Db::startTrans();
  24. try{
  25. $companyId = Db::name('budget_items')
  26. ->where('id',$data['items_id'])
  27. ->value('company_id');
  28. $data['company_id'] = $companyId;
  29. $data['create_time'] = date('Y-m-d H:i:s');
  30. $data['update_time'] = date('Y-m-d H:i:s');
  31. $data['real_money'] = $data['money'];
  32. $data['confirm_money'] = $data['money'];
  33. $data['status'] = 1;
  34. $id = Db::name('budget_dep')->insertGetId($data);
  35. if(!$id){
  36. \exception('操作失败');
  37. }
  38. // 预算变化记录
  39. $d = [
  40. 'org_id' => $data['org_id'],
  41. 'user_id' => $userId,
  42. 'type' => 3,
  43. 'dep_id' => $data['dep_id'],
  44. 'money' => $data['money'],
  45. 'bus_id' => $id,
  46. 'content' => '添加预算'
  47. ];
  48. $ret = model('BudgetDepLog')->add($d);
  49. if(!$ret){
  50. \exception('操作失败');
  51. }
  52. Db::commit();
  53. }catch (Exception $e){
  54. Db::rollback();
  55. HelpHander::error('操作失败'.$e->getMessage());
  56. }
  57. return true;
  58. }
  59. // 确认实际预算
  60. public function confirmRealMoney(){
  61. $id = input('id/d',0);
  62. $real_money = input('realMoney/f',0);
  63. $orgId = input('orgId/d',0);
  64. $userId = input('userId/d',0);
  65. if($id <= 0||$real_money < 0){
  66. HelpHander::error('参数错误');
  67. }
  68. $info = Db::name('budget_dep')->where('id',$id)->find();
  69. if(!$info){
  70. HelpHander::error('记录不存在');
  71. }
  72. if($info['status'] != 0){
  73. HelpHander::error('已确认');
  74. }
  75. Db::startTrans();
  76. try{
  77. $ret = Db::name('budget_dep')->where('id',$id)->update([
  78. 'real_money' => $real_money,
  79. 'confirm_money' => $real_money,
  80. 'status' => 1,
  81. 'update_time' => date('Y-m-d H:i:s')
  82. ]);
  83. if(!$ret){
  84. \exception('操作失败');
  85. }
  86. // 预算变化记录
  87. $d = [
  88. 'org_id' => $orgId,
  89. 'user_id' => $userId,
  90. 'type' => 0,
  91. 'dep_id' => $info['dep_id'],
  92. 'money' => $real_money,
  93. 'bus_id' => $id,
  94. 'content' => '预算确认'
  95. ];
  96. $ret = model('BudgetDepLog')->add($d);
  97. if(!$ret){
  98. \exception('操作失败');
  99. }
  100. Db::commit();
  101. }catch (Exception $e){
  102. Db::rollback();
  103. HelpHander::error($e->getMessage());
  104. }
  105. return true;
  106. }
  107. public function updateRealMoney(){
  108. $type = input('type/d',0);
  109. $money = input('money/d',0);
  110. $id = input('id/d');
  111. $userId = input('userId/d',0);
  112. if($id <= 0||!in_array($type,[0,1])||$money <= 0){
  113. HelpHander::error('参数错误');
  114. }
  115. $info = $this->info($id);
  116. if($info['status'] != 1){
  117. HelpHander::error('该状态无法调整金额');
  118. }
  119. if($type == 0 && $money > $info['real_money']){ // 减少
  120. HelpHander::error('减少的金额必须小于剩余金额');
  121. }
  122. Db::startTrans();
  123. try{
  124. $sjmoney = $money;
  125. if($type == 0){
  126. $sjmoney = -$money;
  127. }
  128. $ret = Db::name('budget_dep')->where('id',$id)->update([
  129. 'confirm_money' => $info['confirm_money'] + $sjmoney,
  130. 'real_money' => $info['real_money'] + $sjmoney,
  131. 'update_time' => date('Y-m-d H:i:s')
  132. ]);
  133. if(!$ret){
  134. \exception('操作失败');
  135. }
  136. // 预算变化记录
  137. $d = [
  138. 'org_id' => $info['org_id'],
  139. 'user_id' => $userId,
  140. 'type' => $type == 0?2:3,
  141. 'dep_id' => $info['dep_id'],
  142. 'money' => $money,
  143. 'bus_id' => $id,
  144. 'content' => '预算修改'
  145. ];
  146. $ret = model('BudgetDepLog')->add($d);
  147. if(!$ret){
  148. \exception('操作失败');
  149. }
  150. Db::commit();
  151. }catch (Exception $e){
  152. Db::rollback();
  153. HelpHander::error('操作失败');
  154. }
  155. return true;
  156. }
  157. public function info($id){
  158. $info = $this->where('id',$id)->find();
  159. if(!$info){
  160. HelpHander::error('数据不存在');
  161. }
  162. return $info->toArray();
  163. }
  164. // 获取用户可用的部门
  165. public function getUserDep($userId,$orgId){
  166. $deps = model('Dep')->getUserDep($userId,$orgId,1);
  167. $depids = [];
  168. if($deps) {
  169. foreach ($deps as $k => $v) {
  170. $depids[] = $v['id'];
  171. }
  172. $map[] = ['bd.dep_id','in',$depids];
  173. }else{
  174. $map[] = ['bd.dep_id','=',0];
  175. }
  176. $map[] = ['bd.del','=',0];
  177. $map[] = ['bd.org_id','=',$orgId];
  178. $map[] = ['b.year','=',date('Y')];
  179. $map[] = ['bd.status','=',1];
  180. $lists = Db::name('budget_dep')
  181. ->alias('bd')
  182. ->join('dep d','d.id = bd.dep_id')
  183. ->join('budget b','b.id = bd.budget_id')
  184. ->where($map)
  185. ->field('d.id,d.name')
  186. ->group('d.id')
  187. ->distinct(true)
  188. ->order('d.sorts asc')
  189. ->select();
  190. return $lists?$lists:[];
  191. }
  192. public function lists($page,$size,$type,$year,$status,$title,$budgetId,$companyId,$userId,$orgId){
  193. $map[] = ['bd.del','=',0];
  194. $map[] = ['bd.org_id','=',$orgId];
  195. if($status >= 0){
  196. $map[] = ['bd.status','=',$status];
  197. }
  198. if($year){
  199. $map[] = ['b.year','=',$year];
  200. }
  201. if($title){
  202. $map[] = ['d.name','like','%'.$title.'%'];
  203. }
  204. if($budgetId > 0){
  205. $map[] = ['bd.budget_id','=',$budgetId];
  206. }
  207. if($companyId > 0){
  208. $map[] = ['bd.company_id','=',$companyId];
  209. }
  210. if($type == 1){ // 本部门
  211. $deps = model('Dep')->getUserDep($userId,$orgId,1);
  212. $depids = [];
  213. if($deps) {
  214. foreach ($deps as $k => $v) {
  215. $depids[] = $v['id'];
  216. }
  217. $map[] = ['bd.dep_id','in',$depids];
  218. }else{
  219. $map[] = ['bd.dep_id','=',0];
  220. }
  221. }
  222. $lists = Db::name('budget_dep')
  223. ->alias('bd')
  224. ->join('dep d','d.id = bd.dep_id')
  225. ->join('budget b','b.id = bd.budget_id')
  226. ->join('budget_items bi','bi.id = bd.items_id')
  227. ->where($map)
  228. ->field('bd.*,d.name as depName,b.title as budgetTitle,bi.title as itemsName')
  229. ->page($page,$size)
  230. ->order('b.year desc,bi.sorts asc,bd.id desc')
  231. ->select();
  232. $lists = $lists?$lists:[];
  233. foreach ($lists as $k=>$v){
  234. // $lists[$k]['itemsName'] = Db::name('budget_items')->where('id',$v['items_id'])->value('title');
  235. $lists[$k]['companyName'] = Db::name('company')->where('id',$v['company_id'])->value('title');
  236. }
  237. $total = Db::name('budget_dep')
  238. ->alias('bd')
  239. ->join('dep d','d.id = bd.dep_id')
  240. ->join('budget b','b.id = bd.budget_id')
  241. ->where($map)->count();
  242. $data = [
  243. 'total' => $total,
  244. 'list' => $lists
  245. ];
  246. return $data;
  247. }
  248. public function lists2($page,$size,$year,$title,$userId,$orgId){
  249. $map[] = ['bd.del','=',0];
  250. $map[] = ['bd.org_id','=',$orgId];
  251. if($year){
  252. $map[] = ['b.year','=',$year];
  253. }
  254. if($title){
  255. $map[] = ['d.name','like','%'.$title.'%'];
  256. }
  257. $lists = Db::name('budget_dep')
  258. ->alias('bd')
  259. ->join('dep d','d.id = bd.dep_id')
  260. ->join('budget b','b.id = bd.budget_id')
  261. ->where($map)
  262. ->field('bd.dep_id,bd.budget_id,d.name as depName,b.year')
  263. ->page($page,$size)
  264. ->group('bd.dep_id,bd.budget_id')
  265. ->order('b.year desc,bd.id desc')
  266. ->select();
  267. $lists = $lists?$lists:[];
  268. foreach ($lists as $k=>$v){
  269. $lists[$k]['money1'] = Db::name('budget_dep')
  270. ->where('del',0)
  271. ->where('dep_id',$v['dep_id'])
  272. ->where('budget_id',$v['budget_id'])
  273. ->where('company_id',1)
  274. ->sum('money');
  275. $lists[$k]['confirm_money1'] = Db::name('budget_dep')
  276. ->where('del',0)
  277. ->where('dep_id',$v['dep_id'])
  278. ->where('budget_id',$v['budget_id'])
  279. ->where('company_id',1)
  280. ->sum('confirm_money');
  281. $lists[$k]['real_money1'] = Db::name('budget_dep')
  282. ->where('del',0)
  283. ->where('dep_id',$v['dep_id'])
  284. ->where('budget_id',$v['budget_id'])
  285. ->where('company_id',1)
  286. ->sum('real_money');
  287. $lists[$k]['money2'] = Db::name('budget_dep')
  288. ->where('del',0)
  289. ->where('dep_id',$v['dep_id'])
  290. ->where('budget_id',$v['budget_id'])
  291. ->where('company_id',2)
  292. ->sum('money');
  293. $lists[$k]['confirm_money2'] = Db::name('budget_dep')
  294. ->where('del',0)
  295. ->where('dep_id',$v['dep_id'])
  296. ->where('budget_id',$v['budget_id'])
  297. ->where('company_id',2)
  298. ->sum('confirm_money');
  299. $lists[$k]['real_money2'] = Db::name('budget_dep')
  300. ->where('del',0)
  301. ->where('dep_id',$v['dep_id'])
  302. ->where('budget_id',$v['budget_id'])
  303. ->where('company_id',2)
  304. ->sum('real_money');
  305. }
  306. $total = Db::name('budget_dep')
  307. ->alias('bd')
  308. ->join('dep d','d.id = bd.dep_id')
  309. ->join('budget b','b.id = bd.budget_id')
  310. ->group('bd.dep_id,bd.budget_id')
  311. ->where($map)->count();
  312. $data = [
  313. 'total' => $total,
  314. 'list' => $lists
  315. ];
  316. return $data;
  317. }
  318. public function alllists($userId,$orgId){
  319. $map[] = ['bd.del','=',0];
  320. $map[] = ['bd.status','=',1];
  321. $map[] = ['bd.org_id','=',$orgId];
  322. $deps = model('Dep')->getUserDep($userId,$orgId);
  323. $depids = [];
  324. if($deps) {
  325. foreach ($deps as $k => $v) {
  326. $depids[] = $v['id'];
  327. }
  328. $map[] = ['bd.dep_id','in',$depids];
  329. }else{
  330. $map[] = ['dep_id','=',0];
  331. }
  332. $lists = Db::name('budget_dep')
  333. ->alias('bd')
  334. ->join('dep d','d.id = bd.dep_id')
  335. ->join('budget b','b.id = bd.budget_id')
  336. ->join('budget_items bi','bi.id = bd.budget_id')
  337. ->where($map)
  338. ->field('bd.id,bi.title,d.title as depName')
  339. ->order('bd.id desc')
  340. ->select();
  341. $lists = $lists?$lists:[];
  342. foreach ($lists as $k=>$v){
  343. $lists[$k]['title'] = $v['title'].'['.$v['depName'].']';
  344. }
  345. return $lists;
  346. }
  347. public function del($id){
  348. $ret = $this->where('id',$id)->setField('del',1);
  349. if(!$ret){
  350. HelpHander::error('删除失败');
  351. }
  352. return true;
  353. }
  354. }