<?php

// ini_set('display_errors', 1);

define("ADMIN", true);
require_once("../common/lib.php");
require_once("../common/define.php");
define("TITLE_ELEMENT", $texts['DASHBOARD']);

if(!isset($_SESSION['user'])){
    header("Location: login.php");
    exit();
}elseif($_SESSION['user']['type'] == "registered"){
    unset($_SESSION['user']);
    $_SESSION['msg_error'][] = "Access denied.";
    header("Location: login.php");
    exit();
}

require_once("includes/fn_module.php"); ?>
<!DOCTYPE html>
<head>
    <?php include("includes/inc_header_common.php"); ?>
	<?php include("includes/functions.php"); ?>
	<script src="https://code.highcharts.com/highcharts.js"></script>
</head>
<body>
    <div id="wrapper">
        <?php include("includes/inc_top.php"); ?>
        <div id="page-wrapper">
            <div class="page-header">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-6">
                            <h1><i class="fa fa-dashboard"></i> <?php echo $texts['DASHBOARD']; ?></h1>
                        </div>
						<div class="col-md-6" style="padding-bottom: 20px;text-align: right;">
						<button type="submit" class="btn btn-default mt15" id="synchroniser" data-toggle="tooltip" data-placement="bottom" title="Synchroniser" style="background-color:#0bb300;">
							<i class="fa fa-refresh"></i><span class="hidden-sm hidden-xs"> Synchroniser</span>
							
						</button>
						<br><span style="font-size:12px;">Dernière synchronisation le 
						<?
						$queryo = "SELECT * from synchro where id = 1 ";
						$resulto = @$db->query($queryo);
						if($resulto !== false){
							foreach($resulto as $rowo){
								echo date('d-m-Y H:i:s',$rowo["date_synchro"]);
							}
						}	
						?>

						</span>
                        </div>
                    </div>
                </div>
            </div>
			<div id="synchro-msg" style="color:#2F9DF2;text-align:center;display:none;" >
