donation/app/Models/CustomerModel.php
2023-12-14 17:38:07 +05:30

164 lines
6.3 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class CustomerModel extends Model
{
protected $table = 'customers';
protected $primaryKey = 'doner_id ';
// protected $allowedFields = ['doner_id ','first_name','city','last_name','email','mobile_no','country','state','postal_code' ,'address','profile_picture','date_of_birth','gender','mode','isactive','business_id'];
protected $allowedFields = ['doner_id ','first_name','city','last_name','email','mobile_no','country','state','postal_code' ,'address','org_name','date_of_birth','pan_no','doner_type','isactive','business_id','created_by','updated_by'];
public function insertCustomer($data)
{
return $this->insert($data);
}
public function getCustomerNamesByBusiness($business_id)
{
$this->builder()->select('doner_id,CONCAT(first_name, " ", last_name) as full_name', false);
$this->builder()->where('business_id ', $business_id); // Filter by business_id
$query = $this->builder()->get(); // Use findAll() instead of get()
return ($query->getResult());
}
// $customerNames = [];
// foreach ($query->getResult() as $row) {
// $customerNames[] = [ $row->full_name ];
// }
// print_r($customerNames);
// die();
// public function getCustomerIdByName($customerName, $businessId)
// {
// $customer = $this->builder()
// ->select('doner_id')
// ->where('CONCAT(first_name, " ", last_name)', $customerName)
// ->where('business_id', $businessId)
// ->get()
// ->getRow();
// if ($customer) {
// return $customer->doner_id;
// }
// return null; // Customer not found
// }
public function insertAddress($addressData) {
$this->db->table('customer_addresses')->insert($addressData);
return $this->db->insertID(); // Return the last inserted ID
}
public function insertAddressBatch($addressesDataArray) {
$this->db->table('customer_addresses')->insertBatch($addressesDataArray);
return $this->db->insertID(); // Note: insertID() might not be applicable for batch inserts
}
public function saveAddressDetails($data){
$i = 0;
$statement = [];
foreach ($data as $row) {
$id = $row['customer_address_id'];
if($id != ''){
unset($row['customer_address_id']); // Remove the id from the data to avoid updating it
unset($row['created_by']); // bcoz here data Updating here.
$this->db->table('customer_addresses')->where('customer_address_id', $id)->update($row);// Update the row with the specified id
$affectedRows = $this->db->affectedRows();
$statement[$i] = "address - ".$id." ". ($affectedRows ? " Updated":" Nothing to Updated");
}else{
$this->db->table('customer_addresses')->insert($row);
$insertID = $this->db->insertID();
$statement[$i] = "address - ".$insertID." Inserted";
}
$i++;
}
return $statement;
}
public function inactiveMissingAddressDetails($where,$missingValues){
$dataToUpdate = ['isactive' => 0];
$this->db->table('customer_addresses')->where($where)->whereIn('customer_address_id',$missingValues)->update($dataToUpdate);
}
public function getInvoicesByCustomerId($doner_id)
{
return $this->db->table('invoice')
->select('invoice_number, subtotal, invoice_date')
->where('doner_id', $doner_id)
->get()
->getResult();
}
public function getSubscriberDataByCustomerId($customerId)
{
// Assuming 'subscription' is the table name where subscriber data is stored.
$builder = $this->db->table('subscription');
$builder->join('books', 'books.book_id = subscription.scheme_id');
// Define the columns you want to select from the subscription table.
$builder->select('subscription.*,books.title');
// Add a where condition to filter results based on customer ID.
$builder->where('doner_id', $customerId);
// Execute the query and return the result as an array of objects.
return $builder->get()->getResult();
}
public function saveGroupDetails($data){
$id = $data['group_id'];
if($id != ''){
unset($data['group_id']); // Remove the id from the data to avoid updating it
unset($data['created_by']); // bcoz here data Updating here.
if ($this->db->table('customer_groups')->where('group_id', $id)->update($data)) {
$affectedRows = $this->db->affectedRows();
$statement['success'] = 'Customer Group has been updated successfully.';
$statement['error'] = "";
$statement['log'] = "Customer Group: has been updated successfully. Updated Customer Group ID = " .$id." Affected Rows = ".$affectedRows;
} else {
$statement['success'] = "";
$statement['error'] = 'Customer group update failed. Please try again.';
$statement['log'] = "Customer Group: Err Failed to update ID = " .$id ;
}
}else{
unset($data['updated_by']);
if ($this->db->table('customer_groups')->insert($data)) {
$insertID = $this->db->insertID();
$statement['success'] = 'Customer Group has been Inserted successfully.';
$statement['error'] = "";
$statement['log'] = "Customer Group : has been added successfully. Inserted ID = " .$insertID ;
} else {
$statement['success'] = "";
$statement['error'] = "Customer could not be added. Please try again..";
$statement['log'] = "Customer: Err could not be added. Please try again.";
}
}
return $statement;
}
public function getGroupDetails($business_id)
{
return $this->db->table('customer_groups as CG' )
->join('users as U1', 'U1.user_id = CG.created_by', 'left')
->join('users as U2', 'U2.user_id = CG.updated_by', 'left')
->select('CG.group_id,CG.group_name,CG.created_on,CG.created_by,CG.updated_on,CG.updated_by,DATE_FORMAT(CG.created_on, "%d/%m/%Y %h:%i %p") AS formatted_created_on,concat(U1.first_name," ",U1.last_name) as created_by_name,concat(U2.first_name," ",U2.last_name) as updated_by_name,CG.isactive')
->where(['CG.group_name != ' => 'EXPIRYDATE'])
->where('CG.business_id', $business_id)
// ->where(['CG.isactive' => 1])
->get()
->getResultArray();
}
}
?>