Search in sources :

Example 16 with MOrg

use of org.compiere.model.MOrg in project adempiere by adempiere.

the class ReplenishReportProduction method createDO.

//	Create Inventory Movements
/**
	 * 	Create Distribution Order
	 */
private void createDO() throws Exception {
    int noMoves = 0;
    String info = "";
    //
    MClient client = null;
    MDDOrder order = null;
    int M_Warehouse_ID = 0;
    int M_WarehouseSource_ID = 0;
    MWarehouse whSource = null;
    MWarehouse wh = null;
    X_T_Replenish[] replenishs = getReplenish("M_WarehouseSource_ID IS NOT NULL");
    for (X_T_Replenish replenish : replenishs) {
        if (whSource == null || whSource.getM_WarehouseSource_ID() != replenish.getM_WarehouseSource_ID())
            whSource = MWarehouse.get(getCtx(), replenish.getM_WarehouseSource_ID());
        if (wh == null || wh.getM_Warehouse_ID() != replenish.getM_Warehouse_ID())
            wh = MWarehouse.get(getCtx(), replenish.getM_Warehouse_ID());
        if (client == null || client.getAD_Client_ID() != whSource.getAD_Client_ID())
            client = MClient.get(getCtx(), whSource.getAD_Client_ID());
        //
        if (order == null || M_WarehouseSource_ID != replenish.getM_WarehouseSource_ID() || M_Warehouse_ID != replenish.getM_Warehouse_ID()) {
            M_WarehouseSource_ID = replenish.getM_WarehouseSource_ID();
            M_Warehouse_ID = replenish.getM_Warehouse_ID();
            order = new MDDOrder(getCtx(), 0, get_TrxName());
            order.setC_DocType_ID(p_C_DocType_ID);
            order.setDescription(Msg.getMsg(getCtx(), "Replenishment") + ": " + whSource.getName() + "->" + wh.getName());
            //	Set Org
            order.setAD_Org_ID(whSource.getAD_Org_ID());
            // Set Org Trx
            MOrg orgTrx = MOrg.get(getCtx(), wh.getAD_Org_ID());
            order.setAD_OrgTrx_ID(orgTrx.getAD_Org_ID());
            int C_BPartner_ID = orgTrx.getLinkedC_BPartner_ID(get_TrxName());
            if (C_BPartner_ID == 0)
                throw new AdempiereUserError(Msg.translate(getCtx(), "C_BPartner_ID") + " @FillMandatory@ ");
            MBPartner bp = new MBPartner(getCtx(), C_BPartner_ID, get_TrxName());
            // Set BPartner Link to Org
            order.setBPartner(bp);
            order.setDateOrdered(new Timestamp(System.currentTimeMillis()));
            //order.setDatePromised(DatePromised);
            order.setDeliveryRule(MDDOrder.DELIVERYRULE_Availability);
            order.setDeliveryViaRule(MDDOrder.DELIVERYVIARULE_Delivery);
            order.setPriorityRule(MDDOrder.PRIORITYRULE_Medium);
            order.setIsInDispute(false);
            order.setIsApproved(false);
            order.setIsDropShip(false);
            order.setIsDelivered(false);
            order.setIsInTransit(false);
            order.setIsPrinted(false);
            order.setIsSelected(false);
            order.setIsSOTrx(false);
            // Warehouse in Transit
            MWarehouse[] whsInTransit = MWarehouse.getForOrg(getCtx(), whSource.getAD_Org_ID());
            for (MWarehouse whInTransit : whsInTransit) {
                if (whInTransit.isInTransit())
                    order.setM_Warehouse_ID(whInTransit.getM_Warehouse_ID());
            }
            if (order.getM_Warehouse_ID() == 0)
                throw new AdempiereUserError("Warehouse inTransit is @FillMandatory@ ");
            if (!order.save())
                return;
            log.fine(order.toString());
            noMoves++;
            info += " - " + order.getDocumentNo();
        }
        //	To
        int M_LocatorTo_ID = wh.getDefaultLocator().getM_Locator_ID();
        int M_Locator_ID = whSource.getDefaultLocator().getM_Locator_ID();
        if (M_LocatorTo_ID == 0 || M_Locator_ID == 0)
            throw new AdempiereUserError(Msg.translate(getCtx(), "M_Locator_ID") + " @FillMandatory@ ");
        //	From: Look-up Storage
        /*MProduct product = MProduct.get(getCtx(), replenish.getM_Product_ID());
			MProductCategory pc = MProductCategory.get(getCtx(), product.getM_Product_Category_ID());
			String MMPolicy = pc.getMMPolicy();
			if (MMPolicy == null || MMPolicy.length() == 0)
				MMPolicy = client.getMMPolicy();
			//
			MStorage[] storages = MStorage.getWarehouse(getCtx(), 
				whSource.getM_Warehouse_ID(), replenish.getM_Product_ID(), 0, 0,
				true, null, 
				MClient.MMPOLICY_FiFo.equals(MMPolicy), get_TrxName());
			
			
			BigDecimal target = replenish.getQtyToOrder();
			for (int j = 0; j < storages.length; j++)
			{
				MStorage storage = storages[j];
				if (storage.getQtyOnHand().signum() <= 0)
					continue;
				BigDecimal moveQty = target;
				if (storage.getQtyOnHand().compareTo(moveQty) < 0)
					moveQty = storage.getQtyOnHand();
				//
				MDDOrderLine line = new MDDOrderLine(order);
				line.setM_Product_ID(replenish.getM_Product_ID());
				line.setQtyEntered(moveQty);
				if (replenish.getQtyToOrder().compareTo(moveQty) != 0)
					line.setDescription("Total: " + replenish.getQtyToOrder());
				line.setM_Locator_ID(storage.getM_Locator_ID());		//	from
				line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
				line.setM_LocatorTo_ID(M_LocatorTo_ID);					//	to
				line.setM_AttributeSetInstanceTo_ID(storage.getM_AttributeSetInstance_ID());
				line.setIsInvoiced(false);
				line.save();
				//
				target = target.subtract(moveQty);
				if (target.signum() == 0)
					break;
			}*/
        MDDOrderLine line = new MDDOrderLine(order);
        line.setM_Product_ID(replenish.getM_Product_ID());
        line.setQty(replenish.getQtyToOrder());
        if (replenish.getQtyToOrder().compareTo(replenish.getQtyToOrder()) != 0)
            line.setDescription("Total: " + replenish.getQtyToOrder());
        //	from
        line.setM_Locator_ID(M_Locator_ID);
        line.setM_AttributeSetInstance_ID(0);
        //	to
        line.setM_LocatorTo_ID(M_LocatorTo_ID);
        line.setM_AttributeSetInstanceTo_ID(0);
        line.setIsInvoiced(false);
        line.save();
    }
    if (replenishs.length == 0) {
        m_info = "No Source Warehouse";
        log.warning(m_info);
    } else {
        m_info = "#" + noMoves + info;
        log.info(m_info);
    }
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MBPartner(org.compiere.model.MBPartner) Timestamp(java.sql.Timestamp) MWarehouse(org.compiere.model.MWarehouse) MClient(org.compiere.model.MClient) MDDOrderLine(org.eevolution.model.MDDOrderLine) MOrg(org.compiere.model.MOrg) X_T_Replenish(org.compiere.model.X_T_Replenish) MDDOrder(org.eevolution.model.MDDOrder)

Example 17 with MOrg

use of org.compiere.model.MOrg in project adempiere by adempiere.

the class InvoiceNGL method createGLJournal.

//	doIt
/**
	 * 	Create GL Journal
	 * 	@return document info
	 */
private String createGLJournal() {
    //FR: [ 2214883 ] Remove SQL code and Replace for Query
    final String whereClause = "AD_PInstance_ID=?";
    List<X_T_InvoiceGL> list = new Query(getCtx(), X_T_InvoiceGL.Table_Name, whereClause, get_TrxName()).setParameters(getAD_PInstance_ID()).setOrderBy("AD_Org_ID").list();
    if (list.size() == 0)
        return " - No Records found";
    //
    MAcctSchema as = MAcctSchema.get(getCtx(), p_C_AcctSchema_ID);
    MAcctSchemaDefault asDefaultAccts = MAcctSchemaDefault.get(getCtx(), p_C_AcctSchema_ID);
    MGLCategory cat = MGLCategory.getDefaultSystem(getCtx());
    if (cat == null) {
        MDocType docType = MDocType.get(getCtx(), p_C_DocTypeReval_ID);
        cat = MGLCategory.get(getCtx(), docType.getGL_Category_ID());
    }
    //
    MJournalBatch batch = new MJournalBatch(getCtx(), 0, get_TrxName());
    batch.setDescription(getName());
    batch.setC_DocType_ID(p_C_DocTypeReval_ID);
    batch.setDateDoc(new Timestamp(System.currentTimeMillis()));
    batch.setDateAcct(p_DateReval);
    batch.setC_Currency_ID(as.getC_Currency_ID());
    if (!batch.save())
        return " - Could not create Batch";
    //
    MJournal journal = null;
    BigDecimal drTotal = Env.ZERO;
    BigDecimal crTotal = Env.ZERO;
    int AD_Org_ID = 0;
    for (int i = 0; i < list.size(); i++) {
        X_T_InvoiceGL gl = list.get(i);
        if (gl.getAmtRevalDrDiff().signum() == 0 && gl.getAmtRevalCrDiff().signum() == 0)
            continue;
        MInvoice invoice = new MInvoice(getCtx(), gl.getC_Invoice_ID(), null);
        if (invoice.getC_Currency_ID() == as.getC_Currency_ID())
            continue;
        //
        if (journal == null) {
            journal = new MJournal(batch);
            journal.setC_AcctSchema_ID(as.getC_AcctSchema_ID());
            journal.setC_Currency_ID(as.getC_Currency_ID());
            journal.setC_ConversionType_ID(p_C_ConversionTypeReval_ID);
            MOrg org = MOrg.get(getCtx(), gl.getAD_Org_ID());
            journal.setDescription(getName() + " - " + org.getName());
            journal.setGL_Category_ID(cat.getGL_Category_ID());
            if (!journal.save())
                return " - Could not create Journal";
        }
        //
        MJournalLine line = new MJournalLine(journal);
        line.setLine((i + 1) * 10);
        line.setDescription(invoice.getSummary());
        //
        MFactAcct fa = new MFactAcct(getCtx(), gl.getFact_Acct_ID(), null);
        line.setC_ValidCombination_ID(MAccount.get(fa));
        BigDecimal dr = gl.getAmtRevalDrDiff();
        BigDecimal cr = gl.getAmtRevalCrDiff();
        drTotal = drTotal.add(dr);
        crTotal = crTotal.add(cr);
        line.setAmtSourceDr(dr);
        line.setAmtAcctDr(dr);
        line.setAmtSourceCr(cr);
        line.setAmtAcctCr(cr);
        line.saveEx();
        //
        if (//	invoice org id
        AD_Org_ID == 0)
            AD_Org_ID = gl.getAD_Org_ID();
        //	Change in Org
        if (AD_Org_ID != gl.getAD_Org_ID()) {
            createBalancing(asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (i + 1) * 10);
            //
            AD_Org_ID = gl.getAD_Org_ID();
            drTotal = Env.ZERO;
            crTotal = Env.ZERO;
            journal = null;
        }
    }
    createBalancing(asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (list.size() + 1) * 10);
    return " - " + batch.getDocumentNo() + " #" + list.size();
}
Also used : MDocType(org.compiere.model.MDocType) Query(org.compiere.model.Query) X_T_InvoiceGL(org.compiere.model.X_T_InvoiceGL) MGLCategory(org.compiere.model.MGLCategory) MJournalLine(org.compiere.model.MJournalLine) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MAcctSchema(org.compiere.model.MAcctSchema) MOrg(org.compiere.model.MOrg) MAcctSchemaDefault(org.compiere.model.MAcctSchemaDefault) MFactAcct(org.compiere.model.MFactAcct) MJournal(org.compiere.model.MJournal) MJournalBatch(org.compiere.model.MJournalBatch)

Example 18 with MOrg

use of org.compiere.model.MOrg in project adempiere by adempiere.

the class DistributionRun method createOrders.

//	adjustAllocation
/**************************************************************************
	 * 	Create Orders
	 * 	@return true if created
	 */
private boolean createOrders() {
    //	Get Counter Org/BP
    int runAD_Org_ID = m_run.getAD_Org_ID();
    if (runAD_Org_ID == 0)
        runAD_Org_ID = Env.getAD_Org_ID(getCtx());
    MOrg runOrg = MOrg.get(getCtx(), runAD_Org_ID);
    int runC_BPartner_ID = runOrg.getLinkedC_BPartner_ID(get_TrxName());
    boolean counter = //	no single Order 
    !m_run.isCreateSingleOrder() && //	Org linked to BP
    runC_BPartner_ID > 0 && //	PO
    !m_docType.isSOTrx();
    MBPartner runBPartner = counter ? new MBPartner(getCtx(), runC_BPartner_ID, get_TrxName()) : null;
    if (!counter || runBPartner == null || runBPartner.get_ID() != runC_BPartner_ID)
        counter = false;
    if (counter)
        log.info("RunBP=" + runBPartner + " - " + m_docType);
    log.info("Single=" + m_run.isCreateSingleOrder() + " - " + m_docType + ",SO=" + m_docType.isSOTrx());
    log.fine("Counter=" + counter + ",C_BPartner_ID=" + runC_BPartner_ID + "," + runBPartner);
    //
    MBPartner bp = null;
    MOrder singleOrder = null;
    MProduct product = null;
    //	Consolidated Order
    if (m_run.isCreateSingleOrder()) {
        bp = new MBPartner(getCtx(), m_run.getC_BPartner_ID(), get_TrxName());
        if (bp.get_ID() == 0)
            throw new IllegalArgumentException("Business Partner not found - C_BPartner_ID=" + m_run.getC_BPartner_ID());
        //
        if (!p_IsTest) {
            singleOrder = new MOrder(getCtx(), 0, get_TrxName());
            singleOrder.setC_DocTypeTarget_ID(m_docType.getC_DocType_ID());
            singleOrder.setC_DocType_ID(m_docType.getC_DocType_ID());
            singleOrder.setIsSOTrx(m_docType.isSOTrx());
            singleOrder.setBPartner(bp);
            if (m_run.getC_BPartner_Location_ID() != 0)
                singleOrder.setC_BPartner_Location_ID(m_run.getC_BPartner_Location_ID());
            singleOrder.setDateOrdered(m_DateOrdered);
            singleOrder.setDatePromised(p_DatePromised);
            if (!singleOrder.save()) {
                log.log(Level.SEVERE, "Order not saved");
                return false;
            }
            m_counter++;
        }
    }
    int lastC_BPartner_ID = 0;
    int lastC_BPartner_Location_ID = 0;
    MOrder order = null;
    //	For all lines
    for (int i = 0; i < m_details.length; i++) {
        MDistributionRunDetail detail = m_details[i];
        //	Create Order Header
        if (m_run.isCreateSingleOrder())
            order = singleOrder;
        else //	New Business Partner
        if (lastC_BPartner_ID != detail.getC_BPartner_ID() || lastC_BPartner_Location_ID != detail.getC_BPartner_Location_ID()) {
            //	finish order
            order = null;
        }
        lastC_BPartner_ID = detail.getC_BPartner_ID();
        lastC_BPartner_Location_ID = detail.getC_BPartner_Location_ID();
        //	New Order
        if (order == null) {
            bp = new MBPartner(getCtx(), detail.getC_BPartner_ID(), get_TrxName());
            if (!p_IsTest) {
                order = new MOrder(getCtx(), 0, get_TrxName());
                order.setC_DocTypeTarget_ID(m_docType.getC_DocType_ID());
                order.setC_DocType_ID(m_docType.getC_DocType_ID());
                order.setIsSOTrx(m_docType.isSOTrx());
                //	Counter Doc
                if (counter && bp.getAD_OrgBP_ID_Int() > 0) {
                    log.fine("Counter - From_BPOrg=" + bp.getAD_OrgBP_ID_Int() + "-" + bp + ", To_BP=" + runBPartner);
                    order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
                    MOrgInfo oi = MOrgInfo.get(getCtx(), bp.getAD_OrgBP_ID_Int(), get_TrxName());
                    if (oi.getM_Warehouse_ID() > 0)
                        order.setM_Warehouse_ID(oi.getM_Warehouse_ID());
                    order.setBPartner(runBPartner);
                } else //	normal
                {
                    log.fine("From_Org=" + runAD_Org_ID + ", To_BP=" + bp);
                    order.setAD_Org_ID(runAD_Org_ID);
                    order.setBPartner(bp);
                    if (detail.getC_BPartner_Location_ID() != 0)
                        order.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID());
                }
                order.setDateOrdered(m_DateOrdered);
                order.setDatePromised(p_DatePromised);
                if (!order.save()) {
                    log.log(Level.SEVERE, "Order not saved");
                    return false;
                }
            }
        }
        //	Line
        if (product == null || product.getM_Product_ID() != detail.getM_Product_ID())
            product = MProduct.get(getCtx(), detail.getM_Product_ID());
        if (p_IsTest) {
            addLog(0, null, detail.getActualAllocation(), bp.getName() + " - " + product.getName());
            continue;
        }
        //	Create Order Line
        MOrderLine line = new MOrderLine(order);
        if (counter && bp.getAD_OrgBP_ID_Int() > 0)
            //	don't overwrite counter doc
            ;
        else //	normal - optionally overwrite
        {
            line.setC_BPartner_ID(detail.getC_BPartner_ID());
            if (detail.getC_BPartner_Location_ID() != 0)
                line.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID());
        }
        //
        line.setProduct(product);
        line.setQty(detail.getActualAllocation());
        line.setPrice();
        if (!line.save()) {
            log.log(Level.SEVERE, "OrderLine not saved");
            return false;
        }
        addLog(0, null, detail.getActualAllocation(), order.getDocumentNo() + ": " + bp.getName() + " - " + product.getName());
    }
    //	finish order
    order = null;
    return true;
}
Also used : MOrder(org.compiere.model.MOrder) MProduct(org.compiere.model.MProduct) MDistributionRunDetail(org.compiere.model.MDistributionRunDetail) MOrg(org.compiere.model.MOrg) MOrgInfo(org.compiere.model.MOrgInfo) MBPartner(org.compiere.model.MBPartner) MOrderLine(org.compiere.model.MOrderLine)

Aggregations

MOrg (org.compiere.model.MOrg)18 MBPartner (org.compiere.model.MBPartner)8 MWarehouse (org.compiere.model.MWarehouse)8 BigDecimal (java.math.BigDecimal)7 Query (org.compiere.model.Query)5 MDDOrder (org.eevolution.model.MDDOrder)5 MDDOrderLine (org.eevolution.model.MDDOrderLine)5 MOrgInfo (org.compiere.model.MOrgInfo)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Timestamp (java.sql.Timestamp)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MClient (org.compiere.model.MClient)3 MLocator (org.compiere.model.MLocator)3 SQLException (java.sql.SQLException)2 Vector (java.util.Vector)2 IDColumn (org.compiere.minigrid.IDColumn)2 MActivity (org.compiere.model.MActivity)2 MBPartnerLocation (org.compiere.model.MBPartnerLocation)2 MCampaign (org.compiere.model.MCampaign)2