Skip to content

Commit

Permalink
Merge branch 'donModelNumero' of github.com:Tetras-Libre/dolibarr int…
Browse files Browse the repository at this point in the history
…o sif-19-v2
  • Loading branch information
dbeniamine committed Jan 2, 2025
2 parents c6a75c1 + 2183471 commit 4cb15ff
Show file tree
Hide file tree
Showing 5 changed files with 525 additions and 7 deletions.
5 changes: 3 additions & 2 deletions htdocs/core/modules/dons/doc/doc_generic_don_odt.modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,11 @@ public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails

$dir = $conf->don->dir_output;
$objectref = dol_sanitizeFileName($object->ref);
$objectid = dol_sanitizeFileName($object->id);
if (!preg_match('/specimen/i', $objectref)) {
$dir .= "/".$objectref;
$dir .= "/".$objectid;
}
$file = $dir."/".$objectref.".odt";
$file = $dir."/".$objectid.".odt";

if (!file_exists($dir)) {
if (dol_mkdir($dir) < 0) {
Expand Down
233 changes: 233 additions & 0 deletions htdocs/core/modules/dons/mod_don_terre.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<?php
/* Copyright (C) 2005-2008 Laurent Destailleur <[email protected]>
* Copyright (C) 2005-2015 Regis Houssin <[email protected]>
*
* 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 <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
*/

/**
* \file htdocs/core/modules/don/mod_don_terre.php
* \ingroup don
* \brief File containing class for numbering module Terre
*/
require_once DOL_DOCUMENT_ROOT.'/core/modules/dons/modules_don.php';

/**
* \class mod_don_terre
* \brief Class of numbering module Terre for dons
*/
class mod_don_terre extends ModeleNumRefDons
{
/**
* Dolibarr version of the loaded document 'development', 'experimental', 'dolibarr'
* @var string
*/
public $version = 'dolibarr';

/**
* Prefix for dons
* @var string
*/
public $prefixdon = 'DON';

/**
* Prefix for replacement dons
* @var string
*/
public $prefixreplacement = 'FA';

/**
* Prefix for credit note
* @var string
*/
public $prefixcreditnote = 'AV';

/**
* Prefix for deposit
* @var string
*/
public $prefixdeposit = 'AC';

/**
* @var string Error code (or message)
*/
public $error = '';


/**
* Constructor
*/
public function __construct()
{
global $conf, $mysoc;

}

/**
* Returns the description of the numbering model
*
* @return string Texte descripif
*/
public function info()
{
global $langs;
$langs->load("bills");
return $langs->trans('TerreNumRefModelDesc1', $this->prefixdon, $this->prefixcreditnote, $this->prefixdeposit);
}

/**
* Return an example of numbering
*
* @return string Example
*/
public function getExample()
{
return $this->prefixdon."0501-0001";
}

/**
* Checks if the numbers already in the database do not
* cause conflicts that would prevent this numbering working.
*
* @return boolean false if conflict, true if ok
*/
public function canBeActivated()
{
global $langs, $conf, $db;

$langs->load("bills");

// Check don num
$fayymm = '';
$max = '';

$posindice = strlen($this->prefixdon) + 6;
$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
$sql .= " FROM ".MAIN_DB_PREFIX."don";
$sql .= " WHERE ref LIKE '".$db->escape($this->prefixdon)."____-%'";
$sql .= " AND entity = ".$conf->entity;

$resql = $db->query($sql);
if ($resql) {
$row = $db->fetch_row($resql);
if ($row) {
$fayymm = substr($row[0], 0, 6);
$max = $row[0];
}
}
if ($fayymm && !preg_match('/'.$this->prefixdon.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
$langs->load("errors");
$this->error = $langs->trans('ErrorNumRefModel', $max);
return false;
}

return true;
}

/**
* Return next value not used or last value used.
* Note to increase perf of this numbering engine, you can create a calculated column and modify request to use this field instead for select:
* ALTER TABLE llx_don ADD COLUMN calculated_numrefonly INTEGER AS (CASE SUBSTRING(ref FROM 1 FOR 2) WHEN 'FA' THEN CAST(SUBSTRING(ref FROM 10) AS SIGNED) ELSE 0 END) PERSISTENT;
* ALTER TABLE llx_don ADD INDEX calculated_numrefonly_idx (calculated_numrefonly);
*
* @param Societe $objsoc Object third party
* @param Don $don Object don
* @param string $mode 'next' for next value or 'last' for last value
* @return string Next ref value or last ref if $mode is 'last', <= 0 if KO
*/
public function getNextValue($objsoc, $don, $mode = 'next')
{
global $db;

dol_syslog(get_class($this)."::getNextValue mode=".$mode, LOG_DEBUG);

$prefix = $this->prefixdon;

// First we get the max value
$posindice = strlen($prefix) + 6;
$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
$sql .= " FROM ".MAIN_DB_PREFIX."don";
$sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
$sql .= " AND entity IN (".getEntity('donnumber', 1, $don).")";

$resql = $db->query($sql);
if ($resql) {
$obj = $db->fetch_object($resql);
if ($obj) {
$max = intval($obj->max);
} else {
$max = 0;
}
} else {
return -1;
}

if ($mode == 'last') {
if ($max >= (pow(10, 4) - 1)) {
$num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
} else {
$num = sprintf("%04s", $max);
}

$ref = '';
$sql = "SELECT ref as ref";
$sql .= " FROM ".MAIN_DB_PREFIX."don";
$sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
$sql .= " AND entity IN (".getEntity('donnumber', 1, $don).")";
$sql .= " ORDER BY ref DESC";

$resql = $db->query($sql);
if ($resql) {
$obj = $db->fetch_object($resql);
if ($obj) {
$ref = $obj->ref;
}
} else {
dol_print_error($db);
}

return $ref;
} elseif ($mode == 'next') {
$date = $don->date; // This is don date (not creation date)
$yymm = strftime("%y%m", $date);

if ($max >= (pow(10, 4) - 1)) {
$num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
} else {
$num = sprintf("%04s", $max + 1);
}

dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
return $prefix.$yymm."-".$num;
} else {
dol_print_error('', 'Bad parameter for getNextValue');
}

return 0;
}

/**
* Return next free value
*
* @param Societe $objsoc Object third party
* @param string $objforref Object for number to search
* @param string $mode 'next' for next value or 'last' for last value
* @return string Next free value
*/
public function getNumRef($objsoc, $objforref, $mode = 'next')
{
return $this->getNextValue($objsoc, $objforref, $mode);
}
}
134 changes: 134 additions & 0 deletions htdocs/don/admin/donation.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
dolibarr_del_const($db, 'DON_ADDON_MODEL', $conf->entity);
}
}
} elseif ($action == 'setmod') {
// TODO Verifier si module numerotation choisi peut etre active
// par appel methode canBeActivated

dolibarr_set_const($db, "DON_ADDON", $value, 'chaine', 0, '', $conf->entity);
}