<h2  >Synchronisation en cours ...</h2>
<img src="/admin/images/loding.gif" width="120"   />
</div>	
            <div class="container-fluid">
                <div class="alert-container">
                    <div class="alert alert-success alert-dismissable"></div>
                    <div class="alert alert-warning alert-dismissable"></div>
                    <div class="alert alert-danger alert-dismissable"></div>
                </div>
				
                <div class="row">
					<div class="col-lg-6">
						<table border="1" width="100%" class="table table-bordered table-hover table-striped">
							<thead>
							<tr>
								<td colspan="7" align="center" style="background: #2F9DF2;color:#FFF;"><b>OUT non payé</b></td>
							</tr>
							
							<tr>
								<th><b>Date</b></th>
								<th><b>Journal</b></th>
								<th><b>Nature</b></th>
								<th><b>Référence</b></th>
								<th><b>Fournisseur</b></th>
								
								<th><b>__Montant__</b></th>
								<th></th>
							</tr>
							</thead>
							<?
							$count_out = 0;
							$date_j = date('d-m-Y', strtotime(date("d-m-Y"). ' + 1 days'));
							$date_jour = strtotime($date_j);

							// echo $date_jour ;
							$query_out = "select * from pm_transaction where type = 'out' AND  journal <> 1 AND date_echeance <".$date_jour." AND (status_paiement IS NULL OR status_paiement <> 1) ORDER BY date_echeance ASC";
							// echo $query_out;
							$result = @$db->query($query_out);
                            if($result !== false){
								foreach($result as $row){	
									$count_out++;			
									// =========== journal ===========
										$query_j = "select * from pm_journal where id =".$row["journal"];
										$result_j = @$db->query($query_j);
										if($result_j !== false){
											foreach($result_j as $row_j){
											$journal = $row_j["name"];
												}
										}
									// =========== mode_reglement ===========
										$query_cam = "select * from pm_mode_paiement where id =".$row["mode_reglement"];
										$result_cam = @$db->query($query_cam);
										if($result_cam !== false){
											foreach($result_cam as $row_cam){
												$mode_reglement = $row_cam["name"];
											}
										}											
									// ============ Fournisseur ============
										$name = "";
										$query_fr = "SELECT name FROM pm_fournisseurs WHERE id = ".$row["fournisseur"];
										$result_fr = $db->query($query_fr);
										if($result_fr !== false && $db->last_row_count() > 0){
											$row_fr = $result_fr->fetch();																
											$name = $row_fr['name'];
										}

									$payer_plus_tard = '';
									if($row["payer_plus_tard"] == 1){
										$payer_plus_tard = "style='background-color:#ffb9b9;'" ;
									}										
?>
								<tr id="row_<? echo $row["id"]; ?>">
									<td  <? echo $payer_plus_tard; ?>><span id="vider_<? echo $row["id"]; ?>"><? echo date('d-m-Y',$row["date_echeance"]); ?></span><input type='text' style="display:none;" class='form-control datepickerss' id="date_<? echo $row["id"]; ?>" value='<? echo date("d-m-Y"); ?>'></td>
									
									<td  <? echo $payer_plus_tard; ?>><? echo $journal; ?></td>
									<td  <? echo $payer_plus_tard; ?>><? echo $mode_reglement; ?></td>
									<td  <? echo $payer_plus_tard; ?>><? echo $row['ref']; ?></td>
									<td  <? echo $payer_plus_tard; ?>><? echo $name; ?></td>
									<td  <? echo $payer_plus_tard; ?> align="right"><a href="/admin/modules/transaction/index.php?view=form&id=<? echo $row["id"]; ?>" target="_blank"><? echo number_format($row['montant'], 3, '.', ' '); ?></a></td>
									<td  <? echo $payer_plus_tard; ?> align="center">
										<button class="btn btn-default payer" type="button" id="payer_<? echo $row["id"]; ?>"><i class="fa fa-key"></i> Payer</button>
									</td>
								</tr>									
							<?								
								}
							}	
							?>
							<?
							//================= Engagements financiers mensuels =========
							$date_j = date("j");
							$date_transa_plus = date('d-m-Y', strtotime(date("d-m-Y"). ' + 1 days'));
							$date_transa_plus = strtotime($date_transa_plus);
							$date_transa_moin = date('d-m-Y', strtotime(date("d-m-Y"). ' - 28 days'));
							$date_transa_moin = strtotime($date_transa_moin);
							$date_eng = date('d-m-Y', strtotime(date("d-m-Y"). ' - 10 days'));
							$date_en = date('j', strtotime($date_eng));
							// echo $date_jour ;
							$query_out = "select * from pm_engagement where jours >=".$date_en." AND jours <= ".$date_j;
							// echo $query_out;
							$result = @$db->query($query_out);
                            if($result !== false){
								foreach($result as $row){
									$count_out++;
									$id_enga = "";
									$query_tran = "select * from pm_transaction where type = 'out' AND libelle = '".$row["name"]."' AND date_trans >= ".$date_transa_moin." AND date_trans < ".$date_transa_plus  ;
									// echo $query_tran;
									$result_tran = @$db->query($query_tran);
									if($result_tran !== false){
										foreach($result_tran as $row_tran){
											$id_enga = $row_tran["id"];
										}
									}	
									if($id_enga == ''){			
									// =========== journal ===========
										$query_j = "select * from pm_journal where id =".$row["journal"];
										$result_j = @$db->query($query_j);
										if($result_j !== false){
											foreach($result_j as $row_j){
											$journal = $row_j["name"];
												}
										}
										
?>
								<input type="hidden" id="journal_<? echo $row["id"]; ?>" value="<? echo $row["journal"]; ?>" />
								<input type="hidden" id="compte_<? echo $row["id"]; ?>" value="<? echo $row["compte"]; ?>" />
								<input type="hidden" id="montant_<? echo $row["id"]; ?>" value="<? echo $row["montant"]; ?>" />
								<input type="hidden" id="libelle_<? echo $row["id"]; ?>" value="<? echo $row["name"]; ?>" />
								<tr id="row_<? echo $row["id"]; ?>">
									<td><? echo $journal; ?></td>
									<td align="center">***</td>
									<td><? echo $row["name"]; ?></td>
									<td align="center">***</td>
									<td align="right"><? echo number_format($row['montant'], 3, '.', ' '); ?></td>
									<td align="center">
										<button class="btn btn-default payer_engagement" type="button" id="payer_<? echo $row["id"]; ?>"><i class="fa fa-key"></i> Payer</button>
									</td>
								</tr>									
							<?	
									}
								}
							}	
								if($count_out == 0){
									echo "<tr><td colspan='6' align='center'>Pas de transaction !</td></tr>";
									
								}							
							?>
						</table>
					</div>
