<?php 

use common\models\Commande ;

$num_jour_semaine = date('w', strtotime($date));
$arr_jour_semaine = [0 => 'dimanche', 1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi'];
$champs_horaires_point_vente = 'horaires_' . $arr_jour_semaine[$num_jour_semaine];

$html = '' ;

// par point de vente
foreach ($points_vente as $pv) {
    if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) {
        
        $html .= '<h3>'.$pv->nom.'</h3>' ;

        $col_credit_pain = '' ;
        if($pv->credit_pain) {
            $col_credit_pain = '<th>Rappel crédit</th>' ;
        }
        
        $html .= '<table class="table table-bordered">'
            . '<thead>'
                . '<tr>'
                    . '<th>Client</th>'
                    . '<th>Produits</th>'
                    . '<th>Commentaire</th>' 
                    . $col_credit_pain 
                    . '<th>Montant</th>'
                . '</tr>'
            . '<tbody>';
        
                foreach ($pv->commandes as $c) {
                    $html .= '<tr>' ;
                    $str_user = '';

                    // username
                    if ($c->user) {
                        $str_user = $c->user->prenom . " " . $c->user->nom; //.' - '.date('d/m', strtotime($c->date)) ;
                    } else {
                        $str_user = $c->username; //.' - '.date('d/m', strtotime($c->date)) ;
                    }
                    
                    if(strlen($c->commentaire_point_vente)) 
                    {
                        $str_user .= '<br /><em>'.$c->commentaire_point_vente.'</em>' ;
                    }
                    
                    // téléphone
                    if (isset($c->user) && strlen($c->user->telephone)) {
                        $str_user .= '<br />' . $c->user->telephone . '';
                    }

                    $html .= '<td>'.$str_user.'</td>';
                    
                    // produits
                    $str_produits = '';
                    foreach ($produits as $p) {
                        if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
                            $add = false;
                            foreach ($c->commandeProduits as $cp) {
                                if ($p->id == $cp->id_produit) {
                                    $str_produits .= $cp->quantite . '&nbsp;' . $p->nom . ', ';
                                    $add = true;
                                }
                            }
                        }
                    }
                    
                    $html .= '<td>'.substr($str_produits, 0, strlen($str_produits) - 2).'</td>';
                    $html .= '<td>'.$c->commentaire.'</td>';
                    
                    if($pv->credit_pain) {
                        $credit = '' ;
                        if(isset($c->user) && isset($c->user->userEtablissement)) {
                            $credit = number_format($c->user->userEtablissement[0]->credit,2).' €' ;
                        }
                        $html .= '<td>'.$credit.'</td>' ;       
                    }
                    
                    $html .= '<td><strong>'.number_format($c->montant, 2) . ' € ';

                    if($c->getStatutPaiement() == Commande::STATUT_PAYEE)
                    {
                        $html .= '(payé)' ;
                    }
                    elseif($c->getStatutPaiement() == Commande::STATUT_IMPAYEE && $c->getMontantPaye())
                    {
                        $html .= '(reste '.$c->getMontantRestant(true).' à payer)' ;
                    }
                    elseif($c->getStatutPaiement() == Commande::STATUT_SURPLUS)
                    {
                        $html .= '(surplus : '.$c->getMontantSurplus(true).' à rembourser)' ;
                    }
                    
                    $html .= '</strong></td>' ;
                    
                    $html .= '</tr>' ;
                    
                }

                $html .= '<tr><td><strong>Total</strong></td>' ;
                
                $str_produits = '';
                foreach ($produits as $p) {
                    if (!$p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
                        $quantite = Commande::getQuantiteProduit($p->id, $pv->commandes);
                        $str_quantite = '';
                        if ($quantite) {
                            $str_quantite = $quantite;
                            $str_produits .= $str_quantite .'&nbsp;'. $p->nom . ', ';
                        }
                    }
                }
                
                $str_produits = substr($str_produits, 0, strlen($str_produits) - 2) ;
                
                $html .= '<td>'.$str_produits.'</td><td></td>' ;
                if($pv->credit_pain) {
                    $html .= '<td></td>' ;
                }
                $html .= '<td><strong>'.number_format($pv->recettes_pain, 2) . ' €</strong></td>';
                
                $html .= '</tbody></table><pagebreak>' ;
    }
}

// par point de vente
$html .= '<h3>Points de vente</h3>' ;
$html .= '<table class="table table-bordered">'
            . '<thead>'
                . '<tr>'
                    . '<th>Point de vente</th>'
                    . '<th>Produits</th>'
                    . '<th>Montant</th>'
                . '</tr>'
            . '<tbody>';

$recettes = 0 ;
foreach ($points_vente as $pv) 
{
    if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) 
    {
        $html .= '<tr><td>'.$pv->nom.'</td><td>' ;
        foreach ($produits as $p) {
            if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
                $quantite = Commande::getQuantiteProduit($p->id, $pv->commandes);
                $str_quantite = '';

                if (!$p->vrac) {
                    if ($quantite)
                        $str_quantite = $quantite;
                }

                if(strlen($str_quantite))
                    $html .= $str_quantite . '&nbsp;'.$p->nom.', ' ;
            }
        }
        
        $html = substr($html, 0, strlen($html) - 2) ;
        
        $html .= '</td><td>'.number_format($pv->recettes_pain, 2).' €</td></tr>' ;
        $recettes += $pv->recettes_pain ;
    }
}

// total
$html .= '<tr><td><strong>Total</strong></td><td>' ;
foreach ($produits as $p) {
    if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) {
        $quantite = Commande::getQuantiteProduit($p->id, $commandes);
        if(!$p->vrac && $quantite)
            $html .= $quantite . '&nbsp;'.$p->nom.', ' ;
    }
}

$html = substr($html, 0, strlen($html) - 2) ;

$html .= '</td><td><strong>'.number_format($recettes, 2).' €</strong></td></tr>' ;

$html .= '</tbody></table>' ;

echo $html ;

?>