我的代码
//入库数据
/* $nowtime1 = date('Y-m-d',time());
$nowtime2 = date('Y-m-d',strtotime('-1 month',time()));*/
$HospitalID=$request->header('HospitalID')?$request->header('HospitalID'):$request->input('HospitalID',1); //医院id
$AssetsName = $request->input('AssetsName');//设备名称
$AssetsID = $request->input('AssetsID');//设备ID
$start = $request->input('start');//统计时间
//$start = '2018-03-08';
$end = $request->input('end');//统计时间
//$end = '2018-06-09';
$limit=$request->input('Limit','10');//每页显示条数
$page=$request->input('page',1);//第几页
$DepartmentName = $request->input('DepartmentName');//科室名称
$isdc = $request->input('isdc');//是否导出
$data1 = DB::table('assetsacceptancet')
->select('assetsacceptancet.AssetsID','AssetDictionaryID','rkrq',DB::raw("count(assetsacceptancet.AssetsID)
as number"),'assetsacceptancet.AssetsPrice','AssetsNumber','AssetsModel','assetsacceptancet.AssetsName','Unit','DepartmentName')
//->leftjoin('assetsstoragedetailt','assetsstoragedetailt.AssetsID','=','assetsacceptancet.AssetsID')
//->leftjoin('assetsstoraget','assetsstoraget.AssetsStorageID','=','assetsstoragedetailt.AssetsStorageID')
->leftjoin('departmentt','departmentt.DepartmentID','=','assetsacceptancet.AffiliatedDepartmentID')
->leftjoin('assetsdictionariest','assetsdictionariest.AssetsName','=','assetsacceptancet.AssetsName')//资产字典表
->where('assetsacceptancet.HospitalID',$HospitalID)
->when($start,function($query)use($start){ //统计时间筛选
return $query->where('rkrq','>',$start);
})
->when($end,function($query)use($end){ //统计时间筛选
return $query->where('rkrq','<=',$end);
})
->when($AssetsName,function($query)use($AssetsName){ //设备名称筛选
return $query->where('assetsacceptancet.AssetsName',$AssetsName);
})
->when($AssetsID,function($query)use($AssetsID){ //设备ID筛选
return $query->where('assetsdictionariest.AssetsDictionariesID',$AssetsID);
})
->when($DepartmentName,function($query)use($DepartmentName){ //科室名称筛选
return $query->where('departmentt.DepartmentName',$DepartmentName);
})
->groupBy('AssetDictionaryID')
->get();
//dd($data1);
//出库数据
$data2 = DB::table('assetsacceptancet')
->select('AssetDictionaryID','ckrq',DB::raw("count(assetsacceptancet.AssetsID)
as number"),'DepartmentName')
//->leftjoin('assetsoutbounddetailt','assetsoutbounddetailt.AssetsCheckID','=','assetsacceptancet.AssetsID')
//->leftjoin('assetsoutboundt','assetsoutboundt.AssetsOutboundID','=','assetsoutbounddetailt.DepartmentOutBoundID')
->leftjoin('departmentt','departmentt.DepartmentID','=','assetsacceptancet.AffiliatedDepartmentID')
->leftjoin('assetsdictionariest','assetsdictionariest.AssetsName','=','assetsacceptancet.AssetsName')//资产字典表
->where('assetsacceptancet.HospitalID',$HospitalID)
->when($start,function($query)use($start){ //统计时间筛选
return $query->where('ckrq','>',$start);
})
->when($end,function($query)use($end){ //统计时间筛选
return $query->where('ckrq','<=',$end);
})
->when($AssetsName,function($query)use($AssetsName){ //设备名称筛选
return $query->where('assetsacceptancet.AssetsName',$AssetsName);
})
->when($AssetsID,function($query)use($AssetsID){ //设备ID筛选
return $query->where('assetsdictionariest.AssetsDictionariesID',$AssetsID);
})
->when($DepartmentName,function($query)use($DepartmentName){ //科室名称筛选
return $query->where('departmentt.DepartmentName',$DepartmentName);
})
->groupBy('AssetDictionaryID','ckrq')
->get();
//dd($data2);
//期初数据,入库时间小于开始时间,出库时间大于结束时间
$data3 = DB::table('assetsacceptancet')
->select('AssetDictionaryID',DB::raw("count(assetsacceptancet.AssetsID)
as number"))
//->leftjoin('assetsstoragedetailt','assetsstoragedetailt.AssetsID','=','assetsacceptancet.AssetsID')
//->leftjoin('assetsstoraget','assetsstoraget.AssetsStorageID','=','assetsstoragedetailt.AssetsStorageID')
//->leftjoin('assetsoutbounddetailt','assetsoutbounddetailt.AssetsCheckID','=','assetsacceptancet.AssetsID')
//->leftjoin('assetsoutboundt','assetsoutboundt.AssetsOutboundID','=','assetsoutbounddetailt.DepartmentOutBoundID')
->leftjoin('assetsdictionariest','assetsdictionariest.AssetsName','=','assetsacceptancet.AssetsName')//资产字典表
->where('assetsacceptancet.HospitalID',$HospitalID)
->when($start,function($query)use($start){ //统计时间筛选
return $query->where('rkrq','<=',$start)
->where('rkrq','<>','0000-00-00');
})
->when($end,function($query)use($end){ //统计时间筛选
return $query->Where(function ($query)use($end) {
$query->where('ckrq','>',$end)
->orwhere('ckrq','0000-00-00');
});
})
->when($AssetsName,function($query)use($AssetsName){ //设备名称筛选
return $query->where('assetsacceptancet.AssetsName',$AssetsName);
})
->when($AssetsID,function($query)use($AssetsID){ //设备ID筛选
return $query->where('assetsdictionariest.AssetsDictionariesID',$AssetsID);
})
->when($DepartmentName,function($query)use($DepartmentName){ //科室名称筛选
return $query->where('departmentt.DepartmentName',$DepartmentName);
})
->groupBy('AssetDictionaryID')
->get();
//dd($data3);
//遍历数组,拼接期初数据
$data1 = json_decode(json_encode($data1),true);
$data2 = json_decode(json_encode($data2),true);
$data3 = json_decode(json_encode($data3),true);
//dd($data1);
//dd($data3);
if($data3){
foreach ($data1 as $k1 => $v1) {
foreach ($data3 as $k2 => $v2) {
if($v1['AssetDictionaryID'] == $v2['AssetDictionaryID'])
{
$data1[$k1]['qcsl'] = $v2['number'];
continue 2;
}else{
$data1[$k1]['qcsl'] = 0;
}
}
}
}else{
foreach ($data1 as $k => $v) {
$data1[$k]['qcsl'] = 0;
}
}
//dd($data1);
//遍历数组,计算结存数量和金额,添加一个出库数量
foreach ($data1 as $k => $v) {
$data1[$k]['jcsl'] = $v['qcsl'] + $v['number'];
$data1[$k]['je'] = ($v['qcsl'] + $v['number'])*$v['AssetsPrice'];
$data1[$k]['rksl'] = $v['number'];
$data1[$k]['cksl'] = 0;
}
//dd($data1);
//遍历入库数据插进数据库
foreach ($data1 as $k => $v) {
$data4 = [
'sbzd_id'=>$v['AssetDictionaryID'],
'AssetsStorageDate'=>$v['rkrq'],
'rksl'=>$v['rksl'],
'cksl'=>$v['cksl'],
'qcsl'=>$v['qcsl'],
'jcsl'=>$v['jcsl'],
'je'=>$v['je'],
'xgsj'=>date('Y-m-d H:m:s',time()),
'AssetsPrice'=>$v['AssetsPrice'],
'bz'=>$v['DepartmentName'].'入库到后勤库房',
'AssetsNumber'=>$v['AssetsNumber'],
'AssetsModel'=>$v['AssetsModel'],
'AssetsName'=>$v['AssetsName'],
'Unit'=>$v['Unit']
];
DB::table('sbjc')->insert($data4);
//sleep(1);
}
//dd($data2);
//遍历出库数据插进数据库
foreach ($data2 as $k => $v) {
$id = $v['AssetDictionaryID'];
$data5 = DB::table('sbjc')
->where('sbzd_id',$id)
->orderBy('sbjc_id','desc')
->first();
$data5 = json_decode(json_encode($data5),true);
$data6 = [
'sbzd_id' => $data5['sbzd_id'],
'AssetsStorageDate'=>$data5['AssetsStorageDate'],
'rksl'=>0,
'cksl'=>$v['number'],
'qcsl'=>$data5['jcsl'],
'jcsl'=>$data5['jcsl']-$v['number'],
'je'=>($data5['jcsl']-$v['number'])*$data5['AssetsPrice'],
'xgsj'=>date('Y-m-d H:m:s',time()),
'AssetsPrice'=>$data5['AssetsPrice'],
'bz'=>'后勤库房出库到'.$v['DepartmentName'],
'AssetsNumber'=>$data5['AssetsNumber'],
'AssetsModel'=>$data5['AssetsModel'],
'AssetsName'=>$data5['AssetsName'],
'Unit'=>$data5['Unit']
];
DB::table('sbjc')->insert($data6);
}
//从数据库取出数据
$data7 = DB::table('sbjc')
->orderBy('sbzd_id')
->get();
$data7 = json_decode(json_encode($data7),true);
foreach ($data7 as $k => $v) {
$data7[$k]['pmgg'] = $v['AssetsName'].$v['AssetsModel'].'-'.$v['AssetsStorageDate'];
}
DB::table('sbjc')->truncate();
//dd($data7);
if($isdc == 1)
{
$header = [];
$header[] = ['代码','品名规格','单位','期初数量','入数量','出数量','结存数量','金额','备注'];
$Row = [];
foreach ($data7 as $v){
$Row[] = [$v['AssetsNumber'],$v['pmgg'],$v['Unit'],$v['qcsl'],$v['rksl'],$v['cksl'],$v['jcsl'],$v['je'],$v['bz'],];
}
$wjm = '库房台账';
$this->dcExcel($wjm,$header,$Row);die;
}
$total = count($data7);
$res2 = $this->page_array($limit,$page,$data7,0);
$jieguo['total'] = $total;
$jieguo['data'] = $res2;
return json_encode($jieguo);