<div class="col-lg-6">
						<table border="1" width="100%" class="table table-bordered table-hover table-striped">
						<thead>
							<tr>
								<td colspan="6" align="center" style="background: #2F9DF2;color:#FFF;"><b>IN non payé</b></td>
							</tr>
							<tr>
								<th><b>Date</b></th>
								<th><b>Journal</b></th>
								<th><b>Nature</b></th>
								<th><b>Référence</b></th>
								<th><b>__Montant__</b></th>
								<th></th>
							</tr>
						</thead>	
							<?
							$count_in = 0;
							$date_j = date('d-m-Y', strtotime(date("d-m-Y"). ' + 1 days'));
							$date_jour = strtotime($date_j);

							// echo $date_jour ;
							$query_out = "select * from pm_transaction where type = 'IN' AND  journal <> 1 AND date_echeance <".$date_jour." AND (status_paiement IS NULL OR status_paiement <> 1) ORDER BY date_echeance ASC";
							// echo $query_out;
							$result = @$db->query($query_out);
                            if($result !== false){
								foreach($result as $row){	
									$count_in ++;			
									// =========== journal ===========
										$query_j = "select * from pm_journal where id =".$row["journal"];
										$result_j = @$db->query($query_j);
										if($result_j !== false){
											foreach($result_j as $row_j){
											$journal = $row_j["name"];
												}
										}
									// =========== mode_reglement ===========
										$query_cam = "select * from pm_mode_paiement where id =".$row["mode_reglement"];
										$result_cam = @$db->query($query_cam);
										if($result_cam !== false){
											foreach($result_cam as $row_cam){
												$mode_reglement = $row_cam["name"];
											}
										}
									
									$payer_plus_tard = '';
									if($row["payer_plus_tard"] == 1){
										$payer_plus_tard = "style='background-color:#ffb9b9;'" ;
									}
										
?>
								<tr id="row_<? echo $row["id"]; ?>">
								<td <? echo $payer_plus_tard; ?>><span id="vider_<? echo $row["id"]; ?>"><? echo date('d-m-Y',$row["date_echeance"]); ?></span><input type='text' style="display:none;" class='form-control datepickerss' id="date_<? echo $row["id"]; ?>" value='<? echo date("d-m-Y"); ?>'></td>
									<td <? echo $payer_plus_tard; ?>><? echo $journal; ?></td>
									<td <? echo $payer_plus_tard; ?>><? echo $mode_reglement; ?></td>
									<td <? echo $payer_plus_tard; ?>><? echo $row['ref']; ?></td>									
									<td  <? echo $payer_plus_tard; ?> align="right"><a href="/admin/modules/transaction/index.php?view=form&id=<? echo $row["id"]; ?>" target="_blank"><? echo number_format($row['montant'], 3, '.', ' '); ?></a></td>
									<td <? echo $payer_plus_tard; ?> align="center">
										<button class="btn btn-default payer" type="button" id="payer_<? echo $row["id"]; ?>"><i class="fa fa-key"></i> Payer</button>
									</td>
								</tr>									
							<?								
								}
								if($count_in == 0){
									echo "<tr><td colspan='6' align='center'>Pas de transaction !</td></tr>";
									
								}
							}	
							?>
						</table>
					</div>

</div>

<div class="row"> 
 
  <div class="col-lg-6">
  
						<table border="1" width="100%" class="table table-bordered table-hover table-striped">
							<thead>
							<tr>
								<td colspan="6" align="center" style="background: #2F9DF2;color:#FFF;"><b>Solde actuelle</b></td>
							</tr>
							
							<tr>
								<th><b>Journal</b></th>
								<th><b>Solde</b></th>
							</tr>
							</thead>
							
							<tbody>


		

		

