Search in sources :

Example 16 with MInvoice

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

the class AllocationAuto method allocateBPOneToOne.

//	allocateIndividualPayments
/**
	 * 	Allocate Payment:Invoice 1:1
	 *	@return allocations
	 */
private int allocateBPOneToOne() throws Exception {
    int count = 0;
    for (int p = 0; p < m_payments.length; p++) {
        MPayment payment = m_payments[p];
        if (payment.isAllocated())
            continue;
        BigDecimal allocatedAmt = payment.getAllocatedAmt();
        log.info(payment + ", Allocated=" + allocatedAmt);
        if (allocatedAmt != null && allocatedAmt.signum() != 0)
            continue;
        BigDecimal availableAmt = payment.getPayAmt().add(payment.getDiscountAmt()).add(payment.getWriteOffAmt()).add(payment.getOverUnderAmt());
        if (!payment.isReceipt())
            availableAmt = availableAmt.negate();
        log.fine("Available=" + availableAmt);
        for (int i = 0; i < m_invoices.length; i++) {
            MInvoice invoice = m_invoices[i];
            if (invoice == null || invoice.isPaid())
                continue;
            if (payment.getC_Currency_ID() == invoice.getC_Currency_ID()) {
                //	log.fine("allocateBPartnerAll - " + invoice);
                BigDecimal openAmt = invoice.getOpenAmt(true, null);
                if (!invoice.isSOTrx())
                    openAmt = openAmt.negate();
                BigDecimal difference = availableAmt.subtract(openAmt).abs();
                log.fine(invoice + ", Open=" + openAmt + " - Difference=" + difference);
                if (difference.signum() == 0) {
                    Timestamp dateAcct = payment.getDateAcct();
                    if (invoice.getDateAcct().after(dateAcct))
                        dateAcct = invoice.getDateAcct();
                    if (!createAllocation(payment.getC_Currency_ID(), "1:1 (" + availableAmt + ")", dateAcct, availableAmt, null, null, null, invoice.getC_BPartner_ID(), payment.getC_Payment_ID(), invoice.getC_Invoice_ID(), invoice.getAD_Org_ID())) {
                        throw new AdempiereSystemError("Cannot create Allocation");
                    }
                    processAllocation();
                    count++;
                    //	remove invoice
                    m_invoices[i] = null;
                    m_payments[p] = null;
                    payment = null;
                    break;
                }
            } else //	Multi-Currency
            {
            }
        }
    //	for all invoices
    }
    //	for all payments
    return count;
}
Also used : AdempiereSystemError(org.compiere.util.AdempiereSystemError) MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 17 with MInvoice

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

the class AllocationAuto method allocateBPartnerAll.

//	allocateOneToOne
/**
	 * 	Allocate all Payments/Invoices using Accounting currency
	 *	@return allocations
	 */
