Search in sources :

Example 36 with MAcctSchema

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

the class CopyCostTypeToCostType method copyCostTypeToCostType.

/**
     * Copy Cost Type to Cost Type
     *
     * @param productId
     * @param accountSchema
     * @param costTypeFrom
     * @param costTypeTo
     * @param costElementFrom
     * @param costElementTo
     * @param trxName
     */
private void copyCostTypeToCostType(int productId, MAcctSchema accountSchema, MCostType costTypeFrom, MCostType costTypeTo, MCostElement costElementFrom, MCostElement costElementTo, String trxName) {
    MProduct product = MProduct.get(getCtx(), productId);
    CostDimension costDimensionFrom = new CostDimension(product, accountSchema, costTypeFrom.get_ID(), getOrganizationId(), getWarehouseId(), 0, costElementFrom.get_ID());
    Optional<MCost> costDimensionFromOptional = Optional.ofNullable(costDimensionFrom.toQuery(MCost.class, trxName).first());
    CostDimension costDimensionTo = new CostDimension(product, accountSchema, costTypeTo.get_ID(), getOrganizationId(), getWarehouseId(), 0, costElementTo.get_ID());
    Optional<MCost> costDimensionToOptional = Optional.ofNullable(costDimensionTo.toQuery(MCost.class, trxName).first());
    if (isUpdateCosting()) {
        // exist cost form and cost to or not exist cost to and exit cost from
        if (costDimensionToOptional.isPresent() && costDimensionFromOptional.isPresent()) {
            MCost costTo = costDimensionToOptional.get();
            if (MCostType.COSTINGMETHOD_StandardCosting.equals(costTypeFrom.getCostingMethod()) && costTo.isCostFrozen())
                ;
            else {
                costTo.setCurrentCostPrice(costDimensionFromOptional.get().getCurrentCostPrice());
                costTo.saveEx();
            }
        } else if (!costDimensionToOptional.isPresent() && costDimensionFromOptional.isPresent()) {
            MCost costTo = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costTypeTo.get_ID(), costElementTo.get_ID());
            if (MCostType.COSTINGMETHOD_StandardCosting.equals(costTypeFrom.getCostingMethod()) && costTo.isCostFrozen())
                ;
            else {
                costDimensionFromOptional.ifPresent(costFrom -> costTo.setCurrentCostPrice(costFrom.getCurrentCostPrice()));
                costTo.saveEx();
            }
        } else if (// cost to and not exist cost from
        costDimensionToOptional.isPresent() && !costDimensionFromOptional.isPresent()) {
            MCost costTo = costDimensionToOptional.get();
            costTo.setCurrentCostPrice(BigDecimal.ZERO);
            costTo.saveEx();
        } else if (!costDimensionToOptional.isPresent() && !costDimensionFromOptional.isPresent()) {
            MCost costTo = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costTypeTo.get_ID(), costElementTo.get_ID());
            costTo.setCurrentCostPrice(BigDecimal.ZERO);
            costTo.saveEx();
        }
    } else if (!costDimensionToOptional.isPresent()) {
        MCost costTo = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costTypeTo.get_ID(), costElementTo.get_ID());
        costDimensionFromOptional.ifPresent(costFrom -> costTo.setCurrentCostPrice(costFrom.getCurrentCostPrice()));
        costTo.saveEx();
    }
}
Also used : Arrays(java.util.Arrays) CostDimension(org.adempiere.engine.CostDimension) MCostType(org.compiere.model.MCostType) MAcctSchema(org.compiere.model.MAcctSchema) ArrayList(java.util.ArrayList) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) List(java.util.List) Query(org.compiere.model.Query) AdempiereException(org.adempiere.exceptions.AdempiereException) MCostElement(org.compiere.model.MCostElement) Optional(java.util.Optional) Trx(org.compiere.util.Trx) MProduct(org.compiere.model.MProduct) MProduct(org.compiere.model.MProduct) MCost(org.compiere.model.MCost) CostDimension(org.adempiere.engine.CostDimension)

Example 37 with MAcctSchema

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

the class CopyPriceToStandard method doIt.

