diff --git a/htdocs/accountancy/admin/productaccount.php b/htdocs/accountancy/admin/productaccount.php index dd55e870009..c1541bd2214 100644 --- a/htdocs/accountancy/admin/productaccount.php +++ b/htdocs/accountancy/admin/productaccount.php @@ -1,6 +1,6 @@ - * Copyright (C) 2013-2020 Alexandre Spangaro + * Copyright (C) 2013-2021 Alexandre Spangaro * Copyright (C) 2014 Florian Henry * Copyright (C) 2014 Juanjo Menent * Copyright (C) 2015 Ari Elbaz (elarifr) @@ -98,6 +98,28 @@ if (empty($action)) { $arrayfields = array(); +$accounting_product_modes = array( + 'ACCOUNTANCY_SELL', + 'ACCOUNTANCY_SELL_INTRA', + 'ACCOUNTANCY_SELL_EXPORT', + 'ACCOUNTANCY_BUY', + 'ACCOUNTANCY_BUY_INTRA', + 'ACCOUNTANCY_BUY_EXPORT' +); + +if ($accounting_product_mode == 'ACCOUNTANCY_BUY') { + $accountancy_field_name = "accountancy_code_buy"; +} elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') { + $accountancy_field_name = "accountancy_code_buy_intra"; +} elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') { + $accountancy_field_name = "accountancy_code_buy_export"; +} elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL') { + $accountancy_field_name = "accountancy_code_sell"; +} elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') { + $accountancy_field_name = "accountancy_code_sell_intra"; +} else { // $accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT' + $accountancy_field_name = "accountancy_code_sell_export"; +} /* * Actions @@ -133,15 +155,6 @@ if ($action == 'update') { if (!empty($btn_changetype)) { $error = 0; - $accounting_product_modes = array( - 'ACCOUNTANCY_SELL', - 'ACCOUNTANCY_SELL_INTRA', - 'ACCOUNTANCY_SELL_EXPORT', - 'ACCOUNTANCY_BUY', - 'ACCOUNTANCY_BUY_INTRA', - 'ACCOUNTANCY_BUY_EXPORT' - ); - if (in_array($accounting_product_mode, $accounting_product_modes)) { if (!dolibarr_set_const($db, 'ACCOUNTING_PRODUCT_MODE', $accounting_product_mode, 'chaine', 0, '', $conf->entity)) { $error++; @@ -153,7 +166,7 @@ if ($action == 'update') { if (!empty($btn_changeaccount)) { //$msg = '
' . $langs->trans("Processing") . '...
'; - if (!empty($chk_prod)) { + if (!empty($chk_prod) && in_array($accounting_product_mode, $accounting_product_modes)) { $accounting = new AccountingAccount($db); //$msg .= '
' . count($chk_prod) . ' ' . $langs->trans("SelectedLines") . '
'; @@ -175,26 +188,15 @@ if ($action == 'update') { } else { $db->begin(); - $sql = " UPDATE ".MAIN_DB_PREFIX."product"; - if ($accounting_product_mode == 'ACCOUNTANCY_BUY') { - $sql .= " SET accountancy_code_buy = ".$accounting->account_number; + if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { + $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_perentity (fk_product, entity, " . $accountancy_field_name . ")"; + $sql .= " VALUES (" . ((int) $productid) . ", " . ((int) $conf->entity) . ", " . $accounting->account_number . ")"; + $sql .= " ON DUPLICATE KEY UPDATE " . $accountancy_field_name . " = " . $accounting->account_number; + } else { + $sql = " UPDATE ".MAIN_DB_PREFIX."product"; + $sql .= " SET " . $accountancy_field_name . " = " . $accounting->account_number; + $sql .= " WHERE rowid = " . ((int) $productid); } - if ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') { - $sql .= " SET accountancy_code_buy_intra = ".$accounting->account_number; - } - if ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') { - $sql .= " SET accountancy_code_buy_export = ".$accounting->account_number; - } - if ($accounting_product_mode == 'ACCOUNTANCY_SELL') { - $sql .= " SET accountancy_code_sell = ".$accounting->account_number; - } - if ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') { - $sql .= " SET accountancy_code_sell_intra = ".$accounting->account_number; - } - if ($accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') { - $sql .= " SET accountancy_code_sell_export = ".$accounting->account_number; - } - $sql .= " WHERE rowid = ".((int) $productid); dol_syslog("/accountancy/admin/productaccount.php sql=".$sql, LOG_DEBUG); if ($db->query($sql)) { @@ -267,50 +269,25 @@ if (empty($pcgvercode)) { } $sql = "SELECT p.rowid, p.ref, p.label, p.description, p.tosell, p.tobuy, p.tva_tx,"; -$sql .= " p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,"; -$sql .= " p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export,"; +if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { + $sql .= " pa.accountancy_code_sell, pa.accountancy_code_sell_intra, pa.accountancy_code_sell_export,"; + $sql .= " pa.accountancy_code_buy, pa.accountancy_code_buy_intra, pa.accountancy_code_buy_export,"; +} else { + $sql .= " p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,"; + $sql .= " p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export,"; +} $sql .= " p.tms, p.fk_product_type as product_type,"; $sql .= " aa.rowid as aaid"; $sql .= " FROM ".MAIN_DB_PREFIX."product as p"; -$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON"; -if ($accounting_product_mode == 'ACCOUNTANCY_BUY') { - $sql .= " p.accountancy_code_buy = aa.account_number AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'"; -} elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') { - $sql .= " p.accountancy_code_buy_intra = aa.account_number AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'"; -} elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') { - $sql .= " p.accountancy_code_buy_export = aa.account_number AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'"; -} elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL') { - $sql .= " p.accountancy_code_sell = aa.account_number AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'"; -} elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') { - $sql .= " p.accountancy_code_sell_intra = aa.account_number AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'"; +if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { + $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_accounting as pa ON pa.fk_product = p.rowid AND pa.entity = " . ((int) $conf->entity); + $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = pa." . $accountancy_field_name . " AND aa.fk_pcg_version = '" . $db->escape($pcgvercode) . "'"; } else { - $sql .= " p.accountancy_code_sell_export = aa.account_number AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'"; + $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = p." . $accountancy_field_name . " AND aa.fk_pcg_version = '" . $db->escape($pcgvercode) . "'"; } $sql .= ' WHERE p.entity IN ('.getEntity('product').')'; -if ($accounting_product_mode == 'ACCOUNTANCY_BUY') { - if (strlen(trim($search_current_account))) { - $sql .= natural_search("p.accountancy_code_buy", $search_current_account); - } -} elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') { - if (strlen(trim($search_current_account))) { - $sql .= natural_search("p.accountancy_code_buy_intra", $search_current_account); - } -} elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') { - if (strlen(trim($search_current_account))) { - $sql .= natural_search("p.accountancy_code_buy_export", $search_current_account); - } -} elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL') { - if (strlen(trim($search_current_account))) { - $sql .= natural_search("p.accountancy_code_sell", $search_current_account); - } -} elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') { - if (strlen(trim($search_current_account))) { - $sql .= natural_search("p.accountancy_code_sell_intra", $search_current_account); - } -} else { - if (strlen(trim($search_current_account))) { - $sql .= natural_search("p.accountancy_code_sell_export", $search_current_account); - } +if (strlen(trim($search_current_account))) { + $sql .= natural_search((!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "pa." : "p.") . $accountancy_field_name, $search_current_account); } if ($search_current_account_valid == 'withoutvalidaccount') { $sql .= " AND aa.account_number IS NULL"; @@ -459,7 +436,7 @@ if ($result) { // On sell if ($accounting_product_mode == 'ACCOUNTANCY_SELL' || $accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA' || $accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') { print ''.$form->selectyesno('search_onsell', $search_onsell, 1, false, 1).''; - } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY' || $accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA' || $accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') { + } else { // On buy print ''.$form->selectyesno('search_onpurchase', $search_onpurchase, 1, false, 1).''; } @@ -484,26 +461,12 @@ if ($result) { } print_liste_field_titre("VATRate", $_SERVER["PHP_SELF"], "p.tva_tx", "", $param, '', $sortfield, $sortorder, 'right '); // On sell / On purchase - if ($accounting_product_mode == 'ACCOUNTANCY_SELL') { + if ($accounting_product_mode == 'ACCOUNTANCY_SELL' || $accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA' || $accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') { print_liste_field_titre("OnSell", $_SERVER["PHP_SELF"], "p.tosell", "", $param, '', $sortfield, $sortorder, 'center '); - $fieldtosortaccount = "p.accountancy_code_sell"; - } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') { - print_liste_field_titre("OnSell", $_SERVER["PHP_SELF"], "p.tosell", "", $param, '', $sortfield, $sortorder, 'center '); - $fieldtosortaccount = "p.accountancy_code_sell_intra"; - } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') { - print_liste_field_titre("OnSell", $_SERVER["PHP_SELF"], "p.tosell", "", $param, '', $sortfield, $sortorder, 'center '); - $fieldtosortaccount = "p.accountancy_code_sell_export"; - } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY') { + } else { print_liste_field_titre("OnBuy", $_SERVER["PHP_SELF"], "p.tobuy", "", $param, '', $sortfield, $sortorder, 'center '); - $fieldtosortaccount = "p.accountancy_code_buy"; - } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') { - print_liste_field_titre("OnBuy", $_SERVER["PHP_SELF"], "p.tobuy", "", $param, '', $sortfield, $sortorder, 'center '); - $fieldtosortaccount = "p.accountancy_code_buy_intra"; - } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') { - print_liste_field_titre("OnBuy", $_SERVER["PHP_SELF"], "p.tobuy", "", $param, '', $sortfield, $sortorder, 'center '); - $fieldtosortaccount = "p.accountancy_code_buy_export"; } - print_liste_field_titre("CurrentDedicatedAccountingAccount", $_SERVER["PHP_SELF"], $fieldtosortaccount, "", $param, '', $sortfield, $sortorder); + print_liste_field_titre("CurrentDedicatedAccountingAccount", $_SERVER["PHP_SELF"], (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "pa." : "p.") . $accountancy_field_name, "", $param, '', $sortfield, $sortorder); print_liste_field_titre("AssignDedicatedAccountingAccount"); $clickpitco = $form->showCheckAddButtons('checkforselect', 1); print_liste_field_titre($clickpitco, '', '', '', '', '', '', '', 'center '); @@ -607,11 +570,10 @@ if ($result) { print vatrate($obj->tva_tx); print ''; + // On sell / On purchase if ($accounting_product_mode == 'ACCOUNTANCY_SELL' || $accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA' || $accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') { print ''.$product_static->getLibStatut(3, 0).''; - } - - if ($accounting_product_mode == 'ACCOUNTANCY_BUY' || $accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA' || $accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') { + } else { print ''.$product_static->getLibStatut(3, 1).''; } diff --git a/htdocs/accountancy/customer/card.php b/htdocs/accountancy/customer/card.php index 98e9151877e..099cecc4a71 100644 --- a/htdocs/accountancy/customer/card.php +++ b/htdocs/accountancy/customer/card.php @@ -1,7 +1,7 @@ * Copyright (C) 2013-2014 Florian Henry - * Copyright (C) 2013-2017 Alexandre Spangaro + * Copyright (C) 2013-2021 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 @@ -100,10 +100,18 @@ $formaccounting = new FormAccounting($db); if (!empty($id)) { $sql = "SELECT f.ref, f.rowid as facid, l.fk_product, l.description, l.price,"; - $sql .= " l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice, p.accountancy_code_sell as code_sell,"; + $sql .= " l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice,"; + if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { + $sql .= " pa.accountancy_code_sell as code_sell,"; + } else { + $sql .= " p.accountancy_code_sell as code_sell,"; + } $sql .= " l.fk_code_ventilation, aa.account_number, aa.label"; $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as l"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product"; + if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { + $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_accounting as pa ON pa.fk_product = p.rowid AND pa.entity = " . ((int) $conf->entity); + } $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON l.fk_code_ventilation = aa.rowid"; $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = l.fk_facture"; $sql .= " WHERE f.fk_statut > 0 AND l.rowid = ".((int) $id); diff --git a/htdocs/accountancy/customer/index.php b/htdocs/accountancy/customer/index.php index a0da7ace065..88a32340daa 100644 --- a/htdocs/accountancy/customer/index.php +++ b/htdocs/accountancy/customer/index.php @@ -1,7 +1,7 @@ * Copyright (C) 2013-2014 Florian Henry - * Copyright (C) 2013-2020 Alexandre Spangaro + * Copyright (C) 2013-2021 Alexandre Spangaro * Copyright (C) 2014 Juanjo Menent * Copyright (C) 2015 Jean-François Ferry * @@ -125,19 +125,34 @@ if ($action == 'validatehistory') { $sql = "SELECT f.rowid as facid, f.ref as ref, f.datef, f.type as ftype,"; $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,"; - $sql .= " p.accountancy_code_sell as code_sell, p.accountancy_code_sell_intra as code_sell_intra, p.accountancy_code_sell_export as code_sell_export,"; + if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { + $sql .= " pa.accountancy_code_sell as code_sell, pa.accountancy_code_sell_intra as code_sell_intra, pa.accountancy_code_sell_export as code_sell_export,"; + } else { + $sql .= " p.accountancy_code_sell as code_sell, p.accountancy_code_sell_intra as code_sell_intra, p.accountancy_code_sell_export as code_sell_export,"; + } $sql .= " aa.rowid as aarowid, aa2.rowid as aarowid_intra, aa3.rowid as aarowid_export, aa4.rowid as aarowid_thirdparty,"; $sql .= " co.code as country_code, co.label as country_label,"; - $sql .= " s.tva_intra, s.accountancy_code_sell as company_code_sell"; + $sql .= " s.tva_intra,"; + if (!empty($conf->global->ACCOUNTANCY_COMPANY_SHARED)) { + $sql .= " sa.accountancy_code_sell as company_code_sell"; + } else { + $sql .= " s.accountancy_code_sell as company_code_sell"; + } + $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = s.fk_pays "; $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as l ON f.rowid = l.fk_facture"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product"; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON p.accountancy_code_sell = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa.entity = ".$conf->entity; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa2 ON p.accountancy_code_sell_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa2.entity = ".$conf->entity; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa3 ON p.accountancy_code_sell_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa3.entity = ".$conf->entity; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa4 ON s.accountancy_code_sell = aa4.account_number AND aa4.active = 1 AND aa4.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa4.entity = ".$conf->entity; + if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) { + $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as pa ON pa.fk_product = p.rowid AND pa.entity = " . ((int) $conf->entity); + } + $alias_company_accounting = !empty($conf->global->ACCOUNTANCY_COMPANY_SHARED) ? "sa" : "s"; + $alias_product_accounting = !empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "pa" : "p"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON " . $alias_product_accounting . ".accountancy_code_sell = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa.entity = ".$conf->entity; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa2 ON " . $alias_product_accounting . ".accountancy_code_sell_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa2.entity = ".$conf->entity; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa3 ON " . $alias_product_accounting . ".accountancy_code_sell_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa3.entity = ".$conf->entity; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa4 ON " . $alias_company_accounting . ".accountancy_code_sell = aa4.account_number AND aa4.active = 1 AND aa4.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa4.entity = ".$conf->entity; $sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0"; $sql .= " AND l.product_type <= 2"; diff --git a/htdocs/accountancy/customer/lines.php b/htdocs/accountancy/customer/lines.php index 6b025e6f3c1..42e4c268b86 100644 --- a/htdocs/accountancy/customer/lines.php +++ b/htdocs/accountancy/customer/lines.php @@ -1,6 +1,6 @@ - * Copyright (C) 2013-2020 Alexandre Spangaro + * Copyright (C) 2013-2021 Alexandre Spangaro * Copyright (C) 2014-2015 Ari Elbaz (elarifr) * Copyright (C) 2014-2016 Florian Henry * Copyright (C) 2014 Juanjo Menent @@ -182,9 +182,13 @@ print '