0
I'm looking to send to the screen the annual total payments and total invoices for customers.

It sounds very easy, right. If I combine the total payments and the total invoices in one query, the results are incorrect.

If I do two separate queries, the results are correct. However, I am unable to display the correct values in the table format. As you can see, the Invoice amount for each customer is the same. However, I'm echoing the values to the screen to prove that my Invoice query is getting the correct results -- it is simply not assigning them correctly. See image attached.

Here are my queries:

    		///////INVOICES QUERY

// the MySQL query that gets pulled from the database
$queryInvoices = "SELECT
o70vm_invoices_contacts.name AS 'Parent_Name',
o70vm_invoices_contacts.id AS 'Parent_ID',
o70vm_invoices_contacts.company AS 'Children',

o70vm_invoices_invoices.id AS 'Invoice_ID',
o70vm_invoices_invoices.user_id AS 'Parent_ID_on_Invoice',
o70vm_invoices_invoices.invoice_num AS 'Invoice_Num',
o70vm_invoices_invoices.invoice_date AS 'Invoice_Date',
o70vm_invoices_invoices.invoice_duedate AS 'Invoice_Date_Due',

o70vm_invoices_items.id AS 'Item_ID',
o70vm_invoices_items.invoice_id AS 'Invoice_ID_on_Items',
o70vm_invoices_items.value AS 'Fee',
o70vm_invoices_items.amount AS 'Qty',

ROUND(SUM((o70vm_invoices_items.value)*(o70vm_invoices_items.amount)),2) AS 'INV-TOTAL'

FROM o70vm_invoices_invoices, o70vm_invoices_contacts, o70vm_invoices_items
WHERE
o70vm_invoices_contacts.id = o70vm_invoices_invoices.user_id
AND
o70vm_invoices_items.invoice_id = o70vm_invoices_invoices.id
AND
o70vm_invoices_invoices.invoice_duedate >= STR_TO_DATE('2014-01-01 00:00:00', '%Y-%m-%d %H:%i:%s')
AND
o70vm_invoices_invoices.invoice_duedate <= NOW()

GROUP BY o70vm_invoices_contacts.id

ORDER BY o70vm_invoices_contacts.company";

// storing the result of this MySQL query
$resultInvoicesQuery = mysql_query($queryInvoices) or die(mysql_error());

while($rowInvoices = mysql_fetch_array($resultInvoicesQuery))
{

$InvoiceTotal = $rowInvoices['INV-TOTAL'];

// testing to print results to screen
// displays correctly
// unable to bring this figure to display in main customer display on front end.

echo "InvoiceTotal:" .$InvoiceTotal;


}


////// PAYMENT QUERY

// the MySQL query that gets pulled from the database
$query = "SELECT
o70vm_invoices_contacts.name AS 'Parent_Name',
o70vm_invoices_contacts.id AS 'Parent_ID',
o70vm_invoices_contacts.company AS 'Children',

o70vm_invoices_invoices.id AS 'Invoice_ID',
o70vm_invoices_invoices.user_id AS 'Parent_ID_on_Invoice',
o70vm_invoices_invoices.invoice_num AS 'Invoice_Num',
o70vm_invoices_invoices.invoice_date AS 'Invoice_Date',
o70vm_invoices_invoices.invoice_duedate AS 'Invoice_Date_Due',

o70vm_invoices_payments.invoice_id,
SUM(o70vm_invoices_payments.payment_amount) AS 'Paid'

FROM o70vm_invoices_invoices, o70vm_invoices_contacts, o70vm_invoices_payments
WHERE
o70vm_invoices_contacts.id = o70vm_invoices_invoices.user_id
AND
o70vm_invoices_payments.invoice_id = o70vm_invoices_invoices.id
AND
o70vm_invoices_payments.payment_datetime >= STR_TO_DATE('2014-01-01 00:00:00', '%Y-%m-%d %H:%i:%s')
AND
o70vm_invoices_payments.payment_datetime <= NOW()

GROUP BY o70vm_invoices_contacts.id

ORDER BY o70vm_invoices_contacts.company";


// storing the result of this MySQL query
$result = mysql_query($query) or die(mysql_error());


//declaring and initalizing row count
$row_count = 0;



// beginning the process of displaying the table


echo "<table width='80%' border='1'>
<tr>
<td><strong>#</strong></td>
<td><strong>Parent</strong></td>
<td><strong>Children</strong></td>
<td><strong>Total Paid</strong></td>
<td><strong>Total Invoices</strong></td>
<td><strong>Account Balance</strong></td>
</tr>";


while($row = mysql_fetch_array($result))
{

// counting the rows
$row_count++;

// The item description based on the ID of the invoice
// Writing the values to the table

echo "<tr>";
echo "<td>".$row_count."</td>";
echo "<td>" . $row['Parent_Name'] . "</td>";
echo "<td>" . $row['Children'] . "</td>";
echo "<td align='right'>$". $row['Paid'] . "</td>";

$TotalParentPayments += $row['Paid'];

echo "<td align='right'>$$InvoiceTotal</td>";

$AccountBalance = $InvoiceTotal - $row['Paid'];


echo "<td>$" .$AccountBalance. "</td>";
echo "</tr>";


}





echo "</table>";


I really hate to keep bothering you but your help is really making this project near completion.

Thanks again,

Ken

[1]: http://i.stack.imgur.com/lu4qw.jpg
Attachments:
Responses (4)
  • Accepted Answer

    Friday, May 23 2014, 10:02 AM - #Permalink
    0
    well my friend, at first sight I can see a wrong "$$InvoiceTotal" in the display TD for the invoice total.

    there's a double $$ that shouldn't be there.
    The reply is currently minimized Show
  • Accepted Answer

    Friday, May 23 2014, 02:59 PM - #Permalink
    0
    Thanks. The first "$" is the dollar sign that prints out in front of the numbers. It appears to not make a difference in the calculation of Total Invoices. The calculation is fine. It is just how to place that calculation into the column that I'm struggling with as the attachment illustrates.

    Thanks again.
    The reply is currently minimized Show
  • Accepted Answer

    Friday, May 23 2014, 03:06 PM - #Permalink
    0
    but Ken.. no offense but the syntax is completely wrong. you are printing a variable which is not part of the "row"... it will just print the last value (taht is the value of the last row on your query)

    in any case you may want to print $row['INV-TOTAL']... not $InvoiceTotal

    remember you're in a bucle (a FOR statement)...
    The reply is currently minimized Show
  • Accepted Answer

    Tuesday, May 27 2014, 12:45 AM - #Permalink
    0
    Yeah, the issue I'm having is I have two different queries but I'm thinking I likely need to have one query with a sub-query (I never really understood them). I can have the correct total invoices appear and the total payments appear in two separate queries, I just can't have the total invoices appear correct in the table view. I'm echoing out the total invoices in the first query and it works fine.

    In short, where the red arrows are I am testing the results of my first query but when I try to echo out these values in the Invoices column (green box), we only pull the last total. Other coding variations (ie, $row['INV-TOTAL'].) do not work as INV-TOTAL is not included in the second query and still do not result in the correct result.
    Attachments:
    The reply is currently minimized Show
Your Reply