Search in sources :

Example 31 with MInOutLine

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

the class InOutGenerateRMA method generateShipment.

private void generateShipment(int M_RMA_ID) {
    MRMA rma = new MRMA(getCtx(), M_RMA_ID, get_TrxName());
    MInOut shipment = createShipment(rma);
    MInOutLine[] shipmentLines = createShipmentLines(rma, shipment);
    if (shipmentLines.length == 0) {
        log.log(Level.WARNING, "No shipment lines created: M_RMA_ID=" + M_RMA_ID + ", M_InOut_ID=" + shipment.get_ID());
    }
    StringBuffer processMsg = new StringBuffer(shipment.getDocumentNo());
    if (!shipment.processIt(p_docAction)) {
        processMsg.append(" (NOT Processed)");
        log.warning("Shipment Processing failed: " + shipment + " - " + shipment.getProcessMsg());
    }
    if (!shipment.save()) {
        throw new IllegalStateException("Could not update shipment");
    }
    // Add processing information to process log
    addLog(shipment.getM_InOut_ID(), shipment.getMovementDate(), null, processMsg.toString());
    m_created++;
}
Also used : MInOut(org.compiere.model.MInOut) MInOutLine(org.compiere.model.MInOutLine) MRMA(org.compiere.model.MRMA)

Example 32 with MInOutLine

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

the class InvoiceGenerateFromShipment method generate.

//@Trifon
private String generate(PreparedStatement pstmt) {
    ResultSet rs = null;
    MInvoice invoice = null;
    try {
        rs = pstmt.executeQuery();
        while (rs.next()) {
            MInOut inOut = new MInOut(getCtx(), rs, get_TrxName());
            if (//	ignore incomplete or reversals 
            !inOut.isComplete() || inOut.getDocStatus().equals(MInOut.DOCSTATUS_Reversed)) {
                continue;
            }
            MOrder order = new MOrder(getCtx(), inOut.getC_Order_ID(), get_TrxName());
            //	New Invoice Location
            if (!p_ConsolidateDocument || (invoice != null && invoice.getC_BPartner_Location_ID() != order.getBill_Location_ID())) {
                invoice = completeInvoice(invoice);
            }
            //				boolean completeOrder = MOrder.INVOICERULE_AfterOrderDelivered.equals(order.getInvoiceRule());
            //	Schedule After Delivery
            boolean doInvoice = false;
            if (MOrder.INVOICERULE_CustomerScheduleAfterDelivery.equals(order.getInvoiceRule())) {
                m_bp = new MBPartner(getCtx(), order.getBill_BPartner_ID(), null);
                if (m_bp.getC_InvoiceSchedule_ID() == 0) {
                    log.warning("BPartner has no Schedule - set to After Delivery");
                    order.setInvoiceRule(MOrder.INVOICERULE_AfterDelivery);
                    order.save();
                } else {
                    MInvoiceSchedule is = MInvoiceSchedule.get(getCtx(), m_bp.getC_InvoiceSchedule_ID(), get_TrxName());
                    if (is.canInvoice(order.getDateOrdered(), order.getGrandTotal()))
                        doInvoice = true;
                    else
                        continue;
                }
            }
            //	After Delivery - Invoice per Delivery
            if (doInvoice || MOrder.INVOICERULE_AfterDelivery.equals(order.getInvoiceRule())) {
                MInOutLine[] shipLines = inOut.getLines(false);
                for (int j = 0; j < shipLines.length; j++) {
                    MInOutLine inOutLine = shipLines[j];
                    if (!inOutLine.isInvoiced()) {
                        invoice = createInvoiceLineFromShipmentLine(invoice, order, inOut, inOutLine);
                    }
                }
                m_line += 1000;
            } else //	After Order Delivered, Immediate
            {
                throw new AdempiereException("Not supported Invoice Rules[Immediate, AfterOrderDelivered]");
            }
        }
    // for all Shipments
    } catch (Exception e) {
        log.log(Level.SEVERE, "", e);
    } finally {
        DB.close(rs, pstmt);
        pstmt = null;
    }
    completeInvoice(invoice);
    return "@Created@ = " + m_created;
}
Also used : MInOut(org.compiere.model.MInOut) MOrder(org.compiere.model.MOrder) MInOutLine(org.compiere.model.MInOutLine) AdempiereException(org.adempiere.exceptions.AdempiereException) ResultSet(java.sql.ResultSet) MInvoice(org.compiere.model.MInvoice) MInvoiceSchedule(org.compiere.model.MInvoiceSchedule) MBPartner(org.compiere.model.MBPartner) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 33 with MInOutLine

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

