From 671018c5cccfc409f4d6015553c89c1d91cb45bd Mon Sep 17 00:00:00 2001
From: aspangaro <alexandre.spangaro@gmail.com>
Date: Sun, 6 Sep 2015 21:53:28 +0200
Subject: [PATCH] New Accountancy Uniformize code & rename a constant

---
 htdocs/accountancy/admin/export.php           | 96 +++++++++++++++++--
 htdocs/accountancy/journal/bankjournal.php    | 11 ++-
 .../accountancy/journal/purchasesjournal.php  | 18 ++--
 htdocs/accountancy/journal/sellsjournal.php   | 12 +--
 htdocs/accountancy/tpl/export_journal.tpl.php | 28 ++++++
 htdocs/accountancy/tpl/index.html             |  0
 .../install/mysql/migration/3.8.0-3.9.0.sql   |  2 +
 7 files changed, 134 insertions(+), 33 deletions(-)
 create mode 100644 htdocs/accountancy/tpl/export_journal.tpl.php
 create mode 100644 htdocs/accountancy/tpl/index.html

diff --git a/htdocs/accountancy/admin/export.php b/htdocs/accountancy/admin/export.php
index 856670e18e2..6ba6c05208a 100644
--- a/htdocs/accountancy/admin/export.php
+++ b/htdocs/accountancy/admin/export.php
@@ -4,7 +4,7 @@
  * Copyright (C) 2014	   Florian Henry		<florian.henry@open-concept.pro>
  * Copyright (C) 2014      Marcos García        <marcosgdf@gmail.com>
  * Copyright (C) 2014	   Juanjo Menent		<jmenent@2byte.es>
- * Copyright (C) 2015       Jean-François Ferry		<jfefe@aternatik.fr>
+ * Copyright (C) 2015      Jean-François Ferry	<jfefe@aternatik.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
@@ -43,8 +43,12 @@ if (!$user->admin)
 
 $action = GETPOST('action', 'alpha');
 
