155 lines
6.2 KiB
PHP
155 lines
6.2 KiB
PHP
<?php
|
|
namespace App\Models;
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
class SubscriptionModel extends Model
|
|
{
|
|
protected $table = 'subscription';
|
|
protected $primaryKey = 'sub_id';
|
|
protected $allowedFields = ['sub_id','scheme_id','customer_id','to_subscription','from_subscription','mode','created_on','created_by','business_id'];
|
|
|
|
|
|
|
|
public function getAllSubscribersWithNames($where)
|
|
{
|
|
|
|
$builder = $this->db->table('subscription');
|
|
$builder->select('subscription.*, books.title, CONCAT(customers.first_name, " ", customers.last_name) as customer_name');
|
|
$builder->join('books', 'books.book_id = subscription.scheme_id');
|
|
$builder->join('customers', 'customers.customer_id = subscription.customer_id');
|
|
$builder->where($where);
|
|
return $builder->get()->getResultArray();
|
|
}
|
|
public function getCustomerNameBySchemeName($schemeName)
|
|
{
|
|
$builder = $this->db->table('subscription');
|
|
$builder->select('customers.first_name, customers.last_name');
|
|
$builder->select('customer_addresses.address_1, customer_addresses.address_2');
|
|
$builder->join('customer_addresses', 'customer_addresses.customer_id = subscription.customer_id');
|
|
$builder->join('customers', 'customers.customer_id = subscription.customer_id');
|
|
$builder->join('schemes', 'schemes.scheme_id = subscription.scheme_id');
|
|
$builder->where('schemes.scheme_name', $schemeName);
|
|
|
|
$query = $builder->get();
|
|
|
|
if ($query->getNumRows() > 0) {
|
|
$row = $query->getRow();
|
|
return $row->first_name . ' ' . $row->last_name;
|
|
} else {
|
|
return 'Customer not found'; // You can return a default value or handle the case where the customer is not found.
|
|
}
|
|
}
|
|
|
|
public function getCustomerAddressesBySchemeName($schemeName)
|
|
{
|
|
$builder = $this->db->table('subscription');
|
|
$builder->select('CONCAT(customers.first_name, " ", customers.last_name) as customer_name, customer_addresses.address_1, customer_addresses.address_2, customer_addresses.city, customer_addresses.state,customer_addresses.postal_code, customer_addresses.address_type,business.address,business.city,business.state,business.postal_code,business.country');
|
|
$builder->join('customers', 'customers.customer_id = subscription.customer_id');
|
|
$builder->join('customer_addresses', 'customer_addresses.customer_id = subscription.customer_id');
|
|
$builder->join('business', 'business.business_id = subscription.business_id');
|
|
$builder->join('books', 'books.book_id = subscription.scheme_id');
|
|
$builder->where('books.title', $schemeName);
|
|
|
|
$query = $builder->get();
|
|
|
|
$addresses = [];
|
|
|
|
if ($query->getNumRows() > 0) {
|
|
foreach ($query->getResult() as $row) {
|
|
$addressType = $row->address_type;
|
|
$addresses[$addressType] = [
|
|
'customer_name' => $row->customer_name,
|
|
'address_1' => $row->address_1,
|
|
'address_2' =>$row->address_2,
|
|
'city' => $row->city,
|
|
'state' => $row->state,
|
|
'postal_code'=>$row->postal_code,
|
|
];
|
|
}
|
|
} else {
|
|
$addresses = [
|
|
'billing' => [
|
|
'customer_name' => 'Customer not found',
|
|
'address_line' => 'Billing Address not found',
|
|
'city' => '',
|
|
'state' => '',
|
|
],
|
|
'shipping' => [
|
|
'customer_name' => 'Customer not found',
|
|
'address_line' => 'Shipping Address not found',
|
|
'city' => '',
|
|
'state' => '',
|
|
],
|
|
];
|
|
}
|
|
|
|
return $addresses;
|
|
}
|
|
public function getAllCustomerDetailsBySchemeName($schemeName)
|
|
{
|
|
$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,business.address,business.city,business.state,business.postal_code,books.title,from_subscription,to_subscription');
|
|
$builder->join('customers', 'customers.customer_id = subscription.customer_id');
|
|
$builder->join('customer_addresses', 'customer_addresses.customer_id = customers.customer_id');
|
|
$builder->join('business', 'business.business_id = subscription.business_id');
|
|
$builder->join('books', 'books.book_id = subscription.scheme_id');
|
|
$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
|
|
|
|
// Fetch the query result
|
|
$query = $builder->get();
|
|
|
|
$customerDetails = [];
|
|
|
|
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)) {
|
|
$customerDetails[] = $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'],
|
|
'postal_code' => $row['postal_code'],
|
|
'address' => $row['address'],
|
|
'city' => $row['city'],
|
|
'state' => $row['state'],
|
|
'postal_code' => $row['postal_code'],
|
|
'title' => $row['title'],
|
|
'to_subscription'=>$row['to_subscription'],
|
|
'title'=>$row['title'],
|
|
];
|
|
|
|
$currentCustomerID = $customerID;
|
|
}
|
|
}
|
|
|
|
// Add the last customer's details (if any)
|
|
if (!empty($shippingAddress)) {
|
|
$customerDetails[] = $shippingAddress;
|
|
}
|
|
}
|
|
|
|
return $customerDetails;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|