BudgetApply.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. use think\response\Json;
  7. class BudgetApply extends Model
  8. {
  9. public function advancedStartBudgetApply($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. $values = [];
  20. foreach ($formJson as $k=>$v){
  21. if($v['componentName'] == 'ddbudgetfield'){
  22. foreach ($v['components'] as $key=>$val){
  23. switch ($val['idx']){
  24. case '0':
  25. $values = json_decode($val['values'],true);
  26. break;
  27. }
  28. }
  29. break;
  30. }
  31. }
  32. $items = $values['items'];
  33. $data['budget_id'] = $items[0]['budgetId'];
  34. $data['company_id'] = $items[0]['companyId'];
  35. $ret = Db::name('budget_apply')->insert($data);
  36. return $ret?true:false;
  37. }
  38. public function advancedEndBudgetApply($id,$orgId,$userId,$formJson){
  39. // 生成部门预算记录
  40. $values = [];
  41. $formJson = json_decode($formJson,true);
  42. foreach ($formJson as $k=>$v){
  43. if($v['componentName'] == 'ddbudgetfield'){
  44. foreach ($v['components'] as $key=>$val){
  45. switch ($val['idx']){
  46. case '0':
  47. $values = json_decode($val['values'],true);
  48. break;
  49. }
  50. }
  51. break;
  52. }
  53. }
  54. $items = $values['items'];
  55. $apply = Db::name('budget_apply')->where('apply_id',$id)->find();
  56. $curTime = date('Y-m-d H:i:s');
  57. $depTotal = Db::name('budget_dep_total')
  58. ->where('dep_id',$apply['dep_id'])
  59. ->where('org_id',$orgId)
  60. ->where('budget_id',$apply['budget_id'])
  61. ->where('company_id',$apply['company_id'])
  62. ->find();
  63. if(!$depTotal){
  64. $ret = Db::name('budget_dep_total')->insertGetId([
  65. 'org_id' => $orgId,
  66. 'dep_id' => $apply['dep_id'],
  67. 'budget_id' => $apply['budget_id'],
  68. 'company_id' => $apply['company_id'],
  69. 'create_time' => $curTime
  70. ]);
  71. if(!$ret){
  72. return false;
  73. }
  74. }
  75. foreach ($items as $k=>$v){
  76. $sdata = [
  77. 'org_id' => $orgId,
  78. 'dep_id' => $apply['dep_id'],
  79. 'budget_id' => $apply['budget_id'],
  80. 'company_id' => $apply['company_id'],
  81. 'items_id' => $v['id'],
  82. 'money' => floatval($v['money']),
  83. 'money2' => !empty($v['money2'])?floatval($v['money2']):0,
  84. 'money3' => !empty($v['money3'])?floatval($v['money3']):0,
  85. 'intro' => !empty($v['intro'])?$v['intro']:'',
  86. 'create_time' => $curTime
  87. ];
  88. $info = Db::name('budget_dep')
  89. ->where('dep_id',$apply['dep_id'])
  90. ->where('org_id',$orgId)
  91. ->where('del',0)
  92. ->where('budget_id',$apply['budget_id'])
  93. ->where('company_id',$apply['company_id'])
  94. ->where('items_id',$v['id'])
  95. ->find();
  96. if($info){ // 存在金额累加
  97. $res = Db::name('budget_dep')->where('id',$info['id'])->update([
  98. 'money' => $sdata['money'] + $info['money'],
  99. 'money2' => $sdata['money2'] + $info['money2'],
  100. 'money3' => $sdata['money3'] + $info['money3'],
  101. 'update_time' => $curTime
  102. ]);
  103. if(!$res){
  104. return false;
  105. }
  106. }else{ // 不存在新增
  107. $res = Db::name('budget_dep')->insert($sdata);
  108. if(!$res){
  109. return false;
  110. }
  111. }
  112. }
  113. $data = [
  114. 'update_time' => date('Y-m-d H:i:s'),
  115. 'status' => 1
  116. ];
  117. $ret = Db::name('budget_apply')->where('apply_id',$id)->update($data);
  118. return $ret?true:false;
  119. }
  120. public function advancedDisagreeBudgetApply($id){
  121. $data = [
  122. 'update_time' => date('Y-m-d H:i:s'),
  123. 'status' => 2
  124. ];
  125. $ret = Db::name('budget_apply')->where('apply_id',$id)->update($data);
  126. return $ret?true:false;
  127. }
  128. public function advancedDelBudgetApply($id){
  129. $info = Db::name('budget_apply')->where('apply_id',$id)->find();
  130. $curTime = date('Y-m-d H:i:s');
  131. $data = [
  132. 'update_time' => $curTime,
  133. 'status' => 2
  134. ];
  135. $ret = Db::name('budget_apply')->where('apply_id',$id)->update($data);
  136. if($ret && $info['status'] == 1){
  137. $formJson = Db::name('apply')->where('id',$info['apply_id'])->value('form_json');
  138. // 删除生成的部门预算记录
  139. $values = [];
  140. $formJson = json_decode($formJson,true);
  141. foreach ($formJson as $k=>$v){
  142. if($v['componentName'] == 'ddbudgetfield'){
  143. foreach ($v['components'] as $key=>$val){
  144. switch ($val['idx']){
  145. case '0':
  146. $values = json_decode($val['values'],true);
  147. break;
  148. }
  149. }
  150. break;
  151. }
  152. }
  153. $items = $values['items'];
  154. foreach ($items as $k=>$v){
  155. $money = floatval($v['money']);
  156. $money2 = !empty($v['money2'])?floatval($v['money2']):0;
  157. $money3 = !empty($v['money3'])?floatval($v['money3']):0;
  158. //检查是否存在以前人的预算,与确认的无法删除
  159. $info = Db::name('budget_dep')
  160. ->where('dep_id',$info['dep_id'])
  161. ->where('org_id',$info['org_id'])
  162. ->where('del',0)
  163. ->where('budget_id',$info['budget_id'])
  164. ->where('company_id',$info['company_id'])
  165. ->where('items_id',$v['id'])
  166. ->find();
  167. if($info){ // 存在金额累加
  168. if($info['status'] == 1){
  169. $this->error = "预算项已确认,无法删除";
  170. return false;
  171. }
  172. $res = Db::name('budget_dep')->where('id',$info['id'])->update([
  173. 'money' => $info['money'] - $money,
  174. 'money2' => $info['money2'] - $money2,
  175. 'money3' => $info['money3'] - $money3,
  176. 'update_time' => $curTime
  177. ]);
  178. if(!$res){
  179. $this->error = "操作失败";
  180. return false;
  181. }
  182. }
  183. }
  184. }
  185. if(!$ret){
  186. $this->error = "操作失败";
  187. }
  188. return $ret?true:false;
  189. }
  190. public function lists($page,$size,$budgetId,$companyId,$orgId){
  191. $map[] = ['status','=',1];
  192. $map[] = ['org_id','=',$orgId];
  193. if($budgetId > 0){
  194. $map[] = ['budget_id','=',$budgetId];
  195. }
  196. if($companyId > 0){
  197. $map[] = ['company_id','=',$companyId];
  198. }
  199. $lists = Db::name('budget_apply')
  200. ->where($map)
  201. ->page($page,$size)
  202. ->order('id desc')
  203. ->select();
  204. $lists = $lists?$lists:[];
  205. foreach ($lists as $k=>$v){
  206. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  207. $lists[$k]['budgetName'] = Db::name('budget')->where('id',$v['budget_id'])->value('title');
  208. $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
  209. $lists[$k]['companyName'] = Db::name('company')->where('id',$v['company_id'])->value('title');
  210. }
  211. $total = Db::name('budget_apply')->where($map)->count();
  212. $data = [
  213. 'total' => $total,
  214. 'list' => $lists?$lists:[]
  215. ];
  216. return $data;
  217. }
  218. }