--- /tmp/dsg/dolibarr/htdocs/compta/resultat/github_clientfourn.php
+++ /tmp/dsg/dolibarr/htdocs/compta/resultat/client_clientfourn.php
@@ -69,13 +69,13 @@
 // Date range

 $year = GETPOST('year', 'int');

 if (empty($year)) {

-	$year_current = strftime("%Y", dol_now());

-	$month_current = strftime("%m", dol_now());

-	$year_start = $year_current;

+    $year_current = strftime("%Y", dol_now());

+    $month_current = strftime("%m", dol_now());

+    $year_start = $year_current;

 } else {

-	$year_current = $year;

-	$month_current = strftime("%m", dol_now());

-	$year_start = $year;

+    $year_current = $year;

+    $month_current = strftime("%m", dol_now());

+    $year_start = $year;

 }

 $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);

 $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);

@@ -83,29 +83,30 @@
 // We define date_start and date_end

 if (empty($date_start) || empty($date_end)) // We define date_start and date_end

 {

-	$q = GETPOST("q") ?GETPOST("q") : 0;

-	if ($q == 0)

-	{

-		// We define date_start and date_end

-		$year_end = $year_start;

-		$month_start = GETPOST("month") ?GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);

-		if (!GETPOST('month'))

-		{

-			if (!GETPOST("year") && $month_start > $month_current)

-			{

-				$year_start--;

-				$year_end--;

-			}

-			$month_end = $month_start - 1;

-			if ($month_end < 1) $month_end = 12;

-			else $year_end++;

-		} else $month_end = $month_start;

-		$date_start = dol_get_first_day($year_start, $month_start, false); $date_end = dol_get_last_day($year_end, $month_end, false);

-	}

-	if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); }

-	if ($q == 2) { $date_start = dol_get_first_day($year_start, 4, false); $date_end = dol_get_last_day($year_start, 6, false); }

-	if ($q == 3) { $date_start = dol_get_first_day($year_start, 7, false); $date_end = dol_get_last_day($year_start, 9, false); }

-	if ($q == 4) { $date_start = dol_get_first_day($year_start, 10, false); $date_end = dol_get_last_day($year_start, 12, false); }

+    $q = GETPOST("q") ?GETPOST("q") : 0;

+    if ($q == 0)

+    {

+        // We define date_start and date_end

+        $year_end = $year_start;

+        $month_start = GETPOST("month") ?GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);

+        if (!GETPOST('month'))

+        {

+            if (!GETPOST("year") && $month_start > $month_current)

+            {

+                $year_start--;

+                $year_end--;

+            }

+            $month_end = $month_start - 1;

+            if ($month_end < 1) $month_end = 12;

+            else $year_end++;

+        }

+        else $month_end = $month_start;

+        $date_start = dol_get_first_day($year_start, $month_start, false); $date_end = dol_get_last_day($year_end, $month_end, false);

+    }

+    if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); }

+    if ($q == 2) { $date_start = dol_get_first_day($year_start, 4, false); $date_end = dol_get_last_day($year_start, 6, false); }

+    if ($q == 3) { $date_start = dol_get_first_day($year_start, 7, false); $date_end = dol_get_last_day($year_start, 9, false); }

+    if ($q == 4) { $date_start = dol_get_first_day($year_start, 10, false); $date_end = dol_get_last_day($year_start, 12, false); }

 }

 

 // $date_start and $date_end are defined. We force $year_start and $nbofyear

@@ -144,42 +145,34 @@
 {

 	$name = $langs->trans("ReportInOut").', '.$langs->trans("ByPredefinedAccountGroups");

 	$calcmode = $langs->trans("CalcModeDebt");

-	$calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '{s1}', '{s2}').')';

-	$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=RECETTES-DEPENSES">', '</a>'), $calcmode);

-	if (!empty($conf->accounting->enabled)) {

-		$calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{s1}', '{s2}').')';

-		$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=BOOKKEEPING">', '</a>'), $calcmode);

-	}

-	$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);

+    $calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=RECETTES-DEPENSES">', '</a>').')';

+    if (!empty($conf->accounting->enabled)) $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=BOOKKEEPING">', '</a>').')';

+    $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);

 	$periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] - 1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] + 1)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");

-	$description = $langs->trans("RulesResultDue");

+    $description = $langs->trans("RulesResultDue");

 	if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description .= $langs->trans("DepositsAreNotIncluded");

-	else $description .= $langs->trans("DepositsAreIncluded");

-	$builddate = dol_now();

-	//$exportlink=$langs->trans("NotYetAvailable");

-} elseif ($modecompta == "RECETTES-DEPENSES")

+	else  $description .= $langs->trans("DepositsAreIncluded");

+    $builddate = dol_now();

+    //$exportlink=$langs->trans("NotYetAvailable");

+}

+elseif ($modecompta == "RECETTES-DEPENSES")

 {

 	$name = $langs->trans("ReportInOut").', '.$langs->trans("ByPredefinedAccountGroups");

 	$calcmode = $langs->trans("CalcModeEngagement");

-	$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{s1}', '{s2}').')';

-	$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=CREANCES-DETTES">', '</a>'), $calcmode);

-	if (!empty($conf->accounting->enabled)) {

-		$calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{s1}', '{s2}').')';

-		$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=BOOKKEEPING">', '</a>'), $calcmode);

-	}

-	$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);

-	$periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] - 1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] + 1)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");

-	$description = $langs->trans("RulesResultInOut");

-	$builddate = dol_now();

-	//$exportlink=$langs->trans("NotYetAvailable");

-} elseif ($modecompta == "BOOKKEEPING")

+    $calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=CREANCES-DETTES">', '</a>').')';

+    if (!empty($conf->accounting->enabled)) $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=BOOKKEEPING">', '</a>').')';

+    $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);

+    $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] - 1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] + 1)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");

+    $description = $langs->trans("RulesResultInOut");

+    $builddate = dol_now();

+    //$exportlink=$langs->trans("NotYetAvailable");

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	$name = $langs->trans("ReportInOut").', '.$langs->trans("ByPredefinedAccountGroups");

 	$calcmode = $langs->trans("CalcModeBookkeeping");

-	$calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '{s1}', '{s2}').')';

-	$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=RECETTES-DEPENSES">', '</a>'), $calcmode);

-	$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{s1}', '{s2}').')';

-	$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=CREANCES-DETTES">', '</a>'), $calcmode);

+    $calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=RECETTES-DEPENSES">', '</a>').')';

+	$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=CREANCES-DETTES">', '</a>').')';

 	$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);

 	$arraylist = array('no'=>$langs->trans("No"), 'yes'=>$langs->trans("AccountWithNonZeroValues"), 'all'=>$langs->trans("All"));

 	$period .= ' &nbsp; &nbsp; '.$langs->trans("DetailByAccount").' '.$form->selectarray('showaccountdetail', $arraylist, $showaccountdetail, 0);

@@ -196,7 +189,7 @@
 

 if (!empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING')

 {

-	print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);

+    print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);

 }

 

 // Show report array

@@ -219,7 +212,9 @@
 if ($modecompta == 'BOOKKEEPING')

 {

 	print_liste_field_titre("Amount", $_SERVER["PHP_SELF"], 'amount', '', $param, 'class="right"', $sortfield, $sortorder);

-} else {

+}

+else

+{

 	if ($modecompta == 'CREANCES-DETTES')

 	{

 		print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], 'amount_ht', '', $param, 'class="right"', $sortfield, $sortorder);

@@ -228,8 +223,6 @@
 }

 print "</tr>\n";

 

-

-$total_ht_outcome = $total_ttc_outcome = $total_ht_income = $total_ttc_income = 0;

 

 

 if ($modecompta == 'BOOKKEEPING')

@@ -252,7 +245,7 @@
 	$sql .= " AND f.entity = ".$conf->entity;

 	if (!empty($date_start) && !empty($date_end))

 		$sql .= " AND f.doc_date >= '".$db->idate($date_start)."' AND f.doc_date <= '".$db->idate($date_end)."'";

-	$sql .= " GROUP BY pcg_type DESC, name, socid";

+	$sql .= " GROUP BY pcg_type, name, socid";

 	$sql .= $db->order($sortfield, $sortorder);

 

 	$oldpcgtype = '';

@@ -284,15 +277,6 @@
 				$total_ht += (isset($objp->amount) ? $objp->amount : 0);

 				$total_ttc += (isset($objp->amount) ? $objp->amount : 0);

 

-				if ($objp->pcg_type == 'INCOME') {

-					$total_ht_income += (isset($objp->amount) ? $objp->amount : 0);

-					$total_ttc_income += (isset($objp->amount) ? $objp->amount : 0);

-				}

-				if ($objp->pcg_type == 'EXPENSE') {

-					$total_ht_outcome -= (isset($objp->amount) ? $objp->amount : 0);

-					$total_ttc_outcome -= (isset($objp->amount) ? $objp->amount : 0);

-				}

-

 				// Loop on detail of all accounts

 				// This make 14 calls for each detail of account (NP, N and month m)

 				if ($showaccountdetail != 'no')

@@ -328,11 +312,16 @@
 

 				$i++;

 			}

-		} else {

+		}

+		else

+		{

 			print '<tr><td colspan="4" class="opacitymedium">'.$langs->trans("NoRecordFound").'</td></tr>';

 		}

-	} else dol_print_error($db);

-} else {

+	}

+	else dol_print_error($db);

+}

+else

+{

 	/*

 	 * Factures clients

 	 */

@@ -340,32 +329,34 @@
 

 	if ($modecompta == 'CREANCES-DETTES')

 	{

-		$sql = "SELECT s.nom as name, s.rowid as socid, sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc";

-		$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";

-		$sql .= ", ".MAIN_DB_PREFIX."facture as f";

-		$sql .= " WHERE f.fk_soc = s.rowid";

-		$sql .= " AND f.fk_statut IN (1,2)";

-		if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))

-			$sql .= " AND f.type IN (0,1,2,5)";

-		else $sql .= " AND f.type IN (0,1,2,3,5)";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

-	} elseif ($modecompta == 'RECETTES-DEPENSES')

+	    $sql = "SELECT s.nom as name, s.rowid as socid, sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";

+	    $sql .= ", ".MAIN_DB_PREFIX."facture as f";

+	    $sql .= " WHERE f.fk_soc = s.rowid";

+	    $sql .= " AND f.fk_statut IN (1,2)";

+	    if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))

+	    	$sql .= " AND f.type IN (0,1,2,5)";

+		else

+			$sql .= " AND f.type IN (0,1,2,3,5)";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

+	}

+	elseif ($modecompta == 'RECETTES-DEPENSES')

 	{

 		/*

 	     * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les

 	     * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)

 	     */

-		$sql = "SELECT s.nom as name, s.rowid as socid, sum(pf.amount) as amount_ttc";

-		$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";

-		$sql .= ", ".MAIN_DB_PREFIX."facture as f";

-		$sql .= ", ".MAIN_DB_PREFIX."paiement_facture as pf";

-		$sql .= ", ".MAIN_DB_PREFIX."paiement as p";

-		$sql .= " WHERE p.rowid = pf.fk_paiement";

-		$sql .= " AND pf.fk_facture = f.rowid";

-		$sql .= " AND f.fk_soc = s.rowid";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+	    $sql = "SELECT s.nom as name, s.rowid as socid, sum(pf.amount) as amount_ttc";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";

+	    $sql .= ", ".MAIN_DB_PREFIX."facture as f";

+	    $sql .= ", ".MAIN_DB_PREFIX."paiement_facture as pf";

+	    $sql .= ", ".MAIN_DB_PREFIX."paiement as p";

+	    $sql .= " WHERE p.rowid = pf.fk_paiement";

+	    $sql .= " AND pf.fk_facture = f.rowid";

+	    $sql .= " AND f.fk_soc = s.rowid";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

 	}

 	$sql .= " AND f.entity IN (".getEntity('invoice').")";

 	if ($socid) $sql .= " AND f.fk_soc = ".$socid;