the class SB_InOutGenerateFromOrderLine method createLine.

//	generate
/**************************************************************************
	 * 	Create Line
	 *	@param order order
	 *	@param orderLine line
	 *	@param qty qty
	 *	@param storages storage info
	 *	@param force force delivery
	 */
private void createLine(MOrder order, MOrderLine orderLine, BigDecimal qty, MStorage[] storages, boolean force) {
    //	Complete last Shipment - can have multiple shipments
    if (m_lastC_BPartner_Location_ID != orderLine.getC_BPartner_Location_ID())
        completeShipment();
    m_lastC_BPartner_Location_ID = orderLine.getC_BPartner_Location_ID();
    //	Create New Shipment
    if (m_shipment == null) {
        m_shipment = new MInOut(order, 0, m_movementDate);
        if (p_C_DocType_ID != 0)
            m_shipment.setC_DocType_ID(p_C_DocType_ID);
        //	sets Org too
        m_shipment.setM_Warehouse_ID(orderLine.getM_Warehouse_ID());
        if (order.getC_BPartner_ID() != orderLine.getC_BPartner_ID())
            m_shipment.setC_BPartner_ID(orderLine.getC_BPartner_ID());
        if (order.getC_BPartner_Location_ID() != orderLine.getC_BPartner_Location_ID())
            m_shipment.setC_BPartner_Location_ID(orderLine.getC_BPartner_Location_ID());
        if (!m_shipment.save())
            throw new IllegalStateException("Could not create Shipment");
    }
    //	Non Inventory Lines
    if (storages == null) {
        MInOutLine line = new MInOutLine(m_shipment);
        line.setOrderLine(orderLine, 0, Env.ZERO);
        //	Correct UOM
        line.setQty(qty);
        if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0)
            line.setQtyEntered(qty.multiply(orderLine.getQtyEntered()).divide(orderLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP));
        line.setLine(m_line + orderLine.getLine());
        if (!line.save())
            throw new IllegalStateException("Could not create Shipment Line");
        log.fine(line.toString());
        return;
    }
    //	Inventory Lines
    ArrayList<MInOutLine> list = new ArrayList<MInOutLine>();
    MProduct product = (MProduct) orderLine.getM_Product();
    BigDecimal toDeliver = qty;
    for (int i = 0; i < storages.length; i++) {
        MStorage storage = storages[i];
        BigDecimal deliver = toDeliver;
        //skip negative storage record
        if (storage.getQtyOnHand().signum() < 0)
            continue;
        //	Not enough On Hand
        if (deliver.compareTo(storage.getQtyOnHand()) > 0 && //	positive storage
        storage.getQtyOnHand().signum() >= 0) {
            if (//	Adjust to OnHand Qty  
            !force || //	if force not on last location
            (force && i + 1 != storages.length))
                deliver = storage.getQtyOnHand();
        }
        if (//	zero deliver
        deliver.signum() == 0)
            continue;
        int M_Locator_ID = storage.getM_Locator_ID();
        //
        MInOutLine line = null;
        if (//      find line with Locator
        orderLine.getM_AttributeSetInstance_ID() == 0) {
            for (int ll = 0; ll < list.size(); ll++) {
                MInOutLine test = (MInOutLine) list.get(ll);
                if (test.getM_Locator_ID() == M_Locator_ID && test.getM_AttributeSetInstance_ID() == 0) {
                    line = test;
                    break;
                }
            }
        }
        if (//	new line
        line == null) {
            line = new MInOutLine(m_shipment);
            line.setOrderLine(orderLine, M_Locator_ID, order.isSOTrx() ? deliver : Env.ZERO);
            line.setQty(deliver);
            if (product != null && product.isASIMandatory(order.isSOTrx(), line.getAD_Org_ID()))
                line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
            list.add(line);
        } else
            //	existing line
            line.setQty(line.getMovementQty().add(deliver));
        if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0)
            line.setQtyEntered(line.getMovementQty().multiply(orderLine.getQtyEntered()).divide(orderLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP));
        line.setLine(m_line + orderLine.getLine());
        if (!line.save())
            throw new IllegalStateException("Could not create Shipment Line");
        log.fine("ToDeliver=" + qty + "/" + deliver + " - " + line);
        toDeliver = toDeliver.subtract(deliver);
        //      Temp adjustment, actual update happen in MInOut.completeIt
        storage.setQtyOnHand(storage.getQtyOnHand().subtract(deliver));
        //
        if (toDeliver.signum() == 0)
            break;
    }
    if (toDeliver.signum() != 0) {
        if (!force) {
            throw new IllegalStateException("Not All Delivered - Remainder=" + toDeliver);
        } else {
            MInOutLine line = new MInOutLine(m_shipment);
            line.setOrderLine(orderLine, 0, order.isSOTrx() ? toDeliver : Env.ZERO);
            line.setQty(toDeliver);
            if (!line.save())
                throw new IllegalStateException("Could not create Shipment Line");
        }
    }
}
Also used : MInOut(org.compiere.model.MInOut) MProduct(org.compiere.model.MProduct) MInOutLine(org.compiere.model.MInOutLine) ArrayList(java.util.ArrayList) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal)

