public function exportOrderExcel(int $bom_id, array $data)
{
$bomGoods = $this->bomGoodsModel->where('bom_id', $bom_id)->select();
if (!$bomGoods) $this->error();
$bomGoods = Collection($bomGoods)->toArray();
$cateName = $this->bomService->getCateName();
$total = 0;
foreach ($data as $key => $value) {
foreach ($value['goods'] as $k => $val) {
$data[$key]['goods'][$k]['header_no'] = substr($val['no'], 0, 2);
$data[$key]['goods'][$k]['cateColor'] = array_key_exists($val['catecolor_id'], $cateName['colorName']) ? $cateName['colorName'][$val['catecolor_id']] : '未设置';
foreach ($bomGoods as $bg) {
if ($val['g_id'] == $bg['goods_id']) {
if (empty($bg['spec']) || empty($bg['craft']) || empty($bg['price'])) $this->error();
$data[$key]['goods'][$k]['specification'] = array_key_exists($bg['spec'], ['specName']) ? $cateName['specName'][$bg['spec']] : '未设置';
$data[$key]['goods'][$k]['specCraft'] = array_key_exists($bg['craft'], $cateName['craftName']) ? $cateName['craftName'][$bg['craft']] : '未设置';
$data[$key]['goods'][$k]['specPrice'] = $bg['price'];
$data[$key]['goods'][$k]['subtotal'] = is_numeric($bg['usable_area']) ? ceil($bg['usable_area'] * $bg['price']) : 0;
$total += $data[$key]['goods'][$k]['subtotal'];
}
}
unset($data[$key]['goods'][$k]['catecolor_id'], $data[$key]['goods'][$k]['g_id'], $data[$key]['goods'][$k]['cate_id'], $data[$key]['goods'][$k]['id'], $data[$key]['goods'][$k]['pid']);
}
}
$bomInfo = $this->bomModel->field(['name', 'project_address', 'project_schedule'])->where('id', $bom_id)->find();
if (!$bomInfo) $this->error();
$bomInfo = $bomInfo->toArray();
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getProperties()->setCreator("楼主666")
->setLastModifiedBy("选材")
->setTitle("选材")
->setSubject("选材")
->setDescription("选材")
->setKeywords("选材")
->setCategory("选材");
$objPHPExcel->getDefaultStyle()->getFont()->setName('等线');
$objPHPExcel = $this->mergeCells($objPHPExcel, ['A1:I1', 'A2:A4', 'C2:I2', 'C3:I3', 'C4:I4', 'E5:H5']);
$objPHPExcel = $this->setWidth($objPHPExcel, [
['line' => 'A', 'width' => 10], ['line' => 'B', 'width' => 20], ['line' => 'C', 'width' => 10], ['line' => 'D', 'width' => 10], ['line' => 'E', 'width' => 10],
['line' => 'F', 'width' => 10], ['line' => 'G', 'width' => 10], ['line' => 'H', 'width' => 10], ['line' => 'I', 'width' => 10], ['line' => 'J', 'width' => 10],
['line' => 'K', 'width' => 10], ['line' => 'L', 'width' => 10],
]);
$objPHPExcel = $this->setValue($objPHPExcel, [
['key' => 'A1', 'value' => '物料清单'], ['key' => 'A2', 'value' => '项目信息'], ['key' => 'B2', 'value' => '项目名称'], ['key' => 'B3', 'value' => '项目地点'], ['key' => 'B4', 'value' => '清单时间'],
['key' => 'A5', 'value' => '序号'], ['key' => 'B5', 'value' => '产品图片'], ['key' => 'C5', 'value' => '产品名称'], ['key' => 'D5', 'value' => '使用空间'], ['key' => 'E5', 'value' => '基本信息'],
['key' => 'I5', 'value' => '备注'], ['key' => 'C2', 'value' => $bomInfo['name']], ['key' => 'C3', 'value' => $bomInfo['project_address']], ['key' => 'C4', 'value' => $bomInfo['project_schedule']]
]);
$objPHPExcel = $this->setContentSize($objPHPExcel, [
['key' => 'A1', 'value' => 15], ['key' => 'A2', 'value' => 10], ['key' => 'B2', 'value' => 10], ['key' => 'B3', 'value' => 10], ['key' => 'B4', 'value' => 10], ['key' => 'A5', 'value' => 12],
['key' => 'C2', 'value' => 12], ['key' => 'C3', 'value' => 12], ['key' => 'C4', 'value' => 12],
]);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('969696');
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A2:I4')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C0C0C0');
$style_array = array(
'borders' => array(
'allborders' => array(
'style' => \PHPExcel_Style_Border::BORDER_THIN
)
));
$objPHPExcel->getActiveSheet()->setTitle('选材助手');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(\PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$key = 1;
$tempCateKey = 6;
$rowNum = 5;
foreach ($data as $v) {
$objPHPExcel->getActiveSheet()->mergeCells('A' . $tempCateKey . ':I' . $tempCateKey)->setCellValue('A' . $tempCateKey, $v['cate_name']);
$objPHPExcel->getActiveSheet()->getStyle('A' . $tempCateKey)->getFont()->setBold(true);
$objPHPExcel = $this->setContentSize($objPHPExcel, [['key' => 'A' . $tempCateKey, 'value' => 12]]);
foreach ($v['goods'] as $item) {
$itemNeedMergeCellKeyStart = $tempCateKey + 1;
$itemNeedMergeCellKeyEnd = $tempCateKey + 7;
$objPHPExcel->getActiveSheet()
->mergeCells('A' . $itemNeedMergeCellKeyStart . ':A' . $itemNeedMergeCellKeyEnd)
->mergeCells('B' . $itemNeedMergeCellKeyStart . ':B' . $itemNeedMergeCellKeyEnd)
->mergeCells('C' . $itemNeedMergeCellKeyStart . ':C' . $itemNeedMergeCellKeyEnd)
->mergeCells('D' . $itemNeedMergeCellKeyStart . ':D' . $itemNeedMergeCellKeyEnd)
->mergeCells('I' . $itemNeedMergeCellKeyStart . ':I' . $itemNeedMergeCellKeyEnd)
->setCellValue('A' . $itemNeedMergeCellKeyStart, $key)
->setCellValue('C' . $itemNeedMergeCellKeyStart, $item['g_name']);
$objPHPExcel->getActiveSheet()->getStyle('C' . $itemNeedMergeCellKeyStart)->getFont()->setSize(8);
$objPHPExcel->getActiveSheet()->setCellValue('D' . $itemNeedMergeCellKeyStart, $item['used_space'])
->setCellValue('E' . ($itemNeedMergeCellKeyStart), '设置编号')->setCellValue('E' . ($itemNeedMergeCellKeyStart + 1), '设计类别')->setCellValue('E' . ($itemNeedMergeCellKeyStart + 2), '设计规格')->setCellValue('E' . ($itemNeedMergeCellKeyStart + 3), '产品颜色')->setCellValue('E' . ($itemNeedMergeCellKeyStart + 4), '防火等级')->setCellValue('E' . ($itemNeedMergeCellKeyStart + 5), '工艺要求')->setCellValue('E' . ($itemNeedMergeCellKeyStart + 6), '原产地')
->setCellValue('G' . ($itemNeedMergeCellKeyStart), '供应厂商')->setCellValue('G' . ($itemNeedMergeCellKeyStart + 1), '产品编号')->setCellValue('G' . ($itemNeedMergeCellKeyStart + 2), '产品规格')->setCellValue('G' . ($itemNeedMergeCellKeyStart + 3), '供货周期')->setCellValue('G' . ($itemNeedMergeCellKeyStart + 4), '单价(元/㎡)')->setCellValue('G' . ($itemNeedMergeCellKeyStart + 5), '面积(㎡)')->setCellValue('G' . ($itemNeedMergeCellKeyStart + 6), '小计')
->setCellValue('F' . ($itemNeedMergeCellKeyStart), $item['header_no'])
->setCellValue('F' . ($itemNeedMergeCellKeyStart + 1), $item['c_name'])
->setCellValue('F' . ($itemNeedMergeCellKeyStart + 2), $item['specification'])
->setCellValue('F' . ($itemNeedMergeCellKeyStart + 3), $item['cateColor'])
->setCellValue('F' . ($itemNeedMergeCellKeyStart + 4), $item['fire_rating'])
->setCellValue('F' . ($itemNeedMergeCellKeyStart + 5), $item['specCraft'])
->setCellValue('F' . ($itemNeedMergeCellKeyStart + 6), $item['ship_address'])
->setCellValue('H' . ($itemNeedMergeCellKeyStart), $item['supplier'])
->setCellValue('H' . ($itemNeedMergeCellKeyStart + 1), $item['no'])
->setCellValue('H' . ($itemNeedMergeCellKeyStart + 2), $item['size'])
->setCellValue('H' . ($itemNeedMergeCellKeyStart + 3), $item['period'])
->setCellValue('H' . ($itemNeedMergeCellKeyStart + 4), $item['specPrice'])
->setCellValue('H' . ($itemNeedMergeCellKeyStart + 5), $item['usable_area'])
->setCellValue('H' . ($itemNeedMergeCellKeyStart + 6), $item['subtotal'])
->setCellValue('I' . ($itemNeedMergeCellKeyStart), $item['remark'])
;
$objPHPExcel = $this->setContentSize($objPHPExcel, [
['key' => 'E' . ($itemNeedMergeCellKeyStart), 'value' => 8], ['key' => 'E' . ($itemNeedMergeCellKeyStart + 1), 'value' => 8], ['key' => 'E' . ($itemNeedMergeCellKeyStart + 2), 'value' => 8], ['key' => 'E' . ($itemNeedMergeCellKeyStart + 3), 'value' => 8], ['key' => 'E' . ($itemNeedMergeCellKeyStart + 4), 'value' => 8], ['key' => 'E' . ($itemNeedMergeCellKeyStart + 5), 'value' => 8], ['key' => 'E' . ($itemNeedMergeCellKeyStart + 6), 'value' => 8],
['key' => 'G' . ($itemNeedMergeCellKeyStart), 'value' => 8], ['key' => 'G' . ($itemNeedMergeCellKeyStart + 1), 'value' => 8], ['key' => 'G' . ($itemNeedMergeCellKeyStart + 2), 'value' => 8], ['key' => 'G' . ($itemNeedMergeCellKeyStart + 3), 'value' => 8], ['key' => 'G' . ($itemNeedMergeCellKeyStart + 4), 'value' => 8], ['key' => 'G', ($itemNeedMergeCellKeyStart + 5), 'value' => 8], ['key' => 'G' . ($itemNeedMergeCellKeyStart + 6), 'value' => 8],
['key' => 'F' . ($itemNeedMergeCellKeyStart), 'value' => 8], ['key' => 'F' . ($itemNeedMergeCellKeyStart + 1), 'value' => 8], ['key' => 'F' . ($itemNeedMergeCellKeyStart + 2), 'value' => 8], ['key' => 'F' . ($itemNeedMergeCellKeyStart + 3), 'value' => 8], ['key' => 'F' . ($itemNeedMergeCellKeyStart + 4), 'value' => 8], ['key' => 'F' . ($itemNeedMergeCellKeyStart + 5), 'value' => 8], ['key' => 'F' . ($itemNeedMergeCellKeyStart + 6), 'value' => 8],
['key' => 'H' . ($itemNeedMergeCellKeyStart), 'value' => 8], ['key' => 'H' . ($itemNeedMergeCellKeyStart + 1), 'value' => 8], ['key' => 'H' . ($itemNeedMergeCellKeyStart + 2), 'value' => 8], ['key' => 'H' . ($itemNeedMergeCellKeyStart + 3), 'value' => 8], ['key' => 'H' . ($itemNeedMergeCellKeyStart + 4), 'value' => 8], ['key' => 'H' . ($itemNeedMergeCellKeyStart + 5), 'value' => 8], ['key' => 'H' . ($itemNeedMergeCellKeyStart + 6), 'value' => 8],
]);
if (!empty($item['cover_image'])) {
$imgPath = $_SERVER['DOCUMENT_ROOT'] . $item['cover_image'];
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setResizeProportional(false);
$objDrawing->setPath($imgPath);
$objDrawing->setWidthAndHeight(159, 100);
$objDrawing->setOffsetY(15);
$objDrawing->setOffsetX(1);
$objDrawing->setCoordinates('B' . $itemNeedMergeCellKeyStart);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
$objPHPExcel->getActiveSheet()->getStyle('E' . $itemNeedMergeCellKeyStart . ':E' . $itemNeedMergeCellKeyEnd)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C0C0C0');
$objPHPExcel->getActiveSheet()->getStyle('G' . $itemNeedMergeCellKeyStart . ':G' . $itemNeedMergeCellKeyEnd)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C0C0C0');
$key += 1;
$tempCateKey = $tempCateKey + 7;
$rowNum += 7;
}
$tempCateKey += 1;
$rowNum += 1;
}
$objPHPExcel->getActiveSheet()->setCellValue('G' . ($rowNum + 1), '总计');
$objPHPExcel = $this->setContentSize($objPHPExcel, [['key' => 'G' . ($rowNum + 1), 'value' => 8]]);
$objPHPExcel = $this->mergeCells($objPHPExcel, ['A' . ($rowNum + 1) . ':' . 'F' . ($rowNum + 1)]);
$objPHPExcel->getActiveSheet()->setCellValue('H' . ($rowNum + 1), $total);
$objPHPExcel = $this->setContentSize($objPHPExcel, [['key' => 'H' . ($rowNum + 1), 'value' => 8]]);
$objPHPExcel->getActiveSheet()->getStyle('A1:I' . ($rowNum + 1))
->applyFromArray($style_array);
$objPHPExcel->getActiveSheet()->getStyle('A1:I' . ($rowNum + 1))->getAlignment()
->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$fileName = ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . 'excel' . DIRECTORY_SEPARATOR . 'inventory.xlsx';
$objWriter->save($fileName);
$xlsx = $this->baseUrl . DIRECTORY_SEPARATOR . 'excel' . DIRECTORY_SEPARATOR . 'inventory.xlsx';
$this->success(__('successful'), [$xlsx]);
}
protected function setContentSize($objPHPExcel, array $data)
{
foreach ($data as $v) {
$objPHPExcel->getActiveSheet()->getStyle($v['key'])->getFont()->setSize($v['value']);
}
return $objPHPExcel;
}
protected function setValue($objPHPExcel, array $data)
{
foreach ($data as $v) {
$objPHPExcel->setActiveSheetIndex()->setCellValue($v['key'], $v['value']);
}
return $objPHPExcel;
}
protected function mergeCells($objPHPExcel, array $data)
{
foreach ($data as $v) {
$objPHPExcel->getActiveSheet()->mergeCells($v);
}
return $objPHPExcel;
}
protected function setWidth($objPHPExcel, array $data)
{
foreach ($data as $value) {
$objPHPExcel->getActiveSheet()->getColumnDimension($value['line'])->setWidth($value['width']);
}
return $objPHPExcel;
}
|