From 6211706752dadad8b854fab7969374bfb5b4a43d Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Tue, 30 Jul 2024 11:18:05 +0000 Subject: [PATCH] FIX_Subscription Address --- app/Models/SubscriptionModel.php | 136 +++++++++---------------------- 1 file changed, 38 insertions(+), 98 deletions(-) diff --git a/app/Models/SubscriptionModel.php b/app/Models/SubscriptionModel.php index 5d65f560..41bff6d2 100644 --- a/app/Models/SubscriptionModel.php +++ b/app/Models/SubscriptionModel.php @@ -156,69 +156,6 @@ public function getAllCustomerDetailsBySchemeName($schemeName) 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) { @@ -283,24 +220,20 @@ public function getAllExpiredCustomerDetailsByScheme($schemeName) return $customerDetails; } -public function getSubscriptionDetailsBySchemeName($selectedScheme, $downloadType){ - - // print_r($selectedScheme); - $customerDetails = []; - $currentDate = date('Y-m-d'); +public function getQueryforSubscriptionDetailsBySchemeName($flag,$condition, $value,$selectedScheme){ $builder = $this->db->table('subscription as s'); - + $builder->select(" - CONCAT_WS(' ', c.first_name, c.last_name) as customer_name, - c.customer_id, c.mobile_no, c.email, DATEDIFF(s.to_subscription, CURDATE()) AS remaining_days, - s.from_subscription, s.to_subscription, s.scheme_id, bs.title, - i.invoice_id, i.invoice_date, i.shipping_address_id, - ca.customer_address_id, ca.address_1, ca.address_2, ca.city, ca.state, ca.postal_code, ca.address_type, st.state_name, - b.title as business_name, b.address as business_address, b.city as business_city, - b.state as business_state, b.postal_code as business_postal_code, b.mobile_no as business_mobile_no + CONCAT_WS(' ', c.first_name, c.last_name) as customer_name, + c.customer_id, c.mobile_no, c.email, DATEDIFF(s.to_subscription, CURDATE()) AS remaining_days, + s.from_subscription, s.to_subscription, s.scheme_id, bs.title, + i.invoice_id, i.invoice_date, i.shipping_address_id, + ca.customer_address_id, ca.address_1, ca.address_2, ca.city, ca.state, ca.postal_code, ca.address_type, st.state_name, + b.title as business_name, b.address as business_address, b.city as business_city, + b.state as business_state, b.postal_code as business_postal_code, b.mobile_no as business_mobile_no "); - + $builder->join('customers as c', 's.customer_id = c.customer_id', 'left'); $builder->join('invoice as i', 's.invoice_id = i.invoice_id', 'left'); // $builder->join('customer_addresses as ca', 'ca.customer_id = s.customer_id AND i.shipping_address_id = ca.customer_address_id AND ca.address_type = 2', 'left'); @@ -308,27 +241,38 @@ public function getSubscriptionDetailsBySchemeName($selectedScheme, $downloadTyp $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('business as b', 'b.business_id = s.business_id', 'left'); - $builder->whereIn('bs.book_id', $selectedScheme); - - if ($downloadType === 'active') { - $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); + if($flag){ + $builder->where($condition, $value); } - $builder->orderBy('s.to_subscription', 'asc'); - $query = $builder->get(); $result = $query->getResultArray(); - // $lastQuery = $this->db->getLastQuery(); - // $finalQueryString = $lastQuery->getQuery(); - // echo count($result); - // echo $finalQueryString; die; - // print_r($result);die; - // Initialize the customerDetails array and a set to track processed customer IDs + return $result; + } + public function getSubscriptionDetailsBySchemeName($selectedScheme, $downloadType){ + + $customerDetails = []; + $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 = []; $processedCustomerIDs = []; @@ -366,15 +310,11 @@ public function getSubscriptionDetailsBySchemeName($selectedScheme, $downloadTyp } } - // Sorting the customerDetails array by from_subscription date usort($customerDetails, function($a, $b) { return strtotime($a['invoice_date']) - strtotime($b['invoice_date']); }); } -// $lastQuery = $this->db->getLastQuery(); -// $finalQueryString = $lastQuery->getQuery(); -// echo $finalQueryString; -// echo count($customerDetails); + // echo $downloadType." == > ".count($customerDetails);die; return $customerDetails; }