FIX_Subscription Address

This commit is contained in:
VE10-Sanjeev 2024-07-30 11:18:05 +00:00
parent a1460ccfd7
commit 6211706752

View File

@ -156,69 +156,6 @@ public function getAllCustomerDetailsBySchemeName($schemeName)
return $customerDetails; return $customerDetails;
} }
public function getAllActiveCustomerDetails($selectedScheme)
{
// $currentDate = date('Y-m-d');
$builder = $this->db->table('subscription');
$builder->select('customers.customer_id, CONCAT(customers.first_name, " ", customers.last_name) as customer_name, customer_addresses.mobile_no, customer_addresses.address_1, customer_addresses.address_2, customer_addresses.city, customer_addresses.state, customer_addresses.postal_code, business.title as business_name, books.title, from_subscription, to_subscription, business.mobile_no as business_mobile_no, business.address as business_address,business.city as business_city,business.state as business_state,business.postal_code as business_postal_code,books.title,from_subscription,to_subscription,business.title as business_name,business.mobile_no as business_mobile_no,states.state_name');
$builder->join('customers', 'customers.customer_id = subscription.customer_id');
$builder->join('customer_addresses', 'customer_addresses.customer_id = customers.customer_id And customer_addresses.address_type = 2','left');
$builder->join('business', 'business.business_id = subscription.business_id');
$builder->join('books', 'books.book_id = subscription.scheme_id');
$builder->join('states', 'states.state_short_name = customer_addresses.state');
// Active subscriptions only
$builder->where('books.title', $schemeName);
// $builder->where('customer_addresses.address_type', 2); // Filter by address_type 2
$builder->orderBy('customers.customer_id', 'ASC'); // Order by customer_id to group by customers
$builder->groupBy('subscription.sub_id');
$query = $builder->get();
$activeCustomerDetails = [];
if ($query->getNumRows() > 0) {
$currentCustomerID = null;
$shippingAddress = [];
foreach ($query->getResultArray() as $row) {
$customerID = $row['customer_id'];
if ($currentCustomerID !== $customerID) {
// New customer, add the previous customer's details (if any)
if (!empty($shippingAddress)) {
$activeCustomerDetails[] = $shippingAddress;
}
// Start a new customer's details
$shippingAddress = [
'customer_name' => $row['customer_name'],
'address_1' => $row['address_1'],
'address_2' => $row['address_2'],
'mobile_no' => $row['mobile_no'],
'city' => $row['city'],
'state' => $row['state'],
'state_name' => $row['state_name'] != "" ? $row['state_name'] : $row['state'],
'postal_code' => $row['postal_code'],
'title' => $row['title'],
'to_subscription' => $row['to_subscription'],
'company_name' => $row['business_name'],
'company_mobile_no' => $row['business_mobile_no'],
'company_city'=>$row['business_city'],
];
$currentCustomerID = $customerID;
}
}
// Add the last customer's details (if any)
if (!empty($shippingAddress)) {
$activeCustomerDetails[] = $shippingAddress;
}
}
return $activeCustomerDetails;
}
public function getAllExpiredCustomerDetailsByScheme($schemeName) public function getAllExpiredCustomerDetailsByScheme($schemeName)
{ {
@ -283,11 +220,7 @@ public function getAllExpiredCustomerDetailsByScheme($schemeName)
return $customerDetails; return $customerDetails;
} }
public function getSubscriptionDetailsBySchemeName($selectedScheme, $downloadType){ public function getQueryforSubscriptionDetailsBySchemeName($flag,$condition, $value,$selectedScheme){
// print_r($selectedScheme);
$customerDetails = [];
$currentDate = date('Y-m-d');
$builder = $this->db->table('subscription as s'); $builder = $this->db->table('subscription as s');
@ -308,27 +241,38 @@ public function getSubscriptionDetailsBySchemeName($selectedScheme, $downloadTyp
$builder->join('books as bs', 'bs.book_id = s.scheme_id', 'left'); $builder->join('books as bs', 'bs.book_id = s.scheme_id', 'left');
$builder->join('states as st', 'st.state_short_name = ca.state', 'left'); $builder->join('states as st', 'st.state_short_name = ca.state', 'left');
$builder->join('business as b', 'b.business_id = s.business_id', 'left'); $builder->join('business as b', 'b.business_id = s.business_id', 'left');
$builder->whereIn('bs.book_id', $selectedScheme); $builder->whereIn('bs.book_id', $selectedScheme);
if($flag){
if ($downloadType === 'active') { $builder->where($condition, $value);
$builder->where('s.isactive', 1);
$builder->where('s.to_subscription >=', $currentDate);
} elseif ($downloadType === 'expired') {
$builder->where('s.isactive', 0);
// $builder->where('s.to_subscription <', $currentDate);
} }
$builder->orderBy('s.to_subscription', 'asc'); $builder->orderBy('s.to_subscription', 'asc');
$query = $builder->get(); $query = $builder->get();
$result = $query->getResultArray(); $result = $query->getResultArray();
// $lastQuery = $this->db->getLastQuery(); return $result;
// $finalQueryString = $lastQuery->getQuery(); }
// echo count($result); public function getSubscriptionDetailsBySchemeName($selectedScheme, $downloadType){
// echo $finalQueryString; die;
// print_r($result);die; $customerDetails = [];
// Initialize the customerDetails array and a set to track processed customer IDs $currentDate = date('Y-m-d');
if ($downloadType === 'active') {
$result = $this->getQueryforSubscriptionDetailsBySchemeName(1,'s.to_subscription >=', $currentDate,$selectedScheme);
}
if ($downloadType === 'expired') {
$expiredResult = $this->getQueryforSubscriptionDetailsBySchemeName(1,'s.to_subscription <', $currentDate, $selectedScheme);
$activeResult = $this->getQueryforSubscriptionDetailsBySchemeName(1,'s.to_subscription >=', $currentDate, $selectedScheme);
$activeCustomerIDs = array_column($activeResult, 'customer_id');
$filteredExpiredResult = array_filter($expiredResult, function($row) use ($activeCustomerIDs) {
return !in_array($row['customer_id'], $activeCustomerIDs);
});
$result = $filteredExpiredResult;
}
if ($downloadType === 'both') {
$result = $this->getQueryforSubscriptionDetailsBySchemeName(0,'','',$selectedScheme);
}
$customerDetails = []; $customerDetails = [];
$processedCustomerIDs = []; $processedCustomerIDs = [];
@ -366,15 +310,11 @@ public function getSubscriptionDetailsBySchemeName($selectedScheme, $downloadTyp
} }
} }
// Sorting the customerDetails array by from_subscription date
usort($customerDetails, function($a, $b) { usort($customerDetails, function($a, $b) {
return strtotime($a['invoice_date']) - strtotime($b['invoice_date']); return strtotime($a['invoice_date']) - strtotime($b['invoice_date']);
}); });
} }
// $lastQuery = $this->db->getLastQuery(); // echo $downloadType." == > ".count($customerDetails);die;
// $finalQueryString = $lastQuery->getQuery();
// echo $finalQueryString;
// echo count($customerDetails);
return $customerDetails; return $customerDetails;
} }