@@ -375,86 +366,83 @@
 	dol_syslog("get customer invoices", LOG_DEBUG);

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

 	if ($result) {

-		$num = $db->num_rows($result);

-		$i = 0;

-		while ($i < $num)

-		{

-			$objp = $db->fetch_object($result);

-

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print "<td>".$langs->trans("Bills").' <a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$objp->socid.'">'.$objp->name."</td>\n";

-

-			if ($modecompta == 'CREANCES-DETTES')

-				print '<td class="right">'.price($objp->amount_ht)."</td>\n";

-			print '<td class="right">'.price($objp->amount_ttc)."</td>\n";

-

-			$total_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);

-			$total_ttc += $objp->amount_ttc;

-			print "</tr>\n";

-			$i++;

-		}

-		$db->free($result);

+	    $num = $db->num_rows($result);

+	    $i = 0;

+	    while ($i < $num)

+	    {

+	        $objp = $db->fetch_object($result);

+

+	        print '<tr class="oddeven"><td>&nbsp;</td>';

+	        print "<td>".$langs->trans("Bills").' <a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$objp->socid.'">'.$objp->name."</td>\n";

+

+	        if ($modecompta == 'CREANCES-DETTES')

+	        	print '<td class="right">'.price($objp->amount_ht)."</td>\n";

+	        print '<td class="right">'.price($objp->amount_ttc)."</td>\n";

+

+	        $total_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);

+	        $total_ttc += $objp->amount_ttc;

+	        print "</tr>\n";

+	        $i++;

+	    }

+	    $db->free($result);

 	} else {

-		dol_print_error($db);

+	    dol_print_error($db);

 	}

 

 	// On ajoute les paiements clients anciennes version, non lie par paiement_facture

 	if ($modecompta == 'RECETTES-DEPENSES')

 	{

-		$sql = "SELECT 'Autres' as name, '0' as idp, sum(p.amount) as amount_ttc";

-		$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";

-		$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";

-		$sql .= ", ".MAIN_DB_PREFIX."paiement as p";

-		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";

-		$sql .= " WHERE pf.rowid IS NULL";

-		$sql .= " AND p.fk_bank = b.rowid";

-		$sql .= " AND b.fk_account = ba.rowid";

-		$sql .= " AND ba.entity IN (".getEntity('bank_account').")";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

-		$sql .= " GROUP BY name, idp";

-		$sql .= " ORDER BY name";

-

-		dol_syslog("get old customer payments not linked to invoices", LOG_DEBUG);

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

-		if ($result) {

-			$num = $db->num_rows($result);

-			$i = 0;

-			if ($num) {

-				while ($i < $num)

-				{

-					$objp = $db->fetch_object($result);

-

-

-					print '<tr class="oddeven"><td>&nbsp;</td>';

-					print "<td>".$langs->trans("Bills")." ".$langs->trans("Other")." (".$langs->trans("PaymentsNotLinkedToInvoice").")\n";

-

-					if ($modecompta == 'CREANCES-DETTES')

-						print '<td class="right">'.price($objp->amount_ht)."</td>\n";

-					print '<td class="right">'.price($objp->amount_ttc)."</td>\n";

-

-					$total_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);

-					$total_ttc += $objp->amount_ttc;

-

-					print "</tr>\n";

-					$i++;

-				}

-			}

-			$db->free($result);

-		} else {

-			dol_print_error($db);

-		}

+	    $sql = "SELECT 'Autres' as name, '0' as idp, sum(p.amount) as amount_ttc";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";

+	    $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";

+	    $sql .= ", ".MAIN_DB_PREFIX."paiement as p";

+	    $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";

+	    $sql .= " WHERE pf.rowid IS NULL";

+	    $sql .= " AND p.fk_bank = b.rowid";

+	    $sql .= " AND b.fk_account = ba.rowid";

+	    $sql .= " AND ba.entity IN (".getEntity('bank_account').")";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+	    $sql .= " GROUP BY name, idp";

+	    $sql .= " ORDER BY name";

+

+	    dol_syslog("get old customer payments not linked to invoices", LOG_DEBUG);

+	    $result = $db->query($sql);

+	    if ($result) {

+	        $num = $db->num_rows($result);

+	        $i = 0;

+	        if ($num) {

+	            while ($i < $num)

+	            {

+	                $objp = $db->fetch_object($result);

+

+

+	                print '<tr class="oddeven"><td>&nbsp;</td>';

+	                print "<td>".$langs->trans("Bills")." ".$langs->trans("Other")." (".$langs->trans("PaymentsNotLinkedToInvoice").")\n";

+

+	                if ($modecompta == 'CREANCES-DETTES')

+	                	print '<td class="right">'.price($objp->amount_ht)."</td>\n";

+	                print '<td class="right">'.price($objp->amount_ttc)."</td>\n";

+

+	                $total_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);

+	                $total_ttc += $objp->amount_ttc;

+

+	                print "</tr>\n";

+	                $i++;

+	            }

+	        }

+	        $db->free($result);

+	    } else {

+	        dol_print_error($db);

+	    }

 	}

 

 	if ($total_ttc == 0)

 	{

-		print '<tr class="oddeven"><td>&nbsp;</td>';

-		print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';

-		print '</tr>';

-	}

-

-	$total_ht_income += $total_ht;

-	$total_ttc_income += $total_ttc;

+	    print '<tr class="oddeven"><td>&nbsp;</td>';

+	    print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';

+	    print '</tr>';

+	}

 

 	print '<tr class="liste_total">';

 	if ($modecompta == 'CREANCES-DETTES')

@@ -462,114 +450,36 @@
 	print '<td colspan="3" class="right">'.price($total_ttc).'</td>';

 	print '</tr>';

 

-	/*

-	 * Donations

-	 */

-

-	if (!empty($conf->don->enabled))

-	{

-		print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("Donations").'</td></tr>';

-

-		if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'RECETTES-DEPENSES')

-		{

-			if ($modecompta == 'CREANCES-DETTES')

-			{

-				$sql = "SELECT p.societe as name, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";

-				$sql .= " FROM ".MAIN_DB_PREFIX."don as p";

-				$sql .= " WHERE p.entity IN (".getEntity('donation').")";

-				$sql .= " AND fk_statut in (1,2)";

-			} else {

-				$sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";

-				$sql .= " FROM ".MAIN_DB_PREFIX."don as p";

-				$sql .= " INNER JOIN ".MAIN_DB_PREFIX."payment_donation as pe ON pe.fk_donation = p.rowid";

-				$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";

-				$sql .= " WHERE p.entity IN (".getEntity('donation').")";

-				$sql .= " AND fk_statut >= 2";

-			}

-			if (!empty($date_start) && !empty($date_end))

-				$sql .= " AND p.datedon >= '".$db->idate($date_start)."' AND p.datedon <= '".$db->idate($date_end)."'";

-		}

-		$sql .= " GROUP BY p.societe, p.firstname, p.lastname, dm";

-		$newsortfield = $sortfield;

-		if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'p.societe, p.firstname, p.lastname, dm';

-		if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

-		if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

-		$sql .= $db->order($newsortfield, $sortorder);

-

-		dol_syslog("get dunning");

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

-		$subtotal_ht = 0;

-		$subtotal_ttc = 0;

-		if ($result)

-		{

-			$num = $db->num_rows($result);

-			$i = 0;

-			if ($num)

-			{

-				while ($i < $num)

-				{

-					$obj = $db->fetch_object($result);

-

-					$total_ht += $obj->amount;

-					$total_ttc += $obj->amount;

-					$subtotal_ht += $obj->amount;

-					$subtotal_ttc += $obj->amount;

-

-					print '<tr class="oddeven"><td>&nbsp;</td>';

-

-					print "<td>".$langs->trans("Donation")." <a href=\"".DOL_URL_ROOT."/don/list.php?search_company=".$obj->name."&search_name=".$obj->firstname." ".$obj->lastname."\">".$obj->name." ".$obj->firstname." ".$obj->lastname."</a></td>\n";

-

-					if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price($obj->amount).'</td>';

-					print '<td class="right">'.price($obj->amount).'</td>';

-					print '</tr>';

-					$i++;

-				}

-			} else {

-				print '<tr class="oddeven"><td>&nbsp;</td>';

-				print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';

-				print '</tr>';

-			}

-		} else {

-			dol_print_error($db);

-		}

-

-		$total_ht_income += $subtotal_ht;

-		$total_ttc_income += $subtotal_ttc;

-

-		print '<tr class="liste_total">';

-		if ($modecompta == 'CREANCES-DETTES')

-			print '<td colspan="3" class="right">'.price($subtotal_ht).'</td>';

-		print '<td colspan="3" class="right">'.price($subtotal_ttc).'</td>';

-		print '</tr>';

-	}

 

 	/*

 	 * Suppliers invoices

 	 */

 	if ($modecompta == 'CREANCES-DETTES')

 	{

-		$sql = "SELECT s.nom as name, s.rowid as socid, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";

-		$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";

-		$sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f";

-		$sql .= " WHERE f.fk_soc = s.rowid";

-		$sql .= " AND f.fk_statut IN (1,2)";

-		if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))

-			$sql .= " AND f.type IN (0,1,2)";

-		else $sql .= " AND f.type IN (0,1,2,3)";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

-	} elseif ($modecompta == 'RECETTES-DEPENSES')

-	{

-		$sql = "SELECT s.nom as name, s.rowid as socid, sum(pf.amount) as amount_ttc";

-		$sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";

-		$sql .= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";

-		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f";

-		$sql .= " ON pf.fk_facturefourn = f.rowid";

-		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s";

-		$sql .= " ON f.fk_soc = s.rowid";

-		$sql .= " WHERE p.rowid = pf.fk_paiementfourn ";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+	    $sql = "SELECT s.nom as name, s.rowid as socid, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";

+	    $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f";

+	    $sql .= " WHERE f.fk_soc = s.rowid";

+	    $sql .= " AND f.fk_statut IN (1,2)";

+	    if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))

+	    	$sql .= " AND f.type IN (0,1,2)";

+		else

+			$sql .= " AND f.type IN (0,1,2,3)";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

+	}

+	elseif ($modecompta == 'RECETTES-DEPENSES')

+	{

+	    $sql = "SELECT s.nom as name, s.rowid as socid, sum(pf.amount) as amount_ttc";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";

+	    $sql .= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";

+	    $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f";

+	    $sql .= " ON pf.fk_facturefourn = f.rowid";

+	    $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s";

+	    $sql .= " ON f.fk_soc = s.rowid";

+	    $sql .= " WHERE p.rowid = pf.fk_paiementfourn ";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

 	}

 

 	$sql .= " AND f.entity = ".$conf->entity;

