Search in sources :

Example 6 with MPeriod

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

the class MHRYear method createPeriods.

/**
	 * 	CreatePeriods.
	 * 	Creates also Period
	 * 	@param  HR_Payroll_ID
	 *	@return true if created
	 */
public boolean createPeriods() {
    int sumDays = 0;
    int C_Calendar_ID = DB.getSQLValueEx(get_TrxName(), "SELECT C_Calendar_ID FROM C_Year WHERE C_Year_ID = ?", getC_Year_ID());
    if (C_Calendar_ID <= 0)
        return false;
    Timestamp StartDate = null;
    Timestamp EndDate = null;
    MHRPayroll payroll = new MHRPayroll(getCtx(), getHR_Payroll_ID(), get_TrxName());
    for (int period = 1; period <= getQty(); period++) {
        //in case of correspondence between period and month
        if ((12 == getQty()) && (28 == getNetDays() || 29 == getNetDays() || 30 == getNetDays() || 31 == getNetDays())) {
            if (period > 1) {
                StartDate = TimeUtil.addDays(EndDate, 1);
            } else {
                StartDate = TimeUtil.addDays(getStartDate(), 0);
            }
            EndDate = TimeUtil.getMonthLastDay(StartDate);
        } else // fortnight payment
        if ((24 == getQty()) && (15 == getNetDays() || 16 == getNetDays())) {
            if (period > 1) {
                StartDate = TimeUtil.addDays(EndDate, 1);
            } else {
                StartDate = TimeUtil.addDays(getStartDate(), 0);
            }
            Boolean par = (period % 2) == 0 ? true : false;
            if (!par)
                EndDate = TimeUtil.addDays(StartDate, getNetDays() - 1);
            else
                EndDate = TimeUtil.getMonthLastDay(StartDate);
        } else {
            sumDays = period == 1 ? 0 : (period - 1) * (getNetDays());
            StartDate = TimeUtil.addDays(getStartDate(), sumDays);
            EndDate = TimeUtil.addDays(StartDate, getNetDays() - 1);
        }
        int C_Period_ID = DB.getSQLValueEx(get_TrxName(), "SELECT C_Period_ID FROM C_Period p " + " INNER JOIN C_Year y ON (p.C_Year_ID=y.C_Year_ID) " + " WHERE " + " ? BETWEEN p.startdate AND p.endDate" + " AND y.C_Calendar_ID=?", EndDate, C_Calendar_ID);
        if (C_Period_ID <= 0)
            return false;
        MPeriod m_period = MPeriod.get(getCtx(), C_Period_ID);
        MHRPeriod HR_Period = new MHRPeriod(getCtx(), 0, get_TrxName());
        HR_Period.setAD_Org_ID(getAD_Org_ID());
        HR_Period.setHR_Year_ID(getHR_Year_ID());
        HR_Period.setHR_Payroll_ID(getHR_Payroll_ID());
        HR_Period.setName(StartDate.toString().substring(0, 10) + " " + Msg.translate(getCtx(), "To") + " " + EndDate.toString().substring(0, 10));
        HR_Period.setDescription(Msg.translate(getCtx(), "HR_Payroll_ID") + " " + payroll.getName().trim() + " " + Msg.translate(getCtx(), "From") + " " + period + " " + Msg.translate(getCtx(), "To") + " " + StartDate.toString().substring(0, 10) + " al " + EndDate.toString().substring(0, 10));
        HR_Period.setPeriodNo(period);
        HR_Period.setC_Period_ID(C_Period_ID);
        HR_Period.setC_Year_ID(m_period.getC_Year_ID());
        HR_Period.setStartDate(StartDate);
        HR_Period.setEndDate(EndDate);
        HR_Period.setDateAcct(EndDate);
        HR_Period.setIsActive(true);
        HR_Period.saveEx();
    }
    return true;
}
Also used : MPeriod(org.compiere.model.MPeriod) Timestamp(java.sql.Timestamp)

Example 7 with MPeriod

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

the class PeriodStatus method doIt.

