Search in sources :

Example 1 with MCharge

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

the class ImportAccount method updateCharge.

//	doIt
private void updateCharge(X_I_ElementValue impEV, MElementValue ev) {
    MCharge charge = (MCharge) impEV.getC_Charge();
    if (charge.get_ID() == 0 && !Util.isEmpty(impEV.getChargeName())) {
        charge.setName(impEV.getChargeName());
        charge.setAD_Org_ID(0);
        if (impEV.getC_TaxCategory_ID() == 0) {
            String sql = "SELECT C_TaxCategory_ID FROM C_TaxCategory WHERE AD_Client_ID = ? ORDER BY IsDefault DESC ";
            int taxc = DB.getSQLValue(get_TrxName(), sql, m_AD_Client_ID);
            charge.setC_TaxCategory_ID(taxc);
        } else {
            charge.setC_TaxCategory_ID(impEV.getC_TaxCategory_ID());
        }
        charge.saveEx();
        impEV.setC_Charge_ID(charge.getC_Charge_ID());
    }
    // add/update charge accounting 
    if (ev != null && !charge.is_new()) {
        // for accounting schemas
        for (MAcctSchema schema : MAcctSchema.getClientAcctSchema(getCtx(), m_AD_Client_ID)) {
            // with the same Account Element as the import
            if (schema.getAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_Account).getC_Element_ID() == ev.getC_Element_ID()) {
                MChargeAcct chargeacct = MChargeAcct.get(schema, charge.getC_Charge_ID(), get_TrxName());
                if (chargeacct == null) {
                    chargeacct = new MChargeAcct(getCtx(), 0, get_TrxName());
                    chargeacct.setAD_Org_ID(charge.getAD_Org_ID());
                    chargeacct.setC_AcctSchema_ID(schema.getC_AcctSchema_ID());
                    chargeacct.setC_Charge_ID(charge.getC_Charge_ID());
                }
                MAccount expenseAccount = (MAccount) chargeacct.getCh_Expense_A();
                if (expenseAccount == null) {
                    //  optional null
                    expenseAccount = MAccount.getDefault(schema, true);
                }
                if (expenseAccount.getAccount_ID() != ev.getC_ElementValue_ID()) {
                    MAccount account = MAccount.get(Env.getCtx(), charge.getAD_Client_ID(), charge.getAD_Org_ID(), schema.getC_AcctSchema_ID(), ev.getC_ElementValue_ID(), expenseAccount.getC_SubAcct_ID(), expenseAccount.getM_Product_ID(), expenseAccount.getC_BPartner_ID(), expenseAccount.getAD_OrgTrx_ID(), expenseAccount.getC_LocFrom_ID(), expenseAccount.getC_LocTo_ID(), expenseAccount.getC_SalesRegion_ID(), expenseAccount.getC_Project_ID(), expenseAccount.getC_Campaign_ID(), expenseAccount.getC_Activity_ID(), expenseAccount.getUser1_ID(), expenseAccount.getUser2_ID(), expenseAccount.getUser3_ID(), expenseAccount.getUser4_ID(), expenseAccount.getUserElement1_ID(), expenseAccount.getUserElement2_ID(), get_TrxName());
                    chargeacct.setCh_Expense_Acct(account.getC_ValidCombination_ID());
                }
                MAccount revenueAccount = (MAccount) chargeacct.getCh_Revenue_A();
                if (revenueAccount == null) {
                    //  optional null
                    revenueAccount = MAccount.getDefault(schema, true);
                }
                if (revenueAccount.getAccount_ID() != ev.getC_ElementValue_ID()) {
                    MAccount account = MAccount.get(Env.getCtx(), charge.getAD_Client_ID(), charge.getAD_Org_ID(), schema.getC_AcctSchema_ID(), ev.getC_ElementValue_ID(), revenueAccount.getC_SubAcct_ID(), revenueAccount.getM_Product_ID(), revenueAccount.getC_BPartner_ID(), revenueAccount.getAD_OrgTrx_ID(), revenueAccount.getC_LocFrom_ID(), revenueAccount.getC_LocTo_ID(), revenueAccount.getC_SalesRegion_ID(), revenueAccount.getC_Project_ID(), revenueAccount.getC_Campaign_ID(), revenueAccount.getC_Activity_ID(), revenueAccount.getUser1_ID(), revenueAccount.getUser2_ID(), revenueAccount.getUser3_ID(), revenueAccount.getUser4_ID(), revenueAccount.getUserElement1_ID(), revenueAccount.getUserElement2_ID(), get_TrxName());
                    chargeacct.setCh_Revenue_Acct(account.getC_ValidCombination_ID());
                }
                chargeacct.saveEx();
            }
        }
    }
}
Also used : MAcctSchema(org.compiere.model.MAcctSchema) MAccount(org.compiere.model.MAccount) MCharge(org.compiere.model.MCharge) MChargeAcct(org.compiere.model.MChargeAcct)

Example 2 with MCharge

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

the class Charge method createCharge.

//  createElementValue
/**
     *  Create Charge and account entries for primary Account Schema.
     *
     *  @param name             charge name
     *  @param elementValueId   element value identifier
     *  @return charge identifier, or 0 if no charge created.
     */
protected int createCharge(String name, int elementValueId) {
    MCharge charge;
    MAccount account;
    log.config(name + " - ");
    // Charge
    charge = new MCharge(Env.getCtx(), 0, null);
    charge.setName(name);
    charge.setC_TaxCategory_ID(m_C_TaxCategory_ID);
    if (!charge.save()) {
        log.log(Level.SEVERE, name + " not created");
        return 0;
    }
    refreshAccountSchema();
    if (!isAccountSchemaValid()) {
        return 0;
    }
    //  Target Account
    account = getAccount(elementValueId, charge);
    if (account == null) {
        return 0;
    }
    updateAccount(charge, account);
    return charge.getC_Charge_ID();
}
Also used : MAccount(org.compiere.model.MAccount) MCharge(org.compiere.model.MCharge)