@@ -584,43 +494,41 @@
 	dol_syslog("get suppliers invoices", LOG_DEBUG);

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

 	if ($result) {

-		$num = $db->num_rows($result);

-		$i = 0;

-		if ($num > 0)

-		{

-			while ($i < $num)

-			{

-				$objp = $db->fetch_object($result);

-

-				print '<tr class="oddeven"><td>&nbsp;</td>';

-				print "<td>".$langs->trans("Bills")." <a href=\"".DOL_URL_ROOT."/fourn/facture/list.php?socid=".$objp->socid."\">".$objp->name."</a></td>\n";

-

-				if ($modecompta == 'CREANCES-DETTES')

-					print '<td class="right">'.price(-$objp->amount_ht)."</td>\n";

-				print '<td class="right">'.price(-$objp->amount_ttc)."</td>\n";

-

-				$total_ht -= (isset($objp->amount_ht) ? $objp->amount_ht : 0);

-				$total_ttc -= $objp->amount_ttc;

-				$subtotal_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);

-				$subtotal_ttc += $objp->amount_ttc;

-

-				print "</tr>\n";

-				$i++;

-			}

-		} else {

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';

-			print '</tr>';

-		}

-

-		$db->free($result);

+	    $num = $db->num_rows($result);

+	    $i = 0;

+	    if ($num > 0)

+	    {

+	        while ($i < $num)

+	        {

+	            $objp = $db->fetch_object($result);

+

+	            print '<tr class="oddeven"><td>&nbsp;</td>';

+	            print "<td>".$langs->trans("Bills")." <a href=\"".DOL_URL_ROOT."/fourn/facture/list.php?socid=".$objp->socid."\">".$objp->name."</a></td>\n";

+

+	            if ($modecompta == 'CREANCES-DETTES')

+	            	print '<td class="right">'.price(-$objp->amount_ht)."</td>\n";

+	            print '<td class="right">'.price(-$objp->amount_ttc)."</td>\n";

+

+	            $total_ht -= (isset($objp->amount_ht) ? $objp->amount_ht : 0);

+	            $total_ttc -= $objp->amount_ttc;

+	            $subtotal_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);

+	            $subtotal_ttc += $objp->amount_ttc;

+

+	            print "</tr>\n";

+	            $i++;

+	        }

+	    }

+	    else

+	    {

+	        print '<tr class="oddeven"><td>&nbsp;</td>';

+	        print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';

+	        print '</tr>';

+	    }

+

+	    $db->free($result);

 	} else {

-		dol_print_error($db);

-	}

-

-	$total_ht_outcome += $subtotal_ht;

-	$total_ttc_outcome += $subtotal_ttc;

-

+	    dol_print_error($db);

+	}

 	print '<tr class="liste_total">';

 	if ($modecompta == 'CREANCES-DETTES')

 		print '<td colspan="3" class="right">'.price(-$subtotal_ht).'</td>';

@@ -628,6 +536,7 @@
 	print '</tr>';

 

 

+

 	/*

 	 * Charges sociales non deductibles

 	 */

@@ -636,24 +545,25 @@
 

 	if ($modecompta == 'CREANCES-DETTES')

 	{

-		$sql = "SELECT c.id, c.libelle as label, sum(cs.amount) as amount";

-		$sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

-		$sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

-		$sql .= " WHERE cs.fk_type = c.id";

-		$sql .= " AND c.deductible = 0";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";

-	} elseif ($modecompta == 'RECETTES-DEPENSES')

-	{

-		$sql = "SELECT c.id, c.libelle as label, sum(p.amount) as amount";

-		$sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

-		$sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

-		$sql .= ", ".MAIN_DB_PREFIX."paiementcharge as p";

-		$sql .= " WHERE p.fk_charge = cs.rowid";

-		$sql .= " AND cs.fk_type = c.id";

-		$sql .= " AND c.deductible = 0";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+	    $sql = "SELECT c.id, c.libelle as label, sum(cs.amount) as amount";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

+	    $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

+	    $sql .= " WHERE cs.fk_type = c.id";

+	    $sql .= " AND c.deductible = 0";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";

+	}

+	elseif ($modecompta == 'RECETTES-DEPENSES')

+	{

+	    $sql = "SELECT c.id, c.libelle as label, sum(p.amount) as amount";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

+	    $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

+	    $sql .= ", ".MAIN_DB_PREFIX."paiementcharge as p";

+	    $sql .= " WHERE p.fk_charge = cs.rowid";

+	    $sql .= " AND cs.fk_type = c.id";

+	    $sql .= " AND c.deductible = 0";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

 	}

 	$sql .= " AND cs.entity = ".$conf->entity;

 	$sql .= " GROUP BY c.libelle, c.id";

@@ -670,35 +580,32 @@
 	$subtotal_ttc = 0;

 	if ($result) {

 		$num = $db->num_rows($result);

-		$i = 0;

-		if ($num) {

-			while ($i < $num) {

-				$obj = $db->fetch_object($result);

-

-				$total_ht -= $obj->amount;

-				$total_ttc -= $obj->amount;

-				$subtotal_ht += $obj->amount;

-				$subtotal_ttc += $obj->amount;

-

-				print '<tr class="oddeven"><td>&nbsp;</td>';

-				print '<td>'.$obj->label.'</td>';

-				if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price(-$obj->amount).'</td>';

-				print '<td class="right">'.price(-$obj->amount).'</td>';

-				print '</tr>';

-				$i++;

-			}

-		} else {

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';

-			print '</tr>';

-		}

+	    $i = 0;

+	    if ($num) {

+	        while ($i < $num) {

+	            $obj = $db->fetch_object($result);

+

+	            $total_ht -= $obj->amount;

+	            $total_ttc -= $obj->amount;

+	            $subtotal_ht += $obj->amount;

+	            $subtotal_ttc += $obj->amount;

+

+	            print '<tr class="oddeven"><td>&nbsp;</td>';

+	            print '<td>'.$obj->label.'</td>';

+	            if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price(-$obj->amount).'</td>';

+	            print '<td class="right">'.price(-$obj->amount).'</td>';

+	            print '</tr>';

+	            $i++;

+	        }

+	    }

+	    else {

+	        print '<tr class="oddeven"><td>&nbsp;</td>';

+	        print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';

+	        print '</tr>';

+	    }

 	} else {

-		dol_print_error($db);

-	}

-

-	$total_ht_outcome += $subtotal_ht;

-	$total_ttc_outcome += $subtotal_ttc;

-

+	    dol_print_error($db);

+	}

 	print '<tr class="liste_total">';

 	if ($modecompta == 'CREANCES-DETTES')

 		print '<td colspan="3" class="right">'.price(-$subtotal_ht).'</td>';

@@ -714,26 +621,27 @@
 

 	if ($modecompta == 'CREANCES-DETTES')

 	{

-		$sql = "SELECT c.id, c.libelle as label, sum(cs.amount) as amount";

-		$sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

-		$sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

-		$sql .= " WHERE cs.fk_type = c.id";

-		$sql .= " AND c.deductible = 1";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";

-		$sql .= " AND cs.entity = ".$conf->entity;

-	} elseif ($modecompta == 'RECETTES-DEPENSES')

-	{

-		$sql = "SELECT c.id, c.libelle as label, sum(p.amount) as amount";

-		$sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

-		$sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

-		$sql .= ", ".MAIN_DB_PREFIX."paiementcharge as p";

-		$sql .= " WHERE p.fk_charge = cs.rowid";

-		$sql .= " AND cs.fk_type = c.id";

-		$sql .= " AND c.deductible = 1";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

-		$sql .= " AND cs.entity = ".$conf->entity;

+	    $sql = "SELECT c.id, c.libelle as label, sum(cs.amount) as amount";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

+	    $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

+	    $sql .= " WHERE cs.fk_type = c.id";

+	    $sql .= " AND c.deductible = 1";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";

+	    $sql .= " AND cs.entity = ".$conf->entity;

+	}

+	elseif ($modecompta == 'RECETTES-DEPENSES')

+	{

+	    $sql = "SELECT c.id, c.libelle as label, sum(p.amount) as amount";

+	    $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

+	    $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

+	    $sql .= ", ".MAIN_DB_PREFIX."paiementcharge as p";

+	    $sql .= " WHERE p.fk_charge = cs.rowid";

+	    $sql .= " AND cs.fk_type = c.id";

+	    $sql .= " AND c.deductible = 1";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+	    $sql .= " AND cs.entity = ".$conf->entity;

 	}

 	$sql .= " GROUP BY c.libelle, c.id";

 	$newsortfield = $sortfield;

@@ -747,43 +655,58 @@
 	$subtotal_ht = 0;

 	$subtotal_ttc = 0;

 	if ($result) {

-		$num = $db->num_rows($result);

-		$i = 0;

-		if ($num) {

-			while ($i < $num) {

-				$obj = $db->fetch_object($result);

-

-				$total_ht -= $obj->amount;

-				$total_ttc -= $obj->amount;

-				$subtotal_ht += $obj->amount;

-				$subtotal_ttc += $obj->amount;

-

-				print '<tr class="oddeven"><td>&nbsp;</td>';

-				print '<td>'.$obj->label.'</td>';

-				if ($modecompta == 'CREANCES-DETTES')

-					print '<td class="right">'.price(-$obj->amount).'</td>';

-				print '<td class="right">'.price(-$obj->amount).'</td>';

-				print '</tr>';

-				$i++;

-			}

-		} else {

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';

-			print '</tr>';

-		}

+	    $num = $db->num_rows($result);

+	    $i = 0;

+	    if ($num) {

+	        while ($i < $num) {

+	            $obj = $db->fetch_object($result);

+

+	            $total_ht -= $obj->amount;

+	            $total_ttc -= $obj->amount;

+	            $subtotal_ht += $obj->amount;

+	            $subtotal_ttc += $obj->amount;

+

+	            print '<tr class="oddeven"><td>&nbsp;</td>';

+	            print '<td>'.$obj->label.'</td>';

+	            if ($modecompta == 'CREANCES-DETTES')

+	            	print '<td class="right">'.price(-$obj->amount).'</td>';

+	            print '<td class="right">'.price(-$obj->amount).'</td>';

+	            print '</tr>';

+	            $i++;

+	        }

+	    }

+	    else {

+	        print '<tr class="oddeven"><td>&nbsp;</td>';

+	        print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';

+	        print '</tr>';

+	    }

 	} else {

-		dol_print_error($db);

-	}

-

-	$total_ht_outcome += $subtotal_ht;

-	$total_ttc_outcome += $subtotal_ttc;

-

+	    dol_print_error($db);

+	}

 	print '<tr class="liste_total">';

 	if ($modecompta == 'CREANCES-DETTES')

 		print '<td colspan="3" class="right">'.price(-$subtotal_ht).'</td>';

 	print '<td colspan="3" class="right">'.price(-$subtotal_ttc).'</td>';

 	print '</tr>';

 

+	if ($mysoc->tva_assuj == 'franchise')	// Non assujetti

+	{

+	    // Total

+	    print '<tr>';

+	    print '<td colspan="4">&nbsp;</td>';

+	    print '</tr>';

+

+	    print '<tr class="liste_total"><td class="left" colspan="2">'.$langs->trans("Profit").'</td>';

+	    if ($modecompta == 'CREANCES-DETTES')

+	    	print '<td class="border right">'.price($total_ht).'</td>';

+	    print '<td class="right">'.price($total_ttc).'</td>';

+	    print '</tr>';

+

+	    print '<tr>';

+	    print '<td colspan="4">&nbsp;</td>';

+	    print '</tr>';

+	}

