<?php 
 /**
  * 
  */
 class Commande extends CI_Controller
 {

  function __construct()
  {
    parent::__construct();
    $this->load->library('Mylibrary');
    $this->ci = & get_instance();
    $this->ci->load->library("user_agent");
    // $this->Is_Connected();
    // $this->verification();

  }

  public function Is_Connected()
  {

   if (empty($this->session->userdata('SOCAR_ID_USER')))
   {
     redirect(base_url('Login/'));
   }
 }




 public function index()
 {
  $title = "Enregistrement Commande";
  $data['typeclient']=$this->Model->getRequete('SELECT ID_CLIENT,NOM,PRENOM,client_type.DESCRIPTION FROM client_client JOIN client_type ON client_type.ID_CLIENT_TYPE=client_client.ID_CLIENT_TYPE WHERE 1');
  $data['tailbout']=$this->Model->getList('syst_taille_bouteille',array());
  $data['title'] = $title;
 $data['ventebout']=$this->Model->getList('sys_bouteille_vente',array());
 $data['lot']=$this->Model->getList('prod_lot',array('ID_STATUS'=>1));

//  print_r($data['lot']);exit();
  $crequette= $this->Model->getRequeteOne("SELECT COUNT(ID_VENTE) AS NOMBRE FROM `vente_vente`");
  $number1 = $crequette['NOMBRE'] + 1;
  $formatted1 = str_pad($number1, 4, '0', STR_PAD_LEFT);
  
  // Get the current month and year
$currentMonth = date('m'); // Format: MM
$currentYear = date('y'); // Format: YY
  $data['NUMERO_VENTE'] = 'V-'.$currentMonth .'-'. $currentYear. '-'.$formatted1;

  $this->load->view("Commande_Add_View",$data);
}

public function add_tocart()
{

  $datas=array(
    'NUMERO_VENTE'=>$this->input->post('NUMERO_VENTE'),
    'PRIX_UNITAIRE'=>$this->input->post('PRIX_UNITAIRE'),
    'QUANTITE_COMMANDE'=>$this->input->post('QUANTITE_COMMANDE'),
    'ID_TAILLE_BOUTEILLE'=>$this->input->post('ID_TAILLE_BOUTEILLE'),
    'ID_LOT'=>$this->input->post('ID_LOT'),
    'VENTE_BOUTEILLE_ID'=>$this->input->post('VENTE_BOUTEILLE_ID'),
    'ID_VENTE'=>0,

  );

  $this->Model->insert_last_id('vente_details',$datas);

  $commune= $this->Model->getRequete("SELECT `ID_VENTE_DETAILS`,syst_taille_bouteille.DESCRIPTION AS TAILLE, QUANTITE_COMMANDE, PRIX_UNITAIRE, NUMERO_VENTE, vente_details.ID_STATUS,sys_bouteille_vente.DESCRIPTION AS BOUTEILLE,prod_lot.LOT_PRODUCTION AS LOT FROM vente_details LEFT JOIN sys_bouteille_vente ON sys_bouteille_vente.VENTE_BOUTEILLE_ID=vente_details.VENTE_BOUTEILLE_ID LEFT JOIN syst_taille_bouteille ON syst_taille_bouteille.ID_TAILLE_BOUTEILLE=vente_details.ID_TAILLE_BOUTEILLE LEFT JOIN prod_lot ON prod_lot.ID_LOT=vente_details.ID_LOT WHERE `NUMERO_VENTE`  LIKE '".$this->input->post('NUMERO_VENTE')."'  ");

  $datas= '<table class="table">';
  $datas.='<tr>
  <td class="text-right"><b>Taille</b></td>
  <td class="text-right"><b>Lot</b></td>
   <td class="text-right"><b>Bouteille</b></td>
  <td class="text-right"><b>Prix Unitaire</b></td>
  <td class="text-right"><b>Quantite</b></td>
  <td class="text-right"><b>Total</b></td>

  </tr>';
  $totolpayabe =0;
  foreach($commune as $commun){
    $ptotal = $commun["QUANTITE_COMMANDE"]*$commun["PRIX_UNITAIRE"];
    $totolpayabe +=$ptotal; 
    $bout =!empty($commun["BOUTEILLE"])   ? $commun["BOUTEILLE"] : "N/A";
    $datas.= '<tr>
    <td class="text-right">'.$commun["TAILLE"].'</td>
    <td class="text-right">'.$commun["LOT"].'</td>
    <td class="text-right">'.$bout.'</td>
    <td class="text-right">'.$commun["PRIX_UNITAIRE"].'</td>
    <td class="text-right">'.$commun["QUANTITE_COMMANDE"].'</td>
    <td class="text-right">'.$ptotal.'</td>
    <td><a class="btn btn-danger btn-xs" onclick="remove_cart('.$commun['ID_VENTE_DETAILS'].')" href="#" role="button">Enlever</a>
    </td>
    </tr>';

  }
  $datas.='<tr>
  <td colspan="5"><b>Total</b></td>

  <td class="text-right"><b>'.$totolpayabe.'</b></td>
  <td></td>
  </tr>';
  $datas.= '</table>';
  echo $datas;
}

function get_lot()
{
  $ID_TAILLE_BOUTEILLE = $this->input->post('ID_TAILLE_BOUTEILLE');

  $query = $this->Model->getRequete("SELECT prod_production.ID_LOT,syst_taille_bouteille.DESCRIPTION, prod_lot.LOT_PRODUCTION,SUM(QUANTITE_PRODUIT) FROM prod_production JOIN prod_lot ON prod_lot.ID_LOT=prod_production.ID_LOT JOIN syst_taille_bouteille ON syst_taille_bouteille.ID_TAILLE_BOUTEILLE=prod_production.ID_TAILLE_BOUTEILLE WHERE prod_production.ID_TAILLE_BOUTEILLE=".$ID_TAILLE_BOUTEILLE." GROUP BY prod_production.ID_LOT ");
  // print_r($query);exit();
  $select = "<option value=''>-</option>";
  foreach ($query as $key) {
      # code...
    $select .= "<option value='" . $key['ID_LOT'] . "'>" . $key['LOT_PRODUCTION'] . "</option>";
  }

  echo $select;
}



function get_prix()
{
  $ID_CLIENT = $this->input->post('ID_CLIENT');
  $ID_TAILLE_BOUTEILLE = $this->input->post('ID_TAILLE_BOUTEILLE');

  $prix = $this->Model->getRequeteOne("SELECT standard_prix_status.MONTANT FROM client_client JOIN standard_status ON standard_status.ID_STANDARD=client_client.ID_STANDARD JOIN standard_prix_status ON standard_prix_status.ID_STANDARD=standard_status.ID_STANDARD JOIN syst_taille_bouteille ON syst_taille_bouteille.ID_TAILLE_BOUTEILLE=standard_prix_status.ID_TAILLE_BOUTEILLE WHERE client_client.ID_CLIENT=".$ID_CLIENT." AND standard_prix_status.ID_TAILLE_BOUTEILLE=".$ID_TAILLE_BOUTEILLE);

  $montant=$prix['MONTANT'];

  echo $montant;
}

public function remove_cart()
{
  
  $ID_VENTE_DETAILS=$this->input->post('ID_VENTE_DETAILS');;
  $pproduct = $this->Model->delete('vente_details',array('ID_VENTE_DETAILS'=>$ID_VENTE_DETAILS));

  redirect(base_url('donnee/Client/listing'));
  
}
public function add()
{

  $NUMERO_VENTE=$this->input->post('NUMERO_VENTE');
  $ID_CLIENT=$this->input->post('ID_CLIENT');
  $ID_VENTE_STATUT=1;
  $DATE_COMMANDE=$this->input->post('DATE_COMMANDE');


  $dataclient = array(          
    'NUMERO_VENTE'=>$NUMERO_VENTE,  
    'ID_CLIENT'=>$ID_CLIENT,  
    'ID_VENTE_STATUT'=>$ID_VENTE_STATUT,  
    'DATE_COMMANDE'=>$DATE_COMMANDE,
  );
  $this->form_validation->set_rules('ID_CLIENT', 'Client', 'required');
  $this->form_validation->set_rules('DATE_COMMANDE', 'Date', 'required');


  if ($this->form_validation->run() == FALSE){        

    $title = "Enregistrement Commande";

    $data['typeclient']=$this->Model->getRequete('SELECT ID_CLIENT,NOM,PRENOM,client_type.DESCRIPTION FROM client_client JOIN client_type ON client_type.ID_CLIENT_TYPE=client_client.ID_CLIENT_TYPE WHERE 1');
    $data['tailbout']=$this->Model->getList('syst_taille_bouteille',array());
    $data['title'] = $title;

        // if ($nrequette['RANGE_FROM'] == NULL) {
    $crequette= $this->Model->getRequeteOne("SELECT COUNT(ID_VENTE) AS NOMBRE FROM `vente_vente`");
    $number1 = $crequette['NOMBRE'] + 1;
    $formatted1 = str_pad($number1, 4, '0', STR_PAD_LEFT);
   $currentMonth = date('m'); // Format: MM
$currentYear = date('y'); // Format: YY
  $data['NUMERO_VENTE'] = 'V-'.$currentMonth .'-'. $currentYear. '-'.$formatted1;
    


    $message = "<div class='alert alert-danger' id='message'>
    Donne manquantes, Processus échoué.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";


    $this->session->set_flashdata(array('message'=>$message));
    $this->load->view("Commande_Add_View",$data);

  }else{
        // prod_production
   $commande=$this->Model->getOne('vente_details',array('NUMERO_VENTE'=>$NUMERO_VENTE));
    if(!empty($commande)){

    $ID_VENTE = $this->Model->insert_last_id('vente_vente',$dataclient);
    $this->Model->update('vente_details',array('NUMERO_VENTE'=>$NUMERO_VENTE),array('ID_VENTE'=>$ID_VENTE));
    $gettot= $this->Model->getRequeteOne("SELECT SUM((QUANTITE_COMMANDE*PRIX_UNITAIRE)) AS TOTAL FROM `vente_details` WHERE `ID_VENTE` = ".$ID_VENTE."");
    $this->Model->update('vente_vente',array('ID_VENTE'=>$ID_VENTE),array('MONTANT_TOTAL'=>$gettot['TOTAL']));

    $message = "<div class='alert alert-success'  id='message'>
    Enregistrement fait avec succès.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";       
    $this->session->set_flashdata(array('message'=>$message));
    redirect(base_url('vente/Commande'));
  }else{
       $title = "Enregistrement Commande";

    $data['typeclient']=$this->Model->getRequete('SELECT ID_CLIENT,NOM,PRENOM,client_type.DESCRIPTION FROM client_client JOIN client_type ON client_type.ID_CLIENT_TYPE=client_client.ID_CLIENT_TYPE WHERE 1');
    $data['tailbout']=$this->Model->getList('syst_taille_bouteille',array());
    $data['title'] = $title;

        // if ($nrequette['RANGE_FROM'] == NULL) {
    $crequette= $this->Model->getRequeteOne("SELECT COUNT(ID_VENTE) AS NOMBRE FROM `vente_vente`");
    $number1 = $crequette['NOMBRE'] + 1;
    $formatted1 = str_pad($number1, 4, '0', STR_PAD_LEFT);
  $currentMonth = date('m'); // Format: MM
$currentYear = date('y'); // Format: YY
  $data['NUMERO_VENTE'] = 'V-'.$currentMonth .'-'. $currentYear. '-'.$formatted1;
    
      $message = "<div class='alert alert-danger' id='message'>
    Pas de commandes, Processus échoué.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";
    
    $this->session->set_flashdata(array('message'=>$message));
    $this->load->view("Commande_Add_View",$data);

  }
  }
}



public function listing()
{

  $data['title'] = "Commande";
  $this->load->view('Commande_List_View',$data);

}



}                        