diff --git a/htdocs/accountancy/bookkeeping/list.php b/htdocs/accountancy/bookkeeping/list.php index cf7b068c9740d..53ff4cbee24e3 100644 --- a/htdocs/accountancy/bookkeeping/list.php +++ b/htdocs/accountancy/bookkeeping/list.php @@ -210,11 +210,16 @@ $deljournal=0; } - if (! empty($delyear) || ! empty($deljournal)) { + if (! empty($delyear) || ! empty($deljournal)) + { $result = $object->deleteByYearAndJournal($delyear,$deljournal); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } + else + { + setEventMessages("RecordDeleted", null, 'mesgs'); + } Header("Location: list.php"); exit; } diff --git a/htdocs/accountancy/class/bookkeeping.class.php b/htdocs/accountancy/class/bookkeeping.class.php index 7b8aa12dca26b..fb8934f39a27b 100644 --- a/htdocs/accountancy/class/bookkeeping.class.php +++ b/htdocs/accountancy/class/bookkeeping.class.php @@ -116,7 +116,6 @@ public function create(User $user, $notrigger = false) { $error = 0; // Clean parameters - if (isset($this->doc_type)) { $this->doc_type = trim($this->doc_type); } @@ -163,6 +162,14 @@ public function create(User $user, $notrigger = false) { $this->piece_num = trim($this->piece_num); } + // Check parameters + if (empty($this->numero_compte)) + { + $this->errors[]='ErrorFieldAccountNotDefined'; + return -1; + } + + $this->db->begin(); $this->piece_num = 0; @@ -174,7 +181,6 @@ public function create(User $user, $notrigger = false) { $sql .= " AND fk_docdet = " . $this->fk_docdet; $sql .= " AND numero_compte = '" . $this->numero_compte . "'"; - dol_syslog(get_class($this) . ":: create sql=" . $sql, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { @@ -274,11 +280,11 @@ public function create(User $user, $notrigger = false) { $this->errors[] = 'Error ' . $this->db->lasterror(); dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR); } - } else { - $result = - 3; - $error ++; - $this->errors[] = 'Error ' . $this->db->lasterror(); - dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR); + } else { // Already exists + $result = -3; + $error++; + $this->errors[] = 'Error Transaction for ('.$this->doc_type.', '.$this->doc_ref.', '.$this->fk_docdet.') were already recorded'; + dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_WARNING); } } else { $result = - 5; @@ -1012,7 +1018,7 @@ public function delete(User $user, $notrigger = false) { /** * Delete bookkepping by importkey * - * @param string $importkey Import key + * @param string $importkey Import key * @return int Result */ function deleteByImportkey($importkey) { @@ -1027,10 +1033,7 @@ function deleteByImportkey($importkey) { if (! $resql) { $this->errors[] = "Error " . $this->db->lasterror(); - foreach ( $this->errors as $errmsg ) { - dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); - $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); - } + dol_syslog(get_class($this)."::delete Error " . $this->db->lasterror(), LOG_ERR); $this->db->rollback(); return - 1; } @@ -1042,17 +1045,24 @@ function deleteByImportkey($importkey) { /** * Delete bookkepping by year * - * @param string $delyear year to delete - * @return int Result + * @param string $delyear Year to delete + * @param string $journal Journal to delete + * @return int Result */ - function deleteByYear($delyear) { + function deleteByYearAndJournal($delyear='', $journal='') { + if (empty($delyear) && empty($journal)) + { + return -1; + } + $this->db->begin(); // first check if line not yet in bookkeeping $sql = "DELETE"; - $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element; - $sql .= " WHERE YEAR(doc_date) = " . $delyear; - + $sql.= " FROM " . MAIN_DB_PREFIX . $this->table_element; + $sql.= " WHERE 1 = 1"; + if ($delyear) $sql.= " AND YEAR(doc_date) = " . $delyear; // FIXME Must use between + if ($journal) $sql.= " AND code_journal = ".$journal; $resql = $this->db->query($sql); if (! $resql) { @@ -1062,7 +1072,7 @@ function deleteByYear($delyear) { $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); } $this->db->rollback(); - return - 1; + return -1; } $this->db->commit(); diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php index 136fa27c314ad..a0c06c1e0ceb4 100644 --- a/htdocs/accountancy/journal/bankjournal.php +++ b/htdocs/accountancy/journal/bankjournal.php @@ -42,6 +42,7 @@ require_once DOL_DOCUMENT_ROOT . '/don/class/paymentdonation.class.php'; require_once DOL_DOCUMENT_ROOT . '/compta/tva/class/tva.class.php'; require_once DOL_DOCUMENT_ROOT . '/compta/salaries/class/paymentsalary.class.php'; +require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php'; require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php'; require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.class.php'; @@ -73,9 +74,13 @@ if ($user->societe_id > 0 && empty($id_bank_account)) accessforbidden(); + /* * View */ + +$error = 0; + $year_current = strftime("%Y", dol_now()); $pastmonth = strftime("%m", dol_now()) - 1; $pastmonthyear = $year_current; @@ -96,8 +101,9 @@ $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY); $idpays = $p[0]; -$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, soc.code_compta, ba.courant,"; -$sql .= " soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, ba.account_number, bu1.type as typeop"; +$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, soc.code_compta,"; +$sql .= " ba.courant, ba.ref as baref, ba.account_number,"; +$sql .= " soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, bu1.type as typeop"; $sql .= " FROM " . MAIN_DB_PREFIX . "bank as b"; $sql .= " JOIN " . MAIN_DB_PREFIX . "bank_account as ba on b.fk_account=ba.rowid"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu1 ON bu1.fk_bank = b.rowid AND bu1.type='company'"; @@ -138,28 +144,33 @@ $account_pay_donation = (! empty($conf->global->DONATION_ACCOUNTINGACCOUNT) ? $conf->global->DONATION_ACCOUNTINGACCOUNT : $langs->trans("CodeNotDef")); $account_transfer = (! empty($conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH) ? $conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH : $langs->trans("CodeNotDef")); + $tabcompany = array(); $tabpay = array (); $tabbq = array (); $tabtp = array (); $tabtype = array (); + // Loop on each line into bank account. For each line, we should get: + // on line tabpay = line into bank + // one line for bank jounral = tabbq + // one line for thirdparty journal = tabtp $i = 0; while ( $i < $num ) { $obj = $db->fetch_object($result); - $tabcompany[$obj->rowid] = array ( - 'id' => $obj->socid, - 'name' => $obj->name, - 'code_client' => $obj->code_compta - ); - - // Controls + // Set accountancy code (for bank and thirdparty) $compta_bank = $obj->account_number; if ($obj->label == '(SupplierInvoicePayment)') $compta_soc = (! empty($obj->code_compta_fournisseur) ? $obj->code_compta_fournisseur : $account_supplier); if ($obj->label == '(CustomerInvoicePayment)') $compta_soc = (! empty($obj->code_compta) ? $obj->code_compta : $account_customer); + $tabcompany[$obj->rowid] = array ( + 'id' => $obj->socid, + 'name' => $obj->name, + 'code_compta' => $compta_soc, + ); + // Variable bookkeeping $tabpay[$obj->rowid]["date"] = $obj->do; $tabpay[$obj->rowid]["type_payment"] = $obj->fk_type; @@ -174,16 +185,24 @@ // get_url may return -1 which is not traversable if (is_array($links)) { + // Now loop on each link of record in bank. foreach ( $links as $key => $val ) { - $tabtype[$obj->rowid] = $links[$key]['type']; + + if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat'))) // So we excluded 'company' here + { + // We save tabtype for a future use, to remember what kind of payment it is + $tabtype[$obj->rowid] = $links[$key]['type']; + } if ($links[$key]['type'] == 'payment') { $paymentstatic->id = $links[$key]['url_id']; $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentstatic->getNomUrl(2); + $tabpay[$obj->rowid]["paymentid"] = $paymentstatic->id; } else if ($links[$key]['type'] == 'payment_supplier') { $paymentsupplierstatic->id = $links[$key]['url_id']; $paymentsupplierstatic->ref = $links[$key]['url_id']; $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentsupplierstatic->getNomUrl(2); + $tabpay[$obj->rowid]["paymentsupplierid"] = $paymentsupplierstatic->id; } else if ($links[$key]['type'] == 'company') { $societestatic->id = $links[$key]['url_id']; $societestatic->name = $links[$key]['label']; @@ -248,6 +267,26 @@ $tabbq[$obj->rowid][$compta_bank] += $obj->amount; + + // Check account number is ok + /*if ($action == 'writebookkeeping') // Make test now in such a case + { + reset($tabbq[$obj->rowid]); + $first_key_tabbq = key($tabbq[$obj->rowid]); + if (empty($first_key_tabbq)) + { + $error++; + setEventMessages($langs->trans('ErrorAccountancyCodeOnBankAccountNotDefined', $obj->baref), null, 'errors'); + } + reset($tabtp[$obj->rowid]); + $first_key_tabtp = key($tabtp[$obj->rowid]); + if (empty($first_key_tabtp)) + { + $error++; + setEventMessages($langs->trans('ErrorAccountancyCodeOnThirdPartyNotDefined'), null, 'errors'); + } + }*/ + // if($obj->socid)$tabtp[$obj->rowid][$compta_soc] += $obj->amount; $i ++; @@ -256,150 +295,182 @@ dol_print_error($db); } +/* +var_dump($tabpay); +var_dump($tabbq); +var_dump($tabtp); +*/ + + /* * Actions */ // Write bookkeeping -if ($action == 'writebookkeeping') { +if (! $error && $action == 'writebookkeeping') { $now = dol_now(); $error = 0; - foreach ( $tabpay as $key => $val ) { + foreach ( $tabpay as $key => $val ) { // $key is rowid into llx_bank + + $errorforline = 0; + + $db->begin(); + // Bank - foreach ( $tabbq[$key] as $k => $mt ) { - $bookkeeping = new BookKeeping($db); - $bookkeeping->doc_date = $val["date"]; - $bookkeeping->doc_ref = $val["ref"]; - $bookkeeping->doc_type = 'bank'; - $bookkeeping->fk_doc = $key; - $bookkeeping->fk_docdet = $val["fk_bank"]; - $bookkeeping->code_tiers = $tabcompany[$key]['code_client']; - $bookkeeping->numero_compte = $k; - $bookkeeping->label_compte = $compte->label; - $bookkeeping->montant = ($mt < 0 ? - $mt : $mt); - $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C'; - $bookkeeping->debit = ($mt >= 0 ? $mt : 0); - $bookkeeping->credit = ($mt < 0 ? - $mt : 0); - $bookkeeping->code_journal = $journal; - $bookkeeping->fk_user_author = $user->id; - $bookkeeping->date_create = $now; - - if ($tabtype[$key] == 'payment') { - - $sqlmid = 'SELECT fac.facnumber'; - $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac "; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid"; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid"; - $sqlmid .= " WHERE pay.fk_bank=" . $key; - dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); - $resultmid = $db->query($sqlmid); - if ($resultmid) { - $objmid = $db->fetch_object($resultmid); - $bookkeeping->doc_ref = $objmid->facnumber; - } - } else if ($tabtype[$key] == 'payment_supplier') { - - $sqlmid = 'SELECT facf.ref_supplier,facf.ref'; - $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf "; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid"; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid"; - $sqlmid .= " WHERE payf.fk_bank=" . $key; - dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); - $resultmid = $db->query($sqlmid); - if ($resultmid) { - $objmid = $db->fetch_object($resultmid); - $bookkeeping->doc_ref = $objmid->ref_supplier . ' (' . $objmid->ref . ')'; - } - } - - $result = $bookkeeping->create($user); - if ($result < 0) { - $error ++; - setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); - } + if (! $errorforline) + { + // Line into bank account + foreach ( $tabbq[$key] as $k => $mt ) { + $bookkeeping = new BookKeeping($db); + $bookkeeping->doc_date = $val["date"]; + $bookkeeping->doc_ref = $val["ref"]; + $bookkeeping->doc_type = 'bank'; + $bookkeeping->fk_doc = $key; + $bookkeeping->fk_docdet = $val["fk_bank"]; + $bookkeeping->numero_compte = $k; + $bookkeeping->label_compte = $compte->label; + $bookkeeping->montant = ($mt < 0 ? - $mt : $mt); + $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C'; + $bookkeeping->debit = ($mt >= 0 ? $mt : 0); + $bookkeeping->credit = ($mt < 0 ? - $mt : 0); + $bookkeeping->code_journal = $journal; + $bookkeeping->fk_user_author = $user->id; + $bookkeeping->date_create = $now; + + if ($tabtype[$key] == 'payment') { + $bookkeeping->code_tiers = $tabcompany[$key]['code_compta']; + + $sqlmid = 'SELECT fac.facnumber'; + $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac "; + $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid"; + $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid"; + $sqlmid .= " WHERE pay.fk_bank=" . $key; + dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); + $resultmid = $db->query($sqlmid); + if ($resultmid) { + $objmid = $db->fetch_object($resultmid); + $bookkeeping->doc_ref = $objmid->facnumber; // Ref of invoice + } + } else if ($tabtype[$key] == 'payment_supplier') { + $bookkeeping->code_tiers = $tabcompany[$key]['code_compta']; + + $sqlmid = 'SELECT facf.ref_supplier, facf.ref'; + $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf "; + $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid"; + $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid"; + $sqlmid .= " WHERE payf.fk_bank=" . $key; + dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); + $resultmid = $db->query($sqlmid); + if ($resultmid) { + $objmid = $db->fetch_object($resultmid); + $bookkeeping->doc_ref = $objmid->ref_supplier . ' (' . $objmid->ref . ')'; // Ref on invoice + } + } + + $result = $bookkeeping->create($user); + if ($result < 0) { + $error++; + $errorforline++; + setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); + } + } } + // Third party - foreach ( $tabtp[$key] as $k => $mt ) { - $bookkeeping = new BookKeeping($db); - $bookkeeping->doc_date = $val["date"]; - $bookkeeping->doc_ref = $val["ref"]; - $bookkeeping->doc_type = 'bank'; - $bookkeeping->fk_doc = $key; - $bookkeeping->fk_docdet = $val["fk_bank"]; - $bookkeeping->label_compte = $tabcompany[$key]['name']; - $bookkeeping->montant = ($mt < 0 ? - $mt : $mt); - $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; - $bookkeeping->debit = ($mt < 0 ? - $mt : 0); - $bookkeeping->credit = ($mt >= 0) ? $mt : 0; - $bookkeeping->code_journal = $journal; - $bookkeeping->fk_user_author = $user->id; - $bookkeeping->date_create = $now; - - if ($tabtype[$key] == 'sc') { - $bookkeeping->code_tiers = ''; - $bookkeeping->numero_compte = $k; - } else if ($tabtype[$key] == 'payment') { - - $sqlmid = 'SELECT fac.facnumber'; - $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac "; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid"; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid"; - $sqlmid .= " WHERE pay.fk_bank=" . $key; - dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); - $resultmid = $db->query($sqlmid); - if ($resultmid) { - $objmid = $db->fetch_object($resultmid); - $bookkeeping->doc_ref = $objmid->facnumber; - } - $bookkeeping->code_tiers = $k; - $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER; - } else if ($tabtype[$key] == 'payment_supplier') { - - $sqlmid = 'SELECT facf.ref_supplier,facf.ref'; - $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf "; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid"; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid"; - $sqlmid .= " WHERE payf.fk_bank=" . $key; - dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); - $resultmid = $db->query($sqlmid); - if ($resultmid) { - $objmid = $db->fetch_object($resultmid); - $bookkeeping->doc_ref = $objmid->ref_supplier . ' (' . $objmid->ref . ')'; - } - $bookkeeping->code_tiers = $k; - $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER; - } else if ($tabtype[$key] == 'company') { - - $sqlmid = 'SELECT fac.facnumber'; - $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac "; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid"; - $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid"; - $sqlmid .= " WHERE pay.fk_bank=" . $key; - dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); - $resultmid = $db->query($sqlmid); - if ($resultmid) { - $objmid = $db->fetch_object($resultmid); - $bookkeeping->doc_ref = $objmid->facnumber; - } - $bookkeeping->code_tiers = $k; - $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER; - } else { - - $bookkeeping->doc_ref = $k; - $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER; - } - - $result = $bookkeeping->create($user); - if ($result < 0) { - $error ++; - setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); - } + if (! $errorforline) + { + // Line into thirdparty account + foreach ( $tabtp[$key] as $k => $mt ) { + $bookkeeping = new BookKeeping($db); + $bookkeeping->doc_date = $val["date"]; + $bookkeeping->doc_ref = $val["ref"]; + $bookkeeping->doc_type = 'bank'; + $bookkeeping->fk_doc = $key; + $bookkeeping->fk_docdet = $val["fk_bank"]; + $bookkeeping->label_compte = $tabcompany[$key]['name']; + $bookkeeping->montant = ($mt < 0 ? - $mt : $mt); + $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; + $bookkeeping->debit = ($mt < 0 ? - $mt : 0); + $bookkeeping->credit = ($mt >= 0) ? $mt : 0; + $bookkeeping->code_journal = $journal; + $bookkeeping->fk_user_author = $user->id; + $bookkeeping->date_create = $now; + + if (in_array($tabtype[$key], array('sc', 'payment_sc'))) { // If payment is payment of social contribution + $bookkeeping->code_tiers = ''; + $bookkeeping->numero_compte = $k; + } else if ($tabtype[$key] == 'payment') { // If payment is payment of customer invoice, we get ref of invoice + $sqlmid = 'SELECT fac.facnumber'; + $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac "; + $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid"; + $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid"; + $sqlmid .= " WHERE pay.fk_bank=" . $key; + dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); + $resultmid = $db->query($sqlmid); + if ($resultmid) { + $objmid = $db->fetch_object($resultmid); + $bookkeeping->doc_ref = $objmid->facnumber; + } + // FIXME Should be + //$bookkeeping->code_tiers = $k; + //$bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER; + $bookkeeping->code_tiers = $tabcompany[$key]['code_compta']; + $bookkeeping->numero_compte = $k; + } else if ($tabtype[$key] == 'payment_supplier') { // If payment is payment of supplier invoice, we get ref of invoice + + $sqlmid = 'SELECT facf.ref_supplier,facf.ref'; + $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf "; + $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid"; + $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid"; + $sqlmid .= " WHERE payf.fk_bank=" . $key; + dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); + $resultmid = $db->query($sqlmid); + if ($resultmid) { + $objmid = $db->fetch_object($resultmid); + $bookkeeping->doc_ref = $objmid->ref_supplier . ' (' . $objmid->ref . ')'; + } + // FIXME Should be + //$bookkeeping->code_tiers = $k; + //$bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER; + $bookkeeping->code_tiers = $tabcompany[$key]['code_compta']; + $bookkeeping->numero_compte = $k; + } else { + // FIXME Should be a temporary account + $bookkeeping->doc_ref = $k; + //$bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER; + $bookkeeping->numero_compte = 'CodeNotDef'; + } + + $result = $bookkeeping->create($user); + if ($result < 0) { + $error++; + $errorforline++; + setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); + } + } + } + + if (! $errorforline) + { + $db->commit(); + } + else + { + $db->rollback(); } } if (empty($error)) { - setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); + setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); + } + elseif (count($tabpay) == $error) + { + setEventMessages($langs->trans("NoRecordSaved"), null, 'warnings'); + } + else + { + setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); } } // Export @@ -437,7 +508,6 @@ $companystatic->id = $tabcompany[$key]['id']; $companystatic->name = $tabcompany[$key]['name']; - $companystatic->client = $tabcompany[$key]['code_client']; // Bank foreach ( $tabbq[$key] as $k => $mt ) { @@ -564,7 +634,9 @@ } else { $form = new Form($db); - + $invoicestatic = new Facture($db); + $invoicesupplierstatic = new FactureFournisseur($db); + llxHeader('', $langs->trans("FinanceJournal")); $nom = $langs->trans("FinanceJournal") . ' - ' . $bank_code_journal->getNomUrl(1); @@ -609,7 +681,7 @@ function writebookkeeping() { print ""; print "" . $langs->trans("Date") . ""; print "" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")"; - print "" . $langs->trans("Account") . ""; + print "" . $langs->trans("AccountAccounting") . ""; print "" . $langs->trans("Type") . ""; print "" . $langs->trans("PaymentMode") . ""; print "" . $langs->trans("Debit") . "" . $langs->trans("Credit") . ""; @@ -618,9 +690,9 @@ function writebookkeeping() { $var = true; $r = ''; - foreach ( $tabpay as $key => $val ) { + foreach ( $tabpay as $key => $val ) { // $key is rowid in llx_bank $date = dol_print_date($db->jdate($val["date"]), 'day'); - + $reflabel = $val["ref"]; if ($reflabel == '(SupplierInvoicePayment)') { $reflabel = $langs->trans('Supplier'); @@ -637,10 +709,45 @@ function writebookkeeping() { if ($reflabel == '(SubscriptionPayment)') { $reflabel = $langs->trans('SubscriptionPayment'); } + + $ref=$reflabel; + if ($tabtype[$key] == 'payment') + { + $sqlmid = 'SELECT payfac.fk_facture as id'; + $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiement_facture as payfac"; + $sqlmid .= " WHERE payfac.fk_paiement=" . $val["paymentid"]; + dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); + $resultmid = $db->query($sqlmid); + if ($resultmid) { + $objmid = $db->fetch_object($resultmid); + $invoicestatic->fetch($objmid->id); + $ref=$langs->trans("Invoice").' '.$invoicestatic->getNomUrl(1); + } + else dol_print_error($db); + } + elseif ($tabtype[$key] == 'payment_supplier') + { + $sqlmid = 'SELECT payfac.fk_facturefourn as id'; + $sqlmid .= " FROM " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfac"; + $sqlmid .= " WHERE payfac.fk_paiementfourn=" . $val["paymentsupplierid"]; + dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG); + $resultmid = $db->query($sqlmid); + if ($resultmid) { + $objmid = $db->fetch_object($resultmid); + $invoicesupplierstatic->fetch($objmid->id); + $ref=$langs->trans("SupplierInvoice").' '.$invoicesupplierstatic->getNomUrl(1); + } + else dol_print_error($db); + } + + /*$invoicestatic->id = $key; + $invoicestatic->ref = $val["ref"]; + $invoicestatic->type = $val["type"];*/ // Bank foreach ( $tabbq[$key] as $k => $mt ) { - print ""; + + print ""; print "" . $date . ""; print "" . $ref . ""; print "" . length_accountg($k) . ""; @@ -690,4 +797,5 @@ function writebookkeeping() { llxFooter(); } + $db->close(); diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index 63b88c5fd393b..6f73dce7e6fb3 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -168,6 +168,10 @@ foreach ($tabfac as $key => $val) { + $errorforline = 0; + + $db->begin(); + $companystatic = new Societe($db); $invoicestatic = new FactureFournisseur($db); @@ -203,8 +207,9 @@ $result = $bookkeeping->create($user); if ($result < 0) { - $error ++; - setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); + $error++; + $errorforline++; + setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); } } @@ -235,7 +240,8 @@ $result = $bookkeeping->create($user); if ($result < 0) { - $error ++; + $error++; + $errorforline++; setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); } } @@ -266,15 +272,36 @@ $result = $bookkeeping->create($user); if ($result < 0) { - $error ++; + $error++; + $errorforline++; setEventMessages($object->error, $object->errors, 'errors'); } } } + + + + if (! $errorforline) + { + $db->commit(); + } + else + { + $db->rollback(); + } + } if (empty($error)) { - setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); + setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); + } + elseif (count($tabpay) == $error) + { + setEventMessages($langs->trans("NoRecordSaved"), null, 'warnings'); + } + else + { + setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); } } @@ -458,7 +485,7 @@ function writebookkeeping() { // /print "".$langs->trans("JournalNum").""; print "" . $langs->trans("Date") . ""; print "" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")"; - print "" . $langs->trans("Account") . ""; + print "" . $langs->trans("AccountAccounting") . ""; print "" . $langs->trans("Type") . "" . $langs->trans("Debit") . "" . $langs->trans("Credit") . ""; print "\n"; diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php index 755cb6ce81b6b..29ec8d50db173 100644 --- a/htdocs/accountancy/journal/sellsjournal.php +++ b/htdocs/accountancy/journal/sellsjournal.php @@ -198,7 +198,11 @@ $error = 0; foreach ( $tabfac as $key => $val ) { - + + $errorforline = 0; + + $db->begin(); + $companystatic = new Societe($db); $invoicestatic = new Facture($db); @@ -230,7 +234,8 @@ $result = $bookkeeping->create($user); if ($result < 0) { - $error ++; + $error++; + $errorforline++; setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); } } @@ -260,7 +265,8 @@ $result = $bookkeeping->create($user); if ($result < 0) { - $error ++; + $error++; + $errorforline++; setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); } } @@ -290,15 +296,35 @@ $result = $bookkeeping->create($user); if ($result < 0) { - $error ++; + $error++; + $errorforline++; setEventMessages($object->error, $object->errors, 'errors'); } } } + + + if (! $errorforline) + { + $db->commit(); + } + else + { + $db->rollback(); + } + } if (empty($error)) { - setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); + setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); + } + elseif (count($tabpay) == $error) + { + setEventMessages($langs->trans("NoRecordSaved"), null, 'warnings'); + } + else + { + setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); } } @@ -475,7 +501,7 @@ function writebookkeeping() { print ""; print "" . $langs->trans("Date") . ""; print "" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")"; - print "" . $langs->trans("Account") . ""; + print "" . $langs->trans("AccountAccounting") . ""; print "" . $langs->trans("Type") . ""; print "" . $langs->trans("Debit") . ""; print "" . $langs->trans("Credit") . ""; diff --git a/htdocs/compta/bank/class/account.class.php b/htdocs/compta/bank/class/account.class.php index a26062e2bacc3..922f2636767c6 100644 --- a/htdocs/compta/bank/class/account.class.php +++ b/htdocs/compta/bank/class/account.class.php @@ -1183,14 +1183,18 @@ public static function countAccountToReconcile() */ function getNomUrl($withpicto=0, $mode='') { - global $langs; + global $conf, $langs; $result=''; $label = '' . $langs->trans("ShowAccount") . ''; - if (! empty($this->label)) - $label .= '
' . $langs->trans('Account') . ': ' . $this->label; - if (! empty($this->number)) - $label .= '
' . $langs->trans('AccountNumber') . ': ' . $this->number; + $label .= '
' . $langs->trans('BankAccount') . ': ' . $this->label; + $label .= '
' . $langs->trans('AccountNumber') . ': ' . $this->number; + if (! empty($conf->accounting->enabled)) + { + $langs->load("accountancy"); + $label .= '
' . $langs->trans('AccountAccounting') . ': ' . $this->account_number; + $label .= '
' . $langs->trans('AccountancyJournal') . ': ' . $this->accountancy_journal; + } $linkclose = '" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">'; if (empty($mode)) diff --git a/htdocs/install/mysql/migration/4.0.0-5.0.0.sql b/htdocs/install/mysql/migration/4.0.0-5.0.0.sql index 3d2e2cbeedc13..75ad8869ed8ff 100644 --- a/htdocs/install/mysql/migration/4.0.0-5.0.0.sql +++ b/htdocs/install/mysql/migration/4.0.0-5.0.0.sql @@ -121,6 +121,10 @@ create table llx_product_warehouse_properties )ENGINE=innodb; ALTER TABLE llx_accounting_bookkeeping ADD COLUMN entity integer DEFAULT 1 NOT NULL; +ALTER TABLE llx_accounting_bookkeeping ADD COLUMN fk_user_modif integer; +ALTER TABLE llx_accounting_bookkeeping ADD COLUMN date_creation datetime; +ALTER TABLE llx_accounting_bookkeeping ADD COLUMN tms timestamp; + ALTER TABLE llx_accounting_account ADD INDEX uk_accounting_account (account_number, entity); diff --git a/htdocs/install/mysql/tables/llx_accounting_bookkeeping.sql b/htdocs/install/mysql/tables/llx_accounting_bookkeeping.sql index 94761416a47f3..fe7c2b392076e 100644 --- a/htdocs/install/mysql/tables/llx_accounting_bookkeeping.sql +++ b/htdocs/install/mysql/tables/llx_accounting_bookkeeping.sql @@ -33,7 +33,10 @@ CREATE TABLE llx_accounting_bookkeeping credit double NOT NULL, montant double NOT NULL, sens varchar(1) DEFAULT NULL, - fk_user_author integer NOT NULL, + fk_user_author integer NOT NULL, -- user creating + fk_user_modif integer, -- user making last change + date_creation datetime, -- date de creation + tms timestamp, -- date last modification import_key varchar(14), code_journal varchar(10) DEFAULT NULL, piece_num integer NOT NULL,