bbone/application/modules/Expence/controllers/Expence.php

541 lines
21 KiB
PHP

<?php defined('BASEPATH') or exit('No direct script access allowed');
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
date_default_timezone_set('Asia/Kolkata');
/**
* AMS
*
* @package HMVC
* @author Venba
* @copyright 2022 Venba
* @license https://venbainfotech.com/licenses/MIT MIT License
* @link <URI> (description)
* @version GIT: $Id$
* @since Version 0.0.1
* @filesource
*
*/
class Expence extends Admin_Controller
{
/**
* [__construct description]
*
* @method __construct
*/
public function __construct()
{
// To inherit directly the attributes of the parent class.
parent::__construct();
### Syntax : $this->load->model('Model Path' , 'Alias name');
### The second parameter (optional) is used to call the method.
$this->load->model('MY_Model','MY_Model');
$this->load->model('Expence_model','Expence_model');
$this->load->model('Employee/Employee_model','Emp_model');
$this->load->model('Audit/Audit_model','audit_model');
$this->load->helper(array('form', 'url'));
}
public function expence_form()
{
$id = $this->input->get('id');
$expense_data = $this->Expence_model->get_expense_data($id);
$expense_child_data = $this->Expence_model->get_expense_child_data($id);
$this->layout->set('expense_child_data', $expense_child_data);
$this->layout->buffer('main_content', 'Expence/expence_form');
$this->layout->render('Employee_Layout');
}
public function get_emp_name()
{
$emp = $this->Expence_model->get_employee_name(user()->business_id);
$html = '<option value="">--select Employee--</option>';
foreach($emp as $val)
{
if($val->user_id != user()->id)
{
$html .='<option id="'.$val->manager.'" value="'.$val->user_id.'">'.$val->name.'</option>';
}
}
echo json_encode($html);
}
public function rise_expence_request()
{
## Logged In Variables
$logged_in_user = user()->id;
$logged_in_business = user()->business_id;
## Form Requested Data
$requested_data = $this->input->post();
//Employee id
$employee_id = (isset($requested_data['emp_id']) && !empty($requested_data['emp_id'])) ? $requested_data['emp_id'] : $logged_in_user;
//Manager id
$get_manager_details = $this->Expence_model->get_manager_id($employee_id);
$manager_id = ((!empty($get_manager_details)) && $get_manager_details[0]->manager != null) ? $get_manager_details[0]->manager : 0;
// Table Name Declarations
$expense_master_table = "expense";
$expense_child_table = "expense_child";
// Array formation for updating the master details
$expense_master_arr = array(
'created_by'=>$logged_in_user,
'requested_for'=>$employee_id,
'expense_name'=>$requested_data['expense_name'],
'business_reason'=>$requested_data['business_reason'],
'manager_id' => $manager_id,
'business_id' => $logged_in_business,
'status'=>'Draft',
);
## Retrive the Inserted ID from Master Table
$create_expence = $this->MY_Model->insert($expense_master_arr,$expense_master_table);
$first = $requested_data['date_of_expence_new'];
## Get the Count for Child Expence
$count = (gettype($first) == 'array') ? count($first) : 0;
if ($count > 0) {
for ($i = 0; $i < $count; $i++) {
##Preparing Array Stored in Expence child Table
$expense_child_arr = array(
'expense_id' => $create_expence,
'emp_id'=>$employee_id ,
'business_id' => $logged_in_business,
'manager_id' => $manager_id,
'status' => 'Draft',
'date_of_expense'=> $this->input->post('date_of_expence_new')[$i],
'date_of_expense_new' => date('Y-m-d', strtotime($this->input->post('date_of_expence_new')[$i])),
'amount' => $this->input->post('amount')[$i],
'description' => $this->input->post('description')[$i],
);
if (!empty($_FILES['bill']['name'][$i])) {
$upload_result = $this->upload_file($_FILES['bill'], $i);
if ($upload_result['success']) {
$expense_child_arr['bill'] = $upload_result['file_name'];
} else {
log_message('error', $upload_result['error']);
}
}
$create_expence_child = $this->MY_Model->insert($expense_child_arr, $expense_child_table);
}
}
$this->session->set_flashdata('Create', '1');
redirect('Expence/Emp_Expence_List');
}
public function edit_expence($id)
{
$expense_data = $this->Expence_model->get_expense_data($id);
$expense_child_data = $this->Expence_model->get_expense_child_data($id);
$this->layout->set('expense_data', $expense_data);
$this->layout->set('expense_child_data', $expense_child_data);
$this->layout->buffer('main_content', 'Expence/edit_expence');
$this->layout->render('Employee_Layout');
}
public function edit_rise_expence_request()
{
## Logged In Variables
$logged_in_user = user()->id;
$logged_in_business = user()->business_id;
## Form Requested Data
$requested_data = $this->input->post();
## Expense primary key
$id = $requested_data['expense_pid'];
// Employee id
$employee_id = (isset($requested_data['emp_id']) && !empty($requested_data['emp_id'])) ? $requested_data['emp_id'] : $logged_in_user;
// Manager id
$get_manager_details = $this->Expence_model->get_manager_id($employee_id);
$manager_id = ((!empty($get_manager_details)) && $get_manager_details[0]->manager != null) ? $get_manager_details[0]->manager : 0;
// Table Name Declarations
$expense_master_table = "expense";
$expense_child_table = "expense_child";
// Array formation for updating the master details
$expense_master_arr = array(
'created_by' => $logged_in_user,
'requested_for' => $employee_id,
'expense_name' => $requested_data['expense_name'],
'business_reason' => $requested_data['business_reason'],
'manager_id' => $manager_id,
'business_id' => $logged_in_business,
'status' => 'Draft',
);
// Update process for master
$update_expense = $this->MY_Model->edit_option($expense_master_arr, $id, $expense_master_table);
log_message('info', "update_expense_master - expense_id => ".$id." is updated == ".$update_expense);
$first = $requested_data['date_of_expence_new'];
$count = (gettype($first) == 'array') ? count($first) : 0;
if ($count > 0) {
for ($i = 0; $i < $count; $i++) {
$expense_child_id = $this->input->post('expense_child_id')[$i];
$first_child = array(
'date_of_expense' => $this->input->post('date_of_expence_new')[$i],
'date_of_expense_new' => date('Y-m-d', strtotime($this->input->post('date_of_expence_new')[$i])),
'amount' => $this->input->post('amount')[$i],
'description' => $this->input->post('description')[$i],
);
$update_expense_list = $this->MY_Model->edit_option($first_child, $expense_child_id, $expense_child_table);
log_message('info', "update_expense_list - expense_id => ".$id." is updated == ".$update_expense_list);
}
}
// Details gathering from JavaScript new entries
$second = $this->input->post('date_of_expence_new2');
$additional_count = (gettype($second) == 'array') ? count($second) : 0;
if ($additional_count > 0) {
for ($j = 0; $j < $additional_count; $j++) {
$second_child = array(
'expense_id' => $id,
'emp_id' => $employee_id,
'business_id' => user()->business_id,
'manager_id' => $manager_id,
'status' => 'Draft',
'date_of_expense' => date('d-m-Y', strtotime($this->input->post('date_of_expence_new2')[$j])),
'date_of_expense_new' => date('Y-m-d', strtotime($this->input->post('date_of_expence_new2')[$j])),
'amount' => $this->input->post('amount2')[$j],
'description' => $this->input->post('description2')[$j],
);
if (!empty($_FILES['bill2']['name'][$j])) {
$upload_result = $this->upload_file($_FILES['bill2'], $j);
if ($upload_result['success']) {
$second_child['bill'] = $upload_result['file_name'];
} else {
log_message('error', $upload_result['error']);
}
}
$create_expense_list = $this->MY_Model->insert($second_child, $expense_child_table);
log_message('info', "create_expense_list - expense_id => ".$id." is created == ".$create_expense_list);
}
}
redirect('Expence/Emp_Expence_List');
}
public function upload_file($file_data, $index)
{
$this->load->library('upload');
$upload_path = APPPATH . '../assets/uploads/Expense_Bill/';
$config['upload_path'] = realpath($upload_path);
$config['allowed_types'] = 'jpg|png|jpeg|pdf';
$config['max_size'] = 80000;
$config['file_name'] = time() . '_' . $file_data['name'][$index];
$_FILES['userfile']['name'] = $file_data['name'][$index];
$_FILES['userfile']['type'] = $file_data['type'][$index];
$_FILES['userfile']['tmp_name'] = $file_data['tmp_name'][$index];
$_FILES['userfile']['error'] = $file_data['error'][$index];
$_FILES['userfile']['size'] = $file_data['size'][$index];
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile')) {
$upload_data = $this->upload->data();
return array('success' => true, 'file_name' => $upload_data['file_name']);
} else {
$error = $this->upload->display_errors();
return array('success' => false, 'error' => $error);
}
}
public function expence_list()
{
$business_id = user()->business_id;
$expence_list = $this->Expence_model->get_expence_list($business_id);
$get_count_amount = $this->Expence_model->get_count_amount();
// $get_business_admin = $this->Expence_model->get_business_admin();
$this->layout->set('count_amount', $get_count_amount);
$this->layout->set('tableData', $expence_list);
$this->layout->buffer('main_content', 'Expence/expence_list');
$this->layout->render('Employee_Layout');
}
public function expence_list_view($id)
{
$expence_list = $this->Expence_model->get_expense_child_data2($id);
$get_expense_name = $this->Expence_model->get_expense_data_not_md5($id);
$expence_list_with_delegation = $this->Expence_model->get_all_leave_request_by_manager_id_with_delegation(user()->business_id, user()->id);
if($expence_list_with_delegation == NULL){
$expence_list_with_delegation = [];
}
$this->layout->set('tableData', $expence_list);
$this->layout->set('expense_name', $get_expense_name);
$this->layout->buffer('main_content', 'Expence/expence_list_view');
$this->layout->render('Employee_Layout');
}
public function expence_list2()
{
$id = $this->input->get('id');
$expence_list2 = $this->Expence_model->get_expence_child_list($id);
echo json_encode($expence_list2);
}
public function Emp_Expence_List()
{
$business_id = user()->business_id;
$expence_list = $this->Expence_model->get_expence_list_all($business_id);
$get_count_amount = $this->Expence_model->get_count_amount();
$this->layout->set('count_amount', $get_count_amount);
$this->layout->set('tableData', $expence_list);
$this->layout->buffer('main_content', 'Expence/emp_expence_list');
$this->layout->render('Employee_Layout');
}
public function Emp_Expence_List_view($id)
{
$expence_list = $this->Expence_model->get_expense_child_data2($id);
$get_expense_name = $this->Expence_model->get_expense_data_not_md5($id);
$this->layout->set('tableData', $expence_list);
$this->layout->set('expense_name', $get_expense_name);
$this->layout->buffer('main_content', 'Expence/emp_expence_list_view');
$this->layout->render('Employee_Layout');
}
public function getDeleteConfirmationPopup()
{
$title = $this->input->get('title');
$data['title'] = $title ? $title : "Are you sure to Approve this Action";
$htmlPage = $this->load->view('confirmation_popup.php',$data,true);
echo json_encode($htmlPage);
}
public function Approve_Expence()
{
$id=$this->input->post('id');
// echo $id; die;
$action['status'] = 'Approved';
$action['approved_by']= user()->name;
$action['approved_at'] = date('Y-m-d h:i:s');
$table = "expense";
$expence_id = $this->MY_Model->edit_option($action, $id, $table);
$this->Expence_model->approve_child_expense($action, $id);
if($expence_id){
$code = 'APPROVE_EXPENCE';
$type = 'Email';
$this->notification->sendNotificationForExpense($id, $code, $type);
}
$this->session->set_flashdata('Create', '1');
redirect('Expence/expence_list');
}
public function Reimbursed_Expence()
{
$id=$this->input->post('id');
$action['status'] = 'Reimbursed';
$action['reimbursed_by']= user()->name;
$action['reimbursed_at'] = date('Y-m-d h:i:s');
$table = "expense";
$expence_id = $this->MY_Model->edit_option($action, $id, $table);
$this->Expence_model->approve_child_expense($action, $id);
if($expence_id){
$code = 'REIMBURSED_EXPENCE';
$type = 'Email';
$this->notification->sendNotificationForExpense($id, $code, $type);
}
$this->session->set_flashdata('Create', '3');
redirect('Expence/expence_list');
}
public function Approve_Child_Expence()
{
if($this->input->post('declinedata'))
{
// echo $this->input->post('expence_id');
// var_dump($this->input->post('expence_id'));
// print_r($this->input->post('declinedata'));
// echo $this->input->post('expence_id');
// die;
$id=$this->input->post('id');
$expense_id=$this->input->post('expence_id');
$decline_action['status'] = 'Declined';
$decline_action['decline_by'] = user()->name;
$decline_action['decline_reason'] = $this->input->post('declinedata');
// echo $this->input->post('expence_id');
$is_decline = $this->Expence_model->approve_child_expense_2($decline_action, $id);
if($is_decline == true)
{
$count = $this->Expence_model->get_status_update($expense_id);
$table = "expense";
if($count->pending_count >= 1)
{
$status_update['status'] = 'pending';
$this->MY_Model->edit_option($status_update, $expense_id, $table);
}
else if($count->approve_count >= 1 && $count->declined_count >= 1)
{
$status_update['status'] = 'Partially approved';
$id_for_expence = $this->MY_Model->edit_option($status_update, $expense_id, $table);
if($id_for_expence){
$code = 'APPROVE_EXPENCE';
$type = 'Email';
$this->notification->sendNotificationForExpense($expense_id, $code, $type);
}
}
else if($count->declined_count >= 1 && $count->approve_count == 0 && $count->pending_count == 0)
{
$status_update['status'] = 'Declined';
$status_update['decline_by'] = user()->name;
$this->MY_Model->edit_option($status_update, $expense_id, $table);
}
}
redirect(base_url('Expence/expence_list_view/'.$this->input->post('expence_id')));
}
else
{
$id=$this->input->post('id');
$expense_id=$this->input->post('expence_id');
$action['status'] = 'Approved';
$action['approved_at'] = date('Y-m-d h:i:s');
$action['approved_by']= user()->name;
$is_approve = $this->Expence_model->approve_child_expense_2($action, $id);
if($is_approve == true)
{
$count = $this->Expence_model->get_status_update($expense_id);
$table = "expense";
if($count->pending_count >= 1)
{
$status_update['status'] = 'pending';
$this->MY_Model->edit_option($status_update, $expense_id, $table);
}
else if($count->approve_count >= 1 && $count->declined_count >= 1)
{
$status_update['status'] = 'Partially approved';
$id_for_expence = $this->MY_Model->edit_option($status_update, $expense_id, $table);
if($id_for_expence){
$code = 'APPROVE_EXPENCE';
$type = 'Email';
$this->notification->sendNotificationForExpense($expense_id, $code, $type);
}
}
else if($count->approve_count >= 1 && $count->declined_count == 0 && $count->pending_count == 0)
{
$status_update['status'] = 'Approved';
$action['approved_at'] = date('Y-m-d h:i:s');
$status_update['approved_by'] = user()->name;
$this->MY_Model->edit_option($status_update, $expense_id, $table);
}
}
redirect('Expence/expence_list_view/'.$this->input->post('expence_id'));
}
}
public function getDeclineConfirmationPopup()
{
$data = array();
$htmlPage = $this->load->view('confirmation_popup_2.php',$data,true);
echo json_encode($htmlPage);
}
public function Decline_expence()
{
$id=$this->input->post('id');
// echo $id; die;
$data['status'] = 'Declined';
$data['decline_by'] = user()->name;
$data['decline_reason'] = $this->input->get('declinedata');
$expence_id = $this->Expence_model->ExpenceDecline($id, $data);
$this->Expence_model->Expence_Decline_Child($id, $data);
if($expence_id){
$code = 'DECLINE_EXPENCE';
$type = 'Email';
$this->notification->sendNotificationForExpense($id, $code, $type);
}
redirect('Expence/expence_list');
}
public function getCancelConfirmationPopup()
{
$title = $this->input->get('title');
$data['title'] = $title ? $title : "Are you sure to Approve this Action";
$htmlPage = $this->load->view('confirmation_popup.php',$data,true);
echo json_encode($htmlPage);
}
public function cancled_expence()
{
$id=$this->input->post('id');
$data['status'] = 'Cancelled';
$data['cancelled_at'] = date('Y-m-d h:i:s');
$first = $this->Expence_model->Expencecancle($id, $data);
$second = $this->Expence_model->Expencecancle_child($id, $data);
redirect('Expence/emp_expence_list');
}
public function remove_row()
{
$get_count_from_child = $this->Expence_model->get_expense_child_data2($this->input->post('expence_id'));
$count = count($get_count_from_child);
if($count === 1)
{
$this->session->set_flashdata('remove_row', '1');
redirect('Expence/edit_expence/'.md5($this->input->post('expence_id')));
}
else
{
$id = $this->input->post('id');
$delete = $this->Expence_model->remove_row($id);
redirect('Expence/edit_expence/'.md5($this->input->post('expence_id')));
}
}
public function submit()
{
$id = $this->input->post('id');
$table1 = 'expense';
$data['status'] = "pending";
$data['updated_on'] = date('Y-m-d h:i:s');
$expence_id = $this->MY_Model->edit_option($data, $id, $table1);
$expence_child_id = $this->Expence_model->update_to_submit_all($id, $data);
if($expence_child_id){
$code = 'EXPENCE_CLAIM';
$type = 'Email';
$this->notification->sendNotificationForExpense($id, $code, $type);
}
redirect('Expence/emp_expence_list');
}
}