2025-03-12 11:19:42 +08:00

177 lines
7.1 KiB
PHP

<?php
namespace app\common\service\xlsx;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 订单清单
*/
class OrderList
{
public function export($data,$order,$other_data)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 合并单元格A1到K1
$sheet->mergeCells('A1:P1');
$sheet->mergeCells('B2:C2');
$sheet->mergeCells('E2:F2');
$sheet->mergeCells('G2:H2');
$sheet->mergeCells('I2:L2');
$sheet->mergeCells('M2:N2');
$sheet->mergeCells('O2:P2');
$sheet->mergeCells('B3:C3');
$sheet->mergeCells('E3:F3');
$sheet->mergeCells('G3:H3');
$sheet->mergeCells('I3:K3');
$sheet->mergeCells('N3:O3');
$sheet->mergeCells('B4:C4');
$sheet->mergeCells('D4:E4');
$sheet->mergeCells('F4:G4');
$sheet->setCellValue('A1', '供 投 里 海 农 特 产 品 下 单 清 单');
$sheet->setCellValue('A2', '姓名:');
$sheet->setCellValue('B2', $other_data->nickname??'');
$sheet->getColumnDimension('C')->setAutoSize(true);
$sheet->setCellValue('D2', '电话:');
$sheet->setCellValue('E2', $other_data->phone??'');
$sheet->setCellValue('G2', '收货地址:');
$sheet->setCellValue('I2', $other_data->address??'');
$sheet->setCellValue('M2', '订单编号:');
$sheet->setCellValue('O2', $order['order_id']??'');
$sheet->setCellValue('A3', '厨师:');
$sheet->setCellValue('B3', $other_data->chef??'');
$sheet->setCellValue('D3', '电话:');
$sheet->setCellValue('E3', $other_data->chef_phone??'');
$sheet->setCellValue('G3', '事由:');
$sheet->setCellValue('I3', $other_data->purpose??'');
$sheet->setCellValue('L3', '桌数:');
$sheet->setCellValue('M3', $other_data->tables??'');
$sheet->setCellValue('N3', '办事时间:');
$sheet->setCellValue('P3', $other_data->days??'');
$sheet->setCellValue('A4', '下单时间:');
$sheet->setCellValue('B4', $order['create_time']??'');
$sheet->setCellValue('D4', '送货时间:');
$sheet->setCellValue('F4', $other_data->arrival_time??'');
$sheet->setCellValue('A5', '编号');
$sheet->setCellValue('B5', '类别');
$sheet->setCellValue('C5', '品名');
$sheet->setCellValue('D5', '毛重');
$sheet->setCellValue('E5', '净重(约)');
$sheet->setCellValue('F5', '单位');
$sheet->setCellValue('G5', '单价/元');
$sheet->setCellValue('H5', '下单数量');
$sheet->setCellValue('I5', '下单金额');
$sheet->setCellValue('J5', '包装');
$sheet->setCellValue('K5', '押金');
$sheet->setCellValue('L5', '售后');
$sheet->setCellValue('M5', '正常损耗');
$sheet->setCellValue('N5', '产地');
$sheet->mergeCells('O5:P5');
$sheet->setCellValue('O5', '备注');
// 设置默认的单元格样式
$defaultStyle = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
],
];
// 应用默认样式到整个工作表
$spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle);
foreach ($data as $k => $v) {
$sheet->setCellValue('A' . ($k + 6), $v['product_id']);
$sheet->setCellValue('B' . ($k + 6), $v['top_cate_name']);
$sheet->setCellValue('C' . ($k + 6), $v['store_name']);
$sheet->setCellValue('D' . ($k + 6), $v['gross_weight']);
$sheet->setCellValue('E' . ($k + 6), $v['net_weight']);
$sheet->setCellValue('F' . ($k + 6), $v['unit_name']);
$sheet->setCellValue('G' . ($k + 6), $v['price']);
$sheet->setCellValue('H' . ($k + 6), $v['cart_num']);
$sheet->setCellValue('I' . ($k + 6), $v['total_price']);
$sheet->setCellValue('J' . ($k + 6), $v['package']);
$sheet->setCellValue('K' . ($k + 6),$v['deposit']);
$sheet->setCellValue('L' . ($k + 6),$v['after_sales']);
$sheet->setCellValue('M' . ($k + 6),$v['loss']);
$sheet->mergeCells('O'.($k + 6).':P'.($k + 6));
$sheet->setCellValue('O' . ($k + 6),$v['mark']);
}
$count = count($data);
$sheet->setCellValue('A' . ($count + 6),'合计');
$sheet->setCellValue('C' . ($count + 6),$count);
$sheet->setCellValue('I' . ($count + 6),$order['total_price']);
$sheet->setCellValue('K' . ($count + 6),$order['deposit']);
$sheet->setCellValue('A' . ($count + 7),'累计');
$sheet->setCellValue('B' . ($count + 7),$count);
$sheet->setCellValue('C' . ($count + 7),'采购预收金额');
$sheet->setCellValue('D' . ($count + 7),$order['total_price'].'元');
$sheet->setCellValue('G' . ($count + 7),'押金');
$sheet->setCellValue('H' . ($count + 7),$order['deposit'].'元');
$sheet->setCellValue('I' . ($count + 7),'合计预收');
$sheet->setCellValue('J' . ($count + 7),bcadd($order['deposit']??0,$order['total_price']).'元');
$sheet->setCellValue('L' . ($count + 7),'接单员');
$sheet->setCellValue('M' . ($count + 7),$order['admin_name']);
$sheet->setCellValue('O' . ($count + 7),'分单员');
$sheet->setCellValue('P' . ($count + 7),$other_data->splitting_officer??'');
$sheet->mergeCells('A' . ($count + 8) . ':P' . $count + 8);
$sheet->setCellValue('A' . $count + 8, '备注:数量金额最终以实际收货金额结算,多退少补。押金退框无利息退还。',);
// 设置单元格的样式
$styleArray = [
'font' => [
'bold' => true,
'size' => 28,
],
];
$sheet->getStyle('A1')->applyFromArray($styleArray);
// 定义线框样式
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN, // 线框样式
'color' => ['argb' => '000000'], // 线框颜色
],
],
];
$sheet->getStyle('A1:P' . ($count + 8))->applyFromArray($styleArray);
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir. '/' .'供投里海农特产品下单清单-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .'供投里海农特产品下单清单-'.date('Y-m-d H:i') . '.xlsx';
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;
}
}