2013-06-12 42 views
1

我对信用卡运行了一些复杂的计算,第一页是总金额的汇总。 我不想做的是通过所有条款来计算摘要,然后再次运行它们以呈现详细视图。 那么是否有可能定义自定义占位符,我可以替换后,我添加了详细视图? 或者你知道另一种方法来实现想要的结果而不用两次运行计算?在TCPDF中使用占位符

编辑:

我这是怎么呈现详细信息视图:

 foreach($this->_creditNote->provisioningClient->customers as $customer) 
     { 
      $provisions = $customer->getProvisionsByBillingPeriodSum($this->_creditNote->billingPeriod); 
      if((count($provisions) >= 3 && $this->y > 230) || $this->y > 250) 
      { 
       $this->AddPage(); 
       $this->SetFillColor(240); 
       $this->SetDrawColor(0); 
       $this->SetFont('Helvetica', 'B', 10); 
       $this->Cell(140, 6, 'Die Provisionen im Einzelnen', 'TB', 0, 'L', 1); 
       $this->Cell(30, 6, "Beträge", 'TB', 1, 'R', 1); 
       $this->SetXY(25, $this->y-11.5); 
       $this->SetTextColor(170,170,170); 
       $this->SetFont('Helvetica', '', 7); 
       $this->Cell(175, 6, 'Seite ' . $this->getAliasNumPage() . ' von ' . $this->getAliasNbPages(), '', 2, 'R'); 
       $this->SetY($this->y+6); 
       $this->SetFont('Helvetica', '', 9); 
       $this->SetTextColor(0,0,0); 
      } 
      if(count($provisions) > 0) 
      { 
       $customerData = array(); 
       $this->SetXY(20, $this->y+1); 
       $this->SetFont('Helvetica', 'B', 10); 
       $this->Cell(140, 0, $customer->contact->name . " (" . $customer->customerNumber . ")"); 
       //add customer 
       $amount = 0; 
       $rows = array(); 
       foreach($provisions as $provision) 
       { 
        $text = $provision->description; 
        $description = ""; 
        if($provision->period != "onetime") 
        { 
         if($provision->isPartial($this->_creditNote->billingPeriod)) 
          $text .= ", anteilig"; 
         $description = $provision->periodName . ", " . $provision->getRuntime($this->_creditNote->billingPeriod); 
        } 
        if($description == "") 
         $description = null; 
        $temp = array($text, $provision->isPartial($this->_creditNote->billingPeriod) ? round($provision->getPartialAmount($this->_creditNote->billingPeriod)/100, 2) : $provision->amount/100, $description); 
        $amount += $temp[1]; 
        $rows[] = $temp; 
       } 
       $this->Cell(30, 0, number_format($amount, 2, ",", ".") . " €", 0, 1, 'R'); 
       foreach($rows as $row) 
       { 
        $this->SetXY(23, $this->y+1); 
        $this->SetFont('Helvetica', '', 8); 
        $this->Cell(137, 0, $row[0]); 
        $this->Cell(30, 0, number_format($row[1], 2, ",", ".") . " €", 0, 1, 'R'); 
        if($row[2]) 
        { 
         $this->SetXY(26, $this->y + 1); 
         $this->SetFont('Helvetica', 'I', 8); 
         $this->MultiCell(140, 0, $row[2], 0, 'L'); 
        } 
       } 
      } 
     } 

由于提前, 托比亚斯

回答

1

我不得不做类似的任务,前一段时间,因此知道多次迭代是不好的。

我创建了一个类,它将首先完成所有的计算。我们称之为'收益'类。这将重复上个月以来的所有订单。然后,它会创建信息为

  • 每个辛格运河订单(除去税的/ etc ...)
  • 的总体视图(有多少项目/总税额的/ etc。)

所以可能看起来像这样

$payoffInfo=new Payoff($arrayWithOrderInfos); 

// do creation of tcpdf object include header/footer/ ... 
$pdf = new TCPDF(); 
$pdf->setPDFVersion('1.4'); 

$pdf->setPrintHeader(FALSE); 
$pdf->setPrintFooter(TRUE); 

// create first page and set 'overall' like this ... 

$pdf->SetFont('Helvetica', '', 12); 
$pdf->Cell(0, 0, "Overall amount ".$payoffInfo->getItemCount() , 0, 1, 'L'); 
$pdf->Ln(); 

$pdf->SetFont('Helvetica', '', 12); 
$pdf->Cell(0, 0,"Overall amount ".$payoffInfo->getOverallAmount() , 0, 1, 'L'); 
$pdf->Ln(); 