<?		

		$count_j = 0;



	

	$query_jour = "select * FROM  pm_journal"  ;

	if($_GET["journal"] <> ""){

		$query_jour .= " WHERE id = '".$_GET["journal"]."' ORDER BY id ASC";

	}

	$result_jour = @$db->query($query_jour);

	if($result_jour !== false){

		foreach($result_jour as $row_jour){

			$total_moin = 0 ;

			$total_plus = 0;

			$total = 0;

			

			$total_moin_all = 0 ;

			$total_plus_all = 0;

			$total_all = 0;

			

			$total_moin_alla = 0 ;

			$total_plus_alla = 0;

			$total_alla = 0;

			

			$total_moina = 0 ;

			$total_plusa = 0;

			$totala = 0;



		

			$get_journal = $row_jour["id"];

			$get_journal_name = $row_jour["name"];



?>




										<?php

										$_GET["date"] = date("d-m-Y");

										if($db !== false){

											$date_paiement = strtotime($_GET["date"]) ;

											$date_paiementa_moin = date('Y-m-d', strtotime($_GET["date"]. ' - 1 days'));

											$date_paiementa_moin = strtotime($date_paiementa_moin);

											$date_paiementa_moin2 = date('Y-m-d', strtotime($_GET["date"]. ' - 2 days'));

											$date_paiementa_moin2 = strtotime($date_paiementa_moin2);

											$date_paiementa = date('Y-m-d', strtotime($_GET["date"]. ' + 1 days'));

											$date_paiementa = strtotime($date_paiementa);

											

											$query_all = "select * from pm_transaction where date_paiement < ".$date_paiement;

											$query_all .= " AND journal = '".$get_journal."' ";

											if($get_journal <> 1){	

												$query_all .= " AND status_paiement = 1";

											}

							

											$result_all = @$db->query($query_all);

											if($result_all !== false){

												foreach($result_all as $row_all){

													if($row_all["type"] == "out"){

														$total_moin_all += $row_all["montant"] ;

													}else{

														$total_plus_all += $row_all["montant"] ;

													}

													

													

												}												

											}	

											$total_all = $total_plus_all - $total_moin_all ;

											$query = "select * from pm_transaction where date_paiement >= ".$date_paiement." AND date_paiement < ".$date_paiementa  ;

											$query .= " AND journal = '".$get_journal."' ";

											if($get_journal <> 1){	

												$query .= " AND status_paiement = 1";

											}

											// echo $query."<br>";

											$count_ligne = 0;

											$result = @$db->query($query);

											if($result !== false){

												foreach($result as $row){

												$count_ligne++;	

												// echo $count_ligne."<br>";

												// =========== journal ===========

												$query_j = "select * from pm_journal where id =".$row["journal"];

												$result_j = @$db->query($query_j);

												if($result_j !== false){

													foreach($result_j as $row_j){

														$journal = $row_j["name"];

													}

												}	

												// =========== Compte ===========

												$query_c = "select * from pm_compte_financier where id =".$row["compte"];

												$result_c = @$db->query($query_c);

												if($result_c !== false){

													foreach($result_c as $row_c){

														$compte = $row_c["name"];

													}

												}	

												// =========== classe Compte ===========

												$query_ca = "select * from pm_classe_compte where id =".$row_c["classe"];

												$result_ca = @$db->query($query_ca);

												if($result_ca !== false){

													foreach($result_ca as $row_ca){

														$classe = $row_ca["name"];

													}

												}	

												// =========== mode_reglement ===========

												$query_cam = "select * from pm_mode_paiement where id =".$row["mode_reglement"];

												$result_cam = @$db->query($query_cam);

												if($result_cam !== false){

													foreach($result_cam as $row_cam){

														$mode_reglement = $row_cam["name"];

													}

												}	

												// ============ Fournisseur ============

												$name = "";

												$query_fr = "SELECT name FROM pm_fournisseurs WHERE id = ".$row["fournisseur"];

															$result_fr = $db->query($query_fr);

															if($result_fr !== false && $db->last_row_count() > 0){

																$row_fr = $result_fr->fetch();																

																$name = $row_fr['name'];

															}	

												// ============ Ref facture achat============

												$ref_facture_achat = "";

												$query_fr = "SELECT ref_bl FROM pm_facture_achat WHERE id = ".$row["ref_facture_achat"];

															$result_fr = $db->query($query_fr);

															if($result_fr !== false && $db->last_row_count() > 0){

																$row_fr = $result_fr->fetch();																

																$ref_facture_achat = $row_fr['ref_bl'];

															}	

												// ============ Ref facture vente ============

												$ref_facture_vente = "";

												$query_fr = "SELECT idd FROM pm_facture WHERE id = ".$row["ref_facture_vente"];

															$result_fr = $db->query($query_fr);

															if($result_fr !== false && $db->last_row_count() > 0){

																$row_fr = $result_fr->fetch();																

																$ref_facture_vente = $row_fr['idd'];

															}	

															

												// ================ Client ============

												$client = '';

												$query_cl = "SELECT name FROM pm_client WHERE id = ".$row["client"];

															// echo $query_fr;

															$result_cl = $db->query($query_cl);

															if($result_cl !== false && $db->last_row_count() > 0){

																$row_cl = $result_cl->fetch();																

																$client = ' / '.$row_cl['name'];

															}												

												// =========== Montant ===========

												if($row["type"] == "out"){

													$montant = "<b style='color:red;'>".number_format($row["montant"] *(-1),3, '.',' ')."</b>";

													$total_moin += $row["montant"] ;

												}else{

													$montant = "<b style='color:green;'>".number_format($row["montant"],3, '.',' ')."</b>";

													$total_plus += $row["montant"] ;

												}	

												$total = $total_plus - $total_moin;	

												

													

												// }

												// $count_j++;

												// =============================================

												// if($ref_facture_vente){

													// $ref_facture_vente = "- FAC1800".$row["ref_facture_vente"];

												// }else{

													// $ref_facture_vente = "";

												// }

										?>

					

										<?	

												}

											}

										}

										if($count_ligne == 0){

										?>

																

										<?

										}

										// =========== Solde du dernier jour ===========

												$total_moin_j = 0 ;

												$total_plus_j = 0;

												$total_j = 0;

												

													$query_allj = "select * from pm_transaction where date_paiement >= ".$date_paiementa_moin." AND date_paiement < ".$date_paiement  ;

													// if($_GET["journal"] <> ""){

														$query_allj .= " AND journal = '".$get_journal."' ";

														$query_allj .= " AND (status_paiement = 1 OR  mode_reglement = 1) ";

														// $query_allj .= " OR mode_reglement = 1";

													// }

													// echo $query_allj."<br>";

													$result_allj = @$db->query($query_allj);

													if($result_allj !== false){

														foreach($result_allj as $row_allj){

															

															if($row_allj["type"] == "out"){

																$montant_j = "<b style='color:red;'>".number_format($row_allj["montant"] *(-1),3, '.',' ')."</b>";

																$total_moin_j += $row_allj["montant"] ;

															}else{

																$montant_j = "<b style='color:green;'>".number_format($row_allj["montant"],3, '.',' ')."</b>";

																$total_plus_j += $row_allj["montant"] ;

															}	

															$total_j = $total_plus_j - $total_moin_j;

															

														}

													}

										// ==============================================================

											$query_alla = "select * from pm_transaction where date_paiement <= ".$date_paiementa_moin;

											$query_alla .= " AND journal = '".$get_journal."' ";

											$query_alla .= " AND (status_paiement = 1 OR  mode_reglement = 1) ";

											// if($get_journal <> 1){	

												// $query_alla .= " AND status_paiement = 1";

											// }

											$result_alla = @$db->query($query_alla);

											if($result_alla !== false){

												foreach($result_alla as $row_alla){

													if($row_alla["type"] == "out"){

														$total_moin_alla += $row_alla["montant"] ;

													}else{

														$total_plus_alla += $row_alla["montant"] ;

													}

													

													

												}												

											}	

											$total_alla = $total_plus_alla - $total_moin_alla ;	

																					

										// ==============================================================	

											// echo 	">>>===".$total_alla."<br>";	

										// ==============================================================						

										?>

										
									<?
										
										$link_journal = "/admin/rapport/rapport.php?date=".date("d-m-Y")."&journal=".$row_jour["id"];
									?>
                          

								<tr>
												<td><b><? echo $row_jour["name"]; ?></b></td>
												<td align="right"><a href="<? echo $link_journal; ?>" target="_blank"><? echo number_format($total_all+$total,3, '.',' '); ?></a></td>
											</tr>


							<?

							

									}

								}
							?>	
								
							</tbody>

						</table>	
						
						