//	prepare
/**
	 * 	Process
	 *	@return message
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("C_Period_ID=" + p_C_Period_ID + ", PeriodAction=" + p_PeriodAction);
    MPeriod period = new MPeriod(getCtx(), p_C_Period_ID, get_TrxName());
    if (period.get_ID() == 0)
        throw new AdempiereUserError("@NotFound@  @C_Period_ID@=" + p_C_Period_ID);
    StringBuffer sql = new StringBuffer("UPDATE C_PeriodControl ");
    sql.append("SET PeriodStatus='");
    //	Open
    if (MPeriodControl.PERIODACTION_OpenPeriod.equals(p_PeriodAction))
        sql.append(MPeriodControl.PERIODSTATUS_Open);
    else //	Close
    if (MPeriodControl.PERIODACTION_ClosePeriod.equals(p_PeriodAction))
        sql.append(MPeriodControl.PERIODSTATUS_Closed);
    else //	Close Permanently
    if (MPeriodControl.PERIODACTION_PermanentlyClosePeriod.equals(p_PeriodAction))
        sql.append(MPeriodControl.PERIODSTATUS_PermanentlyClosed);
    else
        return "-";
    //
    sql.append("', PeriodAction='N', Updated=SysDate,UpdatedBy=").append(getAD_User_ID());
    //	WHERE
    sql.append(" WHERE C_Period_ID=").append(period.getC_Period_ID()).append(" AND PeriodStatus<>'P'").append(" AND PeriodStatus<>'").append(p_PeriodAction).append("'");
    int no = DB.executeUpdate(sql.toString(), get_TrxName());
    CacheMgt.get().reset("C_PeriodControl", 0);
    CacheMgt.get().reset("C_Period", p_C_Period_ID);
    return "@Updated@ #" + no;
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MPeriod(org.compiere.model.MPeriod)

Example 8 with MPeriod

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

the class MHRProcess method setEmployee.

//	copyLinesFrom
/**
	 * Method use for testing setting the variables to execute rule
	 * @param partnerValue
	 * @param conceptValue
     */
public MHREmployee setEmployee(String partnerValue, String conceptValue) {
    MBPartner partner = MBPartner.get(getCtx(), partnerValue);
    if (partner == null)
        throw new AdempiereException("@C_BPartner_ID@ @NotFound@ " + partnerValue);
    partnerId = partner.get_ID();
    MHRConcept concept = MHRConcept.getByValue(getCtx(), conceptValue);
    if (concept == null)
        throw new AdempiereException("@HR_Concept_ID@ @NotFound@ " + conceptValue);
    payrollConceptId = concept.get_ID();
    columnType = concept.getColumnType();
    MHRPeriod payrollPeriod;
    employee = MHREmployee.getActiveEmployee(getCtx(), partnerId, null);
    if (getHR_Payroll_ID() > 0) {
        payrollId = getHR_Payroll_ID();
    }
    if (getHR_Department_ID() > 0) {
        departmentId = getHR_Department_ID();
    }
    if (getHR_Job_ID() > 0) {
        jobId = getHR_Job_ID();
    }
    if (getHR_Period_ID() > 0) {
        payrollPeriod = MHRPeriod.get(getCtx(), getHR_Period_ID());
    } else {
        payrollPeriod = new MHRPeriod(getCtx(), 0, null);
        MPeriod period = MPeriod.get(getCtx(), getDateAcct(), getAD_Org_ID());
        if (period != null) {
            payrollPeriod.setStartDate(period.getStartDate());
            payrollPeriod.setEndDate(period.getEndDate());
        } else {
            payrollPeriod.setStartDate(getDateAcct());
            payrollPeriod.setEndDate(getDateAcct());
        }
    }
    dateFrom = payrollPeriod.getStartDate();
    dateTo = payrollPeriod.getEndDate();
    // Setting Script context for calcualte rule
    scriptCtx.clear();
    scriptCtx.put("process", this);
    scriptCtx.put("_Process", getHR_Process_ID());
    scriptCtx.put("_Period", payrollPeriod.getHR_Period_ID());
    scriptCtx.put("_Payroll", getHR_Payroll_ID());
    scriptCtx.put("_PayrollValue", Optional.ofNullable(getHR_Payroll().getValue()).orElse(null));
    scriptCtx.put("_Department", getHR_Department_ID());
    logger.info("info data - " + Msg.parseTranslation(getCtx(), "@HR_Process_ID@ ") + getHR_Process_ID() + Msg.parseTranslation(getCtx(), ", @HR_Period_ID@ :") + getHR_Period_ID() + Msg.parseTranslation(getCtx(), ", @HR_Payroll_ID@ : ") + getHR_Payroll_ID() + Msg.parseTranslation(getCtx(), ", @HR_Department_ID@ : ") + getHR_Department_ID());
    scriptCtx.put("_From", dateFrom);
    scriptCtx.put("_To", dateTo);
    scriptCtx.put("_Period", payrollPeriod.getPeriodNo());
    scriptCtx.remove("_DateStart");
    scriptCtx.remove("_DateEnd");
    scriptCtx.remove("_Days");
    scriptCtx.remove("_C_BPartner_ID");
    scriptCtx.remove("_HR_Employee_ID");
    scriptCtx.put("_DateStart", employee.getStartDate());
    scriptCtx.put("_DateEnd", employee.getEndDate() == null ? TimeUtil.getDay(2999, 12, 31) : employee.getEndDate());
    scriptCtx.put("_Days", TimeUtil.getDaysBetween(payrollPeriod.getStartDate(), payrollPeriod.getEndDate()) + 1);
    scriptCtx.put("_C_BPartner_ID", employee.getC_BPartner_ID());
    scriptCtx.put("_HR_Employee_ID", employee.getHR_Employee_ID());
    scriptCtx.put("_Employee", employee);
    scriptCtx.remove("_HR_Concept_ID");
    scriptCtx.remove("_HR_Concept");
    scriptCtx.put("_HR_Concept_ID", concept.getHR_Concept_ID());
    scriptCtx.put("_HR_Concept", concept);
    scriptCtx.remove("_HR_PayrollConcept_ID");
    //m_scriptCtx.put("_HR_PayrollConcept_ID", payrollConcept.getHR_PayrollConcept_ID());
    //Define movement cache
    movements = new Hashtable<Integer, MHRMovement>();
    //Load Payroll Concept
    payrollConcepts = MHRPayrollConcept.getPayrollConcepts(this);
    //Load the Manual movement
    loadMovements(movements, employee.getC_BPartner_ID());
    //Remove movement if this is calculated this way can be calculated again
    if (!concept.isManual())
        movements.remove(concept.get_ID());
    return employee;
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) MPeriod(org.compiere.model.MPeriod) MBPartner(org.compiere.model.MBPartner)

