src-db/database/model/functions/MRP_REQUISITION.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 756 ae11e4610537
child 2292 14f461617d84
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION MRP_REQUISITION">
adrian@94
     3
    <function name="MRP_REQUISITION" type="NULL">
juanpablo@1605
     4
      <parameter name="p_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
  -- Logistice
carlos@0
    24
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    25
  v_Message VARCHAR2(2000):='';
juanpablo@1605
    26
  v_Record_ID VARCHAR2(32);
juanpablo@1605
    27
  v_User_ID VARCHAR2(32):='0';
juanpablo@1605
    28
  v_Client_ID VARCHAR2(32);
juanpablo@1605
    29
  v_Org_ID VARCHAR2(32);
gorkaion@586
    30
  v_Description0 VARCHAR(255);
carlos@0
    31
  v_Description VARCHAR(255);
gorkaion@586
    32
  v_Count NUMBER;
carlos@0
    33
juanpablo@1605
    34
  v_MRequisition_ID VARCHAR2(32):=NULL;
juanpablo@1605
    35
  v_MRequisitionLine_ID VARCHAR2(32);
carlos@0
    36
  v_DocumentNo NVARCHAR2(60);
antonio@735
    37
  v_Line NUMBER:= 0;
carlos@0
    38
carlos@0
    39
  FINISH_PROCESS BOOLEAN DEFAULT FALSE;
carlos@0
    40
  --  Parameter
carlos@0
    41
  TYPE RECORD IS REF CURSOR;
carlos@0
    42
    Cur_Parameter RECORD;
carlos@0
    43
    Cur_workproposal RECORD;
carlos@0
    44
  BEGIN
carlos@0
    45
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    46
    v_ResultStr:='PInstanceNotFound';
carlos@0
    47
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    48
  BEGIN --BODY
carlos@0
    49
    -- Get Parameters
carlos@0
    50
    v_ResultStr:='ReadingParameters';
carlos@0
    51
    FOR Cur_Parameter IN
carlos@0
    52
      (SELECT i.Record_ID, i.AD_User_ID, i.AD_Client_ID, i.AD_Org_ID,
carlos@0
    53
        p.ParameterName, p.P_String, p.P_Number, p.P_Date
carlos@0
    54
      FROM AD_PInstance i
carlos@0
    55
      LEFT JOIN AD_PInstance_Para p
carlos@0
    56
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    57
      WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    58
      ORDER BY p.SeqNo
carlos@0
    59
      )
carlos@0
    60
    LOOP
carlos@0
    61
      v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    62
      v_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    63
      v_Client_ID := Cur_Parameter.AD_Client_ID;
carlos@0
    64
      v_Org_ID := Cur_Parameter.AD_Org_ID;
carlos@0
    65
    END LOOP; -- Get Parameter
carlos@0
    66
gorkaion@586
    67
    SELECT NAME || ' ' || TO_CHAR(DATEDOC), Description
gorkaion@586
    68
      INTO v_Description0, v_Description
gorkaion@586
    69
    FROM MRP_RUN_PRODUCTION
gorkaion@586
    70
    WHERE MRP_RUN_PRODUCTION_ID = v_Record_ID;
antonio@737
    71
gorkaion@586
    72
    SELECT count(*) INTO v_Count
gorkaion@586
    73
    FROM MRP_RUN_PRODUCTIONLINE
gorkaion@586
    74
    WHERE MRP_RUN_PRODUCTION_ID = v_Record_ID
gorkaion@586
    75
      AND INOUTTRXTYPE = 'MP'
gorkaion@586
    76
      AND M_REQUISITIONLINE_ID IS NULL;
gorkaion@586
    77
    IF (v_Count = 0) THEN
gorkaion@586
    78
      v_Message := '@NoProposalsWithoutRequisitionLine@';
gorkaion@586
    79
      RAISE_APPLICATION_ERROR(-20000, v_Message);
gorkaion@586
    80
    END IF;
carlos@0
    81
gorkaion@586
    82
    Ad_Sequence_Next('M_Requisition', v_Client_ID, v_MRequisition_ID);
gorkaion@586
    83
    Ad_Sequence_Doc('DocumentNo_M_Requisition', v_Client_ID, 'Y', v_DocumentNo);
antonio@737
    84
gorkaion@586
    85
    v_Description := 'MRP: ' || v_Description0 || CHR(10) || v_Description;
gorkaion@586
    86
gorkaion@586
    87
    INSERT INTO M_Requisition (
gorkaion@586
    88
      M_REQUISITION_ID, AD_CLIENT_ID, AD_ORG_ID,
gorkaion@586
    89
      ISACTIVE, CREATED,  CREATEDBY,  UPDATED,  UPDATEDBY,
gorkaion@586
    90
      DESCRIPTION, DOCUMENTNO, AD_USER_ID
gorkaion@586
    91
    ) VALUES (
gorkaion@586
    92
      v_MRequisition_ID, v_Client_ID, v_Org_ID,
gorkaion@586
    93
      'Y', now(), v_User_ID, now(), v_User_ID,
gorkaion@586
    94
      v_Description, v_DocumentNo, v_User_ID
gorkaion@586
    95
    );