// Options
Expand Down Expand Up @@ -322,6 +327,135 @@

print '</table><br>';

/*
* Numbering module
*/

print load_fiche_titre($langs->trans("BillsNumberingModule"), '', '');

print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Name").'</td>';
print '<td>'.$langs->trans("Description").'</td>';
print '<td class="nowrap">'.$langs->trans("Example").'</td>';
print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
print '</tr>'."\n";

clearstatcache();

foreach ($dirmodels as $reldir) {
$dir = dol_buildpath($reldir."core/modules/dons/");
if (is_dir($dir)) {
$handle = opendir($dir);
if (is_resource($handle)) {
while (($file = readdir($handle)) !== false) {
if (!is_dir($dir.$file) || (substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS')) {
$filebis = $file;
$classname = preg_replace('/\.php$/', '', $file);
// For compatibility
if (!is_file($dir.$filebis)) {
$filebis = $file."/".$file.".modules.php";
$classname = "mod_don_".$file;
}
// Check if there is a filter on country
preg_match('/\-(.*)_(.*)$/', $classname, $reg);
if (!empty($reg[2]) && $reg[2] != strtoupper($mysoc->country_code)) {
continue;
}

$classname = preg_replace('/\-.*$/', '', $classname);
if (!class_exists($classname) && is_readable($dir.$filebis) && (preg_match('/mod_/', $filebis) || preg_match('/mod_/', $classname)) && substr($filebis, dol_strlen($filebis) - 3, 3) == 'php') {
// Charging the numbering class
require_once $dir.$filebis;

$module = new $classname($db);

// Show modules according to features level
if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
continue;
}
if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
continue;
}

if ($module->isEnabled()) {
print '<tr class="oddeven"><td width="100">';
echo preg_replace('/\-.*$/', '', preg_replace('/mod_don_/', '', preg_replace('/\.php$/', '', $file)));
print "</td><td>\n";

print $module->info();

print '</td>';

// Show example of numbering module
print '<td class="nowrap">';
$tmp = $module->getExample();
if (preg_match('/^Error/', $tmp)) {
$langs->load("errors");
print '<div class="error">'.$langs->trans($tmp).'</div>';
} elseif ($tmp == 'NotConfigured') {
print '<span class="opacitymedium">'.$langs->trans($tmp).'</span>';
} else {
print $tmp;
}
print '</td>'."\n";

print '<td class="center">';
//print "> ".$conf->global->DON_ADDON." - ".$file;
if ($conf->global->DON_ADDON == $file || $conf->global->DON_ADDON.'.php' == $file) {
print img_picto($langs->trans("Activated"), 'switch_on');
} else {
print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.preg_replace('/\.php$/', '', $file).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
}
print '</td>';

$don = new Don($db);
$don->initAsSpecimen();

// Example for standard invoice
$htmltooltip = '';
$htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
$don->type = 0;
$nextval = $module->getNextValue($mysoc, $don);
if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
$htmltooltip .= $langs->trans("NextValueForInvoices").': ';
if ($nextval) {
if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
$nextval = $langs->trans($nextval);
}
$htmltooltip .= $nextval.'<br>';
} else {
$htmltooltip .= $langs->trans($module->error).'<br>';
}
}
print '<td class="center">';
print $form->textwithpicto('', $htmltooltip, 1, 0);

if ($conf->global->DON_ADDON.'.php' == $file) { // If module is the one used, we show existing errors
if (!empty($module->error)) {
dol_htmloutput_mesg($module->error, '', 'error', 1);
}
}

print '</td>';

print "</tr>\n";
}
}
}
}
closedir($handle);
}
}
}

print '</table>';
print '</div>';



/*
* Params
*/
Expand Down
Loading

0 comments on commit 4cb15ff

Please sign in to comment.