Search in sources :

Example 16 with MInOut

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

the class InvoiceCreateFrom method addReference.

/**
	 * Add Reference to Order / Invoice / RMA
	 * @param invoice
	 * @param createFromType
	 * @param referenceId
     */
private void addReference(MInvoice invoice, String createFromType, int referenceId) {
    //	Valid Reference
    if (referenceId == 0)
        return;
    if (createFromType.equals(ORDER)) {
        MOrder order = new MOrder(getCtx(), referenceId, get_TrxName());
        invoice.setOrder(order);
    } else if (createFromType.equals(INVOICE)) {
        MInvoice fromInvoice = new MInvoice(getCtx(), referenceId, get_TrxName());
        invoice.setAD_OrgTrx_ID(fromInvoice.getAD_OrgTrx_ID());
        invoice.setC_Project_ID(fromInvoice.getC_Project_ID());
        invoice.setC_Campaign_ID(fromInvoice.getC_Campaign_ID());
        invoice.setC_Activity_ID(fromInvoice.getC_Activity_ID());
        invoice.setUser1_ID(fromInvoice.getUser1_ID());
        invoice.setUser2_ID(fromInvoice.getUser2_ID());
        invoice.setUser3_ID(fromInvoice.getUser3_ID());
        invoice.setUser4_ID(fromInvoice.getUser4_ID());
    } else if (createFromType.equals(RMA)) {
        MRMA rma = new MRMA(getCtx(), referenceId, get_TrxName());
        invoice.setRMA(rma);
    } else if (createFromType.equals(RECEIPT)) {
        MInOut inOut = new MInOut(getCtx(), referenceId, get_TrxName());
        invoice.setShipment(inOut);
    }
    //	Save
    invoice.saveEx();
}
Also used : MInOut(org.compiere.model.MInOut) MOrder(org.compiere.model.MOrder) MInvoice(org.compiere.model.MInvoice) MRMA(org.compiere.model.MRMA)

Example 17 with MInOut

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

the class InvoiceCreateInOut method createLine.

/**
	 * Create shipment/receipt line
	 * @param invoice
	 * @param invoiceLine
	 * @return shipment/receipt line
	 */
private MInOutLine createLine(MInvoice invoice, MInvoiceLine invoiceLine) {
    BigDecimal qtyMatched = invoiceLine.getMatchedQty();
    BigDecimal qtyInvoiced = invoiceLine.getQtyInvoiced();
    BigDecimal qtyNotMatched = qtyInvoiced.subtract(qtyMatched);
    // If is fully matched don't create anything
    if (qtyNotMatched.signum() == 0) {
        return null;
    }
    MInOut inout = getCreateHeader(invoice);
    MInOutLine sLine = new MInOutLine(inout);
    //	Locator 
    sLine.setInvoiceLine(//	Locator 
    invoiceLine, //	Locator 
    0, invoice.isSOTrx() ? qtyNotMatched : Env.ZERO);
    sLine.setQtyEntered(qtyNotMatched);
    sLine.setMovementQty(qtyNotMatched);
    if (invoice.isCreditMemo()) {
        sLine.setQtyEntered(sLine.getQtyEntered().negate());
        sLine.setMovementQty(sLine.getMovementQty().negate());
    }
    sLine.saveEx();
    //
    invoiceLine.setM_InOutLine_ID(sLine.getM_InOutLine_ID());
    invoiceLine.saveEx();
    //
    return sLine;
}
Also used : MInOut(org.compiere.model.MInOut) MInOutLine(org.compiere.model.MInOutLine) BigDecimal(java.math.BigDecimal)

Example 18 with MInOut

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

the class InvoiceGenerate method generate.

//	doIt
/**
	 * 	Generate Shipments
	 * 	@param pstmt order query 
	 *	@return info
	 */
