mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-11-30 22:20:45 +00:00
107 lines
3.4 KiB
PHP
107 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Log;
|
|
use App\Utils\DateTimeUtils;
|
|
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 = [
|
|
'商品编码',
|
|
'商品名称',
|
|
'商品种类',
|
|
'商品品牌',
|
|
'规格编码',
|
|
'规格名称',
|
|
];
|
|
$map = [
|
|
'cost' => ['成本', '更新前成本', '更新后成本'],
|
|
'inventory' => ['库存', '盘点', '更新前盘点'],
|
|
];
|
|
$headTitle = array_merge($headTitle, $map[$this->type]);
|
|
$day = DateTimeUtils::getToday();
|
|
$update = [];
|
|
$logs = Log::query()
|
|
->select(['target_id', 'before_update', 'after_update'])
|
|
->where('target_type', 'goods_sku')
|
|
->where('target_field', $this->type)
|
|
->where('created_at', '>', $day)
|
|
->orderBy('id', 'asc')
|
|
->get();
|
|
foreach ($logs as $log) {
|
|
if ($log['before_update'] != $log['after_update'] || (int)$log['after_update']) {
|
|
if (!isset($update[$log['target_id']])) {
|
|
$update[$log['target_id']]['before_update'] = $log['before_update'];
|
|
}
|
|
$update[$log['target_id']]['after_update'] = $log['after_update'];
|
|
}
|
|
}
|
|
$ids = array_keys($update);
|
|
if (empty($ids)) {
|
|
return [$headTitle];
|
|
}
|
|
$model = GoodsSku::query()
|
|
->when($ids, function ($query, $ids) {
|
|
return $query->whereIn('id', $ids);
|
|
})
|
|
->with(['goods' => function ($query) {
|
|
$query->with(['type:id,name', 'brand:id,name']);
|
|
}]);
|
|
if ('inventory' === $this->type) {
|
|
$model->with(['daily' => function ($query) use ($day) {
|
|
$query->where('day', $day);
|
|
}]);
|
|
}
|
|
$data = $model->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['sku_code'];
|
|
$arr[5] = $item['title'];
|
|
if ('cost' === $this->type) {
|
|
$arr[6] = (string)$item['cost'];
|
|
$arr[7] = (string)$update[$item['id']]['before_update'];
|
|
$arr[8] = (string)$update[$item['id']]['after_update'];
|
|
}
|
|
if ('inventory' === $this->type) {
|
|
$arr[6] = (string)$item['stock'];
|
|
$arr[7] = (string)$item['daily']['inventory'];
|
|
$arr[8] = (string)$update[$item['id']]['before_update'];
|
|
}
|
|
$bodyData[] = $arr;
|
|
}
|
|
unset($arr);
|
|
return [$headTitle, $bodyData];
|
|
}
|
|
}
|