<?php
////Alexis 
class Dashboard_Vente extends CI_Controller
{
  
function index(){

 $DATE1=$this->input->post('DATE1');
 $DATE2=$this->input->post('DATE2');
 

 if (empty($DATE1) && empty($DATE2))
 {
 $data['titre_rapport']=" Rapport  du ".strftime('%d-%m-%Y',strtotime(date('Y-m-d')));
 $cond = '';
 }
 if (!empty($DATE1))
 {
  $data['titre_rapport']=' Rapport du  '.strftime("%d-%m-%Y",strtotime($DATE1));
  $cond = ' AND ("'.$DATE1.'%" BETWEEN conge_demande_conge.DATE_DEBUT_CONGE_ACCORDE AND conge_demande_conge.DATE_FIN_CONGE_ACCORDE)';
 }

 if (!empty($DATE1) && !empty($DATE2))
 {
 $data['titre_rapport']=' Rapport du  '.strftime("%d-%m-%Y",strtotime($DATE1)). ' au '.strftime("%d-%m-%Y",strtotime($DATE2));
 $cond = '';
 }




 ///// rapport 1

            
 $requetv1 = $this->Model->getRequete('SELECT COUNT(vente_vente.ID_VENTE) AS NUMBRE, vente_statut.DESCRIPTION FROM `vente_statut`JOIN vente_vente ON vente_vente.ID_VENTE_STATUT = vente_statut.ID_VENTE_STATUT GROUP BY vente_statut.ID_VENTE_STATUT');
 $datav1 = '[';
 foreach ($requetv1 as $key) {
   
     $datav1 .= "{ name: '".$key['DESCRIPTION']."',
     y: ".$key['NUMBRE'].",
 },";
 }

 $data['datav1'] = $datav1.']';





 // Rapport 2

            $categorven2 = '[';
            $data_nombrevide1 = "[";
            $data_nombrevide2 = "[";
            $data_nombrevide3 = "[";
            $data_nombrevide4 = "[";

            $requet1 = $this->Model->getRequete('SELECT DISTINCT(DATE_COMMANDE) AS DATE_COMMANDE FROM `vente_vente` WHERE 1');

            foreach ($requet1 as $key)
               {

                $categorven2 .= '"'.$key['DATE_COMMANDE'].'",';
                

                  $nombrevide1=$this->Model->getRequeteOne("SELECT SUM(vente_details.QUANTITE_COMMANDE) AS COM FROM `vente_details` JOIN vente_vente ON vente_details.ID_VENTE = vente_vente.ID_VENTE WHERE vente_vente.DATE_COMMANDE LIKE '".$key['DATE_COMMANDE']."' AND vente_details.ID_TAILLE_BOUTEILLE = 1");
                  if ($nombrevide1['COM'] == NULL) {
                    $data_nombrevide1 .= '0,';
                  }
                  else{
                    $data_nombrevide1 .= $nombrevide1['COM'].',';
                  }
                  

                  $nombrevide2=$this->Model->getRequeteOne("SELECT SUM(vente_details.QUANTITE_COMMANDE) AS COM FROM `vente_details` JOIN vente_vente ON vente_details.ID_VENTE = vente_vente.ID_VENTE WHERE vente_vente.DATE_COMMANDE LIKE '".$key['DATE_COMMANDE']."' AND vente_details.ID_TAILLE_BOUTEILLE = 2");
                  
                  if ($nombrevide2['COM'] == NULL) {
                    $data_nombrevide2 .= '0,';
                  }
                  else{
                    $data_nombrevide2 .= $nombrevide2['COM'].',';
                  }

                  $nombrevide3=$this->Model->getRequeteOne("SELECT SUM(vente_details.QUANTITE_COMMANDE) AS COM FROM `vente_details` JOIN vente_vente ON vente_details.ID_VENTE = vente_vente.ID_VENTE WHERE vente_vente.DATE_COMMANDE LIKE '".$key['DATE_COMMANDE']."' AND vente_details.ID_TAILLE_BOUTEILLE = 3");
                  
                  if ($nombrevide3['COM'] == NULL) {
                    $data_nombrevide3 .= '0,';
                  }
                  else{
                    $data_nombrevide3 .= $nombrevide3['COM'].',';
                  }

                  $nombrevide4=$this->Model->getRequeteOne("SELECT SUM(vente_details.QUANTITE_COMMANDE) AS COM FROM `vente_details` JOIN vente_vente ON vente_details.ID_VENTE = vente_vente.ID_VENTE WHERE vente_vente.DATE_COMMANDE LIKE '".$key['DATE_COMMANDE']."' AND vente_details.ID_TAILLE_BOUTEILLE = 4");
                  
                  if ($nombrevide4['COM'] == NULL) {
                    $data_nombrevide4 .= '0,';
                  }
                  else{
                    $data_nombrevide4 .= $nombrevide4['COM'].',';
                  }

            }

