1 (edited by anoopmb 01/07/2019 02:41:14 am)

Topic: New Year reference resetting

hi,
I found one issue that , in India fiscal year end is 31st March of every year, so if I use {YYYY) template on reference, the reference number will reset to start from one on 1st of January.

for eg. 31st December reference was SO201800080 but on January 1st it became SO201900001 which cant be accepted because current fiscal year is not closed and it should be continuation of last reference (SO201800081)  till 31st March.

My ugly hack to resolve this is

1. get current fiscal year begin
2. check context date is in fiscal year and both year is not same
3. if its matching create a new date with previous year and set it as date in reference context

        list($day, $month, $year) = explode_date_to_dmy(begin_fiscalyear());
        list($day1, $month1, $year1) = explode_date_to_dmy($context['date']);

    if(is_date_in_fiscalyear($context['date'])&&$year!=$year1){
            $mdate=sql2date($year."-".$month1."-".$day1);
            $context['date']=$mdate;
        }

added above code in reference.inc line number 99

Please suggest me if there is better way to resolve this

ANOOP
Experience is the name everyone gives to their mistakes!

Re: New Year reference resetting

Your includes/references.inc line number 99 is for FA 2.3.x.
Lines 121, 122 are for FA 2.4.x.

Just take the Year of the fiscal year beginning only!

Besides, this will all go wrong if the template is created with all the date elements for when there are too many transactions each day and a template like {DD}{MM}{YYYY} is used! A separate template like {Y} may be used for this kind of resetting of the year each fiscal beginning.

There is a function get_fiscalyear_begin_for_date($date) that obtains the financial year beginning in the file admin/db/fiscalyears_db.inc. Also available in it is get_current_fiscalyear(). These functions return the MySQL date format whilst taking the user date format as input where needed.

There is another function begin_fiscalyear() that gets the current fiscal year's begin date in user date format in includes/date_functions.inc.

Re: New Year reference resetting

@apmuthu

Thanks and its very easy to do by calling begin fiscal year method

ANOOP
Experience is the name everyone gives to their mistakes!

Re: New Year reference resetting

@joe: This will be desirable to include in the core - {Y}.

Re: New Year reference resetting

I will have Janusz to look at this.

Joe

Re: New Year reference resetting

By the way the wiki is not updated with these patterns for Transaction References. How to use it?

www.boxygen.pk

Re: New Year reference resetting

This Wiki page points to this thread.

@joe: Along with {Y}, try to include {YYY} to indicate both years if they lie in different calendar years like /2018-19.

Re: New Year reference resetting

i have made some changes to my code for now
expecting new format will come shortly

ANOOP
Experience is the name everyone gives to their mistakes!

Re: New Year reference resetting

@anoopmb: kindly submit your tested code changes for peer review and inclusion in the core for both {Y} and {YYY}.

10 (edited by anoopmb 01/07/2019 04:50:44 am)

Re: New Year reference resetting

@apmuthu

you  can see my changes on my fork over

here

update is on 2.4.6 /includes/references.inc file

1. line no 55 updated the placeholder array to

$refline_placeholders = array(
    'MM' => 'date',
    'Y' => 'date',
    'YY' => 'date',
    'YYY' => 'date',
    'YYYY' => 'date',
    'UU' => 'user',
    'P' => 'pos',
//     FIXME:  for placeholders below all the code should work, but as the ref length is variable,
//       length specification in placeholder format should be implemented.
//    'C' => 'customer',
//    'B' => 'branch',
//    'S' => 'supplier',
//    'L' => 'location'
);

2. added new template in switch case line no 124

                        case 'Y':
                        case 'YYY':
                            list($begin_day, $begin_month, $begin_year) = explode_date_to_dmy(begin_fiscalyear());
                            list($end_day, $end_month, $end_year) = explode_date_to_dmy(end_fiscalyear());
                            $out .= $ph == 'Y' ? sprintf('%04d', $begin_year) : sprintf('%04d', $begin_year) . "-" . sprintf('%02d', $end_year % 100);
                            break;

3. updated is_valid function line no 266


function is_valid($reference, $type, $context=null, $line=null)
    {
         if (!isset($line))
             $line = $this->reflines->find_refline_id($reference, $type, true);

         if (!isset($line))
             return false;

        $refline = $this->reflines->get($line);

        if ($this->_legacy_line($refline))    //legacy non-templated line
            return strlen(trim($reference)) > 0;

        $regex = preg_quote($refline['prefix'].$refline['pattern']);
        if (!is_array($context))
            $context = array('date'=>$context);

         $context['pos'] = $_SESSION["wa_current_user"]->pos;

        if (is_date(@$context['date']))
        {
            list($year4, $month, $day) = explode("-", date2sql($context['date']));
            list($begin_year, $begin_month, $begin_day) = explode("-", date2sql(begin_fiscalyear()));
            list($end_year, $end_month, $end_day) = explode("-", date2sql(end_fiscalyear()));
            $year2 = substr($year4, 2);
            $year3 = $begin_year."-".substr($end_year, 2);
        } else
        {
            $month = '\d{2,}';
            $year2 = '\d{2,}';
            $year4 = '\d{4,}';
            $begin_year = '\d{4,}';
            $year3 = '\d{4,}-\d{2,}';
        }
        $cust = @$context['customer'] ? $context['customer'] : '\d+';
        $supp = @$context['supplier'] ? $context['supplier'] : '\d+';
        $branch = @$context['branch'] ? $context['branch'] : '\d+';
        $location = @$context['location'] ? $context['location'] : '[a-z0-9]+';
         $pos = @$context['pos'] ? $context['pos'] : '\d+';
        $user = sprintf("%02d", $_SESSION['wa_current_user']->user);

        $regex = preg_replace(
            array(
                '/\\\{/',    // unquote placeholders
                '/\\\}/',
                '/\{MM\}/',
                '/\{Y\}/',
                '/\{YY\}/',
                '/\{YYY\}/',
                '/\{YYYY\}/',
                '/\{C\}/',
                '/\{B\}/',
                '/\{S\}/',
                '/\{L\}/',
                '/\{UU\}/',
                 '/\{P\}/',
                '/\{\d+}/',
            ),
            array(
                '{',
                '}',
                $month,
                $begin_year,
                $year2,
                $year3,
                $year4,
                $cust,
                $branch,
                $supp,
                $location,
                $user,
                 $pos,
                '\d+',
            ), $regex);

        $regex = '"^'.$regex.'"i';

        return preg_match($regex, $reference, $match) ? 1 : 0;
    }
ANOOP
Experience is the name everyone gives to their mistakes!

Re: New Year reference resetting

@joe: This can be included. Commit Diff.

Post's attachments

references_inc.zip 4.6 kb, 2 downloads since 2019-01-08 

You don't have the permssions to download the attachments of this post.

Re: New Year reference resetting

I have added fix solving this problem to frontaccounting code base.
The number templates for short and long form of fiscal year number is {FF} and {FFFF} respectively, which seems to be more intuitive. Thank you for the idea. This will be available with next minor release.
Janusz

Re: New Year reference resetting

Then we can also have the fiscal year end date as {EE} and {EEEE} as well so that we can have ###/2018-19 if we use {NNN}/{FFFF}-{EE}.

Re: New Year reference resetting

Thanks Janusz and apmuthu

ANOOP
Experience is the name everyone gives to their mistakes!

Re: New Year reference resetting

For those who want the Fiscal Year Ending {EE}/{EEEE} format as well, it is available in my commit.