private int allocateBPartnerAll() throws Exception {
    int C_Currency_ID = MClient.get(getCtx()).getC_Currency_ID();
    Timestamp dateAcct = null;
    //	Payments
    BigDecimal totalPayments = Env.ZERO;
    for (int p = 0; p < m_payments.length; p++) {
        MPayment payment = m_payments[p];
        if (payment.isAllocated())
            continue;
        BigDecimal allocatedAmt = payment.getAllocatedAmt();
        //	log.info("allocateBPartnerAll - " + payment + ", Allocated=" + allocatedAmt);
        if (allocatedAmt != null && allocatedAmt.signum() != 0)
            continue;
        BigDecimal availableAmt = payment.getPayAmt().add(payment.getDiscountAmt()).add(payment.getWriteOffAmt()).add(payment.getOverUnderAmt());
        if (!payment.isReceipt())
            availableAmt = availableAmt.negate();
        //	Foreign currency
        if (payment.getC_Currency_ID() != C_Currency_ID)
            continue;
        //	log.fine("allocateBPartnerAll - Available=" + availableAmt);
        if (dateAcct == null || payment.getDateAcct().after(dateAcct))
            dateAcct = payment.getDateAcct();
        totalPayments = totalPayments.add(availableAmt);
    }
    //	Invoices
    BigDecimal totalInvoices = Env.ZERO;
    for (int i = 0; i < m_invoices.length; i++) {
        MInvoice invoice = m_invoices[i];
        if (invoice.isPaid())
            continue;
        //	log.info("allocateBPartnerAll - " + invoice);
        BigDecimal openAmt = invoice.getOpenAmt(true, null);
        if (!invoice.isSOTrx())
            openAmt = openAmt.negate();
        //	Foreign currency
        if (invoice.getC_Currency_ID() != C_Currency_ID)
            continue;
        //	log.fine("allocateBPartnerAll - Open=" + openAmt);
        if (dateAcct == null || invoice.getDateAcct().after(dateAcct))
            dateAcct = invoice.getDateAcct();
        totalInvoices = totalInvoices.add(openAmt);
    }
    BigDecimal difference = totalInvoices.subtract(totalPayments);
    log.info("= Invoices=" + totalInvoices + " - Payments=" + totalPayments + " = Difference=" + difference);
    if (difference.signum() == 0) {
        for (int p = 0; p < m_payments.length; p++) {
            MPayment payment = m_payments[p];
            if (payment.isAllocated())
                continue;
            BigDecimal allocatedAmt = payment.getAllocatedAmt();
            if (allocatedAmt != null && allocatedAmt.signum() != 0)
                continue;
            BigDecimal availableAmt = payment.getPayAmt().add(payment.getDiscountAmt()).add(payment.getWriteOffAmt()).add(payment.getOverUnderAmt());
            if (!payment.isReceipt())
                availableAmt = availableAmt.negate();
            //	Foreign currency
            if (payment.getC_Currency_ID() != C_Currency_ID)
                continue;
            if (!createAllocation(C_Currency_ID, "BP All", dateAcct, availableAmt, null, null, null, payment.getC_BPartner_ID(), payment.getC_Payment_ID(), 0, payment.getAD_Org_ID())) {
                throw new AdempiereSystemError("Cannot create Allocation");
            }
        }
        //
        for (int i = 0; i < m_invoices.length; i++) {
            MInvoice invoice = m_invoices[i];
            if (invoice.isPaid())
                continue;
            BigDecimal openAmt = invoice.getOpenAmt(true, null);
            if (!invoice.isSOTrx())
                openAmt = openAmt.negate();
            //	Foreign currency
            if (invoice.getC_Currency_ID() != C_Currency_ID)
                continue;
            if (!createAllocation(C_Currency_ID, "BP All", dateAcct, openAmt, null, null, null, invoice.getC_BPartner_ID(), 0, invoice.getC_Invoice_ID(), invoice.getAD_Org_ID())) {
                throw new AdempiereSystemError("Cannot create Allocation");
            }
        }
        //	for all invoices
        processAllocation();
        return 1;
    }
    return 0;
}
Also used : AdempiereSystemError(org.compiere.util.AdempiereSystemError) MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 18 with MInvoice

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

the class WebInfo method getCommissionedInvoices.

//	getAllAds
/**
	 * 	Get Commissioned Invoices
	 *	@return commissioned invoices 
	 */
public ArrayList<MInvoice> getCommissionedInvoices() {
    m_infoMessage = null;
    ArrayList<MInvoice> list = new ArrayList<MInvoice>();
    String sql = "SELECT * FROM C_Invoice " + //	#1
    "WHERE (C_Invoice.SalesRep_ID=?" + " OR EXISTS (SELECT * FROM C_BPartner bp WHERE C_Invoice.C_BPartner_ID=bp.C_BPartner_ID AND bp.SalesRep_ID=?)" + " OR EXISTS (SELECT * FROM C_InvoiceLine il INNER JOIN M_Product p ON (il.M_Product_ID=p.M_Product_ID)  WHERE C_Invoice.C_Invoice_ID=il.C_Invoice_ID AND p.SalesRep_ID=?))" + " AND DocStatus NOT IN ('DR','IN') " + "ORDER BY DocumentNo DESC";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, getAD_User_ID());
        pstmt.setInt(2, getAD_User_ID());
        pstmt.setInt(3, getAD_User_ID());
        rs = pstmt.executeQuery();
        while (rs.next()) list.add(new MInvoice(m_ctx, rs, null));
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    log.fine("#" + list.size());
    return list;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) MInvoice(org.compiere.model.MInvoice) PreparedStatement(java.sql.PreparedStatement)