</div>


  
  <div class="col-lg-6">
  
						<table border="1" width="100%" class="table table-bordered table-hover table-striped">
							<thead>
							<tr>
								<td colspan="7" align="center" style="background: #2F9DF2;color:#FFF;"><b>B. CMD ACHAT</b></td>
							</tr>
							
							<tr>
								<th><b>Date de livraison</b></th>
								<th><b>ID</b></th>
								<th><b>Fournisseur</b></th>
								<th><b>Status</b></th>
								<th></th>
							</tr>
							</thead>
							
							<tbody>
							
<?

	$query_bc = "select * FROM  pm_bc where valider <> 1 AND date_livraison < ".$date_jour." ORDER BY date_livraison ASC"   ;
	// $query_bc = "select * FROM  pm_bc where date_livraison < ".$date_jour." ORDER BY date_livraison DESC"  ;
	$result_bc = @$db->query($query_bc);
// echo $query_bc;
	if($result_bc !== false){

		foreach($result_bc as $row_bc){
			$date_liv = date("d-m-Y", $row_bc["date_livraison"]) ;
			$id_liv  = $row_bc["id"] ;
			$ref_liv  = 'BC_'.$row_bc["id"] ;
			$fournisseur  = fournisseur($row_bc["fournisseur"]) ;
?>
							
							<tr>
								<td><? echo $date_liv; ?></td>
								<td><? echo $ref_liv; ?></td>
								<td><? echo $fournisseur; ?></td>
								<td><? echo get_status_bc($id_liv); ?></td>
								<td>
								<a class="tips get_bc" href="#" name="<? echo $id_liv; ?>" title="Voir état CMD"><i class="fa fa-eye text-success"></i></a>
								<a class="tips" href="/admin/modules/bc/print.php?id=<? echo $id_liv; ?>" target="_blank" name="<? echo $id_liv; ?>" title="" aria-describedby="ui-tooltip-2"><i class="fa fa-print text-success"></i></a>
								</td>
								
							</tr>
<?							
		}
	}		