Example 9 with MPeriod

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

the class ImportBudget method definePeriods.

/**
     * @param calendarId
     * @return
     */
private List<MPeriod> definePeriods(int calendarId) {
    int offset = 0;
    MPeriod period = MPeriod.findByCalendar(getCtx(), getAccountDate(), calendarId, null);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(period.getEndDate().getTime());
    calendar.add(Calendar.DATE, 1);
    Timestamp startTime = new Timestamp(calendar.getTimeInMillis());
    Date startDateNextPeriod = new Date(startTime.getTime());
    offset = startDateNextPeriod.getMonth() - period.getStartDate().getMonth();
    glPeriods.add(period);
    glPeriodsDates.add(getAccountDate());
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(getAccountDate().getTime());
    for (int i = 1; i < 12; i++) {
        cal.add(Calendar.MONTH, offset);
        Timestamp nextDate = new Timestamp(cal.getTimeInMillis());
        glPeriodsDates.add(nextDate);
        MPeriod acctPeriod = MPeriod.findByCalendar(getCtx(), nextDate, calendarId, null);
        cal.setTimeInMillis(nextDate.getTime());
        if (acctPeriod != null)
            glPeriods.add(acctPeriod);
    }
    return glPeriods;
}
Also used : MPeriod(org.compiere.model.MPeriod) Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 10 with MPeriod

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

the class ImportBudget method doIt.

/**
     * Perform process.
     * @return Message
     * @throws Exception
     */
