$tmpHolder = "";
 $customerTotalAmount = 0;
 $customerTotalBalance = 0;
 $lastProjectAmount = 0;
 $lastProjectBalance = 0;
 $termValue = $customer['termValue'];
 $invoicesQuery = "\r\n\t\tSELECT\r\n\t\t\tinvoiceId,\r\n\t\t\tinvoiceDate,\r\n\t\t\tproject.projectId,\r\n\t\t\tprojectName,\r\n\t\t\tSUM(ticketAmount * itemCustomerCost) as totalAmount\r\n\t\tFROM\r\n\t\t\tproject\r\n\t\t\tJOIN invoice using (projectId)\r\n\t\t\tJOIN invoiceticket using (invoiceId)\r\n\t\t\tJOIN ticket using (ticketId)\r\n\t\t\tJOIN item using (itemId)\r\n\t\tWHERE\r\n\t\t\tcustomerId = " . $customer['customerId'] . "\r\n\t\tGROUP BY\r\n\t\t\tinvoiceId\r\n\t\tORDER BY\r\n\t\t\tprojectId,\r\n\t\t\tinvoiceId\r\n\t";
 $invoices = mysql_query($invoicesQuery, $conexion);
 $projectsIssued = array();
 if (mysql_num_rows($invoices) > 0) {
     $tmpHolder = "<tr><th class='customer' colspan='7'>" . $customer['customerName'] . "</th></tr>";
     while ($invoice = mysql_fetch_assoc($invoices)) {
         $paidInfo = mysql_fetch_assoc(mysql_query("SELECT COALESCE(SUM(receiptchequesAmount),0) as totalPaid, count(*) as totalCheques FROM receiptcheques WHERE invoiceId = " . $invoice['invoiceId'], $conexion));
         $invoiceAmount = decimalPad($invoice['totalAmount']);
         $invoicePaid = decimalPad($paidInfo['totalPaid']);
         $invoiceBalance = decimalFill(decimalPad($invoiceAmount - $invoicePaid));
         if ($invoiceBalance == 0) {
             continue;
         } else {
             $shouldAdd = true;
         }
         if (!isset($projectsIssued[$invoice['projectId']])) {
             if ($lastProjectAmount != 0) {
                 $tmpHolder .= "<tr><td colspan='5'></td><td align='right'>" . decimalPad($lastProjectAmount) . "</td><td align='right'>" . decimalPad($lastProjectBalance) . "</td></tr>";
                 $lastProjectAmount = 0;
                 $lastProjectBalance = 0;
             }
             $tmpHolder .= "<tr><td></td><th class='project' colspan='6' >" . $invoice['projectName'] . "</th></tr>";
             $projectsIssued[$invoice['projectId']] = 1;
         }
         $startingDate = strtotime($invoice['invoiceDate']);
    echo "<td>" . to_MDY($invoiceInfo1['ticketDate']) . "</td>";
    echo "<td>" . $invoiceInfo3['brokerPid'] . "-" . $invoiceInfo3['truckNumber'] . "</td>";
    echo "<td align=left >" . $invoiceInfo1['ticketMfi'];
    if ($invoiceInfo1['ticketNumber'] != "" && $invoiceInfo1['ticketNumber'] != $invoiceInfo1['ticketMfi']) {
        echo "/" . $invoiceInfo1['ticketNumber'];
    }
    echo "</td>";
    echo "<td align=left>" . $invoiceInfo2['materialName'] . "</td>";
    echo "<td align=left>" . $invoiceInfo2['itemDisplayFrom'] . "</td>";
    echo "<td align=left>" . $invoiceInfo2['itemDisplayTo'] . "</td>";
    echo "<td >" . decimalPad($invoiceInfo1['ticketAmount']);
    echo "</td>";
    echo "<td align=right >" . decimalPad($invoiceInfo2['itemCustomerCost']);
    echo "</td>";
    echo "<td align=right >" . decimalPad($invoiceInfo1['ticketAmount'] * $invoiceInfo2['itemCustomerCost']);
    echo "</td>";
    echo "</tr>";
    $total += $invoiceInfo1['ticketAmount'] * $invoiceInfo2['itemCustomerCost'];
    $count++;
}
echo "<tr><td colspan='2' align=center> {$count} Tickets </td><td colspan='7'></td></tr>";
echo "<tr><td colspan='7'></td><th><span>Total</span></th><td align=right >" . decimalFill(decimalPad($total));
echo "</td></tr>";
?>
</table>
</form>

</body>
</html>

								<th width=15>Balance</th>
								<th class='last' width='8%' colspan='2'></th>
							</tr>
							<tbody>
						";	
					$colorFlag=true;
						while($invoice = mysql_fetch_assoc($invoices)){
							$paid='Unpaid';
							$queryPaid="select SUM( receiptchequesAmount ) AS chequetotal, COUNT(*) as number from receiptcheques where invoiceId=".$invoice['invoiceId'];
							$paidReg=mysql_query($queryPaid,$conexion);
							$paidInfo = mysql_fetch_assoc($paidReg);
							
							$paidTotal = decimalPad($paidInfo['chequetotal']==null?0:$paidInfo['chequetotal']);
							$cheques = $paidInfo['number'];
							$invoiceTotal = decimalPad($invoice['invoiceTotal']==null?0:$invoice['invoiceTotal']);
							$invoiceBalance = decimalFill( decimalPad($invoiceTotal - $paidTotal));
							
							if($paidTotal > 0 && $paidTotal >= $invoiceTotal ) {$paid='Paid';}
							else if($paidTotal>0 && $paidTotal < $invoiceTotal ) {$paid='Warning';}
							else if($paidTotal == NULL ) {$paid='Unpaid';}
							
							if($colorFlag){echo "<tr class='even".$paid."' id='invoice".$invoice['invoiceId']."'>";}
							else{echo "<tr class='odd".$paid."' id='invoice".$invoice['invoiceId']."'>";}
							$colorFlag=!$colorFlag;
							
							echo "<td class='first style2' width='7%'>".$invoice['projectId']."</td>";
							echo "<td class='first style2' width='7%'>".$invoice['invoiceId']."</td>";
							echo "<td width=10>".$invoice['projectName']."</td>";
							echo "<td width='9%'>".to_MDY($invoice['invoiceDate'])."</td>";
							echo "<td width='9%'>".to_MDY($invoice['invoiceStartDate'])."</td>";
							echo "<td width='9%'>".to_MDY($invoice['invoiceEndDate'])."</td>";