mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-12-01 06:30:49 +00:00
112 lines
3.5 KiB
PHP
112 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\DailyReport;
|
|
use App\Models\Shop;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class WeekDataExport implements FromCollection, ShouldAutoSize
|
|
{
|
|
private $startDate;
|
|
private $endDate;
|
|
private $data;
|
|
|
|
public function __construct($startDate, $endDate)
|
|
{
|
|
$this->startDate = $startDate;
|
|
$this->endDate = $endDate;
|
|
$this->data = $this->createData();
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function collection()
|
|
{
|
|
return new Collection($this->data);
|
|
}
|
|
|
|
private function createData()
|
|
{
|
|
$headTitle = [
|
|
'商品名称',
|
|
'品类',
|
|
'规格',
|
|
'品牌',
|
|
'商品编码',
|
|
'成本',
|
|
'销售数量',
|
|
];
|
|
$shops = Shop::query()
|
|
->whereNotIn('id', [7, 9, 10, 11, 17, 18])
|
|
->orderBy('id')
|
|
->pluck('name', 'id')
|
|
->toArray();
|
|
$headTitle = array_merge($headTitle, array_values($shops));
|
|
|
|
$dailyReports = DailyReport::query()
|
|
->with([
|
|
'goods:id,title',
|
|
'goodsType:id,name',
|
|
'goodsSku:id,title',
|
|
'goodsBrand:id,name',
|
|
])
|
|
->where('date', '>=', $this->startDate)
|
|
->where('date', '<=', $this->endDate)
|
|
->get();
|
|
if ($dailyReports->isEmpty()) {
|
|
return [$headTitle];
|
|
}
|
|
|
|
$arr = [];
|
|
foreach ($dailyReports as $item) {
|
|
$number = $item->total_goods_number - $item->total_cancel_number;
|
|
if (!isset($arr[$item->external_sku_id])) {
|
|
$arr[$item->external_sku_id] = [
|
|
'goodsTitle' => $item->goods->title,
|
|
'goodsTypeName' => $item->goodsType->name,
|
|
'goodsSkuTitle' => $item->goodsSku->title,
|
|
'goodsBrandName' => $item->goodsBrand->name,
|
|
'external_sku_id' => $item->external_sku_id,
|
|
'cost' => [],
|
|
'number' => [],
|
|
'shops' => []
|
|
];
|
|
}
|
|
$arr[$item->external_sku_id]['cost'][] = $item->cost;
|
|
$arr[$item->external_sku_id]['number'][] = $number;
|
|
foreach ($item->shop_data as $shopId => $shopData) {
|
|
$number = $shopData['total_goods_number'] - $shopData['total_cancel_number'];
|
|
if (!isset($arr[$item->external_sku_id]['shops'][$shopId])) {
|
|
$arr[$item->external_sku_id]['shops'][$shopId] = 0;
|
|
}
|
|
$arr[$item->external_sku_id]['shops'][$shopId] += $number;
|
|
}
|
|
}
|
|
$bodyData = [];
|
|
foreach ($arr as $item) {
|
|
$cost = array_sum($item['cost']) / count($item['cost']);
|
|
$shopData = [];
|
|
foreach ($shops as $shopId => $shopName) {
|
|
$shopData[] = $item['shops'][$shopId] ?? 0;
|
|
}
|
|
$data = [
|
|
$item['goodsTitle'],
|
|
$item['goodsTypeName'],
|
|
$item['goodsSkuTitle'],
|
|
$item['goodsBrandName'],
|
|
$item['external_sku_id'],
|
|
$cost,
|
|
array_sum($item['number']),
|
|
];
|
|
|
|
$bodyData[] = array_merge($data, $shopData);
|
|
}
|
|
|
|
return [$headTitle, $bodyData];
|
|
}
|
|
}
|