+

 

 	/*

 	 * Salaries

@@ -796,9 +719,9 @@
 	 	if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'RECETTES-DEPENSES')

 		{

 			if ($modecompta == 'CREANCES-DETTES') {

-				$column = 'p.datev';

+			    $column = 'p.datev';

 			} else {

-				$column = 'p.datep';

+			    $column = 'p.datep';

 			}

 

 			$sql = "SELECT u.rowid, u.firstname, u.lastname, p.fk_user, p.label as label, date_format($column,'%Y-%m') as dm, sum(p.amount) as amount";

@@ -810,9 +733,9 @@
 

 			$sql .= " GROUP BY u.rowid, u.firstname, u.lastname, p.fk_user, p.label, dm";

 			$newsortfield = $sortfield;

-			if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'u.firstname, u.lastname';

-			if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

-			if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

+		    if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'u.firstname, u.lastname';

+		    if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

+		    if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

 			$sql .= $db->order($newsortfield, $sortorder);

 		}

 

@@ -822,40 +745,40 @@
 		$subtotal_ttc = 0;

 		if ($result)

 		{

-			$num = $db->num_rows($result);

-			$i = 0;

-			if ($num)

-			{

-				while ($i < $num)

-				{

-					$obj = $db->fetch_object($result);

-

-					$total_ht -= $obj->amount;

-					$total_ttc -= $obj->amount;

-					$subtotal_ht += $obj->amount;

-					$subtotal_ttc += $obj->amount;

-

-					print '<tr class="oddeven"><td>&nbsp;</td>';

-

-					print "<td>".$langs->trans("Salary")." <a href=\"".DOL_URL_ROOT."/salaries/list.php?filtre=s.fk_user=".$obj->fk_user."\">".$obj->firstname." ".$obj->lastname."</a></td>\n";

-

-					if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price(-$obj->amount).'</td>';

-					print '<td class="right">'.price(-$obj->amount).'</td>';

-					print '</tr>';

-					$i++;

-				}

-			} else {

-				print '<tr class="oddeven"><td>&nbsp;</td>';

-				print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';

-				print '</tr>';

-			}

-		} else {

-			dol_print_error($db);

+		    $num = $db->num_rows($result);

+		    $i = 0;

+		    if ($num)

+		    {

+		        while ($i < $num)

+		        {

+		            $obj = $db->fetch_object($result);

+

+		            $total_ht -= $obj->amount;

+		            $total_ttc -= $obj->amount;

+		            $subtotal_ht += $obj->amount;

+		            $subtotal_ttc += $obj->amount;

+

+		            print '<tr class="oddeven"><td>&nbsp;</td>';

+

+		            print "<td>".$langs->trans("Salary")." <a href=\"".DOL_URL_ROOT."/salaries/list.php?filtre=s.fk_user=".$obj->fk_user."\">".$obj->firstname." ".$obj->lastname."</a></td>\n";

+

+		            if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price(-$obj->amount).'</td>';

+		            print '<td class="right">'.price(-$obj->amount).'</td>';

+		            print '</tr>';

+		            $i++;

+		        }

+		    }

+		    else

+		    {

+		        print '<tr class="oddeven"><td>&nbsp;</td>';

+		        print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';

+		        print '</tr>';

+		    }

 		}

-

-		$total_ht_outcome += $subtotal_ht;

-		$total_ttc_outcome += $subtotal_ttc;

-

+		else

+		{

+		    dol_print_error($db);

+		}

 		print '<tr class="liste_total">';

 		if ($modecompta == 'CREANCES-DETTES')

 			print '<td colspan="3" class="right">'.price(-$subtotal_ht).'</td>';

@@ -899,9 +822,9 @@
 			}

 

 			$sql .= " GROUP BY u.rowid, p.rowid, p.ref, u.firstname, u.lastname, dm";

-			$newsortfield = $sortfield;

-			if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'p.ref';

-			$sql .= $db->order($newsortfield, $sortorder);

+		    $newsortfield = $sortfield;

+		    if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'p.ref';

+		    $sql .= $db->order($newsortfield, $sortorder);

 		}

 

 		print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("ExpenseReport").'</td></tr>';

@@ -930,143 +853,216 @@
 					print '<td class="right">'.price(-$obj->amount_ttc).'</td>';

 					print '</tr>';

 				}

-			} else {

+			}

+			else

+			{

 				print '<tr class="oddeven"><td>&nbsp;</td>';

-				print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';

+				print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';

 				print '</tr>';

 			}

-		} else {

+		}

+		else

+		{

 			dol_print_error($db);

 		}

-

-		$total_ht_outcome += $subtotal_ht;

-		$total_ttc_outcome += $subtotal_ttc;

-

 		print '<tr class="liste_total">';

 		if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" class="right">'.price(-$subtotal_ht).'</td>';

 		print '<td colspan="3" class="right">'.price(-$subtotal_ttc).'</td>';

 		print '</tr>';

 	}

 

-

 	/*

+	 * Donations

+	 */

+

+	if (!empty($conf->don->enabled))

+	{

+		print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("Donations").'</td></tr>';

+

+		if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'RECETTES-DEPENSES')

+		{

+			if ($modecompta == 'CREANCES-DETTES')

+			{

+		    	$sql = "SELECT p.societe as name, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";

+		    	$sql .= " FROM ".MAIN_DB_PREFIX."don as p";

+		    	$sql .= " WHERE p.entity IN (".getEntity('donation').")";

+		    	$sql .= " AND fk_statut in (1,2)";

+			}

+			else

+			{

+			    $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";

+			    $sql .= " FROM ".MAIN_DB_PREFIX."don as p";

+			    $sql .= " INNER JOIN ".MAIN_DB_PREFIX."payment_donation as pe ON pe.fk_donation = p.rowid";

+			    $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";

+			    $sql .= " WHERE p.entity IN (".getEntity('donation').")";

+			    $sql .= " AND fk_statut >= 2";

+			}

+			if (!empty($date_start) && !empty($date_end))

+				$sql .= " AND p.datedon >= '".$db->idate($date_start)."' AND p.datedon <= '".$db->idate($date_end)."'";

+		}

+		$sql .= " GROUP BY p.societe, p.firstname, p.lastname, dm";

+	    $newsortfield = $sortfield;

+	    if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'p.societe, p.firstname, p.lastname, dm';

+	    if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

+	    if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

+	    $sql .= $db->order($newsortfield, $sortorder);

+

+		dol_syslog("get dunning");

+		$result = $db->query($sql);

+		$subtotal_ht = 0;

+		$subtotal_ttc = 0;

+		if ($result)

+		{

+			$num = $db->num_rows($result);

+			$i = 0;

+			if ($num)

+			{

+				while ($i < $num)

+				{

+					$obj = $db->fetch_object($result);

+

+					$total_ht += $obj->amount;

+					$total_ttc += $obj->amount;

+					$subtotal_ht += $obj->amount;

+					$subtotal_ttc += $obj->amount;

+

+					print '<tr class="oddeven"><td>&nbsp;</td>';

+

+					print "<td>".$langs->trans("Donation")." <a href=\"".DOL_URL_ROOT."/don/list.php?search_company=".$obj->name."&search_name=".$obj->firstname." ".$obj->lastname."\">".$obj->name." ".$obj->firstname." ".$obj->lastname."</a></td>\n";

+

+					if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price($obj->amount).'</td>';

+					print '<td class="right">'.price($obj->amount).'</td>';

+					print '</tr>';

+					$i++;

+				}

+			}

+			else

+			{

+				print '<tr class="oddeven"><td>&nbsp;</td>';

+				print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';

+				print '</tr>';

+			}

+		}

+		else

+		{

+			dol_print_error($db);

+		}

+		print '<tr class="liste_total">';

+		if ($modecompta == 'CREANCES-DETTES')

+			print '<td colspan="3" class="right">'.price($subtotal_ht).'</td>';

+		print '<td colspan="3" class="right">'.price($subtotal_ttc).'</td>';

+		print '</tr>';

+	}

+

+    /*

      * Various Payments

      */

-	//$conf->global->ACCOUNTING_REPORTS_INCLUDE_VARPAY = 1;

-

-	if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_VARPAY) && !empty($conf->banque->enabled) && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES"))

-	{

-		$subtotal_ht = 0;

-		$subtotal_ttc = 0;

-

-		print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("VariousPayment").'</td></tr>';

-

-		// Debit

-		$sql = "SELECT SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various as p";

-		$sql .= ' WHERE 1 = 1';

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

-		$sql .= ' GROUP BY p.sens';

-		$sql .= ' ORDER BY p.sens';

-

-		dol_syslog('get various payments', LOG_DEBUG);

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

-		if ($result)

-		{

-			// Debit (payment of suppliers for example)

-			$obj = $db->fetch_object($result);

-			if (isset($obj->amount))

-			{

-				$subtotal_ht += -$obj->amount;

-				$subtotal_ttc += -$obj->amount;

-

-				$total_ht_outcome += $obj->amount;

-				$total_ttc_outcome += $obj->amount;

-			}

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print "<td>".$langs->trans("Debit")."</td>\n";

-			if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price(-$obj->amount).'</td>';

-			print '<td class="right">'.price(-$obj->amount)."</td>\n";

-			print "</tr>\n";

-

-			// Credit (payment received from customer for example)

-			$obj = $db->fetch_object($result);

-			if (isset($obj->amount))

-			{

-				$subtotal_ht += $obj->amount;

-				$subtotal_ttc += $obj->amount;

-

-				$total_ht_income += $obj->amount;

-				$total_ttc_income += $obj->amount;

-			}

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print "<td>".$langs->trans("Credit")."</td>\n";

-			if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price($obj->amount).'</td>';

-			print '<td class="right">'.price($obj->amount)."</td>\n";

-			print "</tr>\n";

-

-			// Total

-			$total_ht += $subtotal_ht;

-			$total_ttc += $subtotal_ttc;

-			print '<tr class="liste_total">';

-			if ($modecompta == 'CREANCES-DETTES')

-				print '<td colspan="3" class="right">'.price($subtotal_ht).'</td>';

-			print '<td colspan="3" class="right">'.price($subtotal_ttc).'</td>';

-			print '</tr>';

-		} else dol_print_error($db);

-	}

-

-	/*

+

+    if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_VARPAY) && !empty($conf->banque->enabled) && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES"))

+    {

+        $subtotal_ht = 0;

+        $subtotal_ttc = 0;

+

+        print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("VariousPayment").'</td></tr>';

+

+        // Debit

+        $sql = "SELECT SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various as p";

+        $sql .= ' WHERE 1 = 1';

+        if (!empty($date_start) && !empty($date_end))

+            $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+        $sql .= ' GROUP BY p.sens';

+        $sql .= ' ORDER BY p.sens';

+

+        dol_syslog('get various payments', LOG_DEBUG);

+        $result = $db->query($sql);

+        if ($result)

+        {

+            // Debit

+            $obj = $db->fetch_object($result);

+            if (isset($obj->amount))

+            {

+                $subtotal_ht += -$obj->amount;

+                $subtotal_ttc += -$obj->amount;

+            }

+            print '<tr class="oddeven"><td>&nbsp;</td>';

+            print "<td>".$langs->trans("Debit")."</td>\n";

+            if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price(-$obj->amount).'</td>';

+            print '<td class="right">'.price(-$obj->amount)."</td>\n";

+            print "</tr>\n";

+

+            // Credit

+            $obj = $db->fetch_object($result);

+            if (isset($obj->amount))

+            {

+                $subtotal_ht += $obj->amount;

+                $subtotal_ttc += $obj->amount;

+            }

+            print '<tr class="oddeven"><td>&nbsp;</td>';

+            print "<td>".$langs->trans("Credit")."</td>\n";

+            if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price($obj->amount).'</td>';

+            print '<td class="right">'.price($obj->amount)."</td>\n";

+            print "</tr>\n";

+

+            // Total

+            $total_ht += $subtotal_ht;

+            $total_ttc += $subtotal_ttc;

+            print '<tr class="liste_total">';

+            if ($modecompta == 'CREANCES-DETTES')

+                print '<td colspan="3" class="right">'.price($subtotal_ht).'</td>';

+            print '<td colspan="3" class="right">'.price($subtotal_ttc).'</td>';

+            print '</tr>';

+        }

+        else dol_print_error($db);

+    }

+

+    /*

      * Payement Loan

      */

 

