Fixes issue 22365: Implement 13th period
authorEduardo Argal Guibert <eduardo.argal@openbravo.com>
Wed, 28 Nov 2012 16:26:43 +0100
changeset 18775 8e775c18f204
parent 18774 8f079938b803
child 18776 dccb1d65d4fb
child 18949 c7791db2bf9c
Fixes issue 22365: Implement 13th period
modules/org.openbravo.advpaymentmngt/build/classes/org/openbravo/advpaymentmngt/modulescript/CreateAccountingConfigurationData.class
modules/org.openbravo.advpaymentmngt/src-util/modulescript/src/org/openbravo/advpaymentmngt/modulescript/CreateAccountingConfiguration_data.xsql
modules/org.openbravo.advpaymentmngt/src-util/modulescript/src/src/org/openbravo/advpaymentmngt/modulescript/CreateAccountingConfigurationData.java
src-db/database/model/functions/C_CHK_OPEN_PERIOD_GLJ.xml
src-db/database/model/functions/C_GET_DEFAULT_PERIOD.xml
src-db/database/model/functions/C_PERIOD_PROCESS.xml
src-db/database/model/functions/C_YEARPERIODS.xml
src-db/database/model/functions/FACT_ACCT_RESET.xml
src-db/database/model/functions/GL_JOURNAL_POST.xml
src-db/database/model/triggers/C_PERIOD_TRG.xml
src-db/database/model/triggers/C_PERIOD_TRG3.xml
src-db/database/sourcedata/AD_COLUMN.xml
src-db/database/sourcedata/AD_ELEMENT.xml
src-db/database/sourcedata/AD_FIELD.xml
src-db/database/sourcedata/AD_MESSAGE.xml
src-db/database/sourcedata/AD_PROCESS.xml
src-db/database/sourcedata/AD_PROCESS_PARA.xml
src-db/database/sourcedata/AD_REF_TABLE.xml
src-db/database/sourcedata/AD_VAL_RULE.xml
src/org/openbravo/erpCommon/ad_callouts/SL_Journal_Period.java
src/org/openbravo/erpCommon/ad_forms/DocGLJournal.java
src/org/openbravo/erpCommon/ad_forms/DocGLJournal_data.xsql
src/org/openbravo/erpCommon/ad_forms/DocInvoice.java
src/org/openbravo/erpCommon/ad_forms/FactLine.java
src/org/openbravo/erpCommon/utility/AccDefUtility.java
Binary file modules/org.openbravo.advpaymentmngt/build/classes/org/openbravo/advpaymentmngt/modulescript/CreateAccountingConfigurationData.class has changed
--- a/modules/org.openbravo.advpaymentmngt/src-util/modulescript/src/org/openbravo/advpaymentmngt/modulescript/CreateAccountingConfiguration_data.xsql	Wed Nov 28 18:39:23 2012 +0530
+++ b/modules/org.openbravo.advpaymentmngt/src-util/modulescript/src/org/openbravo/advpaymentmngt/modulescript/CreateAccountingConfiguration_data.xsql	Wed Nov 28 16:26:43 2012 +0100
@@ -63,6 +63,7 @@
       where ad_reference_id = '183'
       and ad_ref_list.isactive='Y'
       and c_period.c_year_id = c_year.c_year_id