protected String doIt() throws Exception {
    MAcctSchema accountSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
    MCostElement costElement = MCostElement.get(getCtx(), getCostElementId());
    if (!MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType()))
        throw new AdempiereException("Only Material Cost Elements are allowed");
    AtomicInteger countUpdated = new AtomicInteger(0);
    MPriceListVersion priceListVersion = new MPriceListVersion(getCtx(), getPriceListVersionId(), get_TrxName());
    Arrays.stream(priceListVersion.getProductPrice(" AND " + MProductPrice.COLUMNNAME_PriceStd + " <> 0")).forEach(productPrice -> {
        final BigDecimal price;
        int currencyId = priceListVersion.getPriceList().getC_Currency_ID();
        if (currencyId != accountSchema.getC_Currency_ID()) {
            price = MConversionRate.convert(getCtx(), productPrice.getPriceStd(), currencyId, accountSchema.getC_Currency_ID(), getAD_Client_ID(), getOrganizationId());
        } else
            price = productPrice.getPriceStd();
        MProduct product = MProduct.get(getCtx(), productPrice.getM_Product_ID());
        CostDimension costDimension = new CostDimension(product, accountSchema, getCostTypeId(), getOrganizationId(), 0, 0, getCostElementId());
        List<MCost> costs = costDimension.toQuery(MCost.class, get_TrxName()).list();
        costs.stream().filter(cost -> cost != null && cost.getM_CostElement_ID() == costElement.get_ID()).findFirst().ifPresent(cost -> {
            cost.setFutureCostPrice(price);
            cost.saveEx();
            countUpdated.getAndUpdate(count -> count + 1);
        });
    });
    return "@Updated@ # " + countUpdated;
}
Also used : MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MProduct(org.compiere.model.MProduct) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdempiereException(org.adempiere.exceptions.AdempiereException) MCost(org.compiere.model.MCost) MPriceListVersion(org.compiere.model.MPriceListVersion) CostDimension(org.adempiere.engine.CostDimension) BigDecimal(java.math.BigDecimal)

Example 38 with MAcctSchema

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

the class WebEditorFactory method getEditor.

public static WEditor getEditor(GridTab gridTab, GridField gridField, boolean tableEditor) {
    if (gridField == null) {
        return null;
    }
    WEditor editor = null;
    int displayType = gridField.getDisplayType();
    /** Not a Field */
    if (gridField.isHeading()) {
        return null;
    }
    /** String (clear/password) */
    if (displayType == DisplayType.String || displayType == DisplayType.PrinterName || (tableEditor && (displayType == DisplayType.Text || displayType == DisplayType.TextLong))) {
        if (gridField.isEncryptedField()) {
            editor = new WPasswordEditor(gridField);
        } else {
            editor = new WStringEditor(gridField, tableEditor);
        }
    } else /** File */
    if (displayType == DisplayType.FileName) {
        editor = new WFilenameEditor(gridField);
    } else /** File Path */
    if (displayType == DisplayType.FilePath) {
        editor = new WFileDirectoryEditor(gridField);
    } else /** File Path or Name */
    if (displayType == DisplayType.FilePathOrName) {
        editor = new WFilenameEditor(gridField);
    } else /** Number */
    if (DisplayType.isNumeric(displayType)) {
        editor = new WNumberEditor(gridField);
    } else /** YesNo */
    if (displayType == DisplayType.YesNo) {
        editor = new WYesNoEditor(gridField);
        if (tableEditor)
            ((WYesNoEditor) editor).getComponent().setLabel("");
    } else /** Text */
    if (displayType == DisplayType.Text || displayType == DisplayType.Memo || displayType == DisplayType.TextLong) {
        editor = new WStringEditor(gridField);
    } else /** Date */
    if (DisplayType.isDate(displayType)) {
        if (displayType == DisplayType.Time)
            editor = new WTimeEditor(gridField);
        else if (displayType == DisplayType.DateTime)
            editor = new WDatetimeEditor(gridField);
        else
            editor = new WDateEditor(gridField);
    } else /**  Button */
    if (displayType == DisplayType.Button) {
        editor = new WButtonEditor(gridField);
    } else /** Table Direct */
    if (displayType == DisplayType.TableDir || displayType == DisplayType.Table || displayType == DisplayType.List || displayType == DisplayType.ID) {
        editor = new WTableDirEditor(gridField);
    } else if (displayType == DisplayType.URL) {
        editor = new WUrlEditor(gridField);
    } else if (displayType == DisplayType.Search) {
        editor = new WSearchEditor(gridField);
    } else if (displayType == DisplayType.Location) {
        editor = new WLocationEditor(gridField);
    } else if (displayType == DisplayType.Locator) {
        editor = new WLocatorEditor(gridField);
    } else if (displayType == DisplayType.Account) {
        editor = new WAccountEditor(gridField);
    } else if (displayType == DisplayType.Image) {
        editor = new WImageEditor(gridField);
    } else if (displayType == DisplayType.Binary) {
        editor = new WBinaryEditor(gridField);
    } else if (displayType == DisplayType.PAttribute) {
        editor = new WPAttributeEditor(gridTab, gridField);
    } else if (displayType == DisplayType.Assignment) {
        editor = new WAssignmentEditor(gridField);
    } else if (displayType == DisplayType.Chart) {
        editor = new WChartEditor(gridField, gridTab.getWindowNo());
    } else {
        editor = new WUnknownEditor(gridField);
    }
    // Change the label from the column to a user defined value for specific fields.
    if (gridField.getColumnName() != null && gridField.getColumnName().equals("User1_ID") || gridField.getColumnName() != null && gridField.getColumnName().equals("User2_ID") || gridField.getColumnName() != null && gridField.getColumnName().equals("User3_ID") || gridField.getColumnName() != null && gridField.getColumnName().equals("User4_ID")) {
        int accountSchemaId = Env.getContextAsInt(Env.getCtx(), "$C_AcctSchema_ID");
        if (accountSchemaId > 0) {
            MAcctSchema accountSchema = MAcctSchema.get(Env.getCtx(), accountSchemaId);
            if (accountSchema != null) {
                MAcctSchemaElement accountSchemaElement = null;
                if (gridField.getColumnName().equals("User1_ID"))
                    accountSchemaElement = accountSchema.getAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_UserList1);
                else if (gridField.getColumnName().equals("User2_ID"))
                    accountSchemaElement = accountSchema.getAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_UserList2);
                else if (gridField.getColumnName().equals("User3_ID"))
                    accountSchemaElement = accountSchema.getAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_UserList3);
                else if (gridField.getColumnName().equals("User4_ID"))
                    accountSchemaElement = accountSchema.getAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_UserList4);
                if (accountSchemaElement != null)
                    editor.setLabel(accountSchemaElement.getName());
            }
        }
    }
    return editor;
}
Also used : MAcctSchema(org.compiere.model.MAcctSchema) MAcctSchemaElement(org.compiere.model.MAcctSchemaElement)

