166 lines
6.7 KiB
PHP
166 lines
6.7 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 OrderDetail
|
|
{
|
|
|
|
public $warehouse='海吉星仓库';
|
|
public $company='供投里海';
|
|
public $address='泸州龙马潭区海吉星122栋';
|
|
public $phone='08302669767';
|
|
public $tel='17309099881';
|
|
|
|
public function export($data,$system_store,$order)
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
// 合并单元格A1到K1
|
|
$sheet->mergeCells('A1:J1');
|
|
$sheet->mergeCells('B2:E2');
|
|
$sheet->mergeCells('G2:J2');
|
|
$sheet->mergeCells('G3:J3');
|
|
$sheet->mergeCells('B3:E3');
|
|
$sheet->mergeCells('B4:C4');
|
|
$sheet->mergeCells('D4:E4');
|
|
$sheet->setCellValue('A1', $this->company.'公司送货单');
|
|
$sheet->setCellValue('A2', '店铺名称');
|
|
$sheet->setCellValue('B2', $system_store);
|
|
$sheet->setCellValue('F2', '送货时间');
|
|
$sheet->setCellValue('G2', $order['delivery_time']??'');
|
|
$sheet->setCellValue('A3', '开单日期');
|
|
$sheet->setCellValue('B3', $order['pay_time']??'');
|
|
$sheet->setCellValue('F3', '单号');
|
|
$sheet->setCellValue('G3', $order['order_id']??'');
|
|
$sheet->setCellValue('A4', '序号');
|
|
$sheet->setCellValue('B4', '商品名称');
|
|
$sheet->setCellValue('D4', '规格');
|
|
$sheet->setCellValue('F4', '单位');
|
|
$sheet->setCellValue('G4', '单价');
|
|
$sheet->setCellValue('H4', '数量');
|
|
$sheet->setCellValue('I4', '总价');
|
|
$sheet->setCellValue('J4', '备注');
|
|
|
|
// 设置默认的单元格样式
|
|
$defaultStyle = [
|
|
'alignment' => [
|
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
|
'vertical' => Alignment::VERTICAL_CENTER,
|
|
],
|
|
];
|
|
|
|
// 应用默认样式到整个工作表
|
|
$spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle);
|
|
|
|
foreach ($data as $k => $v) {
|
|
$sheet->setCellValue('A' . ($k + 5), $k + 1);
|
|
$sheet->setCellValue('B' . ($k + 5), $v['store_name']);
|
|
$sheet->mergeCells('B' . ($k + 5) . ':C' . $k + 5);
|
|
$sheet->setCellValue('D' . ($k + 5), $v['store_info']);
|
|
$sheet->mergeCells('D' . ($k + 5) . ':E' . $k + 5);
|
|
$sheet->setCellValue('F' . ($k + 5), $v['unit_name']);
|
|
$sheet->setCellValue('G' . ($k + 5), $v['price']);
|
|
$sheet->setCellValue('H' . ($k + 5), $v['cart_num']);
|
|
$sheet->setCellValue('I' . ($k + 5), $v['total_price']);
|
|
}
|
|
|
|
$count = count($data);
|
|
$sheet->mergeCells('A' . ($count + 5) . ':J' . $count + 5);
|
|
|
|
$sheet->mergeCells('B' . ($count + 6) . ':E' . $count + 6);
|
|
$sheet->setCellValue('A' . $count + 6, '合计数量');
|
|
$sheet->setCellValue('B' . $count + 6,$order['total_num']);
|
|
$sheet->mergeCells('G' . ($count + 6) . ':J' . $count + 6);
|
|
$sheet->setCellValue('F' . $count + 6, '合计价格');
|
|
$sheet->setCellValue('G' . $count + 6, $order['total_price']);
|
|
|
|
$sheet->setCellValue('A' . ($count + 7), '备注');
|
|
$sheet->mergeCells('B' . ($count + 7) . ':J' . $count + 7);
|
|
$sheet->setCellValue('B' . ($count + 7), $order['mark']??'');
|
|
|
|
$sheet->mergeCells('A' . ($count + 8) . ':J' . $count + 8);
|
|
|
|
$sheet->mergeCells('A' . ($count + 9) . ':A' . $count + 10);
|
|
$sheet->setCellValue('A' . ($count + 9), '销售单位');
|
|
$sheet->setCellValue('B' . ($count + 9), '名称');
|
|
$sheet->setCellValue('C' . ($count + 9), $this->company);
|
|
$sheet->setCellValue('B' . ($count + 10), '地址');
|
|
$sheet->setCellValue('C' . ($count + 10), $this->address);
|
|
$sheet->setCellValue('G' . ($count + 9), '公司电话');
|
|
$sheet->setCellValue('H' . ($count + 9), $this->phone);
|
|
$sheet->setCellValue('G' . ($count + 10), '售后电话');
|
|
$sheet->setCellValue('H' . ($count + 10), $this->tel);
|
|
|
|
|
|
|
|
|
|
$sheet->mergeCells('C' . ($count + 9) . ':F' . $count + 9);
|
|
$sheet->mergeCells('C' . ($count + 10) . ':F' . $count + 10);
|
|
$sheet->mergeCells('H' . ($count + 9) . ':J' . ($count + 9));
|
|
$sheet->mergeCells('H' . ($count + 10) . ':J' . $count + 10);
|
|
|
|
$sheet->mergeCells('A' . ($count + 11) . ':J' . $count + 11);
|
|
|
|
|
|
$sheet->setCellValue('A' . $count + 12, '下单人',);
|
|
$sheet->mergeCells('B' . ($count + 12) . ':C' . $count + 12);
|
|
$sheet->setCellValue('B' . ($count + 12), $order['real_name']??'');
|
|
|
|
$sheet->setCellValue('D' . $count + 12, '电话');
|
|
$sheet->mergeCells('E' . ($count + 12) . ':F' . $count + 12);
|
|
$sheet->setCellValue('E' . ($count + 12), $order['user_phone']??'');
|
|
|
|
$sheet->setCellValue('G' . $count + 12, '地址');
|
|
$sheet->mergeCells('H' . ($count + 12) . ':J' . $count + 12);
|
|
$sheet->setCellValue('H' . ($count + 12), $order['user_address']??'');
|
|
|
|
$sheet->setCellValue('A' . $count + 13, '仓库',);
|
|
$sheet->mergeCells('B' . ($count + 13) . ':C' . $count + 13);
|
|
$sheet->setCellValue('D' . $count + 13, '送货');
|
|
$sheet->mergeCells('E' . ($count + 13) . ':F' . $count + 13);
|
|
$sheet->setCellValue('G' . $count + 13, '签收人');
|
|
$sheet->mergeCells('H' . ($count + 13) . ':J' . $count + 13);
|
|
|
|
// 设置单元格的样式
|
|
$styleArray = [
|
|
'font' => [
|
|
'bold' => true,
|
|
'size' => 16,
|
|
],
|
|
];
|
|
$sheet->getStyle('A1')->applyFromArray($styleArray);
|
|
// 定义线框样式
|
|
$styleArray = [
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN, // 线框样式
|
|
'color' => ['argb' => '000000'], // 线框颜色
|
|
],
|
|
],
|
|
];
|
|
$sheet->getStyle('A1:J' . ($count + 13))->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 . '/' . $system_store.'出库单-'.date('YmdHi') . '.xlsx';
|
|
$url = '/export/'.date('Y-m') . '/' . $system_store.'出库单-'.date('YmdHi') . '.xlsx';
|
|
// 保存文件到 public 下
|
|
$writer->save($file_path);
|
|
return getenv('APP_URL').$url;
|
|
}
|
|
}
|