123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AssetReceive extends Model
- {
- public function advancedStartAssetReceive($id,$orgId,$userId,$formJson,$extra){
- $formJson = json_decode($formJson,true);
- $data = [
- 'org_id' => $orgId,
- 'user_id' => $userId,
- 'apply_id' => $id,
- 'dep_id' => $extra['depId'],
- 'create_time' => date('Y-m-d H:i:s'),
- 'status' => 0,
- ];
- $items = [];
- foreach ($formJson as $k=>$v){
- if($v['componentName'] == 'ddassetfield'){
- foreach ($v['components'] as $key=>$val){
- switch ($val['idx']){
- case '0':
- $items = json_decode($val['values'],true);
- break;
- case '1':
- $data['remark'] = $val['values'];
- break;
- }
- }
- break;
- }
- }
- $amount = 0;
- foreach ($items as $k=>$v){
- $amount += $v['snums']*$v['price'];
- }
- $data['amount'] = $amount;
- $receiveId = Db::name('asset_receive')->insertGetId($data);
- // 减商品库存
- $sitems = [];
- foreach ($items as $k=>$v){
- $sitems[] = [
- 'receive_id' => $receiveId,
- 'items_id' => $v['id'],
- 'nums' => $v['snums']
- ];
- }
- $count = Db::name('asset_receive_items')->insertAll($sitems);
- if($count != count($sitems)){
- return false;
- }
- return true;
- }
- public function advancedEndAssetReceive($id,$orgId,$userId,$formJson){
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 1
- ];
- $info = Db::name('asset_receive')->where('apply_id',$id)->find();
- if(!$info || $info['status'] != 0){
- return false;
- }
- $ret = Db::name('asset_receive')->where('apply_id',$id)->update($data);
- if(!$ret){
- return false;
- }
- // 添加使用者和使用部门
- $itemsids = Db::name('asset_receive_items')->where('receive_id',$info['id'])->column('items_id');
- Db::name('asset_items')->where('id','in',$itemsids)->update([
- 'enable' => 2,
- 'dep_id' => $info['dep_id'],
- 'user_id' => $info['user_id'],
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- return $ret?true:false;
- }
- public function advancedDisagreeAssetReceive($id){
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 2
- ];
- $ret = Db::name('asset_receive')->where('apply_id',$id)->update($data);
- if(!$ret){
- return false;
- }
- return true;
- }
- public function lists($page,$size,$title,$start,$end,$orgId){
- $map[] = ['or.org_id','=',$orgId];
- if($title){
- $map[] = ['ui.name','like','%'.$title.'%'];
- }
- if($start&&$end){
- $map[] = ['or.create_time','egt',$start];
- $map[] = ['or.create_time','elt',$end];
- }
- $lists = Db::name('asset_receive')
- ->alias('or')
- ->join('user_info ui','ui.user_id = or.receive_user_id')
- ->join('dep d','d.id = or.receive_dep_id')
- ->field('or.*,ui.name as user_name,d.name as dep_name')
- ->where($map)
- ->page($page,$size)
- ->order('or.id desc')
- ->select();
- // $lists = $lists?$lists:[];
- // foreach ($lists as $k=>$v){
- // $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
- // }
- $total = Db::name('asset_receive')
- ->alias('or')
- ->join('user_info ui','ui.user_id = or.receive_user_id')
- ->join('dep d','d.id = or.receive_dep_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'org_id' => input('orgId/d',0),
- 'receive_user_id' => input('receiveUserId/f',0),
- 'receive_dep_id' => input('receiveDepId/d',0),
- 'address' => input('address','','trim'),
- 'used' => input('used','','trim'),
- 'remark' => input('remark','','trim'),
- 'files' => input('files','','trim'),
- 'items' => input('items','','trim')
- ];
- $result = validate('AssetReceive')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('AssetReceive')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- unset($data['items']);
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- Db::startTrans();
- try{
- $data['sn'] = $this->getSn($data['org_id']);
- $items = json_decode($data['items'],true);
- unset($data['items']);
- $receiveId = Db::name('AssetReceive')->insertGetId($data);
- if(!$receiveId){
- \exception('操作失败');
- }
- $d = [];
- $itemids = [];
- foreach ($items as $k=>$v){
- $d[] = [
- 'receive_id' => $receiveId,
- 'items_id' => $v['id'],
- ];
- $itemids[] = $v['id'];
- }
- $res = Db::name('asset_receive_items')->insertAll($d);
- if($res != count($d)){
- \exception('操作失败');
- }
- $sd = [
- 'address' => $data['address'],
- 'used' => $data['used'],
- 'enable' => 2,
- 'user_id' => $data['receive_user_id'],
- 'dep_id' => $data['receive_dep_id'],
- 'update_time' => date('Y-m-d H:i:s')
- ];
- $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
- if($res != count($itemids)){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- }
- return true;
- }
- public function getSn($orgId){
- $max = Db::name('asset_receive')
- ->where('org_id',$orgId)
- ->where('create_time','>=',date('Y-m-d').' 00:00:00')
- ->order('id desc')
- ->value('sn');
- $maxinit = $max?$max:date('Ymd').'0000';
- return $maxinit + 1;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $data = $info->toArray();
- $data['depName'] = Db::name('dep')->where('id',$info['receive_dep_id'])->value('name');
- $data['userName'] = Db::name('user_info')->where('user_id',$info['receive_user_id'])->value('name');
- $data['items'] = Db::name('asset_receive_items')
- ->alias('ari')
- ->join('asset_items ai','ai.id = ari.items_id')
- ->join('asset_unit au','au.id = ai.unit_id')
- ->where('ari.receive_id',$id)
- ->field('ai.*,au.title as unitName')
- ->select();
- return $data;
- }
- }
|