<?php
/**
* $Id: balance.php 418 2007-05-17 17:29:45Z hpfn $
*
* Author : courou@users.sourceforge.net
* Website : http://www.myphpmoney.net
*
* Support : http://sourceforge.net/projects/myphpmoney/
* CVS : http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/myphpmoney/
*
*/
/**
* INCLUDE FILE
*/
require_once 'config/settings.inc.php';
/**
* ENTER IN THE SESSION OR BUILD THE LOGIN FORM
*/
if (SQL_VerifSession()) {
/**
* BUILD THE HEADER
*/
XHTML_header(1,0,1,1,1,1,1,1);
/**
* DEFINE VALUE FOR NOTICE
*/
$PageOrder1 = 'balance';
$PageOrder2 = 'etat';
$sum_dep = '';
$sum_rec = '';
$sum_sol = '';
$limit = '';
$day_beg = '';
$day_end = '';
$symbol = '';
$ecart = '';
$_tempid = '';
$_check = '';
$select_account = '';
/**
* DETAILS FOR BALANCE
*/
if (isset($_POST['CHECK_IMG']) && is_array($_POST['CHECK_IMG'])) {
while(list($kobj,$vobj) = each($_POST['CHECK_IMG'])) { $hid = explode('|',$kobj); }
$day_beg = $hid[2].'-'.$hid[1].'-'.$hid[3];
$day_end = $hid[2].'-'.$hid[1].'-'.$hid[4];
$symbol = '=';
$ecart = sprintf(
$_VAR['BILAN_TITLE_MORE'],
$hid[3].' '.$_VAR['THIS_MONTH'][$hid[1]-1].' '.$hid[2],
$hid[4].' '.$_VAR['THIS_MONTH'][$hid[1]-1].' '.$hid[2]);
}
else if ((isset($_POST['CHECK_ALL']) && is_array($_POST['CHECK_ALL'])) ||
(isset($_POST['orderby2']) && isset($_POST['ID']))) {
$hid[0] = isset($_POST['A_Account']) && $_POST['A_Account'] != '' ? SQL_Clean($_POST['A_Account']) : '';
$day_beg = $_POST['Ayear'].'-'.$_POST['Amonth'].'-'.$_POST['Adays'];
$day_end = $_POST['Byear'].'-'.$_POST['Bmonth'].'-'.$_POST['Bdays'];
$day_beg = SQL_Clean($day_beg);
$day_end = SQL_Clean($day_end);
$symbol = $hid[0] == '' ? '<>' : '=';
$ecart = sprintf(
$_VAR['BILAN_TITLE_MORE'],
$_POST['Adays'].' '.$_VAR['THIS_MONTH'][$_POST['Amonth']-1].' '.$_POST['Ayear'],
$_POST['Bdays'].' '.$_VAR['THIS_MONTH'][$_POST['Bmonth']-1].' '.$_POST['Byear']);
}
/**
* KEEP THE CURRENT PAGE WHEN ORDER BY CHANGE
*/
if (isset($_POST['orderby1']) && isset($_POST['ID'])) $_POST['ID'] = 'look';
if (isset($_POST['orderby2']) && isset($_POST['ID'])) $_POST['ID'] = 'check';
/**
* POST VALUE (MENU SELECT)
*/
if (isset($hid[0]) && isset($hid[1]) && isset($hid[2]) && isset($hid[3]) && isset($hid[4])) {
$_POST['Adays'] = isset($hid[3]) ? $hid[3] : '';
$_POST['Bdays'] = isset($hid[4]) ? $hid[4] : '';
$_POST['Amonth'] = isset($hid[1]) ? $hid[1] : '';
$_POST['Bmonth'] = isset($hid[1]) ? $hid[1] : '';
$_POST['Ayear'] = isset($hid[2]) ? $hid[2] : '';
$_POST['Byear'] = isset($hid[2]) ? $hid[2] : '';
$_POST['A_Account'] = isset($hid[0]) ? $hid[0] : '';
} else {
$_POST['Adays'] = isset($_POST['Adays']) ? $_POST['Adays'] : '';
$_POST['Bdays'] = isset($_POST['Bdays']) ? $_POST['Bdays'] : '';
$_POST['Amonth'] = isset($_POST['Amonth']) ? $_POST['Amonth'] : '';
$_POST['Bmonth'] = isset($_POST['Bmonth']) ? $_POST['Bmonth'] : '';
$_POST['Ayear'] = isset($_POST['Ayear']) ? $_POST['Ayear'] : '';
$_POST['Byear'] = isset($_POST['Byear']) ? $_POST['Byear'] : '';
$_POST['A_Account'] = isset($_POST['A_Account']) ? $_POST['A_Account'] : '';
}
/**
* POST VALUE (TAKE VALUE IN SHUFFLE)
*/
if (isset($_POST['Ashuffle']) && $_POST['Ashuffle'] == 'yes') {
$_tempid = "(".$_MPM['table'][1].".TEMPID=1 OR ".$_MPM['table'][1].".TEMPID=2)";
$_check = ' checked="checked"';
} else {
$_tempid = "".$_MPM['table'][1].".TEMPID=1";
$_check = '';
}
/**
* THE TEMPLATES -- THE BALANCE MAIN
*/
if (__DISPLAY_EURO__ == 0 || __DISPLAY_EURO__ == 2) {
$txt = sprintf($_VAR['LANG_BOOK_14'],__DISPLAY_EURO__ == 2 ? $_MPM['lang_money_country'] : $_VAR['IMG_TXT_EUROS']);
} else if (__DISPLAY_EURO__ == 1) {
$txt = sprintf($_VAR['LANG_BOOK_13'],$_VAR['IMG_TXT_EUROS'],$_MPM['lang_money_country']);
}
$list_day = MPM_listdays();
$_MPM['BeginMixed'] = array
(
'LANG_XX' => $txt,
'CHECKED' => $_check,
'OPEN_CLOSE' => XHTML_OPEN_CLOSE(),
'BILAN_TITLE' => $_VAR['BILAN_TITLE'],
'BILAN_BEGIN' => $_VAR['BILAN_BEGIN'],
'BILAN_END' => $_VAR['BILAN_END'],
'BILAN_CALEND' => $_VAR['BILAN_CALEND'],
'LANG_ACCOUNT_10' => $_VAR['LANG_ACCOUNT_10'],
'LANG_ACCOUNT_20' => $_VAR['LANG_ACCOUNT_20'],
'LANG_ACCOUNT_22' => $_VAR['LANG_ACCOUNT_22'],
'LANG_ACCOUNT_17' => $_VAR['LANG_ACCOUNT_17'],
'LANG_ACCOUNT_18' => $_VAR['LANG_ACCOUNT_18'],
'LANG_BOOK_0' => $_VAR['LANG_BOOK_0'],
'SELECT_DAYS_1' => XHTML_SelectYearMonth(3,$_POST['Adays'],$list_day,'Adays'),
'SELECT_DAYS_2' => XHTML_SelectYearMonth(3,$_POST['Bdays'],$list_day,'Bdays'),
'SELECT_MONTH_1' => XHTML_SelectYearMonth(1,$_POST['Amonth'],SQL_SearchYearMonth('m','ASC'),'Amonth'),
'SELECT_MONTH_2' => XHTML_SelectYearMonth(1,$_POST['Bmonth'],SQL_SearchYearMonth('m','ASC'),'Bmonth'),
'SELECT_YEAR_1' => XHTML_SelectYearMonth(2,$_POST['Ayear'],SQL_SearchYearMonth('Y','ASC'),'Ayear'),
'SELECT_YEAR_2' => XHTML_SelectYearMonth(2,$_POST['Byear'],SQL_SearchYearMonth('Y','DESC'),'Byear'),
'SELECT_ACCOUNT' => SQL_ChoiceAccount(SQL_Clean($_POST['A_Account']),3),
'Adays' => $_POST['Adays'],
'Bdays' => $_POST['Bdays'],
'Amonth' => $_POST['Amonth'],
'Bmonth' => $_POST['Bmonth'],
'Ayear' => $_POST['Ayear'],
'Byear' => $_POST['Byear'],
'A_Account' => $_POST['A_Account'],
'Ashuffle' => isset($_POST['Ashuffle']) && $_POST['Ashuffle'] == 'yes' ? $_POST['Ashuffle'] : '',
'VALIDER' => $_VAR['VALIDER']
);
XHTML_SetFile('bilan_main.tpl',$_MPM['BeginMixed'],2);
/**
* RECUP THE POST VALUE
*/
if (!isset($_POST['ID'])) $_POST['ID'] = '';
switch($_POST['ID']) {
## DISPLAY THE ALL ACCOUNT LIST AND VALUE
case 'look' :
## Verif that all field is not empty
if (($_POST['Adays'] && $_POST['Bdays'] && $_POST['Amonth'] && $_POST['Bmonth'] && $_POST['Ayear'] && $_POST['Byear']) != '' ) {
$begin_search = sprintf("%d-%s-%s",$_POST['Ayear'],$_POST['Amonth'],$_POST['Adays']);
$end_search = sprintf("%d-%s-%s",$_POST['Byear'],$_POST['Bmonth'],$_POST['Bdays']);
} else {
printf("<h2 class=\"center\">%s</h2>",$_VAR['BILAN_LANG_ERR']);
}
## Ok for begin the choice
if (isset($begin_search) && isset($end_search)) {
if ($begin_search < $end_search) {
## SQL ORDER BY ... ASC ... DESC
SQL_Orderby($PageOrder1,1);
if (isset($_POST['order1'][$PageOrder1]) && strpos($_POST['order1'][$PageOrder1], "_") !== false) {
$this_order = explode("_",$_POST['order1'][$PageOrder1]);
if ($this_order[1] == 0) $order = $_MPM['table'][1].".DATES ".$this_order[0];
if ($this_order[1] == 1) $order = $_MPM['table'][1].".ACCOUNT ".$this_order[0];
if (!isset($order)) $order = $_MPM['table'][1].".DATES asc";
} else {
$order = $_MPM['table'][1].".DATES asc";
}
$_begin_search = $_POST['Adays'].' '.$_VAR['THIS_MONTH'][$_POST['Amonth']-1].' '.$_POST['Ayear'];
$_end_search = $_POST['Bdays'].' '.$_VAR['THIS_MONTH'][$_POST['Bmonth']-1].' '.$_POST['Byear'];
$_MPM['BlockInclude'] = MPM_IncludeTemp(0,'bilan_simple.tpl','bilan_double.tpl');
$_MPM['BeginMixed'] = array
(
'IMG_ASC_DESC1' => XHTML_ASC_DESC($PageOrder1,'asc_0','desc_0',1),
'IMG_ASC_DESC2' => XHTML_ASC_DESC($PageOrder1,'asc_1','desc_1',1),
'BILAN_TITLE_MORE' => sprintf($_VAR['BILAN_TITLE_MORE'],$_begin_search,$_end_search),
'LANG_BOOK_4' => $_VAR['LANG_BOOK_4'],
'BILAN_LANG_1' => $_VAR['BILAN_LANG_1']
);
XHTML_SetFile($_MPM['BlockInclude'],$_MPM['BeginMixed'],0);
## If Account is selected
if (isset($_POST['A_Account']) && strstr($_POST['A_Account'],$sess_pid)) {
$select_account = "AND ".$_MPM['table'][1].".ACCOUNT = '".SQL_Clean($_POST['A_Account'])."'";
}
$db->query (
"SELECT
DATE_FORMAT(".$_MPM['table'][1].".DATES,'%m') as d1,
DATE_FORMAT(".$_MPM['table'][1].".DATES,'%Y') as d2,
SUM(".$_MPM['table'][1].".SOUTT) as t1,
SUM(".$_MPM['table'][1].".IMP) as t2,
SUM(".$_MPM['table'][1].".IMP - ".$_MPM['table'][1].".SOUTT) as t3,
".$_MPM['table'][2].".ACCOUNT,
".$_MPM['table'][1].".ACCOUNT as _ACCOUNT
FROM ".$_MPM['table'][1].",".$_MPM['table'][2]."
WHERE $_tempid
AND ".$_MPM['table'][1].".USERID='$sess_pid'
AND ".$_MPM['table'][1].".ACCOUNT=".$_MPM['table'][2].".ACCOUNTID
AND ".$_MPM['table'][1].".DATES > '".SQL_Clean($begin_search)."'
AND ".$_MPM['table'][1].".DATES < '".SQL_Clean($end_search)."'
$select_account
GROUP BY d1, d2, ".$_MPM['table'][1].".ACCOUNT
ORDER BY ".$order
);
$tpl->set_block('FILE_'.$_MPM['BlockInclude'].'','BOUCLE_SEARCH_BILAN','BOUCLE_BEGIN_BILAN_SEARCH');
$nums = $db->num_rows();
while ($db->next_record()) {
$_depense=$db->f('t1'); $_recette=$db->f('t2'); $_soldes=$db->f('t3');
$sum_dep += $_depense; $sum_rec += $_recette; $sum_sol += $_soldes;
## CAS PARTICULIERS POUR LES MOIS NON ENTIER
if (isset($nums) && $nums > 1) {
if ($db->f('d1') == $_POST['Amonth'] && $db->f('d2') == $_POST['Ayear']) {
$limit = $_POST['Adays']."|31";
}
else if ($db->f('d1') == $_POST['Bmonth'] && $db->f('d2') == $_POST['Byear']) {
$limit = "01|".$_POST['Bdays'];
}
else {
$limit = "01|31";
}
} else {
$limit = $_POST['Adays'].'|'.$_POST['Bdays'];
}
$limits = explode('|',$limit);
$middlesearch = $_VAR['THIS_MONTH'][$db->f('d1')-1].' '.$db->f('d2');
$beginsearch = $limits[0].' '.$middlesearch;
$endsearch = $limits[1].' '.$middlesearch;
$tpl->set_var(array(
'_ACCOUNT' => $db->f('_ACCOUNT'),
'_MONTH' => $db->f('d1'),
'_YEAR' => $db->f('d2'),
'_LIMIT' => $limit,
'BILAN_DATE' => $middlesearch,
'BILAN_DATE_IMG' => $_VAR['BILAN_LANG_1'].' : '.$beginsearch.' - '.$endsearch,
'BILAN_ACCOUNT' => $db->f('ACCOUNT'),
'DEP_EURO' => $tools->formatage($_depense),
'DEP_ANO' => $tools->formatage($_depense * $_MPM['lang_euro']),
'REC_EURO' => $tools->formatage($_recette),
'REC_ANO' => $tools->formatage($_recette * $_MPM['lang_euro']),
'SOL_EURO' => $tools->formatage($_soldes),
'SOL_ANO' => $tools->formatage($_soldes * $_MPM['lang_euro']),
'FORMAT_COLOR_1' => $_depense >= 0 ? 'menurouge' : 'menubleu',
'FORMAT_COLOR_2' => $_recette >= 0 ? 'menubleu' : 'menurouge',
'FORMAT_COLOR_3' => $_soldes >= 0 ? 'menubleu' : 'menurouge',
'classbgcolor' => isset($colors) && $colors == __COLOR_DISPLAY_1__ ? 'display2' : 'display1',
'COLOR' => isset($colors) && $colors == __COLOR_DISPLAY_1__ ? $colors = __COLOR_DISPLAY_2__ : $colors = __COLOR_DISPLAY_1__
));
$tpl->parse('BOUCLE_BEGIN_BILAN_SEARCH','BOUCLE_SEARCH_BILAN',true);
$i++;
}
$_MPM['BoucleMixed'] = array
(
'SOM_DEP_EURO' => $tools->formatage($sum_dep),
'SOM_DEP_ANO' => $tools->formatage($sum_dep * $_MPM['lang_euro']),
'SOM_REC_EURO' => $tools->formatage($sum_rec),
'SOM_REC_ANO' => $tools->formatage($sum_rec * $_MPM['lang_euro']),
'SOM_SOL_EURO' => $tools->formatage($sum_sol),
'SOM_SOL_ANO' => $tools->formatage($sum_sol * $_MPM['lang_euro'])
);
XHTML_SetFile($_MPM['BlockInclude'],$_MPM['BoucleMixed'],4);
} else {
printf("<h2 class=\"center\">%s</h2>",$_VAR['BILAN_LANG_ERR']);
}
}
break;
## DETAILS FOR BALANCE
case 'check' :
if (isset($hid[0]) && isset($ecart) && isset($day_beg) && isset($day_end) && isset($symbol)) {
## SQL ORDER BY ... ASC ... DESC
SQL_Orderby($PageOrder2,2);
if (isset($_POST['order2'][$PageOrder2]) && strpos($_POST['order2'][$PageOrder2], "_") !== false) {
$_this_order = explode("_",$_POST['order2'][$PageOrder2]);
if ($_this_order[1] == 0) $_order = $_MPM['table'][0].".CAT ".$_this_order[0];
if ($_this_order[1] == 1) $_order = $_MPM['table'][4].".THIRD ".$_this_order[0];
if (!isset($_order)) $_order = $_MPM['table'][0].".CAT asc";
} else {
$_order = $_MPM['table'][0].".CAT asc";
}
$_MPM['BlockInclude'] = MPM_IncludeTemp(0,'bilan_display_simple.tpl','bilan_display_double.tpl');
$_MPM['BeginMixed'] = array(
'IMG_ASC_DESC1' => XHTML_ASC_DESC($PageOrder2,'asc_0','desc_0',2),
'IMG_ASC_DESC2' => XHTML_ASC_DESC($PageOrder2,'asc_1','desc_1',2),
'BILAN_TITLE_MORE' => $ecart
);
XHTML_SetFile($_MPM['BlockInclude'],$_MPM['BeginMixed'],0);
## SOUTT (DEPENSE)
$request1 = "SELECT ".$_MPM['table'][0].".CAT, ".$_MPM['table'][4].".THIRD, SUM(".$_MPM['table'][1].".SOUTT) as SOUTT
FROM ".$_MPM['table'][1].", ".$_MPM['table'][2].", ".$_MPM['table'][0].", ".$_MPM['table'][4]."
WHERE $_tempid
AND ".$_MPM['table'][0].".USERID = '$sess_pid'
AND ".$_MPM['table'][1].".USERID = '$sess_pid'
AND ".$_MPM['table'][0].".CATID = ".$_MPM['table'][1].".CAT
AND ".$_MPM['table'][4].".THIRDID = ".$_MPM['table'][1].".THIRD
AND ".$_MPM['table'][1].".ACCOUNT = ".$_MPM['table'][2].".ACCOUNTID
AND ".$_MPM['table'][1].".DATES > '$day_beg'
AND ".$_MPM['table'][1].".DATES < '$day_end'
AND ".$_MPM['table'][1].".ACCOUNT $symbol '$hid[0]'
AND ".$_MPM['table'][1].".SOUTT <> 0
GROUP BY ".$_MPM['table'][1].".CAT,".$_MPM['table'][1].".THIRD
ORDER BY ".$_order;
$db->query($request1);
$tpl->set_block('FILE_'.$_MPM['BlockInclude'].'','BOUCLE_DEPENSE_BILAN','BOUCLE_BEGIN_BILAN_DEPENSE');
$CAT = '';
while ($db->next_record()) {
$double = $db->f('CAT');
$_depense=$db->f('SOUTT');
$sum_dep += $_depense;
$tpl->set_var(array(
'_CAT' => $double != $CAT ? $CAT = $double : '',
'_THIRD' => $db->f('THIRD'),
'MONEY1' => $tools->formatage($db->f('SOUTT')),
'MONEY2' => $tools->formatage($db->f('SOUTT') * $_MPM['lang_euro']),
'classbgcolor' => isset($colors) && $colors == __COLOR_DISPLAY_1__ ? 'display2' : 'display1',
'COLOR' => isset($colors) && $colors == __COLOR_DISPLAY_1__ ? $colors = __COLOR_DISPLAY_2__ : $colors = __COLOR_DISPLAY_1__
));
$tpl->parse('BOUCLE_BEGIN_BILAN_DEPENSE','BOUCLE_DEPENSE_BILAN',true);
}
## IMP (RECETTE)
$request2 = "SELECT ".$_MPM['table'][0].".CAT, ".$_MPM['table'][4].".THIRD, SUM(".$_MPM['table'][1].".IMP) as IMP
FROM ".$_MPM['table'][1].", ".$_MPM['table'][2].", ".$_MPM['table'][0].", ".$_MPM['table'][4]."
WHERE $_tempid
AND ".$_MPM['table'][0].".USERID = '$sess_pid'
AND ".$_MPM['table'][1].".USERID = '$sess_pid'
AND ".$_MPM['table'][0].".CATID = ".$_MPM['table'][1].".CAT
AND ".$_MPM['table'][4].".THIRDID = ".$_MPM['table'][1].".THIRD
AND ".$_MPM['table'][1].".ACCOUNT = ".$_MPM['table'][2].".ACCOUNTID
AND ".$_MPM['table'][1].".DATES > '$day_beg'
AND ".$_MPM['table'][1].".DATES < '$day_end'
AND ".$_MPM['table'][1].".ACCOUNT $symbol '$hid[0]'
AND ".$_MPM['table'][1].".IMP <> 0
GROUP BY ".$_MPM['table'][1].".CAT,".$_MPM['table'][1].".THIRD
ORDER BY ".$_order;
$db->query($request2);
$tpl->set_block('FILE_'.$_MPM['BlockInclude'].'','BOUCLE_RECETTE_BILAN','BOUCLE_BEGIN_BILAN_RECETTE');
$CAT = '';
while ($db->next_record()) {
$double = $db->f('CAT');
$_recette=$db->f('IMP');
$sum_rec += $_recette;
$tpl->set_var(array(
'_CAT' => $double != $CAT ? $CAT = $double : '',
'_THIRD' => $db->f('THIRD'),
'MONEY1' => $tools->formatage($db->f('IMP')),
'MONEY2' => $tools->formatage($db->f('IMP') * $_MPM['lang_euro']),
'classbgcolor' => isset($colors) && $colors == __COLOR_DISPLAY_1__ ? 'display2' : 'display1',
'COLOR' => isset($colors) && $colors == __COLOR_DISPLAY_1__ ? $colors = __COLOR_DISPLAY_2__ : $colors = __COLOR_DISPLAY_1__
));
$tpl->parse('BOUCLE_BEGIN_BILAN_RECETTE','BOUCLE_RECETTE_BILAN',true);
}
$_MPM['BoucleMixed'] = array
(
'D_MONEY1' => $tools->formatage($sum_dep),
'D_MONEY2' => $tools->formatage($sum_dep * $_MPM['lang_euro']),
'R_MONEY1' => $tools->formatage($sum_rec),
'R_MONEY2' => $tools->formatage($sum_rec * $_MPM['lang_euro']),
'S_MONEY1' => $tools->formatage($sum_rec - $sum_dep),
'S_MONEY2' => $tools->formatage(($sum_rec - $sum_dep) * $_MPM['lang_euro'])
);
XHTML_SetFile($_MPM['BlockInclude'],$_MPM['BoucleMixed'],4);
} ## end of if (isset($ecart) && isset($day_beg) && isset($day_end) && isset($symbol))
break;
## DEFAULT
default :
break;
} ## end of switch $_POST['ID']
} else {
XHTML_DisplayLogin();
} ## end if (SQL_VerifSession())
/**
* BUILD THE FOOTER
*/
XHTML_footer();