<commit_before><?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class RecolteMailingManager
 * @author mathurin
 */
class RecolteMailingManager {
     
    protected $mailer = null;
    protected $partial_function = null;

    protected $dr = null;
    protected $tiers = null;
    protected $annee = null;
    protected $current_document = null;
    protected $current_pdf_content = null;
    protected $csvContent = null;
    protected $acheteurs = null;



    public function __construct($mailer,$partial_fct, $dr,$tiers,$annee) {
        $this->mailer = $mailer;
        $this->partial_function = $partial_fct;
        $this->dr = $dr;
        $this->tiers = $tiers;
        $this->annee = $annee;
        
        $this->csvContent = $this->getCSVDrContent();
        
        $this->acheteurs = $this->dr->recolte->getAcheteursArray();
        $this->current_document = new ExportDRPdf($this->dr, $this->partial_function);
        $this->current_document->generatePDF();
        $this->current_pdf_content = $this->current_document->output();
        
    }
    
    public function setDR($dr) {
        $this->dr = $dr;
    }
    
    public function getDR() {
        return $this->dr;
    }
    
    
    public function sendMail($visualisation = true) {
        $message = $this->getMailForRecoltant($visualisation);
        try {
            $this->mailer->send($message);
        } catch (Exception $e) {

            return false;
        }
        return true;
    }
    
    public function sendAcheteursMails() {                
      $sendMailAcheteursReport = array();
        
      foreach ($this->acheteurs as $type_cvi => $vol) {
            $type_cvi_infos = explode('_', $type_cvi);
            $sendMailAcheteursReport[$type_cvi] = new stdClass();
            $sendMailAcheteursReport[$type_cvi]->type = $type_cvi_infos[0];
            $sendMailAcheteursReport[$type_cvi]->cvi = $type_cvi_infos[1];
            
            $acheteur = _TiersClient::getInstance()->retrieveByCvi($sendMailAcheteursReport[$type_cvi]->cvi);
            
            $sendMailAcheteursReport[$type_cvi]->nom = $acheteur->nom;
            
            $email = $acheteur->getCompteEmail();
            if($email) {
                $email = $acheteur->email;
            }
            $sendMailAcheteursReport[$type_cvi]->email = $email;
            $message = $this->getMailForAcheteur($acheteur);
        
            try {
                $message->setBcc("<EMAIL>");
                $this->mailer->send($message);
            } catch (Exception $e) {
               $sendMailAcheteursReport[$type_cvi]->sended = false;
            }
            $sendMailAcheteursReport[$type_cvi]->sended = true;
        }
        return $sendMailAcheteursReport;
    }
    
    public function getCSVDrContent(){ 
         $csvContruct = new ExportDRCsv($this->annee,$this->tiers->cvi);         
         $csvContruct->export();
         return $csvContruct->output();
        }
    
        
    public function getMailForAcheteur($acheteur) {
        
            $subject = 'CIVA - Déclaration de récolte de '.$this->tiers->nom;   
        
            $mess = 'Bonjour ' . $acheteur->nom . ',
         
Le vendeur de raisin '.$this->tiers->nom.' a souhaité vous faire parvenir sa déclaration de récolte pour l\'année ' . $this->annee .' depuis le portail du CIVA.
    
Vous trouverez ce document en pièce jointe aux formats PDF et CSV.

Cordialement,

Le CIVA';

        $email = $acheteur->getCompteEmail();
        if($email) {
            $email = $acheteur->email;
        }

        $message = Swift_Message::newInstance()
                ->setFrom(array($this->tiers->getCompteEmail() => $this->tiers->nom))
                ->setTo($email)
                ->setSubject($subject)
                ->setBody($mess);        
        
        
        $attachment_pdf = new Swift_Attachment($this->current_pdf_content, $this->current_document->getFileName(), 'application/pdf');
        $message->attach($attachment_pdf);
        
        if($this->csvContent) {
            $attachment_csv = new Swift_Attachment($this->csvContent, sprintf("DR_%s_%s", $this->tiers->cvi, $this->annee).'.csv', 'application/csv');
            $message->attach($attachment_csv);
        }
        return $message;
    }
        
