Search in sources :

Example 1 with AssetProductStockedException

use of org.compiere.FA.exceptions.AssetProductStockedException 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)

Aggregations

FillMandatoryException (org.adempiere.exceptions.FillMandatoryException)1 AssetProductStockedException (org.compiere.FA.exceptions.AssetProductStockedException)1 MInvoiceLine (org.compiere.model.MInvoiceLine)1 MProduct (org.compiere.model.MProduct)1