?>

		

								
							</tbody>

						</table>	
</div>

</div>
<div class="row">
 <div class="col-lg-6">
	<div id="container" style="width:100%; height:100%;"></div>
 </div>

 <div class="col-lg-6">
	<? include("ajax/dash_production.php"); 	 ?>
 </div>





	</div>
	<div class="row">						
                    <?php
                    if($db !== false){
                        foreach($modules as $module){

                            $title = $module->getTitle();
                            $name = $module->getName();
                            $dir = $module->getDir();
                            $dates = $module->isDates();
                            $count = 0;
                            $last_date = "";
                            $rights = $module->getPermissions($_SESSION['user']['type']);
                            
                            if($module->isDashboard() && !in_array("no_access", $rights) && !empty($rights)){
                                $query = "SELECT count(id) AS nb";
                                if($dates) $query .= ", MAX(add_date) AS last_add_date, MAX(edit_date) AS last_add_date";
                                $query .= " FROM pm_".$name." WHERE 1";
                                if($module->isMultilingual()) $query .= " AND lang = ".DEFAULT_LANG;
                                
                                if(!in_array($_SESSION['user']['type'], array("administrator", "manager", "editor")) && db_column_exists($db, "pm_".$name, "users"))
                                    $query .= " AND users REGEXP '(^|,)".$_SESSION['user']['id']."(,|$)'";
                                
                                $result = @$db->query($query);
                                if($result !== false && $db->last_row_count() > 0){
                                    $row = $result->fetch();
                                    $count = $row[0];
                                    if($dates){
                                        $last_add_date = (!is_null($row[1])) ? $row[1] : 0;
                                        $last_edit_date = (!is_null($row[2])) ? $row[2] : 0;
                                        
                                        $last_date = max($last_edit_date, $last_add_date);
                                        $last_date = ($last_date == 0) ? "" : date("Y-m-d g:ia", $last_date);
                                    } ?>
                                    
                                    <div class="col-lg-3 col-md-4 col-sm-6">
                                        <div class="panel panel-primary">
                                            <div class="panel-heading">
                                                <div class="row">
                                                    <div class="col-xs-3">
                                                        <h3><?php echo $title; ?></h3>
                                                    </div>
                                                    <div class="col-xs-9 text-right">
                                                        <div class="huge"><i class="fa fa-<?php echo $module->getIcon(); ?>"></i> <?php echo $count; ?></div>
                                                        <?php
                                                        if($last_date != ""){
                                                            echo "<i class=\"fa fa-clock-o\"></i> <small>".$last_date."</small>";
                                                        }else echo "<small>&nbsp;</small>"; ?>
                                                    </div>
                                                </div>
                                            </div>
                                            <a href="<?php echo $dir; ?>/index.php?view=list">
                                                <div class="panel-footer">
                                                    <span class="pull-left"><?php echo $texts['SHOW']; ?></span>
                                                    <span class="pull-right"><i class="fa fa-chevron-circle-right"></i></span>
                                                    <div class="clearfix"></div>
                                                </div>
                                            </a>
                                            <a href="<?php echo $dir; ?>/index.php?view=form&id=0">
                                                <div class="panel-footer">
                                                    <span class="pull-left"><?php echo $texts['ADD']; ?></span>
                                                    <span class="pull-right"><i class="fa fa-plus-circle"></i></span>
                                                    <div class="clearfix"></div>
                                                </div>
                                            </a>
                                        </div>
                                    </div>
                                    <?php
                                }
                            }
                        }
                    } ?>
                </div>
				<div class="row">

				</div>
            </div>
        </div>
    </div>
