<?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 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_PAIEMENT'];
      // $MONTANT_RESTANT = $vent['MONTANT_TOTAL'] - $vent['MONTANT_PAYE'];
  if ($TOTALPAYE == $vent['MONTANT_PAYE']) {
  $orga = $this->Model->checkvalue('vente_details', [
    'ID_VENTE' => $vent['ID_VENTE'],
    'ID_STATUS' => 1,
    'QUANTITE_LIVRE' => NULL
  ]);
   if(!$orga){
    echo 'paiement termine<br>';
    $ID_STATUS_PAIEMENT = 3;
  }else{
   echo 'paiement termine<br>';
   $ID_STATUS_PAIEMENT = 2;
 }
  }
  else{
    echo 'paiement non termine<br>';
    $ID_STATUS_PAIEMENT = 2;
  }


  
  $updatevdet=$this->Model->update('vente_vente',array('ID_VENTE'=> $ID_VENTE), array('MONTANT_PAIEMENT'=>$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'));

}



}                        