-	if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_LOAN) && !empty($conf->loan->enabled) && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES"))

-	{

-		$subtotal_ht = 0;

-		$subtotal_ttc = 0;

-

-		print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("PaymentLoan").'</td></tr>';

-

-		$sql = 'SELECT l.rowid as id, l.label AS label, SUM(p.amount_capital + p.amount_insurance + p.amount_interest) as amount FROM '.MAIN_DB_PREFIX.'payment_loan as p';

-		$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'loan AS l ON l.rowid = p.fk_loan';

-		$sql .= ' WHERE 1 = 1';

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

-		$sql .= ' GROUP BY p.fk_loan';

-		$sql .= ' ORDER BY p.fk_loan';

-

-		dol_syslog('get loan payments', LOG_DEBUG);

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

-		if ($result)

-		{

-			require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';

-			$loan_static = new Loan($db);

-			while ($obj = $db->fetch_object($result))

-			{

-				$loan_static->id = $obj->id;

-				$loan_static->ref = $obj->id;

-				$loan_static->label = $obj->label;

-				print '<tr class="oddeven"><td>&nbsp;</td>';

-				print "<td>".$loan_static->getNomUrl(1).' - '.$obj->label."</td>\n";

-				if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price(-$obj->amount).'</td>';

-				print '<td class="right">'.price(-$obj->amount)."</td>\n";

-				print "</tr>\n";

-				$subtotal_ht -= $obj->amount;

-				$subtotal_ttc -= $obj->amount;

-			}

-			$total_ht += $subtotal_ht;

-			$total_ttc += $subtotal_ttc;

-

-			$total_ht_income += $subtotal_ht;

-			$total_ttc_income += $subtotal_ttc;

-

-			print '<tr class="liste_total">';

-			if ($modecompta == 'CREANCES-DETTES')

-				print '<td colspan="3" class="right">'.price($subtotal_ht).'</td>';

-			print '<td colspan="3" class="right">'.price($subtotal_ttc).'</td>';

-			print '</tr>';

-		} else dol_print_error($db);

-	}

+    if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_LOAN) && !empty($conf->loan->enabled) && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES"))

