donation/app/Models/CustomerModel.php
2024-06-07 12:32:22 +00:00

107 lines
4.9 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class CustomerModel extends Model
{
protected $table = 'donor';
protected $primaryKey = 'donor_id ';
// protected $allowedFields = ['donor_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 = ['donor_id ','first_name','city','last_name','email','mobile_no','country','state','postal_code' ,'address','org_name','date_of_birth','pan_no','adhar_no','org_reg_details','donor_type','isactive','business_id','created_by','updated_by','XL_file_name','ip_address','passport_no'];
public function insertCustomer($data)
{
return $this->insert($data);
}
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('donor_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('donor_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('donor_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();
}
public function getReceiptDetailsByDonorId($donorId)
{
return $this->db->table('receipt')
->select('receipt.receipt_number, receipt.donor_id, receipt.amount, receipt.currency,receipt.receipt_date, d.first_name')
->join('donor as d', 'd.donor_id = receipt.donor_id', 'left') // Adjust columns accordingly
->where('receipt.donor_id', $donorId) // Specify the table alias for the column in the where clause
->get()
->getResult();
}
public function getData($table, $where = null)
{
$query = $this->db->table($table);
if ($where) {
$query->where($where);
}
return $query->get()->getResult();
}
function export_excel($business_id)
{
$builder = $this->db->table('donor')
->select('CONCAT_WS(" ", donor.first_name, donor.last_name) AS full_name')
->select('donor.donor_type,donor.org_name,donor.org_reg_details,donor.mobile_no,donor.email,donor.pan_no,donor.adhar_no,donor.passport_no,donor.address,donor.country,donor.state,donor.city,donor.postal_code')
->select('CONCAT_WS(" ", creator.first_name, creator.last_name) AS creator_name')
->select('CONCAT_WS(" ", updater.first_name, updater.last_name) AS updater_name')
->select('IFNULL(DATE_FORMAT(donor.created_on, "%d-%m-%Y"), "-") AS formatted_created_on')
->select('IFNULL(DATE_FORMAT(donor.updated_on, "%d-%m-%Y"), "-") AS formatted_updated_on')
->select('CASE WHEN donor.isactive = 1 THEN "Active" ELSE "Inactive" END AS active_status', false)
->join('users as creator', ' donor.created_by = creator.user_id', 'left')
->join('users as updater', ' donor.updated_by = updater.user_id', 'left')
->where('donor.business_id', $business_id);
$query = $builder->get();
return $query->getResultArray();
}
public function bulkInsert($data) {
return $this->db->table('donor')->insertBatch($data);
}
}
?>