+      and c_period.periodtype <> 'A'
       and c_calendar_id = (select c_calendar_id from ad_org
             where ad_org_id = ad_org_getcalendarowner(a.ad_org_id))
       and not exists (select 1 from c_periodcontrol
--- a/modules/org.openbravo.advpaymentmngt/src-util/modulescript/src/src/org/openbravo/advpaymentmngt/modulescript/CreateAccountingConfigurationData.java	Wed Nov 28 18:39:23 2012 +0530
+++ b/modules/org.openbravo.advpaymentmngt/src-util/modulescript/src/src/org/openbravo/advpaymentmngt/modulescript/CreateAccountingConfigurationData.java	Wed Nov 28 16:26:43 2012 +0100
@@ -30,23 +30,23 @@
   }
 
   public String getField(String fieldName) {
-    if (fieldName.equalsIgnoreCase("C_ACCTSCHEMA_ID") || fieldName.equals("cAcctschemaId"))
+    if (fieldName.equalsIgnoreCase("c_acctschema_id") || fieldName.equals("cAcctschemaId"))
       return cAcctschemaId;
-    else if (fieldName.equalsIgnoreCase("AD_CLIENT_ID") || fieldName.equals("adClientId"))
+    else if (fieldName.equalsIgnoreCase("ad_client_id") || fieldName.equals("adClientId"))
       return adClientId;
-    else if (fieldName.equalsIgnoreCase("AD_ORG_ID") || fieldName.equals("adOrgId"))
+    else if (fieldName.equalsIgnoreCase("ad_org_id") || fieldName.equals("adOrgId"))
       return adOrgId;
-    else if (fieldName.equalsIgnoreCase("AD_TABLE_ID") || fieldName.equals("adTableId"))
+    else if (fieldName.equalsIgnoreCase("ad_table_id") || fieldName.equals("adTableId"))
       return adTableId;
-    else if (fieldName.equalsIgnoreCase("NAME"))
+    else if (fieldName.equalsIgnoreCase("name"))
       return name;
-    else if (fieldName.equalsIgnoreCase("C_PERIOD_ID") || fieldName.equals("cPeriodId"))
+    else if (fieldName.equalsIgnoreCase("c_period_id") || fieldName.equals("cPeriodId"))
       return cPeriodId;
-    else if (fieldName.equalsIgnoreCase("VALUE"))
+    else if (fieldName.equalsIgnoreCase("value"))
       return value;
-    else if (fieldName.equalsIgnoreCase("STATUS"))
+    else if (fieldName.equalsIgnoreCase("status"))
       return status;
-    else if (fieldName.equalsIgnoreCase("ISDEFAULTACCT"))
+    else if (fieldName.equalsIgnoreCase("isdefaultacct"))
       return isdefaultacct;
    else {
      log4j.debug("Field does not exist: " + fieldName);
@@ -83,15 +83,15 @@
       while(continueResult && result.next()) {
         countRecord++;
         CreateAccountingConfigurationData objectCreateAccountingConfigurationData = new CreateAccountingConfigurationData();
-        objectCreateAccountingConfigurationData.cAcctschemaId = UtilSql.getValue(result, "C_ACCTSCHEMA_ID");
-        objectCreateAccountingConfigurationData.adClientId = UtilSql.getValue(result, "AD_CLIENT_ID");
-        objectCreateAccountingConfigurationData.adOrgId = UtilSql.getValue(result, "AD_ORG_ID");
-        objectCreateAccountingConfigurationData.adTableId = UtilSql.getValue(result, "AD_TABLE_ID");
-        objectCreateAccountingConfigurationData.name = UtilSql.getValue(result, "NAME");
-        objectCreateAccountingConfigurationData.cPeriodId = UtilSql.getValue(result, "C_PERIOD_ID");
-        objectCreateAccountingConfigurationData.value = UtilSql.getValue(result, "VALUE");
-        objectCreateAccountingConfigurationData.status = UtilSql.getValue(result, "STATUS");
-        objectCreateAccountingConfigurationData.isdefaultacct = UtilSql.getValue(result, "ISDEFAULTACCT");
+        objectCreateAccountingConfigurationData.cAcctschemaId = UtilSql.getValue(result, "c_acctschema_id");
+        objectCreateAccountingConfigurationData.adClientId = UtilSql.getValue(result, "ad_client_id");
+        objectCreateAccountingConfigurationData.adOrgId = UtilSql.getValue(result, "ad_org_id");
+        objectCreateAccountingConfigurationData.adTableId = UtilSql.getValue(result, "ad_table_id");
+        objectCreateAccountingConfigurationData.name = UtilSql.getValue(result, "name");
+        objectCreateAccountingConfigurationData.cPeriodId = UtilSql.getValue(result, "c_period_id");
+        objectCreateAccountingConfigurationData.value = UtilSql.getValue(result, "value");
+        objectCreateAccountingConfigurationData.status = UtilSql.getValue(result, "status");
+        objectCreateAccountingConfigurationData.isdefaultacct = UtilSql.getValue(result, "isdefaultacct");
         objectCreateAccountingConfigurationData.InitRecordNumber = Integer.toString(firstRegister);
         vector.addElement(objectCreateAccountingConfigurationData);
         if (countRecord >= numberRegisters && numberRegisters != 0) {
@@ -136,7 +136,7 @@
 
       result = st.executeQuery();
       if(result.next()) {
-        boolReturn = !UtilSql.getValue(result, "NAME").equals("0");
+        boolReturn = !UtilSql.getValue(result, "name").equals("0");
       }
       result.close();
     } catch(SQLException e){
@@ -207,6 +207,7 @@
       "      where ad_reference_id = '183'" +
       "      and ad_ref_list.isactive='Y'" +
       "      and c_period.c_year_id = c_year.c_year_id" +
+      "      and c_period.periodtype <> 'A'" +
       "      and c_calendar_id = (select c_calendar_id from ad_org" +
       "            where ad_org_id = ad_org_getcalendarowner(a.ad_org_id))" +
       "      and not exists (select 1 from c_periodcontrol" +
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_CHK_OPEN_PERIOD_GLJ.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -0,0 +1,53 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_CHK_OPEN_PERIOD_GLJ">
+    <function name="C_CHK_OPEN_PERIOD_GLJ" type="NUMERIC">
+      <parameter name="p_orgid" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_period" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <body><![CDATA[/*************************************************************************
+* The contents of this file are subject to the Openbravo  Public  License
+* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+* Version 1.1  with a permitted attribution clause; you may not  use this
+* file except in compliance with the License. You  may  obtain  a copy of
+* the License at http://www.openbravo.com/legal/license.html
+* Software distributed under the License  is  distributed  on  an "AS IS"
+* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+* License for the specific  language  governing  rights  and  limitations
+* under the License.
+* The Original Code is Openbravo ERP.
+* The Initial Developer of the Original Code is Openbravo SLU
+* All portions are Copyright (C) 2012 Openbravo SLU
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+/*************************************************************************
+* Returns 1 if the period is opened for the selected organization, period
+************************************************************************/
+  v_available_period NUMBER:=0;
+BEGIN
+  -- Check the period control is opened
+  
+  IF (p_period IS NOT NULL) THEN
+    SELECT COUNT(C_Period_ID)
+    INTO v_available_period
+    FROM C_Period
+    WHERE c_period_id = p_period
+    AND EXISTS (SELECT 1
+                FROM C_PeriodControl
+                WHERE C_PeriodControl.C_Period_ID=C_Period.C_Period_ID
+                AND DocBaseType= 'GLJ'
+                AND AD_Org_ID=AD_ORG_GETPERIODCONTROLALLOW(p_orgid)
+                AND PeriodStatus='O'
+                );
+  ELSE 
+    v_available_period:=0;
+  END IF;
+   
+  RETURN v_available_period;
+END C_CHK_OPEN_PERIOD_GLJ
+]]></body>
+    </function>
+  </database>
--- a/src-db/database/model/functions/C_GET_DEFAULT_PERIOD.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/model/functions/C_GET_DEFAULT_PERIOD.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -50,6 +50,7 @@
 		FROM C_PERIOD, C_YEAR
 		WHERE C_PERIOD.C_YEAR_ID = C_YEAR.C_YEAR_ID
 		AND C_CALENDAR_ID = v_calendar_id
+		AND PERIODTYPE <> 'A'
 		AND STARTDATE<=trunc(v_dateAcct)
 		AND ENDDATE>=trunc(v_dateAcct);
 
@@ -62,7 +63,8 @@
 				FROM C_PERIOD, C_YEAR
 				WHERE C_PERIOD.C_YEAR_ID = C_YEAR.C_YEAR_ID
 				AND C_CALENDAR_ID = v_calendar_id
-				AND STARTDATE<=(trunc(v_endDate) +1)
+		    AND PERIODTYPE <> 'A'				
+		    AND STARTDATE<=(trunc(v_endDate) +1)
 				AND ENDDATE>=(trunc(v_endDate) +1);
 		
 		    RETURN v_period_id;
--- a/src-db/database/model/functions/C_PERIOD_PROCESS.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/model/functions/C_PERIOD_PROCESS.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -112,7 +112,7 @@
               WHERE C_PERIODCONTROL.C_PERIOD_ID=C_PERIOD.C_PERIOD_ID 
               AND C_PERIOD.C_Year_ID=p_Year
               AND C_PERIODCONTROL.AD_Org_ID=p_Organization
-              AND C_PERIOD.EndDate<=(SELECT EndDate FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
+              AND C_PERIOD.periodno<=(SELECT periodno FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
               AND C_PERIODCONTROL.DocBaseType LIKE COALESCE(p_DocBaseType, '%')
             )
           LOOP
@@ -132,7 +132,7 @@
               AND C_PERIODCONTROL.AD_Org_ID IN (SELECT AD_Org_ID
                                                 FROM AD_Org 
                                                 WHERE AD_ISORGINCLUDED(ad_org.ad_org_id, p_Organization, ad_org.ad_client_id)<>-1)
-              AND C_PERIOD.EndDate<=(SELECT EndDate FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
+              AND C_PERIOD.periodno<=(SELECT periodno FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
               AND C_PERIODCONTROL.DocBaseType LIKE COALESCE(p_DocBaseType, '%')
             )
           LOOP
@@ -169,7 +169,7 @@
               AND C_PERIODCONTROL.AD_Org_ID IN (SELECT AD_Org_ID
                                                 FROM AD_Org 
                                                 WHERE AD_ISORGINCLUDED(ad_org.ad_org_id, p_Organization, ad_org.ad_client_id)<>-1)
-              AND C_PERIOD.EndDate<=(SELECT EndDate FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
+              AND C_PERIOD.periodno<=(SELECT periodno FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
               AND C_PERIODCONTROL.DocBaseType LIKE COALESCE(p_DocBaseType, '%')
             )
           LOOP
@@ -198,7 +198,7 @@
               WHERE C_PERIODCONTROL.C_PERIOD_ID=C_PERIOD.C_PERIOD_ID 
               AND C_PERIOD.C_Year_ID=p_Year
               AND C_PERIODCONTROL.AD_Org_ID=p_Organization
-              AND C_PERIOD.EndDate<=(SELECT EndDate FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
+              AND C_PERIOD.periodno<=(SELECT periodno FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
               AND C_PERIODCONTROL.DocBaseType LIKE COALESCE(p_DocBaseType, '%')
             )
           LOOP
@@ -221,7 +221,7 @@
               AND C_PERIODCONTROL.AD_Org_ID IN (SELECT AD_Org_ID
                                                 FROM AD_Org 
                                                 WHERE AD_ISORGINCLUDED(ad_org.ad_org_id, p_Organization, ad_org.ad_client_id)<>-1)
-              AND C_PERIOD.EndDate<=(SELECT EndDate FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
+              AND C_PERIOD.periodno<=(SELECT periodno FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
               AND C_PERIODCONTROL.DocBaseType LIKE COALESCE(p_DocBaseType, '%')
             )
           LOOP
@@ -235,7 +235,7 @@
               WHERE C_PERIODCONTROL.C_PERIOD_ID=C_PERIOD.C_PERIOD_ID 
               AND C_PERIOD.C_Year_ID=p_Year
               AND C_PERIODCONTROL.AD_Org_ID=p_Organization
-              AND C_PERIOD.EndDate<=(SELECT EndDate FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
+              AND C_PERIOD.periodno<=(SELECT periodno FROM C_PERIOD WHERE C_Period.C_Period_ID=p_PeriodNO)
               AND C_PERIODCONTROL.DocBaseType LIKE COALESCE(p_DocBaseType, '%')
             )
           LOOP
--- a/src-db/database/model/functions/C_YEARPERIODS.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/model/functions/C_YEARPERIODS.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -19,7 +19,7 @@
   * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
   * All Rights Reserved.
   * Contributor(s): Openbravo SLU
-  * Contributions are Copyright (C) 2001-2009 Openbravo, S.L.U.
+  * Contributions are Copyright (C) 2001-2012 Openbravo, S.L.U.
   *
   * Specifically, this derivative work is based upon the following Compiere
   * file and version.
@@ -47,6 +47,7 @@
     v_User_ID VARCHAR2(32);
     v_year_num NUMBER;
     v_language VARCHAR2(6);
+    v_createAdjustment VARCHAR(1):='N';
   BEGIN
     --  Update AD_PInstance
     --  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing');
@@ -71,6 +72,8 @@
       IF Cur_Parameter.ParameterName = 'AD_LANGUAGE_ID' THEN
         SELECT AD_LANGUAGE INTO v_language FROM AD_LANGUAGE WHERE AD_LANGUAGE_ID = Cur_Parameter.P_String;
         v_language := COALESCE(v_language, 'en_US');
+      ELSIF Cur_Parameter.ParameterName = 'CREATEADJUSTMENT' THEN 
+        v_createAdjustment := Cur_Parameter.P_String;
       END IF;
       v_Year_ID:=Cur_Parameter.Record_ID;
     END LOOP; -- Get Parameter
@@ -145,6 +148,24 @@
         DBMS_OUTPUT.PUT_LINE('Month Added') ;
       END IF;
     END LOOP;
+    IF(v_createAdjustment = 'Y') THEN 
+			AD_Sequence_Next('C_Period', v_Year_ID, v_NextNo) ;
+			INSERT
+			INTO C_Period
+				(
+				C_Period_ID, AD_Client_ID, AD_Org_ID, IsActive,
+				Created, CreatedBy, Updated, UpdatedBy,
+				C_Year_ID, PeriodNo, StartDate, PeriodType,
+				Name
+				)
+			VALUES
+			(
+				v_NextNo, v_Client_ID, v_Org_ID, 'Y',
+				now(), v_User_ID, now(), v_User_ID,
+				v_Year_ID, 13, TO_DATE(ADD_MONTHS(v_StartDate, 12))-1, 'A',
+				ad_message_get2('13thPeriod', v_language) || ' - ' || SUBSTR(v_Year_Str,3,2)
+			);
+    END IF;
     --  Update AD_PInstance
     --<<END_PROCEDURE>>
     --  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished');
--- a/src-db/database/model/functions/FACT_ACCT_RESET.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/model/functions/FACT_ACCT_RESET.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -135,13 +135,17 @@
             Cur_Fact_Records RECORD;
           BEGIN
             FOR Cur_Fact_Records IN
-              (SELECT DATEACCT, DOCBASETYPE, C_DOCTYPE_ID
+              (SELECT DATEACCT, DOCBASETYPE, C_DOCTYPE_ID, C_PERIOD_ID
                FROM FACT_ACCT
                WHERE AD_TABLE_ID=v_AD_Table_ID
                AND Record_ID = v_Record_ID
               )
             LOOP
-              v_available_period:=C_CHK_OPEN_PERIOD(v_AD_Org_ID, Cur_Fact_Records.DATEACCT, Cur_Fact_Records.DOCBASETYPE, Cur_Fact_Records.C_DOCTYPE_ID);
+              IF(Cur_Fact_Records.DOCBASETYPE = 'GLJ') THEN 
+								v_available_period:=C_CHK_OPEN_PERIOD_GLJ(v_AD_Org_ID, Cur_Fact_Records.C_PERIOD_ID);
+              ELSE
+              	v_available_period:=C_CHK_OPEN_PERIOD(v_AD_Org_ID, Cur_Fact_Records.DATEACCT, Cur_Fact_Records.DOCBASETYPE, Cur_Fact_Records.C_DOCTYPE_ID);
+              END IF;
               IF (v_available_period<>1) THEN
                 RAISE_APPLICATION_ERROR(-20000, '@PeriodClosedForUnPosting@');
               END IF;
@@ -255,8 +259,8 @@
                       AND AD_Org.ad_client_id=fa_aux.AD_CLIENT_ID
                       AND C_PERIODCONTROL.docbasetype=fa_aux.DOCBASETYPE
                       AND C_PERIODCONTROL.periodstatus<>'O'
-                      AND fa_aux.DATEACCT>=C_PERIOD.StartDate
-                      AND fa_aux.DATEACCT<C_PERIOD.EndDate + 1
+                      AND ((fa_aux.DATEACCT>=C_PERIOD.StartDate AND fa_aux.DATEACCT<C_PERIOD.EndDate + 1 AND fa_aux.DOCBASETYPE <> 'GLJ') 
+                      		OR (fa_aux.DOCBASETYPE = 'GLJ' AND C_PERIOD.C_PERIOD_ID = FACT_ACCT.C_PERIOD_ID))
                       AND C_PeriodControl.AD_Org_ID=AD_Org.AD_Org_ID
                       AND C_PeriodControl.AD_Org_ID=v_AD_Org_CalOwner
                       AND fa_aux.record_id = FACT_ACCT.RECORD_ID
--- a/src-db/database/model/functions/GL_JOURNAL_POST.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/model/functions/GL_JOURNAL_POST.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -40,6 +40,7 @@
   v_Record_ID VARCHAR2(32);
   v_AD_User_ID VARCHAR2(32);
   v_AD_Org_ID VARCHAR2(32);
+  v_C_Period_ID VARCHAR2(32);
   v_DateAcct DATE;
   v_DocType_ID VARCHAR2(32);
   v_BatchOrg_ID VARCHAR2(32);
@@ -104,11 +105,13 @@
     SELECT DocStatus,
       DocAction,
       Processing,
-      Posted, AD_Org_ID, DateAcct, C_DocType_ID
+      Posted, AD_Org_ID, DateAcct, C_DocType_ID,
+      C_Period_ID
     INTO v_DocStatus,
       v_DocAction,
       v_Processing,
-      v_Posted, v_AD_Org_ID, v_DateAcct, v_DocType_ID
+      v_Posted, v_AD_Org_ID, v_DateAcct, v_DocType_ID,
+      v_C_Period_ID
     FROM GL_Journal
     WHERE GL_Journal_ID=v_Record_ID  FOR UPDATE;
     /**
@@ -267,7 +270,7 @@
       AND AD_Org.AD_Org_ID=v_org_bule_id;
       
       IF (v_isacctle='Y') THEN     	
-        SELECT C_CHK_OPEN_PERIOD(v_AD_Org_ID, v_DateAcct, NULL, v_DocType_ID) 
+        SELECT C_CHK_OPEN_PERIOD_GLJ(v_AD_Org_ID, v_C_Period_ID) 
         INTO v_available_period
         FROM DUAL;
         
--- a/src-db/database/model/triggers/C_PERIOD_TRG.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/model/triggers/C_PERIOD_TRG.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -48,6 +48,7 @@
                                                   and ad_org.ad_org_id = ad_org_getcalendarowner(o.AD_ORG_ID))
                           ) a
        WHERE AD_Reference_ID='183'
+       AND VALUE = (CASE WHEN :new.PeriodType = 'S' THEN VALUE ELSE 'GLJ' END)
        ORDER BY 1)
     LOOP
       AD_Sequence_Next('C_PeriodControl', :new.AD_Client_ID, v_NextNo) ;
--- a/src-db/database/model/triggers/C_PERIOD_TRG3.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/model/triggers/C_PERIOD_TRG3.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -33,14 +33,20 @@
       Where C_Period_Id In (Select Distinct(Cp.C_Period_Id)
       From C_Period Cp 
             Join C_Periodcontrol Cpc On Cpc.C_Period_Id= Cp.C_Period_Id 
-      Where Periodstatus <> 'P'))
+      Where Periodstatus <> 'P'
+      and periodtype = 'S'
+      )
+       )
     Loop
-       Select Count(*) 
+       Select Count(*)
        Into V_COUNT
        From C_Period 
-       Where C_Period_Id <> Cur_Period.C_Period_Id
-       And Ad_Org_Id=Cur_Period.Ad_Org_Id
-       And Ad_Client_Id=Cur_Period.Ad_Client_Id
+       Where  exists (select 1 from c_year y1, c_year y2
+       								where y1.c_year_id = c_period.c_year_id
+       								and y1.c_calendar_id = y2.c_calendar_id
+       								and y2.c_year_id = Cur_Period.C_Year_Id
+       								and c_period.c_period_id <> Cur_Period.c_period_id)
+       and periodtype = 'S'
        And Cur_Period.startdate <= Enddate
        And Cur_Period.Enddate >= Startdate;
        If (V_COUNT > 0) Then
--- a/src-db/database/sourcedata/AD_COLUMN.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/sourcedata/AD_COLUMN.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -13606,7 +13606,7 @@
 <!--839-->  <AD_TABLE_ID><![CDATA[145]]></AD_TABLE_ID>
 <!--839-->  <AD_REFERENCE_ID><![CDATA[19]]></AD_REFERENCE_ID>
 <!--839-->  <FIELDLENGTH><![CDATA[22]]></FIELDLENGTH>
-<!--839-->  <DEFAULTVALUE><![CDATA[0]]></DEFAULTVALUE>
+<!--839-->  <DEFAULTVALUE><![CDATA[@AD_Org_ID@]]></DEFAULTVALUE>
 <!--839-->  <ISKEY><![CDATA[N]]></ISKEY>
 <!--839-->  <ISPARENT><![CDATA[N]]></ISPARENT>
 <!--839-->  <ISMANDATORY><![CDATA[Y]]></ISMANDATORY>
@@ -22729,7 +22729,7 @@
 <!--1634-->  <AD_TABLE_ID><![CDATA[224]]></AD_TABLE_ID>
 <!--1634-->  <AD_REFERENCE_ID><![CDATA[15]]></AD_REFERENCE_ID>
 <!--1634-->  <FIELDLENGTH><![CDATA[10]]></FIELDLENGTH>
-<!--1634-->  <DEFAULTVALUE><![CDATA[@DateDoc@]]></DEFAULTVALUE>
+<!--1634-->  <DEFAULTVALUE><![CDATA[@SQL=SELECT COALESCE((SELECT ENDDATE FROM C_PERIOD WHERE C_PERIOD_ID = @C_PERIOD_ID@),to_date(@DateDoc@)) FROM DUAL]]></DEFAULTVALUE>
 <!--1634-->  <ISKEY><![CDATA[N]]></ISKEY>
 <!--1634-->  <ISPARENT><![CDATA[N]]></ISPARENT>
 <!--1634-->  <ISMANDATORY><![CDATA[Y]]></ISMANDATORY>
@@ -22767,7 +22767,7 @@
 <!--1635-->  <AD_TABLE_ID><![CDATA[224]]></AD_TABLE_ID>
 <!--1635-->  <AD_REFERENCE_ID><![CDATA[15]]></AD_REFERENCE_ID>
 <!--1635-->  <FIELDLENGTH><![CDATA[10]]></FIELDLENGTH>
-<!--1635-->  <DEFAULTVALUE><![CDATA[@DateAcct@]]></DEFAULTVALUE>
+<!--1635-->  <DEFAULTVALUE><![CDATA[@SQL=SELECT COALESCE((SELECT ENDDATE FROM C_PERIOD WHERE C_PERIOD_ID = @C_PERIOD_ID@),to_date(@DateAcct@)) FROM DUAL]]></DEFAULTVALUE>
 <!--1635-->  <ISKEY><![CDATA[N]]></ISKEY>
 <!--1635-->  <ISPARENT><![CDATA[N]]></ISPARENT>
 <!--1635-->  <ISMANDATORY><![CDATA[Y]]></ISMANDATORY>
--- a/src-db/database/sourcedata/AD_ELEMENT.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/sourcedata/AD_ELEMENT.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -21665,6 +21665,20 @@
 <!--40D3360F2BB046D3B499A86D2CB78200-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
 <!--40D3360F2BB046D3B499A86D2CB78200--></AD_ELEMENT>
 
+<!--4275E22E56264665A5036996A8045C8D--><AD_ELEMENT>
+<!--4275E22E56264665A5036996A8045C8D-->  <AD_ELEMENT_ID><![CDATA[4275E22E56264665A5036996A8045C8D]]></AD_ELEMENT_ID>
+<!--4275E22E56264665A5036996A8045C8D-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--4275E22E56264665A5036996A8045C8D-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--4275E22E56264665A5036996A8045C8D-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--4275E22E56264665A5036996A8045C8D-->  <COLUMNNAME><![CDATA[CREATEADJUSTMENT]]></COLUMNNAME>
+<!--4275E22E56264665A5036996A8045C8D-->  <NAME><![CDATA[Create Adjustment Period]]></NAME>
+<!--4275E22E56264665A5036996A8045C8D-->  <PRINTNAME><![CDATA[Create Adjustment Period]]></PRINTNAME>
+<!--4275E22E56264665A5036996A8045C8D-->  <DESCRIPTION><![CDATA[Add 13th period for adjustment entries]]></DESCRIPTION>
+<!--4275E22E56264665A5036996A8045C8D-->  <HELP><![CDATA[When flagged process creates 13th period to include manual accounting entries for adjustment purposes.]]></HELP>
+<!--4275E22E56264665A5036996A8045C8D-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--4275E22E56264665A5036996A8045C8D-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
+<!--4275E22E56264665A5036996A8045C8D--></AD_ELEMENT>
+
 <!--42B6380737BC4FB681E7A2F3FE1CE952--><AD_ELEMENT>
 <!--42B6380737BC4FB681E7A2F3FE1CE952-->  <AD_ELEMENT_ID><![CDATA[42B6380737BC4FB681E7A2F3FE1CE952]]></AD_ELEMENT_ID>
 <!--42B6380737BC4FB681E7A2F3FE1CE952-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_FIELD.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/sourcedata/AD_FIELD.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -16378,8 +16378,8 @@
 <!--1271-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--1271-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--1271-->  <NAME><![CDATA[Create Periods]]></NAME>
-<!--1271-->  <DESCRIPTION><![CDATA[Create 12 standard calendar periods (Jan-Dec)]]></DESCRIPTION>
-<!--1271-->  <HELP><![CDATA[Create 12 standard calendar periods (Jan-Dec)]]></HELP>
+<!--1271-->  <DESCRIPTION><![CDATA[Create 12 standard calendar periods (Jan-Dec) and an optional 13th period]]></DESCRIPTION>
+<!--1271-->  <HELP><![CDATA[Create 12 standard calendar periods (Jan-Dec) and an optional 13th period for adjustments]]></HELP>
 <!--1271-->  <ISCENTRALLYMAINTAINED><![CDATA[Y]]></ISCENTRALLYMAINTAINED>
 <!--1271-->  <AD_TAB_ID><![CDATA[129]]></AD_TAB_ID>
 <!--1271-->  <AD_COLUMN_ID><![CDATA[2410]]></AD_COLUMN_ID>
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -16422,6 +16422,17 @@
 <!--40766E66FFC249B9B44F865C4AEC8D76-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--40766E66FFC249B9B44F865C4AEC8D76--></AD_MESSAGE>
 
+<!--4175F8D83F474E808F89BD557C36D3CC--><AD_MESSAGE>
+<!--4175F8D83F474E808F89BD557C36D3CC-->  <AD_MESSAGE_ID><![CDATA[4175F8D83F474E808F89BD557C36D3CC]]></AD_MESSAGE_ID>
+<!--4175F8D83F474E808F89BD557C36D3CC-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--4175F8D83F474E808F89BD557C36D3CC-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--4175F8D83F474E808F89BD557C36D3CC-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--4175F8D83F474E808F89BD557C36D3CC-->  <VALUE><![CDATA[13thPeriod]]></VALUE>
+<!--4175F8D83F474E808F89BD557C36D3CC-->  <MSGTEXT><![CDATA[13th Period]]></MSGTEXT>
+<!--4175F8D83F474E808F89BD557C36D3CC-->  <MSGTYPE><![CDATA[I]]></MSGTYPE>
+<!--4175F8D83F474E808F89BD557C36D3CC-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--4175F8D83F474E808F89BD557C36D3CC--></AD_MESSAGE>
+
 <!--417C01FAE8E447F7985AA6C04074924C--><AD_MESSAGE>
 <!--417C01FAE8E447F7985AA6C04074924C-->  <AD_MESSAGE_ID><![CDATA[417C01FAE8E447F7985AA6C04074924C]]></AD_MESSAGE_ID>
 <!--417C01FAE8E447F7985AA6C04074924C-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_PROCESS.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/sourcedata/AD_PROCESS.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -7,8 +7,8 @@
 <!--100-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--100-->  <VALUE><![CDATA[C_Periods Create Year]]></VALUE>
 <!--100-->  <NAME><![CDATA[Create Periods]]></NAME>
-<!--100-->  <DESCRIPTION><![CDATA[Create 12 standard calendar periods (Jan-Dec)]]></DESCRIPTION>
-<!--100-->  <HELP><![CDATA[Create 12 standard calendar periods (Jan-Dec)]]></HELP>
+<!--100-->  <DESCRIPTION><![CDATA[Create 12 standard calendar periods (Jan-Dec) and an optional 13th period]]></DESCRIPTION>
+<!--100-->  <HELP><![CDATA[Create 12 standard calendar periods (Jan-Dec) and an optional 13th period for adjustments]]></HELP>
 <!--100-->  <ACCESSLEVEL><![CDATA[3]]></ACCESSLEVEL>
 <!--100-->  <ISUSERSTARTABLE><![CDATA[N]]></ISUSERSTARTABLE>
 <!--100-->  <PROCEDURENAME><![CDATA[C_YearPeriods]]></PROCEDURENAME>
--- a/src-db/database/sourcedata/AD_PROCESS_PARA.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/sourcedata/AD_PROCESS_PARA.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -4140,6 +4140,26 @@
 <!--5DB8F1E894624135AA82694065D5B8FE-->  <AD_ELEMENT_ID><![CDATA[213]]></AD_ELEMENT_ID>
 <!--5DB8F1E894624135AA82694065D5B8FE--></AD_PROCESS_PARA>
 
+<!--62D937E6DE704AD98DEE705A732D6786--><AD_PROCESS_PARA>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <AD_PROCESS_PARA_ID><![CDATA[62D937E6DE704AD98DEE705A732D6786]]></AD_PROCESS_PARA_ID>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <NAME><![CDATA[Create Adjustment Period]]></NAME>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <DESCRIPTION><![CDATA[Add 13th period for adjustment entries]]></DESCRIPTION>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <HELP><![CDATA[When flagged process creates 13th period to include manual accounting entries for adjustment purposes.]]></HELP>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <AD_PROCESS_ID><![CDATA[100]]></AD_PROCESS_ID>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <SEQNO><![CDATA[20]]></SEQNO>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <AD_REFERENCE_ID><![CDATA[20]]></AD_REFERENCE_ID>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <COLUMNNAME><![CDATA[CREATEADJUSTMENT]]></COLUMNNAME>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <ISCENTRALLYMAINTAINED><![CDATA[Y]]></ISCENTRALLYMAINTAINED>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <FIELDLENGTH><![CDATA[1]]></FIELDLENGTH>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <ISMANDATORY><![CDATA[N]]></ISMANDATORY>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <ISRANGE><![CDATA[N]]></ISRANGE>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <DEFAULTVALUE><![CDATA[N]]></DEFAULTVALUE>
+<!--62D937E6DE704AD98DEE705A732D6786-->  <AD_ELEMENT_ID><![CDATA[4275E22E56264665A5036996A8045C8D]]></AD_ELEMENT_ID>
+<!--62D937E6DE704AD98DEE705A732D6786--></AD_PROCESS_PARA>
+
 <!--6E19A05CE9564C15BA5FAA118C005859--><AD_PROCESS_PARA>
 <!--6E19A05CE9564C15BA5FAA118C005859-->  <AD_PROCESS_PARA_ID><![CDATA[6E19A05CE9564C15BA5FAA118C005859]]></AD_PROCESS_PARA_ID>
 <!--6E19A05CE9564C15BA5FAA118C005859-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_REF_TABLE.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/sourcedata/AD_REF_TABLE.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -805,6 +805,7 @@
 <!--275-->  <AD_KEY><![CDATA[837]]></AD_KEY>
 <!--275-->  <AD_DISPLAY><![CDATA[482]]></AD_DISPLAY>
 <!--275-->  <ISVALUEDISPLAYED><![CDATA[N]]></ISVALUEDISPLAYED>
+<!--275-->  <WHERECLAUSE><![CDATA[periodtype = 'S']]></WHERECLAUSE>
 <!--275-->  <ORDERBYCLAUSE><![CDATA[C_Period.StartDate]]></ORDERBYCLAUSE>
 <!--275--></AD_REF_TABLE>
 
--- a/src-db/database/sourcedata/AD_VAL_RULE.xml	Wed Nov 28 18:39:23 2012 +0530
+++ b/src-db/database/sourcedata/AD_VAL_RULE.xml	Wed Nov 28 16:26:43 2012 +0100
@@ -2014,7 +2014,7 @@
 <!--E0541EB2493547BC934F5EFA044AB668-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--E0541EB2493547BC934F5EFA044AB668-->  <NAME><![CDATA[C_Period - Org Calendar Period]]></NAME>
 <!--E0541EB2493547BC934F5EFA044AB668-->  <TYPE><![CDATA[S]]></TYPE>
-<!--E0541EB2493547BC934F5EFA044AB668-->  <CODE><![CDATA[ exists (select 1 from c_year where c_period.c_year_id = c_year.c_year_id and (select c_calendar_id from ad_org where ad_org_id = ad_org_getcalendarowner (@AD_Org_ID@)) = c_year.c_calendar_id)]]></CODE>
+<!--E0541EB2493547BC934F5EFA044AB668-->  <CODE><![CDATA[ exists (select 1 from c_year where c_period.c_year_id = c_year.c_year_id and (select c_calendar_id from ad_org where ad_org_id = ad_org_getcalendarowner (@AD_Org_ID@)) = c_year.c_calendar_id) and periodtype = 'S']]></CODE>
 <!--E0541EB2493547BC934F5EFA044AB668-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--E0541EB2493547BC934F5EFA044AB668--></AD_VAL_RULE>
 
--- a/src/org/openbravo/erpCommon/ad_callouts/SL_Journal_Period.java	Wed Nov 28 18:39:23 2012 +0530
+++ b/src/org/openbravo/erpCommon/ad_callouts/SL_Journal_Period.java	Wed Nov 28 16:26:43 2012 +0100
@@ -72,6 +72,7 @@
     final String stradOrgId = vars.getGlobalVariable("inpadOrgId", "SL_Journal_Period|adOrgId", "");
     ;
     String strDateAcct = strDateAcctNew;
+    String strDateDoc = strDateDocNew;
     String strcPeriodId = strcPeriodIdNew;
     // When DateDoc is changed, update DateAcct
     if (strChanged.equals("inpdatedoc")) {
@@ -96,12 +97,16 @@
         if (DateTimeData.compare(this, StartDate, strDateAcct).equals("1")
             || DateTimeData.compare(this, EndDate, strDateAcct).equals("-1"))
           strDateAcct = EndDate;
+      } else {
+        strDateAcct = EndDate;
+        strDateDoc = EndDate;
       }
     }
     StringBuffer resultado = new StringBuffer();
     resultado.append("var calloutName='SL_Journal_Period';\n\n");
     resultado.append("var respuesta = new Array(");
     resultado.append("new Array(\"inpdateacct\", \"" + strDateAcct + "\"),");
+    resultado.append("new Array(\"inpdatedoc\", \"" + strDateAcct + "\"),");
     resultado.append("new Array(\"inpcPeriodId\", \"" + strcPeriodId + "\")");
 
     resultado.append(");");
--- a/src/org/openbravo/erpCommon/ad_forms/DocGLJournal.java	Wed Nov 28 18:39:23 2012 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/DocGLJournal.java	Wed Nov 28 16:26:43 2012 +0100
@@ -72,6 +72,7 @@
 
     m_PostingType = data[0].getField("PostingType");
     m_IsOpening = data[0].getField("isopening");
+    C_Period_ID = isperiodOpen(conn, data[0].getField("period"));
 
     // Contained Objects
     p_lines = loadLines(conn);
@@ -270,6 +271,23 @@
     return true;
   }
 
+  private String isperiodOpen(ConnectionProvider conn, String periodId) {
+    if ("".equals(periodId)) {
+      return "";
+    }
+    try {
+      DocGLJournalData[] data = DocGLJournalData.periodOpen(conn, periodId);
+      if (data != null && data.length > 0) {
+        return periodId;
+      } else {
+        return "";
+      }
+    } catch (ServletException e) {
+      e.printStackTrace();
+      return "";
+    }
+  }
+
   public String getServletInfo() {
     return "Servlet for the accounting";
   } // end of getServletInfo() method
--- a/src/org/openbravo/erpCommon/ad_forms/DocGLJournal_data.xsql	Wed Nov 28 18:39:23 2012 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/DocGLJournal_data.xsql	Wed Nov 28 16:26:43 2012 +0100
@@ -28,7 +28,7 @@
       <![CDATA[
         SELECT G.AD_CLIENT_ID, G.AD_ORG_ID, G.DOCUMENTNO, G.DATEACCT, G.DATEDOC,
         G.C_CURRENCY_ID, G.C_DOCTYPE_ID, G.POSTED, G.POSTINGTYPE, G.ISOPENING,
-        G.C_CAMPAIGN_ID, G.C_PROJECT_ID, G.USER1_ID, G.USER2_ID
+        G.C_CAMPAIGN_ID, G.C_PROJECT_ID, G.USER1_ID, G.USER2_ID, C_PERIOD_ID AS PERIOD
         FROM GL_JOURNAL G
         WHERE AD_CLIENT_ID=?
         AND GL_JOURNAL_ID=?
@@ -50,4 +50,18 @@
      <Parameter name="client"/>
      <Parameter name="id"/>
    </SqlMethod>
-   </SqlClass>
+   <SqlMethod name="periodOpen" type="preparedStatement" return="multiple">
+      <SqlMethodComment></SqlMethodComment>
+      <Sql>
+      <![CDATA[
+      select max(c_period.c_period_id) as period
+      from c_period, c_periodcontrol
+      where c_period.c_period_id = c_periodcontrol.c_period_id
+      and c_periodcontrol.docbasetype = 'GLJ'
+      and c_periodcontrol.periodstatus = 'O'
+      and c_periodcontrol.c_period_id = ?
+      ]]>
+      </Sql>
+      <Parameter name="period"/>
+   </SqlMethod>   
+ </SqlClass>
--- a/src/org/openbravo/erpCommon/ad_forms/DocInvoice.java	Wed Nov 28 18:39:23 2012 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/DocInvoice.java	Wed Nov 28 16:26:43 2012 +0100
@@ -21,21 +21,18 @@
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.List;
 
 import javax.servlet.ServletException;
 
 import org.apache.log4j.Logger;
-import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.secureApp.VariablesSecureApp;
-import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.data.FieldProvider;
 import org.openbravo.database.ConnectionProvider;
+import org.openbravo.erpCommon.utility.AccDefUtility;
 import org.openbravo.erpCommon.utility.OBDateUtils;
 import org.openbravo.erpCommon.utility.SequenceIdData;
 import org.openbravo.model.financialmgmt.calendar.Period;
-import org.openbravo.model.financialmgmt.calendar.Year;
 
 public class DocInvoice extends AcctServer {
   private static final long serialVersionUID = 1L;
@@ -816,7 +813,18 @@
             i == periodNumber ? amount.subtract(total).toString() : periodAmount.toString());
         plan.add(hm);
       }
-      period = getNextPeriod(period);
+      period = AccDefUtility.getNextPeriod(period);
+      try {
+        AcctServerData[] data = AcctServerData.periodOpen(connectionProvider, AD_Client_ID,
+            DocumentType, AD_Org_ID, OBDateUtils.formatDate(period.getEndingDate()));
+        if ("".equals(data[0].period)) {
+          setStatus(STATUS_PeriodClosed);
+          throw new IllegalStateException("DocInvoice - Error getting next year period");
+        }
+      } catch (ServletException e) {
+        log4j.warn("DocInvoice - Error checking period open.", e);
+        e.printStackTrace();
+      }
       date = period.getEndingDate();
       total = total.add(periodAmount);
       i++;
@@ -856,71 +864,6 @@
     return amount.toString();
   }
 
-  private Period getNextPeriod(Period period) {
-    OBCriteria<Period> obc = OBDal.getInstance().createCriteria(Period.class);
-    obc.add(Restrictions.eq(Period.PROPERTY_YEAR, period.getYear()));
-    obc.addOrderBy(Period.PROPERTY_PERIODNO, false);
-    obc.setFilterOnReadableOrganization(false);
-    obc.setFilterOnReadableClients(false);
-    Period targetPeriod = null;
-    if (period.equals(obc.list().get(0))) {
-      targetPeriod = getFirstPeriodOfNextYear(period.getYear());
-    } else {
-      for (Period p : obc.list()) {
-        if (p == period) {
-          return targetPeriod;
-        }
-        targetPeriod = p;
-      }
-    }
-    try {
-      AcctServerData[] data = AcctServerData.periodOpen(connectionProvider, AD_Client_ID,
-          DocumentType, AD_Org_ID, OBDateUtils.formatDate(targetPeriod.getEndingDate()));
-      if ("".equals(data[0].period)) {
-        setStatus(STATUS_PeriodClosed);
-        throw new IllegalStateException("DocInvoice - Error getting next year period");
-      }
-    } catch (ServletException e) {
-      log4j.warn("DocInvoice - Error checking period open.", e);
-      e.printStackTrace();
-    }
-    return targetPeriod;
-  }
-
-  private Period getFirstPeriodOfNextYear(Year year) {
-    OBCriteria<Period> obc = OBDal.getInstance().createCriteria(Period.class);
-    obc.add(Restrictions.eq(Period.PROPERTY_YEAR, getNextYear(year)));
-    obc.addOrderBy(Period.PROPERTY_PERIODNO, true);
-    obc.setFilterOnReadableOrganization(false);
-    obc.setFilterOnReadableClients(false);
-    List<Period> periods = obc.list();
-    if (periods.size() == 0) {
-      setStatus(STATUS_PeriodClosed);
-      throw new IllegalStateException("DocInvoice - Error getting next year period");
-    }
-    return periods.get(0);
-  }
-
-  private Year getNextYear(Year year) {
-    OBCriteria<Year> obc = OBDal.getInstance().createCriteria(Year.class);
-    obc.add(Restrictions.eq(Year.PROPERTY_CALENDAR, year.getCalendar()));
-    obc.addOrderBy(Year.PROPERTY_FISCALYEAR, false);
-    obc.setFilterOnReadableOrganization(false);
-    obc.setFilterOnReadableClients(false);
-    Year targetYear = null;
-    if (year.equals(obc.list().get(0))) {
-      setStatus(STATUS_PeriodClosed);
-      throw new IllegalStateException("DocInvoice - Error getting next year");
-    }
-    for (Year y : obc.list()) {
-      if (y == year) {
-        return targetYear;
-      }
-      targetYear = y;
-    }
-    return targetYear;
-  }
-
   /**
    * Update Product Info. - Costing (PriceLastInv) - PO (PriceLastInv)
    * 
--- a/src/org/openbravo/erpCommon/ad_forms/FactLine.java	Wed Nov 28 18:39:23 2012 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/FactLine.java	Wed Nov 28 16:26:43 2012 +0100
@@ -965,6 +965,9 @@
 
   public String setC_Period_ID(AcctServer m_docVO, String strDateAcct, ConnectionProvider conn) {
     AcctServerData[] data = null;
+    if ("GLJ".equals(m_docVO.DocumentType)) {
+      return m_docVO.C_Period_ID;
+    }
     try {
       data = AcctServerData.periodOpen(conn, m_docVO.AD_Client_ID, m_docVO.DocumentType,
           m_docVO.AD_Org_ID, strDateAcct);
--- a/src/org/openbravo/erpCommon/utility/AccDefUtility.java	Wed Nov 28 18:39:23 2012 +0530
+++ b/src/org/openbravo/erpCommon/utility/AccDefUtility.java	Wed Nov 28 16:26:43 2012 +0100
@@ -55,14 +55,14 @@
     obc.createAlias(Period.PROPERTY_YEAR, "y");
     obc.add(Restrictions.eq("y." + Year.PROPERTY_CALENDAR, fiscalCalendar));
     obc.add(Restrictions.ge(Period.PROPERTY_ENDINGDATE, date));
-    obc.add(Restrictions.ge(Period.PROPERTY_ENDINGDATE, date));
+    obc.add(Restrictions.ne(Period.PROPERTY_PERIODTYPE, "A"));
     obc.add(Restrictions.le(Period.PROPERTY_STARTINGDATE, date));
     obc.addOrderBy(Period.PROPERTY_PERIODNO, false);
     obc.setFilterOnReadableOrganization(false);
     obc.setFilterOnReadableClients(false);
     List<Period> periods = obc.list();
     if (periods.size() == 0) {
-      log4j.error("SL_Invoice_Product - No period defined for invoice date");
+      log4j.error("AccDefUtility - No period defined for invoice date");
       return null;
     } else {
       return periods.get(0);
@@ -72,6 +72,7 @@
   public static Period getNextPeriod(Period period) {
     OBCriteria<Period> obc = OBDal.getInstance().createCriteria(Period.class);
     obc.add(Restrictions.eq(Period.PROPERTY_YEAR, period.getYear()));
+    obc.add(Restrictions.ne(Period.PROPERTY_PERIODTYPE, "A"));
     obc.addOrderBy(Period.PROPERTY_PERIODNO, false);
     obc.setFilterOnReadableOrganization(false);
     obc.setFilterOnReadableClients(false);
@@ -92,12 +93,13 @@
   public static Period getFirstPeriodOfNextYear(Year year) {
     OBCriteria<Period> obc = OBDal.getInstance().createCriteria(Period.class);
     obc.add(Restrictions.eq(Period.PROPERTY_YEAR, getNextYear(year)));
+    obc.add(Restrictions.ne(Period.PROPERTY_PERIODTYPE, "A"));
     obc.addOrderBy(Period.PROPERTY_PERIODNO, true);
     obc.setFilterOnReadableOrganization(false);
     obc.setFilterOnReadableClients(false);
     List<Period> periods = obc.list();
     if (periods.size() == 0) {
-      throw new OBException("SL_Invoice_Product - Error getting next year period");
+      throw new OBException("AccDefUtility - Error getting next year period");
     }
     return periods.get(0);
   }
@@ -110,7 +112,7 @@
     obc.setFilterOnReadableClients(false);
     Year targetYear = null;
     if (year.equals(obc.list().get(0))) {
-      throw new OBException("SL_Invoice_Product - Error getting next year period");
+      throw new OBException("AccDefUtility - Error getting next year period");
     }
     for (Year y : obc.list()) {
       if (y == year) {