Example 34 with MInOutLine

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

the class ProjectIssue method issueReceipt.

//	doIt
/**
	 *	Issue Receipt
	 *	@return Message (clear text)
	 */
private String issueReceipt() {
    MInOut inOut = new MInOut(getCtx(), m_M_InOut_ID, null);
    if (inOut.isSOTrx() || !inOut.isProcessed() || !(MInOut.DOCSTATUS_Completed.equals(inOut.getDocStatus()) || MInOut.DOCSTATUS_Closed.equals(inOut.getDocStatus())))
        throw new IllegalArgumentException("Receipt not valid - " + inOut);
    log.info(inOut.toString());
    //	Set Project of Receipt
    if (inOut.getC_Project_ID() == 0) {
        inOut.setC_Project_ID(m_project.getC_Project_ID());
        inOut.saveEx();
    } else if (inOut.getC_Project_ID() != m_project.getC_Project_ID())
        throw new IllegalArgumentException("Receipt for other Project (" + inOut.getC_Project_ID() + ")");
    MInOutLine[] inOutLines = inOut.getLines(false);
    int counter = 0;
    for (int i = 0; i < inOutLines.length; i++) {
        //	Need to have a Product
        if (inOutLines[i].getM_Product_ID() == 0)
            continue;
        //	Need to have Quantity
        if (inOutLines[i].getMovementQty() == null || inOutLines[i].getMovementQty().signum() == 0)
            continue;
        //	not issued yet
        if (projectIssueHasReceipt(inOutLines[i].getM_InOutLine_ID()))
            continue;
        //	Create Issue
        MProjectIssue pi = new MProjectIssue(m_project);
        pi.setMandatory(inOutLines[i].getM_Locator_ID(), inOutLines[i].getM_Product_ID(), inOutLines[i].getMovementQty());
        if (//	default today
        m_MovementDate != null)
            pi.setMovementDate(m_MovementDate);
        if (m_Description != null && m_Description.length() > 0)
            pi.setDescription(m_Description);
        else if (inOutLines[i].getDescription() != null)
            pi.setDescription(inOutLines[i].getDescription());
        else if (inOut.getDescription() != null)
            pi.setDescription(inOut.getDescription());
        pi.setM_InOutLine_ID(inOutLines[i].getM_InOutLine_ID());
        pi.process();
        //	Find/Create Project Line
        MProjectLine pl = null;
        MProjectLine[] pls = m_project.getLines();
        for (int ii = 0; ii < pls.length; ii++) {
            //	The Order we generated is the same as the Order of the receipt
            if (pls[ii].getC_OrderPO_ID() == inOut.getC_Order_ID() && pls[ii].getM_Product_ID() == inOutLines[i].getM_Product_ID() && //	not issued
            pls[ii].getC_ProjectIssue_ID() == 0) {
                pl = pls[ii];
                break;
            }
        }
        if (pl == null)
            pl = new MProjectLine(m_project);
        //	setIssue
        pl.setMProjectIssue(pi);
        pl.saveEx();
        addLog(pi.getLine(), pi.getMovementDate(), pi.getMovementQty(), null);
        counter++;
    }
    return "@Created@ " + counter;
}
Also used : MInOut(org.compiere.model.MInOut) MProjectIssue(org.compiere.model.MProjectIssue) MInOutLine(org.compiere.model.MInOutLine) MProjectLine(org.compiere.model.MProjectLine)

