Skip to content

Commit

Permalink
Merge pull request #551 from ATM-Consulting/BACKPORT/132/TK2411-3423
Browse files Browse the repository at this point in the history
feat(model-invoice): new field rule for lines dates
  • Loading branch information
atm-lena authored Jan 7, 2025
2 parents 59da789 + fe7d8ed commit 4cb636b
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 4 deletions.
1 change: 1 addition & 0 deletions ChangeLog_Koesio.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Backported from 17.0 :
- Backport develop PR #32129 : New field rule for lines dates
- Backport 17.0 PR #32247 : added error message in cron
- Backport develop : fix abs fatal error
- Backport develop PR 31267 : fix error in code, putline() function in api contract overwrote the data that was not updated by the api - *2024-11-25*
Expand Down
35 changes: 35 additions & 0 deletions htdocs/compta/facture/card-rec.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@
/**BACKPORT PR 31698**/
$object->fk_societe_rib = GETPOSTINT('accountcustomerid');
/**BACKPORT PR 31698**/
/**BACKPORT PR 32129**/
$object->rule_for_lines_dates = GETPOST('rule_for_lines_dates', 'alpha');
/**BACKPORT PR 32129**/

$object->frequency = $frequency;
$object->unit_frequency = GETPOST('unit_frequency', 'alpha');
Expand Down Expand Up @@ -391,6 +394,12 @@
} elseif ($action == 'setmulticurrencyrate' && $usercancreate) {
// Multicurrency rate
$result = $object->setMulticurrencyRate(price2num(GETPOST('multicurrency_tx')), GETPOST('calculation_mode', 'int'));
} elseif ($action == 'setruleforlinesdates' && $usercancreate) {
/** BACKPORT PR 32129 */
$object->context['actionmsg'] = $langs->trans("FieldXModified", $langs->transnoentitiesnoconv("RuleForLinesDates"));
$ruleForLinesDates = GETPOSTISSET('rule_for_lines_dates') ? GETPOST('rule_for_lines_dates', 'alpha') : 'prepaid';
$object->setValueFrom('rule_for_lines_dates', $ruleForLinesDates);
/** BACKPORT PR 32129 */
}

// Delete line
Expand Down Expand Up @@ -1112,6 +1121,13 @@ function showselectfksocieterib(){
print "</td></tr>";
}

// Rule for lines dates
/** BACKPORT PR 32129 */
print "<tr><td>".$langs->trans("RuleForLinesDates")."</td><td>";
print $form->getSelectRuleForLinesDates(GETPOSTISSET('rule_for_lines_dates') ? GETPOST('rule_for_lines_dates', 'alpha') : $object->rule_for_lines_dates);
print "</td></tr>";
/** BACKPORT PR 32129 */