Example 19 with MInvoice

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

the class InOutCreateFrom method doIt.

@Override
protected String doIt() throws Exception {
    // Valid Record Identifier
    if (getRecord_ID() == 0)
        return "";
    AtomicInteger referenceId = new AtomicInteger(0);
    AtomicInteger created = new AtomicInteger(0);
    //	Get Shipment
    MInOut inout = new MInOut(getCtx(), getRecord_ID(), get_TrxName());
    log.config(inout + ", C_Locator_ID=" + getLocator());
    //	Get Default Locator
    MLocator defaultLocator = MLocator.getDefault((MWarehouse) inout.getM_Warehouse());
    List<Integer> recordIds = getSelectionKeys();
    String createFromType = recordIds.size() > 0 ? getSelectionAsString(recordIds.get(0), "CF_CreateFromType") : null;
    log.fine("CreateFromType=" + createFromType);
    if (createFromType == null || createFromType.length() == 0)
        throw new AdempiereException("@CreateFromType@ @NotFound@");
    //	Loop
    recordIds.stream().forEach(key -> {
        int productId = getSelectionAsInt(key, "CF_M_Product_ID");
        int chargeId = getSelectionAsInt(key, "CF_C_Charge_ID");
        int uomId = getSelectionAsInt(key, "CF_C_UOM_ID");
        int locatorId = getSelectionAsInt(key, "CF_M_Locator_ID");
        BigDecimal qtyEntered = getSelectionAsBigDecimal(key, "CF_QtyEntered");
        locatorId = getValidLocator(locatorId, defaultLocator);
        MInvoiceLine invoiceLine = null;
        int precision = 2;
        if (productId != 0) {
            MProduct product = MProduct.get(getCtx(), productId);
            precision = product.getUOMPrecision();
        }
        qtyEntered = qtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
        log.fine("Line QtyEntered=" + qtyEntered + ", Product=" + productId + ", Key=" + key);
        MInOutLine inOutLine = new MInOutLine(inout);
        inOutLine.setM_Product_ID(productId, uomId);
        inOutLine.setQty(qtyEntered);
        if (createFromType.equals(ORDER)) {
            MOrderLine orderLine = new MOrderLine(getCtx(), key, get_TrxName());
            referenceId.set(orderLine.getC_Order_ID());
            inOutLine.setC_OrderLine_ID(key);
            if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0) {
                inOutLine.setMovementQty(qtyEntered.multiply(orderLine.getQtyOrdered()).divide(orderLine.getQtyEntered(), 12, BigDecimal.ROUND_HALF_UP));
                inOutLine.setC_UOM_ID(orderLine.getC_UOM_ID());
            }
            inOutLine.setM_AttributeSetInstance_ID(orderLine.getM_AttributeSetInstance_ID());
            inOutLine.setDescription(orderLine.getDescription());
            inOutLine.setC_Project_ID(orderLine.getC_Project_ID());
            inOutLine.setC_ProjectPhase_ID(orderLine.getC_ProjectPhase_ID());
            inOutLine.setC_ProjectTask_ID(orderLine.getC_ProjectTask_ID());
            inOutLine.setC_Activity_ID(orderLine.getC_Activity_ID());
            inOutLine.setC_Campaign_ID(orderLine.getC_Campaign_ID());
            inOutLine.setAD_OrgTrx_ID(orderLine.getAD_OrgTrx_ID());
            inOutLine.setUser1_ID(orderLine.getUser1_ID());
            inOutLine.setUser2_ID(orderLine.getUser2_ID());
            inOutLine.setUser3_ID(orderLine.getUser3_ID());
            inOutLine.setUser4_ID(orderLine.getUser4_ID());
        } else if (createFromType.equals(INVOICE)) {
            invoiceLine = new MInvoiceLine(getCtx(), key, get_TrxName());
            MInvoice invoice = invoiceLine.getParent();
            referenceId.getAndSet(invoice.getC_Invoice_ID());
            if (invoice.isCreditMemo()) {
                qtyEntered = qtyEntered.negate();
                inOutLine.setQty(qtyEntered);
            }
            if (invoiceLine.getQtyEntered().compareTo(invoiceLine.getQtyInvoiced()) != 0) {
                inOutLine.setMovementQty(qtyEntered.multiply(invoiceLine.getQtyInvoiced()).divide(invoiceLine.getQtyEntered(), 12, BigDecimal.ROUND_HALF_UP));
                inOutLine.setC_UOM_ID(invoiceLine.getC_UOM_ID());
            }
            inOutLine.setDescription(invoiceLine.getDescription());
            inOutLine.setC_Project_ID(invoiceLine.getC_Project_ID());
            inOutLine.setC_ProjectPhase_ID(invoiceLine.getC_ProjectPhase_ID());
            inOutLine.setC_ProjectTask_ID(invoiceLine.getC_ProjectTask_ID());
            inOutLine.setC_Activity_ID(invoiceLine.getC_Activity_ID());
            inOutLine.setC_Campaign_ID(invoiceLine.getC_Campaign_ID());
            inOutLine.setAD_OrgTrx_ID(invoiceLine.getAD_OrgTrx_ID());
            inOutLine.setUser1_ID(invoiceLine.getUser1_ID());
            inOutLine.setUser2_ID(invoiceLine.getUser2_ID());
            inOutLine.setUser3_ID(invoiceLine.getUser3_ID());
            inOutLine.setUser4_ID(invoiceLine.getUser4_ID());
        } else if (createFromType.equals(RMA)) {
            MRMALine rmal = new MRMALine(getCtx(), key, get_TrxName());
            referenceId.set(rmal.getM_RMA_ID());
            inOutLine.setM_RMALine_ID(key);
            inOutLine.setQtyEntered(qtyEntered);
            inOutLine.setDescription(rmal.getDescription());
            inOutLine.setM_AttributeSetInstance_ID(rmal.getM_AttributeSetInstance_ID());
            inOutLine.setC_Project_ID(rmal.getC_Project_ID());
            inOutLine.setC_ProjectPhase_ID(rmal.getC_ProjectPhase_ID());
            inOutLine.setC_ProjectTask_ID(rmal.getC_ProjectTask_ID());
            inOutLine.setC_Activity_ID(rmal.getC_Activity_ID());
            inOutLine.setAD_OrgTrx_ID(rmal.getAD_OrgTrx_ID());
            inOutLine.setUser1_ID(rmal.getUser1_ID());
            inOutLine.setUser2_ID(rmal.getUser2_ID());
            inOutLine.setUser3_ID(rmal.getUser3_ID());
            inOutLine.setUser4_ID(rmal.getUser4_ID());
        }
        if (chargeId != 0)
            inOutLine.setC_Charge_ID(chargeId);
        inOutLine.setM_Locator_ID(locatorId);
        inOutLine.saveEx();
        if (invoiceLine != null) {
            invoiceLine.setM_InOutLine_ID(inOutLine.getM_InOutLine_ID());
            invoiceLine.saveEx();
        }
        created.updateAndGet(createNo -> createNo + 1);
    });
    //	Add reference to Order / Invoice / RMA
    addReference(inout, createFromType, referenceId.get());
    //	
    return "@Created@ " + created.get();
}
Also used : MInOut(org.compiere.model.MInOut) MProduct(org.compiere.model.MProduct) MInOutLine(org.compiere.model.MInOutLine) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdempiereException(org.adempiere.exceptions.AdempiereException) MLocator(org.compiere.model.MLocator) MOrderLine(org.compiere.model.MOrderLine) MRMALine(org.compiere.model.MRMALine)

