<?php

// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: lettre_commande.inc.php,v 1.1 2011-08-16 12:17:30 dbellamy Exp $
if (stristr($_SERVER['REQUEST_URI'], ".inc.php")) {
    die("no access");
}
// popup d'impression PDF pour liste des relances de receptions
// reçoit : id_bibli, id_cde
require_once "{$class_path}/entites.class.php";
require_once "{$base_path}/acquisition/achats/commandes/lettre_commande.class.php";
if ($id_cde && $id_bibli) {
    $lettre = lettreCommande_factory::make();
    $lettre->doLettre($id_bibli, $id_cde);
    $lettre->getLettre();
}
function print_cde($id_bibli = 0, $id_cde = 0, $by_mail = FALSE)
{
    global $dbh, $msg, $charset, $base_path, $acquisition_pdfcde_print;
    global $acquisition_pdfcde_obj_mail, $acquisition_pdfcde_text_mail;
    global $acquisition_pdfcde_by_mail, $PMBuseremailbcc;
    if (!($id_bibli && $id_cde)) {
        return;
    }
    $bib = new entites($id_bibli);
    $bib_coord = pmb_mysql_fetch_object(entites::get_coordonnees($id_bibli, 1));
    $cde = new actes($id_cde);
    $id_fou = $cde->num_fournisseur;
    $fou = new entites($id_fou);
    $fou_coord = pmb_mysql_fetch_object(entites::get_coordonnees($id_fou, 1));
    $no_mail = FALSE;
    if ($by_mail == FALSE || !($acquisition_pdfcde_by_mail && strpos($bib_coord->email, '@') && strpos($fou_coord->email, '@'))) {
        $no_mail = TRUE;
    } else {
        $dest_name = '';
        if ($fou_coord->libelle) {
            $dest_name = $fou_coord->libelle;
        } else {
            $dest_name = $fou->raison_sociale;
        }
        if ($fou_coord->contact) {
            $dest_name .= " " . $fou_coord->contact;
        }
        $dest_mail = $fou_coord->email;
        $obj_mail = $acquisition_pdfcde_obj_mail;
        $text_mail = $acquisition_pdfcde_text_mail;
        $bib_name = $bib_coord->raison_sociale;
        $bib_mail = $bib_coord->email;
        $lettre = lettreCommande_factory::make();
        $lettre->doLettre($id_bibli, $id_cde);
        $piece_jointe = array();
        $piece_jointe[0]['contenu'] = $lettre->getLettre('S');
        $piece_jointe[0]['nomfichier'] = $lettre->getFileName();
        //         mailpmb($to_nom="", $to_mail,   $obj="",   $corps="",  $from_name="", $from_mail, $headers, $copie_CC="", $copie_BCC="", $faire_nl2br=0, $pieces_jointes=array())
        $res_envoi = mailpmb($dest_name, $dest_mail, $obj_mail, $text_mail, $bib_name, $bib_mail, "Content-Type: text/plain; charset=\"{$charset}\"", '', $PMBuseremailbcc, 1, $piece_jointe);
        if (!$res_envoi) {
            $no_mail = TRUE;
        }
        if (!$no_mail) {
            print "<h3>" . sprintf($msg["acquisition_print_emailsucceed"] . "</h3>", $dest_mail);
        } else {
            print "<h3>" . sprintf($msg["acquisition_print_emailfailed"] . "</h3>", $dest_mail);
        }
    }
    if ($no_mail) {
        print "\t\n\t\t\t<form name='print_cde' action='pdf.php?pdfdoc=cmde' target='lettre' method='post'>\t\t\n\t\t\t\t<input type='hidden' name='id_bibli' value='" . $id_bibli . "'/>\n\t\t\t\t<input type='hidden' name='id_cde' value='" . $id_cde . "'/>\n\t\t\t\t<script type='text/javascript'>\n\t\t\t\t\topenPopUp('','lettre', 600, 500, -2, -2, 'toolbar=no, dependent=yes, resizable=yes');\n\t\t\t\t\tdocument.print_cde.submit();\n\t\t\t\t</script>\n\t\t\t</form>";
    }
}