<?php
/*
* Generated by CRUDigniter v3.2
* www.crudigniter.com
*/
class Autorisation extends FrontApplication{
function __construct()
{
parent::__construct();
$this->load->model('Autorisation_model');
}
function pdf($id){
$this->data["autorisation"]=$this->Autorisation_model->get_autorisation_to_pdf($id);
$this->load->library("html2pdf/HTML2PDF");
$this->load->view("autorisation/pdf",$this->data);
}
function exportlist($datedebut, $datefin,$objetautorisation,$etat,$iduser){
if($objetautorisation>0){
$this->db->where("objetautorisation_id",$objetautorisation);
}
if($iduser>0){
$this->db->where("users_id",$iduser);
}
if($etat>=0){
$this->db->where("etat",$etat);
}
$this->data["autorisation"]=$this->Autorisation_model->get_all_autorisation_by_date($datedebut, $datefin);
$this->load->library("html2pdf/HTML2PDF");
$this->load->view("autorisation/export",$this->data);
}
/*
* Listing of autorisation
*/
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"]["Autorisation"]->actions);
$config = $this->config->item('pagination');
$config['base_url'] = site_url('autorisation/index?');
$config['total_rows'] = $this->Autorisation_model->get_all_autorisation_count();
$this->pagination->initialize($config);
$user = $this->session->userdata("auth");
$this->data['autorisation'] = $this->Autorisation_model->get_all_autorisation($params,$user["type"]);
//-----------------------------------------------------------------------
if($msg != null && $msg =="confirm"){
$this->data['msg']= "Vous avez d'autres demandes confirmées dans la méme periode";
}
//-----------------------------------------------------------------------
$this->load->model('Objetautorisation_model');
$this->data['all_objetautorisation'] = $this->Objetautorisation_model->get_all_objetautorisation();
//--------------------------------------------------------------------------
$this->load->model('User_model');
$this->data['all_users'] = $this->User_model->get_all_users();
//-----------------------------------------------------------------------------
$this->data['_view'] = 'autorisation/index';
$this->load->view('layouts/main',$this->data);
}
/*
* Adding a new autorisation
*/
function add()
{
$user = $this->session->userdata("auth");
$solde = $this->Autorisation_model->get_solde_autorisation($user["iduser"]);
$this->load->model('User_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('datedemande','Datedemande','required');
$this->form_validation->set_rules('heursdebut','Heursdebut','required');
$this->form_validation->set_rules('heursfin','Heursfin','required');
$user = $this->session->userdata("auth");
$this->load->model("Ecritureplanning_model");
$inPlanningDates = $this->Ecritureplanning_model->isInPlanningDates($this->input->post('datedemande')." ".$this->input->post('heursdebut'),$this->input->post('datedemande')." ".$this->input->post('heursfin')) ;
$countferie =$this->Ecritureplanning_model->get_count_ferie_in_periode($this->input->post('datedemande'),$this->input->post('datedemande'));
if($inPlanningDates == true && $countferie==0 && $solde["solde"]<$user["soldeautorisation"] && !$this->User_model->isUserOut($user["iduser"],$this->input->post('datedemande')." ".$this->input->post('heursdebut'),$this->input->post('datedemande')." ".$this->input->post('heursfin')) && $this->form_validation->run())
{
$params = array(
'transport_id' => $this->input->post('transport_id'),
'objetautorisation_id' => $this->input->post('objetautorisation_id'),
'description' => $this->input->post('description'),
'etat' =>0,
'creationdate' => date("Y-m-d H:i"),
'datedemande' =>$this->input->post('datedemande'),
'heursdebut' => date("H:i",strtotime($this->input->post('heursdebut'))),
'heursfin' => date("H:i",strtotime($this->input->post('heursfin'))),
'users_id' => $user["iduser"],
'depart_id' => $user["iddepart"],
'description' => $this->input->post('description'),
);
$autorisation_id = $this->Autorisation_model->add_autorisation($params);
$this->data["autorisation"]=$this->Autorisation_model->get_autorisation_to_pdf($autorisation_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('datedemande')." ".$this->input->post('heursdebut'),$this->input->post('datedemande')." ".$this->input->post('heursfin'));
$message = $this->load->view("mail/autorisation",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Autorisation");
$this->email->to($notified["email"]);
$this->email->subject("Notification de demande de autorisation 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/autorisation",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Autorisation");
$this->email->to($interim["email"]);
$this->email->subject("Notification de demande de autorisation pour ".$user["name"]." ".$user["prenom"]);
$this->email->message($message);
$this->email->send();
}
redirect('autorisation/index');
}
else
{
if($this->input->post('datedemande')!= false && $this->User_model->isUserOut($user["iduser"],$this->input->post('datedemande')." ".$this->input->post('heursdebut'),$this->input->post('datedemande')." ".$this->input->post('heursfin'))){
if(!isset($this->data["message"])){
$this->data["message"]="";
}
$this->data["message"].="Vous avez une demande déja en cours dans cette date";
}
if($countferie>0){
if(!isset($this->data["message"])){
$this->data["message"]="";
}
$this->data["message"].="Vous n'avez pas le droit d'une autorisation dans un jour ferié";
}
$this->load->model('Transport_model');
$this->data['all_transport'] = $this->Transport_model->get_all_transport();
$this->load->model('User_model');
$this->data['all_users'] = $this->User_model->get_all_users();
$this->load->model('Objetautorisation_model');
$this->data['all_objetautorisation'] = $this->Objetautorisation_model->get_all_objetautorisation();
$this->data["soldeconsomme"]=$solde["solde"];
$this->data['_view'] = 'autorisation/add';
$this->load->view('layouts/main',$this->data);
}
}
/*
* Editing a autorisation
*/
function edit($id)
{
// check if the autorisation exists before trying to edit it
$this->data['autorisation'] = $this->Autorisation_model->get_autorisation($id);
if(isset($this->data['autorisation']['id']))
{
$this->load->library('form_validation');
$this->form_validation->set_rules('datedemande','Datedemande','required');
$this->form_validation->set_rules('heursdebut','Heursdebut','required');
$this->form_validation->set_rules('heursfin','Heursfin','required');
if($this->form_validation->run())
{
$params = array(
'transport_id' => $this->input->post('transport_id'),
'objetautorisation_id' => $this->input->post('objetautorisation_id'),
'datedemande' =>date("Y-m-d",strtotime($this->input->post('datedemande'))),
'heursdebut' =>date("H:i",strtotime($this->input->post('heursdebut'))),
'heursfin' => date("H:i",strtotime($this->input->post('heursfin'))),
'description' => $this->input->post('description'),
);
$this->Autorisation_model->update_autorisation($id,$params);
redirect('autorisation/index');
}
else
{
$this->load->model('Transport_model');
$this->data['all_transport'] = $this->Transport_model->get_all_transport();
$this->load->model('User_model');
$this->data['all_users'] = $this->User_model->get_all_users();
$this->load->model('Objetautorisation_model');
$this->data['all_objetautorisation'] = $this->Objetautorisation_model->get_all_objetautorisation();
$this->data['_view'] = 'autorisation/edit';
$this->load->view('layouts/main',$this->data);
}
}
else
show_error('The autorisation you are trying to edit does not exist.');
}
/*
* Deleting autorisation
*/
function remove($id)
{
$autorisation = $this->Autorisation_model->get_autorisation($id);
// check if the autorisation exists before trying to delete it
if(isset($autorisation['id']))
{
$this->Autorisation_model->delete_autorisation($id);
redirect('autorisation/index');
}
else
show_error('The autorisation you are trying to delete does not exist.');
}
function confirm($id,$email=null,$continue=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->load->model('User_model');
$autorisation = $this->Autorisation_model->get_autorisation($id);
if($this->User_model->isUserOut($autorisation["users_id"],$autorisation["datedemande"]." ".$autorisation["heursdebut"],$autorisation["datedemande"]." ".$autorisation["heursfin"])){
redirect("Autorisation/index/confirm");
}else{
$updated = $this->Autorisation_model->update_etat($id,1);
if($updated != false){
$this->load->model('Notifier_model');
$user = $this->User_model->get_user($autorisation["users_id"]);
$usermail = $this->User_model->connect($user["login"],$user["password"]);
$notifieds = $this->Notifier_model->get_notified_by_user_id($autorisation["users_id"]);
$this->data["autorisation"]=$this->Autorisation_model->get_autorisation_to_pdf($id);
$this->data["user"]= $usermail;
$this->data["isnotifed"]=false;
$message = $this->load->view("mail/autorisation",$this->data,true);
foreach ($notifieds as $notified) {
if($user["responsable_id"]!=$notified["notifyto"]){
$this->email->from($this->sendmail, 'Notification:'."Autorisation");
$this->email->to($notified["email"]);
$this->email->subject("Notification de demande de autorisation");
$this->email->message($message);
echo $this->email->send();
}
}
$this->data["isnotifed"]=false;
$this->email->from($this->sendmail, 'Notification:'."Autorisation");
$this->email->to($user["email"]);
$this->email->subject("Notification de demande de autorisation est confirmee");
$this->email->message($message);
echo $this->email->send();
redirect('autorisation/index/');
}
}
}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->Autorisation_model->update_etat($id,2);
$autorisation = $this->Autorisation_model->get_autorisation($id);
$this->data["isnotifed"]=false;
$message = $this->load->view("mail/autorisation",$this->data,true);
$this->email->from($this->sendmail, 'Notification:'."Autorisation");
$this->email->to($user["email"]);
$this->email->subject("Notification de demande de autorisation est rejetee");
$this->email->message($message);
echo $this->email->send();
redirect('autorisation/index');
}else{
redirect('Login/index/');
}
}
function annuler($id){
if($this->session->userdata("auth")!= false){
$updated = $this->Autorisation_model->update_etat($id,3);
redirect('conge/index/');
}
}
function ChartEtat($datedebut=null,$datefin=null)
{
if($datedebut==null && $datefin==null){
echo json_encode($this->Autorisation_model->ChartEtat());
}else{
echo json_encode($this->Autorisation_model->ChartEtat($datedebut,$datefin));
}
}
}