commit
d4e438ea39
@ -3,6 +3,7 @@
|
|||||||
namespace App\Exports;
|
namespace App\Exports;
|
||||||
|
|
||||||
use App\Models\Log;
|
use App\Models\Log;
|
||||||
|
use App\Utils\DateTimeUtils;
|
||||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||||
use App\Models\GoodsSku;
|
use App\Models\GoodsSku;
|
||||||
@ -20,8 +21,8 @@ class GoodsSkusExport implements FromCollection, ShouldAutoSize
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
public function collection()
|
public function collection()
|
||||||
{
|
{
|
||||||
return new Collection($this->data);
|
return new Collection($this->data);
|
||||||
@ -36,24 +37,47 @@ class GoodsSkusExport implements FromCollection, ShouldAutoSize
|
|||||||
'商品品牌',
|
'商品品牌',
|
||||||
'规格编码',
|
'规格编码',
|
||||||
'规格名称',
|
'规格名称',
|
||||||
'成本',
|
|
||||||
'库存',
|
|
||||||
];
|
];
|
||||||
$inventoryTime = strtotime(date('Y-m-d 07:00:00'));
|
$map = [
|
||||||
$ids = Log::query()->where('target_type', 'goods_sku')
|
'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('target_field', $this->type)
|
||||||
->where('created_at', '>', $inventoryTime)
|
->where('created_at', '>', $day)
|
||||||
->pluck('target_id')
|
->orderBy('id', 'asc')
|
||||||
->toArray();
|
->get();
|
||||||
$data = GoodsSku::query()
|
foreach ($logs as $log) {
|
||||||
|
if ($log['before_update'] != $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) {
|
->when($ids, function ($query, $ids) {
|
||||||
return $query->whereIn('id', $ids);
|
return $query->whereIn('id', $ids);
|
||||||
})
|
})
|
||||||
->with(['goods' => function ($query) {
|
->with(['goods' => function ($query) {
|
||||||
$query->with(['type:id,name', 'brand:id,name']);
|
$query->with(['type:id,name', 'brand:id,name']);
|
||||||
}])
|
}]);
|
||||||
->get()
|
if ('inventory' === $this->type) {
|
||||||
->toArray();
|
$model->where('stock', '<>', 0);
|
||||||
|
$model->with(['daily' => function ($query) use ($day) {
|
||||||
|
$query->where('day', $day);
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
$data = $model->get()->toArray();
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
return [$headTitle];
|
return [$headTitle];
|
||||||
}
|
}
|
||||||
@ -65,8 +89,15 @@ class GoodsSkusExport implements FromCollection, ShouldAutoSize
|
|||||||
$arr[3] = $item['goods']['brand']['name'];
|
$arr[3] = $item['goods']['brand']['name'];
|
||||||
$arr[4] = $item['sku_code'];
|
$arr[4] = $item['sku_code'];
|
||||||
$arr[5] = $item['title'];
|
$arr[5] = $item['title'];
|
||||||
$arr[6] = $item['cost'];
|
if ('cost' === $this->type) {
|
||||||
$arr[7] = $item['stock'];
|
$arr[6] = $item['cost'];
|
||||||
|
$arr[7] = $update[$item['id']]['before_update'];
|
||||||
|
$arr[8] = $update[$item['id']]['after_update'];
|
||||||
|
}
|
||||||
|
if ('inventory' === $this->type) {
|
||||||
|
$arr[6] = $item['stock'];
|
||||||
|
$arr[7] = $item['daily']['inventory'];
|
||||||
|
}
|
||||||
$bodyData[] = $arr;
|
$bodyData[] = $arr;
|
||||||
}
|
}
|
||||||
unset($arr);
|
unset($arr);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user