    public function getMailForRecoltant($visualisation = true) {
                // si l'on vient de la page de visualisation
        if($visualisation)
        {
            $subject = 'CIVA - Votre déclaration de récolte';
            $mess = 'Bonjour ' . $this->tiers->nom . ',

Vous trouverez ci-joint votre déclaration de récolte pour l\'année ' . $this->annee . '.<commit_msg>Bug précédent commit<commit_after><?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class RecolteMailingManager
 * @author mathurin
 */
class RecolteMailingManager {
     
    protected $mailer = null;
    protected $partial_function = null;

    protected $dr = null;
    protected $tiers = null;
    protected $annee = null;
    protected $current_document = null;
    protected $current_pdf_content = null;
    protected $csvContent = null;
    protected $acheteurs = null;



    public function __construct($mailer,$partial_fct, $dr,$tiers,$annee) {
        $this->mailer = $mailer;
        $this->partial_function = $partial_fct;
        $this->dr = $dr;
        $this->tiers = $tiers;
        $this->annee = $annee;
        
        $this->csvContent = $this->getCSVDrContent();
        
        $this->acheteurs = $this->dr->recolte->getAcheteursArray();
        $this->current_document = new ExportDRPdf($this->dr, $this->partial_function);
        $this->current_document->generatePDF();
        $this->current_pdf_content = $this->current_document->output();
        
    }
    
    public function setDR($dr) {
        $this->dr = $dr;
    }
    
    public function getDR() {
        return $this->dr;
    }
    
    
    public function sendMail($visualisation = true) {
        $message = $this->getMailForRecoltant($visualisation);
        try {
            $this->mailer->send($message);
        } catch (Exception $e) {

            return false;
        }
        return true;
    }
    
    public function sendAcheteursMails() {                
      $sendMailAcheteursReport = array();
        
      foreach ($this->acheteurs as $type_cvi => $vol) {
            $type_cvi_infos = explode('_', $type_cvi);
            $sendMailAcheteursReport[$type_cvi] = new stdClass();
            $sendMailAcheteursReport[$type_cvi]->type = $type_cvi_infos[0];
            $sendMailAcheteursReport[$type_cvi]->cvi = $type_cvi_infos[1];
            
            $acheteur = _TiersClient::getInstance()->retrieveByCvi($sendMailAcheteursReport[$type_cvi]->cvi);
            
            $sendMailAcheteursReport[$type_cvi]->nom = $acheteur->nom;
            
            $email = $acheteur->getCompteEmail();
            if(!$email) {
                $email = $acheteur->email;
            }
            $sendMailAcheteursReport[$type_cvi]->email = $email;
            $message = $this->getMailForAcheteur($acheteur);
        
            try {
                $message->setBcc("<EMAIL>");
                $this->mailer->send($message);
            } catch (Exception $e) {
               $sendMailAcheteursReport[$type_cvi]->sended = false;
            }
            $sendMailAcheteursReport[$type_cvi]->sended = true;
        }
        return $sendMailAcheteursReport;
    }
    
    public function getCSVDrContent(){ 
         $csvContruct = new ExportDRCsv($this->annee,$this->tiers->cvi);         
         $csvContruct->export();
         return $csvContruct->output();
        }
    
        
    public function getMailForAcheteur($acheteur) {
        
            $subject = 'CIVA - Déclaration de récolte de '.$this->tiers->nom;   
        
            $mess = 'Bonjour ' . $acheteur->nom . ',
         
Le vendeur de raisin '.$this->tiers->nom.' a souhaité vous faire parvenir sa déclaration de récolte pour l\'année ' . $this->annee .' depuis le portail du CIVA.
    
Vous trouverez ce document en pièce jointe aux formats PDF et CSV.

Cordialement,

Le CIVA';

        $email = $acheteur->getCompteEmail();
        if(!$email) {
            $email = $acheteur->email;
        }

        $message = Swift_Message::newInstance()
                ->setFrom(array($this->tiers->getCompteEmail() => $this->tiers->nom))
                ->setTo($email)
                ->setSubject($subject)
                ->setBody($mess);        
        
        
        $attachment_pdf = new Swift_Attachment($this->current_pdf_content, $this->current_document->getFileName(), 'application/pdf');
        $message->attach($attachment_pdf);
        
        if($this->csvContent) {
            $attachment_csv = new Swift_Attachment($this->csvContent, sprintf("DR_%s_%s", $this->tiers->cvi, $this->annee).'.csv', 'application/csv');
            $message->attach($attachment_csv);
        }
        return $message;
    }
        
    public function getMailForRecoltant($visualisation = true) {
                // si l'on vient de la page de visualisation
        if($visualisation)
        {
            $subject = 'CIVA - Votre déclaration de récolte';
            $mess = 'Bonjour ' . $this->tiers->nom . ',

Vous trouverez ci-joint votre déclaration de récolte pour l\'année ' . $this->annee . '.
{'fraction_non_alphanumeric': 0.13061812197881312, 'fraction_numerical': 0.00041139565977578934, 'mean_word_length': 2.119666345845364, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 157, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2141393', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2955, 'n_words': 556}