Skip to content

Commit

Permalink
Merge pull request #7790 from frederic34/facrecmodelpdf
Browse files Browse the repository at this point in the history
NEW generate also document when invoice is build from recurring template
  • Loading branch information
eldy committed Nov 11, 2017
2 parents 34c8740 + 5621e85 commit cba3a38
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 9 deletions.
89 changes: 85 additions & 4 deletions htdocs/compta/facture/class/facture-rec.class.php
Expand Up @@ -6,6 +6,7 @@
* Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2017 Frédéric France <frederic.france@netlogic.fr>
*
* 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
Expand Down Expand Up @@ -107,7 +108,8 @@ function create($user, $facid)
$this->nb_gen_done=0;
$this->nb_gen_max=empty($this->nb_gen_max)?0:$this->nb_gen_max;
$this->auto_validate=empty($this->auto_validate)?0:$this->auto_validate;

$this->generate_pdf = empty($this->generate_pdf)?0:$this->generate_pdf;

$this->db->begin();

// Charge facture modele
Expand All @@ -127,6 +129,7 @@ function create($user, $facid)
$sql.= ", remise";
$sql.= ", note_private";
$sql.= ", note_public";
$sql.= ", modelpdf";
$sql.= ", fk_user_author";
$sql.= ", fk_projet";
$sql.= ", fk_account";
Expand All @@ -140,6 +143,7 @@ function create($user, $facid)
$sql.= ", nb_gen_done";
$sql.= ", nb_gen_max";
$sql.= ", auto_validate";
$sql.= ", generate_pdf";
$sql.= ") VALUES (";
$sql.= "'".$this->db->escape($this->titre)."'";
$sql.= ", ".$facsrc->socid;
Expand All @@ -149,6 +153,7 @@ function create($user, $facid)
$sql.= ", ".(!empty($facsrc->remise)?$this->remise:'0');
$sql.= ", ".(!empty($this->note_private)?("'".$this->db->escape($this->note_private)."'"):"NULL");
$sql.= ", ".(!empty($this->note_public)?("'".$this->db->escape($this->note_public)."'"):"NULL");
$sql.= ", ".(!empty($this->modelpdf)?("'".$this->db->escape($this->modelpdf)."'"):"NULL");
$sql.= ", '".$this->db->escape($user->id)."'";
$sql.= ", ".(! empty($facsrc->fk_project)?"'".$facsrc->fk_project."'":"null");
$sql.= ", ".(! empty($facsrc->fk_account)?"'".$facsrc->fk_account."'":"null");
Expand All @@ -162,6 +167,7 @@ function create($user, $facid)
$sql.= ", ".$this->nb_gen_done;
$sql.= ", ".$this->nb_gen_max;
$sql.= ", ".$this->auto_validate;
$sql.= ", ".$this->generate_pdf;
$sql.= ")";

if ($this->db->query($sql))
Expand Down Expand Up @@ -276,9 +282,11 @@ function fetch($rowid, $ref='', $ref_ext='', $ref_int='')
$sql.= ', f.remise_percent, f.remise_absolue, f.remise';
$sql.= ', f.date_lim_reglement as dlr';
$sql.= ', f.note_private, f.note_public, f.fk_user_author';
$sql.= ', f.modelpdf';
$sql.= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet';
$sql.= ', f.fk_account';
$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.= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
$sql.= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
//$sql.= ', el.fk_source';
Expand Down Expand Up @@ -336,7 +344,7 @@ function fetch($rowid, $ref='', $ref_ext='', $ref_int='')
$this->note_private = $obj->note_private;
$this->note_public = $obj->note_public;
$this->user_author = $obj->fk_user_author;
$this->modelpdf = $obj->model_pdf;
$this->modelpdf = $obj->modelpdf;
$this->rang = $obj->rang;
$this->special_code = $obj->special_code;
$this->frequency = $obj->frequency;
Expand All @@ -347,7 +355,8 @@ function fetch($rowid, $ref='', $ref_ext='', $ref_int='')
$this->nb_gen_max = $obj->nb_gen_max;
$this->usenewprice = $obj->usenewprice;
$this->auto_validate = $obj->auto_validate;

$this->generate_pdf = $obj->generate_pdf;

if ($this->statut == self::STATUS_DRAFT) $this->brouillon = 1;

// Retreive all extrafield for thirdparty
Expand Down Expand Up @@ -948,8 +957,18 @@ function createRecurringInvoices()
$this->errors = $facture->errors;
$this->error = $facture->error;
$error++;
}
}
}
if (! $error && $facturerec->generate_pdf)
{
$result = $facture->generateDocument($facturerec->modelpdf, $langs);
if ($result <= 0)
{
$this->errors = $facture->errors;
$this->error = $facture->error;
$error++;
}
}

if (! $error && $invoiceidgenerated >= 0)
{
Expand Down Expand Up @@ -1391,6 +1410,68 @@ function setAutoValidate($validate)
return -1;
}
}

