<?php 
 /**
  * 
  */
 class Facture 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 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_VENTE'=>0,

  );

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

  $commune= $this->Model->getRequete("SELECT `ID_VENTE_DETAILS`,syst_taille_bouteille.DESCRIPTION,`QUANTITE_COMMANDE`,`PRIX_UNITAIRE` FROM `vente_details` JOIN syst_taille_bouteille ON syst_taille_bouteille.ID_TAILLE_BOUTEILLE = vente_details.ID_TAILLE_BOUTEILLE WHERE `NUMERO_VENTE`  LIKE '".$this->input->post('NUMERO_VENTE')."'  ");

  $datas= '<table class="table">';
  $datas.='<tr>
  <td><b>Taille</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>
  <td></td>
  </tr>';
  $totolpayabe =0;
  foreach($commune as $commun){
    $ptotal = $commun["QUANTITE_COMMANDE"]*$commun["PRIX_UNITAIRE"];
    $totolpayabe +=$ptotal;    
    $datas.= '<tr>
    <td>'.$commun["DESCRIPTION"].'</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="3"><b>Total</b></td>

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

public function addss()
{

  $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');


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

    $title = "Enregistrement Commande";


        // $data['typebout']=$this->Model->getList('syst_type_bouteille',array());
    $data['typeclient']=$this->Model->getList('client_client',array());
    $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);
    $data['NUMERO_VENTE'] = 'V-'.$formatted1;
    
        //  }
        //  else{
        //   $datas['NUM_ASSURANCE'] = $nrequette['NEW_NUMBER'];
        //   // $PLAFOND =;
        //  }

    

    $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

    $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'));
  }
}





public function index()
{

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

}

public function listing()
{

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

}

public function paye()
{

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

}


public function add()
{
  $MONTANT_PAIEMENT = $this->input->post('MONTANT_PAIEMENT');
  $DATE_PAIEMENT = $this->input->post('DATE_PAIEMENT');
  $ID_VENTE = $this->input->post('ID_VENTE');
  echo $ID_VENTE;
  echo '<br>';
  echo $MONTANT_PAIEMENT;
  echo '<br>';
  echo $DATE_PAIEMENT;

  $vent = $this->Model->getOne('vente_vente',array('ID_VENTE'=>$ID_VENTE));
  $TOTALPAYE = $MONTANT_PAIEMENT + $vent['MONTANT_PAYE'];
      // $MONTANT_RESTANT = $vent['MONTANT_TOTAL'] - $vent['MONTANT_PAYE'];
  if ($TOTALPAYE == $vent['MONTANT_TOTAL']) {
    echo 'paiement termine<br>';
    $ID_STATUS_PAIEMENT = 3;
  }
  else{
    echo 'paiement non termine<br>';
    $ID_STATUS_PAIEMENT = 2;
  }


  
  $updatevdet=$this->Model->update('vente_vente',array('ID_VENTE'=> $ID_VENTE), array('MONTANT_PAYE'=>$TOTALPAYE,'ID_STATUS_PAIEMENT'=>$ID_STATUS_PAIEMENT));

  $datas = array(
    'ID_VENTE'=>$ID_VENTE,
    'DATE_PAIEMENT'=>$DATE_PAIEMENT,
    'MONTANT_PAIEMENT'=>$MONTANT_PAIEMENT,
    'ID_STATUS_PAIEMENT'=>$ID_STATUS_PAIEMENT,
  );

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




  $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('facture/Facture'));

}

  

}                        