<?php 
//======================================
function get_article($id){
	global $db;
	$journal = "";
	$query_j = "select * from pm_produits where id =".$id;
	$query_j2 = "select * from pm_produitsachat where id =".$id;
	$result_j = @$db->query($query_j);
	$nb_rows_default = $db->last_row_count();
	$result_j2 = @$db->query($query_j2);
	
	
	if($nb_rows_default>0){
	if($result_j !== false){
		foreach($result_j as $row_j){
			$journal = $row_j["desingnation"];
		}
	}
	}else{
	if($result_j2 !== false){
		foreach($result_j2 as $row_j2){
			$journal = $row_j2["desingnation"];
		}
	}		
	}
	return $journal;
}	
//======================================
function get_sous_famille($id){
	global $db;
	$journal = "";
	$query_j = "select * from pm_famille where id =".$id;
	$result_j = @$db->query($query_j);
	if($result_j !== false){
		foreach($result_j as $row_j){
			$journal = $row_j["nom"];
		}
	}
	return $journal;
}//======================================
function get_unite($id){
	global $db;
	$journal = "";
	$query_j = "select * from pm_unite where id =".$id;
	$result_j = @$db->query($query_j);
	if($result_j !== false){
		foreach($result_j as $row_j){
			$journal = $row_j["name"];
		}
	}
	return $journal;
}//======================================
function get_famille($id){
	global $db;
	$journal = "";
	$query_j = "select * from pm_famille_classe where id =".$id;
	$result_j = @$db->query($query_j);
	if($result_j !== false){
		foreach($result_j as $row_j){
			$journal = $row_j["nom"];
		}
	}
	return $journal;
}
//======================================
function get_cat_famille($id){
	global $db;
	$journal = "";
	$query_j = "select * from pm_category where id =".$id;
	$result_j = @$db->query($query_j);
	if($result_j !== false){
		foreach($result_j as $row_j){
			$journal = $row_j["nom"];
		}
	}
	return $journal;
}
//======================================
function get_journal($id){
	global $db;
	$journal = "";
	$query_j = "select * from pm_journal where id =".$id;
	$result_j = @$db->query($query_j);
	if($result_j !== false){
		foreach($result_j as $row_j){
			$journal = $row_j["name"];
		}
	}
	return $journal;
}
//======================================
function get_mode_reglement($id){
	global $db;
	$mode_reglement = "";
	$query_cam = "select * from pm_mode_paiement where id =".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$mode_reglement = $row_cam["name"];
		}
	}
	return $mode_reglement;	
}
//======================================
function compte($id){
	global $db;
	$compte = "";
	$query_cam = "select * from pm_compte_financier where id =".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$compte = $row_cam["name"];
		}
	}
	return $compte;	
}
//======================================
function fournisseur($id){
	global $db;
	$fournisseur = "";
	$query_cam = "SELECT name FROM pm_fournisseurs WHERE id = ".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$fournisseur = $row_cam["name"];
		}
	}
	return $fournisseur;	
}
//======================================
function get_status_bc($id){
	global $db;
	$count_be = 0 ;
													$query_bc = "SELECT SUM(qte) as bc FROM `pm_bc_product` WHERE id_bc = ".$id;
													$result_bc = $db->query($query_bc);
													if($result_bc !== false && $db->last_row_count() > 0){
														$row_bc = $result_bc->fetch();																
														$count_bc = $row_bc['bc'];
													}
													$query_be = "SELECT * from pm_be WHERE ref_bc = ".$id;
													$result_be = $db->query($query_be);
													if($result_be !== false){
														foreach($result_be as $row_be){
															$query_bep = "SELECT qte from pm_be_product WHERE id_be = ".$row_be["id"];
															// echo $query_bep.'<br>';
															$result_bep = $db->query($query_bep);
															if($result_bep !== false){
																foreach($result_bep as $row_bep){
																	$count_be = $count_be + $row_bep["qte"];
																}
															}	
														
														}
													}
													//========= 
													$query_bcs = "SELECT valider FROM `pm_bc` WHERE id = ".$id;
													$result_bcs = $db->query($query_bcs);
													if($result_bcs !== false && $db->last_row_count() > 0){
														$row_bcs = $result_bcs->fetch();																
														$status_bc = $row_bcs['valider'];
													}
													// echo 'status :'.$status_bc.'<br>';
													//=========
													if($count_bc > 0){
														$status = ($count_be*100) / $count_bc;
													}else{
														$status = 0;
													}
													$status = number_format($status, 0);
													if($status_bc == 1){
														$status = 100;
													}
													if($status < 10){
														$color_satatus = "red";
													}elseif($status < 50){
														$color_satatus = "orange";
													}elseif($status < 100){
														$color_satatus = "blue";
													}else{
														$color_satatus = "green";
													}
													
													echo '
														<div class="w3-light-grey">
														  <div class="w3-container w3-round w3-'.$color_satatus.'" style="width:'.$status.'%;max-width:100%;color:#000 !important;font-weight: bold;">'.$status.'%</div>
														</div>
														';
													
}
//======================================
function client($id){
	global $db;
	$client = "";
	$query_cam = "SELECT name FROM pm_client WHERE id =".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$client = $row_cam["name"];
		}
	}
	return $client;	
}
//======================================
function ref_facture($id){
	global $db;
	$ref_facture = "";
	$query_cam = "SELECT ref_bl FROM pm_facture_achat WHERE id =".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$ref_facture = $row_cam["ref_bl"];
		}
	}
	return $ref_facture;	
}
//======================================
function ref_facture_vente($id){
	global $db;
	$ref_facture = "";
	$query_cam = "SELECT idd FROM pm_facture WHERE id =".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$ref_facture = $row_cam["idd"];
		}
	}
	return $ref_facture;	
}
//======================================
function class_compte($id){
	global $db;
	$ref_facture = "";
	$query_cam = "SELECT name FROM pm_classe_compte WHERE id =".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$ref_facture = $row_cam["name"];
		}
	}
	return $ref_facture;	
}
//======================================
function ref_facture_avoir($id){
	global $db;
	$ref_facture = "";
	$query_cam = "SELECT ref_avoir FROM pm_facture_avoir WHERE ref_bl =".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$ref_facture .= $row_cam["ref_avoir"]." - ";
		}
	}
	return $ref_facture;	
}
//======================================
function get_sum_table($table,$champ,$codition){
	global $db;
	$info = "";
	$query_cam = "SELECT SUM(".$champ.") as ".$champ." FROM ".$table." WHERE ".$codition;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$info = $row_cam[$champ];
		}
	}
	return $info;	
}
//======================================
function get_info_table($table,$champ,$id){
	global $db;
	$info = "";
	$query_cam = "SELECT ".$champ." FROM ".$table." WHERE id =".$id;
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$info = $row_cam[$champ];
		}
	}
	return $info;	
}
//======================================
function get_info_table_codition($table,$champ,$codition){
	global $db;
	$info = "";
	$query_cam = "SELECT ".$champ." FROM ".$table." WHERE ".$codition;
	
	$result_cam = @$db->query($query_cam);
	if($result_cam !== false){
		foreach($result_cam as $row_cam){
			$info = $row_cam[$champ];
		}
	}
	return $info;	
}

//======================================
?>