0
0

AssetReceive.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. use think\Request;
  8. class AssetReceive extends Model
  9. {
  10. public function updates(){
  11. $data = request()->post();
  12. $data['org_id'] = cur_org_id();
  13. $result = validate('AssetReceive')->check($data,[],'');
  14. if(true !== $result){
  15. $this->error = validate('AssetReceive')->getError();
  16. return false;
  17. }
  18. $data['receive_dep_id'] = Db::name('user_dep')
  19. ->where('user_id',$data['receive_user_id'])
  20. ->value('dep_id');
  21. $id = $data['id'];
  22. unset($data['id']);
  23. if($id > 0){
  24. $data['update_time'] = date('Y-m-d H:i:s');
  25. unset($data['items']);
  26. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  27. if(!$ret){
  28. $this->error ='操作失败';
  29. return false;
  30. }
  31. }else{
  32. $data['create_time'] = date('Y-m-d H:i:s');
  33. Db::startTrans();
  34. try{
  35. $data['sn'] = $this->getSn($data['org_id']);
  36. $items = $data['items'];
  37. unset($data['items']);
  38. $receiveId = Db::name('asset_receive')->insertGetId($data);
  39. if(!$receiveId){
  40. \exception('操作失败');
  41. }
  42. $d = [];
  43. $itemids = [];
  44. foreach ($items as $k=>$v){
  45. $d[] = [
  46. 'receive_id' => $receiveId,
  47. 'items_id' => $v['id'],
  48. ];
  49. $itemids[] = $v['id'];
  50. }
  51. $res = Db::name('asset_receive_items')->insertAll($d);
  52. if($res != count($d)){
  53. \exception('操作失败');
  54. }
  55. $sd = [
  56. 'address' => $data['address'],
  57. 'used' => $data['used'],
  58. 'enable' => 2,
  59. 'user_id' => $data['receive_user_id'],
  60. 'dep_id' => $data['receive_dep_id'],
  61. 'update_time' => date('Y-m-d H:i:s')
  62. ];
  63. $nums = 0;
  64. foreach ($itemids as $k=>$v){
  65. $res = Db::name('asset_items')->where('id',$v)->update($sd);
  66. if($res){
  67. $nums +=1;
  68. }
  69. }
  70. if($nums != count($itemids)){
  71. \exception('操作失败');
  72. }
  73. Db::commit();
  74. }catch (Exception $e){
  75. Db::rollback();
  76. $this->error = $e->getMessage();
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. public function getSn($orgId){
  83. $max = Db::name('asset_receive')
  84. ->where('org_id',$orgId)
  85. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  86. ->order('id desc')
  87. ->value('sn');
  88. $maxinit = $max?$max:date('Ymd').'0000';
  89. return $maxinit + 1;
  90. }
  91. public function info($id){
  92. $info = $this->where('id',$id)->find();
  93. if(!$info){
  94. HelpHander::error('数据不存在');
  95. }
  96. $data = $info->toArray();
  97. $data['depName'] = Db::name('dep')->where('id',$info['receive_dep_id'])->value('title');
  98. $data['userName'] = Db::name('user')->where('id',$info['receive_user_id'])->value('real_name');
  99. $data['items'] = Db::name('asset_receive_items')
  100. ->alias('ari')
  101. ->join('asset_items ai','ai.id = ari.items_id')
  102. ->join('asset_unit au','au.id = ai.unit_id')
  103. ->where('ari.receive_id',$id)
  104. ->field('ai.*,au.title as unitName')
  105. ->select();
  106. $total = 0;
  107. foreach ($data['items'] as $k=>$v){
  108. $total +=$v['price'];
  109. }
  110. $data['countNums'] = count($data['items']);
  111. $data['total'] = $total;
  112. return $data;
  113. }
  114. }