private String generate(PreparedStatement pstmt) {
    try {
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            MOrder order = new MOrder(getCtx(), rs, get_TrxName());
            //	New Invoice Location
            if (!p_ConsolidateDocument || (m_invoice != null && m_invoice.getC_BPartner_Location_ID() != order.getBill_Location_ID()))
                completeInvoice();
            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.saveEx();
                } 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
            if (doInvoice || MOrder.INVOICERULE_AfterDelivery.equals(order.getInvoiceRule())) {
                MInOut[] shipments = order.getShipments();
                for (int i = 0; i < shipments.length; i++) {
                    MInOut ship = shipments[i];
                    if (//	ignore incomplete or reversals 
                    !ship.isComplete() || ship.getDocStatus().equals(MInOut.DOCSTATUS_Reversed))
                        continue;
                    MInOutLine[] shipLines = ship.getLines(false);
                    for (int j = 0; j < shipLines.length; j++) {
                        MInOutLine shipLine = shipLines[j];
                        if (!order.isOrderLine(shipLine.getC_OrderLine_ID()))
                            continue;
                        if (!shipLine.isInvoiced())
                            createLine(order, ship, shipLine);
                    }
                    m_line += 1000;
                }
            } else //	After Order Delivered, Immediate
            {
                MOrderLine[] oLines = order.getLines(true, null);
                for (int i = 0; i < oLines.length; i++) {
                    MOrderLine oLine = oLines[i];
                    BigDecimal toInvoice = oLine.getQtyOrdered().subtract(oLine.getQtyInvoiced());
                    if (toInvoice.compareTo(Env.ZERO) == 0 && oLine.getM_Product_ID() != 0)
                        continue;
                    BigDecimal notInvoicedShipment = oLine.getQtyDelivered().subtract(oLine.getQtyInvoiced());
                    //
                    boolean fullyDelivered = oLine.getQtyOrdered().compareTo(oLine.getQtyDelivered()) == 0;
                    //	Complete Order
                    if (completeOrder && !fullyDelivered) {
                        log.fine("Failed CompleteOrder - " + oLine);
                        // Elaine 2008/11/25
                        addLog("Failed CompleteOrder - " + oLine);
                        completeOrder = false;
                        break;
                    } else //	Immediate
                    if (MOrder.INVOICERULE_Immediate.equals(order.getInvoiceRule())) {
                        log.fine("Immediate - ToInvoice=" + toInvoice + " - " + oLine);
                        BigDecimal qtyEntered = toInvoice;
                        //	Correct UOM for QtyEntered
                        if (oLine.getQtyEntered().compareTo(oLine.getQtyOrdered()) != 0)
                            qtyEntered = toInvoice.multiply(oLine.getQtyEntered()).divide(oLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP);
                        createLine(order, oLine, toInvoice, qtyEntered);
                    } else {
                        log.fine("Failed: " + order.getInvoiceRule() + " - ToInvoice=" + toInvoice + " - " + oLine);
                        addLog("Failed: " + order.getInvoiceRule() + " - ToInvoice=" + toInvoice + " - " + oLine);
                    }
                }
                //	for all order lines
                if (MOrder.INVOICERULE_Immediate.equals(order.getInvoiceRule()))
                    m_line += 1000;
            }
            //	Complete Order successful
            if (completeOrder && MOrder.INVOICERULE_AfterOrderDelivered.equals(order.getInvoiceRule())) {
                MInOut[] shipments = order.getShipments();
                for (int i = 0; i < shipments.length; i++) {
                    MInOut ship = shipments[i];
                    if (//	ignore incomplete or reversals 
                    !ship.isComplete() || ship.getDocStatus().equals(MInOut.DOCSTATUS_Reversed))
                        continue;
                    MInOutLine[] shipLines = ship.getLines(false);
                    for (int j = 0; j < shipLines.length; j++) {
                        MInOutLine shipLine = shipLines[j];
                        if (!order.isOrderLine(shipLine.getC_OrderLine_ID()))
                            continue;
                        if (!shipLine.isInvoiced())
                            createLine(order, ship, shipLine);
                    }
                    m_line += 1000;
                }
            }
        //	complete Order
        }
        //	for all orders
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, "", e);
    }
    try {
        if (pstmt != null)
            pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        pstmt = null;
    }
    completeInvoice();
    return "@Created@ = " + m_created;
}
Also used : MInOut(org.compiere.model.MInOut) MInOutLine(org.compiere.model.MInOutLine) MInvoiceSchedule(org.compiere.model.MInvoiceSchedule) MBPartner(org.compiere.model.MBPartner) BigDecimal(java.math.BigDecimal) MOrder(org.compiere.model.MOrder) ResultSet(java.sql.ResultSet) MOrderLine(org.compiere.model.MOrderLine)

