File: //var/www/html/qcr24/app/application/controllers/admin/Accident.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Accident extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('admin/maccident');
}
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/accident_guide/general/urls.html
*/
public function index()
{
$data = array('menu_id'=> 4);
$data['accidents'] = array();
$data['accidents'] = $this->maccident->get_accident();
$data['content'] = 'admin/accident/list';
$this->load->view('admin/layouts/index', $data);
}
public function add()
{
$data = array();
$data['content'] = 'admin/accident/add';
$data['drivers'] = $this->maccident->get_driver();
$this->load->view('admin/layouts/index', $data);
}
public function getRentOutCar()
{
$accident_date=date('Y-m-d H:i',strtotime(str_replace('/', '-', $this->input->post('accident_date'))));
$driver_id=$this->input->post('driver_id');
$rent_out_car_data= $this->maccident->getRentOutCar($accident_date,$driver_id);
echo json_encode(array('status'=>true,'rent_out_car_data'=>$rent_out_car_data));
}
public function submit_add_accident()
{
// echo '<pre>'; print_r($this->input->post());
//echo '<pre>'; print_r($_FILES); die;
if(!empty($this->input->post())){
$accident_data = array();
$accident_data['accident_date'] = date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $this->input->post('accident_date'))));
$accident_data['driver_id'] = $this->input->post('driver_id');
$accident_data['car_id'] = $this->input->post('car_id');
$accident_data['rent_out_id'] = $this->input->post('rent_out_id');
$accident_data['fault_not_fault'] = $this->input->post('fault_not_fault');
$accident_data['amount_deduct_from'] = $this->input->post('amount_deduct_from');
$accident_data['payment_type'] = $this->input->post('payment_type');
$accident_data['total_payable_amount'] = $this->input->post('payable_amount');
$accident_data['total_payment_amount'] = $this->input->post('full_or_partial_amt');
$accident_data['total_due_amount'] = floatval($this->input->post('payable_amount')) - floatval($this->input->post('full_or_partial_amt'));
if($accident_data['amount_deduct_from'] == 'Damage'){
$accident_data['damage_amount'] = $this->input->post('damage_amount');
} else{
$accident_data['basic_excess'] = $this->input->post('basic_excess');
$accident_data['age_excess'] = $this->input->post('age_excess');
$accident_data['overseas_dl_excess'] = $this->input->post('overseas_dl_excess');
}
$accident_data['created_by'] = $this->admin_session_data['user_id'];
$accident_data['created_ts'] = date('Y-m-d H:i:s');
if(isset($_FILES['damage_image']['name']) && !empty($_FILES['damage_image']['name'])){
$path = './public/admin_images/accident/';
$upload_file = $this->single_image_upload($path, $_FILES['damage_image'],'damage_image');
if($upload_file['status']==1){
$accident_data['damage_image']=$upload_file['result'];
} else{
$this->session->set_flashdata('error_msg', $upload_file['result']);
$this->add();
}
}
$this->db->trans_start(); # Starting Transaction
$this->db->insert('accidents',$accident_data);
$accident_id = $this->db->insert_id();
$accident_payment_data = array();
$accident_payment_data['accident_id'] = $accident_id;
$accident_payment_data['payment_date'] = date('Y-m-d');
$accident_payment_data['payable_amount'] = $this->input->post('payable_amount');
$accident_payment_data['payment_type'] = $this->input->post('payment_type');
$accident_payment_data['payment_method'] = $this->input->post('payment_method');
$accident_payment_data['payment_amount'] = $this->input->post('full_or_partial_amt');
$accident_payment_data['created_by'] = $this->admin_session_data['user_id'];
$accident_payment_data['created_ts'] = date('Y-m-d H:i:s');
$this->db->insert('accident_payments',$accident_payment_data);
$this->db->trans_complete(); # Completing transaction
if ($this->db->trans_status() === FALSE) {
# Something went wrong.
$this->db->trans_rollback();
$this->session->set_flashdata('error_msg', 'Oops!Something went wrong...');
$this->add();
} else {
# Everything is Perfect.
# Committing data to the database.
$this->db->trans_commit();
//************************************************************************************//
//************************This Part is for Activity Log*******************************//
$activityLogData = array(
'activity_type' => '<b>Add Accident</b>',
'description' => 'New Accident ( Id - '.$accident_id.' ) Added',
'link' => 'admin/accident',
'icon' => '<i class="fa fa-history" aria-hidden="true"></i>',
);
$this->activity_log($activityLogData);
//************************This Part is for Activity Log*******************************//
//************************************************************************************//
$this->session->set_flashdata('success_msg', 'Accident data submitted Successfully');
redirect("admin/accident");
}
} else {
$this->session->set_flashdata('error_msg', 'Please fill up all the required fields');
$this->add();
}
redirect("admin/accident");
}
public function viewaccident($pre_data = array())
{
$data = array();
$data = $pre_data;
$data['roles'] = $this->maccident->get_role();
//$data['states'] = $this->maccident->get_state();
$data['parent_accident'] = $this->maccident->edit_accident($this->admin_session_data['accident_id']);
$data['content'] = 'admin/accident/add';
$this->load->view('admin/layouts/index', $data);
}
public function make_payment($accident_id)
{
$data = array();
$data['content'] = 'admin/accident/payment';
$data['accident_details'] = $this->maccident->get_accident_details($accident_id);
$data['accident_payment_details'] = $this->maccident->get_accident_payment_details($accident_id);
//echo '<pre>';print_r($data);die;
$this->load->view('admin/layouts/index', $data);
}
public function submit_payment_accident()
{
//echo '<pre>'; print_r($this->input->post()); die;
//echo '<pre>'; print_r($_FILES); die;
if(!empty($this->input->post())){
$this->db->trans_start(); # Starting Transaction
$accident_id = $this->input->post('accident_id');
$total_due_amount = floatval($this->input->post('total_due_amount')) - floatval($this->input->post('payment_amount'));
if($total_due_amount == 0){
$payment_type = 'Full';
} else {
$payment_type = 'Partial';
}
$accident_payment_data = array();
$accident_payment_data['accident_id'] = $accident_id;
$accident_payment_data['payment_date'] = date('Y-m-d');
$accident_payment_data['payable_amount'] = $this->input->post('total_due_amount');
$accident_payment_data['payment_type'] = $payment_type;
$accident_payment_data['payment_method'] = $this->input->post('payment_method');
$accident_payment_data['payment_amount'] = $this->input->post('payment_amount');
$accident_payment_data['created_by'] = $this->admin_session_data['user_id'];
$accident_payment_data['created_ts'] = date('Y-m-d H:i:s');
$this->db->insert('accident_payments',$accident_payment_data);
$accident_update_qry = "UPDATE accidents SET total_payment_amount = total_payment_amount + '".$accident_payment_data['payment_amount']."',total_due_amount='".$total_due_amount."',payment_type='".$payment_type."',updated_by='".$this->admin_session_data['user_id']."',updated_ts='".date('Y-m-d H:i:s')."'
WHERE accident_id = ".$accident_id."";
//echo $accident_update_qry;die;
$this->db->query($accident_update_qry);
$this->db->trans_complete(); # Completing transaction
if ($this->db->trans_status() === FALSE) {
# Something went wrong.
$this->db->trans_rollback();
$this->session->set_flashdata('error_msg', 'Oops!Something went wrong...');
$this->make_payment($accident_id);
} else {
# Everything is Perfect.
# Committing data to the database.
$this->db->trans_commit();
$this->session->set_flashdata('success_msg', 'Accident payment done successfully');
redirect("admin/accident");
}
} else {
$this->session->set_flashdata('error_msg', 'Please fill up all the required fields');
redirect("admin/accident");
}
redirect("admin/accident");
}
private function upload_multiple_images($path,$files,$user_id,$document_type){
//echo"<pre>";print_r($product_id); die;
$config = array(
'upload_path' => $path,
'allowed_types' => 'gif|jpg|png|jpeg|pdf|doc|docx',
'overwrite' => 1,
);
$this->load->library('upload', $config);
$images = array();
foreach ($files['name'] as $key => $image) {
$_FILES['images[]']['name']= $files['name'][$key];
$_FILES['images[]']['type']= $files['type'][$key];
$_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['images[]']['error']= $files['error'][$key];
$_FILES['images[]']['size']= $files['size'][$key];
$fileName = $document_type.'_'.time() .'_'. $key .'_'. str_replace(" ","_",$image);
$images[] = $fileName;
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if ($this->upload->do_upload('images[]')) {
if($document_type == 'signature'){
$this->mcommon->delete('user_docs',array('document_type'=>'signature','user_id'=>$user_id));
}
$ap['user_id'] = $user_id;
$ap['document_type'] = $document_type;
$ap['document_path'] = $fileName;
$ap['created_ts'] = date('Y-m-d H:i:s');
/************ Sreela (21/10/19) end ********************/
$this->mcommon->insert('user_docs',$ap);
} else {
//echo $this->upload->display_errors(); die;
return false;
}
}
return $images;
}
private function single_image_upload($path,$files,$document_type){
$config = array(
'upload_path' => $path,
'allowed_types' => 'gif|jpg|png|jpeg',
'overwrite' => 1,
'encrypt_name' => true
);
//print_r($config);die;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($document_type)){
//echo $this->upload->display_errors();die;
$message = array('result' => 'The file you are attempting is not correct. Please select jpg|png|jpeg file','status'=>0);
}else{
$data = array('upload_data' => $this->upload->data());
$message = array('result' => $data['upload_data']['file_name'],'status'=>1);
}
//print_r($message);
return $message;
}
}