diff --git a/htdocs/accountancy/class/accountingaccount.class.php b/htdocs/accountancy/class/accountingaccount.class.php index 95296624145..254c22c9f95 100644 --- a/htdocs/accountancy/class/accountingaccount.class.php +++ b/htdocs/accountancy/class/accountingaccount.class.php @@ -730,7 +730,7 @@ class AccountingAccount extends CommonObject * @param Product $product Product object sell or buy * @param Facture|FactureFournisseur $facture Facture * @param FactureLigne|SupplierInvoiceLine $factureDet Facture Det - * @param array $accountingAccount Array of Account account + * @param array $accountingAccount Array of Accounting account * @param string $type Customer / Supplier * @return array|int Accounting accounts suggested or < 0 if technical error. */ @@ -880,10 +880,33 @@ class AccountingAccount extends CommonObject } $code_l = $accountdeposittoventilated->ref; + $code_p = ''; + $code_t = ''; $suggestedid = $accountdeposittoventilated->rowid; $suggestedaccountingaccountfor = 'deposit'; } + // For credit note invoice, if origin invoice is a deposit invoice, force also on specific customer/supplier deposit account + dol_syslog("fk_facture_source::".$facture->fk_facture_source, LOG_DEBUG); + if (!empty($facture->fk_facture_source)) { + $invoiceSource = new Facture($this->db); + $invoiceSource->fetch($facture->fk_facture_source); + + if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) { + $accountdeposittoventilated = new AccountingAccount($this->db); + if ($type == 'customer') { + $accountdeposittoventilated->fetch('', $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT, 1); + } elseif ($type == 'supplier') { + $accountdeposittoventilated->fetch('', $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT, 1); + } + $code_l = $accountdeposittoventilated->ref; + $code_p = ''; + $code_t = ''; + $suggestedid = $accountdeposittoventilated->rowid; + $suggestedaccountingaccountfor = 'deposit'; + } + } + // If $suggestedid could not be guessed yet, we set it from the generic default accounting code $code_l if (empty($suggestedid) && empty($code_p) && !empty($code_l) && empty($conf->global->ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC)) { if (empty($this->accountingaccount_codetotid_cache[$code_l])) { diff --git a/htdocs/accountancy/customer/list.php b/htdocs/accountancy/customer/list.php index 4def8dfbef6..f9158be9c59 100644 --- a/htdocs/accountancy/customer/list.php +++ b/htdocs/accountancy/customer/list.php @@ -229,7 +229,7 @@ if (empty($chartaccountcode)) { } // Customer Invoice lines -$sql = "SELECT f.rowid as facid, f.ref, f.datef, f.type as ftype,"; +$sql = "SELECT f.rowid as facid, f.ref, f.datef, f.type as ftype, f.fk_facture_source,"; $sql .= " l.rowid, l.fk_product, l.description, l.total_ht, l.fk_code_ventilation, l.product_type as type_l, l.tva_tx as tva_tx_line, l.vat_src_code,"; $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.tva_tx as tva_tx_prod,"; if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { @@ -579,6 +579,7 @@ if ($result) { $facture_static->id = $objp->facid; $facture_static->type = $objp->ftype; $facture_static->date = $db->jdate($objp->datef); + $facture_static->fk_facture_source = $objp->fk_facture_source; $facture_static_det->id = $objp->rowid; $facture_static_det->total_ht = $objp->total_ht; diff --git a/htdocs/accountancy/supplier/list.php b/htdocs/accountancy/supplier/list.php index 3c4a9e7f46b..ded50694a62 100644 --- a/htdocs/accountancy/supplier/list.php +++ b/htdocs/accountancy/supplier/list.php @@ -232,7 +232,7 @@ if (empty($chartaccountcode)) { } // Supplier Invoice Lines -$sql = "SELECT f.rowid as facid, f.ref, f.ref_supplier, f.libelle as invoice_label, f.datef, f.type as ftype,"; +$sql = "SELECT f.rowid as facid, f.ref, f.ref_supplier, f.libelle as invoice_label, f.datef, f.type as ftype, f.fk_facture_source,"; $sql .= " l.rowid, l.fk_product, l.description, l.total_ht, l.fk_code_ventilation, l.product_type as type_l, l.tva_tx as tva_tx_line, l.vat_src_code,"; $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.tva_tx as tva_tx_prod,"; if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { @@ -592,6 +592,7 @@ if ($result) { $facturefourn_static->ref_supplier = $objp->ref_supplier; $facturefourn_static->label = $objp->invoice_label; $facturefourn_static->date = $db->jdate($objp->datef); + $facturefourn_static->fk_facture_source = $objp->fk_facture_source; $facturefourn_static_det->id = $objp->rowid; $facturefourn_static_det->total_ht = $objp->total_ht;