* Copyright (C) 2024 Frédéric France * * 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 * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/product/dynamic_price/editor.php * \ingroup product * \brief Page for editing expression */ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; /** * @var Conf $conf * @var DoliDB $db * @var HookManager $hookmanager * @var Translate $langs * @var User $user */ // Load translation files required by the page $langs->loadLangs(array('products', 'accountancy')); //"Back" translation is on this accountancy file $id = GETPOSTINT('id'); $eid = GETPOSTINT('eid'); $action = GETPOST('action', 'aZ09'); $title = GETPOST('expression_title', 'alpha'); $expression = GETPOST('expression'); $tab = GETPOST('tab', 'alpha'); $tab = (!empty($tab)) ? $tab : 'card'; $tab = strtolower($tab); // Security check $result = restrictedArea($user, 'produit|service&fournisseur', $id, 'product&product', '', '', 'rowid'); //Initialize objects $product = new Product($db); $product->fetch($id, ''); $price_expression = new PriceExpression($db); $price_globals = new PriceGlobalVariable($db); //Fetch expression data if (empty($eid)) { //This also disables fetch when eid == 0 $eid = 0; } elseif ($action != 'delete') { $price_expression->fetch($eid); } $object = $product; $usercanread = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'lire')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'lire'))); $usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'creer')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'creer'))); $usercandelete = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'supprimer')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'supprimer'))); /* * Actions */ if ($action == 'add' && $usercancreate) { if ($eid == 0) { $result = $price_expression->find_title($title); if ($result == 0) { //No existing entry found with title, ok // Check the expression validity by parsing it require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; $priceparser = new PriceParser($db); $price_result = $priceparser->testExpression($id, $expression); if ($price_result < 0) { //Expression is not valid setEventMessages($priceparser->translatedError(), null, 'errors'); } else { $price_expression->title = $title; $price_expression->expression = $expression; $result = $price_expression->create($user); if ($result > 0) { //created successfully, set the eid to newly created entry $eid = $price_expression->id; setEventMessages($langs->trans("RecordSaved"), null, 'mesgs'); } else { setEventMessages("add: ".$price_expression->error, $price_expression->errors, 'errors'); } } } elseif ($result < 0) { setEventMessages("add find: ".$price_expression->error, $price_expression->errors, 'errors'); } else { setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors'); } } } if ($action == 'update' && $usercancreate) { if ($eid != 0) { $result = $price_expression->find_title($title); if ($result == 0 || $result == $eid) { //No existing entry found with title or existing one is the current one, ok // Check the expression validity by parsing it require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; $priceparser = new PriceParser($db); $price_result = $priceparser->testExpression($id, $expression); if ($price_result < 0) { //Expression is not valid setEventMessages($priceparser->translatedError(), null, 'errors'); } else { $price_expression->id = $eid; $price_expression->title = $title; $price_expression->expression = $expression; $result = $price_expression->update($user); if ($result < 0) { setEventMessages("update: ".$price_expression->error, $price_expression->errors, 'errors'); } else { setEventMessages($langs->trans("RecordSaved"), null, 'mesgs'); } } } elseif ($result < 0) { setEventMessages("update find: ".$price_expression->error, $price_expression->errors, 'errors'); } else { setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors'); } } } if ($action == 'delete' && $usercancreate) { if ($eid != 0) { $price_expression->fetch($eid); $result = $price_expression->delete($user); if ($result < 0) { setEventMessages("delete: ".$price_expression->error, $price_expression->errors, 'errors'); } $eid = 0; } } /* * View */ $form = new Form($db); llxHeader("", "", $langs->trans("CardProduct".$product->type), '', 0, 0, '', '', '', 'mod-product page-dynamic_price_editor'); print load_fiche_titre($langs->trans("PriceExpressionEditor")); //Form/Table print '
'; print ''; print ''; print dol_get_fiche_head(); print ''; // Price expression selector print ''; // Title input print ''; //Help text $help_text = $langs->trans("PriceExpressionEditorHelp1"); $help_text .= '

'.$langs->trans("PriceExpressionEditorHelp2"); $help_text .= '

'.$langs->trans("PriceExpressionEditorHelp3"); $help_text .= '

'.$langs->trans("PriceExpressionEditorHelp4"); $help_text .= '

'.$langs->trans("PriceExpressionEditorHelp5"); foreach ($price_globals->listGlobalVariables() as $entry) { $help_text .= '
#global_'.$entry->code.'# '.$entry->description.' = '.$entry->value; } //Price expression editor print ''; print '
'.$langs->trans("PriceExpressionSelected").''; $price_expression_list = array(0 => $langs->trans("New")); //Put the new as first option foreach ($price_expression->list_price_expression() as $entry) { $price_expression_list[$entry->id] = $entry->title; } print $form->selectarray('expression_selection', $price_expression_list, $eid); print '
'.$langs->trans("Name").''; print ''; print '
'.$form->textwithpicto($langs->trans("PriceExpressionEditor"), $help_text, 1).''; require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; $doleditor = new DolEditor('expression', isset($price_expression->expression) ? $price_expression->expression : '', '', 300, '', '', false, false, false, ROWS_4, '90%'); $doleditor->Create(); print '
'; print dol_get_fiche_end(); //Buttons print '
'; print ''; print ''.$langs->trans("Back").''; if ($eid == 0) { print '
'.$langs->trans('Delete').'
'."\n"; } else { print ''; } print '
'; print '
'; // This code reloads the page depending of selected option, goes to page selected by tab when back is pressed print ''; // End of page llxFooter(); $db->close();