From c521b9b8f8285e5e2ece2f034bada99489b6dcd3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 20 Jun 2019 16:58:51 +0200 Subject: [PATCH] Fix dispatch in ledger table of withdraw / direct debit payment --- htdocs/accountancy/journal/bankjournal.php | 56 ++++++++++++++++--- .../journal/expensereportsjournal.php | 4 -- .../accountancy/journal/purchasesjournal.php | 2 - htdocs/accountancy/journal/sellsjournal.php | 2 - .../compta/paiement/class/paiement.class.php | 46 +++++++++++++-- htdocs/langs/en_US/accountancy.lang | 3 +- 6 files changed, 90 insertions(+), 23 deletions(-) diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php index 73f3f9bbfc5..6b5d63bce0f 100644 --- a/htdocs/accountancy/journal/bankjournal.php +++ b/htdocs/accountancy/journal/bankjournal.php @@ -107,8 +107,6 @@ if (! GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end) $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); } -$idpays = $mysoc->country_id; - $sql = "SELECT b.rowid, b.dateo as do, b.datev as dv, b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type, b.fk_account,"; $sql .= " ba.courant, ba.ref as baref, ba.account_number, ba.fk_accountancy_journal,"; $sql .= " soc.code_compta, soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, soc.email as email, bu1.type as typeop_company,"; @@ -154,6 +152,9 @@ $paymentloanstatic = new PaymentLoan($db); $accountLinestatic=new AccountLine($db); $paymentsubscriptionstatic = new Subscription($db); +$tmppayment = new Paiement($db); +$tmpinvoice = new Facture($db); + $accountingaccount = new AccountingAccount($db); // Get code of finance journal @@ -185,13 +186,14 @@ if ($result) { $tabbq = array (); $tabtp = array (); $tabtype = array (); + $tabmoreinfo = array(); // Loop on each line into llx_bank table. For each line, we should get: // one line tabpay = line into bank // one line for bank record = tabbq // one line for thirdparty record = tabtp $i = 0; - while ( $i < $num ) + while ($i < $num) { $obj = $db->fetch_object($result); @@ -215,7 +217,7 @@ if ($result) { // Set accountancy code for bank $compta_bank = $obj->account_number; - // Set accountancy code for thirdparty + // Set accountancy code for thirdparty (example: '411CU...' or '411' if no subledger account defined on customer) $compta_soc = 'NotDefined'; if ($lineisapurchase > 0) $compta_soc = (($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $account_supplier); @@ -257,10 +259,12 @@ if ($result) { //var_dump($i); //var_dump($tabpay); + //var_dump($tabcompany); // By default $tabpay[$obj->rowid]['type'] = 'unknown'; // Can be SOLD, miscellaneous entry, payment of patient, or any old record with no links in bank_url. $tabtype[$obj->rowid] = 'unknown'; + $tabmoreinfo[$obj->rowid] = array(); // get_url may return -1 which is not traversable if (is_array($links) && count($links) > 0) { @@ -285,11 +289,17 @@ if ($result) { } } + if ($links[$key]['type'] == 'withdraw') { + $tabmoreinfo[$obj->rowid]['withdraw']=1; + } + if ($links[$key]['type'] == 'payment') { $paymentstatic->id = $links[$key]['url_id']; $paymentstatic->ref = $links[$key]['url_id']; $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentstatic->getNomUrl(2, '', ''); // TODO Do not include list of invoice in tooltip, the dol_string_nohtmltag is ko with this $tabpay[$obj->rowid]["paymentid"] = $paymentstatic->id; + + } elseif ($links[$key]['type'] == 'payment_supplier') { $paymentsupplierstatic->id = $links[$key]['url_id']; $paymentsupplierstatic->ref = $links[$key]['url_id']; @@ -411,8 +421,30 @@ if ($result) { $tabbq[$obj->rowid][$compta_bank] += $obj->amount; - // If not links were found to know amount on thirdparty, we init it. - if (empty($tabtp[$obj->rowid])) $tabtp[$obj->rowid]['NotDefined']= $tabbq[$obj->rowid][$compta_bank]; + // If no links were found to know the amount on thirdparty, we try to guess it. + // This may happens on bank entries without the links lines to 'company'. + if (empty($tabtp[$obj->rowid]) && ! empty($tabmoreinfo[$obj->rowid]['withdraw'])) // If we dont find 'company' link because it is an old 'withdraw' record + { + foreach ($links as $key => $val) { + if ($links[$key]['type'] == 'payment') { + // Get thirdparty + $tmppayment->fetch($links[$key]['url_id']); + $arrayofamounts = $tmppayment->getAmountsArray(); + foreach($arrayofamounts as $invoiceid => $amount) + { + $tmpinvoice->fetch($invoiceid); + $tmpinvoice->fetch_thirdparty(); + if ($tmpinvoice->thirdparty->code_compta) + { + $tabtp[$obj->rowid][$tmpinvoice->thirdparty->code_compta] += $amount; + } + } + } + } + } + + // If no links were found to know the amount on thirdparty, we init it to account 'NotDefined'. + if (empty($tabtp[$obj->rowid])) $tabtp[$obj->rowid]['NotDefined'] = $tabbq[$obj->rowid][$compta_bank]; // Check account number is ok /*if ($action == 'writebookkeeping') // Make test now in such a case @@ -701,6 +733,7 @@ if (! $error && $action == 'writebookkeeping') { $totalcredit += $bookkeeping->credit; $result = $bookkeeping->create($user); + if ($result < 0) { if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists { @@ -1071,7 +1104,6 @@ if (empty($action) || $action == 'view') { if ($tabtype[$key] == 'payment_salary') $account_ledger = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT; if ($tabtype[$key] == 'payment_vat') $account_ledger = $conf->global->ACCOUNTING_VAT_PAY_ACCOUNT; if ($tabtype[$key] == 'member') $account_ledger = $conf->global->ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT; - $accounttoshow = length_accounta($account_ledger); if (empty($accounttoshow) || $accounttoshow == 'NotDefined') { @@ -1104,6 +1136,7 @@ if (empty($action) || $action == 'view') { print ""; // Subledger account print ""; + if (in_array($tabtype[$key], array('payment', 'payment_supplier', 'payment_expensereport', 'payment_salary'))) // Type of payment with subledger { $accounttoshowsubledger = length_accounta($k); @@ -1115,7 +1148,14 @@ if (empty($action) || $action == 'view') { var_dump($tabtype[$key]); var_dump($tabbq[$key]);*/ //print ''.$langs->trans("ThirdpartyAccountNotDefined").''; - print ''.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknown").''; + if (! empty($tabcompany[$key]['code_compta'])) + { + print ''.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknown", $tabcompany[$key]['code_compta']).''; + } + else + { + print ''.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownBlocking").''; + } } else print $accounttoshowsubledger; } diff --git a/htdocs/accountancy/journal/expensereportsjournal.php b/htdocs/accountancy/journal/expensereportsjournal.php index 3f4632a0103..c06506c0895 100644 --- a/htdocs/accountancy/journal/expensereportsjournal.php +++ b/htdocs/accountancy/journal/expensereportsjournal.php @@ -87,15 +87,11 @@ if (! GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end) $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); } -$idpays = $mysoc->country_id; - $sql = "SELECT er.rowid, er.ref, er.date_debut as de,"; $sql .= " erd.rowid as erdid, erd.comments, erd.total_ht, erd.total_tva, erd.total_localtax1, erd.total_localtax2, erd.tva_tx, erd.total_ttc, erd.fk_code_ventilation, erd.vat_src_code, "; $sql .= " u.rowid as uid, u.firstname, u.lastname, u.accountancy_code as user_accountancy_account,"; $sql .= " f.accountancy_code, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte"; -//$sql .= " ct.accountancy_code_buy as account_tva"; $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd"; -//$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_tva as ct ON erd.tva_tx = ct.taux AND ct.fk_pays = '" . $idpays . "'"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_type_fees as f ON f.id = erd.fk_c_type_fees"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = erd.fk_code_ventilation"; $sql .= " JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport"; diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index 66a54934e1c..8ff70219c68 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -92,8 +92,6 @@ if (! GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end) $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); } -$idpays = $mysoc->country_id; - $sql = "SELECT f.rowid, f.ref as ref, f.type, f.datef as df, f.libelle,f.ref_supplier, f.date_lim_reglement as dlf, f.close_code,"; $sql .= " fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.tva as total_tva, fd.total_localtax1, fd.total_localtax2, fd.tva_tx, fd.total_ttc, fd.vat_src_code,"; $sql .= " s.rowid as socid, s.nom as name, s.fournisseur, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur,"; diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php index 57a2ff1586b..b69f485a558 100644 --- a/htdocs/accountancy/journal/sellsjournal.php +++ b/htdocs/accountancy/journal/sellsjournal.php @@ -95,8 +95,6 @@ if (! GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end) $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); } -$idpays = $mysoc->country_id; - $sql = "SELECT f.rowid, f.ref, f.type, f.datef as df, f.ref_client, f.date_lim_reglement as dlr, f.close_code,"; $sql .= " fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.total_tva, fd.total_localtax1, fd.total_localtax2, fd.tva_tx, fd.total_ttc, fd.situation_percent, fd.vat_src_code,"; $sql .= " s.rowid as socid, s.nom as name, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur,"; diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php index 28417a6d4f1..a3c79d23602 100644 --- a/htdocs/compta/paiement/class/paiement.class.php +++ b/htdocs/compta/paiement/class/paiement.class.php @@ -991,16 +991,16 @@ class Paiement extends CommonObject } /** - * Retourne la liste des factures sur lesquels porte le paiement + * Return list of invoices the payment is related to. * - * @param string $filter Critere de filtre - * @return array Tableau des id de factures + * @param string $filter Filter + * @return int|array <0 if KO or array of invoice id */ public function getBillsArray($filter = '') { - $sql = 'SELECT fk_facture'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'facture as f'; - $sql.= ' WHERE pf.fk_facture = f.rowid AND fk_paiement = '.$this->id; + $sql = 'SELECT pf.fk_facture'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'facture as f'; // We keep link on invoice to allow use of some filters on invoice + $sql.= ' WHERE pf.fk_facture = f.rowid AND pf.fk_paiement = '.$this->id; if ($filter) $sql.= ' AND '.$filter; $resql = $this->db->query($sql); if ($resql) @@ -1026,6 +1026,40 @@ class Paiement extends CommonObject } } + /** + * Return list of amounts of payments. + * + * @return int|array Array of amount of payments + */ + public function getAmountsArray() + { + $sql = 'SELECT pf.fk_facture, pf.amount'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf'; + $sql.= ' WHERE pf.fk_paiement = '.$this->id; + $resql = $this->db->query($sql); + if ($resql) + { + $i=0; + $num=$this->db->num_rows($resql); + $amounts = array(); + + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + $amounts[$obj->fk_facture]=$obj->amount; + $i++; + } + + return $amounts; + } + else + { + $this->error=$this->db->error(); + dol_syslog(get_class($this).'::getAmountsArray Error '.$this->error.' -', LOG_DEBUG); + return -1; + } + } + /** * Return next reference of customer invoice not already used (or last reference) * according to numbering module defined into constant FACTURE_ADDON diff --git a/htdocs/langs/en_US/accountancy.lang b/htdocs/langs/en_US/accountancy.lang index 3a08e422600..3bb0df59812 100644 --- a/htdocs/langs/en_US/accountancy.lang +++ b/htdocs/langs/en_US/accountancy.lang @@ -216,7 +216,8 @@ DescThirdPartyReport=Consult here the list of third-party customers and vendors ListAccounts=List of the accounting accounts UnknownAccountForThirdparty=Unknown third-party account. We will use %s UnknownAccountForThirdpartyBlocking=Unknown third-party account. Blocking error -ThirdpartyAccountNotDefinedOrThirdPartyUnknown=Third-party account not defined or third party unknown. Blocking error. +ThirdpartyAccountNotDefinedOrThirdPartyUnknown=Third-party account not defined or third party unknown. We will use %s +ThirdpartyAccountNotDefinedOrThirdPartyUnknownBlocking=Third-party account not defined or third party unknown. Blocking error. UnknownAccountForThirdpartyAndWaitingAccountNotDefinedBlocking=Unknown third-party account and waiting account not defined. Blocking error PaymentsNotLinkedToProduct=Payment not linked to any product / service