</body>
</html>
<?php
$_SESSION['msg_error'] = array();
$_SESSION['msg_success'] = array();
$_SESSION['msg_notice'] = array(); ?>


<script>
    $(function(){
		$( ".datepickerss" ).datepicker({ dateFormat: 'dd-mm-yy' });
		var btn_click = 0;
		$(document).on( "click",".payer",function() {
			var str = $(this).attr("id");
			var id_facture = str.split( '_' );
			var n = <? echo time(); ?>;
			$("#vider_"+id_facture[1]).hide();
			$("#date_"+id_facture[1]).show();
			btn_click++;
			var parts = $("#date_"+id_facture[1]).val().split("-");
			var day = new Date(parseInt(parts[2], 10),
					parseInt(parts[1], 10) - 1,
					parseInt(parts[0], 10));
			day.setDate(day.getDate() + 1);		
			var milliseconds  = day.getTime() ;
			var date_now = milliseconds.toString().substring(0,10);
			// alert(date_now);
			date_now = date_now - 5000; // == Correction date GMT + 1
			if(btn_click > 1){
				if (confirm("Êtes-vous sûr de vouloir payer la transaction ?")) {
					// mysql = "UPDATE `pm_transaction` SET status_paiement = 1, date_paiement = '"+date_now+"', date_trans = '"+date_now+"' where id="+id_facture[1];
					mysql = "UPDATE `pm_transaction` SET status_paiement = 1, date_paiement = '"+date_now+"' where id="+id_facture[1];
					mysql_exc(mysql);
					$("#row_"+id_facture[1]).html("<td colspan='6' align='center' style='background-color:green;'><b style='color:#FFF;'> le paiement effectué avec succès !</b></td>");
					setTimeout(function(){$("#row_"+id_facture[1]).slideUp("slow"); }, 2000);
					// setTimeout(function(){ document.location.href="index.php?view=list"; }, 500);
					btn_click = 0;
					
				}
			}
		});
		
		$(document).on( "click",".payer_engagement",function() {	
			if (confirm("Êtes-vous sûr de vouloir payer l'Engagements financiers mensuels ?")) {
				var str = $(this).attr("id");
				var id_facture = str.split( '_' );
				var n = <? echo time(); ?>;
				var journal = $("#journal_"+id_facture[1]).val();
				var compte = $("#compte_"+id_facture[1]).val();
				var montant = $("#montant_"+id_facture[1]).val();
				var libelle = $("#libelle_"+id_facture[1]).val();
				mysql = "INSERT INTO `pm_transaction` (type,date_trans,journal,compte,montant,libelle,date_paiement,status_paiement,add_date ) values ('out','"+n+"',"+journal+","+compte+","+montant+",'"+libelle+"',"+n+",1,"+n+")";
				mysql_exc(mysql);
				$("#row_"+id_facture[1]).html("<td colspan='6' align='center' style='background-color:green;'><b style='color:#FFF;'> le paiement effectué avec succès !</b></td>");
				setTimeout(function(){$("#row_"+id_facture[1]).slideUp("slow"); }, 2000);
				// setTimeout(function(){ document.location.href="index.php?view=list"; }, 500);
				
			}
		});
		
		
		
	});	
	
