138 lines
5.5 KiB
PHP
138 lines
5.5 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 WarehouseOrderEntry
|
|
{
|
|
|
|
public $warehouse='海吉星仓库';
|
|
public $company='供投里海';
|
|
public $address='泸州龙马潭区海吉星122栋';
|
|
public $phone='08302669767';
|
|
public $tel='17309099881';
|
|
|
|
public function export($data,$order)
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
// 合并单元格A1到K1
|
|
$sheet->mergeCells('A1:J1');//标题
|
|
$sheet->mergeCells('B2:E2');//开单日期
|
|
$sheet->mergeCells('G2:J2');//单号
|
|
$sheet->mergeCells('B3:C3');
|
|
$sheet->mergeCells('D3:E3');
|
|
|
|
$sheet->setCellValue('A1', $this->company.'公司入库单');
|
|
$sheet->setCellValue('A2', '入库时间');
|
|
$sheet->setCellValue('B2', $order['create_time']??'');
|
|
$sheet->setCellValue('F2', '单号');
|
|
$sheet->setCellValue('G2', $order['code']??'');
|
|
$sheet->setCellValue('A3', '序号');
|
|
$sheet->setCellValue('B3', '商品名称');
|
|
$sheet->setCellValue('D3', '供应商');
|
|
$sheet->setCellValue('F3', '单位');
|
|
$sheet->setCellValue('G3', '单价');
|
|
$sheet->setCellValue('H3', '数量');
|
|
$sheet->setCellValue('I3', '总价');
|
|
$sheet->setCellValue('J3', '支付方式');
|
|
|
|
// 设置默认的单元格样式
|
|
$defaultStyle = [
|
|
'alignment' => [
|
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
|
'vertical' => Alignment::VERTICAL_CENTER,
|
|
],
|
|
];
|
|
|
|
// 应用默认样式到整个工作表
|
|
$spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle);
|
|
|
|
foreach ($data as $k => $v) {
|
|
$sheet->setCellValue('A' . ($k + 4), $v['product_id'] );
|
|
$sheet->setCellValue('B' . ($k + 4), $v['store_name']);
|
|
$sheet->mergeCells('B' . ($k + 4) . ':C' . $k + 4);
|
|
$sheet->setCellValue('D' . ($k + 4), $v['supplier_name']);
|
|
$sheet->mergeCells('D' . ($k + 4) . ':E' . $k + 4);
|
|
$sheet->setCellValue('F' . ($k + 4), $v['unit_name']);
|
|
$sheet->setCellValue('G' . ($k + 4), $v['purchase']);
|
|
$sheet->setCellValue('H' . ($k + 4), $v['nums']);
|
|
$sheet->setCellValue('I' . ($k + 4), $v['total_price']);
|
|
$sheet->setCellValue('J' . ($k + 4), $v['pay_type_name']??'');
|
|
}
|
|
|
|
$count = count($data);
|
|
$sheet->mergeCells('A' . ($count + 4) . ':J' . $count + 4);
|
|
|
|
$sheet->mergeCells('B' . ($count + 5) . ':E' . $count + 5);
|
|
$sheet->setCellValue('A' . $count + 5, '赊账金额');
|
|
$sheet->setCellValue('B' . $count + 5,$order['credit_pay']);
|
|
$sheet->mergeCells('G' . ($count + 5) . ':J' . $count + 5);
|
|
$sheet->setCellValue('F' . $count + 5, '现金金额');
|
|
$sheet->setCellValue('G' . $count + 5, $order['cash_pay']);
|
|
|
|
|
|
$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->mergeCells('C' . ($count + 7) . ':F' . $count + 7);
|
|
// $sheet->mergeCells('C' . ($count + 8) . ':F' . $count + 8);
|
|
// $sheet->mergeCells('H' . ($count + 7) . ':J' . ($count + 7));
|
|
// $sheet->mergeCells('H' . ($count + 8) . ':J' . $count + 8);
|
|
|
|
$sheet->mergeCells('A' . ($count + 7) . ':J' . $count + 7);
|
|
|
|
$sheet->setCellValue('A' . $count + 8, '录入人',);
|
|
// $sheet->setCellValue('B' . $count + 11, $this->warehouse);
|
|
$sheet->mergeCells('B' . ($count + 8) . ':C' . $count + 8);
|
|
$sheet->setCellValue('D' . $count + 8, '采购人');
|
|
$sheet->mergeCells('E' . ($count + 8) . ':F' . $count + 8);
|
|
$sheet->setCellValue('G' . $count + 8, '审核人');
|
|
$sheet->mergeCells('H' . ($count + 8) . ':J' . $count + 8);
|
|
|
|
// 设置单元格的样式
|
|
$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 + 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 . '/' .$this->company.'公司入库单-'.date('Y年m月d日H时i分') . '.xlsx';
|
|
$url = '/export/' . date('Y-m') . '/' .$this->company.'公司入库单-'.date('Y年m月d日H时i分') . '.xlsx';
|
|
// 保存文件到 public 下
|
|
$writer->save($file_path);
|
|
return getenv('APP_URL').$url;
|
|
}
|
|
}
|