<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: resa_multiple.class.php,v 1.2 2026/06/23 13:58:11 dgoron Exp $

if (stristr($_SERVER['REQUEST_URI'], ".class.php")) die("no access");

class resa_multiple {

    protected $id_empr = 0;
    
    protected $records = [];
    
    public function __construct($id_empr=0, $records=[]) {
        $this->id_empr = intval($id_empr);
        $this->records = $records;
    }
		
	public function get_query_pickup_locations() {
	    global $pmb_location_reservation, $empr_location;
	    
	    $query = "SELECT idlocation, location_libelle FROM docs_location WHERE location_visible_opac=1";
// 	    $query .= " AND transfert_pickup_location = 1";
	    if($pmb_location_reservation) {
	        $query .= "  AND idlocation in (select resa_loc from resa_loc where resa_emprloc=$empr_location)";
	    }
	    $query .= " ORDER BY location_libelle";
	    return $query;
	}
		
	public function get_options_pickup_locations() {
	    global $empr_location;
	    
	    $optionsHtml = '';
	    
	    $loc_list=array();
	    $flag_transferable=0;
	    $req_loc_list = $this->get_query_pickup_expl_location();
	    $res_loc_list = pmb_mysql_query($req_loc_list);
	    if(pmb_mysql_num_rows($res_loc_list)){
	        while ($r = pmb_mysql_fetch_object($res_loc_list)){
	            $loc_list[]=$r->expl_location;
	            // au moins un expl transférable
	            $flag_transferable=1;
	        }
	    }
	    $loc_req = $this->get_query_pickup_locations();
	    $res = pmb_mysql_query($loc_req);
	    //on parcours la liste des localisations
	    while ($value = pmb_mysql_fetch_array($res)) {
	        if(!$flag_transferable){
	            // il y en a un ici?
	            $req= "select expl_id from exemplaires, docs_statut where expl_notice IN (".implode(",",$this->records).") AND expl_bulletin='0' and expl_location = " . $value[0] . "
					and expl_statut=idstatut and statut_allow_resa=1 ";
	            $res_expl = pmb_mysql_query($req);
	            if(!pmb_mysql_num_rows($res_expl)){
	                continue;
	            }
	        }
	        if($value[0]==$empr_location) {
	            $selected=" selected='selected' ";
	        } else {
	            $selected="";
	        }
	        $optionsHtml .= "<option value='" . $value[0] . "' $selected >" . translation::get_translated_text($value[0], "docs_location", "location_libelle", $value[1]) . "</option>";
	    }
	    return $optionsHtml;
	}
		
	public function get_query_pickup_expl_location() {
	    global $pmb_location_reservation, $empr_location;
	    
	    $query = "SELECT expl_location FROM exemplaires, docs_statut WHERE expl_notice IN (".implode(",",$this->records).") and  expl_statut=idstatut
            AND transfert_flag=1 AND statut_allow_resa=1 AND expl_bulletin='0'";
	    if($pmb_location_reservation) {
	        $query .= " AND expl_location in (select resa_loc from resa_loc where resa_emprloc=$empr_location)";
	    }
	    return $query;
	}
		
	public function get_location_form() {
	    global $msg, $lvl, $sub;
	    
	    $optionsHtml = $this->get_options_pickup_locations();
	    if($optionsHtml) {
	        $tmpHtml = '<form method="post" action="do_resa.php?lvl='.$lvl.'&sub='.$sub.'">';
	        $tmpHtml .= $msg['reservation_selection_localisation'].'<br />';
	        $tmpHtml .= '<select name="idloc">';
	        $tmpHtml .= $optionsHtml;
	        $tmpHtml .= "</select><input type='hidden' name='listeNotices' value='".implode(",",$this->records)."'><br /><br /><input class='bouton' type='submit' value='" . $msg['reservation_bt_choisir_localisation'] . "'></form>";
	        return $tmpHtml;
	    }
	    return '';
	}
		