Example 3 with MCharge

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

the class InventoryUtil method getCreateCharge.

public static MCharge getCreateCharge(String value) {
    Properties ctx = Env.getCtx();
    String whereClause = MCharge.COLUMNNAME_Name + "=?";
    MCharge charge = new Query(ctx, MCharge.Table_Name, whereClause, null).setParameters(new Object[] { value }).setOnlyActiveRecords(true).setClient_ID().firstOnly();
    if (charge == null) {
        charge = new MCharge(ctx, 0, null);
        charge.setName(value);
        setGeneratedTag(charge);
        charge.saveEx();
    }
    return charge;
}
Also used : Query(org.compiere.model.Query) MCharge(org.compiere.model.MCharge) Properties(java.util.Properties)

Example 4 with MCharge

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

the class RequisitionPOCreate method newLine.

//	closeOrder
/**
	 * 	New Order Line (different Product)
	 *	@param rLine request line
	 * 	@throws Exception
	 */
private void newLine(MRequisitionLine rLine) throws Exception {
    if (m_orderLine != null) {
        m_orderLine.saveEx();
    }
    m_orderLine = null;
    MProduct product = MProduct.get(getCtx(), rLine.getM_Product_ID());
    //	Get Business Partner
    int C_BPartner_ID = rLine.getC_BPartner_ID();
    if (C_BPartner_ID != 0) {
        ;
    } else if (rLine.getC_Charge_ID() != 0) {
        MCharge charge = MCharge.get(getCtx(), rLine.getC_Charge_ID());
        C_BPartner_ID = charge.getC_BPartner_ID();
        if (C_BPartner_ID == 0) {
            throw new AdempiereUserError("No Vendor for Charge " + charge.getName());
        }
    } else {
        // Find Strategic Vendor for Product
        // TODO: refactor
        MProductPO[] ppos = MProductPO.getOfProduct(getCtx(), product.getM_Product_ID(), null);
        for (int i = 0; i < ppos.length; i++) {
            if (ppos[i].isCurrentVendor() && ppos[i].getC_BPartner_ID() != 0) {
                C_BPartner_ID = ppos[i].getC_BPartner_ID();
                break;
            }
        }
        if (C_BPartner_ID == 0 && ppos.length > 0) {
            C_BPartner_ID = ppos[0].getC_BPartner_ID();
        }
        if (C_BPartner_ID == 0) {
            throw new NoVendorForProductException(product.getName());
        }
    }
    if (!isGenerateForVendor(C_BPartner_ID)) {
        log.info("Skip for partner " + C_BPartner_ID);
        return;
    }
    //	New Order - Different Vendor
    if (m_order == null || m_order.getC_BPartner_ID() != C_BPartner_ID || m_order.getDatePromised().compareTo(rLine.getDateRequired()) != 0) {
        newOrder(rLine, C_BPartner_ID);
    }
    //	No Order Line
    m_orderLine = new MOrderLine(m_order);
    m_orderLine.setDatePromised(rLine.getDateRequired());
    if (product != null) {
        m_orderLine.setProduct(product);
        m_orderLine.setM_AttributeSetInstance_ID(rLine.getM_AttributeSetInstance_ID());
    } else {
        m_orderLine.setC_Charge_ID(rLine.getC_Charge_ID());
        m_orderLine.setPriceActual(rLine.getPriceActual());
    }
    m_orderLine.setAD_Org_ID(rLine.getAD_Org_ID());
    //	Prepare Save
    m_M_Product_ID = rLine.getM_Product_ID();
    m_M_AttributeSetInstance_ID = rLine.getM_AttributeSetInstance_ID();
    m_orderLine.saveEx();
}
Also used : NoVendorForProductException(org.adempiere.exceptions.NoVendorForProductException) MProduct(org.compiere.model.MProduct) AdempiereUserError(org.compiere.util.AdempiereUserError) MCharge(org.compiere.model.MCharge) MOrderLine(org.compiere.model.MOrderLine)

Example 5 with MCharge

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

the class InventoryUtil method getCreateCharge.

public static MCharge getCreateCharge(String value) {
    Properties ctx = Env.getCtx();
    String whereClause = MCharge.COLUMNNAME_Name + "=?";
    MCharge charge = new Query(ctx, MCharge.Table_Name, whereClause, null).setParameters(new Object[] { value }).setOnlyActiveRecords(true).setClient_ID().firstOnly();
    if (charge == null) {
        charge = new MCharge(ctx, 0, null);
        charge.setName(value);
        setGeneratedTag(charge);
        charge.saveEx();
    }
    return charge;
}
Also used : Query(org.compiere.model.Query) MCharge(org.compiere.model.MCharge) Properties(java.util.Properties)

Aggregations

MCharge (org.compiere.model.MCharge)5 Properties (java.util.Properties)2 MAccount (org.compiere.model.MAccount)2 Query (org.compiere.model.Query)2 NoVendorForProductException (org.adempiere.exceptions.NoVendorForProductException)1 MAcctSchema (org.compiere.model.MAcctSchema)1 MChargeAcct (org.compiere.model.MChargeAcct)1 MOrderLine (org.compiere.model.MOrderLine)1 MProduct (org.compiere.model.MProduct)1 AdempiereUserError (org.compiere.util.AdempiereUserError)1