Search in sources :

Example 31 with MInvoiceLine

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

the class ModelValidator method validateFixedAssetsInvoice_LRO.

/**
	 * Check if is a valid fixed asset related invoice (LRO)
	 * @param invoice
	 */
private void validateFixedAssetsInvoice_LRO(MInvoice invoice) {
    if (invoice.get_ValueAsBoolean("IsFixedAssetInvoice")) {
        boolean hasFixedAssetLines = false;
        boolean hasNormalLines = false;
        for (MInvoiceLine line : invoice.getLines()) {
            if (line.get_ValueAsBoolean("IsFixedAssetInvoice")) {
                hasFixedAssetLines = true;
            } else if (line.getM_Product_ID() > 0) {
                MProduct product = MProduct.get(line.getCtx(), line.getM_Product_ID());
                if (product.isItem()) {
                    // Only items are forbiden for FA invoices because in Romania these should use
                    // V_Liability vendor account and not V_Liability_FixedAssets vendor account
                    hasNormalLines = true;
                }
            }
            // No mixed lines are allowed
            if (hasFixedAssetLines && hasNormalLines) {
                throw new AssetInvoiceWithMixedLines_LRO();
            }
        }
    }
}
Also used : MProduct(org.compiere.model.MProduct) MInvoiceLine(org.compiere.model.MInvoiceLine) AssetInvoiceWithMixedLines_LRO(org.compiere.FA.exceptions.AssetInvoiceWithMixedLines_LRO)

Example 32 with MInvoiceLine

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

the class ModelValidator method modelChange_InvoiceLine.

// docValidate
/**
	 * Model Change Invoice Line
	 * @param model model
	 * @param changeType set when called from model validator (See TYPE_*); else -1, when called from callout
	 */
