erp/app/Exports/OrderBlankExport.php

87 lines
2.6 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',
])
->whereIn('goods_code', $goodsCodes)
->get(['goods_code', 'type_id', 'brand_id'])
->toArray();
$goods = ArrayUtils::index($goods, 'goods_code');
foreach ($excelDate as $key => $item) {
$goodsInfo = $goods[$item['goods_code']];
$arr[0] = $goodsInfo['type']['name'];
$arr[1] = $goodsInfo['brand']['name'];
$arr[2] = $key;
$arr[3] = $item['goods_name'];
$arr[4] = $item['num'];
$arr[5] = $item['local'];
$arr[6] = implode(' ', $item['p']);
$data[] = $arr;
}
return $data;
}
public function styles(Worksheet $sheet)
{
$count = $this->count + 2;
$sheet->mergeCells('B2:G2');
$sheet->getStyle('B2')->getAlignment()
->setVertical('center')
->setWrapText(true);
$sheet->getStyle('G3:G' . $count)->getAlignment()
->setVertical('center')
->setWrapText(true);
$sheet->getColumnDimension('A')->setWidth(10);
$sheet->getColumnDimension('B')->setWidth(10);
$sheet->getColumnDimension('C')->setWidth(10);
$sheet->getColumnDimension('D')->setWidth(60);
$sheet->getColumnDimension('E')->setWidth(5);
$sheet->getColumnDimension('F')->setWidth(11);
$sheet->getColumnDimension('G')->setWidth(14);
}
}