From 0336fe56862e09477099875768d1809bfa572e9c Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Tue, 1 Sep 2020 16:18:07 +0200 Subject: [PATCH 1/3] NEW Accountancy - On transfers, select the periodicity by default --- htdocs/accountancy/admin/index.php | 8 ++- htdocs/accountancy/journal/bankjournal.php | 51 ++++++++++++++++--- .../journal/expensereportsjournal.php | 51 ++++++++++++++++--- .../accountancy/journal/purchasesjournal.php | 51 ++++++++++++++++--- htdocs/accountancy/journal/sellsjournal.php | 51 ++++++++++++++++--- htdocs/langs/en_US/accountancy.lang | 1 + htdocs/langs/en_US/other.lang | 2 + 7 files changed, 181 insertions(+), 34 deletions(-) diff --git a/htdocs/accountancy/admin/index.php b/htdocs/accountancy/admin/index.php index 26ea76d2277..2125bfcf8bb 100644 --- a/htdocs/accountancy/admin/index.php +++ b/htdocs/accountancy/admin/index.php @@ -34,7 +34,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; // Load translation files required by the page -$langs->loadLangs(array("compta", "bills", "admin", "accountancy")); +$langs->loadLangs(array("compta", "bills", "admin", "accountancy", "other")); // Security access if (empty($user->rights->accounting->chartofaccount)) @@ -54,6 +54,7 @@ $list = array( $list_binding = array( 'ACCOUNTING_DATE_START_BINDING', + 'ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER' ); /* @@ -354,7 +355,10 @@ foreach ($list_binding as $key) print ''; if ($key == 'ACCOUNTING_DATE_START_BINDING') { print $form->selectDate(($conf->global->$key ? $db->idate($conf->global->$key) : -1), $key, 0, 0, 1); - } else { + } elseif ($key == 'ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER') { + $array=array(0=>$langs->trans("PreviousMonth"), 1=>$langs->trans("CurrentMonth"), 2=>$langs->trans("Fiscalyear")); + print $form->selectarray($key, $array, (isset($conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER)?$conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER:0)); + } else { print ''; } diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php index 29f1cb76d0b..a98be38d67c 100644 --- a/htdocs/accountancy/journal/bankjournal.php +++ b/htdocs/accountancy/journal/bankjournal.php @@ -90,17 +90,52 @@ if ($user->socid > 0 && empty($id_journal)) $error = 0; -$year_current = strftime("%Y", dol_now()); -$pastmonth = strftime("%m", dol_now()) - 1; -$pastmonthyear = $year_current; -if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear--; -} - $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); +// Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) +$periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; +isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; +if($periodbydefaultontransfer == 2) { + $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; + $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; + $sql .= $db->plimit(1); + $res = $db->query($sql); + if ($res->num_rows > 0) { + $fiscalYear = $db->fetch_object($res); + $date_start = strtotime($fiscalYear->date_start); + $date_end = strtotime($fiscalYear->date_end); + } else { + $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); + $year_start = dol_print_date(dol_now(), '%Y'); + $year_end = $year_start + 1; + $month_end = $month_start - 1; + if ($month_end < 1) + { + $month_end = 12; + $year_end--; + } + $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); + $date_end = dol_get_last_day($year_end, $month_end); + } +} elseif ($periodbydefaultontransfer == 1) { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now()); + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } +} else { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now())-1; + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } +} + if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form { $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); diff --git a/htdocs/accountancy/journal/expensereportsjournal.php b/htdocs/accountancy/journal/expensereportsjournal.php index 4a1d2935f0f..7c5fa86231c 100644 --- a/htdocs/accountancy/journal/expensereportsjournal.php +++ b/htdocs/accountancy/journal/expensereportsjournal.php @@ -70,17 +70,52 @@ $accountingjournalstatic->fetch($id_journal); $journal = $accountingjournalstatic->code; $journal_label = $accountingjournalstatic->label; -$year_current = strftime("%Y", dol_now()); -$pastmonth = strftime("%m", dol_now()) - 1; -$pastmonthyear = $year_current; -if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear--; -} - $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); +// Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) +$periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; +isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; +if($periodbydefaultontransfer == 2) { + $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; + $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; + $sql .= $db->plimit(1); + $res = $db->query($sql); + if ($res->num_rows > 0) { + $fiscalYear = $db->fetch_object($res); + $date_start = strtotime($fiscalYear->date_start); + $date_end = strtotime($fiscalYear->date_end); + } else { + $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); + $year_start = dol_print_date(dol_now(), '%Y'); + $year_end = $year_start + 1; + $month_end = $month_start - 1; + if ($month_end < 1) + { + $month_end = 12; + $year_end--; + } + $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); + $date_end = dol_get_last_day($year_end, $month_end); + } +} elseif ($periodbydefaultontransfer == 1) { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now()); + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } +} else { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now())-1; + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } +} + if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form { $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index 4c9e25725f2..279e294f865 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -75,17 +75,52 @@ $accountingjournalstatic->fetch($id_journal); $journal = $accountingjournalstatic->code; $journal_label = $accountingjournalstatic->label; -$year_current = strftime("%Y", dol_now()); -$pastmonth = strftime("%m", dol_now()) - 1; -$pastmonthyear = $year_current; -if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear--; -} - $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); +// Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) +$periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; +isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; +if($periodbydefaultontransfer == 2) { + $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; + $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; + $sql .= $db->plimit(1); + $res = $db->query($sql); + if ($res->num_rows > 0) { + $fiscalYear = $db->fetch_object($res); + $date_start = strtotime($fiscalYear->date_start); + $date_end = strtotime($fiscalYear->date_end); + } else { + $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); + $year_start = dol_print_date(dol_now(), '%Y'); + $year_end = $year_start + 1; + $month_end = $month_start - 1; + if ($month_end < 1) + { + $month_end = 12; + $year_end--; + } + $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); + $date_end = dol_get_last_day($year_end, $month_end); + } +} elseif ($periodbydefaultontransfer == 1) { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now()); + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } +} else { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now())-1; + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } +} + if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form { $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php index 4b66feb9e3d..fe16ad0e4a5 100644 --- a/htdocs/accountancy/journal/sellsjournal.php +++ b/htdocs/accountancy/journal/sellsjournal.php @@ -78,17 +78,52 @@ $accountingjournalstatic->fetch($id_journal); $journal = $accountingjournalstatic->code; $journal_label = $accountingjournalstatic->label; -$year_current = strftime("%Y", dol_now()); -$pastmonth = strftime("%m", dol_now()) - 1; -$pastmonthyear = $year_current; -if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear--; -} - $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); +// Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) +$periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; +isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; +if($periodbydefaultontransfer == 2) { + $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; + $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; + $sql .= $db->plimit(1); + $res = $db->query($sql); + if ($res->num_rows > 0) { + $fiscalYear = $db->fetch_object($res); + $date_start = strtotime($fiscalYear->date_start); + $date_end = strtotime($fiscalYear->date_end); + } else { + $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); + $year_start = dol_print_date(dol_now(), '%Y'); + $year_end = $year_start + 1; + $month_end = $month_start - 1; + if ($month_end < 1) + { + $month_end = 12; + $year_end--; + } + $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); + $date_end = dol_get_last_day($year_end, $month_end); + } +} elseif ($periodbydefaultontransfer == 1) { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now()); + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } +} else { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now())-1; + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } +} + if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form { $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); diff --git a/htdocs/langs/en_US/accountancy.lang b/htdocs/langs/en_US/accountancy.lang index 7b59e33b296..0b032867881 100644 --- a/htdocs/langs/en_US/accountancy.lang +++ b/htdocs/langs/en_US/accountancy.lang @@ -150,6 +150,7 @@ BANK_DISABLE_DIRECT_INPUT=Disable direct recording of transaction in bank accoun ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL=Enable draft export on journal ACCOUNTANCY_COMBO_FOR_AUX=Enable combo list for subsidiary account (may be slow if you have a lot of third parties) ACCOUNTING_DATE_START_BINDING=Define a date to start binding & transfer in accountancy. Below this date, the transactions will not be transferred to accounting. +ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER=On accountancy transfer, select period show by default ACCOUNTING_SELL_JOURNAL=Sell journal ACCOUNTING_PURCHASE_JOURNAL=Purchase journal diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index 54c0572d453..ac0e7be5ccb 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -16,6 +16,8 @@ PreviousMonthOfInvoice=Previous month (number 1-12) of invoice date TextPreviousMonthOfInvoice=Previous month (text) of invoice date NextMonthOfInvoice=Following month (number 1-12) of invoice date TextNextMonthOfInvoice=Following month (text) of invoice date +PreviousMonth=Previous month +CurrentMonth=Current month ZipFileGeneratedInto=Zip file generated into %s. DocFileGeneratedInto=Doc file generated into %s. JumpToLogin=Disconnected. Go to login page... From 028275582a8eb662f7f451c84c2fff5b783e32dc Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Tue, 1 Sep 2020 14:26:16 +0000 Subject: [PATCH 2/3] Fixing style errors. --- htdocs/accountancy/journal/bankjournal.php | 2 +- htdocs/accountancy/journal/expensereportsjournal.php | 2 +- htdocs/accountancy/journal/purchasesjournal.php | 2 +- htdocs/accountancy/journal/sellsjournal.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php index a98be38d67c..0aa223fb05d 100644 --- a/htdocs/accountancy/journal/bankjournal.php +++ b/htdocs/accountancy/journal/bankjournal.php @@ -96,7 +96,7 @@ $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); // Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) $periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; -if($periodbydefaultontransfer == 2) { +if ($periodbydefaultontransfer == 2) { $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; $sql .= $db->plimit(1); diff --git a/htdocs/accountancy/journal/expensereportsjournal.php b/htdocs/accountancy/journal/expensereportsjournal.php index 7c5fa86231c..94f64fbe2bc 100644 --- a/htdocs/accountancy/journal/expensereportsjournal.php +++ b/htdocs/accountancy/journal/expensereportsjournal.php @@ -76,7 +76,7 @@ $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); // Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) $periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; -if($periodbydefaultontransfer == 2) { +if ($periodbydefaultontransfer == 2) { $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; $sql .= $db->plimit(1); diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index 279e294f865..d9de54cc8e5 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -81,7 +81,7 @@ $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); // Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) $periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; -if($periodbydefaultontransfer == 2) { +if ($periodbydefaultontransfer == 2) { $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; $sql .= $db->plimit(1); diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php index fe16ad0e4a5..2b5a31daf64 100644 --- a/htdocs/accountancy/journal/sellsjournal.php +++ b/htdocs/accountancy/journal/sellsjournal.php @@ -84,7 +84,7 @@ $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); // Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) $periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; -if($periodbydefaultontransfer == 2) { +if ($periodbydefaultontransfer == 2) { $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; $sql .= $db->plimit(1); From 8c75f0b95bf3925cf8b80eeac0aab664022d30ad Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Mon, 7 Sep 2020 21:38:39 +0200 Subject: [PATCH 3/3] Code optimize --- htdocs/accountancy/journal/bankjournal.php | 48 ++------------- .../journal/expensereportsjournal.php | 48 ++------------- .../accountancy/journal/purchasesjournal.php | 48 ++------------- htdocs/accountancy/journal/sellsjournal.php | 48 ++------------- htdocs/core/lib/accounting.lib.php | 60 +++++++++++++++++++ 5 files changed, 84 insertions(+), 168 deletions(-) diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php index 0aa223fb05d..12fe8792401 100644 --- a/htdocs/accountancy/journal/bankjournal.php +++ b/htdocs/accountancy/journal/bankjournal.php @@ -93,48 +93,12 @@ $error = 0; $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); -// Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) -$periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; -isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; -if ($periodbydefaultontransfer == 2) { - $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; - $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; - $sql .= $db->plimit(1); - $res = $db->query($sql); - if ($res->num_rows > 0) { - $fiscalYear = $db->fetch_object($res); - $date_start = strtotime($fiscalYear->date_start); - $date_end = strtotime($fiscalYear->date_end); - } else { - $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); - $year_start = dol_print_date(dol_now(), '%Y'); - $year_end = $year_start + 1; - $month_end = $month_start - 1; - if ($month_end < 1) - { - $month_end = 12; - $year_end--; - } - $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); - $date_end = dol_get_last_day($year_end, $month_end); - } -} elseif ($periodbydefaultontransfer == 1) { - $year_current = strftime("%Y", dol_now()); - $pastmonth = strftime("%m", dol_now()); - $pastmonthyear = $year_current; - if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear --; - } -} else { - $year_current = strftime("%Y", dol_now()); - $pastmonth = strftime("%m", dol_now())-1; - $pastmonthyear = $year_current; - if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear --; - } -} +// Period by default on transfer +$dates = getDefaultDatesForTransfer(); +$date_start = $dates['date_start']; +$date_end = $dates['date_end']; +$pastmonthyear = $dates['pastmonthyear']; +$pastmonth = $dates['pastmonth']; if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form { diff --git a/htdocs/accountancy/journal/expensereportsjournal.php b/htdocs/accountancy/journal/expensereportsjournal.php index 94f64fbe2bc..b24f8550e50 100644 --- a/htdocs/accountancy/journal/expensereportsjournal.php +++ b/htdocs/accountancy/journal/expensereportsjournal.php @@ -73,48 +73,12 @@ $journal_label = $accountingjournalstatic->label; $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); -// Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) -$periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; -isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; -if ($periodbydefaultontransfer == 2) { - $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; - $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; - $sql .= $db->plimit(1); - $res = $db->query($sql); - if ($res->num_rows > 0) { - $fiscalYear = $db->fetch_object($res); - $date_start = strtotime($fiscalYear->date_start); - $date_end = strtotime($fiscalYear->date_end); - } else { - $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); - $year_start = dol_print_date(dol_now(), '%Y'); - $year_end = $year_start + 1; - $month_end = $month_start - 1; - if ($month_end < 1) - { - $month_end = 12; - $year_end--; - } - $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); - $date_end = dol_get_last_day($year_end, $month_end); - } -} elseif ($periodbydefaultontransfer == 1) { - $year_current = strftime("%Y", dol_now()); - $pastmonth = strftime("%m", dol_now()); - $pastmonthyear = $year_current; - if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear --; - } -} else { - $year_current = strftime("%Y", dol_now()); - $pastmonth = strftime("%m", dol_now())-1; - $pastmonthyear = $year_current; - if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear --; - } -} +// Period by default on transfer +$dates = getDefaultDatesForTransfer(); +$date_start = $dates['date_start']; +$date_end = $dates['date_end']; +$pastmonthyear = $dates['pastmonthyear']; +$pastmonth = $dates['pastmonth']; if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form { diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index d9de54cc8e5..779c04f68fa 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -78,48 +78,12 @@ $journal_label = $accountingjournalstatic->label; $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); -// Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) -$periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; -isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; -if ($periodbydefaultontransfer == 2) { - $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; - $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; - $sql .= $db->plimit(1); - $res = $db->query($sql); - if ($res->num_rows > 0) { - $fiscalYear = $db->fetch_object($res); - $date_start = strtotime($fiscalYear->date_start); - $date_end = strtotime($fiscalYear->date_end); - } else { - $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); - $year_start = dol_print_date(dol_now(), '%Y'); - $year_end = $year_start + 1; - $month_end = $month_start - 1; - if ($month_end < 1) - { - $month_end = 12; - $year_end--; - } - $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); - $date_end = dol_get_last_day($year_end, $month_end); - } -} elseif ($periodbydefaultontransfer == 1) { - $year_current = strftime("%Y", dol_now()); - $pastmonth = strftime("%m", dol_now()); - $pastmonthyear = $year_current; - if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear --; - } -} else { - $year_current = strftime("%Y", dol_now()); - $pastmonth = strftime("%m", dol_now())-1; - $pastmonthyear = $year_current; - if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear --; - } -} +// Period by default on transfer +$dates = getDefaultDatesForTransfer(); +$date_start = $dates['date_start']; +$date_end = $dates['date_end']; +$pastmonthyear = $dates['pastmonthyear']; +$pastmonth = $dates['pastmonth']; if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form { diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php index 2b5a31daf64..998f34745ac 100644 --- a/htdocs/accountancy/journal/sellsjournal.php +++ b/htdocs/accountancy/journal/sellsjournal.php @@ -81,48 +81,12 @@ $journal_label = $accountingjournalstatic->label; $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); -// Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) -$periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; -isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; -if ($periodbydefaultontransfer == 2) { - $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; - $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; - $sql .= $db->plimit(1); - $res = $db->query($sql); - if ($res->num_rows > 0) { - $fiscalYear = $db->fetch_object($res); - $date_start = strtotime($fiscalYear->date_start); - $date_end = strtotime($fiscalYear->date_end); - } else { - $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); - $year_start = dol_print_date(dol_now(), '%Y'); - $year_end = $year_start + 1; - $month_end = $month_start - 1; - if ($month_end < 1) - { - $month_end = 12; - $year_end--; - } - $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); - $date_end = dol_get_last_day($year_end, $month_end); - } -} elseif ($periodbydefaultontransfer == 1) { - $year_current = strftime("%Y", dol_now()); - $pastmonth = strftime("%m", dol_now()); - $pastmonthyear = $year_current; - if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear --; - } -} else { - $year_current = strftime("%Y", dol_now()); - $pastmonth = strftime("%m", dol_now())-1; - $pastmonthyear = $year_current; - if ($pastmonth == 0) { - $pastmonth = 12; - $pastmonthyear --; - } -} +// Period by default on transfer +$dates = getDefaultDatesForTransfer(); +$date_start = $dates['date_start']; +$date_end = $dates['date_end']; +$pastmonthyear = $dates['pastmonthyear']; +$pastmonth = $dates['pastmonth']; if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form { diff --git a/htdocs/core/lib/accounting.lib.php b/htdocs/core/lib/accounting.lib.php index 16b124443da..c6c0c874cf4 100644 --- a/htdocs/core/lib/accounting.lib.php +++ b/htdocs/core/lib/accounting.lib.php @@ -243,3 +243,63 @@ function journalHead($nom, $variante, $period, $periodlink, $description, $build print "\n\n\n"; } + +/** + * Return Default dates for transfer based on periodicity option in accountancy setup + * + * @return array Dates of periodicity by default + */ +function getDefaultDatesForTransfer() +{ + global $db, $conf; + + // Period by default on transfer (0: previous month | 1: current month | 2: fiscal year) + $periodbydefaultontransfer = $conf->global->ACCOUNTING_DEFAULT_PERIOD_ON_TRANSFER; + isset($periodbydefaultontransfer)?$periodbydefaultontransfer:0; + if ($periodbydefaultontransfer == 2) { + $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear "; + $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'"; + $sql .= $db->plimit(1); + $res = $db->query($sql); + if ($res->num_rows > 0) { + $fiscalYear = $db->fetch_object($res); + $date_start = strtotime($fiscalYear->date_start); + $date_end = strtotime($fiscalYear->date_end); + } else { + $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); + $year_start = dol_print_date(dol_now(), '%Y'); + $year_end = $year_start + 1; + $month_end = $month_start - 1; + if ($month_end < 1) + { + $month_end = 12; + $year_end--; + } + $date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start); + $date_end = dol_get_last_day($year_end, $month_end); + } + } elseif ($periodbydefaultontransfer == 1) { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now()); + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } + } else { + $year_current = strftime("%Y", dol_now()); + $pastmonth = strftime("%m", dol_now())-1; + $pastmonthyear = $year_current; + if ($pastmonth == 0) { + $pastmonth = 12; + $pastmonthyear --; + } + } + + return array( + 'date_start' => $date_start, + 'date_end' => $date_end, + 'pastmonthyear' => $pastmonthyear, + 'pastmonth' => $pastmonth + ); +}