Example 19 with MInOut

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

the class ReverseTheSalesTransaction method doIt.

@Override
protected String doIt() throws Exception {
    today = new Timestamp(System.currentTimeMillis());
    // Get Order
    MOrder sourceOrder = new MOrder(getCtx(), getOrderId(), get_TrxName());
    // Get Invoices for ths order
    MInOut[] shipments = sourceOrder.getShipments();
    // If not exist invoice then only is necessary reverse shipment
    if (shipments.length > 0) {
        // Validate if partner not is POS partner standard then reverse shipment
        if (sourceOrder.getC_BPartner_ID() != getInvoicePartnerId() || isCancelled()) {
            cancelShipments(shipments);
        }
    }
    MInvoice[] invoices = sourceOrder.getInvoices();
    if (invoices.length > 0) {
        if (sourceOrder.getC_BPartner_ID() != getInvoicePartnerId() || isCancelled())
            cancelInvoices();
    }
    //Cancel original payment
    for (MPayment payment : cancelPayments(sourceOrder, today)) addLog(payment.getDocumentInfo());
    sourceOrder.processIt(DocAction.ACTION_Close);
    sourceOrder.saveEx();
    return "@Ok@";
}
Also used : MInOut(org.compiere.model.MInOut) MOrder(org.compiere.model.MOrder) MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp)

Example 20 with MInOut

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

the class ReverseTheSalesTransaction method cancelShipments.