	public function get_form() {
	    global $idloc;
	    
	    //ON ENVOIT QUELQUES EVENTS...
	    $evth = events_handler::get_instance();
	    $evt = new event_resa_mutiple("resa_multiple", "show_form");
	    $evt->set_notices($this->records);
	    $evth->send($evt);
	    if($evt->get_form() != ''){
	        return $evt->get_form();
	    }else{
	        if ($idloc==''){
	            //les transferts sont actifs, avec un choix du lieu de retrait et pas de choix encore fait
	            //=> on affiche les localisations
	            return $this->get_location_form();
	        }
	    }
        return '';
	}
		
	public function get_display_table($id_location=0, $with_event=false) {
	    global $msg;
	    
	    $evth = events_handler::get_instance();
	    $resa_cart_display = '<table><tr><th colspan="2">'.$msg['empr_menu_resa'].' : </th></tr>';
	    foreach($this->records as $notice_id){
	        $resa_cart_display.= '<tr>';
	        $resa = reservation::get_instance_from_empr_and_notice($this->id_empr, $notice_id);
	        if ($id_location) {
	            if ($with_event) {
		            $event = new event_resa_mutiple('resa_multiple','before_validate');
		            $event->set_empr_id($_SESSION["id_empr_session"]);
		            $event->set_notice($resa->id_notice);
		            $event->set_bulletin($resa->id_bulletin);
		            $evth->send($event);
		            if($event->get_id_loc() !== false && $event->get_id_loc()){
		                $id_location = $event->get_id_loc();
		            }
	            }
	            if($resa->add($id_location)){
	                $resa_cart_display.= '<td>'.$resa->notice.'</td><td>'.$resa->message.'</td>';
	                reservation::alert_mail_users_pmb($resa->id_notice, $resa->id_bulletin, $_SESSION["id_empr_session"]);
	                //On retire la notice du panier ?
	                delete_cart_record($notice_id);
	            }else{
	                $resa_cart_display.= '<td>'.$resa->notice.'</td><td>'.$resa->message.'</td>';
	            }
	            if ($with_event) {
		            $event = new event_resa_mutiple('resa_multiple','validate_resa');
		            $event->set_empr_id($_SESSION["id_empr_session"]);
		            $event->set_notice($resa->id_notice);
		            $event->set_bulletin($resa->id_bulletin);
		            $event->set_resa_id($resa->id);
		            $evth->send($event);
	            }
	        } else {
		        $resa_cart_display.= '<td>'.$resa->notice.'</td><td><strong>'.$msg['resa_no_expl'].'</strong></td>';
	        }
	        $resa_cart_display.= '</tr>';
	    }
	    $resa_cart_display.='</table>';
	    if ($with_event && $id_location) {
	        $event = new event_resa_mutiple('resa_multiple','finished');
	        $event->set_empr_id($_SESSION["id_empr_session"]);
	        $evth->send($event);
	    }
	    return $resa_cart_display;
	}
		
	public function get_display() {
	    global $pmb_transferts_actif, $transferts_choix_lieu_opac;
	    global $idloc, $listeNotices;
	    
	    if ($pmb_transferts_actif=='1' && $transferts_choix_lieu_opac=='1' ) {
	        $display = $this->get_form();
	        if (empty($display)) {
	            if ($idloc==''){
	                $display = $this->get_display_table();
	            } else {
// 		                $notices=explode(',',$listeNotices);
	                $this->records = explode(',',$listeNotices);
	                $display = $this->get_display_table($idloc, true);
	            }
	        }
	    } else {
	        $display = $this->get_display_table($_SESSION['empr_location']);
	    }
	    return $display;
	}
		
	public function proceed() {
	    global $base_path, $opac_resa_popup;
	    
	    if ($opac_resa_popup != 1){
	        require_once $base_path.'/includes/show_cart.inc.php';
	    }
	    print '<br/><br/>'.$this->get_display();
	}
}