            $data['categorven2'] = $categorven2."]"; 
            $data['data_nombrevide1'] = $data_nombrevide1."]"; 
            $data['data_nombrevide2'] = $data_nombrevide2."]"; 
            $data['data_nombrevide3'] = $data_nombrevide3."]"; 
            $data['data_nombrevide4'] = $data_nombrevide4."]"; 




 // Rapport 2

 $categories2 = '[';
 $data_nombreplien1 = "[";
 $data_nombreplien2 = "[";

 $requet2 = $this->Model->getRequete('SELECT DATE_SORTIE_STOCK, syst_taille_bouteille.DESCRIPTION, SUM(QUANTITE) AS TOTAL_QUANTITE FROM stock_sortie_stock JOIN syst_taille_bouteille ON syst_taille_bouteille.ID_TAILLE_BOUTEILLE = stock_sortie_stock.ID_TAILLE_BOUTEILLE GROUP BY DATE_SORTIE_STOCK, syst_taille_bouteille.DESCRIPTION');

 foreach ($requet2 as $key)
    {

     $categories2 .= '"'.$key['DATE_SORTIE_STOCK'].'",';
     

       $nombreplien1=$this->Model->getRequeteOne("SELECT SUM(QUANTITE) AS NUMB FROM `stock_sortie_stock` WHERE DATE_SORTIE_STOCK LIKE '".$key['DATE_SORTIE_STOCK']."' AND ID_TAILLE_BOUTEILLE = 1 AND ID_TYPE_BOUTEILLE = 2 ");
       if ($nombreplien1['NUMB'] == NULL) {
         $data_nombreplien1 .= '0,';
       }
       else{
         $data_nombreplien1 .= $nombreplien1['NUMB'].',';
       }
       

       $nombreplien2=$this->Model->getRequeteOne("SELECT SUM(QUANTITE) AS NUMB FROM `stock_sortie_stock` WHERE DATE_SORTIE_STOCK LIKE '".$key['DATE_SORTIE_STOCK']."' AND ID_TAILLE_BOUTEILLE = 2 AND ID_TYPE_BOUTEILLE = 2 ");
       
       if ($nombreplien2['NUMB'] == NULL) {
         $data_nombreplien2 .= '0,';
       }
       else{
         $data_nombreplien2 .= $nombreplien2['NUMB'].',';
       }

       

 }

 $data['categories2'] = $categories2."]"; 
 $data['data_nombreplien1'] = $data_nombreplien1."]"; 
 $data['data_nombreplien2'] = $data_nombreplien2."]"; 
//  $data['data_nombrevide3'] = $data_nombrevide3."]"; 
//  $data['data_nombrevide4'] = $data_nombrevide4."]"; 


///// rapport 3

            
$requv3 = $this->Model->getRequete('SELECT syst_taille_bouteille.DESCRIPTION, stock_stock.QUANTITE FROM `syst_taille_bouteille` JOIN stock_stock ON stock_stock.ID_TAILLE_BOUTEILLE = syst_taille_bouteille.ID_TAILLE_BOUTEILLE WHERE stock_stock.ID_TYPE_BOUTEILLE = 2');
$datav3 = '[';
foreach ($requv3 as $key) {
  
    $datav3 .= "{ name: '".$key['DESCRIPTION']."',
    y: ".$key['QUANTITE'].",
},";
}

