mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-11-30 22:20:45 +00:00
feat: #20220804 新增导出
This commit is contained in:
parent
088c41cee9
commit
2dc1308675
75
app/Exports/GoodsSkusExport.php
Normal file
75
app/Exports/GoodsSkusExport.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use App\Models\Log;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
use App\Models\GoodsSku;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class GoodsSkusExport implements FromCollection, ShouldAutoSize
|
||||
{
|
||||
private $data;
|
||||
private $type;
|
||||
|
||||
public function __construct($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->data = $this->createData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
return new Collection($this->data);
|
||||
}
|
||||
|
||||
private function createData()
|
||||
{
|
||||
$headTitle = [
|
||||
'商品编码',
|
||||
'商品名称',
|
||||
'商品种类',
|
||||
'商品品牌',
|
||||
'规格编码',
|
||||
'规格名称',
|
||||
'成本',
|
||||
'库存',
|
||||
];
|
||||
$inventoryTime = strtotime(date('Y-m-d 07:00:00'));
|
||||
$ids = Log::query()->where('target_type', 'sku')
|
||||
->where('target_field', $this->type)
|
||||
->where('created_at', '>', $inventoryTime)
|
||||
->pluck('sku_id')
|
||||
->toArray();
|
||||
$data = GoodsSku::query()
|
||||
->when($ids, function ($query, $ids) {
|
||||
return $query->whereIn('id', $ids);
|
||||
})
|
||||
->with(['goods' => function ($query) {
|
||||
$query->with(['type:id,name', 'brand:id,name']);
|
||||
}])
|
||||
->get()
|
||||
->toArray();
|
||||
if (empty($data)) {
|
||||
return [$headTitle];
|
||||
}
|
||||
$bodyData = [];
|
||||
foreach ($data as $item) {
|
||||
$arr[0] = $item['goods']['goods_code'];
|
||||
$arr[1] = $item['goods']['title'];
|
||||
$arr[2] = $item['goods']['type']['name'];
|
||||
$arr[3] = $item['goods']['brand']['name'];
|
||||
$arr[4] = $item['goods']['sku_code'];
|
||||
$arr[5] = $item['goods']['title'];
|
||||
$arr[6] = $item['goods']['cost'];
|
||||
$arr[7] = $item['goods']['stock'];
|
||||
$bodyData[] = $arr;
|
||||
}
|
||||
unset($arr);
|
||||
return [$headTitle, $bodyData];
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Goods;
|
||||
|
||||
use App\Exports\GoodsSkusExport;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\GoodsRequest;
|
||||
use App\Http\Requests\GoodsSkuRequest;
|
||||
@ -340,4 +341,11 @@ class GoodsSkusController extends Controller
|
||||
|
||||
return response($this->res, $this->res['httpCode']);
|
||||
}
|
||||
|
||||
public function export(Request $request)
|
||||
{
|
||||
$type = $request->get('exportType');
|
||||
ob_end_clean();
|
||||
return Excel::download(new GoodsSkusExport($type), $type, '.xlsx');
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Goods\GoodsSkusController;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Web Routes
|
||||
@ -26,3 +28,5 @@ Route::get('/login', function () {
|
||||
Route::get('/register', function () {
|
||||
return view('welcome');
|
||||
})->name('register');
|
||||
|
||||
Route::get('goods_skus/export', [GoodsSkusController::class, 'export'])->name('goods_skus.export');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user