public static void modelChange_InvoiceLine(SetGetModel model, int changeType) {
    // Set Asset Related Fields:
    if (-1 == changeType || TYPE_BEFORE_NEW == changeType || TYPE_BEFORE_CHANGE == changeType) {
        //int invoice_id = SetGetUtil.get_AttrValueAsInt(m, MInvoiceLine.COLUMNNAME_C_Invoice_ID);
        //boolean isSOTrx = DB.isSOTrx(MInvoice.Table_Name, MInvoice.COLUMNNAME_C_Invoice_ID+"="+invoice_id);
        boolean isAsset = false;
        /* comment by @win
			boolean isFixedAsset = false;
			*/
        int assetGroupId = 0;
        //Goodwill - invoice is an Asset type Invoice
        isAsset = SetGetUtil.get_AttrValueAsBoolean(model, MInvoiceLine.COLUMNNAME_A_CreateAsset);
        //@win commenting this out to enable relating AR Invoice to Asset Disposal
        /*
			if (!isSOTrx) {
				int product_id = SetGetUtil.get_AttrValueAsInt(m, MInvoiceLine.COLUMNNAME_M_Product_ID);
				if (product_id > 0) {
					MProduct prod = MProduct.get(m.getCtx(), product_id);
					isAsset = (prod != null && prod.get_ID() > 0 && prod.isCreateAsset());
					assetGroup_ID = prod.getA_Asset_Group_ID();
					
					//isFixedAsset = MAssetType.isFixedAssetGroup(m.getCtx(), assetGroup_ID); //commented by @win - remove asset type
				}
			}
			*/
        int productId = SetGetUtil.get_AttrValueAsInt(model, MInvoiceLine.COLUMNNAME_M_Product_ID);
        if (productId > 0) {
            MProduct product = MProduct.get(model.getCtx(), productId);
            if (product.isCreateAsset()) {
                isAsset = (product != null && product.get_ID() > 0 && product.isCreateAsset());
                assetGroupId = product.getA_Asset_Group_ID();
            } else
                //Goodwill - if the product is not Asset Type
                assetGroupId = SetGetUtil.get_AttrValueAsInt(model, MInvoiceLine.COLUMNNAME_A_Asset_Group_ID);
        }
        // end modification by @win
        model.set_AttrValue(MInvoiceLine.COLUMNNAME_A_CreateAsset, isAsset);
        if (isAsset) {
            model.set_AttrValue(MInvoiceLine.COLUMNNAME_A_Asset_Group_ID, assetGroupId);
            /* comment by @win
				m.set_AttrValue(MInvoiceLine.COLUMNNAME_IsFixedAssetInvoice, isFixedAsset);
				*/
            model.set_AttrValue("IsFixedAssetInvoice", isAsset);
            model.set_AttrValue(MInvoiceLine.COLUMNNAME_A_CreateAsset, "Y");
        } else {
            model.set_AttrValue(MInvoiceLine.COLUMNNAME_A_Asset_Group_ID, null);
            model.set_AttrValue(MInvoiceLine.COLUMNNAME_A_Asset_ID, null);
            model.set_AttrValue("IsFixedAssetInvoice", false);
        }
        // Validate persistent object: 
        if (isAsset && (model instanceof MInvoiceLine)) {
            MInvoiceLine invoiceLine = (MInvoiceLine) model;
            // If is expense, then asset is mandatory
            if (MInvoiceLine.A_CAPVSEXP_Expense.equals(invoiceLine.getA_CapvsExp()) && invoiceLine.getA_Asset_ID() <= 0) {
                throw new FillMandatoryException(MInvoiceLine.COLUMNNAME_A_Asset_ID);
            }
            // Check Amounts & Qty
            if (invoiceLine.getLineNetAmt().signum() == 0) {
                throw new FillMandatoryException(MInvoiceLine.COLUMNNAME_QtyEntered, MInvoiceLine.COLUMNNAME_PriceEntered);
            }
            //
            // Check Product - fixed assets products shouldn't be stocked (but inventory objects are allowed)
            MProduct product = invoiceLine.getProduct();
            if (product.isStocked() && invoiceLine.get_ValueAsBoolean("IsFixedAssetInvoice")) {
                throw new AssetProductStockedException(product);
            }
        }
    }
    // Update Invoice Header:
    if (TYPE_AFTER_NEW == changeType || TYPE_AFTER_CHANGE == changeType || TYPE_AFTER_DELETE == changeType) {
        int invoiceId = SetGetUtil.get_AttrValueAsInt(model, MInvoiceLine.COLUMNNAME_C_Invoice_ID);
        String sql = "UPDATE C_Invoice i SET IsFixedAssetInvoice" + "=(SELECT COALESCE(MAX(il.IsFixedAssetInvoice),'N')" + " FROM C_InvoiceLine il" + " WHERE il.C_Invoice_ID=i.C_Invoice_ID" + " AND il." + MInvoiceLine.COLUMNNAME_IsDescription + "='N'" + ")" + " WHERE C_Invoice_ID=?";
        DB.executeUpdateEx(sql, new Object[] { invoiceId }, model.get_TrxName());
    }
}
Also used : MProduct(org.compiere.model.MProduct) MInvoiceLine(org.compiere.model.MInvoiceLine) AssetProductStockedException(org.compiere.FA.exceptions.AssetProductStockedException) FillMandatoryException(org.adempiere.exceptions.FillMandatoryException)

Example 33 with MInvoiceLine

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

the class ModelValidator method docValidate.

