Transactional calulation for page breaking working OK it seems

This commit is contained in:
Karl Cordes 2010-02-26 10:42:21 +11:00
parent b57145a34a
commit f9d1d13431
3 changed files with 331 additions and 279 deletions

View file

@ -181,8 +181,8 @@ class QuotesController extends AppController {
$this->set('commercialDetails', $commercialDetails);
debug($quote);
debug($commercialDetails);
//debug($quote);
//debug($commercialDetails);

324
vendors/xtcpdf.php vendored
View file

@ -9,7 +9,7 @@ class XTCPDF extends TCPDF {
var $xfooterfont = PDF_FONT_NAME_MAIN ;
var $xfooterfontsize = 8 ;
function header() {
/* function header() {
$this->SetHeaderMargin(4);
$this->ln();
$pageNo = $this->PageNoFormatted();
@ -18,7 +18,7 @@ class XTCPDF extends TCPDF {
// $this->MultiCell($w, $h, $txt, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, $ishtml)
//$this->MultiCell(0, 0, "<b>PRICING AND SPECIFICATIONS</b>", 0, 'C', 0, 1, null, null, true, 0, true);
$this->writeHTMLCell(0, 0, null, null, "<b>PRICING AND SPECIFICATIONS</b>", 0, 1, 0, true, 'C');
}
} */
/**
* Overwrites the default header
@ -258,6 +258,99 @@ ENDPRODUCT;
}
/**
* Start a TCPDF transaction and do the calculations for the product table.
* @param <type> $principleName
* @param <type> $currency
* @param <type> $gst
* @param <type> $products
* @param <type> $docType
* @param <type> $commercialDetails
*/
function calculateProductPage($principleName, $currency,$gst, $products ,$docType, $commercialDetails) {
$itemColwidth = 12;
$qtyColwidth = 10;
$descColwidth = 120;
$unitpriceColwidth = 25;
$totalPricColwidth = -10;
//$availableHeight = 253;
$availableHeight = 300;
$heightLeft = $availableHeight; //height left on this particular page.
$pagesRequired = 1;
foreach ($products as $product) {
$this->startTransaction();
$fullDesc = "<b>".$product[$docType]['title'].'GG</b><br>'.$product[$docType]['description'];
$this->MultiCell($descColwidth, 0, $fullDesc, 1, "L", 0, 0, null, null, true, 0, true);
$lasth = $this->getLastH();
$this->rollbackTransaction($this);
//echo "Last height needed for item: ".$product[$docType]['item_number']." was: ".$lasth."<br>";
echo $product[$docType]['id']." needs ".$lasth." has $heightLeft left<br>";
$itemno = $product[$docType]['item_number'];
if($heightLeft - $lasth <= 0) {
echo "<br>OUT OF SPACE ON PAGE $pagesRequired making a new one<br>";
$pagesRequired++;
$heightLeft = $availableHeight; //Start checking the next page;
echo "<br>remaining height on new one = ".$heightLeft."<br>";
$heightLeft -= $lasth;
$itemno = $product[$docType]['item_number'];
echo "<br>Using $lasth for $itemno so now have $heightLeft left<br>";
$pageProducts[$pagesRequired][] = $product[$docType]['id'];
}
else {
$heightLeft -= $lasth;
$pageProducts[$pagesRequired][] = $product[$docType]['id'];
}
}
/* Reached the last product in this Product Table.
* Need to see if we still have enough room to print the totals, if not will need to put the last item on a new page.
*/
$this->startTransaction();
$subtotal = 0;
$totalsHeight = $this->totals($currency, $subtotal, 1);
$commDetailsHeight = $this->commercialComments($commercialDetails);
$this->rollbackTransaction($this);
echo "Currently have $heightLeft Comm details height is ".$commDetailsHeight." totals height is: ".$totalsHeight."<br>";
if($heightLeft - ($commDetailsHeight + $totalsHeight) <= 0) {
echo "<br>Need a new page for last item and commercial comments";
array_pop($pageProducts[$pagesRequired]);
$pagesRequired++;
$heightLeft = $availableHeight;
$heightLeft -= $commDetailsHeight;
$pageProducts[$pagesRequired][] = $product[$docType]['id'];
}
debug($pageProducts);
return $pageProducts;
}
/**
*
* @param <type> $principleName
@ -267,13 +360,14 @@ ENDPRODUCT;
* @param String $docType - the model name of the Products we want to put in the table. ie. QuoteProduct, or InvoiceProduct, etc
* @param Map $commercialDetails - the details array/map - passed to commercialComments()
*/
function productPage($principleName, $currency,$gst, $products ,$docType, $commercialDetails) {
function productPage($principleName, $currency,$gst, $products ,$docType, $commercialDetails, $pageProducts) {
$this->SetFontSize(10);
//$lineHeight = 5; //Size of a single line of text. If the company name is more, multiply this by the number of lines it needs
$lineHeight = 5;
$lineHeight = 5; //Size of a single line of text. If the company name is more,
// multiply this by the number of lines it needs
$itemColwidth = 12;
@ -284,99 +378,20 @@ ENDPRODUCT;
$this->SetFillColor(240);
//$heightNeeded = $this->getNumLines("ITEM\NO.", $itemColwidth);
//echo "HEADER ROW HEIGHT IS $heightNeeded";
/* Need to work out how many Pages the Product Table will need to be split over */
//echo "Product Page height is: ".$this->getPageHeight();
$availableHeight = 253; //Figured out experimentally. Approx 253mm of space left for the actual table data after the header rows.
// echo "Remaining space on the page is: $availableHeight <br>";
$pagesRequired = 1;
$theseProductsHeight = 0;
$remainingHeight = $availableHeight;
$pageProducts = array(); //Array in the form $pageProducts[$pageNumber] = NUMBER_OF_ITEMS_ON_THIS_PAGE
echo "Page height is ".$this->getPageHeight();
foreach ($products as $product) {
$fullDesc = "<b>".$product[$docType]['title'].'</b><br>'.$product[$docType]['description'];
$linesNeeded = $this->getNumLines($fullDesc, $descColwidth);
$mmNeeded = ($linesNeeded*$lineHeight);
echo $product[$docType]['item_number']." needs ".$mmNeeded." has $remainingHeight left<br>";
if($remainingHeight - $mmNeeded <= 0) {
echo "<br>OUT OF SPACE ON PAGE $pagesRequired making a new one";
$pagesRequired++;
$remainingHeight = $availableHeight; //Start checking the next page;
echo "<br>remaining height on new one = ".$remainingHeight;
$remainingHeight -= $mmNeeded;
$itemno = $product[$docType]['item_number'];
echo "<br>Using $mmNeeded for $itemno so now have $remainingHeight left<br>";
$pageProducts[$pagesRequired][] = $product;
}
else {
$remainingHeight -= $mmNeeded;
$pageProducts[$pagesRequired][] = $product;
}
/* Reached the last product in this Product Table.
* Need to see if we still have enough room to print the totals, if not will need to put the last item on a new page.
*/
if(end($products) == $product) {
echo $product[$docType]['item_number']. " IS LAST";
$commMMneeded = 23*$lineHeight; //hardcoded. Which is bad. so Dont go and change text size on me now..
echo "MM needed for commercial comments ".$commMMneeded;
echo "MM remaining right now : ".$remainingHeight;
if($remainingHeight - $commMMneeded <= 0) {
echo "<br>Need a new page for last item and commercial comments";
array_pop($pageProducts[$pagesRequired]);
$pagesRequired++;
$remainingHeight = $availableHeight;
$remainingHeight -= $commMMneeded;
$pageProducts[$pagesRequired][] = $product;
}
}
}
//print_r($pageProducts[1]);
//echo "We need to make $pagesRequired product pages<br>";
$subTotal = 0;
for($i=1; $i<=$pagesRequired; $i++) {
foreach ($pageProducts as $page) {
$this->AddPage();
//Draw Header Rows
$this->setFontSize(12);
$pageNo = $this->PageNoFormatted();
$this->MultiCell(0, 0, "PAGE $pageNo OF {nb}", 0, "R", 0, 1);
$this->MultiCell(0, 0, "PRICING & SPECIFICATIONS", 0, "C", 0, 1);
$this->MultiCell($itemColwidth, 0, "ITEM\nNO.", 1, "C", 1, 0);
$this->setFontSize(10);
$this->MultiCell($itemColwidth, 0, "ITEM NO.", 1, "C", 1, 0);
$heightNeeded = $this->getLastH();
/**
@ -400,11 +415,14 @@ ENDPRODUCT;
$this->MultiCell($totalPricColwidth, $heightNeeded, $currency['iso4217'], 1, "C", 1, 1);
echo "<h2>$pageNo</h2>";
print_r($page);
//Start Printing Product cells, until we run out of room. Then continue on the next page
foreach($pageProducts[$i] as $product) {
foreach($page as $productID) {
$product = array_shift($products);
$fullDesc = "<b>".$product[$docType]['title'].'</b><br>'.$product[$docType]['description'];
@ -434,55 +452,12 @@ ENDPRODUCT;
$lastX = $this->GetX();
//echo $product[$docType]['item_number']." Last X is $lastX, Last Y is $lastY <br>";
if(end($pageProducts[$pagesRequired]) == $product) {
//$this->MultiCell($w, $h, $txt, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, $ishtml, $autopadding);
$this->MultiCell($itemColwidth, 6, "", 1, 'C', 0, 0);
$this->MultiCell($qtyColwidth, 6, "", 1, 'C', 0, 0);
$this->MultiCell($descColwidth, 6, "SUB-TOTAL (EXCLUDING GST)", 1, 'L', 0, 0);
$this->MultiCell($unitpriceColwidth, 6, "", 1, 'C', 0, 0);
$this->MultiCell($totalPricColwidth, 6, $this->formatCurrency($currency['symbol'], $subTotal), 1, 'C', 0, 1);
$this->MultiCell($itemColwidth, 6, "", 1, 'C', 0, 0);
$this->MultiCell($qtyColwidth, 6, "", 1, 'C', 0, 0);
$this->MultiCell($descColwidth, 6, "GST (10%)", 1, 'L', 0, 0);
$this->MultiCell($unitpriceColwidth, 6, "", 1, 'C', 0, 0);
if($gst == 1) {
$gstAmount = 0.1*$subTotal;
$this->MultiCell($totalPricColwidth, 6, $this->formatCurrency($currency['symbol'], $gstAmount), 1 , 'C', 0, 1);
}
else {
$this->MultiCell($totalPricColwidth, 6, "N/A", 1 , 'C', 0, 1);
}
$this->MultiCell($itemColwidth, 6, "", 1, 'C', 0, 0);
$this->MultiCell($qtyColwidth, 6, "", 1, 'C', 0, 0);
$this->MultiCell($descColwidth, 6, "TOTAL PAYABLE", 1, 'L', 0, 0);
$this->MultiCell($unitpriceColwidth, 6, "", 1, 'C', 0, 0);
if($gst == 1) {
$totalPrice = $subTotal * 1.1;
}
else {
$totalPrice = $subTotal;
}
$this->MultiCell($totalPricColwidth, 6, $this->formatCurrency($currency['symbol'], $totalPrice), 1, 'C', 0, 1);
}
}
debug($products);
@ -511,6 +486,8 @@ ENDPRODUCT;
*/
$this->totals($currency, $subTotal, $gst);
$this->commercialComments($commercialDetails);
@ -561,10 +538,10 @@ ENDPRODUCT;
*/
function commercialComments($details) {
//$this->MultiCell($w, $h, $txt, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, $ishtml)
$totalHeight = 0;
$this->MultiCell(0, 0, "<b><u>Commercial Comments:</u></b>", 0, 'L', 0, 1, null, null, 1, null, true);
$totalHeight += $this->getLastH();
$validUntilDate = strtotime($details["dateIssued"]. ' + '.$details["validFor"].' days');
@ -588,9 +565,15 @@ ENDHTMLCONTENTS;
//$commlines = $this->getNumLines($listHtmlContents);
//echo "lines required for commercial comments: ".$commlines;
$this->MultiCell(0, 0, $listHtmlContents, 0, 'L', 0, 1, null, null, 1, null, true);
$this->MultiCell(0, 0, "Signed: ____________________________", 0, 'L');
$this->MultiCell(0, 0, " ".$details["salesEngineer"], 0, 'L');
$totalHeight += $this->getLastH();
$this->MultiCell(0, 0, "Signed: ____________________________", 0, 'L');
$totalHeight += $this->getLastH();
$this->MultiCell(0, 0, " ".$details["salesEngineer"], 0, 'L');
$totalHeight += $this->getLastH();
return $totalHeight;
}
@ -603,6 +586,69 @@ ENDHTMLCONTENTS;
}
function totals($currency, $subTotal, $gst) {
$itemColwidth = 12;
$qtyColwidth = 10;
$descColwidth = 120;
$unitpriceColwidth = 25;
$totalPricColwidth = -10;
$totalHeight = 0;
echo "desc col wdith : ".$descColwidth;
$this->MultiCell($itemColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($qtyColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($descColwidth, 6, "SUB-TOTAL (EXCLUDING GST)", 1, 'L', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($unitpriceColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($totalPricColwidth, 6, $this->formatCurrency($currency['symbol'], $subTotal), 1, 'C', 0, 1);
$totalHeight += $this->getLastH();
$this->MultiCell($itemColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($qtyColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($descColwidth, 6, "GST (10%)", 1, 'L', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($unitpriceColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
if($gst == 1) {
$gstAmount = 0.1*$subTotal;
$this->MultiCell($totalPricColwidth, 6, $this->formatCurrency($currency['symbol'], $gstAmount), 1 , 'C', 0, 1);
$totalHeight += $this->getLastH();
}
else {
$this->MultiCell($totalPricColwidth, 6, "N/A", 1 , 'C', 0, 1);
$totalHeight += $this->getLastH();
}
$this->MultiCell($itemColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($qtyColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($descColwidth, 6, "TOTAL PAYABLE", 1, 'L', 0, 0);
$totalHeight += $this->getLastH();
$this->MultiCell($unitpriceColwidth, 6, "", 1, 'C', 0, 0);
$totalHeight += $this->getLastH();
if($gst == 1) {
$totalPrice = $subTotal * 1.1;
}
else {
$totalPrice = $subTotal;
}
$this->MultiCell($totalPricColwidth, 6, $this->formatCurrency($currency['symbol'], $totalPrice), 1, 'C', 0, 1);
$totalHeight += $this->getLastH();
return $totalHeight;
}

View file

@ -26,6 +26,8 @@ $tcpdf->SetHeaderMargin(2);
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
$principle_name = strtoupper($enquiry['Principle']['name']);
$pageProducts = $tcpdf->calculateProductPage($principle_name, $quote['Currency'], $enquiry['Enquiry']['gst'], $products, 'LineItem', $commercialDetails);
$page1done = false; //Have we already made the first page? If so, don't show the CMC header/footer
@ -88,13 +90,17 @@ foreach ($quote['QuotePage'] as $page) {
//print_r($products);
$principle_name = strtoupper($enquiry['Principle']['name']);
debug($commercialDetails);
//debug($commercialDetails);
//$tcpdf->productPageHTML($principle_name, $quote['Currency'], $enquiry['Enquiry']['gst'], $products, 'LineItem', $commercialDetails);
$tcpdf->productPage($principle_name, $quote['Currency'], $enquiry['Enquiry']['gst'], $products, 'LineItem', $commercialDetails);
$tcpdf->productPage($principle_name, $quote['Currency'], $enquiry['Enquiry']['gst'], $products, 'LineItem', $commercialDetails,
$pageProducts);