$data['datav3'] = $datav3.']';
            


            

         /////////////  Rapport 5 //////

         
         $requet5 = $this->Model->getRequete('SELECT * FROM `stock_motif_retour`');

         $datar5 = '[';
         $data_vide1 = "[";
         $data_plein1 = "[";
            foreach ($requet5 as $key) {
              
                $datar5 .= "'".$key['DESCRIPTION']."',";
                
                $nombren1=$this->Model->getRequeteOne("SELECT SUM(QUANTITE) AS QUANTITES FROM `stock_retour_stock` WHERE ID_MOTIF_RETOUR = ".$key['ID_MOTIF_RETOUR']." AND ID_TYPE_BOUTEILLE = 1");
                  if ($nombren1['QUANTITES'] == NULL) {
                    $data_vide1 .= '0,';
                  }
                  else{
                    $data_vide1 .= $nombren1['QUANTITES'].',';
                  }

                  $nombren2=$this->Model->getRequeteOne("SELECT SUM(QUANTITE) AS QUANTITES FROM `stock_retour_stock` WHERE ID_MOTIF_RETOUR = ".$key['ID_MOTIF_RETOUR']." AND ID_TYPE_BOUTEILLE = 2");
                  if ($nombren2['QUANTITES'] == NULL) {
                    $data_plein1 .= '0,';
                  }
                  else{
                    $data_plein1 .= $nombren2['QUANTITES'].',';
                  }


            }
            
            $data['datar5'] = $datar5.']';
            $data['data_vide1'] = $data_vide1."]"; 
            $data['data_plein1'] = $data_plein1."]"; 


            // requette 6
            $categories6 = '[';
            $data_prod1 = "[";
            $data_prod2 = "[";
            $data_prod3 = "[";
            $data_prod4 = "[";
            $requet6 = $this->Model->getRequete('SELECT DISTINCT(DATE_PRODUCTION) AS DATEPROD FROM `prod_production` WHERE 1');

            foreach ($requet6 as $key) {
              $categories6 .= '"'.$key['DATEPROD'].'",';

              $prod1=$this->Model->getRequeteOne("SELECT SUM(QUANTITE_PRODUIT) AS QPROD FROM `prod_production` WHERE ID_TAILLE_BOUTEILLE = 1 AND DATE_PRODUCTION LIKE '".$key['DATEPROD']."'");
                  if ($prod1['QPROD'] == NULL) {
                    $data_prod1 .= '0,';
                  }
                  else{
                    $data_prod1 .= $prod1['QPROD'].',';
                  }


              $prod2=$this->Model->getRequeteOne("SELECT SUM(QUANTITE_PRODUIT) AS QPROD FROM `prod_production` WHERE ID_TAILLE_BOUTEILLE = 2 AND DATE_PRODUCTION LIKE '".$key['DATEPROD']."'");
                  if ($prod2['QPROD'] == NULL) {
                    $data_prod2 .= '0,';
                  }
                  else{
                    $data_prod2 .= $prod2['QPROD'].',';
                  }

                  $prod3=$this->Model->getRequeteOne("SELECT SUM(QUANTITE_PRODUIT) AS QPROD FROM `prod_production` WHERE ID_TAILLE_BOUTEILLE = 3 AND DATE_PRODUCTION LIKE '".$key['DATEPROD']."'");
                  if ($prod3['QPROD'] == NULL) {
                    $data_prod3 .= '0,';
                  }
                  else{
                    $data_prod3 .= $prod3['QPROD'].',';
                  }


                  $prod4=$this->Model->getRequeteOne("SELECT SUM(QUANTITE_PRODUIT) AS QPROD FROM `prod_production` WHERE ID_TAILLE_BOUTEILLE = 4 AND DATE_PRODUCTION LIKE '".$key['DATEPROD']."'");
                  if ($prod4['QPROD'] == NULL) {
                    $data_prod4 .= '0,';
                  }
                  else{
                    $data_prod4 .= $prod4['QPROD'].',';
                  }

            }

            $data['categories6'] = $categories6."]"; 
            $data['data_prod1'] = $data_prod1."]"; 
            $data['data_prod2'] = $data_prod2."]"; 
            $data['data_prod3'] = $data_prod3."]"; 
            $data['data_prod4'] = $data_prod4."]"; 

            
    $this->load->view('Dashboard_Vente_View',$data);
}



}
?>
