AssetWithdraw.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 AssetWithdraw extends Model
  8. {
  9. public function advancedStartAssetWithdraw($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. $items = [];
  20. foreach ($formJson as $k=>$v){
  21. if($v['componentName'] == 'ddassetwithdrawfield'){
  22. foreach ($v['components'] as $key=>$val){
  23. switch ($val['idx']){
  24. case '0':
  25. $items = json_decode($val['values'],true);
  26. break;
  27. case '1':
  28. $data['remark'] = $val['values'];
  29. break;
  30. }
  31. }
  32. break;
  33. }
  34. }
  35. $amount = 0;
  36. foreach ($items as $k=>$v){
  37. $amount += $v['snums']*$v['price'];
  38. }
  39. $data['amount'] = $amount;
  40. $receiveId = Db::name('asset_withdraw')->insertGetId($data);
  41. $sitems = [];
  42. foreach ($items as $k=>$v){
  43. $sitems[] = [
  44. 'withdraw_id' => $receiveId,
  45. 'items_id' => $v['id'],
  46. 'nums' => $v['snums']
  47. ];
  48. }
  49. $count = Db::name('asset_withdraw_items')->insertAll($sitems);
  50. if($count != count($sitems)){
  51. return false;
  52. }
  53. return true;
  54. }
  55. public function advancedEndAssetWithdraw($id,$orgId,$userId,$formJson){
  56. $data = [
  57. 'update_time' => date('Y-m-d H:i:s'),
  58. 'status' => 1
  59. ];
  60. $info = Db::name('asset_withdraw')->where('apply_id',$id)->find();
  61. if(!$info || $info['status'] != 0){
  62. return false;
  63. }
  64. $ret = Db::name('asset_withdraw')->where('apply_id',$id)->update($data);
  65. if(!$ret){
  66. return false;
  67. }
  68. // 删除使用者和使用部门
  69. $itemsids = Db::name('asset_withdraw_items')->where('withdraw_id',$info['id'])->column('items_id');
  70. Db::name('asset_items')->where('id','in',$itemsids)->update([
  71. 'enable' => 1,
  72. 'dep_id' => 0,
  73. 'user_id' => 0,
  74. 'update_time' => date('Y-m-d H:i:s')
  75. ]);
  76. return $ret?true:false;
  77. }
  78. public function advancedDisagreeAssetWithdraw($id){
  79. $data = [
  80. 'update_time' => date('Y-m-d H:i:s'),
  81. 'status' => 2
  82. ];
  83. $ret = Db::name('asset_withdraw')->where('apply_id',$id)->update($data);
  84. if(!$ret){
  85. return false;
  86. }
  87. return true;
  88. }
  89. public function lists($page,$size,$title,$start,$end,$orgId){
  90. $map[] = ['or.org_id','=',$orgId];
  91. // $map[] = ['or.status','=',1];
  92. if($title){
  93. $map[] = ['ui.name','like','%'.$title.'%'];
  94. }
  95. if($start&&$end){
  96. $map[] = ['or.create_time','egt',$start];
  97. $map[] = ['or.create_time','elt',$end];
  98. }
  99. $lists = Db::name('asset_withdraw')
  100. ->alias('or')
  101. ->join('user_info ui','ui.user_id = or.agent_user_id')
  102. ->field('or.*,ui.name as agent_user_name')
  103. ->where($map)
  104. ->page($page,$size)
  105. ->order('or.id desc')
  106. ->select();
  107. $lists = $lists?$lists:[];
  108. foreach ($lists as $k=>$v){
  109. $lists[$k]['agent_dep_name'] = Db::name('dep')->where('id',$v['agent_dep_id'])->value('name');
  110. $lists[$k]['with_dep_name'] = Db::name('dep')->where('id',$v['with_dep_id'])->value('name');
  111. }
  112. $total = Db::name('asset_withdraw')
  113. ->alias('or')
  114. ->join('user_info ui','ui.user_id = or.agent_user_id')
  115. ->where($map)->count();
  116. $data = [
  117. 'total' => $total,
  118. 'list' => $lists
  119. ];
  120. return $data;
  121. }
  122. public function add(){
  123. $data = [
  124. 'id' => input('id/d',0),
  125. 'org_id' => input('orgId/d',0),
  126. 'agent_user_id' => input('agentUserId/d',0),
  127. 'agent_dep_id' => input('agentDepId/d',0),
  128. 'with_dep_id' => input('withDepId/d',0),
  129. 'address' => input('address','','trim'),
  130. 'remark' => input('remark','','trim'),
  131. 'files' => input('files','','trim'),
  132. 'items' => input('items','','trim')
  133. ];
  134. $result = validate('AssetWithdraw')->check($data,[],'');
  135. if(true !== $result){
  136. HelpHander::error(validate('AssetWithdraw')->getError());
  137. }
  138. $id = $data['id'];
  139. unset($data['id']);
  140. if($id > 0){
  141. $data['update_time'] = date('Y-m-d H:i:s');
  142. unset($data['items']);
  143. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  144. if(!$ret){
  145. HelpHander::error('操作失败');
  146. }
  147. }else{
  148. $data['create_time'] = date('Y-m-d H:i:s');
  149. Db::startTrans();
  150. try{
  151. $data['sn'] = $this->getSn($data['org_id']);
  152. $items = json_decode($data['items'],true);
  153. unset($data['items']);
  154. $receiveId = Db::name('AssetWithdraw')->insertGetId($data);
  155. if(!$receiveId){
  156. \exception('操作失败');
  157. }
  158. $d = [];
  159. $itemids = [];
  160. foreach ($items as $k=>$v){
  161. $iteminfo = Db::name('asset_items')
  162. ->where('id',$v['id'])
  163. ->field('id,user_id,dep_id')
  164. ->find();
  165. $d[] = [
  166. 'withdraw_id' => $receiveId,
  167. 'items_id' => $v['id'],
  168. 'user_id' => $iteminfo['user_id'],
  169. 'dep_id' => $iteminfo['dep_id'],
  170. ];
  171. $itemids[] = $v['id'];
  172. }
  173. $res = Db::name('asset_withdraw_items')->insertAll($d);
  174. if($res != count($d)){
  175. \exception('操作失败');
  176. }
  177. $sd = [
  178. 'address' => $data['address'],
  179. 'enable' => 1,
  180. 'user_id' => 0,
  181. 'dep_id' => 0,
  182. 'update_time' => date('Y-m-d H:i:s')
  183. ];
  184. $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
  185. if($res != count($itemids)){
  186. \exception('操作失败');
  187. }
  188. Db::commit();
  189. }catch (Exception $e){
  190. Db::rollback();
  191. HelpHander::error($e->getMessage());
  192. }
  193. }
  194. return true;
  195. }
  196. public function getSn($orgId){
  197. $max = Db::name('asset_withdraw')
  198. ->where('org_id',$orgId)
  199. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  200. ->order('id desc')
  201. ->value('sn');
  202. $maxinit = $max?$max:date('Ymd').'0000';
  203. return $maxinit + 1;
  204. }
  205. public function info($id){
  206. $info = $this->where('id',$id)->find();
  207. if(!$info){
  208. HelpHander::error('数据不存在');
  209. }
  210. $data = $info->toArray();
  211. $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('name');
  212. $data['withDepName'] = Db::name('dep')->where('id',$info['with_dep_id'])->value('name');
  213. $data['agentUserName'] = Db::name('user_info')->where('user_id',$info['agent_user_id'])->value('name');
  214. $items = Db::name('asset_withdraw_items')
  215. ->alias('ari')
  216. ->join('asset_items ai','ai.id = ari.items_id')
  217. ->join('asset_unit au','au.id = ai.unit_id')
  218. ->where('ari.withdraw_id',$id)
  219. ->field('ai.*,au.title as unitName,ari.user_id as old_user_id,ari.dep_id as old_dep_id')
  220. ->select();
  221. foreach ($items as $k=>$v){
  222. $items[$k]['userName'] = Db::name('user_info')->where('user_id',$v['old_user_id'])->value('name');
  223. $items[$k]['depName'] = Db::name('dep')->where('id',$v['old_dep_id'])->value('name');
  224. }
  225. $data['items'] = $items?$items:[];
  226. return $data;
  227. }
  228. }