private void cancelShipments(MInOut[] sourceShipments) {
    for (MInOut sourceShipment : sourceShipments) {
        MRMA rma = new MRMA(getCtx(), 0, get_TrxName());
        rma.setM_InOut_ID(sourceShipment.getM_InOut_ID());
        rma.setAD_Org_ID(sourceShipment.getAD_Org_ID());
        rma.setM_RMAType_ID(getRMATypeId());
        rma.setC_BPartner_ID(sourceShipment.getC_BPartner_ID());
        rma.setName(sourceShipment.getDocumentInfo());
        rma.setIsSOTrx(true);
        rma.setSalesRep_ID(sourceShipment.getSalesRep_ID());
        rma.setC_DocType_ID(MDocType.getDocTypeBaseOnSubType(sourceShipment.getAD_Org_ID(), MDocType.DOCBASETYPE_SalesOrder, MDocType.DOCSUBTYPESO_ReturnMaterial));
        rma.setDocStatus(DocAction.STATUS_Drafted);
        rma.setDocAction(DocAction.ACTION_Complete);
        rma.saveEx();
        MInOut customerReturn = new MInOut(getCtx(), 0, get_TrxName());
        PO.copyValues(sourceShipment, customerReturn);
        customerReturn.setDocumentNo(null);
        customerReturn.setM_RMA_ID(rma.getM_RMA_ID());
        customerReturn.setIsSOTrx(true);
        customerReturn.setC_BPartner_ID(sourceShipment.getC_BPartner_ID());
        customerReturn.setC_Order_ID(-1);
        for (MDocType documentType : MDocType.getOfDocBaseType(getCtx(), MDocType.DOCBASETYPE_MaterialReceipt)) if (documentType.isSOTrx())
            customerReturn.setC_DocType_ID();
        customerReturn.setMovementType(MInOut.MOVEMENTTYPE_CustomerReturns);
        customerReturn.setDocStatus(DocAction.STATUS_Drafted);
        customerReturn.setDocAction(DocAction.ACTION_Complete);
        customerReturn.setProcessed(false);
        customerReturn.saveEx();
        for (MInOutLine sourceShipmentLine : sourceShipment.getLines()) {
            MRMALine rmaLine = new MRMALine(getCtx(), 0, get_TrxName());
            rmaLine.setM_RMA_ID(rma.getM_RMA_ID());
            rmaLine.setAD_Org_ID(sourceShipmentLine.getAD_Org_ID());
            rmaLine.setM_InOutLine_ID(sourceShipmentLine.getM_InOutLine_ID());
            rmaLine.setQty(sourceShipmentLine.getMovementQty());
            rmaLine.saveEx();
            MInOutLine customerReturnLine = new MInOutLine(getCtx(), 0, get_TrxName());
            customerReturnLine.setM_InOut_ID(customerReturn.getM_InOut_ID());
            customerReturnLine.setM_RMALine_ID(rmaLine.getM_RMALine_ID());
            customerReturnLine.setM_Product_ID(rmaLine.getM_Product_ID());
            customerReturnLine.setM_Locator_ID(sourceShipmentLine.getM_Locator_ID());
            customerReturnLine.setMovementQty(rmaLine.getQty());
            customerReturnLine.saveEx();
        }
        rma.processIt(DocAction.ACTION_Complete);
        rma.saveEx();
        addLog(rma.getDocumentInfo());
        if (customerReturn.getC_DocType().isShipConfirm() && isShipReceiptConfirmation()) {
            customerReturn.processIt(DocAction.STATUS_InProgress);
            customerReturn.saveEx();
            for (MInOutConfirm confirm : customerReturn.getConfirmations(true)) {
                for (MInOutLineConfirm confirmLine : confirm.getLines(true)) {
                    confirmLine.setConfirmedQty(confirmLine.getTargetQty());
                    confirmLine.saveEx();
                }
                confirm.processIt(DocAction.ACTION_Complete);
                confirm.saveEx();
                addLog(confirm.getDocumentInfo());
            }
        }
        customerReturn.processIt(DocAction.STATUS_Completed);
        customerReturn.saveEx();
        addLog(customerReturn.getDocumentInfo());
        customerReturns.add(customerReturn);
    }
}
Also used : MInOut(org.compiere.model.MInOut) MDocType(org.compiere.model.MDocType) MInOutLine(org.compiere.model.MInOutLine) MInOutLineConfirm(org.compiere.model.MInOutLineConfirm) MRMALine(org.compiere.model.MRMALine) MInOutConfirm(org.compiere.model.MInOutConfirm) MRMA(org.compiere.model.MRMA)

Aggregations

MInOut (org.compiere.model.MInOut)38 MInOutLine (org.compiere.model.MInOutLine)22 BigDecimal (java.math.BigDecimal)13 MOrder (org.compiere.model.MOrder)13 MInvoice (org.compiere.model.MInvoice)10 MOrderLine (org.compiere.model.MOrderLine)9 ArrayList (java.util.ArrayList)6 MProduct (org.compiere.model.MProduct)5 MRMA (org.compiere.model.MRMA)5 ResultSet (java.sql.ResultSet)4 Timestamp (java.sql.Timestamp)4 MInvoiceLine (org.compiere.model.MInvoiceLine)4 MRMALine (org.compiere.model.MRMALine)4 MBPartner (org.compiere.model.MBPartner)3 MInOutConfirm (org.compiere.model.MInOutConfirm)3 MInvoiceSchedule (org.compiere.model.MInvoiceSchedule)3 MLocator (org.compiere.model.MLocator)3 MStorage (org.compiere.model.MStorage)3 PreparedStatement (java.sql.PreparedStatement)2 AdempiereException (org.adempiere.exceptions.AdempiereException)2