FIX_dashboard Pavithra query implemented
This commit is contained in:
parent
32b29af518
commit
9679619553
@ -428,7 +428,10 @@ class DashboardController extends ResourceController
|
|||||||
public function businessDashboard()
|
public function businessDashboard()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$raw = false;
|
||||||
$manager_id = $this->request->getGet('manager_id');
|
$manager_id = $this->request->getGet('manager_id');
|
||||||
|
$raw_param = strtolower($this->request->getGet('raw') ?? '');
|
||||||
|
$raw = ($raw_param === 'true' || $raw_param === '1');
|
||||||
|
|
||||||
// Initialize the details array to ensure the final output is always structured.
|
// Initialize the details array to ensure the final output is always structured.
|
||||||
$details = [ 'brokerWise' => [], 'productWise' => [], 'insurerWise' => []];
|
$details = [ 'brokerWise' => [], 'productWise' => [], 'insurerWise' => []];
|
||||||
@ -437,9 +440,9 @@ class DashboardController extends ResourceController
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. Fetch all data concurrently (or sequentially, as shown)
|
// 1. Fetch all data concurrently (or sequentially, as shown)
|
||||||
$details['brokerWise'] = $this->brokerWise($manager_id);
|
$details['brokerWise'] = $this->brokerWise($manager_id,$raw);
|
||||||
$details['productWise'] = $this->productWise($manager_id);
|
$details['productWise'] = $this->productWise($manager_id,$raw);
|
||||||
$details['insurerWise'] = $this->insurerWise($manager_id);
|
$details['insurerWise'] = $this->insurerWise($manager_id,$raw);
|
||||||
|
|
||||||
// 2. Check for overall emptiness (optional, but good practice)
|
// 2. Check for overall emptiness (optional, but good practice)
|
||||||
$is_empty = array_reduce($details, function ($carry, $item) {
|
$is_empty = array_reduce($details, function ($carry, $item) {
|
||||||
@ -492,7 +495,11 @@ class DashboardController extends ResourceController
|
|||||||
public function partnerDashboard()
|
public function partnerDashboard()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
$manager_id = $this->request->getGet('manager_id');
|
$manager_id = $this->request->getGet('manager_id');
|
||||||
|
$raw_param = strtolower(trim($this->request->getGet('raw') ?? ''));
|
||||||
|
$raw = in_array($raw_param, ['true', '1'], true);
|
||||||
|
|
||||||
// Initialize the details array to ensure the final output is always structured.
|
// Initialize the details array to ensure the final output is always structured.
|
||||||
$code = 200;
|
$code = 200;
|
||||||
$message = "Partner Dashboard Data Retrieved Successfully";
|
$message = "Partner Dashboard Data Retrieved Successfully";
|
||||||
@ -503,14 +510,11 @@ class DashboardController extends ResourceController
|
|||||||
// 1. Fetch all data concurrently (or sequentially, as shown)
|
// 1. Fetch all data concurrently (or sequentially, as shown)
|
||||||
$date = $this->request->getGet('date');
|
$date = $this->request->getGet('date');
|
||||||
$date = $date ?? date('Y-m-d');
|
$date = $date ?? date('Y-m-d');
|
||||||
$limit = $limit ?? 50;
|
|
||||||
$value = $value ?? 10;
|
|
||||||
$unit = $unit ?? 'DAY';
|
|
||||||
|
|
||||||
$details['monthlyPolicyCount'] = $this->agentMonthlyPolicyCount($date,$limit,$manager_id);
|
$details['monthlyPolicyCount'] = $this->agentMonthlyPolicyCount($date,$manager_id,$raw);
|
||||||
$details['monthlyPremiumAmount'] = $this->agentMonthlyPremiumAmount($date,$limit,$manager_id);
|
$details['monthlyPremiumAmount'] = $this->agentMonthlyPremiumAmount($date,$manager_id,$raw);
|
||||||
$details['noPolicyTimeRange'] = $this->agentNoPolicyTimeRange($value,$unit,$manager_id);
|
$details['noPolicyTimeRange'] = $this->agentNoPolicyTimeRange($manager_id,$raw);
|
||||||
$details['below50tPremiumTimeRange'] = $this->agentBelow50tPremiumTimeRange($value,$unit,$manager_id);
|
$details['below50tPremiumTimeRange'] = $this->agentBelow50tPremiumTimeRange($manager_id,$raw);
|
||||||
|
|
||||||
|
|
||||||
// 2. Check for overall emptiness (optional, but good practice)
|
// 2. Check for overall emptiness (optional, but good practice)
|
||||||
@ -562,7 +566,10 @@ class DashboardController extends ResourceController
|
|||||||
|
|
||||||
public function productivityDashboard(){
|
public function productivityDashboard(){
|
||||||
|
|
||||||
|
$raw = false;
|
||||||
$manager_id = $this->request->getGet('manager_id');
|
$manager_id = $this->request->getGet('manager_id');
|
||||||
|
$raw_param = strtolower($this->request->getGet('raw') ?? '');
|
||||||
|
$raw = ($raw_param === 'true' || $raw_param === '1');
|
||||||
|
|
||||||
// Initialize the details array to ensure the final output is always structured.
|
// Initialize the details array to ensure the final output is always structured.
|
||||||
$details = [ 'StaffWise' => []];
|
$details = [ 'StaffWise' => []];
|
||||||
@ -571,8 +578,7 @@ class DashboardController extends ResourceController
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. Fetch all data concurrently (or sequentially, as shown)
|
// 1. Fetch all data concurrently (or sequentially, as shown)
|
||||||
$details['StaffWise'] = $this->staffWiseProductList($manager_id);
|
$details['StaffWise'] = $this->staffWiseProductList($manager_id,$raw);
|
||||||
// $details['ProductWise'] = $this->productivityProductWise($manager_id);
|
|
||||||
|
|
||||||
// 2. Check for overall emptiness (optional, but good practice)
|
// 2. Check for overall emptiness (optional, but good practice)
|
||||||
$is_empty = array_reduce($details, function ($carry, $item) {
|
$is_empty = array_reduce($details, function ($carry, $item) {
|
||||||
@ -587,7 +593,7 @@ class DashboardController extends ResourceController
|
|||||||
// 3. Return a successful, combined JSON response
|
// 3. Return a successful, combined JSON response
|
||||||
return $this->response->setJSON([
|
return $this->response->setJSON([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'data' => $details, // Contains the nested arrays: brokerWise, productWise, insurerWise
|
'data' => $details,
|
||||||
'code' => 200,
|
'code' => 200,
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
'ref' => 'NIL'
|
'ref' => 'NIL'
|
||||||
@ -622,7 +628,7 @@ class DashboardController extends ResourceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Step 1.1
|
//Step 1.1
|
||||||
public function insurerWise($manager_id){
|
public function insurerWise($manager_id, $raw){
|
||||||
|
|
||||||
$builder = $this->db->table('partner_policy pp');
|
$builder = $this->db->table('partner_policy pp');
|
||||||
|
|
||||||
@ -690,7 +696,7 @@ class DashboardController extends ResourceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Step 1.2
|
//Step 1.2
|
||||||
public function brokerWise($manager_id){
|
public function brokerWise($manager_id, $raw){
|
||||||
|
|
||||||
$builder = $this->db->table('partner_policy pp');
|
$builder = $this->db->table('partner_policy pp');
|
||||||
$builder->select("
|
$builder->select("
|
||||||
@ -752,7 +758,7 @@ class DashboardController extends ResourceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Step 1.3
|
//Step 1.3
|
||||||
public function productWise($manager_id){
|
public function productWise($manager_id , $raw){
|
||||||
$builder = $this->db->table('partner_policy pp');
|
$builder = $this->db->table('partner_policy pp');
|
||||||
$builder->select("
|
$builder->select("
|
||||||
pp.vehicle_type,
|
pp.vehicle_type,
|
||||||
@ -810,11 +816,8 @@ class DashboardController extends ResourceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Step 2.1
|
//Step 2.1
|
||||||
public function agentMonthlyPolicyCount($customDate,$limit,$manager_id){
|
public function agentMonthlyPolicyCount($customDate,$manager_id, $raw){
|
||||||
|
|
||||||
// 2.1
|
|
||||||
// $customDate = $custom_date; // '2025-11-05' or CURRENT_DATE()
|
|
||||||
// $limit = 50;
|
|
||||||
|
|
||||||
$builder = $this->db->table('partner_policy pp');
|
$builder = $this->db->table('partner_policy pp');
|
||||||
|
|
||||||
@ -835,7 +838,7 @@ class DashboardController extends ResourceController
|
|||||||
|
|
||||||
$builder->groupBy(['pa.id']);
|
$builder->groupBy(['pa.id']);
|
||||||
$builder->orderBy('policy_count', 'DESC');
|
$builder->orderBy('policy_count', 'DESC');
|
||||||
$builder->limit($limit);
|
$builder->limit(50);
|
||||||
|
|
||||||
$result = $builder->get()->getResultArray();
|
$result = $builder->get()->getResultArray();
|
||||||
return $result;
|
return $result;
|
||||||
@ -843,11 +846,8 @@ class DashboardController extends ResourceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Step 2.2
|
//Step 2.2
|
||||||
public function agentMonthlyPremiumAmount($customDate,$limit,$manager_id){
|
public function agentMonthlyPremiumAmount($customDate,$manager_id, $raw){
|
||||||
|
|
||||||
// 2.2
|
|
||||||
// $customDate = $custom_date; // Example '2025-11-05'
|
|
||||||
// $limit = 50;
|
|
||||||
$builder = $this->db->table('partner_policy pp');
|
$builder = $this->db->table('partner_policy pp');
|
||||||
|
|
||||||
$builder->select("
|
$builder->select("
|
||||||
@ -858,27 +858,26 @@ class DashboardController extends ResourceController
|
|||||||
");
|
");
|
||||||
|
|
||||||
$builder->join('partner_agent pa', 'pp.agent_id = pa.id', 'inner');
|
$builder->join('partner_agent pa', 'pp.agent_id = pa.id', 'inner');
|
||||||
$builder->join('partner_enquiry pe', 'pe.id = pp.enquiry_id', 'left');
|
|
||||||
|
|
||||||
$builder->where("MONTH(pp.issued_date) = MONTH('$customDate')", null, false);
|
$builder->where("MONTH(pp.issued_date) = MONTH('$customDate')", null, false);
|
||||||
$builder->where("YEAR(pp.issued_date) = YEAR('$customDate')", null, false);
|
$builder->where("YEAR(pp.issued_date) = YEAR('$customDate')", null, false);
|
||||||
|
|
||||||
$builder->where('pp.manager_id',$manager_id);
|
$builder->where('pp.manager_id',$manager_id);
|
||||||
|
|
||||||
// $builder->groupBy(['pa.id']);
|
$builder->groupBy(['pa.id, pa.name, pa.agent_code']);
|
||||||
$builder->orderBy('total_premium_amount', 'DESC');
|
$builder->orderBy('total_premium_amount', 'DESC');
|
||||||
$builder->limit($limit);
|
$builder->limit(50);
|
||||||
|
if ($raw) {
|
||||||
|
return $builder->getCompiledSelect();
|
||||||
|
}
|
||||||
$result = $builder->get()->getResultArray();
|
$result = $builder->get()->getResultArray();
|
||||||
return $result;
|
return $result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Step 3
|
//Step 3
|
||||||
public function agentNoPolicyTimeRange($value,$unit,$manager_id){
|
public function agentNoPolicyTimeRange($manager_id, $raw){
|
||||||
|
|
||||||
// $value = 10; // dynamic number (10, 50, 1, 2, 3, etc.)
|
$interval = "DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)";
|
||||||
// $unit = 'DAY'; // DAY, MONTH, YEAR (dynamic)
|
|
||||||
$interval = "DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL $value $unit)";
|
|
||||||
|
|
||||||
$builder = $this->db->table('partner_agent pa');
|
$builder = $this->db->table('partner_agent pa');
|
||||||
$builder->select('
|
$builder->select('
|
||||||
@ -898,21 +897,21 @@ class DashboardController extends ResourceController
|
|||||||
);
|
);
|
||||||
|
|
||||||
$builder->where('pa.manager_id',$manager_id);
|
$builder->where('pa.manager_id',$manager_id);
|
||||||
|
$builder->where("pp.issued_date >= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)", null, false);
|
||||||
$builder->where('pp.id', null);
|
$builder->where('pp.id', null);
|
||||||
$builder->orderBy('pa.name');
|
$builder->orderBy('pa.name');
|
||||||
|
if ($raw) {
|
||||||
|
return $builder->getCompiledSelect();
|
||||||
|
}
|
||||||
$result = $builder->get()->getResultArray();
|
$result = $builder->get()->getResultArray();
|
||||||
return $result;
|
return $result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Step 4
|
//Step 4
|
||||||
public function agentBelow50tPremiumTimeRange($value,$unit,$manager_id){
|
public function agentBelow50tPremiumTimeRange($manager_id, $raw){
|
||||||
|
|
||||||
// 4th
|
$builder = $this->db->table('partner_policy pp');
|
||||||
// $value = 10; // Dynamic (10, 50, 1, 2, 3...)
|
|
||||||
// $unit = 'DAY'; // DAY, MONTH, YEAR
|
|
||||||
$interval = "DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL $value $unit)";
|
|
||||||
|
|
||||||
|
|
||||||
$builder = $this->db->table('partner_policy pp');
|
$builder = $this->db->table('partner_policy pp');
|
||||||
@ -920,28 +919,47 @@ class DashboardController extends ResourceController
|
|||||||
$builder->select("
|
$builder->select("
|
||||||
pa.id AS agent_id,
|
pa.id AS agent_id,
|
||||||
pa.name AS agent_name,
|
pa.name AS agent_name,
|
||||||
pa.agent_code AS agent_code,
|
pa.agent_code,
|
||||||
SUM(pp.premium_amount) AS total_premium_amount,
|
SUM(pp.premium_amount) AS total_premium_amount,
|
||||||
CASE
|
CASE
|
||||||
WHEN pa.is_active = 1 THEN 'Active Agent'
|
WHEN pa.is_active = 1 THEN 'Active Agent'
|
||||||
ELSE 'Inactive Agent'
|
ELSE 'Inactive Agent'
|
||||||
END AS agent_status,
|
END AS agent_status,
|
||||||
DATE_FORMAT(pp.issued_date, '%d-%m-%Y') AS created_date_ui_formatted,
|
MAX(pp.issued_date) AS last_business_date
|
||||||
pp.issued_date AS created_date_sql_formatted
|
|
||||||
");
|
");
|
||||||
$builder->join('partner_agent pa', 'pa.id = pp.agent_id', 'left');
|
|
||||||
|
$builder->join('partner_agent pa', 'pa.id = pp.agent_id', 'inner');
|
||||||
|
|
||||||
$builder->where('pp.manager_id', $manager_id);
|
$builder->where('pp.manager_id', $manager_id);
|
||||||
$builder->where('pp.premium_amount <', 50000);
|
$builder->where('pp.premium_amount <', 50000);
|
||||||
$builder->where("pp.issued_date >= $interval", null, false); // raw SQL
|
|
||||||
$builder->groupBy('pa.id');
|
// RAW condition must be inside where() with FALSE
|
||||||
|
$builder->where("
|
||||||
|
pp.issued_date >= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)
|
||||||
|
", null, false);
|
||||||
|
|
||||||
|
// groupBy must be array of separate fields
|
||||||
|
$builder->groupBy([
|
||||||
|
'pa.id',
|
||||||
|
'pa.name',
|
||||||
|
'pa.agent_code',
|
||||||
|
'pa.is_active'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// orderBy must not contain semicolon + must be separated
|
||||||
$builder->orderBy('total_premium_amount', 'DESC');
|
$builder->orderBy('total_premium_amount', 'DESC');
|
||||||
$builder->orderBy('created_date_sql_formatted', 'DESC');
|
$builder->orderBy('last_business_date', 'ASC');
|
||||||
|
|
||||||
|
if ($raw) {
|
||||||
|
return $builder->getCompiledSelect();
|
||||||
|
}
|
||||||
|
|
||||||
return $builder->get()->getResultArray();
|
return $builder->get()->getResultArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 5
|
// Step 5
|
||||||
public function staffWiseProductList($manager_id)
|
public function staffWiseProductList($manager_id, $raw){
|
||||||
{
|
|
||||||
$builder = $this->db->table('partner_policy pp');
|
$builder = $this->db->table('partner_policy pp');
|
||||||
|
|
||||||
$builder->select("
|
$builder->select("
|
||||||
@ -997,6 +1015,10 @@ class DashboardController extends ResourceController
|
|||||||
// Group by sales exec + vehicle type
|
// Group by sales exec + vehicle type
|
||||||
$builder->groupBy("pa.sales_executive_id, pp.vehicle_type");
|
$builder->groupBy("pa.sales_executive_id, pp.vehicle_type");
|
||||||
$builder->orderBy("pa.sales_executive_id");
|
$builder->orderBy("pa.sales_executive_id");
|
||||||
|
if ($raw) {
|
||||||
|
return $builder->getCompiledSelect();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$rows = $builder->get()->getResultArray();
|
$rows = $builder->get()->getResultArray();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user