Example 20 with MInvoice

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

the class InOutCreateFrom method addReference.

/**
	 * Add Reference to Order / Invoice / RMA
	 * @param inout
	 * @param createFromType
	 * @param referenceId
     */
private void addReference(MInOut inout, String createFromType, int referenceId) {
    //	Valid Reference
    if (referenceId == 0)
        return;
    if (createFromType.equals(ORDER)) {
        MOrder order = new MOrder(getCtx(), referenceId, get_TrxName());
        inout.setC_Order_ID(order.getC_Order_ID());
        inout.setAD_OrgTrx_ID(order.getAD_OrgTrx_ID());
        inout.setC_Project_ID(order.getC_Project_ID());
        inout.setC_Campaign_ID(order.getC_Campaign_ID());
        inout.setC_Activity_ID(order.getC_Activity_ID());
        inout.setUser1_ID(order.getUser1_ID());
        inout.setUser2_ID(order.getUser2_ID());
        inout.setUser3_ID(order.getUser3_ID());
        inout.setUser4_ID(order.getUser4_ID());
        //	For Drop Ship
        if (order.isDropShip()) {
            inout.setM_Warehouse_ID(order.getM_Warehouse_ID());
            inout.setIsDropShip(order.isDropShip());
            inout.setDropShip_BPartner_ID(order.getDropShip_BPartner_ID());
            inout.setDropShip_Location_ID(order.getDropShip_Location_ID());
            inout.setDropShip_User_ID(order.getDropShip_User_ID());
        }
    } else if (createFromType.equals(INVOICE)) {
        MInvoice invoice = new MInvoice(getCtx(), referenceId, get_TrxName());
        if (inout.getC_Order_ID() == 0)
            inout.setC_Order_ID(invoice.getC_Order_ID());
        inout.setC_Invoice_ID(invoice.getC_Invoice_ID());
        inout.setAD_OrgTrx_ID(invoice.getAD_OrgTrx_ID());
        inout.setC_Project_ID(invoice.getC_Project_ID());
        inout.setC_Campaign_ID(invoice.getC_Campaign_ID());
        inout.setC_Activity_ID(invoice.getC_Activity_ID());
        inout.setUser1_ID(invoice.getUser1_ID());
        inout.setUser2_ID(invoice.getUser2_ID());
        inout.setUser3_ID(invoice.getUser3_ID());
        inout.setUser4_ID(invoice.getUser4_ID());
    } else if (createFromType.equals(RMA)) {
        MRMA rma = new MRMA(getCtx(), referenceId, get_TrxName());
        MInOut originalIO = rma.getShipment();
        inout.setIsSOTrx(rma.isSOTrx());
        inout.setC_Order_ID(0);
        inout.setC_Invoice_ID(0);
        inout.setM_RMA_ID(rma.getM_RMA_ID());
        inout.setAD_OrgTrx_ID(originalIO.getAD_OrgTrx_ID());
        inout.setC_Project_ID(originalIO.getC_Project_ID());
        inout.setC_Campaign_ID(originalIO.getC_Campaign_ID());
        inout.setC_Activity_ID(originalIO.getC_Activity_ID());
        inout.setUser1_ID(originalIO.getUser1_ID());
        inout.setUser2_ID(originalIO.getUser2_ID());
        inout.setUser3_ID(originalIO.getUser3_ID());
        inout.setUser4_ID(originalIO.getUser4_ID());
    }
    //	Save
    inout.saveEx();
}
Also used : MInOut(org.compiere.model.MInOut) MOrder(org.compiere.model.MOrder) MInvoice(org.compiere.model.MInvoice) MRMA(org.compiere.model.MRMA)

Aggregations

MInvoice (org.compiere.model.MInvoice)70 BigDecimal (java.math.BigDecimal)25 MInvoiceLine (org.compiere.model.MInvoiceLine)23 MPayment (org.compiere.model.MPayment)12 ResultSet (java.sql.ResultSet)10 Timestamp (java.sql.Timestamp)10 MBPartner (org.compiere.model.MBPartner)10 MInOut (org.compiere.model.MInOut)10 MOrder (org.compiere.model.MOrder)10 PreparedStatement (java.sql.PreparedStatement)9 MInOutLine (org.compiere.model.MInOutLine)8 MOrderLine (org.compiere.model.MOrderLine)6 ArrayList (java.util.ArrayList)5 AdempiereException (org.adempiere.exceptions.AdempiereException)5 MDocType (org.compiere.model.MDocType)5 KeyNamePair (org.compiere.util.KeyNamePair)5 ValueNamePair (org.compiere.util.ValueNamePair)5 MCashLine (org.compiere.model.MCashLine)4 MClient (org.compiere.model.MClient)4 MProduct (org.compiere.model.MProduct)4