// Project
if (isModEnabled('project') && is_object($object->thirdparty) && $object->thirdparty->id > 0) {
$projectid = GETPOST('projectid') ?GETPOST('projectid') : $object->fk_project;
Expand Down Expand Up @@ -1519,6 +1535,25 @@ function showselectfksocieterib(){
print "</td>";
print '</tr>';

// Billing Term
/** BACKPORT 32129 */
print '<tr><td>';
print '<table class="nobordernopadding centpercent"><tr><td>';
print $langs->trans('RuleForLinesDates');
print '</td>';
if ($action != 'editruleforlinesdates' && $user->hasRight('facture', 'creer')) {
print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editruleforlinesdates&token='.newToken().'&facid='.$object->id.'">'.img_edit($langs->trans('SetRuleForLinesDates'), 1).'</a></td>';
}
print '</tr></table>';
print '</td><td>';
if ($action == 'editruleforlinesdates') {
$form->form_rule_for_lines_dates($_SERVER['PHP_SELF'].'?facid='.$object->id, $object->rule_for_lines_dates, 'rule_for_lines_dates');
} else {
$form->form_rule_for_lines_dates($_SERVER['PHP_SELF'].'?facid='.$object->id, $object->rule_for_lines_dates, 'none');
}
print '</td></tr>';
/** BACKPORT 32129 */

// Model pdf
print '<tr><td class="nowrap">';
print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
Expand Down
22 changes: 22 additions & 0 deletions htdocs/compta/facture/class/facture-rec.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ class FactureRec extends CommonInvoice
*/
public $fk_societe_rib;

/** BACKPORT PR 32129 */
/**
* @var string
*/
public $rule_for_lines_dates;
/** BACKPORT PR 32129 */

const PAYMENTCODETOEDITSOCIETERIB = "PRV";
/**BACKPORT PR 31698**/

Expand Down Expand Up @@ -220,6 +227,9 @@ class FactureRec extends CommonInvoice
/**BACKPORT PR 31698**/
'fk_societe_rib' => array('type' => 'integer', 'label' => 'Fk Societe RIB', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 175),
/**BACKPORT PR 31698**/
/** BACKPORT PR 32129 **/
'rule_for_lines_dates' => array('type' => 'varchar(255)', 'label' => 'RuleForLinesDates', 'enabled' => 1, 'visible' => 1, 'position' => 153, 'arrayofkeyval' => array('prepaid' => "Prepaid", 'postpaid' => "Postpaid"), 'default' => 'prepaid'),
/** BACKPORT PR 32129 **/
);
// END MODULEBUILDER PROPERTIES

Expand Down Expand Up @@ -314,6 +324,9 @@ public function create($user, $facid, $notrigger = 0)
/**BACKPORT PR 31698**/
$sql .= ", fk_societe_rib";
/**BACKPORT PR 31698**/
/**BACKPORT PR 32129**/
$sql .= ", rule_for_lines_dates";
/**BACKPORT PR 32129**/
$sql .= ") VALUES (";
$sql .= "'".$this->db->escape($this->titre ? $this->titre : $this->title)."'";
$sql .= ", ".((int) $this->fk_soc);
Expand Down Expand Up @@ -345,6 +358,9 @@ public function create($user, $facid, $notrigger = 0)
/**BACKPORT PR 31698**/
$sql .= ", ".(!empty($this->fk_societe_rib) ? ((int) $this->fk_societe_rib) : 'NULL');
/**BACKPORT PR 31698**/
/** BACKPORT PR 32129 */
$sql .= ", ".(!empty($this->rule_for_lines_dates) ? ("'".$this->db->escape($this->rule_for_lines_dates)."'") : "NULL");
/** BACKPORT PR 32129 */
$sql .= ")";
if ($this->db->query($sql)) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec");
Expand Down Expand Up @@ -557,6 +573,9 @@ public function fetch($rowid, $ref = '', $ref_ext = '')
/**BACKPORT PR 31698**/
$sql .= ', f.fk_account, f.fk_societe_rib';
/**BACKPORT PR 31698**/
/**BACKPORT PR 32129 **/
$sql .= ', f.rule_for_lines_dates';
/**BACKPORT PR 32129 **/
$sql .= ', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.usenewprice, f.auto_validate';
$sql .= ', f.generate_pdf';
$sql .= ", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
Expand Down Expand Up @@ -620,6 +639,9 @@ public function fetch($rowid, $ref = '', $ref_ext = '')
//$this->special_code = $obj->special_code;
$this->frequency = $obj->frequency;
$this->unit_frequency = $obj->unit_frequency;
/** BACKPORT PR 32129 **/
$this->rule_for_lines_dates = $obj->rule_for_lines_dates;
/** BACKPORT PR 32129 **/
$this->date_when = $this->db->jdate($obj->date_when);
$this->date_last_gen = $this->db->jdate($obj->date_last_gen);
$this->nb_gen_done = $obj->nb_gen_done;
Expand Down
17 changes: 15 additions & 2 deletions htdocs/compta/facture/class/facture.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,23 @@ public function create(User $user, $notrigger = 0, $forceduedate = 0)
// Define some dates
$originaldatewhen = $_facrec->date_when;
$nextdatewhen = null; $previousdaynextdatewhen = null;
/** BACKPORT PR 32129 **/
if ($originaldatewhen) {
$nextdatewhen = dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency);
$previousdaynextdatewhen = dol_time_plus_duree($nextdatewhen, -1, 'd');
if ($_facrec->rule_for_lines_dates == 'prepaid') {
$nextdatewhen = dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency);
}

if ($_facrec->rule_for_lines_dates == 'postpaid') {
$previousdaynextdatewhen = dol_time_plus_duree($originaldatewhen, -1, 'd');
} else {
$previousdaynextdatewhen = dol_time_plus_duree($nextdatewhen, -1, 'd');
}

$originaldatewhen = $_facrec->rule_for_lines_dates == 'postpaid'
? dol_time_plus_duree($originaldatewhen, -$_facrec->frequency, $_facrec->unit_frequency)
: $originaldatewhen;
}
/** BACKPORT PR 32129 **/

if (!empty($_facrec->frequency)) { // Invoice are created on same thirdparty than template when there is a recurrence, but not necessarly when there is no recurrence.
$this->socid = $_facrec->socid;
Expand Down
104 changes: 104 additions & 0 deletions htdocs/core/class/html.form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Form
public $cache_demand_reason = array();
public $cache_types_fees = array();
public $cache_vatrates = array();
public $cache_rule_for_lines_dates = array();


/**
Expand Down Expand Up @@ -4226,6 +4227,24 @@ public function getSelectConditionsPaiements($selected = 0, $htmlname = 'condid'
return $out;
}

/**
* Loads into cache rule for lines dates
*
* @return int 1=OK ; -1=Empty
*/
public function load_cache_rule_for_lines_dates()
{
$factureRec = new FactureRec($this->db);

$this->cache_rule_for_lines_dates = $factureRec->fields['rule_for_lines_dates']['arrayofkeyval'];

if (empty($this->cache_rule_for_lines_dates)) {
return -1;
}

return 1;
}


// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
Expand Down Expand Up @@ -4639,6 +4658,47 @@ public function selectSituationInvoices($selected = '', $socid = 0)
return $opt;
}