-// Other parameters ACCOUNTING_EXPORT_*
-$list = array (
+// Parameters ACCOUNTING_EXPORT_*
+$main_option = array (
+		'ACCOUNTING_EXPORT_PREFIX_SPEC'
+);
+
+$model_option = array (
 		'ACCOUNTING_EXPORT_SEPARATORCSV',
 		'ACCOUNTING_EXPORT_DATE',
 		'ACCOUNTING_EXPORT_PIECE',
@@ -60,10 +64,18 @@ $list = array (
 if ($action == 'update') {
 	$error = 0;
 
+	$format = GETPOST('format', 'alpha');
 	$modelcsv = GETPOST('modelcsv', 'int');
+	
+	if (! empty($format)) {
+		if (! dolibarr_set_const($db, 'ACCOUNTING_EXPORT_FORMAT', $format, 'chaine', 0, '', $conf->entity)) {
+			$error ++;
+		}
+	} else {
+		$error ++;
+	}
 
 	if (! empty($modelcsv)) {
-
 		if (! dolibarr_set_const($db, 'ACCOUNTING_EXPORT_MODELCSV', $modelcsv, 'chaine', 0, '', $conf->entity)) {
 			$error ++;
 		}
@@ -71,7 +83,15 @@ if ($action == 'update') {
 		$error ++;
 	}
 
-	foreach ( $list as $constname ) {
+	foreach ($main_option as $constname) {
+		$constvalue = GETPOST($constname, 'alpha');
+
+		if (! dolibarr_set_const($db, $constname, $constvalue, 'chaine', 0, '', $conf->entity)) {
+			$error ++;
+		}
+	}
+
+	foreach ($model_option as $constname) {
 		$constvalue = GETPOST($constname, 'alpha');
 
 		if (! dolibarr_set_const($db, $constname, $constvalue, 'chaine', 0, '', $conf->entity)) {
@@ -106,9 +126,67 @@ print '<input type="hidden" name="action" value="update">';
 
 dol_fiche_head($head, 'export', $langs->trans("Configuration"), 0, 'cron');
 
-print '<table class="noborder" width="100%">';
 $var = true;
 
+/*
+ * Main Options
+ */
+print '<table class="noborder" width="100%">';
+print '<tr class="liste_titre">';
+print '<td colspan="3">' . $langs->trans('MainOptions') . '</td>';
+print "</tr>\n";
+
+$var = ! $var;
+
+print '<tr ' . $bc[$var] . '>';
+print '<td width="50%">' . $langs->trans("Selectformat") . '</td>';
+if (! $conf->use_javascript_ajax)
+{
+	print '<td class="nowrap">';
+	print $langs->trans("NotAvailableWhenAjaxDisabled");
+	print "</td>";
+}
+else
+{
+	print '<td>';
+	$listformat=array(
+		'csv'=>$langs->trans("csv"),
+		'txt'=>$langs->trans("txt")
+	);
+	print $form->selectarray("format",$listformat,$conf->global->ACCOUNTING_EXPORT_FORMAT,0);
+
+	print '</td>';
+}
+print "</td></tr>";
+
+$num = count($main_option);
+if ($num)
+{
+	foreach ($main_option as $key) {
+		$var = ! $var;
+
+		print '<tr ' . $bc[$var] . ' class="value">';
+
+		// Param
+		$label = $langs->trans($key);
+		print '<td width="50%">' . $label . '</td>';
+
+		// Value
+		print '<td>';
+		print '<input type="text" size="20" name="' . $key . '" value="' . $conf->global->$key . '">';
+		print '</td></tr>';
+	}
+}
+
+print "</table>\n";
+
+print "<br>\n";
+
+/*
+ * Export model
+ */
+print '<table class="noborder" width="100%">';
+
 print '<tr class="liste_titre">';
 print '<td colspan="2">' . $langs->trans("Modelcsv") . '</td>';
 print '</tr>';
@@ -143,8 +221,8 @@ print "<br>\n";
  *  Parameters
  */
 
-$num = count($list);
-if ($num)
+$num2 = count($model_option);
+if ($num2)
 {
 	print '<table class="noborder" width="100%">';
 	print '<tr class="liste_titre">';
@@ -152,7 +230,7 @@ if ($num)
 	print "</tr>\n";
 	if ($conf->global->ACCOUNTING_EXPORT_MODELCSV > 1) print '<tr><td colspan="2" bgcolor="red"><b>' . $langs->trans('OptionsDeactivatedForThisExportModel') . '</b></td></tr>';
 
-	foreach ( $list as $key ) {
+	foreach ($model_option as $key) {
 		$var = ! $var;
 
 		print '<tr ' . $bc[$var] . ' class="value">';
diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php
index 250d9001679..3034297de7b 100644
--- a/htdocs/accountancy/journal/bankjournal.php
+++ b/htdocs/accountancy/journal/bankjournal.php
@@ -16,7 +16,7 @@
  * 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.
+ * GNU General Public License fr more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -25,7 +25,7 @@
 /**
  *	\file		htdocs/accountancy/journal/bankjournal.php
  *	\ingroup	Accounting Expert
- *	\brief		Page with sells journal
+ *	\brief		Page with bank journal
  */
 
 require '../../main.inc.php';
@@ -66,6 +66,8 @@ $date_endday = GETPOST('date_endday');
 $date_endyear = GETPOST('date_endyear');
 $action = GETPOST('action');
 
+$now = dol_now();
+
 // Security check
 if ($user->societe_id > 0)
 	accessforbidden();
@@ -421,10 +423,9 @@ if ($action == 'writeBookKeeping')
 if ($action == 'export_csv')
 {
 	$sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
-	$bank_journal = $conf->global->ACCOUNTING_BANK_JOURNAL;
+	$journal = $conf->global->ACCOUNTING_BANK_JOURNAL;
 
-	header('Content-Type: text/csv');
-	header('Content-Disposition: attachment;filename=journal_banque.csv');
+	include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
 
 	$companystatic = new Client($db);
 
diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php
index 8d0502ad57c..a5ba24b871f 100644
--- a/htdocs/accountancy/journal/purchasesjournal.php
+++ b/htdocs/accountancy/journal/purchasesjournal.php
@@ -3,7 +3,7 @@
  * Copyright (C) 2007-2010	Jean Heimburger		<jean@tiaris.info>
  * Copyright (C) 2011		Juanjo Menent		<jmenent@2byte.es>
  * Copyright (C) 2012		Regis Houssin		<regis@dolibarr.fr>
- * Copyright (C) 2013-2014  Alexandre Spangaro	<aspangaro.dolibarr@gmail.com>
+ * Copyright (C) 2013-2015  Alexandre Spangaro	<aspangaro.dolibarr@gmail.com>
  * Copyright (C) 2013-2014  Olivier Geffroy		<jeff@jeffinfo.com>
  * Copyright (C) 2013-2014  Florian Henry	    <florian.henry@open-concept.pro>
  *
@@ -52,6 +52,8 @@ $date_endmonth = GETPOST('date_endmonth');
 $date_endday = GETPOST('date_endday');
 $date_endyear = GETPOST('date_endyear');
 
+$now = dol_now();
+
 // Security check
 if ($user->societe_id > 0)
 	accessforbidden();
@@ -156,7 +158,6 @@ if ($result) {
 
 // Bookkeeping Write
 if ($action == 'writebookkeeping') {
-	$now = dol_now();
 
 	foreach ( $tabfac as $key => $val ) {
 		foreach ( $tabttc[$key] as $k => $mt ) {
@@ -246,15 +247,10 @@ $companystatic = new Fournisseur($db);
 if ($action == 'export_csv')
 {
 	$sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
-	$purchase_journal = $conf->global->ACCOUNTING_PURCHASE_JOURNAL;
-
-	header('Content-Type: text/csv');
-	if ($conf->global->EXPORT_PREFIX_SPEC)
-		$filename=$conf->global->EXPORT_PREFIX_SPEC."_"."journal_achats.csv";
-	else
-		$filename="journal_achats.csv";
-	header('Content-Disposition: attachment;filename='.$filename);
-
+	$journal = $conf->global->ACCOUNTING_PURCHASE_JOURNAL;
+	
+	include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
+	
 	if ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 2) 	// Model Cegid Expert Export
 	{
 		$sep = ";";
diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php
index 60fd2460067..c0fe6e84f96 100644
--- a/htdocs/accountancy/journal/sellsjournal.php
+++ b/htdocs/accountancy/journal/sellsjournal.php
@@ -54,6 +54,8 @@ $date_endmonth = GETPOST('date_endmonth');
 $date_endday = GETPOST('date_endday');
 $date_endyear = GETPOST('date_endyear');
 
+$now = dol_now();
+
 // Security check
 if ($user->societe_id > 0)
 	accessforbidden();
@@ -191,7 +193,6 @@ if ($result) {
 // Bookkeeping Write
 if ($action == 'writebookkeeping')
 {
-	$now = dol_now();
 
 	foreach ($tabfac as $key => $val)
 	{
@@ -275,14 +276,9 @@ if ($action == 'writebookkeeping')
 if ($action == 'export_csv')
 {
 	$sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
-	$sell_journal = $conf->global->ACCOUNTING_SELL_JOURNAL;
+	$journal = $conf->global->ACCOUNTING_SELL_JOURNAL;
 
-	header('Content-Type: text/csv');
-	if ($conf->global->EXPORT_PREFIX_SPEC)
-		$filename=$conf->global->EXPORT_PREFIX_SPEC."_"."journal_ventes.csv";
-	else
-		$filename="journal_ventes.csv";
-	header('Content-Disposition: attachment;filename='.$filename);
+	include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
 
 	$companystatic = new Client($db);
 
diff --git a/htdocs/accountancy/tpl/export_journal.tpl.php b/htdocs/accountancy/tpl/export_journal.tpl.php
new file mode 100644
index 00000000000..c1ac19912ae
--- /dev/null
+++ b/htdocs/accountancy/tpl/export_journal.tpl.php
@@ -0,0 +1,28 @@
+<?php
+/* Copyright (C) 2015  Alexandre Spangaro	<aspangaro.dolibarr@gmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+ 
+$prefix = $conf->global->ACCOUNTING_EXPORT_PREFIX_SPEC;
+$format = $conf->global->ACCOUNTING_EXPORT_FORMAT;
+	
+$date_export = dol_print_date($now, '%Y%m%d%H%M%S');
+
+header('Content-Type: text/csv');
+if ($prefix)
+	$filename = $prefix . "_" . "journal_" . $journal . $date_export . "." . $format;
+else
+	$filename = "journal_" . $journal . $date_export . "." . $format;
+header('Content-Disposition: attachment;filename='.$filename);
\ No newline at end of file
diff --git a/htdocs/accountancy/tpl/index.html b/htdocs/accountancy/tpl/index.html
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/htdocs/install/mysql/migration/3.8.0-3.9.0.sql b/htdocs/install/mysql/migration/3.8.0-3.9.0.sql
index 8b64acf2ce8..53b0eb4a6b6 100644
--- a/htdocs/install/mysql/migration/3.8.0-3.9.0.sql
+++ b/htdocs/install/mysql/migration/3.8.0-3.9.0.sql
@@ -23,3 +23,5 @@ INSERT INTO llx_const (name, value, type, note, visible) values ('MAIN_DELAY_EXP
 
 ALTER TABLE llx_accounting_system MODIFY COLUMN pcg_version varchar(32);
 ALTER TABLE llx_accountingaccount MODIFY COLUMN fk_pcg_version varchar(32);
+
+UPDATE llx_const SET name = __ENCRYPT('ACCOUNTING_EXPORT_PREFIX_SPEC')__ WHERE __DECRYPT('name')__ = 'EXPORT_PREFIX_SPEC';
-- 
GitLab