/**
* Update the auto generate documents
*
* @param int $validate 0 no document, 1 to generate document
* @return int <0 if KO, >0 if OK
*/
function setGeneratePdf($validate)
{
if (! $this->table_element)
{
dol_syslog(get_class($this)."::setGeneratePdf was called on objet with property table_element not defined",LOG_ERR);
return -1;
}

$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
$sql.= ' SET generate_pdf = '.$validate;
$sql.= ' WHERE rowid = '.$this->id;

dol_syslog(get_class($this)."::setGeneratePdf", LOG_DEBUG);
if ($this->db->query($sql))
{
$this->generate_pdf = $validate;
return 1;
}
else
{
dol_print_error($this->db);
return -1;
}
}

/**
* Update the model for documents
*
* @param string $model model of document generator
* @return int <0 if KO, >0 if OK
*/
function setModelPdf($model)
{
if (! $this->table_element)
{
dol_syslog(get_class($this)."::setModelPdf was called on objet with property table_element not defined",LOG_ERR);
return -1;
}

$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
$sql.= ' SET modelpdf = "' . $model . '"';
$sql.= ' WHERE rowid = '.$this->id;

dol_syslog(get_class($this)."::setModelPdf", LOG_DEBUG);
if ($this->db->query($sql))
{
$this->modelpdf = $model;
return 1;
}
else
{
dol_print_error($this->db);
return -1;
}
}
}


Expand Down
72 changes: 69 additions & 3 deletions htdocs/compta/facture/fiche-rec.php
Expand Up @@ -8,6 +8,7 @@
* Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
* Copyright (C) 2016 Meziane Sof <virtualsof@yahoo.fr>
* Copyright (C) 2017 Frédéric France <frederic.france@netlogic.fr>
*
* 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
Expand Down Expand Up @@ -210,14 +211,15 @@
{
$object->titre = GETPOST('titre', 'alpha');
$object->note_private = GETPOST('note_private','none');
$object->note_public = GETPOST('note_public','none');
$object->note_public = GETPOST('note_public','none');
$object->modelpdf = GETPOST('modelpdf', 'alpha');
$object->usenewprice = GETPOST('usenewprice');

$object->frequency = $frequency;
$object->unit_frequency = GETPOST('unit_frequency', 'alpha');
$object->nb_gen_max = $nb_gen_max;
$object->auto_validate = GETPOST('auto_validate', 'int');

$object->generate_pdf = GETPOST('generate_pdf', 'int');
$object->fk_project = $projectid;

$date_next_execution = dol_mktime($rehour, $remin, 0, $remonth, $reday, $reyear);
Expand Down Expand Up @@ -345,6 +347,16 @@
elseif ($action == 'setauto_validate' && $user->rights->facture->creer)
{
$object->setAutoValidate(GETPOST('auto_validate', 'int'));
}
// Set generate pdf
elseif ($action == 'setgenerate_pdf' && $user->rights->facture->creer)
{
$object->setGeneratepdf(GETPOST('generate_pdf', 'int'));
}
// Set model pdf
elseif ($action == 'setmodelpdf' && $user->rights->facture->creer)
{
$object->setModelpdf(GETPOST('modelpdf', 'alpha'));
}

// Delete line
Expand Down Expand Up @@ -1040,6 +1052,13 @@
print "</td></tr>";
}

// Model pdf
print "<tr><td>".$langs->trans('Model')."</td><td>";
include_once DOL_DOCUMENT_ROOT . '/core/modules/facture/modules_facture.php';
$list = ModelePDFFactures::liste_modeles($db);
print $form->selectarray('modelpdf', $list, $conf->global->FACTURE_ADDON_PDF);
print "</td></tr>";

print "</table>";

dol_fiche_end();
Expand Down Expand Up @@ -1075,6 +1094,12 @@
print $form->selectarray('auto_validate', $select, GETPOST('auto_validate'));
print "</td></tr>";

// Auto generate document
print "<tr><td>".$langs->trans("StatusOfGeneratedDocuments")."</td><td>";
$select = array('0'=>$langs->trans('DoNotGenerateDoc'),'1'=>$langs->trans('AutoGenerateDoc'));
print $form->selectarray('generate_pdf', $select, GETPOST('generate_pdf'));
print "</td></tr>";

print "</table>";

dol_fiche_end();
Expand Down Expand Up @@ -1345,6 +1370,34 @@
print "</td>";
print '</tr>';

// Model pdf
$langs->load('banks');

print '<tr><td class="nowrap">';
print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
print $langs->trans('Model');
print '<td>';
if (($action != 'editmodelpdf') && $user->rights->facture->creer && ! empty($object->brouillon))
print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editmodelpdf&amp;id='.$object->id.'">'.img_edit($langs->trans('SetModel'),1).'</a></td>';
print '</tr></table>';
print '</td><td colspan="3">';
if ($action == 'editmodelpdf')
{
include_once DOL_DOCUMENT_ROOT . '/core/modules/facture/modules_facture.php';
$list = array();
$models = ModelePDFFactures::liste_modeles($db);
foreach ($models as $model) {
$list[] = $model . ':' . $model;
}
$select = 'select;'.implode(',', $list);
print $form->editfieldval($langs->trans("Model"), 'modelpdf', $object->modelpdf, $object, $user->rights->facture->creer, $select);
}
else
{
print $object->modelpdf;
}
print "</td>";
print '</tr>';

