From 1cd2783bd2722b2d414addde718cc675feb85d87 Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Tue, 25 Mar 2025 16:28:51 +0100 Subject: [PATCH] NEW Add tags / category on supplier invoice (#33490) * NEW Add tags / category on supplier invoice * CI * Fix condition if constant MAIN_USE_NEW_SUPPLIERMOD is used * Fix link * Fix typo * Fix element type on export * Fix element type on export --- htdocs/categories/class/categorie.class.php | 17 ++- htdocs/categories/viewcat.php | 82 ++++++++++++++ htdocs/core/class/html.form.class.php | 103 ++++++++++++++++++ htdocs/core/menus/standard/eldy.lib.php | 6 + htdocs/core/modules/modCategorie.class.php | 35 +++++- .../fourn/class/fournisseur.facture.class.php | 28 +++++ htdocs/fourn/facture/card.php | 31 +++++- htdocs/langs/en_US/categories.lang | 3 + 8 files changed, 298 insertions(+), 7 deletions(-) diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index 31d692afab1..1b54a7c0735 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -68,6 +68,7 @@ class Categorie extends CommonObject const TYPE_ORDER = 'order'; const TYPE_INVOICE = 'invoice'; const TYPE_SUPPLIER_ORDER = 'supplier_order'; + const TYPE_SUPPLIER_INVOICE = 'supplier_invoice'; /** @@ -97,7 +98,8 @@ class Categorie extends CommonObject 'fichinter' => 14, 'order' => 16, 'invoice' => 17, - 'supplier_order' => 20 + 'supplier_order' => 20, + 'supplier_invoice' => 21 ); /** @@ -123,7 +125,8 @@ class Categorie extends CommonObject 14 => 'fichinter', 16 => 'order', 17 => 'invoice', - 20 => 'supplier_order' + 20 => 'supplier_order', + 21 => 'supplier_invoice' ); /** @@ -172,7 +175,8 @@ class Categorie extends CommonObject 'fichinter' => 'Fichinter', 'order' => 'Commande', 'invoice' => 'Facture', - 'supplier_order' => 'CommandeFournisseur' + 'supplier_order' => 'CommandeFournisseur', + 'supplier_invoice' => 'FactureFournisseur' ); /** @@ -198,7 +202,8 @@ class Categorie extends CommonObject 'fichinter' => 'FichintersCategoriesArea', 'order' => 'OrderCategoriesArea', 'invoice' => 'InvoicesCategoriesArea', - 'supplier_order' => 'SuppliersOrdersCategoriesArea' + 'supplier_order' => 'SuppliersOrdersCategoriesArea', + 'supplier_invoice' => 'SuppliersInvoicesCategoriesArea' ); /** @@ -217,7 +222,8 @@ class Categorie extends CommonObject 'fichinter' => 'fichinter', 'order' => 'commande', 'invoice' => 'facture', - 'supplier_order' => 'commande_fournisseur' + 'supplier_order' => 'commande_fournisseur', + 'supplier_invoice' => 'facture_fourn' ); /** @@ -285,6 +291,7 @@ class Categorie extends CommonObject * @see Categorie::TYPE_ORDER * @see Categorie::TYPE_INVOICE * @see Categorie::TYPE_SUPPLIER_ORDER + * @see Categorie::TYPE_SUPPLIER_INVOICE */ public $type; diff --git a/htdocs/categories/viewcat.php b/htdocs/categories/viewcat.php index aa54f2fb4bf..696a8ba5c84 100644 --- a/htdocs/categories/viewcat.php +++ b/htdocs/categories/viewcat.php @@ -270,6 +270,10 @@ if ($elemid && $action == 'addintocategory') { // Test on permission not require require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php'; $newobject = new CommandeFournisseur($db); $elementtype = 'supplier_order'; + } elseif ($type == Categorie::TYPE_SUPPLIER_INVOICE && $user->hasRight('fournisseur', 'facture', 'creer')) { + require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; + $newobject = new FactureFournisseur($db); + $elementtype = 'supplier_invoice'; } else { dol_print_error(null, "Not supported value of type = ".$type); } @@ -1691,6 +1695,84 @@ if ($type == Categorie::TYPE_SUPPLIER_ORDER) { } } +// List of Supplier Invoices +if ($type == Categorie::TYPE_SUPPLIER_INVOICE) { + require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; + + $permission = $user->rights->fournisseur->facture->creer; + + $objects = $object->getObjectsInCateg($type, 0, $limit, $offset); + if ($objects < 0) { + dol_print_error($db, $object->error, $object->errors); + } else { + // Form to add record into a category + $showclassifyform = $user->hasRight('fournisseur', 'facture', 'creer');; + if ($showclassifyform) { + print '
'; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'; + print $langs->trans("AddSupplierInvoiceIntoCategory").'  '; + $form->selectSupplierInvoice('', 'elemid'); + print '
'; + print '
'; + } + + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + $param = '&limit='.$limit.'&id='.$id.'&type='.$type; $num = count($objects); $nbtotalofrecords = ''; $newcardbutton = ''; + + // @phan-suppress-next-line PhanPluginSuspiciousParamOrder + print_barre_liste($langs->trans("SuppliersOrders"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'supplier_order', 0, $newcardbutton, '', $limit); + + print "\n"; + print ''."\n"; + + if (count($objects) > 0) { + $i = 0; + foreach ($objects as $key => $supplier_invoice) { + $i++; + if ($i > $limit) { + break; + } + + print "\t".''."\n"; + print '\n"; + print '\n"; + // Link to delete from category + print '\n"; + } + } else { + print ''; + } + print "
'.$langs->trans("Ref").'
'; + print $supplier_invoice->getNomUrl(1); + print "'.$supplier_invoice->ref."'; + if ($permission) { + print ""; + print $langs->trans("DeleteFromCat"); + print img_picto($langs->trans("DeleteFromCat"), 'unlink', '', 0, 0, 0, '', 'paddingleft'); + print ""; + } + print "
'.$langs->trans("ThisCategoryHasNoItems").'
\n"; + + print '
'."\n"; + } +} + // Note that $action and $object may have been modified by some hooks $parameters = array('type' => $type, 'id' => $id, 'label' => $label); $reshook = $hookmanager->executeHooks('addMoreCategoriesList', $parameters, $object, $action); diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 9162a6f7593..2c8f9ab982a 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -11616,6 +11616,109 @@ class Form } } + /** + * Output a combo list with supplier invoices qualified for a third party + * + * @param string $selected Id supplier order preselected + * @param string $htmlname Name of HTML select + * @param int $maxlength Maximum length of label + * @param int $option_only Return only html options lines without the select tag + * @param string $show_empty Add an empty line ('1' or string to show for empty line) + * @param int $discard_closed Discard closed projects (0=Keep,1=hide completely,2=Disable) + * @param int $forcefocus Force focus on field (works with javascript only) + * @param int $disabled Disabled + * @param string $morecss More css added to the select component + * + * @return int Nbr of project if OK, <0 if KO + */ + public function selectSupplierInvoice($selected = '', $htmlname = 'supplierinvoiceid', $maxlength = 24, $option_only = 0, $show_empty = '1', $discard_closed = 0, $forcefocus = 0, $disabled = 0, $morecss = 'maxwidth500') + { + global $user, $conf, $langs; + + $out = ''; + + $hideunselectables = false; + if (getDolGlobalString('SUPPLIER_INVOICE_HIDE_UNSELECTABLES')) { + $hideunselectables = true; + } + + // Search all supplier orders + $sql = "SELECT ff.rowid, ff.ref"; + $sql .= ' FROM '.$this->db->prefix().'facture_fourn as ff'; + $sql .= " ORDER BY ff.ref ASC"; + + $resql = $this->db->query($sql); + if ($resql) { + // Use select2 selector + if (!empty($conf->use_javascript_ajax)) { + include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php'; + $comboenhancement = ajax_combobox($htmlname, array(), 0, $forcefocus); + $out .= $comboenhancement; + $morecss = 'minwidth200imp maxwidth500'; + } + + if (empty($option_only)) { + $out .= ''; + } + + print $out; + + $this->db->free($resql); + return $num; + } else { + dol_print_error($this->db); + return -1; + } + } + /** * Output the component to make advanced search criteries * diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index 93bd4a816a0..c6fc6a19e64 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -1565,6 +1565,12 @@ function get_left_menu_billing($mainmenu, &$newmenu, $usemenuhider = 1, $leftmen } $newmenu->add("/compta/facture/stats/index.php?mode=supplier&leftmenu=suppliers_bills_stats", $langs->trans("Statistics"), 1, $user->hasRight('fournisseur', 'facture', 'lire'), '', $mainmenu, 'suppliers_bills_stats'); + + // Categories + if (isModEnabled('category')) { + $langs->load("categories"); + $newmenu->add("/categories/index.php?leftmenu=cat&type=21", $langs->trans("Categories"), 1, $user->rights->categorie->lire, '', $mainmenu, 'cat'); + } } // Orders diff --git a/htdocs/core/modules/modCategorie.class.php b/htdocs/core/modules/modCategorie.class.php index d4e1bdc8256..dc10152beb6 100644 --- a/htdocs/core/modules/modCategorie.class.php +++ b/htdocs/core/modules/modCategorie.class.php @@ -177,6 +177,9 @@ class modCategorie extends DolibarrModules if ((isModEnabled('fournisseur') && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) || (isModEnabled('supplier_order'))) { $typeexample .= ($typeexample ? " / " : "")."20=Supplier order"; } + if ((isModEnabled('fournisseur') && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) || (isModEnabled('supplier_invoice'))) { + $typeexample .= ($typeexample ? " / " : "")."21=Supplier invoice"; + } // Definition of vars $this->export_fields_array[$r] = array('cat.rowid' => "CategId", 'cat.label' => "Label", 'cat.type' => "Type", 'cat.description' => "Description", 'cat.fk_parent' => "ParentCategoryID", 'pcat.label' => "ParentCategoryLabel", 'cat.color' => "Color", 'cat.date_creation' => "DateCreation", 'cat.tms' => "DateLastModification"); @@ -535,6 +538,24 @@ class modCategorie extends DolibarrModules ); } + // 21 Supplier invoice + if ((isModEnabled('fournisseur') && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) || (isModEnabled('supplier_invoice'))) { + ++$r; + $this->exportTagLinks( + $r, + 'supplier_invoice', + 'invoice_supplier', + '(isModEnabled("fournisseur") && !getDolGlobalString("MAIN_USE_NEW_SUPPLIERMOD")) || (isModEnabled("supplier_invoice"))', + ['fournisseur', 'facture', 'export'], + [ + 'rowid' => [ + 'name' => 'SupplierInvoiceID', + 'type' => 'Numeric' + ] + ] + ); + } + // Imports //-------- @@ -551,7 +572,7 @@ class modCategorie extends DolibarrModules 'ca.label' => "Label*", 'ca.type' => "Type*", 'ca.description' => "Description", 'ca.fk_parent' => 'ParentCategory' ); - $this->import_regex_array[$r] = array('ca.type' => '^(0|1|2|3|4|5|6|7|8|9|10|11|16|17|20)$'); + $this->import_regex_array[$r] = array('ca.type' => '^(0|1|2|3|4|5|6|7|8|9|10|11|16|17|20|21)$'); $this->import_convertvalue_array[$r] = array( 'ca.fk_parent' => array( 'rule' => 'fetchidfromcodeandlabel', @@ -757,6 +778,18 @@ class modCategorie extends DolibarrModules 'CommandeFournisseur' ); } + + // 21 Supplier invoice + if ((isModEnabled('fournisseur') && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) || (isModEnabled('supplier_invoice'))) { + ++$r; + $this->importTagLinks( + $r, + 'supplier_invoice', + '/fourn/class/fournisseur.facture.class.php', + 'invoice_supplier', + 'FactureFournisseur' + ); + } } /** diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index dfbecd88a78..b2b320653f6 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -1552,6 +1552,18 @@ class FactureFournisseur extends CommonInvoice } } + // Remove linked categories. + if (!$error) { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_invoice"; + $sql .= " WHERE fk_invoice = ".((int) $this->id); + + $result = $this->db->query($sql); + if (!$result) { + $error++; + $this->errors[] = $this->db->lasterror(); + } + } + // Remove extrafields if (!$error) { $result = $this->deleteExtraFields(); @@ -2974,6 +2986,22 @@ class FactureFournisseur extends CommonInvoice return $result; } + /** + * Sets object to given categories. + * + * Adds it to non existing supplied categories. + * Existing categories are left untouch. + * + * @param int[]|int $categories Category or categories IDs + * + * @return int Return integer <0 if KO, >0 if OK + */ + public function setCategories($categories) + { + require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; + return parent::setCategoriesCommon($categories, Categorie::TYPE_SUPPLIER_INVOICE); + } + /** * Return next reference of supplier invoice not already used (or last reference) * according to numbering module defined into constant INVOICE_SUPPLIER_ADDON_NUMBER diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php index a1efa9f17c1..ae8e9cc18df 100644 --- a/htdocs/fourn/facture/card.php +++ b/htdocs/fourn/facture/card.php @@ -8,7 +8,7 @@ * Copyright (C) 2013-2022 Philippe Grand * Copyright (C) 2013 Florian Henry * Copyright (C) 2014-2016 Marcos García - * Copyright (C) 2016-2023 Alexandre Spangaro + * Copyright (C) 2016-2025 Alexandre Spangaro * Copyright (C) 2018-2025 Frédéric France * Copyright (C) 2019 Ferran Marcet * Copyright (C) 2022 Gauthier VERDOL @@ -37,6 +37,7 @@ // Load Dolibarr environment require '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php'; @@ -247,6 +248,13 @@ if (empty($reshook)) { setEventMessages($object->error, $object->errors, 'errors'); } else { + if (isModEnabled('category')) { + $categories = GETPOST('categories', 'array'); + if (method_exists($object, 'setCategories')) { + $object->setCategories($categories); + } + } + $db->commit(); // Define output language @@ -454,6 +462,8 @@ if (empty($reshook)) { } elseif ($action == 'set_incoterms' && isModEnabled('incoterm') && $usercancreate) { // Set incoterm $result = $object->setIncoterms(GETPOSTINT('incoterm_id'), GETPOST('location_incoterms')); + } elseif ($action == 'settags' && isModEnabled('category')) { // Set tags + $result = $object->setCategories(GETPOST('categories', 'array')); } elseif ($action == 'setmode' && $usercancreate) { // payment mode $result = $object->setPaymentMethods(GETPOSTINT('mode_reglement_id')); @@ -2838,6 +2848,15 @@ if ($action == 'create') { print $object->showOptionals($extrafields, 'create'); } + // Categories + if (isModEnabled("category")) { + print ''.$langs->trans("Categories").''; + $cate_arbo = $form->select_all_categories(Categorie::TYPE_SUPPLIER_INVOICE, '', 'parent', 64, 0, 1); + $arrayselected = GETPOST('categories', 'array'); + print img_picto('', 'category').$form->multiselectarray('categories', $cate_arbo, $arrayselected, 0, 0, 'quatrevingtpercent widthcentpercentminusx', 0, 0); + print ""; + } + // Public note print ''.$langs->trans('NotePublic').''; print ''; @@ -3252,6 +3271,16 @@ if ($action == 'create') { $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline', '', 0, 1); } + // Category + if (isModEnabled('category')) { + // Categories + print ''.$langs->trans("Categories").''; + $cate_arbo = $form->select_all_categories(Categorie::TYPE_SUPPLIER_INVOICE, '', 'parent', 64, 0, 1); + $arrayselected = GETPOST('categories', 'array'); + print img_picto('', 'category').$form->multiselectarray('categories', $cate_arbo, $arrayselected, 0, 0, 'quatrevingtpercent widthcentpercentminusx', 0, '0'); + print ""; + } + $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid); $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook if (empty($reshook)) { diff --git a/htdocs/langs/en_US/categories.lang b/htdocs/langs/en_US/categories.lang index 1281dbdff5a..d0063b3a4fa 100644 --- a/htdocs/langs/en_US/categories.lang +++ b/htdocs/langs/en_US/categories.lang @@ -19,6 +19,7 @@ ProjectsCategoriesArea=Project tags/categories area UsersCategoriesArea=User tags/categories area InvoicesCategoriesArea=Invoice tags/categories area SuppliersOrdersCategoriesArea=Supplier order tags/categories area +SuppliersInvoicesCategoriesArea=Supplier invoice tags/categories area Bank_linesCategoriesArea==Bank lines tags/categories area SubCats=Sub-categories CatList=List of tags/categories @@ -83,6 +84,7 @@ CatUsersList=List of users tags/categories CatOrdersLinks=Links between orders and tags/categories CatInvoicesLinks=Links between invoices and tags/categories CatSupplier_ordersLinks=Links between supplier orders and tags/categories +CatSupplierInvoicesLinks=Links between invoices and tags/categories CatSupLinks=Links between vendors and tags/categories CatCusLinks=Links between customers/prospects and tags/categories CatContactsLinks=Links between contacts/addresses and tags/categories @@ -102,6 +104,7 @@ AddFichinterIntoCategory=Assign the category to the intervention AddOrderIntoCategory=Assign the category to the order AddInvoiceIntoCategory=Assign the category to the invoice AddSupplierOrderIntoCategory=Assign the category to the supplier order +AddSupplierInvoiceIntoCategory=Assign the category to the supplier invoice AssignCategoryTo=Assign the category to ShowCategory=Show tag/category ByDefaultInList=By default in list