I am working on cakephp project where i've to generate the reports. I've two tables one table stores the value of customer and another table store the value of leads assigned to each user. Each lead is having status. I want to increment the counter of progressleads if status is 0, if status is 1 then counter of wonleads should be increment and so on...
I am joining two tables and getting array like this =>
Array
(
[0] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)
[Opportunity] => Array
(
[status] => 0
[value] => 50000
)
)
[1] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)
[Opportunity] => Array
(
[status] => 1
[value] => 10000
)
)
[2] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)
[Opportunity] => Array
(
[status] => 1
[value] => 7500
)
)
[3] => Array
(
[Customer] => Array
(
[id] => 15
[name] => DEF
)
[Opportunity] => Array
(
[status] => 0
[value] => 45000
)
)
[4] => Array
(
[Customer] => Array
(
[id] => 19
[name] => ST
)
[Opportunity] => Array
(
[status] => 2
[value] => 50000
)
)
[5] => Array
(
[Customer] => Array
(
[id] => 16
[name] => TEST
)
[Opportunity] => Array
(
[status] => 2
[value] => 1000000
)
)
[6] => Array
(
[Customer] => Array
(
[id] => 19
[name] => ST
)
[Opportunity] => Array
(
[status] => 0
[value] => 1000
)
)
[7] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)
[Opportunity] => Array
(
[status] => 0
[value] =>
)
)
)
From this array i want one record for each users with total leads, inprogress leads, won leads counter. I've tried following code :-
$customerdetails = array();
$totalopp = 0;
$progressopp = 0;
$oppval = 0;
$wonopp = 0;
$lostopp = 0;
$billedopp = 0;
$onholdopp = 0;
$newcustid = NULL;
foreach($customer as $k => $val){
$custid = $val["Customer"]["id"];
if($newcustid != $custid){
$oppstatus = $val["Opportunity"]["status"];
$oppval += $val["Opportunity"]["opo_value"];
$totalopp++;
if($oppstatus == 0){
$progressopp++;
}
if($oppstatus == 1){
$wonopp++;
}
if($oppstatus == 2){
$lostopp++;
}
if($oppstatus == 3){
$billedopp++;
}
if($oppstatus == 4){
$onholdopp++;
}
$newcustid = $custid;
}
$customerdetails[$custid]["customername"] = $val["Customer"]["customer_name"];
$customerdetails[$custid]["opportunities"] = $totalopp;
$customerdetails[$custid]["value"] = $oppval;
$customerdetails[$custid]["inprogress"] = $progressopp;
$customerdetails[$custid]["won"] = $wonopp;
$customerdetails[$custid]["lost"] = $lostopp;
$customerdetails[$custid]["billed"] = $billedopp;
$customerdetails[$custid]["onhold"] = $onholdopp;
}
After printing $customerdetails array i am getting following results =>
Array
(
[14] => Array
(
[customername] => ABC
[opportunities] => 6
[value] => 1146000
[inprogress] => 4
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)
[15] => Array
(
[customername] => DEF
[opportunities] => 2
[value] => 95000
[inprogress] => 2
[won] => 0
[lost] => 0
[billed] => 0
[onhold] => 0
)
[19] => Array
(
[customername] => ST
[opportunities] => 5
[value] => 1146000
[inprogress] => 3
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)
[16] => Array
(
[customername] => TEST
[opportunities] => 4
[value] => 1145000
[inprogress] => 2
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)
)
As you can see there are only 4 leads assigned to ABC but it is showing opportunities as 6 similarly the other counters are also displaying incorrect. Can anyone help me what i am doing wrong here?