+    {

+        $subtotal_ht = 0;

+        $subtotal_ttc = 0;

+

+        print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("PaymentLoan").'</td></tr>';

+

+        $sql = 'SELECT l.rowid as id, l.label AS label, SUM(p.amount_capital + p.amount_insurance + p.amount_interest) as amount FROM '.MAIN_DB_PREFIX.'payment_loan as p';

+        $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'loan AS l ON l.rowid = p.fk_loan';

+        $sql .= ' WHERE 1 = 1';

+        if (!empty($date_start) && !empty($date_end))

+            $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+        $sql .= ' GROUP BY p.fk_loan';

+        $sql .= ' ORDER BY p.fk_loan';

+

+        dol_syslog('get loan payments', LOG_DEBUG);

+        $result = $db->query($sql);

+        if ($result)

+        {

+            require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';

+            $loan_static = new Loan($db);

+            while ($obj = $db->fetch_object($result))

+            {

+                $loan_static->id = $obj->id;

+                $loan_static->ref = $obj->id;

+                $loan_static->label = $obj->label;

+                print '<tr class="oddeven"><td>&nbsp;</td>';

+                print "<td>".$loan_static->getNomUrl(1).' - '.$obj->label."</td>\n";

+                if ($modecompta == 'CREANCES-DETTES') print '<td class="right">'.price(-$obj->amount).'</td>';

+                print '<td class="right">'.price(-$obj->amount)."</td>\n";

+                print "</tr>\n";

+                $subtotal_ht -= $obj->amount;

+                $subtotal_ttc -= $obj->amount;

+            }

+            $total_ht += $subtotal_ht;

+            $total_ttc += $subtotal_ttc;

+            print '<tr class="liste_total">';

+            if ($modecompta == 'CREANCES-DETTES')

+                print '<td colspan="3" class="right">'.price($subtotal_ht).'</td>';

+            print '<td colspan="3" class="right">'.price($subtotal_ttc).'</td>';

+            print '</tr>';

+        }

+        else dol_print_error($db);

+    }

 

 	/*

 	 * VAT

@@ -1080,214 +1076,204 @@
 	{

 		if ($modecompta == 'CREANCES-DETTES')

 		{

-			// VAT to pay

-			$amount = 0;

-			$sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.tva) as amount";

-			$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";

-			$sql .= " WHERE f.fk_statut IN (1,2)";

-			if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))

-				$sql .= " AND f.type IN (0,1,2,5)";

-			else $sql .= " AND f.type IN (0,1,2,3,5)";

-			if (!empty($date_start) && !empty($date_end))

-				$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

-			$sql .= " AND f.entity IN (".getEntity('invoice').")";

-			$sql .= " GROUP BY dm";

-			$newsortfield = $sortfield;

-			if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';

-			if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

-			if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

-			$sql .= $db->order($newsortfield, $sortorder);

-

-			dol_syslog("get vat to pay", LOG_DEBUG);

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

-			if ($result)

-			{

-				$num = $db->num_rows($result);

-				$i = 0;

-				if ($num)

-				{

-					while ($i < $num)

-					{

-						$obj = $db->fetch_object($result);

-

-						$amount -= $obj->amount;

-						//$total_ht -= $obj->amount;

-						$total_ttc -= $obj->amount;

-						//$subtotal_ht -= $obj->amount;

-						$subtotal_ttc -= $obj->amount;

-						$i++;

-					}

-				}

-			} else {

-				dol_print_error($db);

-			}

-

-			$total_ht_outcome -= 0;

-			$total_ttc_outcome -= $amount;

-

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print "<td>".$langs->trans("VATToPay")."</td>\n";

-			print '<td class="right">&nbsp;</td>'."\n";

-			print '<td class="right">'.price($amount)."</td>\n";

-			print "</tr>\n";

-

-			// VAT to retreive

-			$amount = 0;

-			$sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_tva) as amount";

-			$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";

-			$sql .= " WHERE f.fk_statut IN (1,2)";

-			if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))

-				$sql .= " AND f.type IN (0,1,2)";

-			else $sql .= " AND f.type IN (0,1,2,3)";

-			if (!empty($date_start) && !empty($date_end))

-				$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

-			$sql .= " AND f.entity = ".$conf->entity;

-			$sql .= " GROUP BY dm";

-			$newsortfield = $sortfield;

-			if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';

-			if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

-			if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

-			$sql .= $db->order($newsortfield, $sortorder);

-

-			dol_syslog("get vat received back", LOG_DEBUG);

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

-			if ($result)

-			{

-				$num = $db->num_rows($result);

-				$i = 0;

-				if ($num)

-				{

-					while ($i < $num)

-					{

-						$obj = $db->fetch_object($result);

-

-						$amount += $obj->amount;

-						//$total_ht += $obj->amount;

-						$total_ttc += $obj->amount;

-						//$subtotal_ht += $obj->amount;

-						$subtotal_ttc += $obj->amount;

-

-						$i++;

-					}

-				}

-			} else {

-				dol_print_error($db);

-			}

-

-			$total_ht_income += 0;

-			$total_ttc_income += $amount;

-

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print '<td>'.$langs->trans("VATToCollect")."</td>\n";

-			print '<td class="right">&nbsp;</td>'."\n";

-			print '<td class="right">'.price($amount)."</td>\n";

-			print "</tr>\n";

-		} else {

-			// VAT really already paid

-			$amount = 0;

-			$sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";

-			$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";

-			$sql .= " WHERE amount > 0";

-			if (!empty($date_start) && !empty($date_end))

-				$sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";

-			$sql .= " AND t.entity = ".$conf->entity;

-			$sql .= " GROUP BY dm";

-			$newsortfield = $sortfield;

-			if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';

-			if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

-			if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

-			$sql .= $db->order($newsortfield, $sortorder);

-

-			dol_syslog("get vat really paid", LOG_DEBUG);

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

-			if ($result) {

-				$num = $db->num_rows($result);

-				$i = 0;

-				if ($num) {

-					while ($i < $num) {

-						$obj = $db->fetch_object($result);

-

-						$amount -= $obj->amount;

-						$total_ht -= $obj->amount;

-						$total_ttc -= $obj->amount;

-						$subtotal_ht -= $obj->amount;

-						$subtotal_ttc -= $obj->amount;

-

-						$i++;

-					}

-				}

-				$db->free($result);

-			} else {

-				dol_print_error($db);

-			}

-

-			$total_ht_outcome -= 0;

-			$total_ttc_outcome -= $amount;

-

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print "<td>".$langs->trans("VATPaid")."</td>\n";

-			if ($modecompta == 'CREANCES-DETTES')

-				print '<td <class="right"></td>'."\n";

-			print '<td class="right">'.price($amount)."</td>\n";

-			print "</tr>\n";

-

-			// VAT really received

-			$amount = 0;

-			$sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";

-			$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";

-			$sql .= " WHERE amount < 0";

-			if (!empty($date_start) && !empty($date_end))

-				$sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";

-			$sql .= " AND t.entity = ".$conf->entity;

-			$sql .= " GROUP BY dm";

-			$newsortfield = $sortfield;

-			if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';

-			if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

-			if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

-			$sql .= $db->order($newsortfield, $sortorder);

-

-			dol_syslog("get vat really received back", LOG_DEBUG);

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

-			if ($result) {

-				$num = $db->num_rows($result);

-				$i = 0;

-				if ($num) {

-					while ($i < $num) {

-						$obj = $db->fetch_object($result);

-

-						$amount += -$obj->amount;

-						$total_ht += -$obj->amount;

-						$total_ttc += -$obj->amount;

-						$subtotal_ht += -$obj->amount;

-						$subtotal_ttc += -$obj->amount;

-

-						$i++;

-					}

-				}

-				$db->free($result);

-			} else {

-				dol_print_error($db);

-			}

-

-			$total_ht_income += 0;

-			$total_ttc_income += $amount;

-

-			print '<tr class="oddeven"><td>&nbsp;</td>';

-			print "<td>".$langs->trans("VATCollected")."</td>\n";

-			if ($modecompta == 'CREANCES-DETTES')

-				print '<td class="right"></td>'."\n";

-			print '<td class="right">'.price($amount)."</td>\n";

-			print "</tr>\n";

+		    // TVA a payer

+		    $amount = 0;

+		    $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.tva) as amount";

+		    $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";

+		    $sql .= " WHERE f.fk_statut IN (1,2)";

+		    if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))

+		    	$sql .= " AND f.type IN (0,1,2,5)";

+			else

+				$sql .= " AND f.type IN (0,1,2,3,5)";

+		    if (!empty($date_start) && !empty($date_end))

+		    	$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

+		    $sql .= " AND f.entity IN (".getEntity('invoice').")";

+		    $sql .= " GROUP BY dm";

+		    $newsortfield = $sortfield;

+		    if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';

+		    if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

+		    if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

+		    $sql .= $db->order($newsortfield, $sortorder);

+

+		    dol_syslog("get vat to pay", LOG_DEBUG);

+		    $result = $db->query($sql);

+		    if ($result)

+		    {

+		        $num = $db->num_rows($result);

+		        $i = 0;

+		        if ($num)

+		        {

+		            while ($i < $num)

+		            {

+		                $obj = $db->fetch_object($result);

+

+		                $amount -= $obj->amount;

+		                //$total_ht -= $obj->amount;

+		                $total_ttc -= $obj->amount;

+		                //$subtotal_ht -= $obj->amount;

+		                $subtotal_ttc -= $obj->amount;

+		                $i++;

+		            }

+		        }

+		    } else {

+		        dol_print_error($db);

+		    }

+		    print '<tr class="oddeven"><td>&nbsp;</td>';

+		    print "<td>".$langs->trans("VATToPay")."</td>\n";

+		    print '<td class="right">&nbsp;</td>'."\n";

+		    print '<td class="right">'.price($amount)."</td>\n";

+		    print "</tr>\n";

+

+		    // TVA a recuperer

+		    $amount = 0;

+		    $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_tva) as amount";

+		    $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";

+		    $sql .= " WHERE f.fk_statut IN (1,2)";

+		    if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))

+		    	$sql .= " AND f.type IN (0,1,2)";

+			else

+				$sql .= " AND f.type IN (0,1,2,3)";

+		    if (!empty($date_start) && !empty($date_end))

+		    	$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

+		    $sql .= " AND f.entity = ".$conf->entity;

+		    $sql .= " GROUP BY dm";

+		    $newsortfield = $sortfield;

+		    if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';

+		    if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

+		    if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

+		    $sql .= $db->order($newsortfield, $sortorder);

+

+		    dol_syslog("get vat received back", LOG_DEBUG);

+		    $result = $db->query($sql);

+		    if ($result)

+		    {

+		        $num = $db->num_rows($result);

+		        $i = 0;

+		        if ($num)

+		        {

+		            while ($i < $num)

+		            {

+		                $obj = $db->fetch_object($result);

+

+		                $amount += $obj->amount;

+		                //$total_ht += $obj->amount;

+		                $total_ttc += $obj->amount;

+		                //$subtotal_ht += $obj->amount;

+		                $subtotal_ttc += $obj->amount;

+

+		                $i++;

+		            }

+		        }

+		    } else {

+		        dol_print_error($db);

+		    }

+		    print '<tr class="oddeven"><td>&nbsp;</td>';

+		    print '<td>'.$langs->trans("VATToCollect")."</td>\n";

+		    print '<td class="right">&nbsp;</td>'."\n";

+		    print '<td class="right">'.price($amount)."</td>\n";

+		    print "</tr>\n";

 		}

-	}

-

-	if ($mysoc->tva_assuj != '0')	// Assujetti

-	{

-		print '<tr class="liste_total">';

-		if ($modecompta == 'CREANCES-DETTES')

-			print '<td colspan="3" class="right">&nbsp;</td>';

-		print '<td colspan="3" class="right">'.price(price2num($subtotal_ttc, 'MT')).'</td>';

-		print '</tr>';

+		else

+		{

+		    // VAT really already paid

+		    $amount = 0;

+		    $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";

+		    $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";

+		    $sql .= " WHERE amount > 0";

+		    if (!empty($date_start) && !empty($date_end))

+		    	$sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";

+		    $sql .= " AND t.entity = ".$conf->entity;

+		    $sql .= " GROUP BY dm";

+		    $newsortfield = $sortfield;

+		    if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';

+		    if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

+		    if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

+		    $sql .= $db->order($newsortfield, $sortorder);

+

+		    dol_syslog("get vat really paid", LOG_DEBUG);

+		    $result = $db->query($sql);

+		    if ($result) {

+		        $num = $db->num_rows($result);

+		        $i = 0;

+		        if ($num) {

+		            while ($i < $num) {

+		                $obj = $db->fetch_object($result);

+

+		                $amount -= $obj->amount;

+		                $total_ht -= $obj->amount;

+		                $total_ttc -= $obj->amount;

+		                $subtotal_ht -= $obj->amount;

+		                $subtotal_ttc -= $obj->amount;

+

+		                $i++;

+		            }

+		        }

+		        $db->free($result);

+		    } else {

+		        dol_print_error($db);

+		    }

+		    print '<tr class="oddeven"><td>&nbsp;</td>';

+		    print "<td>".$langs->trans("VATPaid")."</td>\n";

+		    if ($modecompta == 'CREANCES-DETTES')

+		    	print '<td <class="right">'.price($amount)."</td>\n";

+		    print '<td class="right">'.price($amount)."</td>\n";

+		    print "</tr>\n";

+

+		    // VAT really received

+		    $amount = 0;

+		    $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";

+		    $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";

+		    $sql .= " WHERE amount < 0";

+		    if (!empty($date_start) && !empty($date_end))

+		    	$sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";

+		    $sql .= " AND t.entity = ".$conf->entity;

+		    $sql .= " GROUP BY dm";

+		    $newsortfield = $sortfield;

+		    if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';

+		    if ($newsortfield == 'amount_ht') $newsortfield = 'amount';

+		    if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';

+		    $sql .= $db->order($newsortfield, $sortorder);

+

+		    dol_syslog("get vat really received back", LOG_DEBUG);

+		    $result = $db->query($sql);

+		    if ($result) {

+		        $num = $db->num_rows($result);

+		        $i = 0;

+		        if ($num) {

+		            while ($i < $num) {

+		                $obj = $db->fetch_object($result);

+

+		                $amount += -$obj->amount;

+		                $total_ht += -$obj->amount;

+		                $total_ttc += -$obj->amount;

+		                $subtotal_ht += -$obj->amount;

+		                $subtotal_ttc += -$obj->amount;

+

+		                $i++;

+		            }

+		        }

+		        $db->free($result);

+		    }

+		    else

+		    {

+		        dol_print_error($db);

+		    }

+		    print '<tr class="oddeven"><td>&nbsp;</td>';

+		    print "<td>".$langs->trans("VATCollected")."</td>\n";

+		    if ($modecompta == 'CREANCES-DETTES')

+		    	print '<td class="right">'.price($amount)."</td>\n";

+		    print '<td class="right">'.price($amount)."</td>\n";

+		    print "</tr>\n";

+		}

+	}

+

+	if ($mysoc->tva_assuj != 'franchise')	// Assujetti

+	{

+	    print '<tr class="liste_total">';

+	    if ($modecompta == 'CREANCES-DETTES')

+	    	print '<td colspan="3" class="right">&nbsp;</td>';

+	    print '<td colspan="3" class="right">'.price(price2num($subtotal_ttc, 'MT')).'</td>';

+	    print '</tr>';

 	}

 }

 

@@ -1296,34 +1282,25 @@
 $parameters["mode"] = $modecompta;

 $parameters["date_start"] = $date_start;

 $parameters["date_end"] = $date_end;

+$parameters["bc"] = $bc;

 // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array

 $hookmanager->initHooks(array('externalbalance'));

 $reshook = $hookmanager->executeHooks('addBalanceLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks

 print $hookmanager->resPrint;

 

-// Total

-print '<tr>';

-print '<td colspan="4">&nbsp;</td>';

-print '</tr>';

-

-print '<tr class="liste_total"><td class="left" colspan="2">'.$langs->trans("Income").'</td>';

-if ($modecompta == 'CREANCES-DETTES') {

-	print '<td class="liste_total right">'.price(price2num($total_ht_income, 'MT')).'</td>';

+if ($mysoc->tva_assuj != 'franchise')	// Assujetti

+{

+    // Total

+    print '<tr>';

+    print '<td colspan="4">&nbsp;</td>';

+    print '</tr>';

+

+    print '<tr class="liste_total"><td class="left" colspan="2">'.$langs->trans("Profit").'</td>';

+    if ($modecompta == 'CREANCES-DETTES')

+    	print '<td class="liste_total right">'.price(price2num($total_ht, 'MT')).'</td>';

+    print '<td class="liste_total right">'.price(price2num($total_ttc, 'MT')).'</td>';

+    print '</tr>';

 }

-print '<td class="liste_total right">'.price(price2num($total_ttc_income, 'MT')).'</td>';

-print '</tr>';

-print '<tr class="liste_total"><td class="left" colspan="2">'.$langs->trans("Outcome").'</td>';

-if ($modecompta == 'CREANCES-DETTES') {

-	print '<td class="liste_total right">'.price(price2num(-$total_ht_outcome, 'MT')).'</td>';

-}

-print '<td class="liste_total right">'.price(price2num(-$total_ttc_outcome, 'MT')).'</td>';

-print '</tr>';

-print '<tr class="liste_total"><td class="left" colspan="2">'.$langs->trans("Profit").'</td>';

-if ($modecompta == 'CREANCES-DETTES') {

-	print '<td class="liste_total right">'.price(price2num($total_ht, 'MT')).'</td>';

-}

-print '<td class="liste_total right">'.price(price2num($total_ttc, 'MT')).'</td>';

-print '</tr>';

 

 print "</table>";

 print '<br>';

--- /tmp/dsg/dolibarr/htdocs/compta/resultat/github_index.php
+++ /tmp/dsg/dolibarr/htdocs/compta/resultat/client_index.php
@@ -78,7 +78,8 @@
 			$month_end = $month_start - 1;

 			if ($month_end < 1) $month_end = 12;

 			else $year_end++;

-		} else $month_end = $month_start;

+		}

+		else $month_end = $month_start;

 		$date_start = dol_get_first_day($year_start, $month_start, false); $date_end = dol_get_last_day($year_end, $month_end, false);

 	}

 	if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); }

@@ -123,43 +124,35 @@
 {

 	$name = $langs->trans("ReportInOut").', '.$langs->trans("ByYear");

 	$calcmode = $langs->trans("CalcModeDebt");

-	$calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '{s1}', '{s2}').')';

-	$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">', '</a>'), $calcmode);

-	if (!empty($conf->accounting->enabled)) {

-		$calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{s1}', '{s2}').')';

-		$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=BOOKKEEPING">', '</a>'), $calcmode);

-	}

+	$calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">', '</a>').')';

+	if (!empty($conf->accounting->enabled)) $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=BOOKKEEPING">', '</a>').')';

 	$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);

 	$periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");

 	$description = $langs->trans("RulesAmountWithTaxIncluded");

 	$description .= '<br>'.$langs->trans("RulesResultDue");

 	if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description .= "<br>".$langs->trans("DepositsAreNotIncluded");

-	else $description .= "<br>".$langs->trans("DepositsAreIncluded");

+	else  $description .= "<br>".$langs->trans("DepositsAreIncluded");

 	$builddate = dol_now();

 	//$exportlink=$langs->trans("NotYetAvailable");

-} elseif ($modecompta == "RECETTES-DEPENSES") {

+}

+elseif ($modecompta == "RECETTES-DEPENSES") {

 	$name = $langs->trans("ReportInOut").', '.$langs->trans("ByYear");

 	$calcmode = $langs->trans("CalcModeEngagement");

-	$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{s1}', '{s2}').')';

-	$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">', '</a>'), $calcmode);

-	if (!empty($conf->accounting->enabled)) {

-		$calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{s1}', '{s2}').')';

-		$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=BOOKKEEPING">', '</a>'), $calcmode);

-	}

+	$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">', '</a>').')';

+	if (!empty($conf->accounting->enabled)) $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=BOOKKEEPING">', '</a>').')';

 	$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);

 	$periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");

 	$description = $langs->trans("RulesAmountWithTaxIncluded");

 	$description .= '<br>'.$langs->trans("RulesResultInOut");

 	$builddate = dol_now();

 	//$exportlink=$langs->trans("NotYetAvailable");

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	$name = $langs->trans("ReportInOut").', '.$langs->trans("ByYear");

 	$calcmode = $langs->trans("CalcModeBookkeeping");

-	$calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '{s1}', '{s2}').')';

-	$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">', '</a>'), $calcmode);

-	$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{s1}', '{s2}').')';

-	$calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">', '</a>'), $calcmode);

+	$calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">', '</a>').')';

+	$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">', '</a>').')';

 	$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);

 	$periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");

 	$description = $langs->trans("RulesAmountOnInOutBookkeepingRecord");

@@ -174,7 +167,7 @@
 

 if (!empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING')

 {

-	print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);

+    print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);

 }

 

 

@@ -196,9 +189,10 @@
 		$sql .= " AND f.fk_statut IN (1,2)";

 		if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";

 		else $sql .= " AND f.type IN (0,1,2,3,5)";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

-	} elseif ($modecompta == "RECETTES-DEPENSES")

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

+	}

+	elseif ($modecompta == "RECETTES-DEPENSES")

 	{

 		/*

 		 * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les

@@ -210,8 +204,8 @@
 		$sql .= ", ".MAIN_DB_PREFIX."paiement as p";

 		$sql .= " WHERE p.rowid = pf.fk_paiement";

 		$sql .= " AND pf.fk_facture = f.rowid";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+	    if (!empty($date_start) && !empty($date_end))

+	    	$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

 	}

 

 	$sql .= " AND f.entity IN (".getEntity('invoice').")";

@@ -234,10 +228,12 @@
 			$i++;

 		}

 		$db->free($result);

-	} else {

+	}

+	else {

 		dol_print_error($db);

 	}

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	// Nothing from this table

 }

@@ -256,8 +252,8 @@
 		$sql .= " AND p.fk_bank = b.rowid";

 		$sql .= " AND b.fk_account = ba.rowid";

 		$sql .= " AND ba.entity IN (".getEntity('bank_account').")";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

 		$sql .= " GROUP BY dm";

 		$sql .= " ORDER BY dm";

 

@@ -278,14 +274,17 @@
 

 				$i++;

 			}

-		} else {

+		}

+		else {

 			dol_print_error($db);

 		}

-	} elseif ($modecompta == "RECETTES-DEPENSES")

+	}

+	elseif ($modecompta == "RECETTES-DEPENSES")

 	{

 		// Nothing from this table

 	}

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	// Nothing from this table

 }

@@ -306,9 +305,10 @@
 		$sql .= " WHERE f.fk_statut IN (1,2)";

 		if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2)";

 		else $sql .= " AND f.type IN (0,1,2,3)";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

-	} elseif ($modecompta == "RECETTES-DEPENSES")

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

+	}

+	elseif ($modecompta == "RECETTES-DEPENSES")

 	{

 		$sql = "SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";

 		$sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";

@@ -316,8 +316,8 @@
 		$sql .= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";

 		$sql .= " WHERE f.rowid = pf.fk_facturefourn";

 		$sql .= " AND p.rowid = pf.fk_paiementfourn";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

 	}

 

 	$sql .= " AND f.entity = ".$conf->entity;

@@ -343,10 +343,12 @@
 			$i++;

 		}

 		$db->free($result);

-	} else {

+	}

+	else {

 		dol_print_error($db);

 	}

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	// Nothing from this table

 }

@@ -370,8 +372,8 @@
 		if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";

 		else $sql .= " AND f.type IN (0,1,2,3,5)";

 		$sql .= " AND f.entity IN (".getEntity('invoice').")";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

 		$sql .= " GROUP BY dm";

 

 		dol_syslog("get vat to pay", LOG_DEBUG);

@@ -402,8 +404,8 @@
 		if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2)";

 		else $sql .= " AND f.type IN (0,1,2,3)";

 		$sql .= " AND f.entity = ".$conf->entity;

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

 		$sql .= " GROUP BY dm";

 

 		dol_syslog("get vat to receive back", LOG_DEBUG);

@@ -427,15 +429,16 @@
 		} else {

 			dol_print_error($db);

 		}

-	} elseif ($modecompta == "RECETTES-DEPENSES")

+	}

+	elseif ($modecompta == "RECETTES-DEPENSES")

 	{

 		// TVA really already paid

 		$sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";

 		$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";

 		$sql .= " WHERE amount > 0";

 		$sql .= " AND t.entity = ".$conf->entity;

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";

 		$sql .= " GROUP BY dm";

 

 		dol_syslog("get vat really paid", LOG_DEBUG);

@@ -459,13 +462,13 @@
 		} else {

 			dol_print_error($db);

 		}

-		// TVA retrieved

+		// TVA retreived

 		$sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";

 		$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";

 		$sql .= " WHERE amount < 0";

 		$sql .= " AND t.entity = ".$conf->entity;

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";

 		$sql .= " GROUP BY dm";

 

 		dol_syslog("get vat really received back", LOG_DEBUG);

@@ -490,7 +493,8 @@
 			dol_print_error($db);

 		}

 	}

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	// Nothing from this table

 }

@@ -509,9 +513,10 @@
 		$sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

 		$sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";

 		$sql .= " WHERE cs.fk_type = c.id";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";

-	} elseif ($modecompta == "RECETTES-DEPENSES")

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";

+	}

+	elseif ($modecompta == "RECETTES-DEPENSES")

 	{

 		$sql = "SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount";

 		$sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";

@@ -519,8 +524,8 @@
 		$sql .= ", ".MAIN_DB_PREFIX."paiementcharge as p";

 		$sql .= " WHERE p.fk_charge = cs.rowid";

 		$sql .= " AND cs.fk_type = c.id";

-		if (!empty($date_start) && !empty($date_end))

-			$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+    	if (!empty($date_start) && !empty($date_end))

+    		$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

 	}

 

 	$sql .= " AND cs.entity = ".$conf->entity;

@@ -547,7 +552,8 @@
 	} else {

 		dol_print_error($db);

 	}

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	// Nothing from this table

 }

@@ -594,7 +600,8 @@
 	} else {

 		dol_print_error($db);

 	}

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	// Nothing from this table

 }

@@ -652,10 +659,13 @@
 				$decaiss_ttc[$obj->dm] += $obj->amount_ttc;

 			}

 		}

-	} else {

+	}

+	else

+	{

 		dol_print_error($db);

 	}

-} elseif ($modecompta == 'BOOKKEEPING') {

+}

+elseif ($modecompta == 'BOOKKEEPING') {

 	// Nothing from this table

 }

 

@@ -666,55 +676,58 @@
 

 if (!empty($conf->don->enabled) && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES"))

 {

-	$subtotal_ht = 0;

-	$subtotal_ttc = 0;

-

-	if ($modecompta == 'CREANCES-DETTES') {

-		$sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";

-		$sql .= " FROM ".MAIN_DB_PREFIX."don as p";

-		$sql .= " WHERE p.entity IN (".getEntity('donation').")";

-		$sql .= " AND fk_statut in (1,2)";

+    $subtotal_ht = 0;

+    $subtotal_ttc = 0;

+

+    if ($modecompta == 'CREANCES-DETTES') {

+        $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";

+        $sql .= " FROM ".MAIN_DB_PREFIX."don as p";

+        $sql .= " WHERE p.entity IN (".getEntity('donation').")";

+        $sql .= " AND fk_statut in (1,2)";

 		if (!empty($date_start) && !empty($date_end))

 			$sql .= " AND p.datedon >= '".$db->idate($date_start)."' AND p.datedon <= '".$db->idate($date_end)."'";

-	} elseif ($modecompta == 'RECETTES-DEPENSES') {

-		$sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(pe.datep,'%Y-%m') as dm, sum(p.amount) as amount";

-		$sql .= " FROM ".MAIN_DB_PREFIX."don as p";

+    } elseif ($modecompta == 'RECETTES-DEPENSES') {

+        $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(pe.datep,'%Y-%m') as dm, sum(p.amount) as amount";

+        $sql .= " FROM ".MAIN_DB_PREFIX."don as p";

 		$sql .= " INNER JOIN ".MAIN_DB_PREFIX."payment_donation as pe ON pe.fk_donation = p.rowid";

 		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";

 		$sql .= " WHERE p.entity IN (".getEntity('donation').")";

-   		$sql .= " AND fk_statut >= 2";

-		if (!empty($date_start) && !empty($date_end)) {

-			$sql .= " AND pe.datep >= '".$db->idate($date_start)."' AND pe.datep <= '".$db->idate($date_end)."'";

-		}

-	}

-

-	$sql .= " GROUP BY p.societe, p.firstname, p.lastname, dm";

-

-	dol_syslog("get donation payments");

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

-	if ($result)

-	{

-		$num = $db->num_rows($result);

-		$i = 0;

-		if ($num)

-		{

-			while ($i < $num)

-			{

-				$obj = $db->fetch_object($result);

-

-				if (!isset($encaiss[$obj->dm])) $encaiss[$obj->dm] = 0;

-				$encaiss[$obj->dm] += $obj->amount;

-

-				if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;

-				$encaiss_ttc[$obj->dm] += $obj->amount;

-

-				$i++;

-			}

-		}

-	} else {

-		dol_print_error($db);

-	}

-} elseif ($modecompta == 'BOOKKEEPING') {

+   	    $sql .= " AND fk_statut >= 2";

+        if (!empty($date_start) && !empty($date_end)) {

+            $sql .= " AND pe.datep >= '".$db->idate($date_start)."' AND pe.datep <= '".$db->idate($date_end)."'";

+        }

+    }

+

+    $sql .= " GROUP BY p.societe, p.firstname, p.lastname, dm";

+

+    dol_syslog("get donation payments");

+    $result = $db->query($sql);

+    if ($result)

+    {

+    	$num = $db->num_rows($result);

+    	$i = 0;

+    	if ($num)

+    	{

+    		while ($i < $num)

+    		{

+    			$obj = $db->fetch_object($result);

+

+    			if (!isset($encaiss[$obj->dm])) $encaiss[$obj->dm] = 0;

+    			$encaiss[$obj->dm] += $obj->amount;

+

+    			if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;

+    			$encaiss_ttc[$obj->dm] += $obj->amount;

+

+    			$i++;

+    		}

+    	}

+    }

+    else

+    {

+    	dol_print_error($db);

+    }

+}

+elseif ($modecompta == 'BOOKKEEPING') {

 	// Nothing from this table

 }

 

@@ -724,61 +737,65 @@
 

 if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_VARPAY) && !empty($conf->banque->enabled) && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES"))

 {

-	// decaiss

-

-	$sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various as p";

-	$sql .= ' WHERE p.sens = 0';

-	if (!empty($date_start) && !empty($date_end))

-		$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

-	$sql .= ' GROUP BY dm';

-

-	dol_syslog("get various payments");

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

-	if ($result)

-	{

-		$num = $db->num_rows($result);

-		$i = 0;

-		if ($num)

-		{

-			while ($i < $num)

-			{

-				$obj = $db->fetch_object($result);

-				if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;

-				if (isset($obj->amount)) $decaiss_ttc[$obj->dm] += $obj->amount;

-				$i++;

-			}

-		}

-	} else {

-		dol_print_error($db);

-	}

-

-	// encaiss

-

-	$sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various AS p";

-	$sql .= ' WHERE p.sens = 1';

-	if (!empty($date_start) && !empty($date_end))

-		$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

-	$sql .= ' GROUP BY dm';

-

-	dol_syslog("get various payments");

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

-	if ($result)

-	{

-		$num = $db->num_rows($result);

-		$i = 0;

-		if ($num)

-		{

-			while ($i < $num)

-			{

-				$obj = $db->fetch_object($result);

-				if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;

-				if (isset($obj->amount)) $encaiss_ttc[$obj->dm] += $obj->amount;

-				$i++;

-			}

-		}

-	} else {

-		dol_print_error($db);

-	}

+    // decaiss

+

+    $sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various as p";

+    $sql .= ' WHERE p.sens = 0';

+    if (!empty($date_start) && !empty($date_end))

+        $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+    $sql .= ' GROUP BY dm';

+

+    dol_syslog("get various payments");

+    $result = $db->query($sql);

+    if ($result)

+    {

+    	$num = $db->num_rows($result);

+    	$i = 0;

+    	if ($num)

+    	{

+    		while ($i < $num)

+    		{

+    			$obj = $db->fetch_object($result);

+    			if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;

+                if (isset($obj->amount)) $decaiss_ttc[$obj->dm] += $obj->amount;

+    			$i++;

+    		}

+    	}

+    }

+    else

+    {

+    	dol_print_error($db);

+    }

+

+    // encaiss

+

+    $sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various AS p";

+    $sql .= ' WHERE p.sens = 1';

+    if (!empty($date_start) && !empty($date_end))

+        $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+    $sql .= ' GROUP BY dm';

+

+    dol_syslog("get various payments");

+    $result = $db->query($sql);

+    if ($result)

+    {

+    	$num = $db->num_rows($result);

+    	$i = 0;

+    	if ($num)

+    	{

+    		while ($i < $num)

+    		{

+    			$obj = $db->fetch_object($result);

+    			if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;

+                if (isset($obj->amount)) $encaiss_ttc[$obj->dm] += $obj->amount;

+    			$i++;

+    		}

+    	}

+    }

+    else

+    {

+    	dol_print_error($db);

+    }

 }

 // Useless with BOOKKEEPING

 //elseif ($modecompta == 'BOOKKEEPING') {

@@ -790,31 +807,33 @@
 

 if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_LOAN) && !empty($conf->loan->enabled) && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES"))

 {

-	$sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount_capital + p.amount_insurance + p.amount_interest) AS amount FROM ".MAIN_DB_PREFIX."payment_loan AS p";

-	$sql .= ' WHERE 1 = 1';

-	if (!empty($date_start) && !empty($date_end))

-		$sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

-	$sql .= ' GROUP BY dm';

-

-	dol_syslog("get loan payments");

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

-	if ($result)

-	{

-		$num = $db->num_rows($result);

-		$i = 0;

-		if ($num)

-		{

-			while ($i < $num)

-			{

-				$obj = $db->fetch_object($result);

-				if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;

-				if (isset($obj->amount)) $decaiss_ttc[$obj->dm] += $obj->amount;

-				$i++;

-			}

-		}

-	} else {

-		dol_print_error($db);

-	}

+    $sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount_capital + p.amount_insurance + p.amount_interest) AS amount FROM ".MAIN_DB_PREFIX."payment_loan AS p";

+    $sql .= ' WHERE 1 = 1';

+    if (!empty($date_start) && !empty($date_end))

+        $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

+    $sql .= ' GROUP BY dm';

+

+    dol_syslog("get loan payments");

+    $result = $db->query($sql);

+    if ($result)

+    {

+    	$num = $db->num_rows($result);

+    	$i = 0;

+    	if ($num)

+    	{

+    		while ($i < $num)

+    		{

+    			$obj = $db->fetch_object($result);

+    			if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;

+                if (isset($obj->amount)) $decaiss_ttc[$obj->dm] += $obj->amount;

+    			$i++;

+    		}

+    	}

+    }

+    else

+    {

+    	dol_print_error($db);

+    }

 }

 // Useless with BOOKKEEPING

 //elseif ($modecompta == 'BOOKKEEPING') {

@@ -828,15 +847,17 @@
 if (!empty($conf->accounting->enabled) && ($modecompta == 'BOOKKEEPING'))

 {

 	$predefinedgroupwhere = "(";

-	$predefinedgroupwhere .= " (aa.pcg_type = 'EXPENSE')";

+	//$predefinedgroupwhere.= " (pcg_type = 'EXPENSE' and pcg_subtype in ('PRODUCT','SERVICE'))";

+	$predefinedgroupwhere .= " (pcg_type = 'EXPENSE')";

 	$predefinedgroupwhere .= " OR ";

-	$predefinedgroupwhere .= " (aa.pcg_type = 'INCOME')";

+	//$predefinedgroupwhere.= " (pcg_type = 'INCOME' and pcg_subtype in ('PRODUCT','SERVICE'))";

+	$predefinedgroupwhere .= " (pcg_type = 'INCOME')";

 	$predefinedgroupwhere .= ")";

 

 	$charofaccountstring = $conf->global->CHARTOFACCOUNTS;

 	$charofaccountstring = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');

 

-	$sql = "SELECT b.doc_ref, b.numero_compte, b.subledger_account, b.subledger_label, aa.pcg_type, date_format(b.doc_date,'%Y-%m') as dm, sum(b.debit) as debit, sum(b.credit) as credit, sum(b.montant) as amount";

+	$sql = "SELECT b.doc_ref, b.numero_compte, b.subledger_account, b.subledger_label, pcg_type, date_format(b.doc_date,'%Y-%m') as dm, sum(b.debit) as debit, sum(b.credit) as credit, sum(b.montant) as amount";

 	$sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b, ".MAIN_DB_PREFIX."accounting_account as aa";

 	$sql .= " WHERE b.entity = ".$conf->entity;

 	$sql .= " AND aa.entity = ".$conf->entity;

@@ -863,18 +884,10 @@
 			{

 				$obj = $db->fetch_object($result);

 

-				if ($obj->pcg_type == 'INCOME') {

-					if (!isset($encaiss[$obj->dm])) $encaiss[$obj->dm] = 0;	// To avoid warning of var not defined

-					$encaiss[$obj->dm] += $obj->credit;

-					$encaiss[$obj->dm] -= $obj->debit;

-				}

-				if ($obj->pcg_type == 'EXPENSE') {

-					if (!isset($decaiss[$obj->dm])) $decaiss[$obj->dm] = 0;	// To avoid warning of var not defined

-					$decaiss[$obj->dm] += $obj->debit;

-					$decaiss[$obj->dm] -= $obj->credit;

-				}

-

-				// ???

+				if (!isset($encaiss[$obj->dm])) $encaiss[$obj->dm] = 0;

+				if (!isset($decaiss[$obj->dm])) $decaiss[$obj->dm] = 0;

+				$encaiss[$obj->dm] += $obj->credit;

+				$decaiss[$obj->dm] += $obj->debit;

 				if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;

 				if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;

 				$encaiss_ttc[$obj->dm] += 0;

@@ -883,7 +896,9 @@
 				$i++;

 			}

 		}

-	} else {

+	}

+	else

+	{

 		dol_print_error($db);

 	}

 }

@@ -931,7 +946,7 @@
 	print '</td>';

 	print '<td class="liste_titre" align="center" class="borderrightlight">';

 	$htmlhelp = '';

-	// if ($modecompta == 'RECETTES-DEPENSES') $htmlhelp=$langs->trans("SalesPlusVATToRetrieve");

+	// if ($modecompta == 'RECETTES-DEPENSES') $htmlhelp=$langs->trans("SalesPlusVATToRetreive");

 	print $form->textwithpicto($langs->trans("Income"), $htmlhelp);

 	print '</td>';

 }

@@ -962,7 +977,9 @@
 				if (!isset($totsorties[$annee])) $totsorties[$annee] = 0;

 				$totsorties[$annee] += $decaiss[$case];

 			}

-		} else {

+		}

+		else

+		{

 			if (isset($decaiss_ttc[$case]) && $decaiss_ttc[$case] != 0)

 			{

 				print '<a href="clientfourn.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">'.price(price2num($decaiss_ttc[$case], 'MT')).'</a>';

@@ -981,7 +998,9 @@
 				if (!isset($totentrees[$annee])) $totentrees[$annee] = 0;

 				$totentrees[$annee] += $encaiss[$case];

 			}

-		} else {

+		}

+		else

+		{

 			if (isset($encaiss_ttc[$case]))

 			{

 				print '<a href="clientfourn.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">'.price(price2num($encaiss_ttc[$case], 'MT')).'</a>';

--- /tmp/dsg/dolibarr/htdocs/compta/resultat/github_result.php
+++ /tmp/dsg/dolibarr/htdocs/compta/resultat/client_result.php
@@ -90,7 +90,8 @@
 			$month_end = $month_start - 1;

 			if ($month_end < 1) $month_end = 12;

 			else $year_end++;

-		} else $month_end = $month_start;

+		}

+		else $month_end = $month_start;

 		$date_start = dol_get_first_day($year_start, $month_start, false); $date_end = dol_get_last_day($year_end, $month_end, false);

 	}

 	if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); }

@@ -177,10 +178,11 @@
 	//$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';

 	$description = $langs->trans("RulesResultDue");

 	if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description .= $langs->trans("DepositsAreNotIncluded");

-	else $description .= $langs->trans("DepositsAreIncluded");

+	else  $description .= $langs->trans("DepositsAreIncluded");

 	$builddate = dol_now();

 	//$exportlink=$langs->trans("NotYetAvailable");

-} elseif ($modecompta == "RECETTES-DEPENSES") {

+}

+elseif ($modecompta == "RECETTES-DEPENSES") {

 	$name = $langs->trans("AnnualByAccountInputOutputMode");

 	$calcmode = $langs->trans("CalcModeEngagement");

 	$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month") > 0 ? '&month='.GETPOST("month") : '').'&modecompta=CREANCES-DETTES">', '</a>').')';

@@ -190,7 +192,8 @@
 	$description = $langs->trans("RulesResultInOut");

 	$builddate = dol_now();

 	//$exportlink=$langs->trans("NotYetAvailable");

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	$name = $langs->trans("ReportInOut").', '.$langs->trans("ByPersonalizedAccountGroups");

 	$calcmode = $langs->trans("CalcModeBookkeeping");

@@ -213,7 +216,7 @@
 

 if (!empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING')

 {

-	print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);

+    print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);

 }

 

 

@@ -245,11 +248,13 @@
 {

 	//if (! empty($date_start) && ! empty($date_end))

 	//	$sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";

-} elseif ($modecompta == "RECETTES-DEPENSES")

+}

+elseif ($modecompta == "RECETTES-DEPENSES")

 {

 	//if (! empty($date_start) && ! empty($date_end))

 	//	$sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";

-} elseif ($modecompta == "BOOKKEEPING")

+}

+elseif ($modecompta == "BOOKKEEPING")

 {

 	// Get array of all report groups that are active

 	$cats = $AccCat->getCats(); // WARNING: Computed groups must be after group they include

@@ -286,9 +291,9 @@
 		setEventMessages(null, $AccCat->errors, 'errors');

 	} elseif (is_array($cats) && count($cats) > 0) {

 		foreach ($cats as $cat) {

-			// Loop on each group

+            // Loop on each group

 			if (!empty($cat['category_type'])) {

-				// category calculed

+                // category calculed

 				// When we enter here, $sommes was filled by group of accounts

 

 				$formula = $cat['formula'];

@@ -370,7 +375,7 @@
 				print "</tr>\n";

 

 				//var_dump($sommes);

-			} else // normal category

+			} else            // normal category

 			{

 				$code = $cat['code']; // Category code we process