protected String doIt() throws Exception {
    // Validate Budget
    X_I_Budget importBudgetCurrent = new X_I_Budget(getCtx(), getRecord_ID(), null);
    int budgetId = importBudgetCurrent.getGL_Budget_ID();
    if (budgetId <= 0)
        budgetId = getId(X_GL_Budget.Table_Name, X_GL_Budget.COLUMNNAME_Name + "=?", get_TrxName(), importBudgetCurrent.getBudgetCode());
    if (budgetId <= 0)
        throw new AdempiereException("@GL_Budget_ID@ @NotFound@");
    else {
        importBudgetCurrent.setGL_Budget_ID(budgetId);
        importBudgetCurrent.saveEx();
    }
    MAcctSchema acctSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
    MAcctSchemaElement[] acctSchemaElements = MAcctSchemaElement.getAcctSchemaElements(acctSchema);
    MPeriod period = MPeriod.get(getCtx(), acctSchema.getC_Period_ID());
    int glCategoryId = DB.getSQLValue(get_TrxName(), "SELECT GL_Category_ID FROM GL_Category WHERE Name = 'Manual'  AND AD_Client_ID =? ", Env.getAD_Client_ID(getCtx()));
    int currencyId = acctSchema.getC_Currency_ID();
    int docTypeId = DB.getSQLValue(get_TrxName(), "SELECT C_DocType_ID FROM C_DocType WHERE Name = 'GL Journal' AND AD_Client_ID =?", Env.getAD_Client_ID(getCtx()));
    int conversionTypeId = DB.getSQLValue(get_TrxName(), "SELECT C_ConversionType_ID FROM C_ConversionType WHERE Value =? ", "S");
    String documentNo = importBudgetCurrent.getBatchDocumentNo();
    int calendarId = period.getC_Calendar_ID();
    if (isDeleteoldimportedrecords())
        Arrays.stream(getBudget(documentNo, true, true)).forEach(importBudgetId -> {
            X_I_Budget importBudget = new X_I_Budget(getCtx(), importBudgetId, null);
            importBudget.deleteEx(true);
        });
    //Validation document
    int[] budgetLinesIds = getBudget(documentNo, false, false);
    validateBudget(budgetLinesIds);
    //AtomicInteger importedRecord = new AtomicInteger(0);
    //AtomicInteger withErrors = new AtomicInteger(0);
    Arrays.stream(budgetLinesIds).forEach(importBudgetId -> {
        Trx.run(trxName -> {
            X_I_Budget importBudget = new X_I_Budget(getCtx(), importBudgetId, trxName);
            importBudget.setI_ErrorMsg(null);
            fillIdValues(importBudget, acctSchemaElements, trxName);
        });
    });
    // Validate  have not Error
    Arrays.stream(budgetLinesIds).forEach(importBudgetId -> {
        X_I_Budget importBudget = new X_I_Budget(getCtx(), importBudgetId, null);
        if (importBudget.getI_ErrorMsg() != null)
            throw new AdempiereException("@GL_BudgetID@ @ProcessFailed@");
    });
    Trx.run(trxName -> generateJournalBatch(trxName, documentNo, glCategoryId, docTypeId, currencyId, conversionTypeId, calendarId, importBudgetCurrent.getGL_Budget_ID()));
    return "@Ok@";
}
Also used : MAcctSchemaElement(org.compiere.model.MAcctSchemaElement) Arrays(java.util.Arrays) MProject(org.compiere.model.MProject) X_I_Budget(org.compiere.model.X_I_Budget) CLogger(org.compiere.util.CLogger) MJournalLine(org.compiere.model.MJournalLine) MJournalBatch(org.compiere.model.MJournalBatch) Env(org.compiere.util.Env) SimpleDateFormat(java.text.SimpleDateFormat) X_GL_Budget(org.compiere.model.X_GL_Budget) MCampaign(org.compiere.model.MCampaign) AtomicReference(java.util.concurrent.atomic.AtomicReference) MAcctSchema(org.compiere.model.MAcctSchema) ArrayList(java.util.ArrayList) MActivity(org.compiere.model.MActivity) MSalesRegion(org.compiere.model.MSalesRegion) BigDecimal(java.math.BigDecimal) Query(org.compiere.model.Query) DB(org.compiere.util.DB) Calendar(java.util.Calendar) MBPartner(org.compiere.model.MBPartner) Msg(org.compiere.util.Msg) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MElementValue(org.compiere.model.MElementValue) MPeriod(org.compiere.model.MPeriod) Trx(org.compiere.util.Trx) MJournal(org.compiere.model.MJournal) DateFormat(java.text.DateFormat) Timestamp(java.sql.Timestamp) X_C_SubAcct(org.compiere.model.X_C_SubAcct) Logger(java.util.logging.Logger) MColumn(org.compiere.model.MColumn) Date(java.sql.Date) I_AD_Column(org.compiere.model.I_AD_Column) List(java.util.List) MOrg(org.compiere.model.MOrg) AdempiereException(org.adempiere.exceptions.AdempiereException) X_A_Asset(org.compiere.model.X_A_Asset) MTable(org.compiere.model.MTable) MAccount(org.compiere.model.MAccount) MProduct(org.compiere.model.MProduct) I_I_Budget(org.compiere.model.I_I_Budget) MAcctSchema(org.compiere.model.MAcctSchema) AdempiereException(org.adempiere.exceptions.AdempiereException) MPeriod(org.compiere.model.MPeriod) X_I_Budget(org.compiere.model.X_I_Budget) MAcctSchemaElement(org.compiere.model.MAcctSchemaElement)

Aggregations

MPeriod (org.compiere.model.MPeriod)10 Timestamp (java.sql.Timestamp)4 MElementValue (org.compiere.model.MElementValue)3 Date (java.sql.Date)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 AdempiereException (org.adempiere.exceptions.AdempiereException)2 MBPartner (org.compiere.model.MBPartner)2 BigDecimal (java.math.BigDecimal)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Arrays (java.util.Arrays)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Logger (java.util.logging.Logger)1 I_AD_Column (org.compiere.model.I_AD_Column)1 I_I_Budget (org.compiere.model.I_I_Budget)1 MAccount (org.compiere.model.MAccount)1 MAcctSchema (org.compiere.model.MAcctSchema)1