//==========================================
function mysql_exc(str)
{
		var url_get = "/admin/ajax/mysql_exc.php"
				
			$.ajax({
                url: url_get,
                type: "POST",
                       data: {
                           query: str,
                       },
                       dataType: "JSON",
                       success: function (jsonStr) { 
						var text = JSON.stringify(jsonStr) ;
						console.log(text);						
                       }
             });		  
}	
// ==========================================	
function Get_dash() { 
    var myChart = Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'TOTAL PRODUCTION : <? echo date("F"); ?>'
    },
    xAxis: {
        categories: [          
            'MAXXX',
			'OOPS',
			'WOW',
			'PROMO'
        ],
        crosshair: true
    },
    
	<?
		include("ajax/get_dash_production.php"); 		
	?>
	
    yAxis: {
        min: 0,
        title: {
            text: 'Variation'
        }
    },
    tooltip: {
        headerFormat: '<span style="font-size:15px">{point.key}</span><table>',
        pointFormat: '<tr><td style="color:{series.color};padding:0;font-size:15px">{series.name}: </td>' +
            '<td style="padding:0;font-size:15px;"><b>{point.y:.f}</b></td></tr>',
        footerFormat: '</table>',
        shared: true,
        useHTML: true
    },
    plotOptions: {
        column: {
            pointPadding: 0.2,
            borderWidth: 0
        }
    }
});
}
</script>		


<script>
									$(document).ready(function() {
										//==========================================
											$("#synchroniser").click(function() {
												$("#synchro-msg").show();
												$(".container-fluid").hide();
												var url_get = "/admin/synchro/product.php"
														
													$.ajax({
														url: url_get,
														type: "POST",
														dataType: "JSON",
														complete: function (jsonStr) { 
															   // alert("ok");
															location.reload();
																													
														}
													});		  
											});
															
									});
								</script>
<script>
$(document).ready(function() {
Highcharts.chart('container', {
    chart: {
        type: 'line'
    },
    title: {
        text: 'TOTAL PRODUCTION'
    },
    subtitle: {
        text: ''
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    yAxis: {
        title: {
            text: 'Variation ( K )'
        }
    },
    plotOptions: {
        line: {
            dataLabels: {
                enabled: true
            },
            enableMouseTracking: false
        }
    },
	    tooltip: {
        headerFormat: '<span style="font-size:15px">{point.key}</span><table>',
        pointFormat: '<tr>' +
            '<td style="padding:0;font-size:20px;"><b>{point.y:.f}</b></td></tr>',
        footerFormat: '</table>',
        shared: true,
        useHTML: true
    },
    plotOptions: {
        column: {
            pointPadding: 0.2,
            borderWidth: 0
        }
    },
    series: [
	<?
	
include("ajax/dash_production_graph_2018.php");
echo ',' ;
include("ajax/dash_production_graph_2019.php");
	?>
	]
	
	// {
        // name: 'Tokyo',
        // data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
    // }, {
        // name: 'London',
        // data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
    // }]
});	
	
});	
</script>

<?

function restyle_text($input){
    $input = number_format($input);
    $input_count = substr_count($input, ',');
    if($input_count != '0'){
        if($input_count == '1'){
            return substr($input, 0, -4);
        } else if($input_count == '2'){
            return substr($input, 0, -8);
        } else if($input_count == '3'){
            return substr($input, 0,  -12);
        } else {
            return;
        }
    } else {
        return $input;
    }
}

?>