调拨订单导出和插入
This commit is contained in:
parent
c26790ebbe
commit
6e291277d0
@ -7,7 +7,8 @@ use app\admin\controller\BaseAdminController;
|
|||||||
use app\admin\lists\inventory_transfer_order\InventoryTransferOrderLists;
|
use app\admin\lists\inventory_transfer_order\InventoryTransferOrderLists;
|
||||||
use app\admin\logic\inventory_transfer_order\InventoryTransferOrderLogic;
|
use app\admin\logic\inventory_transfer_order\InventoryTransferOrderLogic;
|
||||||
use app\admin\validate\inventory_transfer_order\InventoryTransferOrderValidate;
|
use app\admin\validate\inventory_transfer_order\InventoryTransferOrderValidate;
|
||||||
|
use app\common\service\xlsx\InventoryTransferXlsx;
|
||||||
|
use app\common\model\inventory_transfer\InventoryTransfer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 商品调拨订单控制器
|
* 商品调拨订单控制器
|
||||||
@ -104,4 +105,32 @@ class InventoryTransferOrderController extends BaseAdminController
|
|||||||
return $this->success('删除成功', [], 1, 1);
|
return $this->success('删除成功', [], 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @notes 审核商品调拨订单
|
||||||
|
* @author admin
|
||||||
|
* @date 2025/01/24 09:59
|
||||||
|
*/
|
||||||
|
public function insert()
|
||||||
|
{
|
||||||
|
$params = $this->request->post();
|
||||||
|
$result = InventoryTransferOrderLogic::insert($params);
|
||||||
|
if (true == $result) {
|
||||||
|
return $this->success('添加成功', [], 1, 1);
|
||||||
|
}
|
||||||
|
return $this->fail(InventoryTransferOrderLogic::getError());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出调拨表格
|
||||||
|
*/
|
||||||
|
public function export()
|
||||||
|
{
|
||||||
|
$params = $this->request->post();
|
||||||
|
$xlsx = new InventoryTransferXlsx();
|
||||||
|
$order = InventoryTransferOrderLogic::detail($params);
|
||||||
|
$file_path = $xlsx->export($order['product_list'], $order);
|
||||||
|
return $this->success('导出成功', ['url' => $file_path]);
|
||||||
|
}
|
||||||
}
|
}
|
@ -228,9 +228,30 @@ class InventoryTransferOrderLogic extends BaseLogic
|
|||||||
$data->type_name = $type_name;
|
$data->type_name = $type_name;
|
||||||
$data['product_list'] = InventoryTransfer::where('oid', $params['id'])->select()->each(function ($item) {
|
$data['product_list'] = InventoryTransfer::where('oid', $params['id'])->select()->each(function ($item) {
|
||||||
$find = StoreProduct::where('id', $item->product_id)->withTrashed()->field('store_name')->find();
|
$find = StoreProduct::where('id', $item->product_id)->withTrashed()->field('store_name')->find();
|
||||||
$item->store_name=$find['store_name'];
|
$item->store_name = $find['store_name'] ?? '';
|
||||||
})
|
})
|
||||||
->toArray();
|
->toArray();
|
||||||
return $data->toArray();
|
return $data->toArray();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 插入调拨订单
|
||||||
|
*/
|
||||||
|
public static function insert($params)
|
||||||
|
{
|
||||||
|
Db::startTrans();
|
||||||
|
try {
|
||||||
|
$find = InventoryTransfer::where('product_id', $params['product_id'])->where('oid', $params['id'])->find()->toArray();
|
||||||
|
if ($find) {
|
||||||
|
unset($find['id']);
|
||||||
|
$find['nums'] = $params['nums'];
|
||||||
|
$find['create_time'] = time();
|
||||||
|
}
|
||||||
|
InventoryTransfer::insert($find);
|
||||||
|
Db::commit();
|
||||||
|
return true;
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Db::rollback();
|
||||||
|
throw new BusinessException($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
123
app/common/service/xlsx/InventoryTransferXlsx.php
Normal file
123
app/common/service/xlsx/InventoryTransferXlsx.php
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
<?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 InventoryTransferXlsx
|
||||||
|
{
|
||||||
|
|
||||||
|
public function export($data, $order)
|
||||||
|
{
|
||||||
|
$spreadsheet = new Spreadsheet();
|
||||||
|
$sheet = $spreadsheet->getActiveSheet();
|
||||||
|
$sheet->setTitle('调拨订单'); // 设置列宽
|
||||||
|
$sheet->getDefaultRowDimension()->setRowHeight(20); // 设置行高
|
||||||
|
$sheet->getDefaultColumnDimension()->setWidth(20); // 设置列宽
|
||||||
|
$sheet->getPageSetup()->setFitToPage(true); // 自动适应页面大小
|
||||||
|
|
||||||
|
$sheet->getPageSetup()->setHorizontalCentered(true); // 页面水平居中
|
||||||
|
$sheet->getPageSetup()->setVerticalCentered(true); // 页面垂直居中
|
||||||
|
$sheet->getPageMargins()->setTop(0.5); // 上边距
|
||||||
|
$sheet->getPageMargins()->setBottom(0.5); // 下边距
|
||||||
|
$sheet->getPageMargins()->setLeft(0.5); // 左边距
|
||||||
|
$sheet->getPageMargins()->setRight(0.5); // 右边距
|
||||||
|
$sheet->getPageMargins()->setHeader(0.5); // 页眉边距
|
||||||
|
$sheet->getPageMargins()->setFooter(0.5); // 页脚边距
|
||||||
|
$sheet->getStyle('A1:G1')->getFont()->setBold(true); // 设置标题字体加粗
|
||||||
|
$sheet->getStyle('A1:G1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); // 设置标题水平居中
|
||||||
|
$sheet->getStyle('A1:G1')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); // 设置标题垂直居中
|
||||||
|
$sheet->getStyle('A2:G2')->getFont()->setBold(true); // 设置标题字体加粗
|
||||||
|
$sheet->getStyle('A2:G2')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); // 设置标题水平居中
|
||||||
|
// 合并单元格并设置标题
|
||||||
|
$sheet->mergeCells('A1:G1')->setCellValue('A1', '调拨订单');
|
||||||
|
$sheet->setCellValue('A2', '订单编号');
|
||||||
|
$sheet->setCellValue('B2', '转出方');
|
||||||
|
$sheet->setCellValue('C2', '方式');
|
||||||
|
$sheet->setCellValue('D2', '转入方');
|
||||||
|
$sheet->setCellValue('E2', '商品');
|
||||||
|
$sheet->setCellValue('F2', '数量');
|
||||||
|
$sheet->setCellValue('G2', '创建时间');
|
||||||
|
|
||||||
|
// 设置列宽
|
||||||
|
$sheet->getColumnDimension('A')->setAutoSize(true);
|
||||||
|
$sheet->getColumnDimension('B')->setAutoSize(true);
|
||||||
|
$sheet->getColumnDimension('C')->setWidth(20);
|
||||||
|
$sheet->getColumnDimension('D')->setWidth(20);
|
||||||
|
$sheet->getColumnDimension('E')->setAutoSize(true);
|
||||||
|
$sheet->getColumnDimension('F')->setAutoSize(true);
|
||||||
|
$sheet->getColumnDimension('G')->setAutoSize(true);
|
||||||
|
// 设置默认的单元格样式
|
||||||
|
$defaultStyle = [
|
||||||
|
'alignment' => [
|
||||||
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
||||||
|
'vertical' => Alignment::VERTICAL_CENTER,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle);
|
||||||
|
|
||||||
|
// 写入数据
|
||||||
|
$row = 3;
|
||||||
|
foreach ($data as $item) {
|
||||||
|
$sheet->setCellValue('A' . $row, $order['order_id'])
|
||||||
|
->setCellValue('B' . $row, $order['one_name'])
|
||||||
|
->setCellValue('C' . $row, $order['type_name'])
|
||||||
|
->setCellValue('D' . $row, $order['two_name'])
|
||||||
|
->setCellValue('E' . $row, $item['store_name'])
|
||||||
|
->setCellValue('F' . $row, $item['nums'])
|
||||||
|
->setCellValue('G' . $row, $item['create_time']);
|
||||||
|
$row++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = count($data);
|
||||||
|
// 应用默认样式到整个工作表
|
||||||
|
// 设置单元格的样式
|
||||||
|
$styleArray = [
|
||||||
|
'font' => [
|
||||||
|
'bold' => true,
|
||||||
|
'size' => 16,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$sheet->getStyle('A1')->applyFromArray($styleArray);
|
||||||
|
// 定义线框样式
|
||||||
|
$styleArray = [
|
||||||
|
'borders' => [
|
||||||
|
'allBorders' => [
|
||||||
|
'borderStyle' => Border::BORDER_THIN, // 线框样式
|
||||||
|
'color' => ['argb' => '000000'], // 线框颜色
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$sheet->getStyle('A1:G' . ($count + 2))->applyFromArray($styleArray);
|
||||||
|
// 保存文件到 public 下
|
||||||
|
$writer = new Xlsx($spreadsheet);
|
||||||
|
$url = '/export/' . "调拨订单-" . date('YmdHi') . '.xlsx';
|
||||||
|
$file_path = public_path() . $url;
|
||||||
|
if (!is_dir(dirname($file_path))) {
|
||||||
|
mkdir(dirname($file_path), 0777, true);
|
||||||
|
}
|
||||||
|
$writer->save($file_path);
|
||||||
|
return getenv('APP_URL') . $url;
|
||||||
|
|
||||||
|
// $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'));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// $url = $dir . '/' . "调拨订单 " . date('YmdHi') . '.xlsx';
|
||||||
|
// $file_path = public_path() . $url;
|
||||||
|
// if (!is_dir(dirname($file_path))) {
|
||||||
|
// mkdir(dirname($file_path), 0777, true);
|
||||||
|
// }
|
||||||
|
// $writer->save($file_path);
|
||||||
|
// return getenv('APP_URL') . $url;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user