antonio@737
    96
carlos@0
    97
    FOR Cur_workproposal IN (
carlos@0
    98
      SELECT *
carlos@0
    99
      FROM MRP_RUN_PRODUCTIONLINE
carlos@0
   100
      WHERE MRP_RUN_PRODUCTION_ID = v_Record_ID
carlos@0
   101
        AND INOUTTRXTYPE = 'MP'
carlos@0
   102
        AND M_REQUISITIONLINE_ID IS NULL
carlos@0
   103
      ORDER BY PLANNEDDATE
carlos@0
   104
      ) LOOP
carlos@0
   105
      v_ResultStr:='Create Requisition';
carlos@0
   106
gorkaion@586
   107
      v_Line := v_Line + 10;
gorkaion@586
   108
      Ad_Sequence_Next('M_RequisitionLine', v_Client_ID, v_MRequisitionLine_ID);
gorkaion@586
   109
      INSERT INTO M_REQUISITIONLINE (
gorkaion@586
   110
        M_REQUISITIONLINE_ID,  AD_CLIENT_ID, AD_ORG_ID,
carlos@0
   111
        ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY,
gorkaion@586
   112
        M_REQUISITION_ID,  NEEDBYDATE, M_PRODUCT_ID,
gorkaion@586
   113
        QTY, LINE
gorkaion@586
   114
      ) VALUES (
gorkaion@586
   115
        v_MRequisitionLine_ID, v_Client_ID, v_Org_ID,
gorkaion@586
   116
        'Y', now(), v_User_ID, now(), v_User_ID,
gorkaion@586
   117
        v_MRequisition_ID, Cur_workproposal.PLANNEDDATE, Cur_workproposal.M_PRODUCT_ID,
gorkaion@586
   118
        Cur_workproposal.QTY, v_Line
gorkaion@586
   119
      );
carlos@0
   120
carlos@0
   121
      UPDATE MRP_RUN_PRODUCTIONLINE
carlos@0
   122
         SET M_REQUISITIONLINE_ID=v_MRequisitionLine_ID
gorkaion@586
   123
      WHERE MRP_RUN_PRODUCTIONLINE_ID = Cur_workproposal.MRP_RUN_PRODUCTIONLINE_ID;
carlos@0
   124
    END LOOP;
carlos@0
   125
  END; --BODY
gorkaion@586
   126
  v_Message := '@CreatedRequisition@: ' || v_DocumentNo;
gorkaion@586
   127
  v_ResultStr :='Set requisition lines as planned';
antonio@737
   128
  UPDATE M_RequisitionLine
gorkaion@586
   129
  SET REQSTATUS = 'P'
gorkaion@586
   130
  WHERE M_RequisitionLine_ID IN (SELECT M_RequisitionLine_ID
gorkaion@586
   131
                                 FROM MRP_RUN_PRODUCTIONLINE
gorkaion@586
   132
                                 WHERE MRP_RUN_PRODUCTION_ID = v_Record_ID
gorkaion@586
   133
                                   AND INOUTTRXTYPE = 'MF');
antonio@737
   134
gorkaion@586
   135
  UPDATE M_Requisition
gorkaion@586
   136
  SET DocStatus = 'CL'
antonio@737
   137
  WHERE M_Requisition_ID IN (SELECT M_Requisition_ID
antonio@737
   138
                            FROM M_RequisitionLine
gorkaion@586
   139
                            WHERE M_RequisitionLine_ID IN (SELECT M_RequisitionLine_ID
gorkaion@586
   140
                                                          FROM MRP_RUN_PRODUCTIONLINE
gorkaion@586
   141
                                                          WHERE MRP_RUN_PRODUCTION_ID = v_Record_ID
gorkaion@586
   142
                                                            AND INOUTTRXTYPE = 'MF'))
gorkaion@586
   143
    AND NOT EXISTS (SELECT 1
gorkaion@586
   144
                    FROM M_RequisitionLine rl
gorkaion@586
   145
                    WHERE rl.REQSTATUS = 'O'
gorkaion@586
   146
                      AND rl.M_Requisition_ID = M_Requisition.M_Requisition_ID);
carlos@0
   147
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
carlos@0
   148
EXCEPTION
carlos@0
   149
WHEN OTHERS THEN
carlos@0
   150
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   151
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   152
  ROLLBACK;
carlos@0
   153
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   154
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   155
  END IF;
carlos@0
   156
--  RETURN;
adrian@94
   157
END MRP_REQUISITION
gorkaion@239
   158
]]></body>
adrian@94
   159
    </function>
adrian@94
   160
  </database>