Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

What does this mean?

I've got a SELECT statement configured in PHP, the last part of which looks like this:

 and cg.id in (select chargegroupid from txn where accountid = $statement[AccountID]) "
            . (
                $filterDate ?
                    "
                    and cg.id not in (
                        select chargegroupid from txn where accountid = $statement[AccountID] group by chargegroupid having max(posted) > dbo.ufn_lastpaperstatementbefore($statement[AccountID], cast('2050-01-01' as date))
                    )"
                    :
                    ""
            ) . "
            group by t1.accountid, t1.chargegroupid, t1.encountercode "
            . (
                is_int($ageOutZeroAfter)?
                    " having datediff(d, max(t1.created), getDate()) <= $ageOutZeroAfter or sum(amount) <> 0 "
                    :
                    ""
            ) . "
order by dos";

Open in new window


When you print that portion of the sql out in MSSQL Studio, it looks like this:

cg.id in (select chargegroupid from txn where accountid = 12159877) group by t1.accountid, t1.chargegroupid, t1.encountercode having datediff(d, max(t1.created), getDate()) <= 45 or sum(amount) <> 0 order by dos

I didn't want to thicken the plot unnecessarily, but for the sake of ensuring you've got a complete picture of what's going on, here's the whole function:

protected static function getOldTransactions(
        $statement,
        $showPhysician = true,
        $filterDate = true,
        $ageOutZeroAfter = false
    ){
        /**
        Get the Old Transactions
         **/
        //krumo('oldtxn');
        $sql = "
            select
                t1.accountid,
                t1.chargegroupid,
                t1.encountercode,
                /* Max automatically disregards nulls */" . (
                    $showPhysician ?
                    "
                    coalesce(max(physician), '')
                    "
                    :
                    ("'" .
                    str_replace("'", "''", $statement["PracticeName"])
                    . "'")
                ) . " as provider,
                sum(case when t1.type = 'c' then amount else 0 end) as total_charges,
                sum(case when t1.type = 'a' then amount else 0 end) as total_adjustments,
                sum(case when t1.type = 'p' then amount else 0 end) as total_payments,
                sum(amount) as left_to_pay,
                /* Charge description */
                /* blank for now */
                '' as description,
                 cast(min(case when t1.type = 'c' then dos else null end) as date) as dos

            from txn t1
                left join chargegroup cg on t1.chargegroupid = cg.id
                /* Charge description join */
                left join (
                    select
                        max(case when (txn.type = 'C') then amount else null end ) as max_charge_amount,
                        accountid,
                        chargegroupid
                    from txn
                    group by accountid, chargegroupid
                ) t2 on t2.accountid = t1.accountid and t2.chargegroupid = t1.chargegroupid
                where t1.accountid = $statement[AccountID]
                and reversedref is null
                and (cg.status1 not in('rt','hd','rp','fc') or cg.status1 is null)
                and cg.id in (select chargegroupid from txn where accountid = $statement[AccountID]) "
            . (
                $filterDate ?
                    "
                    and cg.id not in (
                        select chargegroupid from txn where accountid = $statement[AccountID] group by chargegroupid having max(posted) > dbo.ufn_lastpaperstatementbefore($statement[AccountID], cast('2050-01-01' as date))
                    )"
                    :
                    ""
            ) . "
            group by t1.accountid, t1.chargegroupid, t1.encountercode "
            . (
                is_int($ageOutZeroAfter)?
                    " having datediff(d, max(t1.created), getDate()) <= $ageOutZeroAfter or sum(amount) <> 0 "
                    :
                    ""
            ) . "

        order by dos";
		echo $sql;
        return StatementImage::runQuery($sql);
    }

}

Open in new window


so $filterDate is true and  $ageOutZeroAfter = false

I'm not certain if the "?" represents the equivalent to an IF clause or a CASE dynamic. But what would the purpose be if the properties are fixed?

Bottom line: I don't understand the purpose of the "?" and the overall flow.

Thoughts?
SOLUTION
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Thank you!

So, basically, since $filterDate is always true, I can expect...

   and cg.id not in (
                        select chargegroupid from txn where accountid = $statement[AccountID] group by chargegroupid having max(posted) > dbo.ufn_lastpaperstatementbefore($statement[AccountID], cast('2050-01-01' as date))
                    )

Open in new window


...to always run. I'll never have an empty string.

Got it!

And Julian, I went out and looked up "ternary." I see how it's an appropriate label in that you're looking at "three" elements as part of building the IF dynamic. Makes sense, although I can see how there's a lot of room for error.

I was able to go back into the code and see how every time this method is called, the $ageOutZeroAfter value is passed into the method as the number 45, which I'm assuming overrides the property as it's defined by default as "false."

Correct?

Thank you, gentlemen!
Yes, if $ageOutZeroAfter is passed in, it will override the default 'false'.

That being said, it's against generally accepted good coding practices to allow a variable to change data types.  In this case, $ageOutZeroAfter could be both a boolean and a number.  PHP is not a strongly typed language, so it allows variables to change data types.  But just because it allows it doesn't mean it should be done.  It's a potential source for bugs.  So it should be avoided.  Booleans variables should only be assigned true / false.  Numeric variables should only be assigned a number.
It's funny that you say that because this is the way the method is called earlier in the page:

$number_of_days_to_keep_zero_balance_encounters_on_the_statement = 45;
$st['oldtxns'] = StatementImage::getOldTransactions(
      $st,
      $st['showphysician'] == '1',
      $st['StatementRollUp'] == '0',
      $number_of_days_to_keep_zero_balance_encounters_on_the_statement
);

Here's the way the method appears on the page:

    protected static function getOldTransactions(
        $statement,
        $showPhysician = true,
        $filterDate = true,
        $ageOutZeroAfter = false
    ){

So, if I understand you correctly and I'm reading this right, $filterDate is being set by $st['StatementRollUp'] which is 0. That's going to resonate as "false-y," will it not? That means that part of the method will never run, thus making a large part of this SELECT irrelevant.

There you go...

Thanks!

BTW: I would love y'all to take a look at my code samples and give me some input: https://www.experts-exchange.com/questions/28974267/My-Code-Samples-What-Do-You-Think.html
Yes, the string '0' will be interpreted as false.