/** BACKPORT PR 32129 */
/**
* Returns select with rule for lines dates
*
* @param $selected Selected value
* @param $htmlname HTML element name
* @param $addempty Add empty option ?
* @return string HTML string with all datas
*/
public function getSelectRuleForLinesDates($selected = '', $htmlname = 'rule_for_lines_dates', $addempty = 0)
{
global $langs;

$out = '';

$this->load_cache_rule_for_lines_dates();

$out .= '<select id="' . $htmlname . '" class="flat selectbillingterm" name="' . $htmlname . '">';
if ($addempty) {
$out .= '<option value="-1">&nbsp;</option>';
}


foreach ($this->cache_rule_for_lines_dates as $rule_for_lines_dates_key => $rule_for_lines_dates_name) {
if ($selected == $rule_for_lines_dates_key) {
$out .= '<option value="' . $rule_for_lines_dates_key . '" selected>';
} else {
$out .= '<option value="' . $rule_for_lines_dates_key . '">';
}

$out .= $langs->trans($rule_for_lines_dates_name);
$out .= '</option>';
}
$out .= '</select>';

$out .= ajax_combobox($htmlname);

return $out;
}
/** BACKPORT PR 32129 */

/**
* Creates HTML units selector (code => label)
*
Expand Down Expand Up @@ -6205,6 +6265,50 @@ public function selectCurrency($selected = '', $htmlname = 'currency_id', $mode
return $out;
}

// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Form select for rule for lines dates
*
* @param $page Page
* @param string $selected Id condition pre-selectionne
* @param string $htmlname Name of select html field
* @param int $addempty Add empty entry
* @param int $nooutput No print is done. String is returned.
* @return string HTML output or ''
*/
public function form_rule_for_lines_dates($page, $selected = '', $htmlname = 'rule_for_lines_dates', $addempty = 0, $nooutput = 0): string
{
global $langs;

$out = '';

if ($htmlname != 'none') {
$out .= '<form method="POST" action="' . $page . '">';
$out .= '<input type="hidden" name="action" value="setruleforlinesdates">';
$out .= '<input type="hidden" name="token" value="' . newToken() . '">';
$out .= $this->getSelectRuleForLinesDates($selected, $htmlname, $addempty);
$out .= '<input type="submit" class="button valignmiddle smallpaddingimp" value="' . $langs->trans("Modify") . '">';
$out .= '</form>';
} else {
if (isset($selected)) {
$this->load_cache_rule_for_lines_dates();
if (isset($this->cache_rule_for_lines_dates[$selected])) {
$label = $this->cache_rule_for_lines_dates[$selected];
$out .= $langs->trans($label);
}
} else {
$out .= '&nbsp;';
}
}

if (empty($nooutput)) {
print $out;
return '';
}

return $out;
}

/**
* Return array of currencies in user language
*
Expand Down
3 changes: 2 additions & 1 deletion htdocs/install/mysql/tables/llx_facture_rec.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ create table llx_facture_rec
usenewprice integer DEFAULT 0, -- update invoice with current price of product instead of recorded price
frequency integer, -- frequency (for example: 3 for every 3 month)
unit_frequency varchar(2) DEFAULT 'm', -- 'm' for month (date_when must be a day <= 28), 'y' for year, ...

rule_for_lines_dates varchar(255) DEFAULT 'prepaid',

date_when datetime DEFAULT NULL, -- date for next gen (when an invoice is generated, this field must be updated with next date)
date_last_gen datetime DEFAULT NULL, -- date for last gen (date with last successfull generation of invoice)
nb_gen_done integer DEFAULT NULL, -- nb of generation done (when an invoice is generated, this field must incremented)
Expand Down
6 changes: 5 additions & 1 deletion htdocs/langs/en_US/compta.lang
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,8 @@ InvoiceToPay15Days=To pay (15 to 30 days)
InvoiceToPay30Days=To pay (> 30 days)
ConfirmPreselectAccount=Preselect accountancy code
ConfirmPreselectAccountQuestion=Are you sure you want to preselect the %s selected lines with this accountancy code ?
AmountPaidMustMatchAmountOfDownPayment=Amount paid must match amount of down payment
AmountPaidMustMatchAmountOfDownPayment=Amount paid must match amount of down payment
RuleForLinesDates=Rule for lines dates
Prepaid=Prepaid
Postpaid=Postpaid
SetRuleForLinesDates=Set rule for lines dates
4 changes: 4 additions & 0 deletions htdocs/langs/fr_FR/compta.lang
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,7 @@ InvoiceToPay30Days=A payer (dans plus de 30 jours)
ConfirmPreselectAccount=Présélectionner le code comptable
ConfirmPreselectAccountQuestion=Êtes-vous sûr de vouloir présélectionner les %s lignes sélectionnées avec ce code comptable ?
AmountPaidMustMatchAmountOfDownPayment=Le montant payé doit correspondre au montant de l'acompte
RuleForLinesDates=Règle pour les dates de lignes
Prepaid=Échu
Postpaid=À échoir
SetRuleForLinesDates=Définir la règle pour les dates de lignes

0 comments on commit 4cb636b

Please sign in to comment.