bb_sign/app/Controllers/MasterController.php
2024-07-08 11:51:54 +05:30

913 lines
50 KiB
PHP

<?php
namespace App\Controllers;
use App\Helpers\MailHelper;
use App\Models\ServiceModel;
use App\Models\TemplateModel;
use App\Models\StaffModel;
use App\Models\ClientDocsModel;
use App\Models\ClientModel;
use App\Models\BusinessModel;
use App\Models\BranchModel;
use App\Models\ServiceGroupModel;
use App\Models\ClientBranchModel;
use App\Models\DocsStatusModel;
use \Mpdf\Mpdf;
use App\Helpers\NotificationHelper;
use CodeIgniter\API\ResponseTrait;
class MasterController extends BaseController
{
protected $db;
public $session;
protected $ServiceModel;
protected $TemplateModel;
protected $StaffModel;
protected $ClientDocsModel;
protected $ClientModel;
protected $BusinessModel;
protected $BranchModel;
protected $ServiceGroupModel;
protected $ClientBranchModel;
protected $DocsStatusModel;
use ResponseTrait;
public function __construct()
{
$this->session = session();
$this->ServiceModel = new ServiceModel();
$this->TemplateModel = new TemplateModel();
$this->StaffModel = new StaffModel();
$this->ClientDocsModel = new ClientDocsModel();
$this->ClientModel = new ClientModel();
$this->BusinessModel = new BusinessModel();
$this->BranchModel = new BranchModel();
$this->ServiceGroupModel = new ServiceGroupModel();
$this->ClientBranchModel = new ClientBranchModel();
$this->DocsStatusModel = new DocsStatusModel();
$this->db = \Config\Database::connect();
}
function getBusinessDetails($businessIds) {
$businessDetails = [];
foreach ($businessIds as $businessId) {
// Example data fetched from the database
$businessDetails[] = $this->BusinessModel->select('business_name')->where('business_id', $businessId)->first();
}
return $businessDetails;
}
public function service_group_list()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$serviceGroups = $this->ServiceGroupModel->select('service_group.*')->findAll();
// Fetch services grouped by service_group_id
$services = $this->ServiceGroupModel->select('service_group.service_group_id, service_group.business_id, services.service_name')
->join('services', 'service_group.service_group_id = services.service_group_id', 'left')
->findAll();
// Initialize an array to store service counts and service names by group
$serviceCounts = [];
$serviceNamesByGroup = [];
// Group services by service group id and count services
foreach ($services as $service) {
$serviceGroupId = $service['service_group_id'];
if (!isset($serviceCounts[$serviceGroupId])) {
$serviceCounts[$serviceGroupId] = 0;
$serviceNamesByGroup[$serviceGroupId] = [];
}
if (!empty($service['service_name'])) {
$serviceCounts[$serviceGroupId]++;
$serviceNamesByGroup[$serviceGroupId][] = $service['service_name'];
}
}
// Assign service counts and names to service groups
foreach ($serviceGroups as &$group) {
$groupId = $group['service_group_id'];
$group['service_count'] = isset($serviceCounts[$groupId]) ? $serviceCounts[$groupId] : 0;
$group['service_list'] = isset($serviceNamesByGroup[$groupId]) ? $serviceNamesByGroup[$groupId] : [];
}
// Set the modified service groups to the data array
$data['serviceGroupList'] = $serviceGroups;
foreach ($serviceGroups as &$group) {
$groupId = $group['service_group_id'];
$group['service_count'] = isset($serviceNamesByGroup[$groupId]) ? count($serviceNamesByGroup[$groupId]) : 0;
$group['service_list'] = isset($serviceNamesByGroup[$groupId]) ? $serviceNamesByGroup[$groupId] : [];
}
// Set the modified service groups to the data array
$data['serviceGroupList'] = $serviceGroups;
$data['business_list'] = $this->BusinessModel->findAll();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
foreach ($data['serviceGroupList'] as $key => $value) {
// Decode the JSON encoded business_id
$businessIds = json_decode($value['business_id'], true);
// Find the business details
$businessDetails = $this->getBusinessDetails($businessIds);
// Add business count
$value['business_count'] = count($businessIds);
// Add business details to the service group
$value['business_list'] = $businessDetails;
// Update the serviceGroupList array with the new data
$data['serviceGroupList'][$key] = $value;
}
echo view('layout/header', ['Data'=>$staffdata]);
echo view('service_group_list',$data);
echo view('layout/footer');
}
public function service_group_create()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
$serviceGroupData = $this->request->getVar();
if( isset($serviceGroupData['service_group_id']) ){
$serviceGroupData['business_id'] = json_encode($serviceGroupData['business']);
$this->ServiceGroupModel->where('service_group_id', $this->request->getVar('service_group_id') )->set($serviceGroupData)
->update();
}else{
$serviceGroupData = $this->request->getVar();
$serviceGroupData['business_id'] = json_encode($this->request->getPost('business'));
$serviceGroupData['branch_id'] = $this->session->get('logged_in_staff_branch_id');
$this->ServiceGroupModel->insert($serviceGroupData);
}
return redirect()->to(base_url()."service_group_list");
}
}
public function service_group_edit()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$service_group_id = $this->request->getVar('service_group_id');
$data = $this->ServiceGroupModel->where('service_group_id',$service_group_id)->get()->getRow();
return $this->response->setJSON(['success' => true , 'data' =>$data]);
}
public function service_list()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$data['serviceList'] = $this->ServiceModel->select('services.* ,service_group.business_id, service_group.group_name ,COUNT(template.template_id) as template_count')
->join('template', 'services.service_id = template.service_id', 'left')
->join('service_group', 'services.service_group_id = service_group.service_group_id', 'left')
// ->where('services.branch_id',$this->session->get('logged_in_staff_branch_id'))
->groupBy('services.service_id')
->orderBy('services.service_group_id')
->findAll();
$data['serviceGroupData'] = $this->ServiceGroupModel->where('is_active',1)->findall();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
$data['business_list'] = $this->BusinessModel->findAll();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('service_list',$data);
echo view('layout/footer');
}
public function service_create()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
$serviceData = $this->request->getVar();
if( isset($serviceData['service_id']) ){
$this->ServiceModel->where('service_id', $this->request->getVar('service_id') )->set($serviceData)->update();
}else{
$serviceData = $this->request->getVar();
$serviceData['branch_id'] = $this->session->get('logged_in_staff_branch_id');
$this->ServiceModel->insert($serviceData);
}
return redirect()->to(base_url()."service_list");
}else{
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('service_form');
echo view('layout/footer');
}
}
public function service_edit()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$service_id = $this->request->getVar('service_id');
$data = $this->ServiceModel->where('service_id',$service_id)->get()->getRow();
return $this->response->setJSON(['success' => true , 'data' =>$data]);
}
public function template_list()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$data['templateData'] = $this->TemplateModel->select('template.* , services.service_name , service_group.group_name, service_group.business_id')
->join('services', 'services.service_id = template.service_id', 'left')
->join('service_group', 'template.service_group_id = service_group.service_group_id', 'left')
->findAll();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
$data['business_list'] = $this->BusinessModel->findAll();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('template_list',$data);
echo view('layout/footer');
}
public function template_create()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
$templateData = $this->request->getVar();
if($this->request->getVar('template_id') != 'null'){
$id = $this->request->getVar('template_id');
unset($templateData['template_id']);
$this->TemplateModel->where('template_id', $id )->set($templateData)->update();
return $this->response->setJSON(['success' => true , 'service_id' =>$this->request->getVar('service_id')]);
}else{
unset($templateData['template_id']);
$this->TemplateModel->insert($templateData);
return $this->response->setJSON(['success' => true , 'service_id' =>$this->request->getVar('service_id')]);
}
}else{
$id = $this->request->getVar('service_id');
$data['AllServiceGroupData'] = $this->ServiceGroupModel->findAll();
$data['AllBusinessData'] = $this->BusinessModel->where('isactive',1)->findAll();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
if($staffdata->role == 'Staff'){
foreach (json_decode($staffdata->business_id) as $key => $value) {
$business = $this->BusinessModel->where('business_id', $value)->first();
if ($business) {
$data['business_list'][] = $business;
}
}
}else{
$data['business_list'] = $this->BusinessModel->findAll();
}
echo view('layout/header', ['Data'=>$staffdata]);
echo view('template_form',$data);
echo view('layout/footer');
}
}
public function template_edit()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$id = $this->request->getVar('template_id');
$data['templateData'] = $this->TemplateModel->where('md5(template_id)',$id)->get()->getRow();
$data['serviceData'] = $this->ServiceModel->where('service_id',$data['templateData']->service_id)->get()->getRow();
$data['AllServiceData'] = $this->ServiceModel->findAll();
$data['serviceGroupData'] = $this->ServiceGroupModel->where('service_group_id',$data['templateData']->service_group_id)
->get()
->getRow();
$data['AllServiceGroupData'] = $this->ServiceGroupModel->findAll();
$data['BusinessData'] = $this->BusinessModel->where('business_id',$data['templateData']->template_header_business)
->where('isactive',1)
->get()
->getRow();
$data['AllBusinessData'] = $this->BusinessModel->where('isactive',1)->findAll();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('template_form',$data);
echo view('layout/footer');
}
public function template_preview()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$id = $this->request->getVar('template_id');
$data['templateData'] = $this->TemplateModel->where('md5(template_id)',$id)->get()->getRow();
$data['serviceData'] = $this->ServiceModel->where('service_id',$data['templateData']->service_id)->get()->getRow();
$data['businessData'] = $this->BusinessModel->where('business_id',$data['templateData']->template_header_business)
->get()
->getRow();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
$data['body_html'] = $data['templateData']->body_html;
echo view('template_preview',$data);
}
public function shared_docs_list()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$currentDate = new \DateTime();
// Determine the start and end dates of the current financial year
if ($currentDate->format('n') >= 4) {
// If the current month is April (4) or later, the financial year started this year
$financialYearStart = new \DateTime($currentDate->format('Y') . '-04-01');
$financialYearEnd = new \DateTime(($currentDate->format('Y') + 1) . '-03-31');
} else {
// If the current month is before April, the financial year started last year
$financialYearStart = new \DateTime(($currentDate->format('Y') - 1) . '-04-01');
$financialYearEnd = new \DateTime($currentDate->format('Y') . '-03-31');
}
// Convert to the format suitable for your database (assuming MySQL)
$financialYearStart = $financialYearStart->format('Y-m-d');
$financialYearEnd = $financialYearEnd->format('Y-m-d');
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
if($this->session->get('logged_in_staff_role') == 'Admin'){
$business_id = $this->session->get('logged_in_staff_business_id');
$data['doc_list'] = $this->ClientDocsModel->select('client_docs.*,
services.service_name,
service_group.group_name,
client_branch.name as client_branch_name,
client.name as client_name, service_group.business_id')
->join('template', 'template.template_id = client_docs.template_id', 'left')
->join('services', 'services.service_id = template.service_id', 'left')
->join('service_group', 'service_group.service_group_id = template.service_group_id', 'left')
->join('client_branch', 'client_branch.id = client_docs.client_branch_id', 'left')
->join('client', 'client.client_id = client_branch.client_id ' , 'left')
// ->where('client.business_id', $business_id)
->where('client_docs.created_at >=', $financialYearStart)
->where('client_docs.created_at <=', $financialYearEnd)
// ->where("JSON_CONTAINS(service_group.business_id, '" . $business_id . "')")
->orderBy('client_docs.id', 'DESC')
->get()
->getResultArray();
$business_id_check = 0;
foreach ($data['doc_list'] as $index => $value) {
foreach (json_decode($value['business_id']) as $key => $values) {
if($values == $business_id){
$business_id_check = $business_id;
}
}
if($business_id_check != 0){
$data['doc_list'][$index] =$value;
}else{
unset($data['doc_list'][$index]); // Remove the element completely
}
$business_id_check = 0;
}
$docStatusByClientId = [];
// Fetch all doc_status entries and group them by client_docs_id
$data['clientdata'] = $this->ClientBranchModel->select('client.name as client_name , client_branch.name as client_branch_name , client_branch.id as client_branch_id')
->join('client', 'client_branch.client_id = client.client_id', 'left')
// ->where('client.business_id',$this->session->get('logged_in_staff_business_id'))
->findAll();
$data['serviceGroupData'] = $this->ServiceGroupModel->findAll();
foreach ($data['serviceGroupData'] as $key => $value) {
$check_its_okay = 0;
foreach (json_decode($value['business_id']) as $values) {
if($values == $business_id){
$check_its_okay =1;
}
}
if($check_its_okay == 0){
unset($data['serviceGroupData'][$key]);
}
$check_its_okay = 0;
}
}else{
$business_id = $this->session->get('logged_in_staff_business_id');
$data['doc_list'] = $this->ClientDocsModel->select('client_docs.*,
services.service_name,
service_group.group_name,
client_branch.name as client_branch_name,
client.name as client_name, service_group.business_id')
->join('template', 'template.template_id = client_docs.template_id', 'left')
->join('services', 'services.service_id = template.service_id', 'left')
->join('service_group', 'service_group.service_group_id = template.service_group_id', 'left')
->join('client_branch', 'client_branch.id = client_docs.client_branch_id', 'left')
->join('client', 'client.client_id = client_branch.client_id ' , 'left')
// ->where('client.business_id', $business_id)
->where('client_docs.created_at >=', $financialYearStart)
->where('client_docs.created_at <=', $financialYearEnd)
// ->where("JSON_CONTAINS(service_group.business_id, '" . $business_id . "')")
->orderBy('client_docs.id', 'DESC')
->get()
->getResultArray();
$business_id_check = 0;
foreach ($data['doc_list'] as $index => $value) {
foreach (json_decode($value['business_id']) as $key => $values) {
if($values == $business_id){
$business_id_check = $business_id;
}
}
if($business_id_check != 0){
$data['doc_list'][$index] =$value;
}else{
unset($data['doc_list'][$index]); // Remove the element completely
}
$business_id_check = 0;
}
$docStatusByClientId = [];
// Fetch all doc_status entries and group them by client_docs_id
$data['clientdata'] = $this->ClientBranchModel->select('client.name as client_name , client_branch.name as client_branch_name , client_branch.id as client_branch_id')
->join('client', 'client_branch.client_id = client.client_id', 'left')
// ->where('client.business_id',$this->session->get('logged_in_staff_business_id'))
->findAll();
$data['serviceGroupData'] = $this->ServiceGroupModel->findAll();
foreach ($data['serviceGroupData'] as $key => $value) {
$check_its_okay = 0;
foreach (json_decode($value['business_id']) as $values) {
if($values == $business_id){
$check_its_okay =1;
}
}
if($check_its_okay == 0){
unset($data['serviceGroupData'][$key]);
}
$check_its_okay = 0;
}
}
foreach ($data['doc_list'] as $doc) {
$doc_id_temp = $doc['id'];
$docStatusByClientId[$doc_id_temp] = $this->db->table('docs_status')
->select('created_at as doc_created_date')
->where('client_docs_id', $doc_id_temp)
->where('status', "Approved")
->get()
->getResultArray();
}
// Assign doc_status arrays to each document in doc_list
foreach ($data['doc_list'] as &$doc) {
$doc['doc_status'] = isset($docStatusByClientId[$doc['id']]) ? $docStatusByClientId[$doc['id']] : [];
}
$data['business_list'] = [];
if($staffdata->role == 'Staff'){
foreach (json_decode($staffdata->business_id) as $key => $value) {
$business = $this->BusinessModel->where('business_id', $value)->first();
if ($business) {
$data['business_list'][] = $business;
}
}
}else{
$data['business_list'] = $this->BusinessModel->findAll();
}
// echo "<pre>";
// print_r($data);die;
echo view('layout/header', ['Data'=>$staffdata]);
echo view('share_docs',$data);
echo view('layout/footer');
}
public function set_include_archived_session($checked_or_not) {
if($checked_or_not == true){
// $includeArchived = $this->request->getGet('include_archived');
$this->session->set('include_archived', $checked_or_not);
}else{
$this->session->set('include_archived', $checked_or_not);
}
// print_r($this->session->get('include_archived'));die;
return $this->response->setJSON(['status' => 'success']);
}
public function shared_doc_non_financial_year_to(){
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$currentDate = new \DateTime();
// Determine the start and end dates of the current financial year
if ($currentDate->format('n') >= 4) {
// If the current month is April (4) or later, the financial year started this year
$financialYearStart = new \DateTime($currentDate->format('Y') . '-04-01');
$financialYearEnd = new \DateTime(($currentDate->format('Y') + 1) . '-03-31');
} else {
// If the current month is before April, the financial year started last year
$financialYearStart = new \DateTime(($currentDate->format('Y') - 1) . '-04-01');
$financialYearEnd = new \DateTime($currentDate->format('Y') . '-03-31');
}
// Convert to the format suitable for your database (assuming MySQL)
$financialYearStart = $financialYearStart->format('Y-m-d');
$financialYearEnd = $financialYearEnd->format('Y-m-d');
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
if($this->session->get('logged_in_staff_role') == 'Admin'){
$data['doc_list'] = $this->ClientDocsModel->select('client_docs.* , services.service_name ,
service_group.group_name , client_branch.name as client_branch_name ,
client.name as client_name, service_group.business_id')
->join('template', 'template.template_id = client_docs.template_id', 'left')
->join('services', 'services.service_id = template.service_id', 'left')
->join('service_group', 'service_group.service_group_id = template.service_group_id', 'left')
->join('client_branch', 'client_branch.id = client_docs.client_branch_id', 'left')
->join('client', 'client.client_id = client_branch.client_id', 'left')
->orderBy('client_docs.id','DESC')
->findAll();
$data['clientdata'] = $this->ClientBranchModel->select('client.name as client_name , client_branch.name as client_branch_name , client_branch.id as client_branch_id')
->join('client', 'client_branch.client_id = client.client_id', 'left')
// ->where('client.business_id',$this->session->get('logged_in_staff_business_id'))
->findAll();
$data['serviceGroupData'] = $this->ServiceGroupModel->findAll();
}
$business_id = $this->session->get('logged_in_staff_business_id');
$business_id_check = 0;
foreach ($data['doc_list'] as $index => $value) {
foreach (json_decode($value['business_id']) as $key => $values) {
if($values == $business_id){
$business_id_check = $business_id;
}
}
if($business_id_check != 0){
$data['doc_list'][$index] =$value;
}else{
unset($data['doc_list'][$index]); // Remove the element completely
}
$business_id_check = 0;
}
foreach ($data['doc_list'] as $doc) {
$doc_id_temp = $doc['id'];
$docStatusByClientId[$doc_id_temp] = $this->db->table('docs_status')
->select('created_at as doc_created_date')
->where('client_docs_id', $doc_id_temp)
->where('status', "Approved")
->get()
->getResultArray();
}
// Assign doc_status arrays to each document in doc_list
foreach ($data['doc_list'] as &$doc) {
$doc['doc_status'] = isset($docStatusByClientId[$doc['id']]) ? $docStatusByClientId[$doc['id']] : [];
}
$data['business_list'] = $this->BusinessModel->findAll();
// foreach (json_decode($staffdata->business_id) as $key => $value) {
// $business = $this->BusinessModel->where('business_id', $value)->first();
// if ($business) {
// $data['business_list'][] = $business;
// }
// }
echo view('layout/header', ['Data'=>$staffdata]);
echo view('share_docs',$data);
echo view('layout/footer');
}
public function shared_document_edit()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
$data = $this->request->getVar();
$id = $this->request->getVar('id');
unset($data['id']);
$this->ClientDocsModel->where('id', $id )->set($data)->update();
return $this->response->setJSON(['success' => true ]);
}else{
$id = $this->request->getVar('doc_id');
$data['templateData'] = $this->ClientDocsModel->where('md5(id)',$id)->get()->getRow();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('edit_shared_doc',$data);
echo view('layout/footer');
}
}
public function get_service_data()
{
$service_group_id = $this->request->getVar('service_group_id');
$result = $this->ServiceModel->where('service_group_id',$service_group_id)->findAll();
return $this->response->setJSON(['count'=>count($result) , 'data'=>$result ]);
}
public function get_templates()
{
$result = $this->TemplateModel->where('service_id',$this->request->getVar('service_id'))->findAll();
return $this->response->setJSON(['count'=>count($result) , 'data'=>$result ]);
}
public function create_docs()
{
$docData = $this->request->getVar();
$docData['created_by'] = $this->session->has('is_staff_logged_in');
$docData['body_html'] = $this->TemplateModel->where('template_id',$this->request->getVar('template_id'))->get()->getRow()->body_html;
$insert = $this->ClientDocsModel->insert($docData);
if($insert){
$data['client_docs_id'] = $insert;
$data['is_staff'] = 1;
$data['created_by'] = $this->session->get('is_staff_logged_in');
$data['status'] = 'Created';
$this->DocsStatusModel->insert($data);
echo true;
} else {
echo false;
}
}
public function client_docs_preview()
{
if($this->session->has('is_staff_logged_in')){
$userData = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
}else if($this->session->has('is_client_logged_in')){
$userData = $this->ClientBranchModel->where('id',$this->session->get('is_client_logged_in'))->get()->getRow();
}
$doc_id = $this->request->getVar('doc_id');
$data['docData'] = $this->ClientDocsModel->docPreview($doc_id);
$data['clientData'] = $this->ClientBranchModel->select('client.name , client.pan_no ,
client_branch.gst_no , client_branch.mobile_no ,
client_branch.email , client_branch.address ,
client_branch.country , client_branch.state ,
client_branch.city , client_branch.pin_code ')
->join('client', 'client_branch.client_id = client.client_id', 'left')
->where('client_branch.id',$data['docData']->client_branch_id)
->get()->getRow();
$data['header_html'] = $data['docData']->header_html;
$data['footer_html'] = $data['docData']->footer_html;
$data['body_html'] = $data['docData']->body_html;
//replace client details in template content
foreach ($data['clientData'] as $key => $value) {
$placeHolder = '%'.$key.'%';
$data['body_html'] = str_replace($placeHolder,$value,$data['body_html']);
}
if($data['docData']->is_active == 1){
$DateAndYears = $data['docData']->place_holder_object;
}else{
$DateAndYears = $this->dateAndYearPlaceholder();
}
//replace Date and Year values in template content
foreach (json_decode($DateAndYears,true) as $key => $value) {
$placeHolder = '%'.$key.'%';
$data['body_html'] = str_replace($placeHolder,$value,$data['body_html']);
}
echo view('layout/header', ['Data'=>$userData]);
echo view('client_docs_preview',$data);
echo view('layout/footer');
}
public function dateAndYearPlaceholder()
{
$currentDate = date('d-m-Y');
$currentYear = date('Y');
$currentMonth = date('m');
if ($currentMonth >= 4) {
$financialYearStart = $currentYear . '-04-01';
$financialYearEnd = ($currentYear + 1) . '-03-31';
$financialYear = $currentYear . '-' . ($currentYear + 1);
$assessmentYear = ($currentYear + 1) . '-' . ($currentYear + 2);
$q1 = '01-04-' . $currentYear . ' to ' . '30-06-' . $currentYear;
$q2 = '01-07-' . $currentYear . ' to ' . '30-09-' . $currentYear;
$q3 = '01-10-' . $currentYear . ' to ' . '31-12-' . $currentYear;
$q4 = '01-01-' . ($currentYear + 1) . ' to ' . '31-03-' . ($currentYear + 1);
} else {
$financialYearStart = ($currentYear - 1) . '-04-01';
$financialYearEnd = $currentYear . '-03-31';
$financialYear = ($currentYear - 1) . '-' . $currentYear;
$assessmentYear = $currentYear . '-' . ($currentYear + 1);
$q1 = '01-04-' . ($currentYear - 1) . ' to ' . '30-06-' . ($currentYear - 1);
$q2 = '01-07-' . ($currentYear - 1) . ' to ' . '30-09-' . ($currentYear - 1);
$q3 = '01-10-' . ($currentYear - 1) . ' to ' . '31-12-' . ($currentYear - 1);
$q4 = '01-01-' . $currentYear . ' to ' . '31-03-' . $currentYear;
}
$DateAndYears = [
"CD" => $currentDate,
"CY" => $currentYear,
"FY" => $financialYear,
"AY" => $assessmentYear,
"Q1" => $q1,
"Q2" => $q2,
"Q3" => $q3,
"Q4" => $q4,
];
return json_encode($DateAndYears);
}
public function client_docs_histroy($doc_id)
{
if($this->session->has('is_staff_logged_in')){
$userData = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
}else if($this->session->has('is_client_logged_in')){
$userData = $this->ClientBranchModel->where('id',$this->session->get('is_client_logged_in'))->get()->getRow();
}
$data['docData'] = $this->ClientDocsModel->docPreview($doc_id);
// Fetch the document history
$clientDocHistory = $this->DocsStatusModel->select('*')
->where('client_docs_id', $doc_id)
->get()
->getResult();
// Initialize an array to store the augmented results
$augmentedHistory = [];
foreach ($clientDocHistory as $history) {
if ($history->is_staff == 1) {
// Fetch staff details
$staffDetails = $this->StaffModel->select('name, email') // Adjust the columns as needed
->where('staff_id', $history->created_by)
->get()
->getRow();
$history->creator_details = $staffDetails;
$history->creator_type = 'staff';
} else {
// Fetch client details
$clientDetails = $this->ClientBranchModel->select('name, email') // Adjust the columns as needed
->where('id', $history->created_by)
->get()
->getRow();
$history->creator_details = $clientDetails;
$history->creator_type = 'client';
}
// Add the augmented history to the array
$augmentedHistory[] = $history;
}
// Assign the augmented history to the data array
$data['client_doc_history'] = $augmentedHistory;
// Debug output
return json_encode($data);
}
public function change_doc_status()
{
try {
$doc_id = $this->request->getPost('doc_id');
$is_active = $this->request->getPost('is_active');
$data['is_active'] = $is_active;
if($is_active == 1){
$DateAndYears = $this->dateAndYearPlaceholder();
$data['place_holder_object'] = $DateAndYears;
$MailHelper = new MailHelper();
$clientData = $this->ClientDocsModel->select('client.name as client_name, client_branch.email, client_branch.*')
->join('client_branch', 'client_docs.client_branch_id = client_branch.id', 'left')
->join('client', 'client_branch.client_id = client.client_id', 'left')
->where('client_docs.id', $doc_id)->get()->getRow();
$documentUrl = base_url();
helper('url');
$url ='<a href="' . generate_signed_url('preview', ['doc_id'=> md5($doc_id)], 36000) . '">Preview and Approve Document</a>';
$businessData = $this->BranchModel->select('branches.* , business.business_name ')
->join('business', 'branches.business_id = business.business_id', 'left')
->where('branches.branch_id',$this->session->get('logged_in_staff_branch_id'))
->get()->getRow();
$subject = 'BB-Sign Verify Document';
$message = "Dear " . $clientData->client_name . ",<br>" . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve.<br><br>" . $url;
$mail_send = $MailHelper->send_url_email($clientData->email , $businessData->email, $subject, $message);
$notificationHelper = new NotificationHelper();
$number = '91'.$clientData->mobile_no; // Replace with dynamic recipient number
if($clientData->whatsapp == 1){
$whatsApp_message = "Dear " . substr($clientData->client_name, 0, 20) . "," . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve." ;
// $notificationHelper->sendWhatsAppMessage($number, $whatsApp_message, $url);
}
$statusData['client_docs_id'] = $doc_id;
$statusData['is_staff'] = 1;
$statusData['created_by'] = $this->session->get('is_staff_logged_in');
$statusData['status'] = 'Shared';
$this->DocsStatusModel->insert($statusData);
}else{
$statusData['client_docs_id'] = $doc_id;
$statusData['is_staff'] = 1;
$statusData['created_by'] = $this->session->get('is_staff_logged_in');
$statusData['status'] = 'Revert';
$this->DocsStatusModel->insert($statusData);
}
$updated = $this->ClientDocsModel->update($doc_id, $data);
if ($updated) {
return $this->response->setJSON(['status' => 'success','code' => 200,'data' => true],200);
} else {
$result = "No Match's";
return $this->response->setJSON(['status' => 'failed','code' => 404,'data' => $result],404);
}
} catch (\Exception $exception) {
return $this->response->setJSON(['status' => 'failed','code' => 500,'data' => $exception],500);
}
}
public function rememeber_notification()
{
try {
$doc_id = $this->request->getPost('doc_id');
$MailHelper = new MailHelper();
$clientData = $this->ClientDocsModel->select('client.name as client_name, client_branch.email, client_branch.mobile_no')
->join('client_branch', 'client_docs.client_branch_id = client_branch.id', 'left')
->join('client', 'client_branch.client_id = client.client_id', 'left')
->where('client_docs.id', $doc_id)->get()->getRow();
helper('url');
$url ='<a href="' . generate_signed_url('preview', ['doc_id'=>md5($doc_id)], 100) . '">Preview and Approve Document</a>';
$businessData = $this->BranchModel->select('branches.* , business.business_name ')
->join('business', 'branches.business_id = business.business_id', 'left')
->where('branches.branch_id',$this->session->get('logged_in_staff_branch_id'))
->get()->getRow();
$subject = 'BB-Sign Verify Document';
$message = "Dear " . $clientData->client_name . ",<br>" . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve.<br><br>" . $url;
$MailHelper->send_url_email($clientData->email , $businessData->email, $subject, $message);
$notificationHelper = new NotificationHelper();
$number = '91'.$clientData->mobile_no; // Replace with dynamic recipient number
if($clientData->whatsapp == 1){
$whatsApp_message = "Dear " . substr($clientData->client_name, 0, 20) . "," . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve." ;
//$notificationHelper->sendWhatsAppMessage($number, $whatsApp_message, $url);
}
return $this->respond(['success' => true, 'data' => 'Remember Send Successfully']);
} catch (\Exception $exception) {
return $this->response->setJSON(['status' => 'failed','code' => 500,'data' => $exception],500);
}
}
public function doc_generate_otp()
{
try {
$mobile_no = $this->request->getPost('mobile_no');
$client_data = $this->ClientBranchModel->where('mobile_no', $mobile_no )->first();
if($client_data){
$id = $this->request->getPost('doc_id');
$otp = mt_rand(100000, 999999);
$data = ['otp' => $otp];
$clint_full_data = $this->ClientDocsModel->select('client_docs.* , services.service_name , service_group.group_name , client_branch.name as client_branch_name , client.name as client_name')
->join('template', 'template.template_id = client_docs.template_id', 'left')
->join('services', 'services.service_id = template.service_id', 'left')
->join('service_group', 'service_group.service_group_id = template.service_group_id', 'left')
->join('client_branch', 'client_branch.id = client_docs.client_branch_id', 'left')
->join('client', 'client.client_id = client_branch.client_id', 'left')
->where('client_docs.id', $id)
->first();
$update = $this->ClientDocsModel->update($id, $data);
if($update){
$notificationHelper = new NotificationHelper();
$number = '91'.$mobile_no; // Replace with dynamic recipient number
$message = 'Document Verification OTP : ' . $otp;
if($client_data['whatsapp'] == 1){
// $notificationHelper->sendWhatsAppMessage($number, $message);
}
if($client_data['sms'] == 1){
$notificationHelper->sendSms($number, $client_data['name'] , $otp, '', 'doc_verify',$clint_full_data);
}
return $this->respond(['success' => true, 'otp' => $otp]);
}
} else {
return false;
}
} catch (\Exception $e) {
// Handle any exceptions that occur during execution
return $this->response->setJSON(['status' => 'error', 'code' => 500, 'message' => $e->getMessage()], 500);
}
}
public function doc_verify_otp()
{
try {
$id = $this->request->getPost('doc_id');
$otp = $this->request->getPost('otp');
$client_doc_data = $this->ClientDocsModel->where('id', $id )->where('otp', $otp)->first();
$data = ['otp' =>'' ,'rejection_reason' =>''];
if($client_doc_data){
$data['is_approved'] = $this->request->getPost('is_approved');
$data['ip_address'] = $this->request->getPost('clientIp');
$update = $this->ClientDocsModel->update($id , $data);
if($this->session->has('is_client_logged_in')){
$logged_user = $this->session->get('is_client_logged_in');
$statusData['is_staff'] = 0;
}else{
$logged_user = $this->session->get('is_staff_logged_in');
$statusData['is_staff'] = 1;
}
$statusData['client_docs_id'] = $id;
$statusData['created_by'] = $logged_user;
$statusData['status'] = 'Approved';
$statusData['note'] = $this->request->getPost('clientIp');
$this->DocsStatusModel->insert($statusData);
if($update){
return json_encode(true);
}
} else {
return json_encode('');
}
} catch (\Exception $e) {
// Handle any exceptions that occur during execution
return $this->response->setJSON(['status' => 'error', 'code' => 500, 'message' => $e->getMessage()], 500);
}
}
public function status_service()
{
try {
$service_id = $this->request->getPost('service_id');
$is_active = $this->request->getPost('is_active');
$data = ['is_active' => $is_active];
$updated = $this->ServiceModel->update($service_id, $data);
if ($updated) {
return $this->respond(['status' => 'success','code' => 200,'data' => true],200);
} else {
$result = "No Match's";
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
}
} catch (\Exception $exception) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $exception],500);
}
}
public function download_doc(){
if ($this->request->getMethod() === 'POST') {
// Get the HTML content from the POST request
$htmlContent = $this->request->getPost('htmlContent');
try {
// Create new PDF document
$mpdf = new Mpdf();
$mpdf->WriteHTML($htmlContent);
// Output PDF to a string
$pdfContent = $mpdf->Output('', 'S');
// Encode PDF content to base64
$base64Pdf = base64_encode($pdfContent);
// Set response headers
return $this->respond(['pdf' => $base64Pdf], 200);
} catch (\Exception $e) {
// Log error
log_message('error', 'Error generating PDF: ' . $e->getMessage());
return $this->failServerError('Error generating PDF');
}
} else {
return $this->fail('Invalid request method', 405);
}
}
}