erp/app/Exports/GoodsSkusExport.php

76 lines
2.1 KiB
PHP
Raw Normal View History

2022-08-04 14:25:12 +08:00
<?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'));
2022-08-23 17:49:18 +08:00
$ids = Log::query()->where('target_type', 'goods_sku')
2022-08-04 14:25:12 +08:00
->where('target_field', $this->type)
->where('created_at', '>', $inventoryTime)
2022-08-23 17:49:18 +08:00
->pluck('target_id')
2022-08-04 14:25:12 +08:00
->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];
}
}