2013-06-12 41 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 –