Search in sources :

Example 81 with MOrderLine

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

the class Match method createMatchRecord.

//  tableLoad
/**
	 *  Create Matching Record
	 *  @param invoice true if matching invoice false if matching PO
	 *  @param M_InOutLine_ID shipment line
	 *  @param Line_ID C_InvoiceLine_ID or C_OrderLine_ID
	 *  @param qty quantity
	 *  @param trxName 
	 *  @return true if created
	 */
protected boolean createMatchRecord(boolean invoice, int M_InOutLine_ID, int Line_ID, BigDecimal qty, String trxName) {
    if (qty.compareTo(Env.ZERO) == 0)
        return true;
    log.fine("IsInvoice=" + invoice + ", M_InOutLine_ID=" + M_InOutLine_ID + ", Line_ID=" + Line_ID + ", Qty=" + qty);
    //
    boolean success = false;
    MInOutLine sLine = new MInOutLine(Env.getCtx(), M_InOutLine_ID, trxName);
    if (//	Shipment - Invoice
    invoice) {
        //	Update Invoice Line
        MInvoiceLine iLine = new MInvoiceLine(Env.getCtx(), Line_ID, trxName);
        iLine.setM_InOutLine_ID(M_InOutLine_ID);
        if (sLine.getC_OrderLine_ID() != 0)
            iLine.setC_OrderLine_ID(sLine.getC_OrderLine_ID());
        iLine.saveEx();
        //	Create Shipment - Invoice Link
        if (iLine.getM_Product_ID() != 0) {
            MMatchInv match = new MMatchInv(iLine, null, qty);
            match.setM_InOutLine_ID(M_InOutLine_ID);
            if (match.save()) {
                success = true;
                if (MClient.isClientAccountingImmediate()) {
                    String ignoreError = DocumentEngine.postImmediate(match.getCtx(), match.getAD_Client_ID(), match.get_Table_ID(), match.get_ID(), true, match.get_TrxName());
                }
            } else
                log.log(Level.SEVERE, "Inv Match not created: " + match);
        } else
            success = true;
        //	Create PO - Invoice Link = corrects PO
        if (iLine.getC_OrderLine_ID() != 0 && iLine.getM_Product_ID() != 0) {
            MMatchPO matchPO = MMatchPO.create(iLine, sLine, null, qty);
            matchPO.setC_InvoiceLine_ID(iLine);
            matchPO.setM_InOutLine_ID(M_InOutLine_ID);
            if (!matchPO.save())
                log.log(Level.SEVERE, "PO(Inv) Match not created: " + matchPO);
            if (MClient.isClientAccountingImmediate()) {
                String ignoreError = DocumentEngine.postImmediate(matchPO.getCtx(), matchPO.getAD_Client_ID(), matchPO.get_Table_ID(), matchPO.get_ID(), true, matchPO.get_TrxName());
            }
        }
    } else //	Shipment - Order
    {
        //	Update Shipment Line
        sLine.setC_OrderLine_ID(Line_ID);
        sLine.saveEx();
        //	Update Order Line
        MOrderLine oLine = new MOrderLine(Env.getCtx(), Line_ID, trxName);
        if (//	other in MInOut.completeIt
        oLine.get_ID() != 0) {
            oLine.setQtyReserved(oLine.getQtyReserved().subtract(qty));
            if (!oLine.save())
                log.severe("QtyReserved not updated - C_OrderLine_ID=" + Line_ID);
        }
        //	Create PO - Shipment Link
        if (sLine.getM_Product_ID() != 0) {
            MMatchPO match = new MMatchPO(sLine, null, qty);
            if (!match.save())
                log.log(Level.SEVERE, "PO Match not created: " + match);
            else {
                success = true;
                //	Correct Ordered Qty for Stocked Products (see MOrder.reserveStock / MInOut.processIt)
                if (sLine.getProduct() != null && sLine.getProduct().isStocked())
                    success = MStorage.add(Env.getCtx(), sLine.getM_Warehouse_ID(), sLine.getM_Locator_ID(), sLine.getM_Product_ID(), sLine.getM_AttributeSetInstance_ID(), oLine.getM_AttributeSetInstance_ID(), null, null, qty.negate(), trxName);
            }
        } else
            success = true;
    }
    return success;
}
Also used : MInOutLine(org.compiere.model.MInOutLine) MMatchPO(org.compiere.model.MMatchPO) MInvoiceLine(org.compiere.model.MInvoiceLine) MMatchInv(org.compiere.model.MMatchInv) MOrderLine(org.compiere.model.MOrderLine)

Example 82 with MOrderLine

use of org.compiere.model.MOrderLine in project lar_361 by comitsrl.

the class ImportOrder method doIt.

