Fix stuff in accoutancy

This commit is contained in:
florian HENRY 2016-06-29 16:54:37 +02:00
parent dfc4f3e7fe
commit ea3e851ec0
4 changed files with 290 additions and 264 deletions

View File

@ -36,7 +36,7 @@ $langs->load("accountancy");
$mesg = ''; $mesg = '';
$action = GETPOST('action'); $action = GETPOST('action');
$cat_id = GETPOST('account_category'); $cat_id = GETPOST('account_category');
$selectcpt = GETPOST('cpt_bk'); $selectcpt = GETPOST('cpt_bk', 'array');
$cpt_id = GETPOST('cptid'); $cpt_id = GETPOST('cptid');
if ($cat_id == 0) { if ($cat_id == 0) {
@ -56,19 +56,18 @@ $AccCat = new AccountancyCategory($db);
// si ajout de comptes // si ajout de comptes
if (! empty($selectcpt)) { if (! empty($selectcpt)) {
$cpts = array (); $cpts = array ();
$i = 0;
foreach ( $selectcpt as $selectedOption ) { foreach ( $selectcpt as $selectedOption ) {
$cpts[$i] = "'".$selectedOption."'"; if (! array_key_exists($selectedOption, $cpts))
$i++; $cpts[$selectedOption] = "'" . $selectedOption . "'";
} }
if($AccCat->updateAccAcc($cat_id, $cpts)){ $return= $AccCat->updateAccAcc($cat_id, $cpts);
setEventMessages($langs->trans('Saved'), null, 'mesgs');
if ($return<0) {
setEventMessages($langs->trans('errors'), $AccCat->errors, 'errors');
} else { } else {
setEventMessages($langs->trans('errors'), null, 'errors'); setEventMessages($langs->trans('Saved'), null, 'mesgs');
} }
} }
if ($action == 'delete') { if ($action == 'delete') {
if ($cpt_id) { if ($cpt_id) {
@ -104,16 +103,17 @@ $form = new Form($db);
print '<input class="button" type="submit" value="' . $langs->trans("Display") . '">'; print '<input class="button" type="submit" value="' . $langs->trans("Display") . '">';
print '</td></tr>'; print '</td></tr>';
if (! empty($cat_id)) { if (! empty($cat_id)) {
$obj = $AccCat->getCptBK($cat_id); $return = $AccCat->getCptBK($cat_id);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
}
print '<tr><td>' . $langs->trans("AddCompteFromBK") . '</td>'; print '<tr><td>' . $langs->trans("AddCompteFromBK") . '</td>';
print '<td>'; print '<td>';
if(!empty($obj)){ if (is_array($AccCat->lines_cptbk) && count($AccCat->lines_cptbk) > 0) {
print '<select size="' . count($obj) . '" name="cpt_bk[]" multiple>'; print '<select size="' . count($obj) . '" name="cpt_bk[]" multiple>';
foreach ( $obj as $cpt ) { foreach ( $AccCat->lines_cptbk as $cpt ) {
print '<option value="' . length_accountg($cpt->numero_compte) . '">' . length_accountg($cpt->numero_compte) . ' (' . $cpt->label_compte . ' ' . $cpt->doc_ref . ')</option>'; print '<option value="' . length_accountg($cpt->numero_compte) . '">' . length_accountg($cpt->numero_compte) . ' (' . $cpt->label_compte . ' ' . $cpt->doc_ref . ')</option>';
} }
print '</select> - <input class="button" type="submit" id="" class="action-delete" value="' . $langs->trans("add") . '"> '; print '</select> - <input class="button" type="submit" id="" class="action-delete" value="' . $langs->trans("add") . '"> ';
} }
@ -133,10 +133,13 @@ if ($action == 'display' || $action == 'delete') {
print '<tr class="liste_titre"><th class="liste_titre">' . $langs->trans("Numerocompte") . '</th><th class="liste_titre">' . $langs->trans("Description") . '</th><th class="liste_titre" width="60" align="center">Action</th></tr>'; print '<tr class="liste_titre"><th class="liste_titre">' . $langs->trans("Numerocompte") . '</th><th class="liste_titre">' . $langs->trans("Description") . '</th><th class="liste_titre" width="60" align="center">Action</th></tr>';
if (! empty($cat_id)) { if (! empty($cat_id)) {
$obj = $AccCat->display($cat_id); $return = $AccCat->display($cat_id);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
}
$j = 1; $j = 1;
if(!empty($obj)){ if (is_array($AccCat->lines_display) && count($AccCat->lines_display) > 0) {
foreach ( $obj as $cpt ) { foreach ( $AccCat->lines_display as $cpt ) {
$var = ! $var; $var = ! $var;
print '<tr' . $bc[$var] . '>'; print '<tr' . $bc[$var] . '>';
print '<td>' . length_accountg($cpt->account_number) . '</td>'; print '<td>' . length_accountg($cpt->account_number) . '</td>';

View File

@ -33,17 +33,19 @@ class AccountancyCategory
private $db; private $db;
public $error; public $error;
public $errors = array (); public $errors = array ();
//public $element='accounting_category'; public $element = 'accounting_category';
//public $table_element='c_accounting_category'; public $table_element = 'c_accounting_category';
public $id; public $id;
public $lines_cptbk;
public $lines_display;
public $sdc;
/** /**
* Constructor * Constructor
* *
* @param DoliDB $db Database handler * @param DoliDB $db Database handler
*/ */
public function __construct($db) public function __construct($db) {
{
$this->db = $db; $this->db = $db;
return 1; return 1;
@ -56,29 +58,28 @@ class AccountancyCategory
* *
* @return int <0 if KO, 0 if not found, >0 if OK * @return int <0 if KO, 0 if not found, >0 if OK
*/ */
public function display($id) public function display($id) {
{
$sql = "SELECT t.rowid, t.account_number, t.label"; $sql = "SELECT t.rowid, t.account_number, t.label";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t"; $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t";
$sql .= " WHERE t.fk_accounting_category = " . $id; $sql .= " WHERE t.fk_accounting_category = " . $id;
$this->lines_display = array ();
dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG); dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$i = 0;
$obj = '';
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
if ($num) { if ($num) {
while ( $i < $num ) { while ( $obj = $this->db->fetch_object($resql) ) {
$obj[$i] = $this->db->fetch_object($resql); $this->lines_display[] = $obj;
$i ++;
} }
} }
return $obj; return $num;
} else { } else {
$this->error = "Error " . $this->db->lasterror(); $this->error = "Error " . $this->db->lasterror();
dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR); $this->errors[] = $this->error;
dol_syslog(__METHOD__ . " " . implode(',' . $this->errors), LOG_ERR);
return - 1; return - 1;
} }
@ -91,8 +92,7 @@ class AccountancyCategory
* *
* @return int <0 if KO, 0 if not found, >0 if OK * @return int <0 if KO, 0 if not found, >0 if OK
*/ */
public function getCptBK($id) public function getCptBK($id) {
{
global $conf; global $conf;
$sql = "SELECT t.numero_compte, t.label_compte, t.doc_ref"; $sql = "SELECT t.numero_compte, t.label_compte, t.doc_ref";
@ -107,25 +107,26 @@ class AccountancyCategory
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version"; $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
$sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS; $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
$sql .= " AND aa.active = 1)"; $sql .= " AND aa.active = 1)";
$sql.= " GROUP BY t.numero_compte"; $sql .= " GROUP BY t.numero_compte, t.label_compte, t.doc_ref";
$sql .= " ORDER BY t.numero_compte";
dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG); $this->lines_CptBk = array ();
dol_syslog(__METHOD__, LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$i = 0;
$obj = '';
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
if ($num) { if ($num) {
while ( $i < $num ) { while ( $obj = $this->db->fetch_object($resql) ) {
$obj[$i] = $this->db->fetch_object($resql); $this->lines_cptbk[] = $obj;
$i ++;
} }
} }
return $obj; return $num;
} else { } else {
$this->error = "Error " . $this->db->lasterror(); $this->error = "Error " . $this->db->lasterror();
dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR); $this->errors[] = $this->error;
dol_syslog(__METHOD__ . " " . implode(',' . $this->errors), LOG_ERR);
return - 1; return - 1;
} }
@ -139,24 +140,39 @@ class AccountancyCategory
* *
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function updateAccAcc($id_cat, $cpts = array()) public function updateAccAcc($id_cat, $cpts = array()) {
{
global $conf; global $conf;
$error = 0; $error = 0;
$sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account as aa"; require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
$sql = "SELECT aa.rowid,aa.account_number ";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version"; $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
$sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS; $sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
$sql .= " AND aa.active = 1"; $sql .= " AND aa.active = 1";
$sql.= " SET fk_accounting_category=" . $id_cat;
$sql.= " WHERE aa.account_number IN (" . join(',',$cpts) .")";
$this->db->begin();
dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG); dol_syslog(__METHOD__, LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if (! $resql) { if (! $resql) {
$error ++; $error ++;
$this->errors[] = "Error " . $this->db->lasterror(); $this->errors[] = "Error " . $this->db->lasterror();
return -1;
}
$this->db->begin();
while ( $obj = $this->db->fetch_object($resql)) {
if (array_key_exists(length_accountg($obj->account_number), $cpts)) {
$sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account";
$sql .= " SET fk_accounting_category=" . $id_cat;
$sql .= " WHERE rowid=".$obj->rowid;
dol_syslog(__METHOD__, LOG_DEBUG);
$resql = $this->db->query($sql);
if (! $resql) {
$error ++;
$this->errors[] = "Error " . $this->db->lasterror();
}
}
} }
// Commit or rollback // Commit or rollback
@ -182,8 +198,7 @@ class AccountancyCategory
* *
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function deleteCptCat($cpt_id) public function deleteCptCat($cpt_id) {
{
$error = 0; $error = 0;
$sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account as aa"; $sql = "UPDATE " . MAIN_DB_PREFIX . "accounting_account as aa";
@ -219,19 +234,16 @@ class AccountancyCategory
* *
* @return array Result in table * @return array Result in table
*/ */
public function getCatsCpts() public function getCatsCpts() {
{
global $mysoc; global $mysoc;
$sql = ""; $sql = "";
if (empty($mysoc->country_id) && empty($mysoc->country_code)) if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
{
dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined'); dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
exit; exit();
} }
if (! empty($mysoc->country_id)) if (! empty($mysoc->country_id)) {
{
$sql = "SELECT t.rowid, t.account_number, t.label as name_cpt, cat.code, cat.position, cat.label as name_cat, cat.sens "; $sql = "SELECT t.rowid, t.account_number, t.label as name_cpt, cat.code, cat.position, cat.label as name_cat, cat.sens ";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t, " . MAIN_DB_PREFIX . "c_accounting_category as cat"; $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as t, " . MAIN_DB_PREFIX . "c_accounting_category as cat";
$sql .= " WHERE t.fk_accounting_category IN ( SELECT c.rowid "; $sql .= " WHERE t.fk_accounting_category IN ( SELECT c.rowid ";
@ -240,9 +252,7 @@ class AccountancyCategory
$sql .= " AND c.fk_country = " . $mysoc->country_id . ")"; $sql .= " AND c.fk_country = " . $mysoc->country_id . ")";
$sql .= " AND cat.rowid = t.fk_accounting_category"; $sql .= " AND cat.rowid = t.fk_accounting_category";
$sql .= " ORDER BY cat.position ASC"; $sql .= " ORDER BY cat.position ASC";
} } else {
else
{
$sql = "SELECT c.rowid, c.code, c.label, c.category_type "; $sql = "SELECT c.rowid, c.code, c.label, c.category_type ";
$sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c, " . MAIN_DB_PREFIX . "c_country as co"; $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 .= " WHERE c.active = 1 AND c.fk_country = co.rowid";
@ -257,8 +267,7 @@ class AccountancyCategory
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
$data = array (); $data = array ();
if ($num) { if ($num) {
while ( $i < $num ) { while ( $obj = $this->db->fetch_object($resql) ) {
$obj = $this->db->fetch_object($resql);
$name_cat = $obj->name_cat; $name_cat = $obj->name_cat;
$data[$name_cat][$i] = array ( $data[$name_cat][$i] = array (
'id' => $obj->rowid, 'id' => $obj->rowid,
@ -266,7 +275,7 @@ class AccountancyCategory
'position' => $obj->position, 'position' => $obj->position,
'account_number' => $obj->account_number, 'account_number' => $obj->account_number,
'name_cpt' => $obj->name_cpt, 'name_cpt' => $obj->name_cpt,
'sens' => $obj->sens, 'sens' => $obj->sens
); );
$i ++; $i ++;
} }
@ -290,11 +299,10 @@ class AccountancyCategory
* *
* @return array Result in table * @return array Result in table
*/ */
public function getResult($cpt, $month, $year, $sens) public function getResult($cpt, $month, $year, $sens) {
{
$sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit"; $sql = "SELECT SUM(t.debit) as debit, SUM(t.credit) as credit";
$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as t"; $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as t";
$sql.= " WHERE t.numero_compte = " . $cpt; $sql .= " WHERE t.numero_compte = '" . $cpt."'";
$sql .= " AND YEAR(t.doc_date) = " . $year; $sql .= " AND YEAR(t.doc_date) = " . $year;
if (! empty($month)) { if (! empty($month)) {
@ -306,16 +314,16 @@ class AccountancyCategory
if ($resql) { if ($resql) {
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
$sdc = 0; $this->sdc = 0;
if ($num) { if ($num) {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
if ($sens == 1) { if ($sens == 1) {
$sdc = $obj->debit - $obj->credit; $this->sdc = $obj->debit - $obj->credit;
} else { } else {
$sdc = $obj->credit - $obj->debit; $this->sdc = $obj->credit - $obj->debit;
} }
} }
return $sdc; return $num;
} else { } else {
$this->error = "Error " . $this->db->lasterror(); $this->error = "Error " . $this->db->lasterror();
dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR); dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
@ -329,26 +337,21 @@ class AccountancyCategory
* *
* @return array Result in table * @return array Result in table
*/ */
public function getCatsCal() public function getCatsCal() {
{
global $db, $langs, $user, $mysoc; global $db, $langs, $user, $mysoc;
if (empty($mysoc->country_id) && empty($mysoc->country_code)) if (empty($mysoc->country_id) && empty($mysoc->country_code)) {
{
dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined'); dol_print_error('', 'Call to select_accounting_account with mysoc country not yet defined');
exit; exit();
} }
if (! empty($mysoc->country_id)) if (! empty($mysoc->country_id)) {
{
$sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position"; $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position";
$sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c"; $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c";
$sql .= " WHERE c.active = 1 AND c.category_type = 1 "; $sql .= " WHERE c.active = 1 AND c.category_type = 1 ";
$sql .= " AND c.fk_country = " . $mysoc->country_id; $sql .= " AND c.fk_country = " . $mysoc->country_id;
$sql .= " ORDER BY c.position ASC"; $sql .= " ORDER BY c.position ASC";
} } else {
else
{
$sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position"; $sql = "SELECT c.rowid, c.code, c.label, c.formula, c.position";
$sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c, " . MAIN_DB_PREFIX . "c_country as co"; $sql .= " FROM " . MAIN_DB_PREFIX . "c_accounting_category as c, " . MAIN_DB_PREFIX . "c_country as co";
$sql .= " WHERE c.active = 1 AND c.category_type = 1 AND c.fk_country = co.rowid"; $sql .= " WHERE c.active = 1 AND c.category_type = 1 AND c.fk_country = co.rowid";
@ -378,7 +381,8 @@ class AccountancyCategory
return $data; return $data;
} else { } else {
$this->error = "Error " . $this->db->lasterror(); $this->error = "Error " . $this->db->lasterror();
dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR); $this->errors[] = $this->error;
dol_syslog(__METHOD__ . " " . implode(',' . $this->errors), LOG_ERR);
return - 1; return - 1;
} }

View File

@ -123,8 +123,21 @@ if(!empty($cats))
$position = $cpt['position']; $position = $cpt['position'];
$code = $cpt['code']; $code = $cpt['code'];
$resultNP = $AccCat->getResult($cpt['account_number'], 0, $year_current -1, $cpt['dc']); $return = $AccCat->getResult($cpt['account_number'], 0, $year_current -1, $cpt['dc']);
$resultN = $AccCat->getResult($cpt['account_number'], 0, $year_current, $cpt['dc']); if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
$resultNP=0;
} else {
$resultNP=$AccCat->sdc;
}
$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]['NP'] += $resultNP;
$sommes[$code]['N'] += $resultN; $sommes[$code]['N'] += $resultN;
print '<tr'. $bc[$var].'>'; print '<tr'. $bc[$var].'>';
@ -134,7 +147,13 @@ if(!empty($cats))
print '<td>' . price($resultN) . '</td>'; print '<td>' . price($resultN) . '</td>';
foreach($months as $k => $v){ foreach($months as $k => $v){
$resultM = $AccCat->getResult($cpt['account_number'], $k+1, $year_current, $cpt['dc']); $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;
}
$sommes[$code]['M'][$k] += $resultM; $sommes[$code]['M'][$k] += $resultM;
print '<td align="right">' . price($resultM) . '</td>'; print '<td align="right">' . price($resultM) . '</td>';
} }

View File

@ -71,7 +71,7 @@ class modAccounting extends DolibarrModules
$this->requiredby = array(); // List of modules id to disable if this one is disabled $this->requiredby = array(); // List of modules id to disable if this one is disabled
$this->conflictwith = array("modComptabilite"); // List of modules are in conflict with this module $this->conflictwith = array("modComptabilite"); // List of modules are in conflict with this module
$this->phpmin = array(5, 3); // Minimum version of PHP required by module $this->phpmin = array(5, 3); // Minimum version of PHP required by module
$this->need_dolibarr_version = array(3, 7); // Minimum version of Dolibarr required by module $this->need_dolibarr_version = array(3, 9); // Minimum version of Dolibarr required by module
$this->langfiles = array("accountancy"); $this->langfiles = array("accountancy");
// Constants // Constants