diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index b46c249d1b0..21698339c67 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -6,6 +6,7 @@ * Copyright (C) 2012 Cedric Salvador * Copyright (C) 2013 Florian Henry * Copyright (C) 2015 Marcos García + * Copyright (C) 2017 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 @@ -107,7 +108,8 @@ class FactureRec extends CommonInvoice $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 @@ -127,6 +129,7 @@ class FactureRec extends CommonInvoice $sql.= ", remise"; $sql.= ", note_private"; $sql.= ", note_public"; + $sql.= ", modelpdf"; $sql.= ", fk_user_author"; $sql.= ", fk_projet"; $sql.= ", fk_account"; @@ -140,6 +143,7 @@ class FactureRec extends CommonInvoice $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; @@ -149,6 +153,7 @@ class FactureRec extends CommonInvoice $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"); @@ -162,6 +167,7 @@ class FactureRec extends CommonInvoice $sql.= ", ".$this->nb_gen_done; $sql.= ", ".$this->nb_gen_max; $sql.= ", ".$this->auto_validate; + $sql.= ", ".$this->generate_pdf; $sql.= ")"; if ($this->db->query($sql)) @@ -276,9 +282,11 @@ class FactureRec extends CommonInvoice $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'; @@ -336,7 +344,7 @@ class FactureRec extends CommonInvoice $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; @@ -347,7 +355,8 @@ class FactureRec extends CommonInvoice $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 @@ -948,8 +957,18 @@ class FactureRec extends CommonInvoice $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) { @@ -1391,6 +1410,68 @@ class FactureRec extends CommonInvoice 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; + } + } } diff --git a/htdocs/compta/facture/fiche-rec.php b/htdocs/compta/facture/fiche-rec.php index 622b1bc7731..64e7eb3e15d 100644 --- a/htdocs/compta/facture/fiche-rec.php +++ b/htdocs/compta/facture/fiche-rec.php @@ -8,6 +8,7 @@ * Copyright (C) 2012 Cedric Salvador * Copyright (C) 2015 Alexandre Spangaro * Copyright (C) 2016 Meziane Sof + * Copyright (C) 2017 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 @@ -210,14 +211,15 @@ if (empty($reshook)) { $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); @@ -345,6 +347,16 @@ if (empty($reshook)) 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 @@ -1040,6 +1052,13 @@ if ($action == 'create') print ""; } + // Model pdf + print "".$langs->trans('Model').""; + 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 ""; + print ""; dol_fiche_end(); @@ -1075,6 +1094,12 @@ if ($action == 'create') print $form->selectarray('auto_validate', $select, GETPOST('auto_validate')); print ""; + // Auto generate document + print "".$langs->trans("StatusOfGeneratedDocuments").""; + $select = array('0'=>$langs->trans('DoNotGenerateDoc'),'1'=>$langs->trans('AutoGenerateDoc')); + print $form->selectarray('generate_pdf', $select, GETPOST('generate_pdf')); + print ""; + print ""; dol_fiche_end(); @@ -1345,6 +1370,34 @@ else print ""; print ''; + // Model pdf + $langs->load('banks'); + + print ''; + print ''; + print '
'; + print $langs->trans('Model'); + print ''; + if (($action != 'editmodelpdf') && $user->rights->facture->creer && ! empty($object->brouillon)) + print 'id.'">'.img_edit($langs->trans('SetModel'),1).'
'; + print ''; + 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 ""; + print ''; // Other attributes $cols = 2; @@ -1449,12 +1502,25 @@ else else print $langs->trans("StatusOfGeneratedInvoices"); print ''; - $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 ''; + // Auto generate documents + print ''; + 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 ''; + $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 ''; print ''; print ''; diff --git a/htdocs/core/lib/security2.lib.php b/htdocs/core/lib/security2.lib.php index e953a8018cb..7d305ded7d4 100644 --- a/htdocs/core/lib/security2.lib.php +++ b/htdocs/core/lib/security2.lib.php @@ -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; diff --git a/htdocs/install/mysql/migration/6.0.0-7.0.0.sql b/htdocs/install/mysql/migration/6.0.0-7.0.0.sql index 72ede0445de..c185d3c9afd 100644 --- a/htdocs/install/mysql/migration/6.0.0-7.0.0.sql +++ b/htdocs/install/mysql/migration/6.0.0-7.0.0.sql @@ -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); diff --git a/htdocs/install/mysql/tables/llx_facture_rec.sql b/htdocs/install/mysql/tables/llx_facture_rec.sql index 941bc06627d..87bb56e7dbe 100644 --- a/htdocs/install/mysql/tables/llx_facture_rec.sql +++ b/htdocs/install/mysql/tables/llx_facture_rec.sql @@ -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), @@ -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; diff --git a/htdocs/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang index b3f9c030d3e..07b5e6cc0a8 100644 --- a/htdocs/langs/en_US/bills.lang +++ b/htdocs/langs/en_US/bills.lang @@ -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 \ No newline at end of file