// prepare
/**
 *  Perform process.
 *  @return Message
 *  @throws Exception
 */
protected String doIt() throws java.lang.Exception {
    StringBuffer sql = null;
    int no = 0;
    String clientCheck = " AND AD_Client_ID=" + m_AD_Client_ID;
    // Delete Old Imported
    if (m_deleteOldImported) {
        sql = new StringBuffer("DELETE I_Order " + "WHERE I_IsImported='Y'").append(clientCheck);
        no = DB.executeUpdate(sql.toString(), get_TrxName());
        log.fine("Delete Old Impored =" + no);
    }
    // Set Client, Org, IsActive, Created/Updated
    sql = new StringBuffer("UPDATE I_Order " + "SET AD_Client_ID = COALESCE (AD_Client_ID,").append(m_AD_Client_ID).append(")," + " AD_Org_ID = COALESCE (AD_Org_ID,").append(m_AD_Org_ID).append(")," + " IsActive = COALESCE (IsActive, 'Y')," + " Created = COALESCE (Created, SysDate)," + " CreatedBy = COALESCE (CreatedBy, 0)," + " Updated = COALESCE (Updated, SysDate)," + " UpdatedBy = COALESCE (UpdatedBy, 0)," + " I_ErrorMsg = ' '," + " I_IsImported = 'N' " + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.info("Reset=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Org, '" + "WHERE (AD_Org_ID IS NULL OR AD_Org_ID=0" + " OR EXISTS (SELECT * FROM AD_Org oo WHERE o.AD_Org_ID=oo.AD_Org_ID AND (oo.IsSummary='Y' OR oo.IsActive='N')))" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Org=" + no);
    // Document Type - PO - SO
    sql = new StringBuffer(// PO Document Type Name
    "UPDATE I_Order o " + "SET C_DocType_ID=(SELECT C_DocType_ID FROM C_DocType d WHERE d.Name=o.DocTypeName" + " AND d.DocBaseType='POO' AND o.AD_Client_ID=d.AD_Client_ID) " + "WHERE C_DocType_ID IS NULL AND IsSOTrx='N' AND DocTypeName IS NOT NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set PO DocType=" + no);
    sql = new StringBuffer(// SO Document Type Name
    "UPDATE I_Order o " + "SET C_DocType_ID=(SELECT C_DocType_ID FROM C_DocType d WHERE d.Name=o.DocTypeName" + " AND d.DocBaseType='SOO' AND o.AD_Client_ID=d.AD_Client_ID) " + "WHERE C_DocType_ID IS NULL AND IsSOTrx='Y' AND DocTypeName IS NOT NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set SO DocType=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_DocType_ID=(SELECT C_DocType_ID FROM C_DocType d WHERE d.Name=o.DocTypeName" + " AND d.DocBaseType IN ('SOO','POO') AND o.AD_Client_ID=d.AD_Client_ID) " + // + "WHERE C_DocType_ID IS NULL AND IsSOTrx IS NULL AND DocTypeName IS NOT NULL AND I_IsImported<>'Y'").append (clientCheck);
    "WHERE C_DocType_ID IS NULL AND DocTypeName IS NOT NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set DocType=" + no);
    sql = new StringBuffer(// Error Invalid Doc Type Name
    "UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid DocTypeName, ' " + "WHERE C_DocType_ID IS NULL AND DocTypeName IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid DocTypeName=" + no);
    // DocType Default
    sql = new StringBuffer(// Default PO
    "UPDATE I_Order o " + "SET C_DocType_ID=(SELECT MAX(C_DocType_ID) FROM C_DocType d WHERE d.IsDefault='Y'" + " AND d.DocBaseType='POO' AND o.AD_Client_ID=d.AD_Client_ID) " + "WHERE C_DocType_ID IS NULL AND IsSOTrx='N' AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set PO Default DocType=" + no);
    sql = new StringBuffer(// Default SO
    "UPDATE I_Order o " + "SET C_DocType_ID=(SELECT MAX(C_DocType_ID) FROM C_DocType d WHERE d.IsDefault='Y'" + " AND d.DocBaseType='SOO' AND o.AD_Client_ID=d.AD_Client_ID) " + "WHERE C_DocType_ID IS NULL AND IsSOTrx='Y' AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set SO Default DocType=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_DocType_ID=(SELECT MAX(C_DocType_ID) FROM C_DocType d WHERE d.IsDefault='Y'" + " AND d.DocBaseType IN('SOO','POO') AND o.AD_Client_ID=d.AD_Client_ID) " + "WHERE C_DocType_ID IS NULL AND IsSOTrx IS NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Default DocType=" + no);
    sql = new StringBuffer(// No DocType
    "UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No DocType, ' " + "WHERE C_DocType_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No DocType=" + no);
    // Set IsSOTrx
    sql = new StringBuffer("UPDATE I_Order o SET IsSOTrx='Y' " + "WHERE EXISTS (SELECT * FROM C_DocType d WHERE o.C_DocType_ID=d.C_DocType_ID AND d.DocBaseType='SOO' AND o.AD_Client_ID=d.AD_Client_ID)" + " AND C_DocType_ID IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set IsSOTrx=Y=" + no);
    sql = new StringBuffer("UPDATE I_Order o SET IsSOTrx='N' " + "WHERE EXISTS (SELECT * FROM C_DocType d WHERE o.C_DocType_ID=d.C_DocType_ID AND d.DocBaseType='POO' AND o.AD_Client_ID=d.AD_Client_ID)" + " AND C_DocType_ID IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set IsSOTrx=N=" + no);
    // Price List
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_PriceList_ID=(SELECT MAX(M_PriceList_ID) FROM M_PriceList p WHERE p.IsDefault='Y'" + " AND p.C_Currency_ID=o.C_Currency_ID AND p.IsSOPriceList=o.IsSOTrx AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_PriceList_ID IS NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Default Currency PriceList=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_PriceList_ID=(SELECT MAX(M_PriceList_ID) FROM M_PriceList p WHERE p.IsDefault='Y'" + " AND p.IsSOPriceList=o.IsSOTrx AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_PriceList_ID IS NULL AND C_Currency_ID IS NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Default PriceList=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_PriceList_ID=(SELECT MAX(M_PriceList_ID) FROM M_PriceList p " + " WHERE p.C_Currency_ID=o.C_Currency_ID AND p.IsSOPriceList=o.IsSOTrx AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_PriceList_ID IS NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Currency PriceList=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_PriceList_ID=(SELECT MAX(M_PriceList_ID) FROM M_PriceList p " + " WHERE p.IsSOPriceList=o.IsSOTrx AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_PriceList_ID IS NULL AND C_Currency_ID IS NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set PriceList=" + no);
    // 
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No PriceList, ' " + "WHERE M_PriceList_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No PriceList=" + no);
    // @Trifon - Import Order Source
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_OrderSource_ID=(SELECT C_OrderSource_ID FROM C_OrderSource p" + " WHERE o.C_OrderSourceValue=p.Value AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE C_OrderSource_ID IS NULL AND C_OrderSourceValue IS NOT NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Order Source=" + no);
    // Set proper error message
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Not Found Order Source, ' " + "WHERE C_OrderSource_ID IS NULL AND C_OrderSourceValue IS NOT NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No OrderSource=" + no);
    // Payment Term
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_PaymentTerm_ID=(SELECT C_PaymentTerm_ID FROM C_PaymentTerm p" + " WHERE o.PaymentTermValue=p.Value AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE C_PaymentTerm_ID IS NULL AND PaymentTermValue IS NOT NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set PaymentTerm=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_PaymentTerm_ID=(SELECT MAX(C_PaymentTerm_ID) FROM C_PaymentTerm p" + " WHERE p.IsDefault='Y' AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE C_PaymentTerm_ID IS NULL AND o.PaymentTermValue IS NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Default PaymentTerm=" + no);
    // 
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No PaymentTerm, ' " + "WHERE C_PaymentTerm_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No PaymentTerm=" + no);
    // Warehouse
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_Warehouse_ID=(SELECT MAX(M_Warehouse_ID) FROM M_Warehouse w" + " WHERE o.AD_Client_ID=w.AD_Client_ID AND o.AD_Org_ID=w.AD_Org_ID) " + "WHERE M_Warehouse_ID IS NULL AND I_IsImported<>'Y'").append(clientCheck);
    // Warehouse for Org
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.fine("Set Warehouse=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_Warehouse_ID=(SELECT M_Warehouse_ID FROM M_Warehouse w" + " WHERE o.AD_Client_ID=w.AD_Client_ID) " + "WHERE M_Warehouse_ID IS NULL" + " AND EXISTS (SELECT AD_Client_ID FROM M_Warehouse w WHERE w.AD_Client_ID=o.AD_Client_ID GROUP BY AD_Client_ID HAVING COUNT(*)=1)" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.fine("Set Only Client Warehouse=" + no);
    // 
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Warehouse, ' " + "WHERE M_Warehouse_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No Warehouse=" + no);
    // BP from EMail
    sql = new StringBuffer("UPDATE I_Order o " + "SET (C_BPartner_ID,AD_User_ID)=(SELECT C_BPartner_ID,AD_User_ID FROM AD_User u" + " WHERE o.EMail=u.EMail AND o.AD_Client_ID=u.AD_Client_ID AND u.C_BPartner_ID IS NOT NULL) " + "WHERE C_BPartner_ID IS NULL AND EMail IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set BP from EMail=" + no);
    // BP from ContactName
    sql = new StringBuffer("UPDATE I_Order o " + "SET (C_BPartner_ID,AD_User_ID)=(SELECT C_BPartner_ID,AD_User_ID FROM AD_User u" + " WHERE o.ContactName=u.Name AND o.AD_Client_ID=u.AD_Client_ID AND u.C_BPartner_ID IS NOT NULL) " + "WHERE C_BPartner_ID IS NULL AND ContactName IS NOT NULL" + " AND EXISTS (SELECT Name FROM AD_User u WHERE o.ContactName=u.Name AND o.AD_Client_ID=u.AD_Client_ID AND u.C_BPartner_ID IS NOT NULL GROUP BY Name HAVING COUNT(*)=1)" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set BP from ContactName=" + no);
    // BP from Value
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_BPartner_ID=(SELECT MAX(C_BPartner_ID) FROM C_BPartner bp" + " WHERE o.BPartnerValue=bp.Value AND o.AD_Client_ID=bp.AD_Client_ID) " + "WHERE C_BPartner_ID IS NULL AND BPartnerValue IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set BP from Value=" + no);
    // Default BP
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_BPartner_ID=(SELECT C_BPartnerCashTrx_ID FROM AD_ClientInfo c" + " WHERE o.AD_Client_ID=c.AD_Client_ID) " + "WHERE C_BPartner_ID IS NULL AND BPartnerValue IS NULL AND Name IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Default BP=" + no);
    // Existing Location ? Exact Match
    sql = new StringBuffer("UPDATE I_Order o " + "SET (BillTo_ID,C_BPartner_Location_ID)=(SELECT C_BPartner_Location_ID,C_BPartner_Location_ID" + " FROM C_BPartner_Location bpl INNER JOIN C_Location l ON (bpl.C_Location_ID=l.C_Location_ID)" + " WHERE o.C_BPartner_ID=bpl.C_BPartner_ID AND bpl.AD_Client_ID=o.AD_Client_ID" + " AND DUMP(o.Address1)=DUMP(l.Address1) AND DUMP(o.Address2)=DUMP(l.Address2)" + " AND DUMP(o.City)=DUMP(l.City) AND DUMP(o.Postal)=DUMP(l.Postal)" + " AND o.C_Region_ID=l.C_Region_ID AND o.C_Country_ID=l.C_Country_ID) " + "WHERE C_BPartner_ID IS NOT NULL AND C_BPartner_Location_ID IS NULL" + " AND I_IsImported='N'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Found Location=" + no);
    // Set Bill Location from BPartner
    sql = new StringBuffer("UPDATE I_Order o " + "SET BillTo_ID=(SELECT MAX(C_BPartner_Location_ID) FROM C_BPartner_Location l" + " WHERE l.C_BPartner_ID=o.C_BPartner_ID AND o.AD_Client_ID=l.AD_Client_ID" + " AND ((l.IsBillTo='Y' AND o.IsSOTrx='Y') OR (l.IsPayFrom='Y' AND o.IsSOTrx='N'))" + ") " + "WHERE C_BPartner_ID IS NOT NULL AND BillTo_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set BP BillTo from BP=" + no);
    // Set Location from BPartner
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_BPartner_Location_ID=(SELECT MAX(C_BPartner_Location_ID) FROM C_BPartner_Location l" + " WHERE l.C_BPartner_ID=o.C_BPartner_ID AND o.AD_Client_ID=l.AD_Client_ID" + " AND ((l.IsShipTo='Y' AND o.IsSOTrx='Y') OR o.IsSOTrx='N')" + ") " + "WHERE C_BPartner_ID IS NOT NULL AND C_BPartner_Location_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set BP Location from BP=" + no);
    // 
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No BP Location, ' " + "WHERE C_BPartner_ID IS NOT NULL AND (BillTo_ID IS NULL OR C_BPartner_Location_ID IS NULL)" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No BP Location=" + no);
    // Set Country
    /**
     *		sql = new StringBuffer ("UPDATE I_Order o "
     *			  + "SET CountryCode=(SELECT MAX(CountryCode) FROM C_Country c WHERE c.IsDefault='Y'"
     *			  + " AND c.AD_Client_ID IN (0, o.AD_Client_ID)) "
     *			  + "WHERE C_BPartner_ID IS NULL AND CountryCode IS NULL AND C_Country_ID IS NULL"
     *			  + " AND I_IsImported<>'Y'").append (clientCheck);
     *		no = DB.executeUpdate(sql.toString(), get_TrxName());
     *		log.fine("Set Country Default=" + no);
     */
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_Country_ID=(SELECT C_Country_ID FROM C_Country c" + " WHERE o.CountryCode=c.CountryCode AND c.AD_Client_ID IN (0, o.AD_Client_ID)) " + "WHERE C_BPartner_ID IS NULL AND C_Country_ID IS NULL AND CountryCode IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Country=" + no);
    // 
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Country, ' " + "WHERE C_BPartner_ID IS NULL AND C_Country_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Country=" + no);
    // Set Region
    sql = new StringBuffer("UPDATE I_Order o " + "Set RegionName=(SELECT MAX(Name) FROM C_Region r" + " WHERE r.IsDefault='Y' AND r.C_Country_ID=o.C_Country_ID" + " AND r.AD_Client_ID IN (0, o.AD_Client_ID)) " + "WHERE C_BPartner_ID IS NULL AND C_Region_ID IS NULL AND RegionName IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Region Default=" + no);
    // 
    sql = new StringBuffer("UPDATE I_Order o " + "Set C_Region_ID=(SELECT C_Region_ID FROM C_Region r" + " WHERE r.Name=o.RegionName AND r.C_Country_ID=o.C_Country_ID" + " AND r.AD_Client_ID IN (0, o.AD_Client_ID)) " + "WHERE C_BPartner_ID IS NULL AND C_Region_ID IS NULL AND RegionName IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Region=" + no);
    // 
    sql = new StringBuffer("UPDATE I_Order o " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Region, ' " + "WHERE C_BPartner_ID IS NULL AND C_Region_ID IS NULL " + " AND EXISTS (SELECT * FROM C_Country c" + " WHERE c.C_Country_ID=o.C_Country_ID AND c.HasRegion='Y')" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Region=" + no);
    // Product
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p" + " WHERE o.ProductValue=p.Value AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_Product_ID IS NULL AND ProductValue IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Product from Value=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p" + " WHERE o.UPC=p.UPC AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_Product_ID IS NULL AND UPC IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Product from UPC=" + no);
    sql = new StringBuffer("UPDATE I_Order o " + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p" + " WHERE o.SKU=p.SKU AND o.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_Product_ID IS NULL AND SKU IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Product fom SKU=" + no);
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Product, ' " + "WHERE M_Product_ID IS NULL AND (ProductValue IS NOT NULL OR UPC IS NOT NULL OR SKU IS NOT NULL)" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Product=" + no);
    // Charge
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_Charge_ID=(SELECT C_Charge_ID FROM C_Charge c" + " WHERE o.ChargeName=c.Name AND o.AD_Client_ID=c.AD_Client_ID) " + "WHERE C_Charge_ID IS NULL AND ChargeName IS NOT NULL AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Charge=" + no);
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Charge, ' " + "WHERE C_Charge_ID IS NULL AND (ChargeName IS NOT NULL)" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Charge=" + no);
    // 
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Product and Charge, ' " + "WHERE M_Product_ID IS NOT NULL AND C_Charge_ID IS NOT NULL " + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Product and Charge exclusive=" + no);
    // Tax
    sql = new StringBuffer("UPDATE I_Order o " + "SET C_Tax_ID=(SELECT MAX(C_Tax_ID) FROM C_Tax t" + " WHERE o.TaxIndicator=t.TaxIndicator AND o.AD_Client_ID=t.AD_Client_ID) " + "WHERE C_Tax_ID IS NULL AND TaxIndicator IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Tax=" + no);
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Tax, ' " + "WHERE C_Tax_ID IS NULL AND TaxIndicator IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Tax=" + no);
    commitEx();
    // -- New BPartner ---------------------------------------------------
    // Go through Order Records w/o C_BPartner_ID
    sql = new StringBuffer("SELECT * FROM I_Order " + "WHERE I_IsImported='N' AND C_BPartner_ID IS NULL").append(clientCheck);
    try {
        PreparedStatement pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            X_I_Order imp = new X_I_Order(getCtx(), rs, get_TrxName());
            if (imp.getBPartnerValue() == null) {
                if (imp.getEMail() != null)
                    imp.setBPartnerValue(imp.getEMail());
                else if (imp.getName() != null)
                    imp.setBPartnerValue(imp.getName());
                else
                    continue;
            }
            if (imp.getName() == null) {
                if (imp.getContactName() != null)
                    imp.setName(imp.getContactName());
                else
                    imp.setName(imp.getBPartnerValue());
            }
            // BPartner
            MBPartner bp = MBPartner.get(getCtx(), imp.getBPartnerValue());
            if (bp == null) {
                bp = new MBPartner(getCtx(), -1, get_TrxName());
                bp.setClientOrg(imp.getAD_Client_ID(), imp.getAD_Org_ID());
                bp.setValue(imp.getBPartnerValue());
                bp.setName(imp.getName());
                if (!bp.save())
                    continue;
            }
            imp.setC_BPartner_ID(bp.getC_BPartner_ID());
            // BP Location
            MBPartnerLocation bpl = null;
            MBPartnerLocation[] bpls = bp.getLocations(true);
            for (int i = 0; bpl == null && i < bpls.length; i++) {
                if (imp.getC_BPartner_Location_ID() == bpls[i].getC_BPartner_Location_ID())
                    bpl = bpls[i];
                else // Same Location ID
                if (imp.getC_Location_ID() == bpls[i].getC_Location_ID())
                    bpl = bpls[i];
                else // Same Location Info
                if (imp.getC_Location_ID() == 0) {
                    MLocation loc = bpls[i].getLocation(false);
                    if (loc.equals(imp.getC_Country_ID(), imp.getC_Region_ID(), imp.getPostal(), "", imp.getCity(), imp.getAddress1(), imp.getAddress2()))
                        bpl = bpls[i];
                }
            }
            if (bpl == null) {
                // New Location
                MLocation loc = new MLocation(getCtx(), 0, get_TrxName());
                loc.setAddress1(imp.getAddress1());
                loc.setAddress2(imp.getAddress2());
                loc.setCity(imp.getCity());
                loc.setPostal(imp.getPostal());
                if (imp.getC_Region_ID() != 0)
                    loc.setC_Region_ID(imp.getC_Region_ID());
                loc.setC_Country_ID(imp.getC_Country_ID());
                if (!loc.save())
                    continue;
                // 
                bpl = new MBPartnerLocation(bp);
                bpl.setC_Location_ID(loc.getC_Location_ID());
                if (!bpl.save())
                    continue;
            }
            imp.setC_Location_ID(bpl.getC_Location_ID());
            imp.setBillTo_ID(bpl.getC_BPartner_Location_ID());
            imp.setC_BPartner_Location_ID(bpl.getC_BPartner_Location_ID());
            // User/Contact
            if (imp.getContactName() != null || imp.getEMail() != null || imp.getPhone() != null) {
                MUser[] users = bp.getContacts(true);
                MUser user = null;
                for (int i = 0; user == null && i < users.length; i++) {
                    String name = users[i].getName();
                    if (name.equals(imp.getContactName()) || name.equals(imp.getName())) {
                        user = users[i];
                        imp.setAD_User_ID(user.getAD_User_ID());
                    }
                }
                if (user == null) {
                    user = new MUser(bp);
                    if (imp.getContactName() == null)
                        user.setName(imp.getName());
                    else
                        user.setName(imp.getContactName());
                    user.setEMail(imp.getEMail());
                    user.setPhone(imp.getPhone());
                    if (user.save())
                        imp.setAD_User_ID(user.getAD_User_ID());
                }
            }
            imp.save();
        }
        // for all new BPartners
        rs.close();
        pstmt.close();
    // 
    } catch (SQLException e) {
        log.log(Level.SEVERE, "BP - " + sql.toString(), e);
    }
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No BPartner, ' " + "WHERE C_BPartner_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No BPartner=" + no);
    commitEx();
    // -- New Orders -----------------------------------------------------
    int noInsert = 0;
    int noInsertLine = 0;
    // Go through Order Records w/o
    sql = new StringBuffer("SELECT * FROM I_Order " + "WHERE I_IsImported='N'").append(clientCheck).append(" ORDER BY C_BPartner_ID, BillTo_ID, C_BPartner_Location_ID, I_Order_ID");
    try {
        PreparedStatement pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        ResultSet rs = pstmt.executeQuery();
        // 
        int oldC_BPartner_ID = 0;
        int oldBillTo_ID = 0;
        int oldC_BPartner_Location_ID = 0;
        String oldDocumentNo = "";
        // 
        MOrder order = null;
        int lineNo = 0;
        while (rs.next()) {
            X_I_Order imp = new X_I_Order(getCtx(), rs, get_TrxName());
            String cmpDocumentNo = imp.getDocumentNo();
            if (cmpDocumentNo == null)
                cmpDocumentNo = "";
            // New Order
            if (oldC_BPartner_ID != imp.getC_BPartner_ID() || oldC_BPartner_Location_ID != imp.getC_BPartner_Location_ID() || oldBillTo_ID != imp.getBillTo_ID() || !oldDocumentNo.equals(cmpDocumentNo)) {
                if (order != null) {
                    if (m_docAction != null && m_docAction.length() > 0) {
                        order.setDocAction(m_docAction);
                        if (!order.processIt(m_docAction)) {
                            log.warning("Order Process Failed: " + order + " - " + order.getProcessMsg());
                            throw new IllegalStateException("Order Process Failed: " + order + " - " + order.getProcessMsg());
                        }
                    }
                    order.saveEx();
                }
                oldC_BPartner_ID = imp.getC_BPartner_ID();
                oldC_BPartner_Location_ID = imp.getC_BPartner_Location_ID();
                oldBillTo_ID = imp.getBillTo_ID();
                oldDocumentNo = imp.getDocumentNo();
                if (oldDocumentNo == null)
                    oldDocumentNo = "";
                // 
                order = new MOrder(getCtx(), 0, get_TrxName());
                order.setClientOrg(imp.getAD_Client_ID(), imp.getAD_Org_ID());
                order.setC_DocTypeTarget_ID(imp.getC_DocType_ID());
                order.setIsSOTrx(imp.isSOTrx());
                if (imp.getDeliveryRule() != null) {
                    order.setDeliveryRule(imp.getDeliveryRule());
                }
                if (imp.getDocumentNo() != null)
                    order.setDocumentNo(imp.getDocumentNo());
                // Ship Partner
                order.setC_BPartner_ID(imp.getC_BPartner_ID());
                order.setC_BPartner_Location_ID(imp.getC_BPartner_Location_ID());
                if (imp.getAD_User_ID() != 0)
                    order.setAD_User_ID(imp.getAD_User_ID());
                // Bill Partner
                order.setBill_BPartner_ID(imp.getC_BPartner_ID());
                order.setBill_Location_ID(imp.getBillTo_ID());
                // 
                if (imp.getDescription() != null)
                    order.setDescription(imp.getDescription());
                order.setC_PaymentTerm_ID(imp.getC_PaymentTerm_ID());
                order.setM_PriceList_ID(imp.getM_PriceList_ID());
                order.setM_Warehouse_ID(imp.getM_Warehouse_ID());
                if (imp.getM_Shipper_ID() != 0)
                    order.setM_Shipper_ID(imp.getM_Shipper_ID());
                // SalesRep from Import or the person running the import
                if (imp.getSalesRep_ID() != 0)
                    order.setSalesRep_ID(imp.getSalesRep_ID());
                if (order.getSalesRep_ID() == 0)
                    order.setSalesRep_ID(getAD_User_ID());
                // 
                if (imp.getAD_OrgTrx_ID() != 0)
                    order.setAD_OrgTrx_ID(imp.getAD_OrgTrx_ID());
                if (imp.getC_Activity_ID() != 0)
                    order.setC_Activity_ID(imp.getC_Activity_ID());
                if (imp.getC_Campaign_ID() != 0)
                    order.setC_Campaign_ID(imp.getC_Campaign_ID());
                if (imp.getC_Project_ID() != 0)
                    order.setC_Project_ID(imp.getC_Project_ID());
                // 
                if (imp.getDateOrdered() != null)
                    order.setDateOrdered(imp.getDateOrdered());
                if (imp.getDateAcct() != null)
                    order.setDateAcct(imp.getDateAcct());
                // Set Order Source
                if (imp.getC_OrderSource() != null)
                    order.setC_OrderSource_ID(imp.getC_OrderSource_ID());
                // 
                order.saveEx();
                noInsert++;
                lineNo = 10;
            }
            imp.setC_Order_ID(order.getC_Order_ID());
            // New OrderLine
            MOrderLine line = new MOrderLine(order);
            line.setLine(lineNo);
            lineNo += 10;
            if (imp.getM_Product_ID() != 0)
                line.setM_Product_ID(imp.getM_Product_ID(), true);
            if (imp.getC_Charge_ID() != 0)
                line.setC_Charge_ID(imp.getC_Charge_ID());
            line.setQty(imp.getQtyOrdered());
            line.setPrice();
            if (imp.getPriceActual().compareTo(Env.ZERO) != 0)
                line.setPrice(imp.getPriceActual());
            if (imp.getC_Tax_ID() != 0)
                line.setC_Tax_ID(imp.getC_Tax_ID());
            else {
                line.setTax();
                imp.setC_Tax_ID(line.getC_Tax_ID());
            }
            if (imp.getFreightAmt() != null)
                line.setFreightAmt(imp.getFreightAmt());
            if (imp.getLineDescription() != null)
                line.setDescription(imp.getLineDescription());
            line.saveEx();
            imp.setC_OrderLine_ID(line.getC_OrderLine_ID());
            imp.setI_IsImported(true);
            imp.setProcessed(true);
            // 
            if (imp.save())
                noInsertLine++;
        }
        if (order != null) {
            if (m_docAction != null && m_docAction.length() > 0) {
                order.setDocAction(m_docAction);
                if (!order.processIt(m_docAction)) {
                    log.warning("Order Process Failed: " + order + " - " + order.getProcessMsg());
                    throw new IllegalStateException("Order Process Failed: " + order + " - " + order.getProcessMsg());
                }
            }
            order.saveEx();
        }
        rs.close();
        pstmt.close();
    } catch (Exception e) {
        log.log(Level.SEVERE, "Order - " + sql.toString(), e);
    }
    // Set Error to indicator to not imported
    sql = new StringBuffer("UPDATE I_Order " + "SET I_IsImported='N', Updated=SysDate " + "WHERE I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    addLog(0, null, new BigDecimal(no), "@Errors@");
    // 
    addLog(0, null, new BigDecimal(noInsert), "@C_Order_ID@: @Inserted@");
    addLog(0, null, new BigDecimal(noInsertLine), "@C_OrderLine_ID@: @Inserted@");
    return "#" + noInsert + "/" + noInsertLine;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) MBPartner(org.compiere.model.MBPartner) SQLException(java.sql.SQLException) BigDecimal(java.math.BigDecimal) MBPartnerLocation(org.compiere.model.MBPartnerLocation) MOrder(org.compiere.model.MOrder) X_I_Order(org.compiere.model.X_I_Order) ResultSet(java.sql.ResultSet) MOrderLine(org.compiere.model.MOrderLine) MLocation(org.compiere.model.MLocation) MUser(org.compiere.model.MUser)

Example 83 with MOrderLine

use of org.compiere.model.MOrderLine in project lar_361 by comitsrl.

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()); @emmie
                    // 
                    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 84 with MOrderLine

use of org.compiere.model.MOrderLine in project lar_361 by comitsrl.

the class PosOrderModel method deleteLine.

// deleteOrder
/**
 * to erase the lines from order
 * @return true if deleted
 */
public void deleteLine(int C_OrderLine_ID) {
    if (C_OrderLine_ID != -1) {
        for (MOrderLine line : getLines(true, "M_Product_ID")) {
            if (line.getC_OrderLine_ID() == C_OrderLine_ID) {
                line.delete(true);
                line.save();
            }
        }
    }
}
Also used : MOrderLine(org.compiere.model.MOrderLine)

Example 85 with MOrderLine

use of org.compiere.model.MOrderLine in project lar_361 by comitsrl.

the class SubCurrentLine method saveLine.

// newLine
/**
 * Save Line
 *
 * @return true if saved
 */
public boolean saveLine() {
    MProduct product = getProduct();
    if (product == null)
        return false;
    BigDecimal qtyOrdered = (BigDecimal) f_quantity.getValue();
    BigDecimal priceActual = (BigDecimal) f_price.getValue();
    if (p_posPanel.m_order == null) {
        p_posPanel.m_order = PosOrderModel.createOrder(p_posPanel.p_pos, p_posPanel.f_order.getBPartner(), p_posPanel.f_order.getC_BPartner_Location_ID(), trxName);
    }
    MOrderLine line = null;
    if (p_posPanel.m_order != null) {
        line = p_posPanel.m_order.createLine(product, qtyOrdered, priceActual, p_posPanel.getWindowNo());
        if (line == null)
            return false;
        if (!line.save())
            return false;
    }
    orderLineId = line.getC_OrderLine_ID();
    setM_Product_ID(0);
    // 
    return true;
}
Also used : MProduct(org.compiere.model.MProduct) MOrderLine(org.compiere.model.MOrderLine) BigDecimal(java.math.BigDecimal)

Aggregations

MOrderLine (org.compiere.model.MOrderLine)87 BigDecimal (java.math.BigDecimal)44 MOrder (org.compiere.model.MOrder)42 MInOutLine (org.compiere.model.MInOutLine)16 MProduct (org.compiere.model.MProduct)15 MBPartner (org.compiere.model.MBPartner)14 MInOut (org.compiere.model.MInOut)11 ResultSet (java.sql.ResultSet)10 ArrayList (java.util.ArrayList)8 Query (org.compiere.model.Query)8 PreparedStatement (java.sql.PreparedStatement)7 MInvoice (org.compiere.model.MInvoice)7 MInvoiceLine (org.compiere.model.MInvoiceLine)7 AdempiereException (org.adempiere.exceptions.AdempiereException)6 SQLException (java.sql.SQLException)5 MLocator (org.compiere.model.MLocator)5 Timestamp (java.sql.Timestamp)4 MRMALine (org.compiere.model.MRMALine)4 MBPartnerLocation (org.compiere.model.MBPartnerLocation)3 MDocType (org.compiere.model.MDocType)3