erp/app/Exports/OrderBlankExport.php

101 lines
3.0 KiB
PHP

<?php
namespace App\Exports;
use App\Models\Goods;
use App\Utils\ArrayUtils;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class OrderBlankExport implements FromCollection, WithStyles
{
private $data;
private $shopName;
private $no;
private $count;
public function __construct($shopName, $no, $excelDate)
{
$this->shopName = $shopName;
$this->no = $no;
$this->count = count($excelDate);
$this->data = $this->createData($excelDate);
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return new Collection($this->data);
}
private function createData($excelDate)
{
$noStr = implode(',', $this->no);
$data = [
['店铺名称', $this->shopName],
['跟团号', $noStr],
['种类', '品牌', '商品名称', '数量', '货架', '配货'],
];
$goodsCodes = array_column($excelDate, 'goods_code');
$goods = Goods::query()
->with([
'brand:id,name',
'type:id,name',
'skus:id,goods_id,sku_code,title'
])
->whereIn('goods_code', $goodsCodes)
->get(['id', 'title', 'goods_code', 'type_id', 'brand_id'])
->toArray();
$goods = ArrayUtils::index($goods, 'goods_code');
foreach ($excelDate as $key => $item) {
$str = '';
$num = 0;
foreach ($item['p'] as $k => $v) {
$str .= $v . ';';
$num++;
if (6 === $num) {
$str .= PHP_EOL;
$num = 0;
}
}
$goodsInfo = $goods[$item['goods_code']];
$name = '';
foreach ($goodsInfo['skus'] as $sku) {
if ($sku['sku_code'] === $item['sku_code']) {
$name = $goodsInfo['title'] . $sku['title'];
}
}
$arr[0] = $goodsInfo['type']['name'];
$arr[1] = $goodsInfo['brand']['name'];
$arr[2] = $name;
$arr[3] = $item['num'];
$arr[4] = $item['local'];
$arr[5] = $str;
$data[] = $arr;
}
return $data;
}
public function styles(Worksheet $sheet)
{
$count = $this->count + 3;
$sheet->getStyle('B2')->getAlignment()
->setWrapText(true);
$sheet->mergeCells('B2:F2');
$sheet->getStyle('F3:F' . $count)->getAlignment()
->setWrapText(true);
$sheet->getColumnDimension('A')->setWidth(8);
$sheet->getColumnDimension('B')->setWidth(10);
$sheet->getColumnDimension('C')->setWidth(36);
$sheet->getColumnDimension('D')->setWidth(5);
$sheet->getColumnDimension('E')->setWidth(5);
$sheet->getColumnDimension('F')->setWidth(36);
$sheet->getRowDimension(2)->setRowHeight(90);
}
}