// Other attributes
$cols = 2;
Expand Down Expand Up @@ -1449,12 +1502,25 @@
else
print $langs->trans("StatusOfGeneratedInvoices");
print '</td><td>';
$select = 'select;0:'.$langs->trans('BillStatusDraft').',1:'.$langs->trans('BillStatusValidated');
$select = 'select;0:'.$langs->trans('BillStatusDraft').',1:'.$langs->trans('BillStatusValidated');
if ($action == 'auto_validate' || $object->frequency > 0)
{
print $form->editfieldval($langs->trans("StatusOfGeneratedInvoices"), 'auto_validate', $object->auto_validate, $object, $user->rights->facture->creer, $select);
}
print '</td>';
// Auto generate documents
print '<tr><td>';
if ($action == 'generate_pdf' || $object->frequency > 0)
print $form->editfieldkey($langs->trans("StatusOfGeneratedDocuments"), 'generate_pdf', $object->generate_pdf, $object, $user->rights->facture->creer);
else
print $langs->trans("StatusOfGeneratedDocuments");
print '</td><td>';
$select = 'select;0:'.$langs->trans('DoNotGenerateDoc').',1:'.$langs->trans('AutogenerateDoc');
if ($action == 'generate_pdf' || $object->frequency > 0)
{
print $form->editfieldval($langs->trans("StatusOfGeneratedDocuments"), 'generate_pdf', $object->generate_pdf, $object, $user->rights->facture->creer, $select);
}
print '</td>';
print '</tr>';

print '</table>';
Expand Down
2 changes: 1 addition & 1 deletion htdocs/core/lib/security2.lib.php
Expand Up @@ -456,7 +456,7 @@ function getRandomPassword($generic=false)
$randomCode = "";
$nbofchar = round($length/3);
$nbofcharlast = ($length - 2*$nbofchar);
var_dump($nbofchar.'-'.$nbofcharlast);
//var_dump($nbofchar.'-'.$nbofcharlast);
if (function_exists('random_int')) // Cryptographic random
{
$max = strlen($lowercase) - 1;
Expand Down
3 changes: 3 additions & 0 deletions htdocs/install/mysql/migration/6.0.0-7.0.0.sql
Expand Up @@ -528,6 +528,9 @@ ALTER TABLE llx_resource ADD UNIQUE INDEX uk_resource_ref (ref, entity);
ALTER TABLE llx_product ADD COLUMN accountancy_code_sell_intra varchar(32) AFTER accountancy_code_sell;
ALTER TABLE llx_product ADD COLUMN accountancy_code_sell_export varchar(32) AFTER accountancy_code_sell_intra;

ALTER TABLE llx_facture_rec ADD COLUMN modelpdf varchar(255) AFTER note_public;
ALTER TABLE llx_facture_rec ADD COLUMN generate_pdf integer DEFAULT 0 AFTER auto_validate;

-- SPEC : use database type 'double' to store monetary values
ALTER TABLE llx_blockedlog MODIFY COLUMN amounts double(24,8);
ALTER TABLE llx_chargessociales MODIFY COLUMN amount double(24,8);
Expand Down
4 changes: 3 additions & 1 deletion htdocs/install/mysql/tables/llx_facture_rec.sql
Expand Up @@ -54,6 +54,7 @@ create table llx_facture_rec
fk_account integer, -- bank account id
note_private text,
note_public text,
modelpdf varchar(255),

fk_multicurrency integer,
multicurrency_code varchar(255),
Expand All @@ -70,5 +71,6 @@ create table llx_facture_rec
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)
nb_gen_max integer DEFAULT NULL, -- maximum number of generation
auto_validate integer DEFAULT 0 -- 0 to create in draft, 1 to create and validate the new invoice
auto_validate integer DEFAULT 0, -- 0 to create in draft, 1 to create and validate the new invoice
generate_pdf integer DEFAULT 0 -- 0 disable pdf, 1 to generate pdf
)ENGINE=innodb;
2 changes: 2 additions & 0 deletions htdocs/langs/en_US/bills.lang
Expand Up @@ -512,3 +512,5 @@ DeleteRepeatableInvoice=Delete template invoice
ConfirmDeleteRepeatableInvoice=Are your sure you want to delete the template invoice?
CreateOneBillByThird=Create one invoice per third party (otherwise, one invoice per order)
BillCreated=%s bill(s) created
StatusOfGeneratedDocuments=Status of document generation
AutogenerateDoc=Auto generate document

0 comments on commit cba3a38

Please sign in to comment.