Example 39 with MAcctSchema

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

the class InvoiceNGL method createGLJournal.

//	doIt
/**
	 * 	Create GL Journal
	 * 	@return document info
	 */
private String createGLJournal() {
    //FR: [ 2214883 ] Remove SQL code and Replace for Query
    final String whereClause = "AD_PInstance_ID=?";
    List<X_T_InvoiceGL> list = new Query(getCtx(), X_T_InvoiceGL.Table_Name, whereClause, get_TrxName()).setParameters(getAD_PInstance_ID()).setOrderBy("AD_Org_ID").list();
    if (list.size() == 0)
        return " - No Records found";
    //
    MAcctSchema as = MAcctSchema.get(getCtx(), p_C_AcctSchema_ID);
    MAcctSchemaDefault asDefaultAccts = MAcctSchemaDefault.get(getCtx(), p_C_AcctSchema_ID);
    MGLCategory cat = MGLCategory.getDefaultSystem(getCtx());
    if (cat == null) {
        MDocType docType = MDocType.get(getCtx(), p_C_DocTypeReval_ID);
        cat = MGLCategory.get(getCtx(), docType.getGL_Category_ID());
    }
    //
    MJournalBatch batch = new MJournalBatch(getCtx(), 0, get_TrxName());
    batch.setDescription(getName());
    batch.setC_DocType_ID(p_C_DocTypeReval_ID);
    batch.setDateDoc(new Timestamp(System.currentTimeMillis()));
    batch.setDateAcct(p_DateReval);
    batch.setC_Currency_ID(as.getC_Currency_ID());
    if (!batch.save())
        return " - Could not create Batch";
    //
    MJournal journal = null;
    BigDecimal drTotal = Env.ZERO;
    BigDecimal crTotal = Env.ZERO;
    int AD_Org_ID = 0;
    for (int i = 0; i < list.size(); i++) {
        X_T_InvoiceGL gl = list.get(i);
        if (gl.getAmtRevalDrDiff().signum() == 0 && gl.getAmtRevalCrDiff().signum() == 0)
            continue;
        MInvoice invoice = new MInvoice(getCtx(), gl.getC_Invoice_ID(), null);
        if (invoice.getC_Currency_ID() == as.getC_Currency_ID())
            continue;
        //
        if (journal == null) {
            journal = new MJournal(batch);
            journal.setC_AcctSchema_ID(as.getC_AcctSchema_ID());
            journal.setC_Currency_ID(as.getC_Currency_ID());
            journal.setC_ConversionType_ID(p_C_ConversionTypeReval_ID);
            MOrg org = MOrg.get(getCtx(), gl.getAD_Org_ID());
            journal.setDescription(getName() + " - " + org.getName());
            journal.setGL_Category_ID(cat.getGL_Category_ID());
            if (!journal.save())
                return " - Could not create Journal";
        }
        //
        MJournalLine line = new MJournalLine(journal);
        line.setLine((i + 1) * 10);
        line.setDescription(invoice.getSummary());
        //
        MFactAcct fa = new MFactAcct(getCtx(), gl.getFact_Acct_ID(), null);
        line.setC_ValidCombination_ID(MAccount.get(fa));
        BigDecimal dr = gl.getAmtRevalDrDiff();
        BigDecimal cr = gl.getAmtRevalCrDiff();
        drTotal = drTotal.add(dr);
        crTotal = crTotal.add(cr);
        line.setAmtSourceDr(dr);
        line.setAmtAcctDr(dr);
        line.setAmtSourceCr(cr);
        line.setAmtAcctCr(cr);
        line.saveEx();
        //
        if (//	invoice org id
        AD_Org_ID == 0)
            AD_Org_ID = gl.getAD_Org_ID();
        //	Change in Org
        if (AD_Org_ID != gl.getAD_Org_ID()) {
            createBalancing(asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (i + 1) * 10);
            //
            AD_Org_ID = gl.getAD_Org_ID();
            drTotal = Env.ZERO;
            crTotal = Env.ZERO;
            journal = null;
        }
    }
    createBalancing(asDefaultAccts, journal, drTotal, crTotal, AD_Org_ID, (list.size() + 1) * 10);
    return " - " + batch.getDocumentNo() + " #" + list.size();
}
Also used : MDocType(org.compiere.model.MDocType) Query(org.compiere.model.Query) X_T_InvoiceGL(org.compiere.model.X_T_InvoiceGL) MGLCategory(org.compiere.model.MGLCategory) MJournalLine(org.compiere.model.MJournalLine) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MAcctSchema(org.compiere.model.MAcctSchema) MOrg(org.compiere.model.MOrg) MAcctSchemaDefault(org.compiere.model.MAcctSchemaDefault) MFactAcct(org.compiere.model.MFactAcct) MJournal(org.compiere.model.MJournal) MJournalBatch(org.compiere.model.MJournalBatch)

