src-db/database/model/functions/M_WH_PERIOD_GENERATE.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 756 ae11e4610537
child 2586 683779f1e610
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION M_WH_PERIOD_GENERATE">
adrian@94
     3
    <function name="M_WH_PERIOD_GENERATE" type="NULL">
juanpablo@1605
     4
      <parameter name="pinstance_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
gorkaion@239
     7
      <body><![CDATA[/*************************************************************************
carlos@0
     8
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
     9
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    10
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    11
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    12
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    13
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    14
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    15
* License for the specific  language  governing  rights  and  limitations
carlos@0
    16
* under the License.
carlos@0
    17
* The Original Code is Openbravo ERP.
carlos@0
    18
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    19
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    20
* All Rights Reserved.
carlos@0
    21
* Contributor(s):  ______________________________________.
carlos@0
    22
************************************************************************/
carlos@0
    23
  -- Parameter
carlos@0
    24
  TYPE RECORD IS REF CURSOR;
carlos@0
    25
    Cur_Parameter RECORD;
juanpablo@1605
    26
    v_WHSchedule_ID VARCHAR2(32);
carlos@0
    27
    v_DateFrom DATE;
carlos@0
    28
    v_DateTo DATE;
carlos@0
    29
    --
carlos@0
    30
    v_no NUMBER;
carlos@0
    31
    v_count NUMBER;
juanpablo@1605
    32
    v_nextNo VARCHAR2(32) ;
carlos@0
    33
    v_startDate DATE;
carlos@0
    34
    v_endDate DATE;
carlos@0
    35
    v_ResultStr VARCHAR(60) ;
carlos@0
    36
    --  WHSchedule Variables
juanpablo@1605
    37
    v_Client_ID VARCHAR2(32);
juanpablo@1605
    38
    v_Org_ID VARCHAR2(32);
carlos@0
    39
    v_Periodicity CHAR(1) ;
juanpablo@1605
    40
    v_User_ID VARCHAR2(32);
carlos@0
    41
  BEGIN
carlos@0
    42
    --  Update AD_PInstance
carlos@0
    43
    v_ResultStr:='PInstanceNotFound';
carlos@0
    44
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    45
  BEGIN --BODY
carlos@0
    46
    -- Get Parameters
carlos@0
    47
    v_ResultStr:='ReadingParameters';
carlos@0
    48
    FOR Cur_Parameter IN
carlos@0
    49
      (SELECT i.Record_ID,
carlos@0
    50
        p.ParameterName,
carlos@0
    51
        p.P_String,
carlos@0
    52
        p.P_Number,
carlos@0
    53
        p.P_Date,
carlos@0
    54
        i.CreatedBy
carlos@0
    55
      FROM AD_PInstance i
carlos@0
    56
      LEFT JOIN AD_PInstance_Para p
carlos@0
    57
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    58
      WHERE i.AD_PInstance_ID=PInstance_ID
carlos@0
    59
      ORDER BY p.SeqNo
carlos@0
    60
      )
carlos@0
    61
    LOOP
carlos@0
    62
      v_WHSchedule_ID:=Cur_Parameter.Record_ID;
carlos@0
    63
      DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_WHSchedule_ID) ;
carlos@0
    64
      v_User_ID:=Cur_Parameter.CreatedBy;
carlos@0
    65
      IF(Cur_Parameter.ParameterName='DateFrom') THEN
carlos@0
    66
        v_DateFrom:=Cur_Parameter.P_Date;
carlos@0
    67
        DBMS_OUTPUT.PUT_LINE('  DateFrom=' || v_DateFrom) ;
carlos@0
    68
      ELSIF(Cur_Parameter.ParameterName='DateTo') THEN
carlos@0
    69
        v_DateTo:=Cur_Parameter.P_Date;
carlos@0
    70
        DBMS_OUTPUT.PUT_LINE('  DateTo=' || v_DateTo) ;
carlos@0
    71
      ELSE
carlos@0
    72
        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
carlos@0
    73
      END IF;
carlos@0
    74
    END LOOP; -- Get Parameter
carlos@0
    75
    --  Get C_Year Record
carlos@0
    76
    DBMS_OUTPUT.PUT_LINE('Get Year info') ;
carlos@0
    77
    v_ResultStr:='YearNotFound';
carlos@0
    78
    SELECT AD_Client_ID,
carlos@0
    79
      AD_Org_ID,
carlos@0
    80
      Periodicity
carlos@0
    81
    INTO v_Client_ID,
carlos@0
    82
      v_Org_ID,
carlos@0
    83
      v_Periodicity
carlos@0
    84
    FROM M_WH_Schedule
carlos@0
    85
    WHERE M_WH_Schedule_ID=v_WHSchedule_ID;
carlos@0
    86
    IF(v_Periodicity='D') THEN --daily
carlos@0
    87
      v_startDate:=v_DateFrom;
carlos@0
    88
    ELSIF(v_Periodicity='F') THEN --Fortnightly
carlos@0
    89
      IF(TO_NUMBER(TO_CHAR(v_DateFrom, 'DD'))=1) THEN
carlos@0
    90
        v_startDate:=v_DateFrom;
gorkaion@239
    91
      ELSIF(TO_NUMBER(TO_CHAR(v_DateFrom, 'DD'))>15) THEN
carlos@0
    92
        v_startDate:=TO_DATE('01/'||TO_CHAR(ADD_MONTHS(v_startDate, 1), 'MM/YYYY'), 'DD/MM/YYYY') ;
carlos@0
    93
      ELSE
carlos@0
    94
        v_startDate:=TO_DATE('01/'||TO_CHAR(v_startDate, 'MM/YYYY'), 'DD/MM/YYYY') ;
carlos@0
    95
      END IF;
carlos@0
    96
    ELSIF(v_Periodicity='M') THEN
carlos@0
    97
      IF(TO_NUMBER(TO_CHAR(v_DateFrom, 'DD'))=1) THEN
carlos@0
    98
        v_startDate:=v_DateFrom;
carlos@0
    99
      ELSE
carlos@0
   100
        v_startDate:=TO_DATE('01/'||TO_CHAR(ADD_MONTHS(v_startDate, 1), 'MM/YYYY'), 'DD/MM/YYYY') ;
carlos@0
   101
      END IF;
carlos@0
   102
    END IF;
carlos@0
   103
    FOR v_no IN 1..1000
carlos@0
   104
    LOOP -- max number of periods inserted: 1000
gorkaion@239
   105
      IF(v_startDate>v_DateTo) THEN
carlos@0
   106
        EXIT; -- Stop inserting
carlos@0
   107
      END IF;
carlos@0
   108
      IF(v_Periodicity='D') THEN
carlos@0
   109
        v_endDate:=v_startDate;
carlos@0
   110
      ELSIF(v_Periodicity='F') THEN
carlos@0
   111
        IF(TO_NUMBER(TO_CHAR(v_startDate, 'DD'))=1) THEN
carlos@0
   112
          v_endDate:=TO_DATE('15/'||TO_CHAR(v_startDate, 'MM/YYYY'), 'DD/MM/YYYY') ;
carlos@0
   113
        ELSE
carlos@0
   114
          v_endDate:=LAST_DAY(v_startDate) ;
carlos@0
   115
        END IF;
carlos@0
   116
      ELSE
carlos@0
   117
        v_endDate:=LAST_DAY(v_startDate) ;
carlos@0
   118
      END IF;
adrian@170
   119
      --  Do we have the period already:1
carlos@0
   120
      v_ResultStr:='Checking Period no: '||v_no;
carlos@0
   121
      SELECT COUNT(*)
carlos@0
   122
      INTO v_count
carlos@0
   123
      FROM M_WH_PERIOD
carlos@0
   124
      WHERE M_WH_Schedule_ID=v_WHSchedule_ID
carlos@0
   125
        AND STARTDATE=v_startDate;
carlos@0
   126
      IF v_count=0 THEN
carlos@0
   127
        -- get new NextNo
carlos@0
   128
        AD_Sequence_Next('M_WH_Period', v_WHSchedule_ID, v_nextNo) ;
carlos@0
   129
        INSERT
carlos@0
   130
        INTO M_WH_Period
carlos@0
   131
          (
carlos@0
   132
            M_WH_Period_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
   133
            Created, CreatedBy, Updated, UpdatedBy,
carlos@0
   134
            M_WH_Schedule_ID, StartDate, EndDate)
carlos@0
   135
          VALUES
carlos@0
   136
          (
carlos@0
   137
            v_nextNo, v_Client_ID, v_Org_ID, 'Y',
carlos@0
   138
            now(), v_User_ID, now(), v_User_ID,
carlos@0
   139
            v_WHSchedule_ID, v_startDate, v_endDate)
carlos@0
   140
          ;
carlos@0
   141
        DBMS_OUTPUT.PUT_LINE('Period Added') ;
carlos@0
   142
      END IF;
carlos@0
   143
      --next Date
carlos@0
   144
      IF(v_Periodicity='D') THEN
carlos@0
   145
        v_startDate:=v_startDate + 1;
carlos@0
   146
      ELSIF(v_Periodicity='F') THEN
carlos@0
   147
        IF(TO_NUMBER(TO_CHAR(v_startDate, 'DD'))=1) THEN
carlos@0
   148
          v_startDate:=TO_DATE('16/'||TO_CHAR(v_startDate, 'MM/YYYY'), 'DD/MM/YYYY') ;
carlos@0
   149
        ELSE
carlos@0
   150
          v_startDate:=TO_DATE('01/'||TO_CHAR(ADD_MONTHS(v_startDate, 1), 'MM/YYYY'), 'DD/MM/YYYY') ;
carlos@0
   151
        END IF;
carlos@0
   152
      ELSE
carlos@0
   153
        v_startDate:=ADD_MONTHS(v_startDate, 1) ;
carlos@0
   154
      END IF;
carlos@0
   155
    END LOOP;
carlos@0
   156
    --  Update AD_PInstance
gorkaion@239
   157
    --<<END_PROCEDURE>>
carlos@0
   158
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 1, NULL) ;
carlos@0
   159
    RETURN;
carlos@0
   160
  END; --BODY
carlos@0
   161
EXCEPTION
carlos@0
   162
WHEN OTHERS THEN
carlos@0
   163
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   164
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   165
  ROLLBACK;
carlos@0
   166
  IF(PInstance_ID IS NOT NULL) THEN
carlos@0
   167
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   168
  END IF;
carlos@0
   169
  RETURN;
antonio@735
   170
END M_WH_PERIOD_GENERATE
gorkaion@239
   171
]]></body>
adrian@94
   172
    </function>
adrian@94
   173
  </database>