diff --git a/htdocs/accountancy/class/accountancycategory.class.php b/htdocs/accountancy/class/accountancycategory.class.php index db75947771e..7668756a046 100644 --- a/htdocs/accountancy/class/accountancycategory.class.php +++ b/htdocs/accountancy/class/accountancycategory.class.php @@ -429,4 +429,158 @@ class AccountancyCategory return - 1; } } + + public function getCats() { + global $db, $langs, $user, $mysoc; + + if (empty($mysoc->country_id) && empty($mysoc->country_code)) { + dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined'); + exit(); + } + + if (! empty($mysoc->country_id)) { + $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type"; + $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c"; + $sql .= " WHERE c.active = 1 "; + $sql .= " AND c.fk_country = " . $mysoc->country_id; + $sql .= " ORDER BY c.position ASC"; + } else { + $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position, c.category_type"; + $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c, " . MAIN_DB_PREFIX . "c_country as co"; + $sql .= " WHERE c.active = 1 AND c.fk_country = co.rowid"; + $sql .= " AND co.code = '" . $mysoc->country_code . "'"; + $sql .= " ORDER BY c.position ASC"; + } + + dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $i = 0; + $obj = ''; + $num = $this->db->num_rows($resql); + $data = array (); + if ($num) { + while ( $i < $num ) { + $obj = $this->db->fetch_object($resql); + + $data[] = array ( + 'rowid' => $obj->rowid, + 'code' => $obj->code, + 'position' => $obj->position, + 'label' => $obj->label, + 'formula' => $obj->formula, + 'category_type' => $obj->category_type + ); + $i ++; + } + } + return $data; + } else { + $this->error = "Error " . $this->db->lasterror(); + $this->errors[] = $this->error; + dol_syslog(__METHOD__ . " " . implode(',', $this->errors), LOG_ERR); + + return - 1; + } + } + + + // calcule + + const PATTERN = '/(?:\-?\d+(?:\.?\d+)?[\+\-\*\/])+\-?\d+(?:\.?\d+)?/'; + + const PARENTHESIS_DEPTH = 10; + + public function calculate($input){ + if(strpos($input, '+') != null || strpos($input, '-') != null || strpos($input, '/') != null || strpos($input, '*') != null){ + // Remove white spaces and invalid math chars + $input = str_replace(',', '.', $input); + $input = preg_replace('[^0-9\.\+\-\*\/\(\)]', '', $input); + + // Calculate each of the parenthesis from the top + $i = 0; + while(strpos($input, '(') || strpos($input, ')')){ + $input = preg_replace_callback('/\(([^\(\)]+)\)/', 'self::callback', $input); + + $i++; + if($i > self::PARENTHESIS_DEPTH){ + break; + } + } + + // Calculate the result + if(preg_match(self::PATTERN, $input, $match)){ + return $this->compute($match[0]); + } + + return 0; + } + + return $input; + } + + private function compute($input){ + $compute = create_function('', 'return '.$input.';'); + + return 0 + $compute(); + } + + private function callback($input){ + if(is_numeric($input[1])){ + return $input[1]; + } + elseif(preg_match(self::PATTERN, $input[1], $match)){ + return $this->compute($match[0]); + } + + return 0; + } + + /** + * get cpts of category + * + * @return array Result in table + */ + public function getCptsCat($cat_id) { + global $mysoc; + $sql = ""; + + if (empty($mysoc->country_id) && empty($mysoc->country_code)) { + dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined'); + exit(); + } + + $sql = "SELECT t.rowid, t.account_number, t.label as name_cpt"; + $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t"; + $sql .= " WHERE t.fk_accounting_category = ".$cat_id; + $sql .= " ORDER BY t.account_number "; + + //echo $sql; + + $resql = $this->db->query($sql); + if ($resql) { + $i = 0; + $obj = ''; + $num = $this->db->num_rows($resql); + $data = array (); + if ($num) { + while ( $obj = $this->db->fetch_object($resql) ) { + $name_cat = $obj->name_cat; + $data[] = array ( + 'id' => $obj->rowid, + 'account_number' => $obj->account_number, + 'name_cpt' => $obj->name_cpt, + ); + $i ++; + } + } + return $data; + } else { + $this->error = "Error " . $this->db->lasterror(); + dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR); + + return -1; + } + } + } diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php index 10467294052..ed8d1d7501a 100644 --- a/htdocs/accountancy/journal/bankjournal.php +++ b/htdocs/accountancy/journal/bankjournal.php @@ -738,7 +738,7 @@ if (empty($action) || $action == 'view') { $builddate = time(); //$description = $langs->trans("DescFinanceJournal") . '
'; $description.= $langs->trans("DescJournalOnlyBindedVisible").'
'; - $period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1); + $period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1). ' - ' .$langs->trans("AlreadyInGeneralLedger").' '. $form->selectyesno('in_bookkeeping',$in_bookkeeping,0); $varlink = 'id_journal=' . $id_journal; diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index c3ed681308c..e023e14a817 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -489,7 +489,7 @@ if (empty($action) || $action == 'view') { $description .= $langs->trans("DepositsAreIncluded"); } - $period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1); + $period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1). ' - ' .$langs->trans("AlreadyInGeneralLedger").' '. $form->selectyesno('in_bookkeeping',$in_bookkeeping,0); $varlink = 'id_journal=' . $id_journal; diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php index c77d9305d12..edaf3a87f27 100644 --- a/htdocs/accountancy/journal/sellsjournal.php +++ b/htdocs/accountancy/journal/sellsjournal.php @@ -505,7 +505,7 @@ if (empty($action) || $action == 'view') { $description .= $langs->trans("DepositsAreNotIncluded"); else $description .= $langs->trans("DepositsAreIncluded"); - $period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1); + $period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1). ' - ' .$langs->trans("AlreadyInGeneralLedger").' '. $form->selectyesno('in_bookkeeping',$in_bookkeeping,0); $varlink = 'id_journal=' . $id_journal; diff --git a/htdocs/accountancy/report/result.php b/htdocs/accountancy/report/result.php index 801ba0a2007..175c9955498 100644 --- a/htdocs/accountancy/report/result.php +++ b/htdocs/accountancy/report/result.php @@ -1,6 +1,6 @@ - * Copyright (C) 2016 Alexandre Spangaro +/* Copyright (C) 2016/17 Jamal Elbaz + * Copyright (C) 2016 Alexandre Spangaro * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,6 +25,7 @@ require '../../main.inc.php'; // Class require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php'; +require_once DOL_DOCUMENT_ROOT . '/core/lib/report.lib.php'; require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountancycategory.class.php'; require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php'; @@ -35,12 +36,14 @@ $langs->load("accountancy"); $langs->load("compta"); $mesg = ''; -$action = GETPOST('action','aZ09'); +$action = GETPOST('action'); $cat_id = GETPOST('account_category'); $selectcpt = GETPOST('cpt_bk'); $id = GETPOST('id', 'int'); $rowid = GETPOST('rowid', 'int'); $cancel = GETPOST('cancel'); +$simple_report = GETPOST('simple_report'); + // Filter $year = GETPOST('year','int'); @@ -77,7 +80,16 @@ $form = new Form($db); $textprevyear = '' . img_previous() . ''; $textnextyear = ' ' . img_next() . ''; -print load_fiche_titre($langs->trans('ReportInOut'), $textprevyear . " " . $langs->trans("Year") . " " . $year_start . " " . $textnextyear, 'title_accountancy'); + $nom = $langs->trans("ReportInOut"); + $nomlink = ''; + $periodlink = ''; + $exportlink = ''; + $builddate = time(); + $description = ''; + $period = $langs->trans("Detail").' '. $form->selectyesno('simple_report',$simple_report,0) . " " .$textprevyear . " " . $langs->trans("Year") . " " . $year_start . " " . $textnextyear ; +report_header($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array ( + 'action' => '' + )); $moreforfilter=''; @@ -108,37 +120,97 @@ foreach($months as $k => $v){ } print ''; -$cats = $AccCat->getCatsCpts(); -if ($cats < 0) dol_print_error($db, $AccCat->error, $AccCat->errors); -$catsCalcule = $AccCat->getCatsCal(); + +//All categories +$cats = $AccCat->getCats(); if ($catsCalcule < 0) dol_print_error($db, $AccCat->error, $AccCat->errors); $j=1; $sommes = array(); -if (!empty($cats)) -{ - foreach ($cats as $name_cat => $cpts) - { +foreach($cats as $cat ){ + if(!empty($cat['category_type'])){ // category calculed + + $formula = $cat['formula']; + print ""; - print '' . $name_cat . ''; + print '' . $cat['label'] . ''; + + $vars = array(); + + // Previous Fiscal year (N-1) + foreach($sommes as $code => $det){ + $vars[$code] = $det['NP']; + } + + + $result = strtr($formula, $vars); + + + $r = $AccCat->calculate($result); + + print '' . price($r) . ''; + $code = $cat['code']; // code categorie de calcule + $sommes[$code]['NP'] += $r; + + // Current fiscal year (N) + if (is_array($sommes) && ! empty($sommes)){ + foreach($sommes as $code => $det){ + $vars[$code] = $det['N']; + } + } + + $result = strtr($formula, $vars); + + $r = $AccCat->calculate($result); + + print '' . price($r) . ''; + $sommes[$code]['N'] += $r; + + // Detail by month + foreach($months as $k => $v){ + foreach($sommes as $code => $det){ + $vars[$code] = $det['M'][$k]; + } + $result = strtr($formula, $vars); + $r = $AccCat->calculate($result); + print '' . price($r) . ''; + $sommes[$code]['M'][$k] += $r; + } + + print "\n"; - $position = -1; - $code = -1; + + + }else{ // normal category + + $totCat = array(); + $totCat['M'] = array(); + + // get cpts of category + $cpts = $AccCat->getCptsCat($cat['rowid']); + + + print ""; + print '' . $cat['label'] . ''; + foreach($cpts as $i => $cpt){ + $var = ! $var; - $position = $cpt['position']; - $code = $cpt['code']; - + $code = $cat['code']; + + // N-1 $return = $AccCat->getResult($cpt['account_number'], 0, $year_current -1, $cpt['dc']); + if ($return < 0) { setEventMessages(null, $AccCat->errors, 'errors'); $resultNP=0; } else { $resultNP=$AccCat->sdc; } - + + //N $return = $AccCat->getResult($cpt['account_number'], 0, $year_current, $cpt['dc']); if ($return < 0) { setEventMessages(null, $AccCat->errors, 'errors'); @@ -146,13 +218,64 @@ if (!empty($cats)) } else { $resultN=$AccCat->sdc; } + + $totCat['NP'] += $resultNP; + $totCat['N'] += $resultN; + + foreach($months as $k => $v){ + $return = $AccCat->getResult($cpt['account_number'], $k+1, $year_current, $cpt['dc']); + if ($return < 0) { + setEventMessages(null, $AccCat->errors, 'errors'); + $resultM=0; + } else { + $resultM=$AccCat->sdc; + } + $totCat['M'][$k] += $resultM; + + } + } + + print '' . price($totCat['NP']) . ''; + print '' . price($totCat['N']) . ''; + + foreach($totCat['M'] as $k => $v){ + print '' . price($v) . ''; + } + print "\n"; + + foreach($cpts as $i => $cpt){ + $var = ! $var; + + $code = $cat['code']; + + // N-1 + $return = $AccCat->getResult($cpt['account_number'], 0, $year_current -1, $cpt['dc']); + + if ($return < 0) { + setEventMessages(null, $AccCat->errors, 'errors'); + $resultNP=0; + } else { + $resultNP=$AccCat->sdc; + } + + //N + $return = $AccCat->getResult($cpt['account_number'], 0, $year_current, $cpt['dc']); + if ($return < 0) { + setEventMessages(null, $AccCat->errors, 'errors'); + $resultN=0; + } else { + $resultN=$AccCat->sdc; + } + $sommes[$code]['NP'] += $resultNP; $sommes[$code]['N'] += $resultN; - print ''; - print '' . $cpt['account_number'] . ''; + print ''; + if ($simple_report == 'yes') { + print '' . length_accountg($cpt['account_number']) . ''; print '' . $cpt['name_cpt'] . ''; - print '' . price($resultNP) . ''; - print '' . price($resultN) . ''; + print '' . price($resultNP) . ''; + print '' . price($resultN) . ''; + } foreach($months as $k => $v){ $return = $AccCat->getResult($cpt['account_number'], $k+1, $year_current, $cpt['dc']); @@ -163,102 +286,15 @@ if (!empty($cats)) $resultM=$AccCat->sdc; } $sommes[$code]['M'][$k] += $resultM; + if ($simple_report == 'yes') { print '' . price($resultM) . ''; - } - - print "\n"; - } - - // If it's a calculated catgory - $p = $position + 1; - if(array_key_exists($p, $catsCalcule)){ - $formula = $catsCalcule[$p]['formula']; - - print ""; - print '' . $catsCalcule[$p]['label'] . ''; - - $vars = array(); - - // Previous Fiscal year (N-1) - foreach($sommes as $code => $det){ - $vars[$code] = $det['NP']; - } - $result = strtr($formula, $vars); - eval( '$result = (' . $result . ');' ); - print '' . price($result) . ''; - $code = $catsCalcule[$p]['code']; // code categorie de calcule - $sommes[$code]['NP'] += $result; - - // Current fiscal year (N) - foreach($sommes as $code => $det){ - $vars[$code] = $det['N']; - } - $result = strtr($formula, $vars); - eval( '$result = (' . $result . ');' ); - print '' . price($result) . ''; - $sommes[$code]['N'] += $result; - - // Detail by month - foreach($months as $k => $v){ - foreach($sommes as $code => $det){ - $vars[$code] = $det['M'][$k]; } - $result = strtr($formula, $vars); - eval( '$result = (' . $result . ');' ); - print '' . price($result) . ''; - $sommes[$code]['M'][$k] += $result; } - //print '' . $catsCalcule[$p]['formula'] . ''; print "\n"; - unset($catsCalcule[$p]); // j'élimine la catégorie calculée après affichage } - $j++; - } - - // Others calculed category - foreach($catsCalcule as $p => $catc) - { - $formula = $catsCalcule[$p]['formula']; - - print ""; - print '' . $catsCalcule[$p]['label'] . ''; - - $vars = array(); - - // Previous Fiscal year (N-1) - foreach($sommes as $code => $det){ - $vars[$code] = $det['NP']; - } - $result = strtr($formula, $vars); - eval( '$result = (' . $result . ');' ); - print '' . price($result) . ''; - $code = $catsCalcule[$p]['code']; // code categorie de calcule - $sommes[$code]['NP'] += $result; - - // Current fiscal year (N) - foreach($sommes as $code => $det){ - $vars[$code] = $det['N']; - } - $result = strtr($formula, $vars); - eval( '$result = (' . $result . ');' ); - print '' . price($result) . ''; - $sommes[$code]['N'] += $result; - - // Detail by month - foreach($months as $k => $v){ - foreach($sommes as $code => $det){ - $vars[$code] = $det['M'][$k]; - } - $result = strtr($formula, $vars); - eval( '$result = (' . $result . ');' ); - print '' . price($result) . ''; - $sommes[$code]['M'][$k] += $result; - } - - //print '' . $catsCalcule[$p]['formula'] . ''; - print "\n"; } + } print ""; diff --git a/htdocs/accountancy/supplier/lines.php b/htdocs/accountancy/supplier/lines.php index 9c7a72cb94c..398d12503ab 100644 --- a/htdocs/accountancy/supplier/lines.php +++ b/htdocs/accountancy/supplier/lines.php @@ -52,6 +52,8 @@ $search_desc = GETPOST('search_desc', 'alpha'); $search_amount = GETPOST('search_amount', 'alpha'); $search_account = GETPOST('search_account', 'alpha'); $search_vat = GETPOST('search_vat', 'alpha'); +$search_country = GETPOST('search_country', 'alpha'); +$search_tvaintra = GETPOST('search_tvaintra', 'alpha'); // Load variable for pagination $limit = GETPOST('limit','int')?GETPOST('limit', 'int'):(empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)?$conf->liste_limit:$conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION); @@ -94,6 +96,8 @@ if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETP $search_amount = ''; $search_account = ''; $search_vat = ''; + $search_country = ''; + $search_tvaintra = ''; } if (is_array($changeaccount) && count($changeaccount) > 0) { @@ -149,13 +153,15 @@ print '