Cakephp sum() with multiplication

##Is it possible to find the cakephp with additional condition or can we multiply the sum() field with conditional variable? I don’t know, what could be a best question to let you guys know what I’m asking. so I’m giving a more detail about question. I’m able to find similar data with single array with some trick. but I’m unable to find for this. Please help me or give me an idea to do such task. Thanks.

Using CakePhp 2.6

I want to find data from first table here…

$incentiveweekly=$this->Incentive->find(‘all’,
array(‘conditions’ =>
array(
“AND”=>array(
“Incentive.fromdate >=” => $from,
“Incentive.todate <=” => $to
)
)
)
);

according to number of rows. I have to find another table

Here is the result of above find condition.
Array
(
[Incentive] => Array
(
[id] => 2
[target1] => 3000
[price1] => 1.5
[target2] => 6000
[price2] => 2.5
[target3] => 8000
[price3] => 3.5
[formonth] =>
[type] => 1
[fromdate] => 2016-11-13
[todate] => 2016-11-21
[updatedby] => 1
[created] => 2016-11-15 23:57:21
[modified] => 2016-11-15 23:57:21
)

        )
        Array
        (
            [Incentive] => Array
                (
                    [id] => 3
                    [target1] => 3000
                    [price1] => 1.5
                    [target2] => 6000
                    [price2] => 2.5
                    [target3] => 8000
                    [price3] => 3.5
                    [formonth] => 
                    [type] => 1
                    [fromdate] => 2016-11-24
                    [todate] => 2016-11-28
                    [updatedby] => 1
                    [created] => 2016-11-15 23:57:21
                    [modified] => 2016-11-15 23:57:21
                )

        )

Now I want to find the array according to number of array record.

$byweek=array();    // Storing Customer data by Target dates in array()             
foreach ($incentiveweekly as $weekly){ 
    print_r($weekly);
    $target3=$weekly['Incentive']['target3'];
    $target2=$weekly['Incentive']['target2'];
    $target1=$weekly['Incentive']['target1'];
    $price3=$weekly['Incentive']['price3'];
    $price2=$weekly['Incentive']['price2'];
    $price1=$weekly['Incentive']['price1'];
    $byweek[]=$customers=$this->Customer->find('all',array(
        'fields'=>array(
        'SUM(amount) AS amount',
        'created_by'
        ),
        'group' => 'Customer.created_by',
        'conditions' => array(
                        "AND" =>array(
                            "Customer.created >=" => $weekly['Incentive']['fromdate'], 
                            "Customer.created <=" => $weekly['Incentive']['todate']
                            )
                        ),
        'recursive'=>-1     )
                );
    //print_r($byweek);
    }

I’m getting result like …

        Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [Customer] => Array
                                (
                                    [created_by] => 3
                                )

                        )

                )

        )
        Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [Customer] => Array
                                (
                                    [created_by] => 3
                                )

                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [Customer] => Array
                                (
                                    [created_by] => 1
                                )

                        )

                    [1] => Array
                        (
                            [Customer] => Array
                                (
                                    [created_by] => 2
                                )

                        )

                )

        )

But I want that amount would multiply with on the if else condition where I’m using ternary operator.

$value[0]['amount']>=$valuem['Incentive']['target3']?"Target Third":($value[0]['amount']>=$valuem['Incentive']['target2']?"Target Second":($value[0]['amount']>=$valuem['Incentive']['target1']?"Target First":"None"))

Main purpose to find the details are. I want to create an incentive amount where total sales amount should be match with given target amount. If target1 amount >=totalamount then incentive would be total amount*price1 and same with target2 and target3. where i’m using tenantry operator. Target price should be multiply(by condition) with the same find condition data.

Thanks Oldskool for this solution Virtualfields with If Condition

Finaly I, Got my solutions by this code bellow. I’m getting result as expected. :slight_smile:

$this->Customer->virtualFields=array(
  	  'incentive' => "if(SUM(Customer.amount)>=$target3,(SUM(Customer.amount))*$price3,if(SUM(Customer.amount)>=$target2,(SUM(Customer.amount))*$price2,if(SUM(Customer.amount)>=$target1,(SUM(Customer.amount))*$price1,0)))",
);

Now I can use Virtualfied to get the Incentive value.