我正在尝试使用Codeigniter中的PHPExcel从数据库创建Excel工作表以生成报告。我有主表sales
和超过4个表从中获取数据。我在表之间使用多个JOINS编写了查询。
以下是我的情景:
在处理生成销售报告时,客户可能已购买了多个产品,此详细信息存储在另一个表sale_items
中。目前我已经为其他表编写了一个带有连接的主查询,并且在foreach循环中获取结果后,我正在编写另一个查询来从sale_items
表中选择相应的数据。
我试图在excel表中显示单个客户,如果他购买了多个产品,我需要在excel表中用逗号分隔这些产品。我使用第二个查询来获取产品名称并破坏结果并尝试在excel中的相应行中显示。如果我和一个客户一起尝试,它的工作正常。如果我为多个客户试用,该页面会显示服务器错误。
这是我的代码:
$dateTmp = "DATE_FORMAT(".$this->db->dbprefix('sales').".date,'%m/%Y')";
$condition = "if(".$this->db->dbprefix('sales').".biller_id = 5, 'A', if(".$this->db->dbprefix('sales').".biller_id = 6, 'B',if(".$this->db->dbprefix('sales').".biller_id = 7,'C','D' )))";
if ($pdf || $xls) {
$this->db
->select("date, ".$this->db->dbprefix('warehouses').".name,
CONCAT(".$this->db->dbprefix('warehouses').".name,'-',".$dateTmp.",'-', ".$condition.",".$this->db->dbprefix('sales').".invoice_no) as month,"
." biller, customer,
scan_no, CONCAT(" . $this->db->dbprefix('sale_items') . ".product_name, '__', " . $this->db->dbprefix('sale_items') . ".quantity) as iname,
unit_price,if(tax_rate_id=2,item_tax,0) as taxId1,if(tax_rate_id=4,item_tax,0) as taxId2 ,
tin,cin,cst,total_discount, item_tax, shipping,subtotal,payment_status,sales.id as saleid ", FALSE)
->from('sales')
->join('sale_items', 'sale_items.sale_id=sales.id', 'left')
->join('companies','companies.id=sales.customer_id', 'left')
->join('salersperson','salersperson.id=companies.salesperson_id', 'left')
->join('hosipital_group','hosipital_group.id=companies.hospital_id', 'left')
->join('doctor','doctor.id=companies.dr_id', 'left')
->join('warehouses', 'warehouses.id=sales.warehouse_id', 'left')
->group_by('sales.id')
->order_by('sales.date desc'); //MAIN JOIN QUERY
$q = $this->db->get();
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
} else {
$data = NULL;
}
if (!empty($data))
{
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle(lang('sales_report'));
$this->excel->getActiveSheet()->SetCellValue('A1', lang('date'));
$this->excel->getActiveSheet()->SetCellValue('B1', lang('branch'));
$this->excel->getActiveSheet()->SetCellValue('C1', lang('invoice_no'));
$this->excel->getActiveSheet()->SetCellValue('D1', lang('biller'));
$this->excel->getActiveSheet()->SetCellValue('E1', lang('customer'));
$this->excel->getActiveSheet()->SetCellValue('F1', lang('tin'));
$this->excel->getActiveSheet()->SetCellValue('G1', lang('cin'));
$this->excel->getActiveSheet()->SetCellValue('H1', lang('cst'));
$this->excel->getActiveSheet()->SetCellValue('I1', lang('scan_no'));
$this->excel->getActiveSheet()->SetCellValue('J1', lang('product_qty'));
$this->excel->getActiveSheet()->SetCellValue('K1', lang('prod_price'));
$this->excel->getActiveSheet()->SetCellValue('L1', lang('vat1'));
$this->excel->getActiveSheet()->SetCellValue('M1', lang('vat2'));
$this->excel->getActiveSheet()->SetCellValue('N1', lang('discount'));
$this->excel->getActiveSheet()->SetCellValue('O1', lang('product_tax'));
$this->excel->getActiveSheet()->SetCellValue('P1', lang('shipping'));
$this->excel->getActiveSheet()->SetCellValue('Q1', lang('total'));
$row = 2;
$total = 0;
$paid = 0;
$balance = 0;
foreach ($data as $data_row) {
$saleid=$data_row->saleid;
$this->db->select("product_name")
->from('sale_items')
->where('sale_items.sale_id',$saleid); /*SECOND QUERY TO FETCH FROM SALE_ITEMS TABLE*/
$q1 = $this->db->get();
if ($q1->num_rows() > 0)
{
foreach (($q1->result()) as $row1)
{
$prdtarray[] = $row1->product_name;
}
$product=implode(',',$prdtarray); /*PRODUCTS NAME SEPARATED BY COMMA AND STORED IN A VARIABLE*/
}
else
{
$data1 = NULL;
}
$this->excel->getActiveSheet()->SetCellValue('A' . $row, $this->sma->hrld($data_row->date));
$this->excel->getActiveSheet()->SetCellValue('B' . $row, $data_row->name);
$this->excel->getActiveSheet()->SetCellValue('C' . $row, $data_row->month);
$this->excel->getActiveSheet()->SetCellValue('D' . $row, $data_row->biller);
$this->excel->getActiveSheet()->SetCellValue('E' . $row, $data_row->customer);
$this->excel->getActiveSheet()->SetCellValue('F' . $row, $data_row->tin);
$this->excel->getActiveSheet()->SetCellValue('G' . $row, $data_row->cin);
$this->excel->getActiveSheet()->SetCellValue('H' . $row, $data_row->cst);
$this->excel->getActiveSheet()->SetCellValue('I' . $row, $data_row->scan_no);
$this->excel->getActiveSheet()->SetCellValue('J' . $row, $product); //THE COMMA SEPARATED PRODUCT NAMES
$this->excel->getActiveSheet()->SetCellValue('K' . $row, $data_row->unit_price);
$this->excel->getActiveSheet()->SetCellValue('L' . $row, $data_row->taxId1);
$this->excel->getActiveSheet()->SetCellValue('M' . $row, $data_row->taxId2);
$this->excel->getActiveSheet()->SetCellValue('N' . $row, $data_row->total_discount);
$this->excel->getActiveSheet()->SetCellValue('O' . $row, $data_row->item_tax);
$this->excel->getActiveSheet()->SetCellValue('P' . $row, $data_row->shipping);
$this->excel->getActiveSheet()->SetCellValue('Q' . $row, $data_row->subtotal);
$total += $data_row->subtotal;
$paid += $data_row->item_tax;
// $balance += ($data_row->grand_total - $data_row->paid);
$row++;
}
$this->excel->getActiveSheet()->getStyle("L" . $row . ":M" . $row)->getBorders()
->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
$this->excel->getActiveSheet()->SetCellValue('Q' . $row, $total);
$this->excel->getActiveSheet()->SetCellValue('O' . $row, $paid);
// $this->excel->getActiveSheet()->SetCellValue('H' . $row, $balance);
$this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('K')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('L')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('M')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('N')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('O')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('Q')->setWidth(30);
$filename = 'sales_report';
$this->excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
if ($xls) {
$this->excel->getActiveSheet()->getStyle('E2:E' . $row)->getAlignment()->setWrapText(true);
ob_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
header('Cache-Control: max-age=0');
ob_clean();
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
exit();
}
}
$this->session->set_flashdata('error', lang('nothing_found'));
//redirect($_SERVER["HTTP_REFERER"]);
}
我不知道在与多个客户一起尝试时会发生什么。我的逻辑可能是错的。任何人都可以帮我找到我的错误.. ??提前致谢