public String docValidate(PO po, int timing) {
    log.info(po.get_TableName() + " Timing: " + timing);
    String result = null;
    // TABLE C_Invoice
    String tableName = po.get_TableName();
    if (tableName.equals(MInvoice.Table_Name)) {
        // Invoice - Validate Fixed Assets Invoice (LRO)
        if (timing == TIMING_AFTER_PREPARE) {
            MInvoice invoice = (MInvoice) po;
            validateFixedAssetsInvoice_LRO(invoice);
        }
        if (timing == TIMING_AFTER_COMPLETE) {
            MInvoice mi = (MInvoice) po;
            if (mi.isSOTrx()) {
                MInvoiceLine[] mils = mi.getLines();
                for (MInvoiceLine mil : mils) {
                    if (mil.isA_CreateAsset() && !mil.isA_Processed()) {
                        MAssetDisposed.createAssetDisposed(mil);
                    }
                }
            }
        }
        if (timing == TIMING_AFTER_VOID) {
            MInvoice invoice = (MInvoice) po;
            String error = afterVoid(invoice);
            if (error != null)
                return error;
        }
        if (timing == TIMING_BEFORE_REVERSECORRECT) {
            MInvoice invoice = (MInvoice) po;
            String error = beforeReverseCorrect(invoice);
            if (error != null)
                return error;
        }
    }
    if (tableName.equals(MInOut.Table_Name)) {
        if (timing == TIMING_AFTER_COMPLETE) {
            MInOut inOut = (MInOut) po;
            for (MInOutLine inOutLine : inOut.getLines()) {
                MProduct product = inOutLine.getProduct();
                //	Create Asset for SO
                if (product != null && inOut.isSOTrx() && product.isCreateAsset() && !product.getM_Product_Category().getA_Asset_Group().isFixedAsset() && inOutLine.getMovementQty().signum() > 0 && !inOut.isReversal()) {
                    log.fine("Asset");
                    //info.append("@A_Asset_ID@: ");
                    int noAssets = inOutLine.getMovementQty().intValue();
                    if (!product.isOneAssetPerUOM())
                        noAssets = 1;
                    for (int i = 0; i < noAssets; i++) {
                        //if (i > 0)
                        //	info.append(" - ");
                        int deliveryCount = i + 1;
                        if (!product.isOneAssetPerUOM())
                            deliveryCount = 0;
                        MAsset asset = new MAsset(inOut, inOutLine, deliveryCount);
                        if (!asset.save(inOut.get_TrxName())) {
                            //return DocAction.STATUS_Invalid;
                            throw new IllegalStateException("Could not create Asset");
                        }
                    //info.append(asset.getValue());
                    }
                }
            }
        //	Asset
        }
        if (timing == TIMING_AFTER_REVERSECORRECT) {
            MInOut inOut = (MInOut) po;
            I_M_InOut inOutReversal = inOut.getReversal();
            for (MInOutLine inOutLine : inOut.getLines()) {
                //	De-Activate Asset
                MAsset asset = MAsset.getFromShipment(inOut.getCtx(), inOutLine.getM_InOutLine_ID(), inOut.get_TrxName());
                if (asset != null) {
                    asset.setIsActive(false);
                    asset.setDescription(asset.getDescription() + " (" + inOutReversal.getDocumentNo() + " #" + inOutLine.getLine() + "<-)");
                    asset.saveEx();
                }
            }
        }
    }
    return result;
}
Also used : MInOut(org.compiere.model.MInOut) MProduct(org.compiere.model.MProduct) MInOutLine(org.compiere.model.MInOutLine) I_M_InOut(org.compiere.model.I_M_InOut) MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) MAsset(org.compiere.model.MAsset)

Example 34 with MInvoiceLine

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

the class HRCreateInvoice method createInvoiceLine.

/**
     * Create Invoice Line to Employee
     * @param invoice
     * @param employee
     * @param movement
     * @param chargeId
     * @return
     */
private MInvoiceLine createInvoiceLine(MInvoice invoice, MHREmployee employee, MHRMovement movement, int chargeId) {
    MInvoiceLine invoiceLine = new MInvoiceLine(invoice);
    invoiceLine.setC_Charge_ID(chargeId);
    //
    invoiceLine.setQty(BigDecimal.ONE);
    invoiceLine.setDescription(movement.getHR_Concept().getName());
    invoiceLine.setC_Activity_ID(employee.getC_Activity_ID());
    invoiceLine.setPrice(movement.getAmount());
    invoiceLine.setTax();
    invoiceLine.saveEx();
    addLog(invoiceLine.getLine(), invoice.getDateInvoiced(), invoiceLine.getLineNetAmt(), movement.getHR_Concept().getName());
    movement.setC_InvoiceLine_ID(invoiceLine.getC_InvoiceLine_ID());
    movement.saveEx();
    return invoiceLine;
}
Also used : MInvoiceLine(org.compiere.model.MInvoiceLine)

Aggregations

MInvoiceLine (org.compiere.model.MInvoiceLine)34 MInvoice (org.compiere.model.MInvoice)23 BigDecimal (java.math.BigDecimal)13 MInOutLine (org.compiere.model.MInOutLine)10 MProduct (org.compiere.model.MProduct)7 MBPartner (org.compiere.model.MBPartner)6 MOrderLine (org.compiere.model.MOrderLine)6 MRMALine (org.compiere.model.MRMALine)6 ArrayList (java.util.ArrayList)4 MInOut (org.compiere.model.MInOut)4 MLocation (org.compiere.model.MLocation)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MClient (org.compiere.model.MClient)3 MDocType (org.compiere.model.MDocType)3 SQLException (java.sql.SQLException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 FillMandatoryException (org.adempiere.exceptions.FillMandatoryException)2 ALayoutConstraint (org.compiere.apps.ALayoutConstraint)2