AssetReceive.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 AssetReceive extends Model
  8. {
  9. public function advancedStartAssetReceive($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'] == 'ddassetfield'){
  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_receive')->insertGetId($data);
  41. // 减商品库存
  42. $sitems = [];
  43. foreach ($items as $k=>$v){
  44. $sitems[] = [
  45. 'receive_id' => $receiveId,
  46. 'items_id' => $v['id'],
  47. 'nums' => $v['snums']
  48. ];
  49. }
  50. $count = Db::name('asset_receive_items')->insertAll($sitems);
  51. if($count != count($sitems)){
  52. return false;
  53. }
  54. return true;
  55. }
  56. public function advancedEndAssetReceive($id,$orgId,$userId,$formJson){
  57. $data = [
  58. 'update_time' => date('Y-m-d H:i:s'),
  59. 'status' => 1
  60. ];
  61. $info = Db::name('asset_receive')->where('apply_id',$id)->find();
  62. if(!$info || $info['status'] != 0){
  63. return false;
  64. }
  65. $ret = Db::name('asset_receive')->where('apply_id',$id)->update($data);
  66. if(!$ret){
  67. return false;
  68. }
  69. // 添加使用者和使用部门
  70. $itemsids = Db::name('asset_receive_items')->where('receive_id',$info['id'])->column('items_id');
  71. Db::name('asset_items')->where('id','in',$itemsids)->update([
  72. 'enable' => 2,
  73. 'dep_id' => $info['dep_id'],
  74. 'user_id' => $info['user_id'],
  75. 'update_time' => date('Y-m-d H:i:s')
  76. ]);
  77. return $ret?true:false;
  78. }
  79. public function advancedDisagreeAssetReceive($id){
  80. $data = [
  81. 'update_time' => date('Y-m-d H:i:s'),
  82. 'status' => 2
  83. ];
  84. $ret = Db::name('asset_receive')->where('apply_id',$id)->update($data);
  85. if(!$ret){
  86. return false;
  87. }
  88. return true;
  89. }
  90. public function lists($page,$size,$title,$start,$end,$orgId){
  91. $map[] = ['or.org_id','=',$orgId];
  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_receive')
  100. ->alias('or')
  101. ->join('user_info ui','ui.user_id = or.receive_user_id')
  102. ->join('dep d','d.id = or.receive_dep_id')
  103. ->field('or.*,ui.name as user_name,d.name as dep_name')
  104. ->where($map)
  105. ->page($page,$size)
  106. ->order('or.id desc')
  107. ->select();
  108. // $lists = $lists?$lists:[];
  109. // foreach ($lists as $k=>$v){
  110. // $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
  111. // }
  112. $total = Db::name('asset_receive')
  113. ->alias('or')
  114. ->join('user_info ui','ui.user_id = or.receive_user_id')
  115. ->join('dep d','d.id = or.receive_dep_id')
  116. ->where($map)->count();
  117. $data = [
  118. 'total' => $total,
  119. 'list' => $lists
  120. ];
  121. return $data;
  122. }
  123. public function add(){
  124. $data = [
  125. 'id' => input('id/d',0),
  126. 'org_id' => input('orgId/d',0),
  127. 'receive_user_id' => input('receiveUserId/f',0),
  128. 'receive_dep_id' => input('receiveDepId/d',0),
  129. 'address' => input('address','','trim'),
  130. 'used' => input('used','','trim'),
  131. 'remark' => input('remark','','trim'),
  132. 'files' => input('files','','trim'),
  133. 'items' => input('items','','trim')
  134. ];
  135. $result = validate('AssetReceive')->check($data,[],'');
  136. if(true !== $result){
  137. HelpHander::error(validate('AssetReceive')->getError());
  138. }
  139. $id = $data['id'];
  140. unset($data['id']);
  141. if($id > 0){
  142. $data['update_time'] = date('Y-m-d H:i:s');
  143. unset($data['items']);
  144. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  145. if(!$ret){
  146. HelpHander::error('操作失败');
  147. }
  148. }else{
  149. $data['create_time'] = date('Y-m-d H:i:s');
  150. Db::startTrans();
  151. try{
  152. $data['sn'] = $this->getSn($data['org_id']);
  153. $items = json_decode($data['items'],true);
  154. unset($data['items']);
  155. $receiveId = Db::name('AssetReceive')->insertGetId($data);
  156. if(!$receiveId){
  157. \exception('操作失败');
  158. }
  159. $d = [];
  160. $itemids = [];
  161. foreach ($items as $k=>$v){
  162. $d[] = [
  163. 'receive_id' => $receiveId,
  164. 'items_id' => $v['id'],
  165. ];
  166. $itemids[] = $v['id'];
  167. }
  168. $res = Db::name('asset_receive_items')->insertAll($d);
  169. if($res != count($d)){
  170. \exception('操作失败');
  171. }
  172. $sd = [
  173. 'address' => $data['address'],
  174. 'used' => $data['used'],
  175. 'enable' => 2,
  176. 'user_id' => $data['receive_user_id'],
  177. 'dep_id' => $data['receive_dep_id'],
  178. 'update_time' => date('Y-m-d H:i:s')
  179. ];
  180. $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
  181. if($res != count($itemids)){
  182. \exception('操作失败');
  183. }
  184. Db::commit();
  185. }catch (Exception $e){
  186. Db::rollback();
  187. HelpHander::error($e->getMessage());
  188. }
  189. }
  190. return true;
  191. }
  192. public function getSn($orgId){
  193. $max = Db::name('asset_receive')
  194. ->where('org_id',$orgId)
  195. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  196. ->order('id desc')
  197. ->value('sn');
  198. $maxinit = $max?$max:date('Ymd').'0000';
  199. return $maxinit + 1;
  200. }
  201. public function info($id){
  202. $info = $this->where('id',$id)->find();
  203. if(!$info){
  204. HelpHander::error('数据不存在');
  205. }
  206. $data = $info->toArray();
  207. $data['depName'] = Db::name('dep')->where('id',$info['receive_dep_id'])->value('name');
  208. $data['userName'] = Db::name('user_info')->where('user_id',$info['receive_user_id'])->value('name');
  209. $data['items'] = Db::name('asset_receive_items')
  210. ->alias('ari')
  211. ->join('asset_items ai','ai.id = ari.items_id')
  212. ->join('asset_unit au','au.id = ai.unit_id')
  213. ->where('ari.receive_id',$id)
  214. ->field('ai.*,au.title as unitName')
  215. ->select();
  216. return $data;
  217. }
  218. }