Example 35 with MInOutLine

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

the class CostResult method createMaterialReceipt.

public MInOut createMaterialReceipt(Timestamp movementDate, BigDecimal qtyMovement, int C_OrderLine_ID) {
    // Create Material Receipt
    MInOut receipt = new MInOut(getCtx(), 0, trxName);
    receipt.setAD_Org_ID(Env.getAD_Org_ID(getCtx()));
    receipt.setC_DocType_ID(MDocType.getDocType(MDocType.DOCBASETYPE_MaterialReceipt));
    receipt.setC_BPartner_ID(bp.getC_BPartner_ID());
    receipt.setC_BPartner_Location_ID(bpls[0].getC_BPartner_Location_ID());
    receipt.setMovementDate(movementDate);
    receipt.setDateAcct(movementDate);
    receipt.setIsSOTrx(false);
    receipt.setM_Warehouse_ID(w.getM_Warehouse_ID());
    receipt.setMovementType(MInOut.MOVEMENTTYPE_VendorReceipts);
    receipt.saveEx();
    // Create Material Receipt Line
    MInOutLine receiptLine = new MInOutLine(receipt);
    receiptLine.setC_OrderLine_ID(C_OrderLine_ID);
    receiptLine.setM_Product_ID(product.getM_Product_ID());
    receiptLine.setMovementQty(qtyMovement);
    receiptLine.setM_Locator_ID(qtyMovement);
    receiptLine.saveEx();
    // Complete Receipt
    receipt.setDocStatus(DocAction.STATUS_Drafted);
    receipt.setDocAction(DocAction.ACTION_Complete);
    receipt.processIt(DocAction.ACTION_Complete);
    receipt.saveEx();
    return receipt;
}
Also used : MInOut(org.compiere.model.MInOut) MInOutLine(org.compiere.model.MInOutLine)

Aggregations

MInOutLine (org.compiere.model.MInOutLine)42 MInOut (org.compiere.model.MInOut)22 BigDecimal (java.math.BigDecimal)19 MOrderLine (org.compiere.model.MOrderLine)15 MInvoiceLine (org.compiere.model.MInvoiceLine)10 MProduct (org.compiere.model.MProduct)10 MMatchPO (org.compiere.model.MMatchPO)9 MInvoice (org.compiere.model.MInvoice)8 MOrder (org.compiere.model.MOrder)8 MMatchInv (org.compiere.model.MMatchInv)7 MInventoryLine (org.compiere.model.MInventoryLine)6 MMovementLine (org.compiere.model.MMovementLine)6 MRMALine (org.compiere.model.MRMALine)6 MTransaction (org.compiere.model.MTransaction)6 ArrayList (java.util.ArrayList)5 MLandedCostAllocation (org.compiere.model.MLandedCostAllocation)5 MStorage (org.compiere.model.MStorage)5 AdempiereException (org.adempiere.exceptions.AdempiereException)4 MCostElement (org.compiere.model.MCostElement)4 MCostType (org.compiere.model.MCostType)4