• Resolved Simba123

    (@simba123)


    Hi,
    I have two parts to my question.

    1. I have Advanced Custom Fields installed and I am using that to build an accounting system for myself.

    I’m working on the cashflow page and I need to show total money in per month and year automatically. eg. April 2015, Total money in is £100.

    My custom field (number) ‘total_invoice_cost’ is what gets the figure (£100). I have another custom field (text) ‘paid’ which will have the options ‘yes’ ‘no’ and ‘overdue’

    I need a way to show month by month the sum of invoices which have a figure inputted and are set to yes.

    Here is what I have so far:

    <?php
    $meta_key = 'total_invoice_cost';
    $annualincome = $wpdb->get_var( $wpdb->prepare(
    	"
    		SELECT sum(meta_value)
    		FROM $wpdb->postmeta
    		WHERE meta_key = %s
    
    	",
    	$meta_key
    ) );
    echo "<p>Total money in this year is {$annualincome}</p>";
    ?>

    This shows the sum of all the ‘total_invoice_cost’ fields filled out and it isn’t broken up by month either.

    So what needs to be done is only add if the custom field ‘paid’ is set to ‘yes’ and to also filter by month automatically.

    2. How can I show the sum of ‘total_invoice_cost’ by itself for the current month? So the code above is nearly there for this part of the question, I just need it to show the total for the current month (and changing automatically) so I can keep track of money due in.

    I hope this all makes sense, any help would be much appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Simba123

    (@simba123)

    Update:

    The following code works to an extent.
    It outputs the sum of the custom field ‘total_invoice_cost per month ONLY if the custom field ‘paid’ has a value of ‘yes’. (which is what I wanted)

    <?php
    global $wpdb;
    $results = $wpdb->get_results("SELECT SUM(x.total_invoice_cost),CONCAT( YEAR(x.post_date), '-', MONTH(x.post_date) ) AS grandtotal
    FROM (
    SELECT ID, total_invoice_cost.meta_value AS total_invoice_cost, paid.meta_value AS paid, wp_posts.post_date
    FROM wp_posts
    LEFT OUTER JOIN
    (SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key='paid') paid
    ON wp_posts.id=paid.post_id
    LEFT OUTER JOIN
    (SELECT post_id, meta_value, meta_key FROM wp_postmeta WHERE meta_key='total_invoice_cost') total_invoice_cost
    ON wp_posts.id=total_invoice_cost.post_id
    ) x
    WHERE x.total_invoice_cost IS NOT NULL AND x.paid='yes' GROUP BY CONCAT( YEAR(x.post_date), '-', MONTH(x.post_date)
    )");
    print "<p>\n";
    print_r($results);
    print "</p>";
    
    ?>

    However, it outputs the results like this:

    Array ( [0] => stdClass Object ( [SUM(x.total_invoice_cost)] => 150 [grandtotal] => 2015-4 ) )

    How can I edit this so that it shows it as £150 – 2015-4? I assume it has something to do with printing as a result, but I’m not sure how to change this.

    Thread Starter Simba123

    (@simba123)

    This is my final working code:

    <?php
    global $wpdb;
    $sum = '0';
    $results = $wpdb->get_results("SELECT SUM(x.total_invoice_cost) AS final ,CONCAT( YEAR(x.post_date), '-', MONTH(x.post_date) ) AS grandtotal
    FROM (
    SELECT ID, total_invoice_cost.meta_value AS total_invoice_cost, paid.meta_value AS paid, wp_posts.post_date
    FROM wp_posts
    LEFT OUTER JOIN
    (SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key='paid') paid
    ON wp_posts.id=paid.post_id
    LEFT OUTER JOIN
    (SELECT post_id, meta_value, meta_key FROM wp_postmeta WHERE meta_key='total_invoice_cost') total_invoice_cost
    ON wp_posts.id=total_invoice_cost.post_id
    ) x
    WHERE x.total_invoice_cost IS NOT NULL AND x.paid='yes' GROUP BY CONCAT( YEAR(x.post_date), '-', MONTH(x.post_date)
    )");
    
    if(!empty($results)) {
         foreach($results as $r) {
              echo "<p>".$r->grandtotal."</p>";
    	   	 echo "<p>£".$r->final."</p>";
         }
    } else {
         echo "<p>No invoices this month!</p>";
    } 
    
    ?>

    It outputs like this:

    2015-4
    £100
    
    2015-3
    £50

    I’m not sure if this is secure and/or open to SQL injections etc. I would appreciate it if someone could check it over and make sure it is all correct. 🙂

    Thread Starter Simba123

    (@simba123)

    If anyone needs to add multiple sums with different queries from the same table this is how you do it.

    My example:
    – I needed to show the sum of total_invoice_cost if paid had been set to ‘yes, no or overdue’.

    – Each sum case has a different alias ie. yes, overdue, no, grandtotal

    – These aliases are then used to individually show each sum (right at the bottom)

    Here is the code:

    <?php global $wpdb;
    $results = $wpdb->get_results(
      	"SELECT SUM(total_invoice_cost) AS expecting,
    	SUM(case WHEN paid='yes' THEN total_invoice_cost ELSE 0 END) AS yes,
    	SUM(case WHEN paid='overdue' THEN total_invoice_cost ELSE 0 END) AS overdue,
    	SUM(case WHEN paid='no' THEN total_invoice_cost ELSE 0 END) AS no,
    	CONCAT( YEAR(post_date), '-', MONTH(post_date) ) AS grandtotal
    
    	FROM (
    	SELECT ID, total_invoice_cost.meta_value AS total_invoice_cost, paid.meta_value AS paid, wp_posts.post_date
    	FROM wp_posts 
    
    	LEFT OUTER JOIN
    	(SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key='paid') paid
    	ON wp_posts.id=paid.post_id
    
    	LEFT OUTER JOIN
    	(SELECT post_id, meta_value, meta_key FROM wp_postmeta WHERE meta_key='total_invoice_cost') total_invoice_cost
    	ON wp_posts.id=total_invoice_cost.post_id
    	) x
    
    	WHERE total_invoice_cost IS NOT NULL
    	GROUP BY CONCAT( YEAR(post_date), '-', MONTH(post_date) )
    
    ");	
    
    if(!empty($results)) {
         foreach($results as $r) {
    	   	echo "<p>".$r->grandtotal."</p>";
    	    echo "<p>EXPECTING IN THIS MONTH £".$r->expecting."</p>";
    	   	echo "<p>PAID IN £".$r->yes."</p>";
    	   	echo "<p>NOT PAID £".$r->no."</p>";
    	    echo "<p>OVERDUE £".$r->overdue."</p>";
    
         }
    
    } else {
         echo "<p>No invoices!</p>";
    } 
    
      ?>

    And here is an example output:

    2014-3
    
    EXPECTING IN THIS MONTH £50
    
    PAID IN £50
    
    NOT PAID £0
    
    OVERDUE £0
    
    2015-3
    
    EXPECTING IN THIS MONTH £170
    
    PAID IN £100
    
    NOT PAID £70
    
    OVERDUE £0
    Thread Starter Simba123

    (@simba123)

    Marking as resolved.

    thank you…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘SQL sum of custom field split into months’ is closed to new replies.