2022-08-04 14:25:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Exports;
|
|
|
|
|
|
2022-10-12 15:26:38 +08:00
|
|
|
use App\Models\DailyStockRecord;
|
2022-08-04 14:25:12 +08:00
|
|
|
use App\Models\Log;
|
2022-10-12 15:26:38 +08:00
|
|
|
use App\Utils\ArrayUtils;
|
2022-09-02 16:23:11 +08:00
|
|
|
use App\Utils\DateTimeUtils;
|
2022-08-04 14:25:12 +08:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-02 16:23:11 +08:00
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
|
*/
|
2022-08-04 14:25:12 +08:00
|
|
|
public function collection()
|
|
|
|
|
{
|
|
|
|
|
return new Collection($this->data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function createData()
|
|
|
|
|
{
|
|
|
|
|
$headTitle = [
|
|
|
|
|
'商品编码',
|
|
|
|
|
'商品名称',
|
|
|
|
|
'商品种类',
|
|
|
|
|
'商品品牌',
|
|
|
|
|
'规格编码',
|
|
|
|
|
'规格名称',
|
|
|
|
|
];
|
2022-09-02 16:23:11 +08:00
|
|
|
$map = [
|
|
|
|
|
'cost' => ['成本', '更新前成本', '更新后成本'],
|
2022-10-12 15:26:38 +08:00
|
|
|
'inventory' => ['库存', '盘点', '上新'],
|
2022-09-02 16:23:11 +08:00
|
|
|
];
|
|
|
|
|
$headTitle = array_merge($headTitle, $map[$this->type]);
|
2022-10-12 15:26:38 +08:00
|
|
|
if ('cost' === $this->type) {
|
|
|
|
|
$update = $this->getChangeCostLogs();
|
|
|
|
|
}
|
|
|
|
|
if ('inventory' === $this->type) {
|
|
|
|
|
$update = $this->getInventoryRecord();
|
2022-09-02 16:23:11 +08:00
|
|
|
}
|
|
|
|
|
$ids = array_keys($update);
|
|
|
|
|
if (empty($ids)) {
|
|
|
|
|
return [$headTitle];
|
|
|
|
|
}
|
|
|
|
|
$model = GoodsSku::query()
|
2022-08-04 14:25:12 +08:00
|
|
|
->when($ids, function ($query, $ids) {
|
|
|
|
|
return $query->whereIn('id', $ids);
|
|
|
|
|
})
|
2023-04-18 11:22:16 +08:00
|
|
|
->where('is_combination', 0)
|
2022-08-04 14:25:12 +08:00
|
|
|
->with(['goods' => function ($query) {
|
2022-10-12 15:26:38 +08:00
|
|
|
$query->with(['type:id,name', 'brand:id,name'])
|
|
|
|
|
->orderBy('type_id')
|
|
|
|
|
->orderBy('brand_id');
|
2022-09-02 16:23:11 +08:00
|
|
|
}]);
|
|
|
|
|
$data = $model->get()->toArray();
|
2022-08-04 14:25:12 +08:00
|
|
|
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'];
|
2022-08-23 19:36:39 +08:00
|
|
|
$arr[4] = $item['sku_code'];
|
|
|
|
|
$arr[5] = $item['title'];
|
2022-09-02 16:23:11 +08:00
|
|
|
if ('cost' === $this->type) {
|
2022-09-02 17:05:50 +08:00
|
|
|
$arr[6] = (string)$item['cost'];
|
|
|
|
|
$arr[7] = (string)$update[$item['id']]['before_update'];
|
|
|
|
|
$arr[8] = (string)$update[$item['id']]['after_update'];
|
2022-09-02 16:23:11 +08:00
|
|
|
}
|
|
|
|
|
if ('inventory' === $this->type) {
|
2022-09-02 17:05:50 +08:00
|
|
|
$arr[6] = (string)$item['stock'];
|
2022-10-12 15:26:38 +08:00
|
|
|
$arr[7] = (string)$update[$item['id']]['inventory'];
|
|
|
|
|
$arr[8] = (string)$update[$item['id']]['arrived_today_num'];
|
2022-09-02 16:23:11 +08:00
|
|
|
}
|
2022-08-04 14:25:12 +08:00
|
|
|
$bodyData[] = $arr;
|
|
|
|
|
}
|
|
|
|
|
unset($arr);
|
|
|
|
|
return [$headTitle, $bodyData];
|
|
|
|
|
}
|
2022-10-12 15:26:38 +08:00
|
|
|
|
|
|
|
|
private function getChangeCostLogs()
|
|
|
|
|
{
|
|
|
|
|
$day = DateTimeUtils::getToday();
|
|
|
|
|
$logs = Log::query()
|
|
|
|
|
->select(['target_id', 'before_update', 'after_update'])
|
|
|
|
|
->where('target_type', 'goods_sku')
|
|
|
|
|
->where('target_field', 'cost')
|
|
|
|
|
->where('created_at', '>', $day)
|
|
|
|
|
->orderBy('id', 'asc')
|
|
|
|
|
->get();
|
|
|
|
|
$update = [];
|
|
|
|
|
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'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $update;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getInventoryRecord()
|
|
|
|
|
{
|
|
|
|
|
$day = date('Y-m-d', strtotime('-1 day'));
|
|
|
|
|
$records = DailyStockRecord::query()
|
|
|
|
|
->select(['sku_id', 'inventory', 'arrived_today_num'])
|
|
|
|
|
->where('day', $day)
|
|
|
|
|
->where(function ($query) {
|
|
|
|
|
$query->where('arrived_today_num', '<>', 0)
|
|
|
|
|
->orWhere('inventory', '<>', 0);
|
|
|
|
|
})
|
|
|
|
|
->get()
|
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
|
|
return ArrayUtils::index($records, 'sku_id');
|
|
|
|
|
}
|
2022-08-04 14:25:12 +08:00
|
|
|
}
|