Example 40 with MAcctSchema

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

the class ValuationEffectiveDate method doIt.

// prepare
/**
	 * execute the Valuation Effective Date
	 */
protected String doIt() throws Exception {
    setup();
    for (MAcctSchema acctSchema : acctSchemas) for (MWarehouse warehouse : warehouses) for (MCostType costType : costTypes) for (MCostElement costElement : costElements) for (int productId : products) generateInventoryValue(productId, acctSchema.getC_AcctSchema_ID(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), warehouse.getM_Warehouse_ID());
    pstmt.executeBatch();
    commitEx();
    DB.close(pstmt);
    DB.executeUpdate("UPDATE T_InventoryValue SET Cost = CASE WHEN QtyOnHand <> 0 THEN (CostAmt + CostAmtLL) / QtyOnHand ELSE  0 END  ,  CumulatedAmt = CASE WHEN QtyOnHand <> 0  THEN  CostAmt + CostAmtLL ELSE 0 END ,  DateValue = " + DB.TO_DATE(getValuationDate()) + " WHERE AD_PInstance_ID=?", getAD_PInstance_ID(), get_TrxName());
    return "@Ok@ " + count;
}
Also used : MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MCostType(org.compiere.model.MCostType) MWarehouse(org.compiere.model.MWarehouse)

Aggregations

MAcctSchema (org.compiere.model.MAcctSchema)45 BigDecimal (java.math.BigDecimal)20 MCostElement (org.compiere.model.MCostElement)18 MProduct (org.compiere.model.MProduct)15 ArrayList (java.util.ArrayList)14 MCostType (org.compiere.model.MCostType)14 List (java.util.List)12 MCost (org.compiere.model.MCost)12 Arrays (java.util.Arrays)11 Query (org.compiere.model.Query)11 Trx (org.compiere.util.Trx)9 AdempiereException (org.adempiere.exceptions.AdempiereException)8 Env (org.compiere.util.Env)8 CostDimension (org.adempiere.engine.CostDimension)7 SQLException (java.sql.SQLException)6 MAcctSchemaElement (org.compiere.model.MAcctSchemaElement)6 Msg (org.compiere.util.Msg)6 Timestamp (java.sql.Timestamp)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 AdempiereSystemError (org.compiere.util.AdempiereSystemError)5