lihaiMiddleOffice/app/common/service/xlsx/OrderOutboundFinance.php
2025-03-12 11:19:42 +08:00

161 lines
6.6 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 OrderOutboundFinance
{
public function export($data,$order,$other_data,$type=1)
{
if($type==1){
$title='供 投 里 海 农 特 产 品 出 库 结 算 单';
$title2='供投里海农特产品出库结算单-';
}else{
$title='供 投 里 海 农 特 产 品 出 库 供 货 单';
$title2='供投里海农特产品出库供货单-';
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 合并单元格A1到K1
$sheet->mergeCells('A1:I1');
$sheet->mergeCells('D2:E2');
$sheet->mergeCells('H2:I2');
$sheet->mergeCells('G2:I2');
$sheet->mergeCells('H3:I3');
$sheet->setCellValue('A1', $title);
$sheet->setCellValue('A2', '姓名:');
$sheet->setCellValue('B2', $other_data->nickname??'');
$sheet->getColumnDimension('B')->setWidth(16);
$sheet->getColumnDimension('H')->setAutoSize(true);
$sheet->setCellValue('C2', '电话:');
$sheet->setCellValue('D2', $other_data->phone??'');
$sheet->setCellValue('F2', '订单编号:');
$sheet->setCellValue('G2', $order['order_id']??'');
$sheet->setCellValue('A3', '编号');
$sheet->setCellValue('B3', '品名');
$sheet->setCellValue('C3', '单位');
$sheet->setCellValue('D3', '净重(约)');
$sheet->setCellValue('E3', '数量');
$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->setCellValue('C' . ($k + 4), $v['unit_name']);
$sheet->setCellValue('D' . ($k + 4), $v['gross_weight']);
$sheet->setCellValue('E' . ($k + 4), $v['nums']);
$sheet->setCellValue('F' . ($k + 4), $v['price']);
$sheet->setCellValue('G' . ($k + 4), $v['total_price']);
$sheet->mergeCells('H' . ($k + 4) . ':I' . $k + 4);
$sheet->setCellValue('H' . ($k + 4), $v['mark']);
}
$count = count($data);
$sheet->setCellValue('A' . ($count + 4),'合计');
$sheet->setCellValue('B' . ($count + 4),$count);
$sheet->setCellValue('A' . ($count + 5),'累计接单:');
$sheet->setCellValue('C' . ($count + 5),'预收金额:');
$sheet->setCellValue('D' . ($count + 5),0);
$sheet->setCellValue('E' . ($count + 5),'预收押金:');
$sheet->setCellValue('G' . ($count + 5),'合计预收:');
$sheet->setCellValue('H' . ($count + 5),0);
$sheet->setCellValue('A' . ($count + 6),'出库合计:');
$sheet->setCellValue('C' . ($count + 6),'出库金额:');
$sheet->setCellValue('D' . ($count + 6),$order['pay_price']==0?$order['total_price']:$order['pay_price']);
$sheet->setCellValue('E' . ($count + 6),'实收押金:');
$sheet->setCellValue('G' . ($count + 6),'合计:');
$sheet->setCellValue('H' . ($count + 6),$order['pay_price']==0?$order['total_price']:$order['pay_price']);
$sheet->setCellValue('A' . ($count + 7),'应收:');
$sheet->setCellValue('B' . ($count + 7),$order['pay_price']==0?$order['total_price']:$order['pay_price']);
$sheet->setCellValue('C' . ($count + 7),'应退:');
$sheet->setCellValue('F' . ($count + 7),'门店:');
$sheet->mergeCells('G' . ($count + 7) . ':I' . $count + 7);
$sheet->setCellValue('G' . ($count + 7),$order['system_store_name']);
$sheet->mergeCells('B' . ($count + 8) . ':C' . $count + 8);
$sheet->setCellValue('A'. ($count + 8), '收货地址:');
$sheet->mergeCells('E' . ($count + 8) . ':F' . $count + 8);
$sheet->setCellValue('D'. ($count + 8), '下单时间:');
$sheet->setCellValue('E'. ($count + 8), $order['create_time']);
$sheet->setCellValue('G'. ($count + 8), '送货时间:');
$sheet->mergeCells('H' . ($count + 8) . ':I' . $count + 8);
$sheet->setCellValue('H'. ($count + 8), $other_data->arrival_time??'');
$sheet->setCellValue('B'. ($count + 8), $other_data->address??'');
$sheet->mergeCells('B' . ($count + 9) . ':E' . $count + 9);
$sheet->setCellValue('A' . $count + 9, '销售地址:',);
$sheet->setCellValue('B' . $count + 9, '泸州市海吉星农产品商贸物流园122栋',);
$sheet->setCellValue('F' . $count + 9, '电话:',);
$sheet->setCellValue('G' . $count + 9, '08302669767',);
$sheet->setCellValue('H' . $count + 9, '签收:',);
// 设置单元格的样式
$styleArray = [
'font' => [
'bold' => true,
'size' => 16,
],
];
$sheet->getStyle('A1')->applyFromArray($styleArray);
// 定义线框样式
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN, // 线框样式
'color' => ['argb' => '000000'], // 线框颜色
],
],
];
$sheet->getStyle('A1:I' . ($count + 9))->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 . '/' .$title2.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .$title2.date('Y-m-d H:i') . '.xlsx';
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;
}
}