// include more Overview Informations ... 
// Iterate over all single orders ... 

foreach($payoffInfo->getOrders() as $item){ 
    $html = "<p><ul>"; 
    $html .= "<li>Item ID: ".$item['order_id']."</li>"; 
    $html .= "<li>Item Price ".$item['price']."</li>"; 
    $html .= "<li>Item Clear Amount ".$item['price_without_tax']."</li>"; 
    $html .= "</ul></p>"; 
    $pdf->writeHTML($html, TRUE, FALSE, TRUE, FALSE, 'L'); 
} 
// Mark Items as billed or sth. else so we don't include them again next month 
$payoffInfo->setItemsToStatus("billed"); 

$pdf->Output($yourDesiredLocation, 'F'); 

要减少的时间量,以防止生成PDF格式的,而服务器具有高负载我搬到这一个“任务”(或其他任何东西)。这项任务随后会在半夜由一个cron任务调用一次(取决于您需要什么时间段)。

此解决方案适用于每月约400,000个订单,可能可以做更多。

如果你想要可以完全按照你想要的样式设计的时尚pdf,我今天会使用某种LaTeX“界面”。因为风格包括页眉和页脚在内的整个内容要容易得多。

希望这可以帮助你反思你的任务。

+0

结束我做了你的方式。我计算了总数,并循环了我存储它们以供以后在渲染时访问的条款。多谢! 虽然真的很伤心,但没有办法在tcpdf中使用自定义占位符:( –

2

据我所知,你目前的代码看起来是这样的:

// this will contain the computation for every orders 
$orders_infos = array(); 

// you need $orders_infos to be already filled here, but it is done after 
$total = 0; 
foreach ($orders_infos as $info) { 
    $total += $info['total']; 
} 
$html = "html for the overview page: ".$total; // $total is wrong ! 
$pdf->writeHTML($html); 

// compute infos for every order and display 
foreach ($orders as $k => $order) { 
    $orders_infos[$k] = compute_order_infos(); 
    $html = "html for this order page ".$orders_infos[$k]['total']; 
    $pdf->writeHTML($html); 
} 

而你的问题是,总览页面中的总数不正确,因为之后会运行计算。改变它的简单方法是运行所有计算,然后生成最终的PDF,但是你说因为某种原因你不想要。

达到你想要的下一个最好的办法就是骗一点,并保持命令列表的轨道上运行的closures,然后在年底运行它们,这样你保持最当前的代码组织,但仍之后做所有的展示;

$commands = array(); 

// this will contain the computation for every orders 
$orders_infos = array(); 

// you need $orders_infos to be already filled here, but it is done after 
$total = 0; 
$commands[] = function($pdf, $orders_infos) { 
    foreach ($orders_infos as $info) { 
    $total += $info['total']; 
    } 
    $html = "html for the overview page: ".$total; 
    $pdf->writeHTML($html); 
}; 

// compute infos for every order and display 
foreach ($orders as $k => $order) { 
    $orders_infos[$k] = compute_order_infos(); 
    $commands[] = function($pdf, $orders_infos) use ($k) { 
    $html = "html for this order page ".$orders_infos[$k]['total']; 
    $pdf->writeHTML($html); 
    }; 
} 

// now run all the commands 
foreach ($commands as $command) { 
    $command($pdf, $orders_infos); 
} 

所有的PDF写入命令,包括总览,都会在每次计算完成后执行。

类似但更简单的例子,所以你可以有一个如何将工作更好的视野:

$arr = array(); 
for ($i = 0; $i < 5; $i++) { 
    $arr[$i] = $i; 
} 
$funcs = array(); 
foreach ($arr as $k => $v) { 
    $funcs[] = function($arr) use($k) { 
    echo $arr[$k]."\n"; 
    }; 
} 

foreach ($funcs as $func) { 
    echo "func: "; 
    $func($arr); 
} 

显示:

$ php test.php 
func: 0 
func: 1 
func: 2 
func: 3 
func: 4 
+0

谢谢你的回答。我将在我的问题中粘贴一些我的代码。 每个月,我都必须生成一些用于配置客户端的信用注释,但有时候会有很多广告客户。所以我不想做的是计算总结,然后遍历所有规定并详细描述它们。所以我想我可以像使用aliasnbpages一样使用占位符。我也曾想过要通过它来运行它,并将所有需要的数据保存在数组中,以便在最后呈现它们,但我认为可能会有一种更舒适的方式:D –