<?php
/*
* Generated by CRUDigniter v3.2
* www.crudigniter.com
*/
class Conge extends FrontApplication{
function __construct()
{
parent::__construct();
$this->load->model('Conge_model');
}
function pdf($id){
$this->data["conge"]=$this->Conge_model->get_conge_to_pdf($id);
$this->data["userconges"]= $this->Conge_model->get_conge_by_user($this->data["conge"]["users_id"]);
$this->load->model("User_model");
$userConge = $this->User_model->get_user($this->data["conge"]["users_id"]);
$user = $this->User_model->connect($userConge["login"],$userConge["password"]);
$this->data["soldeconge"] = $user[0]["soldeconge"];
$this->load->library("html2pdf/HTML2PDF");
$this->load->view("conge/pdf",$this->data);
}
function exportlist($datedebut, $datefin,$typeconge,$etat,$iduser){
$this->load->model("Ecritureplanning_model");
$this->data["ecriture"]=$this->Ecritureplanning_model;
if($typeconge>0){
$this->db->where("typeconge_id",$typeconge);
}
if($iduser>0){
$this->db->where("users_id",$iduser);
}
if($etat>=0){
$this->db->where("etat",$etat);
}
$this->data["conge"]=$this->Conge_model->get_all_conge_by_date($datedebut, $datefin);
$this->load->library("html2pdf/HTML2PDF");
$this->load->view("conge/export",$this->data);
}
/*
* Listing of conge
*/
function index($msg=null)
{
$params['limit'] = RECORDS_PER_PAGE;
$params['offset'] = ($this->input->get('per_page')) ? $this->input->get('per_page') : 0;
$params['readException']=in_array($this->data["readException"], $this->data["access"]["Conge"]->actions);
$config = $this->config->item('pagination');
$config['base_url'] = site_url('conge/index?');
$config['total_rows'] = $this->Conge_model->get_all_conge_count();
$this->pagination->initialize($config);
$user = $this->session->userdata("auth");
//-----------------------------------------------------------------------
$this->load->model('Typeconge_model');
$this->data['all_typeconge'] = $this->Typeconge_model->get_all_typeconge();
//--------------------------------------------------------------------------
$this->load->model('User_model');
$this->data['all_users'] = $this->User_model->get_all_users();
//-----------------------------------------------------------------------------
$this->data['conge'] = $this->Conge_model->get_all_conge($params,$user["type"]);
if($msg != null && $msg =="confirm"){
$this->data['msg']= "Vous avez d'autres demandes confirmées dans la méme periode";
}
if($msg != null && $msg =="rejectsolde"){
$this->data['msg']= "Votre solde de congé est insiffusant";
}
$this->load->model("Ecritureplanning_model");
$this->data["ecriture"]=$this->Ecritureplanning_model;
$this->data['_view'] = 'conge/index';
$this->load->view('layouts/main',$this->data);
}
/*
* Adding a new conge
*/
function add()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('datedebut','Datedebut','required');
$this->form_validation->set_rules('datefin','Datefin','required');
$this->load->model('User_model');
$user = $this->session->userdata("auth");
if( !$this->User_model->isUserOut($user["iduser"],$this->input->post('firstdate'),$this->input->post('lastdate')) && $this->form_validation->run())
{
$params = array(
'typeconge_id' => $this->input->post('typeconge_id'),
'interim_id' => $this->input->post('interim_id'),
'etat' => 0,
'creationdate' => date("Y-m-d H:i"),
'datedebut' =>$this->input->post('datedebut'),
'datefin' => $this->input->post('datefin'),
'users_id' => $user["iduser"],
'depart_id' => $user["iddepart"],
'description' => $this->input->post('description'),
);
if($this->input->post('demijourned')!=false){
$params['demijourned']=$this->input->post('demijourned');
if( $this->input->post('demijourned') =="Matin"){
$params['tempdebut']="08:00";
}else if( $this->input->post('demijourned') =="Aprés Midi"){
$params['tempdebut']="14:00";
}
}
else{
$params['demijourned']="0";
$params['tempdebut']="08:00";
}
if($this->input->post('demijournef')!=false){
$params['demijournef']=$this->input->post('demijournef');
if( $this->input->post('demijournef') =="Matin"){
$params['tempfin']="12:00";
}else if( $this->input->post('demijournef') =="Aprés Midi"){
$params['tempfin']="18:00";
}
}
else{
$params['demijournef']="0";
$params['tempfin']="18:00";
}
$this->load->model("Ecritureplanning_model");
$reste = ($user["soldeconge"]-$this->Conge_model->get_sum_conge($user["iduser"]))>=$this->Conge_model->calculate_sum($params);
$this->data["ecriture"]=$this->Ecritureplanning_model;
if($reste){
$conge_id = $this->Conge_model->add_conge($params);
}else{
redirect("Conge/index/rejectsolde");
}
$this->data["conge"]=$this->Conge_model->get_conge_to_pdf($conge_id);
$this->data["user"]= $user;
$notified = $this->User_model->get_user($user["responsable_id"]);
$this->data["isnotifed"]= true;
$this->data["crypted"] = md5($notified["email"]);
$this->data["counter"]= $this->User_model->HasDemande($user["iduser"],$this->input->post('firstdate'),$this->input->post('lastdate'));
$message = $this->load->view("mail/conge",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Conge");
$this->email->to($notified["email"]);
$this->email->subject("Notification de demande de conge pour ".$user["name"]." ".$user["prenom"]);
$this->email->message($message);
$this->email->send();
if($this->User_model->isResponsableOut($user["responsable_id"])){
$interim = $this->User_model->get_user_interim($user["responsable_id"]);
$this->data["crypted"] = md5($interim["email"]);
$message = $this->load->view("mail/conge",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Conge");
$this->email->to($interim["email"]);
$this->email->subject("Notification de demande de conge pour ".$user["name"]." ".$user["prenom"]);
$this->email->message($message);
$this->email->send();
}
redirect('conge/index');
}
else
{
if($this->input->post('datedebut')!= false && $this->User_model->isUserOut($user["iduser"],$this->input->post('firstdate'),$this->input->post('lastdate'))){
$this->data["message"]="Vous avez une demande déja en cours dans cette date";
}
$this->load->model('Typeconge_model');
$this->data['all_typeconge'] = $this->Typeconge_model->get_all_typeconge();
$this->load->model('User_model');
$user = $this->session->userdata("auth");
$this->data['all_users'] = $this->User_model->get_all_users_same_service($user["service_id"]);
$this->data['_view'] = 'conge/add';
$this->load->view('layouts/main',$this->data);
}
}
/*
* Editing a conge
*/
function edit($id)
{
$this->data['conge'] = $this->Conge_model->get_conge($id);
if(isset($this->data['conge']['id']))
{
$this->load->library('form_validation');
$this->form_validation->set_rules('datedebut','Datedebut','required');
$this->form_validation->set_rules('datefin','Datefin','required');
if($this->form_validation->run())
{
$params = array(
'typeconge_id' => $this->input->post('typeconge_id'),
'interim_id' => $this->input->post('interim_id'),
'datedebut' => date("Y-m-d",strtotime($this->input->post('datedebut'))),
'tempdebut' => $this->input->post('tempdebut'),
'datefin' => date("Y-m-d",strtotime($this->input->post('datefin'))),
'tempfin' => $this->input->post('tempfin'),
'users_id' => $this->input->post('users_id'),
'description' => $this->input->post('description'),
);
$this->Conge_model->update_conge($id,$params);
redirect('conge/index');
}
else
{
$this->load->model('User_model');
$this->data['all_users'] = $this->User_model->get_all_users();
$this->data["user"] = $this->User_model->get_user($this->data['conge']["users_id"]);
$this->load->model('Transport_model');
$this->data['all_transport'] = $this->Transport_model->get_all_transport();
$this->load->model('Objetmission_model');
$this->data['all_objetmission'] = $this->Objetmission_model->get_all_objetmission();
$this->load->model('Typemission_model');
$this->data['all_typemission'] = $this->Typemission_model->get_all_typemission();
$this->load->model('Typeconge_model');
$this->data['all_typeconge'] = $this->Typeconge_model->get_all_typeconge();
$this->load->model('Objetautorisation_model');
$this->data['all_objetautorisation'] = $this->Objetautorisation_model->get_all_objetautorisation();
$this->data['_view'] = 'conge/edit';
$this->load->view('layouts/main',$this->data);
}
}
else
show_error('The conge you are trying to edit does not exist.');
}
/*
* Deleting conge
*/
function remove($id)
{
$conge = $this->Conge_model->get_conge($id);
// check if the conge exists before trying to delete it
if(isset($conge['id']))
{
$this->Conge_model->delete_conge($id);
redirect('conge/index');
}
else
show_error('The conge you are trying to delete does not exist.');
}
function annuler($id){
if($this->session->userdata("auth")!= false){
$updated = $this->Conge_model->update_etat($id,3);
redirect('conge/index/');
}
}
function confirm($id,$email=null){
if($email!= null){
$this->load->model("User_model");
$connected = $this->User_model->connectMail($email);
if(isset($connected[0])){
$this->session->set_userdata("auth",$connected[0]);
}
}
if($this->session->userdata("auth")!= false){
$conge = $this->Conge_model->get_conge($id);
$this->load->model('User_model');
if($this->User_model->isUserOut($conge["users_id"],$conge["datedebut"]." ".$conge["tempdebut"],$conge["datefin"]." ".$conge["tempfin"])){
redirect("Conge/index/confirm");
}else{
$updated = $this->Conge_model->update_etat($id,1);
if($updated != false){
$this->load->model('Notifier_model');
$user = $this->User_model->get_user($conge["users_id"]);
$usermail = $this->User_model->connect($user["login"],$user["password"]);
$notifieds = $this->Notifier_model->get_notified_by_user_id($conge["users_id"]);
$this->data["conge"]=$this->Conge_model->get_conge_to_pdf($id);
$this->data["user"]= $usermail;
$this->data["isnotifed"]=false;
$this->load->model("Ecritureplanning_model");
$this->data["ecriture"]= $this->Ecritureplanning_model;
$message = $this->load->view("mail/conge",$this->data,true);
foreach ($notifieds as $notified) {
if($user["responsable_id"]!=$notified["notifyto"]){
$this->email->from($this->sendmail, 'Notification:'."Conge");
$this->email->to($notified["email"]);
$this->email->subject("Notification de demande de conge");
$this->email->message($message);
echo $this->email->send();
}
}
$this->email->from($this->sendmail, 'Notification:'."Conge");
$this->email->to($user["email"]);
$this->email->subject('La demande de conge est confirmee');
$this->email->message($message);
echo $this->email->send();
redirect('conge/index/');
//echo "<script>window.close();</script>";
}
}
}else{
redirect('Login/index/');
}
}
function reject($id,$email=null){
if($email!= null){
$this->load->model("User_model");
$connected = $this->User_model->connectMail($email);
if(isset($connected[0])){
$this->session->set_userdata("auth",$connected[0]);
}
}
if($this->session->userdata("auth")!= false){
$this->Conge_model->update_etat($id,2);
$conge = $this->Conge_model->get_conge($id);
$this->data["isnotifed"]=false;
$message = $this->load->view("mail/conge",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Conge");
$this->email->to($user["email"]);
$this->email->subject("Notification de demande de conge est rejetee");
$this->email->message($message);
echo $this->email->send();
redirect('conge/index');
}else{
redirect('Login/index/');
}
}
function ChartEtat($datedebut=null,$datefin=null)
{
if($datedebut==null && $datefin==null){
echo json_encode($this->Conge_model->ChartEtat());
}else{
echo json_encode($this->Conge_model->ChartEtat($datedebut,$datefin));
}
}
function CalculateService(){
//print_r($this->input->post());
echo $this->Conge_model->calculate_sum($this->input->post());
}
}