martes, 16 de abril de 2019

Generate Inventory Move From Material Receipt


/*****************************************************************************/

-- POSTGRESQL

alter table m_movementline add id_linea_recepcion varchar(60);

create table m_matchmov
(
    m_matchmov_id numeric(10,0) primary key not null,
    ad_client_id numeric(10) NOT NULL,
    ad_org_id numeric(10) NOT NULL,
    isactive bpchar(1) NOT NULL DEFAULT 'Y'::bpchar,
    created timestamp NOT NULL DEFAULT now(),
    createdby numeric(10) NOT NULL,
    updated timestamp NOT NULL DEFAULT now(),
    updatedby numeric(10) NOT NULL,
    m_inoutline_id numeric(10,0) not null,
    m_product_id numeric(10,0) not null,
    m_movementline_id numeric(10,0) not null,
    qty numeric NOT NULL DEFAULT 0
);


create or replace function fnt_insert_matchmov(p_inout_id numeric(10,0), p_usuario_id numeric(10,0))
returns boolean
as $$
begin
    insert into m_matchmov
    (
        m_matchmov_id,
        ad_client_id,
        ad_org_id,
        isactive,
        created,
        createdby,
        updated,
        updatedby,
        m_inoutline_id,
        m_product_id,
        m_movementline_id,
        qty
    )
    select
        coalesce((select max(mm.m_matchmov_id) from m_matchmov mm), 999999) +
        row_number() over(order by il.line) as m_matchmov_id,
        1000000::numeric(10,0),
        1000000::numeric(10,0),
        'Y'::char,
        now()::timestamp without time zone,
        p_usuario_id,
        now()::timestamp without time zone,
        p_usuario_id,
        il.m_inoutline_id,
        il.m_product_id,
        ml.m_movementline_id,
        il.qtyentered
    from m_inoutline il
        join m_movementline ml
        on il.m_inoutline_id = ml.id_linea_recepcion::int
    where il.m_inout_id = p_inout_id
        and not exists
        (
            select 1 from m_matchmov m
            where m.m_inoutline_id = il.m_inoutline_id
                and m.m_movementline_id = ml.m_movementline_id
        );
   
    return true;
end;
$$ language plpgsql;

/*****************************************************************************/

// PROCESS PLUGIN

/**********************************************************************
* This file is part of iDempiere ERP Open Source                      *
* http://www.idempiere.org                                            *
*                                                                     *
* Copyright (C) Contributors                                          *
*                                                                     *
* This program is free software; you can redistribute it and/or       *
* modify it under the terms of the GNU General Public License         *
* as published by the Free Software Foundation; either version 2      *
* of the License, or (at your option) any later version.              *
*                                                                     *
* This program is distributed in the hope that it will be useful,     *
* but WITHOUT ANY WARRANTY; without even the implied warranty of      *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        *
* GNU General Public License for more details.                        *
*                                                                     *
* You should have received a copy of the GNU General Public License   *
* along with this program; if not, write to the Free Software         *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,          *
* MA 02110-1301, USA.                                                 *
*                                                                     *
* Contributors:                                                       *
* - Jose Francisco (jfrodriguez.idempiere@gmail.com)                                         *
**********************************************************************/

package company.process.inout;

import java.math.BigDecimal;
import java.util.logging.Level;

import org.compiere.model.MInOut;
import org.compiere.model.MInOutLine;
import org.compiere.model.MMovement;
import org.compiere.model.MMovementLine;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class InOutCreateMovement extends SvrProcess
{
    /**    Shipment                    */
    private int     p_M_InOut_ID = 0;
   
    /* Document No                    */
    private String    m_MovementDocumentNo = null;
   
    private int p_LocatorTo_ID = 0;
   
    /**
     *  Prepare - e.g., get Parameters.
     */
    @Override
    protected void prepare()
    {
        ProcessInfoParameter[] para = getParameter();
       
        for (int i = 0; i < para.length; i++)
        {
            String name = para[i].getParameterName();
            if (para[i].getParameter() == null)
                ;
            else if (name.equals("m_MovementDocumentNo"))
                m_MovementDocumentNo = (String)para[i].getParameter();
            else if (name.equals("p_LocatorTo_ID"))
                p_LocatorTo_ID = ((BigDecimal)para[i].getParameter()).intValue();
            else
                log.log(Level.SEVERE, "Unknown Parameter: " + name);
        }
       
        p_M_InOut_ID = getRecord_ID();
    }    //    prepare

    /**
     *     Create Movement.
     *    @return document no
     *    @throws Exception
     */
    @Override
    protected String doIt() throws Exception
    {
        int v_user_id = Env.getAD_User_ID(Env.getCtx());
       
        if (log.isLoggable(Level.INFO)) log.info("M_InOut_ID=" + p_M_InOut_ID
                + ", m_MovementDocumentNo=" + m_MovementDocumentNo);
            if (p_M_InOut_ID == 0)
                throw new IllegalArgumentException("No Shipment");
            //
           
            MInOut ship = new MInOut (getCtx(), p_M_InOut_ID, get_TrxName());
           
            if (ship.get_ID() == 0)
                throw new IllegalArgumentException("Shipment not found");
            if (!MInOut.DOCSTATUS_Completed.equals(ship.getDocStatus()))
                throw new IllegalArgumentException("Shipment not completed");
           
            MMovement movement = new MMovement(getCtx(), 0, get_TrxName());
           
            movement.setAD_Org_ID(ship.getAD_Org_ID());
           
            if(ship.getDateReceived() != null)
                movement.setMovementDate(ship.getDateReceived());
           
            movement.setSalesRep_ID(ship.getSalesRep_ID());
           
            if (m_MovementDocumentNo != null && m_MovementDocumentNo.length() > 0)
                movement.setDocumentNo(m_MovementDocumentNo);
           
            if (!movement.save())
                throw new IllegalArgumentException("Cannot save Movement");
           
            MInOutLine[] shipLines = ship.getLines(false);
           
            for(int i = 0; i < shipLines.length; i++)
            {
                MInOutLine sLine = shipLines[i];
                MMovementLine line = new MMovementLine(movement);
               
                line.setLine(sLine.getLine());
                line.setM_Locator_ID(sLine.getM_Locator_ID());
                line.setM_LocatorTo_ID(p_LocatorTo_ID);
                line.setM_Product_ID(sLine.getM_Product_ID());
                line.setDescription(sLine.getDescription());
                line.setMovementQty(sLine.getQtyEntered());
                line.set_CustomColumn("id_linea_recepcion", sLine.getM_InOutLine_ID());
               
                if (!line.save())
                    throw new IllegalArgumentException("Cannot save Movement Line");
            }
           
            movement.saveEx();
           
            addLog(movement.getM_Movement_ID(), movement.getMovementDate(), null, movement.getDocumentNo(), movement.get_Table_ID(), movement.getM_Movement_ID());
           
            StringBuilder sql = new StringBuilder();
           
            sql.append("SELECT 1 FROM adempiere.fnt_insert_matchmov(" + ship.get_ID() + "," + v_user_id + ");");
           
            DB.executeUpdate(sql.toString(), null);
           
            String mensaje = "*******  Registro Generado Satisfactoriamente  *******";
           
            return mensaje + "\n" + movement.getDocumentNo();
    }
}

/*****************************************************************************/

No hay comentarios:

Publicar un comentario