123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- use think\Request;
- class AssetReceive extends Model
- {
- public function updates(){
- $data = request()->post();
- $data['org_id'] = cur_org_id();
- $result = validate('AssetReceive')->check($data,[],'');
- if(true !== $result){
- $this->error = validate('AssetReceive')->getError();
- return false;
- }
- $data['receive_dep_id'] = Db::name('user_dep')
- ->where('user_id',$data['receive_user_id'])
- ->value('dep_id');
- $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){
- $this->error ='操作失败';
- return false;
- }
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- Db::startTrans();
- try{
- $data['sn'] = $this->getSn($data['org_id']);
- $items = $data['items'];
- unset($data['items']);
- $receiveId = Db::name('asset_receive')->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')
- ];
- $nums = 0;
- foreach ($itemids as $k=>$v){
- $res = Db::name('asset_items')->where('id',$v)->update($sd);
- if($res){
- $nums +=1;
- }
- }
- if($nums != count($itemids)){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- 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('title');
- $data['userName'] = Db::name('user')->where('id',$info['receive_user_id'])->value('real_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();
- $total = 0;
- foreach ($data['items'] as $k=>$v){
- $total +=$v['price'];
- }
- $data['countNums'] = count($data['items']);
- $data['total'] = $total;
- return $data;
- }
- }
|