get('roleid');
$userId = $current_user->get('id');
$viewer = new Vtiger_Viewer();
$viewer->assign('MODULE', 'Home');
$viewer->assign('SELECTED_MENU_CATEGORY', 'MARKETING');
$viewer->assign('COMPANY_LOGO', Vtiger_CompanyDetails_Model::getInstanceById()->getLogo());
$viewer->assign('COMPANY_DETAILS_SETTINGS',new Settings_Vtiger_CompanyDetails_Model());
$viewer->assign('USER_MODEL', $current_user);
$viewer->assign('CURRENT_USER_MODEL', $current_user);
$viewer->assign('PAGETITLE','Encaissement');
$viewer->view('CustomDashboard.tpl');
if(isset($_POST['valider'])) {
$Factures = getFacturesAndEncaissements($_POST['code_client'],$userId,$roleid);
$sumFactures = sumFactures($_POST['code_client'] , $userId , $roleid);
$sumEncaissements = sumEncaissements($_POST['code_client'], $userId , $roleid);
}
function result($Factures){
echo '
| '.$Factures['libelle'].' |
'.$Factures['date'].' |
';if($Factures['libelle'] == "Encaissement"){ echo number_format($Factures['total'],2,',','.');}else{echo "0";} echo' |
';if($Factures['libelle'] == "Facture"){echo number_format($Factures['total'],2,',','.');}else{echo "0";} echo' |
';
}
function getFacturesAndEncaissements($idClient,$idUser,$roleid){
global $adb;
if ($_POST['dateF'] != null && $_POST['dateD'] != null ) {
if(isDG($roleid)){
$query1 = "SELECT invoiceid as id , inv.invoicedate as date,inv.total as total ,'Facture' as libelle FROM vtiger_crmentity crm JOIN vtiger_invoice inv ON inv.invoiceid = crm.crmid and crm.deleted=0 and crm.setype='Invoice' and inv.accountid=".$idClient." and CAST(crm.createdtime as date) >= '".$_POST['dateD']."' and CAST(crm.createdtime as date) <= '".$_POST['dateF']."' UNION SELECT code_encaissement as id , date_encaissement as date ,montant as total ,'Encaissement' as libelle FROM encaissement WHERE code_client =".$idClient." and CAST(dateTime_creation as date) >= '".$_POST['dateD']."' and CAST(dateTime_creation as date) <= '".$_POST['dateF']."' Order by date";
}else{
$query1 = "SELECT invoiceid as id , inv.invoicedate as date,inv.total as total ,'Facture' as libelle FROM vtiger_crmentity crm JOIN vtiger_invoice inv ON inv.invoiceid = crm.crmid and crm.deleted=0 and crm.smownerid=".$idUser." and crm.setype='Invoice' and inv.accountid=".$idClient." and CAST(crm.createdtime as date) >= '".$_POST['dateD']."' and CAST(crm.createdtime as date) <= '".$_POST['dateF']."' UNION SELECT code_encaissement as id , date_encaissement as date ,montant as total ,'Encaissement' as libelle FROM encaissement WHERE code_client =".$idClient." and user_id=".$idUser." and CAST(dateTime_creation as date) >= '".$_POST['dateD']."' and CAST(dateTime_creation as date) <= '".$_POST['dateF']."' Order by date";
}
}else{
if(isDG($roleid)){
$query1 = "SELECT invoiceid as id , inv.invoicedate as date,inv.total as total ,'Facture' as libelle FROM vtiger_crmentity crm JOIN vtiger_invoice inv ON inv.invoiceid = crm.crmid and crm.deleted=0 and crm.setype='Invoice' and inv.accountid=".$idClient." UNION SELECT code_encaissement as id , date_encaissement as date ,montant as total ,'Encaissement' as libelle FROM encaissement WHERE code_client =".$idClient." Order by date";
}else{
$query1 = "SELECT invoiceid as id , inv.invoicedate as date,inv.total as total ,'Facture' as libelle FROM vtiger_crmentity crm JOIN vtiger_invoice inv ON inv.invoiceid = crm.crmid and crm.deleted=0 and crm.smownerid=".$idUser." and crm.setype='Invoice' and inv.accountid=".$idClient." UNION SELECT code_encaissement as id , date_encaissement as date ,montant as total ,'Encaissement' as libelle FROM encaissement WHERE code_client =".$idClient." and user_id=".$idUser." Order by date";
}
}
$sql_get_result1 = $adb->query($query1);
$Factures = array();
while ($recordinfo = $adb->fetch_array($sql_get_result1)) {
$Factures[] = $recordinfo;
}
return $Factures;
}
function sumEncaissements($idClient , $idUser ,$roleid){
global $adb;
if ($_POST['dateF'] != null && $_POST['dateD'] != null ) {
if(isDG($roleid)){
$query1 = "SELECT sum(montant) as total FROM encaissement WHERE code_client =".$idClient." and CAST(dateTime_creation as date) >= '".$_POST['dateD']."' and CAST(dateTime_creation as date) <= '".$_POST['dateF']."'";
}else{
$query1 = "SELECT sum(montant) as total FROM encaissement WHERE code_client =".$idClient." and user_id=".$idUser." and CAST(dateTime_creation as date) >= '".$_POST['dateD']."' and CAST(dateTime_creation as date) <= '".$_POST['dateF']."'";
}
}else{
if(isDG($roleid)){
$query1 = "SELECT sum(montant) as total FROM encaissement WHERE code_client ='".$idClient."' ";
}else{
$query1 = "SELECT sum(montant) as total FROM encaissement WHERE code_client =".$idClient." and user_id=".$idUser." ";
}
}
$sql_get_result1 = $adb->query($query1);
$sumEncaissements = array();
while ($recordinfo = $adb->fetch_array($sql_get_result1)) {
$sumEncaissements[] = $recordinfo;
}
return $sumEncaissements;
}
function sumFactures($idClient , $idUser , $roleid){
global $adb;
if ($_POST['dateF'] != null && $_POST['dateD'] != null ) {
if(isDG($roleid)){
$query1 = "SELECT sum(inv.total) as total FROM vtiger_crmentity crm JOIN vtiger_invoice inv ON inv.invoiceid = crm.crmid and crm.deleted=0 and crm.setype='Invoice' and inv.accountid=".$idClient." and CAST(crm.createdtime as date) >= '".$_POST['dateD']."' and CAST(crm.createdtime as date) <= '".$_POST['dateF']."'";
}else{
$query1 = "SELECT sum(inv.total) as total FROM vtiger_crmentity crm JOIN vtiger_invoice inv ON inv.invoiceid = crm.crmid and crm.deleted=0 and crm.smownerid=".$idUser." and crm.setype='Invoice' and inv.accountid=".$idClient." and CAST(crm.createdtime as date) >= '".$_POST['dateD']."' and CAST(crm.createdtime as date) <= '".$_POST['dateF']."'";
}
}else{
if(isDG($roleid)){
$query1 = "SELECT sum(inv.total) as total FROM vtiger_crmentity crm JOIN vtiger_invoice inv ON inv.invoiceid = crm.crmid and crm.deleted=0 and crm.setype='Invoice' and inv.accountid=".$idClient." ";
}else{
$query1 = "SELECT sum(inv.total) as total FROM vtiger_crmentity crm JOIN vtiger_invoice inv ON inv.invoiceid = crm.crmid and crm.deleted=0 and crm.smownerid=".$idUser." and crm.setype='Invoice' and inv.accountid=".$idClient." ";
}
}
$sql_get_result1 = $adb->query($query1);
$sumFactures = array();
while ($recordinfo = $adb->fetch_array($sql_get_result1)) {
$sumFactures[] = $recordinfo;
}
return $sumFactures;
}
function getAllGrossiste(){
global $adb;
$query5 = "SELECT accountid,accountname FROM `vtiger_account` WHERE `industry`='grossiste' ";
$result5 =$adb->query($query5);
$Grossistes=array();
while ($recordinfo = $adb->fetch_array($result5)) {
$Grossistes[] = $recordinfo;
}
return $Grossistes;
}
echo '