$Period_1 = $CurrentPeriod - 1;
 $Period_2 = $CurrentPeriod - 2;
 $Period_3 = $CurrentPeriod - 3;
 $Period_4 = $CurrentPeriod - 4;
 $Period_5 = $CurrentPeriod - 5;
 while ($InventoryPlan = DB_fetch_array($InventoryResult)) {
     if ($Category != $InventoryPlan['categoryid']) {
         $FontSize = 10;
         if ($Category != '') {
             /*Then it's NOT the first time round */
             /*draw a line under the CATEGORY TOTAL*/
             $YPos -= $line_height;
             $pdf->line($Left_Margin, $YPos, $Page_Width - $Right_Margin, $YPos);
             $YPos -= 2 * $line_height;
         }
         $LeftOvers = $pdf->addTextWrap($Left_Margin, $YPos, 260 - $Left_Margin, $FontSize, $InventoryPlan['categoryid'] . ' - ' . $InventoryPlan['categorydescription'], 'left');
         $Category = $InventoryPlan['categoryid'];
         $FontSize = 8;
     }
     $YPos -= $line_height;
     if ($_POST['Location'] == 'All') {
         $SQL = "SELECT SUM(CASE WHEN prd='" . $CurrentPeriod . "' THEN -qty ELSE 0 END) AS prd0,\n\t\t\t\t   \t\tSUM(CASE WHEN prd='" . $Period_1 . "' THEN -qty ELSE 0 END) AS prd1,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_2 . "' THEN -qty ELSE 0 END) AS prd2,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_3 . "' THEN -qty ELSE 0 END) AS prd3,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_4 . "' THEN -qty ELSE 0 END) AS prd4,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_5 . "' THEN -qty ELSE 0 END) AS prd5\n\t\t\t\t\tFROM stockmoves\n\t\t\t\t\tINNER JOIN locationusers ON locationusers.loccode=stockmoves.loccode AND locationusers.userid='" . $_SESSION['UserID'] . "' AND locationusers.canview=1\n\t\t\t\t\tWHERE stockid='" . $InventoryPlan['stockid'] . "'\n\t\t\t\t\tAND (type=10 OR type=11)\n\t\t\t\t\tAND stockmoves.hidemovt=0";
     } else {
         $SQL = "SELECT SUM(CASE WHEN prd='" . $CurrentPeriod . "' THEN -qty ELSE 0 END) AS prd0,\n\t\t\t\t   \t\tSUM(CASE WHEN prd='" . $Period_1 . "' THEN -qty ELSE 0 END) AS prd1,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_2 . "' THEN -qty ELSE 0 END) AS prd2,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_3 . "' THEN -qty ELSE 0 END) AS prd3,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_4 . "' THEN -qty ELSE 0 END) AS prd4,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_5 . "' THEN -qty ELSE 0 END) AS prd5\n\t\t\t\t\tFROM stockmoves\n\t\t\t\t\tINNER JOIN locationusers ON locationusers.loccode=stockmoves.loccode AND locationusers.userid='" . $_SESSION['UserID'] . "' AND locationusers.canview=1\n\t\t\t\t\tWHERE stockid='" . $InventoryPlan['stockid'] . "'\n\t\t\t\t\tAND stockmoves.loccode ='" . $_POST['Location'] . "'\n\t\t\t\t\tAND (stockmoves.type=10 OR stockmoves.type=11)\n\t\t\t\t\tAND stockmoves.hidemovt=0";
     }
     $SalesResult = DB_query($SQL, '', '', false, false);
     if (DB_error_no() != 0) {
         $Title = _('Inventory Planning') . ' - ' . _('Problem Report') . '....';
         include 'includes/header.inc';
         prnMsg(_('The sales quantities could not be retrieved by the SQL because') . ' - ' . DB_error_msg(), 'error');
         echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
    include 'includes/footer.inc';
    exit;
}
include 'includes/PDFInventoryValnPageHeader.inc';
$Tot_Val = 0;
$Category = '';
$CatTot_Val = 0;
while ($InventoryValn = DB_fetch_array($InventoryResult, $db)) {
    if ($Category != $InventoryValn['categoryid']) {
        $FontSize = 10;
        if ($Category != '') {
            /*Then it's NOT the first time round */
            /* need to print the total of previous category */
            if ($_POST['DetailedReport'] == 'Yes') {
                $YPos -= 2 * $line_height;
                $LeftOvers = $pdf->addTextWrap($Left_Margin, $YPos, 260 - $Left_Margin, $FontSize, _('Total for') . ' ' . $Category . ' - ' . $CategoryName);
            }
            $DisplayCatTotVal = locale_money_format($CatTot_Val, $_SESSION['CompanyRecord']['currencydefault']);
            $LeftOvers = $pdf->addTextWrap(500, $YPos, 60, $FontSize, $DisplayCatTotVal, 'right');
            $YPos -= $line_height;
            if ($_POST['DetailedReport'] == 'Yes') {
                /*draw a line under the CATEGORY TOTAL*/
                $pdf->line($Left_Margin, $YPos + $line_height - 2, $Page_Width - $Right_Margin, $YPos + $line_height - 2);
                $YPos -= 2 * $line_height;
            }
            $CatTot_Val = 0;
        }
        $LeftOvers = $pdf->addTextWrap($Left_Margin, $YPos, 260 - $Left_Margin, $FontSize, $InventoryValn['categoryid'] . " - " . $InventoryValn['categorydescription']);
        $Category = $InventoryValn['categoryid'];
        $CategoryName = $InventoryValn['categorydescription'];
    }
 $CurrentPeriod = GetPeriod(Date($_SESSION['DefaultDateFormat']), $db);
 $Period_1 = $CurrentPeriod - 1;
 $Period_2 = $CurrentPeriod - 2;
 $Period_3 = $CurrentPeriod - 3;
 $Period_4 = $CurrentPeriod - 4;
 while ($InventoryPlan = DB_fetch_array($InventoryResult, $db)) {
     if ($SupplierID != $InventoryPlan['supplierno']) {
         $FontSize = 10;
         if ($SupplierID != '') {
             /*Then it's NOT the first time round */
             /*draw a line under the supplier*/
             $YPos -= $line_height;
             $pdf->line($Left_Margin, $YPos, $Page_Width - $Right_Margin, $YPos);
             $YPos -= 2 * $line_height;
         }
         $LeftOvers = $pdf->addTextWrap($Left_Margin, $YPos, 260 - $Left_Margin, $FontSize, $InventoryPlan['supplierno'] . ' - ' . $InventoryPlan['suppname'], 'left');
         $SupplierID = $InventoryPlan['supplierno'];
         $FontSize = 8;
     }
     $YPos -= $line_height;
     if ($_POST['Location'] == 'All') {
         $SQL = "SELECT SUM(CASE WHEN (prd>='" . $Period_1 . "' OR\n\t\t\t\t\t\t\t\t\tprd<='" . $Period_4 . "') THEN -qty ELSE 0 END) AS 4mthtotal,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_1 . "' THEN -qty ELSE 0 END) AS prd1,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_2 . "' THEN -qty ELSE 0 END) AS prd2,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_3 . "' THEN -qty ELSE 0 END) AS prd3,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_4 . "' THEN -qty ELSE 0 END) AS prd4\n\t\t\t\t\t\tFROM stockmoves\n\t\t\t\t\t\tWHERE stockid='" . $InventoryPlan['stockid'] . "'\n\t\t\t\t\t\tAND (type=10 OR type=11)\n\t\t\t\t\t\tAND stockmoves.hidemovt=0";
     } else {
         $SQL = "SELECT SUM(CASE WHEN (prd>='" . $Period_1 . "' OR\n\t\t\t\t\t\t\t\t\tprd<='" . $Period_4 . "') THEN -qty ELSE 0 END) AS 4mthtotal,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_1 . "' THEN -qty ELSE 0 END) AS prd1,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_2 . "' THEN -qty ELSE 0 END) AS prd2,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_3 . "' THEN -qty ELSE 0 END) AS prd3,\n\t\t\t\t\t\tSUM(CASE WHEN prd='" . $Period_4 . "' THEN -qty ELSE 0 END) AS prd4\n\t\t\t\t\t\tFROM stockmoves\n\t\t\t\t\t\tWHERE stockid='" . $InventoryPlan['stockid'] . "'\n\t\t\t\t\t\tAND stockmoves.loccode ='" . $_POST['Location'] . "'\n\t\t\t\t\t\tAND (stockmoves.type=10 OR stockmoves.type=11)\n\t\t\t\t\t\tAND stockmoves.hidemovt=0";
     }
     $SalesResult = DB_query($SQL, $db, '', '', FALSE, FALSE);
     if (DB_error_no($db) != 0) {
         $Title = _('Inventory Planning') . ' - ' . _('Problem Report') . '....';
         include 'includes/header.inc';
         prnMsg(_('The sales quantities could not be retrieved by the SQL because') . ' - ' . DB_error_msg($db), 'error');
         echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
 if (DB_num_rows($result) > 0) {
     $FontSize = 10;
     $PageNumber = 1;
     include 'includes/PDFTransPageHeaderPortrait.inc';
     $FirstPage = False;
     while ($myrow2 = DB_fetch_array($result)) {
         if ($myrow2['discountpercent'] == 0) {
             $DisplayDiscount = '';
         } else {
             $DisplayDiscount = locale_number_format($myrow2['discountpercent'] * 100, 2) . '%';
             $DiscountPrice = $myrow2['fxprice'] * (1 - $myrow2['discountpercent']);
         }
         $DisplayNet = locale_number_format($myrow2['fxnet'], $myrow['decimalplaces']);
         $DisplayPrice = locale_number_format($myrow2['fxprice'], $myrow['decimalplaces']);
         $DisplayQty = locale_number_format($myrow2['quantity'], $myrow2['decimalplaces']);
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 5, $YPos, 71, $FontSize, $myrow2['stockid']);
         //Get translation if it exists
         $TranslationResult = DB_query("SELECT descriptiontranslation\n\t\t\t\t\t\t\t\t\t\t\t\tFROM stockdescriptiontranslations\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE stockid='" . $myrow2['stockid'] . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND language_id='" . $myrow['language_id'] . "'");
         if (DB_num_rows($TranslationResult) == 1) {
             //there is a translation
             $TranslationRow = DB_fetch_array($TranslationResult);
             $LeftOvers = $pdf->addTextWrap($Left_Margin + 80, $YPos, 186, $FontSize, $TranslationRow['descriptiontranslation']);
         } else {
             $LeftOvers = $pdf->addTextWrap($Left_Margin + 80, $YPos, 186, $FontSize, $myrow2['description']);
         }
         $lines = 1;
         while ($LeftOvers != '') {
             $LeftOvers = $pdf->addTextWrap($Left_Margin + 80, $YPos - 10 * $lines, 186, $FontSize, $LeftOvers);
             $lines++;
         }
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 270, $YPos, 76, $FontSize, $DisplayPrice, 'right');
 } else {
     $FontSize = 10;
     $PageNumber = 1;
     include 'includes/PDFTransPageHeader.inc';
     $FirstPage = False;
     while ($myrow2 = DB_fetch_array($result)) {
         if ($myrow2['discountpercent'] == 0) {
             $DisplayDiscount = '';
         } else {
             $DisplayDiscount = locale_number_format($myrow2['discountpercent'] * 100, 2) . '%';
             $DiscountPrice = $myrow2['fxprice'] * (1 - $myrow2['discountpercent']);
         }
         $DisplayNet = locale_number_format($myrow2['fxnet'], $myrow['decimalplaces']);
         $DisplayPrice = locale_number_format($myrow2['fxprice'], $myrow['decimalplaces']);
         $DisplayQty = locale_number_format($myrow2['quantity'], $myrow2['decimalplaces']);
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 3, $YPos, 95, $FontSize, $myrow2['stockid']);
         //Get translation if it exists
         $TranslationResult = DB_query("SELECT descriptiontranslation\n\t\t\t\t\t\t\t\t\t\t\t\t\tFROM stockdescriptiontranslations\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE stockid='" . $myrow2['stockid'] . "'\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND language_id='" . $myrow['language_id'] . "'");
         if (DB_num_rows($TranslationResult) == 1) {
             //there is a translation
             $TranslationRow = DB_fetch_array($TranslationResult);
             $LeftOvers = $pdf->addTextWrap($Left_Margin + 100, $YPos, 251, $FontSize, $TranslationRow['descriptiontranslation']);
         } else {
             $LeftOvers = $pdf->addTextWrap($Left_Margin + 100, $YPos, 251, $FontSize, $myrow2['description']);
         }
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 353, $YPos, 96, $FontSize, $DisplayPrice, 'right');
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 453, $YPos, 95, $FontSize, $DisplayQty, 'right');
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 553, $YPos, 35, $FontSize, $myrow2['units'], 'centre');
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 590, $YPos, 50, $FontSize, $DisplayDiscount, 'right');
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 642, $YPos, 120, $FontSize, $DisplayNet, 'right');
         $YPos -= $line_height;
 // Javier: needs check.
 $pdf->setPrintHeader(false);
 // Javier: I added this must be called before Add Page
 $pdf->AddPage();
 //	$this->SetLineWidth(1); 	   Javier: It was ok for FPDF but now is too gross with TCPDF. TCPDF defaults to 0'57 pt (0'2 mm) which is ok.
 $pdf->cMargin = 0;
 // Javier: needs check.
 /* END Brought from class.pdf.php constructor */
 $FontSize = 12;
 $line_height = 16;
 include 'includes/PDFOrderPageHeader.inc';
 while ($myrow2 = DB_fetch_array($result)) {
     $DisplayQty = locale_number_format($myrow2['quantity'], $myrow2['decimalplaces']);
     $DisplayPrevDel = locale_number_format($myrow2['qtyinvoiced'], $myrow2['decimalplaces']);
     $DisplayQtySupplied = locale_number_format($myrow2['quantity'] - $myrow2['qtyinvoiced'], $myrow2['decimalplaces']);
     $LeftOvers = $pdf->addTextWrap(13, $YPos, 135, $FontSize, $myrow2['stkcode']);
     $LeftOvers = $pdf->addTextWrap(148, $YPos, 239, $FontSize, $myrow2['description']);
     $LeftOvers = $pdf->addTextWrap(387, $YPos, 90, $FontSize, $DisplayQty, 'right');
     $LeftOvers = $pdf->addTextWrap(505, $YPos, 90, $FontSize, $DisplayQtySupplied, 'right');
     $LeftOvers = $pdf->addTextWrap(604, $YPos, 90, $FontSize, $DisplayPrevDel, 'right');
     if ($YPos - $line_height <= 136) {
         /* We reached the end of the page so finsih off the page and start a newy */
         $PageNumber++;
         include 'includes/PDFOrderPageHeader.inc';
     }
     //end if need a new page headed up
     /*increment a line down for the next line item */
     $YPos -= $line_height;
 }
 //end while there are line items to print out
 $pdf->OutputD($_SESSION['DatabaseName'] . '_Customer_Order_' . $_GET['TransNo'] . '_' . Date('Y-m-d') . '.pdf');
 if (DB_num_rows($result) > 0) {
     $FontSize = 10;
     $PageNumber = 1;
     include 'includes/PDFTransPageHeaderPortrait.inc';
     $FirstPage = False;
     while ($myrow2 = DB_fetch_array($result)) {
         if ($myrow2['discountpercent'] == 0) {
             $DisplayDiscount = '';
         } else {
             $DisplayDiscount = locale_number_format($myrow2['discountpercent'] * 100, 2) . '%';
             $DiscountPrice = $myrow2['fxprice'] * (1 - $myrow2['discountpercent']);
         }
         $DisplayNet = locale_number_format($myrow2['fxnet'], $myrow['decimalplaces']);
         $DisplayPrice = locale_number_format($myrow2['fxprice'], $myrow['decimalplaces']);
         $DisplayQty = locale_number_format($myrow2['quantity'], $myrow2['decimalplaces']);
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 5, $YPos, 71, $FontSize, $myrow2['stockid']);
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 80, $YPos, 186, $FontSize, $myrow2['description']);
         $lines = 1;
         while ($LeftOvers != '') {
             $LeftOvers = $pdf->addTextWrap($Left_Margin + 80, $YPos - 10 * $lines, 186, $FontSize, $LeftOvers);
             $lines++;
         }
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 270, $YPos, 76, $FontSize, $DisplayPrice, 'right');
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 350, $YPos, 36, $FontSize, $DisplayQty, 'right');
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 390, $YPos, 26, $FontSize, $myrow2['units'], 'center');
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 420, $YPos, 26, $FontSize, $DisplayDiscount, 'right');
         $LeftOvers = $pdf->addTextWrap($Left_Margin + 450, $YPos, 72, $FontSize, $DisplayNet, 'right');
         if ($myrow2['controlled'] == 1) {
             $GetControlMovts = DB_query("SELECT moveqty,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tserialno\n\t\t\t\t\t\t\t\t\t\t\t\t FROM stockserialmoves\n\t\t\t\t\t\t\t\t\t\t\t\t WHERE stockmoveno='" . $myrow2['stkmoveno'] . "'", $db);
             if ($myrow2['serialised'] == 1) {
                 while ($ControlledMovtRow = DB_fetch_array($GetControlMovts)) {