private void createMovements() throws Exception {
  m_scriptCtx.clear();
  m_scriptCtx.put("process",this);
  m_scriptCtx.put("_Process",getHR_Process_ID());
  m_scriptCtx.put("_Period",getHR_Period_ID());
  m_scriptCtx.put("_Payroll",getHR_Payroll_ID());
  m_scriptCtx.put("_Department",getHR_Department_ID());
  log.info("info data - " + " Process: " + getHR_Process_ID() + ", Period: "+ getHR_Period_ID()+ ", Payroll: "+ getHR_Payroll_ID()+ ", Department: "+ getHR_Department_ID());
  MHRPeriod hrPeriod=null;
  if (getHR_Period_ID() > 0) {
    hrPeriod=MHRPeriod.get(getCtx(),getHR_Period_ID());
  }
 else {
    hrPeriod=new MHRPeriod(getCtx(),0,get_TrxName());
    MPeriod period=MPeriod.get(getCtx(),getDateAcct(),getAD_Org_ID());
    if (period != null) {
      hrPeriod.setStartDate(period.getStartDate());
      hrPeriod.setEndDate(period.getEndDate());
    }
 else {
      hrPeriod.setStartDate(getDateAcct());
      hrPeriod.setEndDate(getDateAcct());
    }
  }
  m_dateFrom=hrPeriod.getStartDate();
  m_dateTo=hrPeriod.getEndDate();
  m_scriptCtx.put("_From",m_dateFrom);
  m_scriptCtx.put("_To",m_dateTo);
  m_scriptCtx.put("_Period",hrPeriod.getPeriodNo());
  if (getHR_Payroll_ID() > 0) {
    m_HR_Payroll_ID=getHR_Payroll_ID();
  }
  if (getHR_Department_ID() > 0) {
    m_HR_Department_ID=getHR_Department_ID();
  }
  if (getHR_Job_ID() > 0) {
    m_HR_Job_ID=getHR_Job_ID();
  }
  int no=DB.executeUpdateEx("DELETE FROM HR_Movement m WHERE HR_Process_ID=? AND IsManual<>?",new Object[]{getHR_Process_ID(),true},get_TrxName());
  log.info("HR_Movement deleted #" + no);
  linesConcept=MHRPayrollConcept.getPayrollConcepts(this);
  MBPartner[] linesEmployee=MHREmployee.getEmployees(this);
  int count=1;
  for (  MBPartner bp : linesEmployee) {
    log.info("Employee " + count + "  ---------------------- "+ bp.getName());
    count++;
    m_C_BPartner_ID=bp.get_ID();
    m_employee=MHREmployee.getActiveEmployee(getCtx(),m_C_BPartner_ID,get_TrxName());
    m_scriptCtx.remove("_DateStart");
    m_scriptCtx.remove("_DateEnd");
    m_scriptCtx.remove("_Days");
    m_scriptCtx.remove("_C_BPartner_ID");
    m_scriptCtx.put("_DateStart",m_employee.getStartDate());
    m_scriptCtx.put("_DateEnd",m_employee.getEndDate() == null ? TimeUtil.getDay(2999,12,31) : m_employee.getEndDate());
    m_scriptCtx.put("_Days",TimeUtil.getDaysBetween(hrPeriod.getStartDate(),hrPeriod.getEndDate()) + 1);
    m_scriptCtx.put("_C_BPartner_ID",bp.getC_BPartner_ID());
    if (getHR_Period_ID() > 0)     createCostCollectorMovements(bp.get_ID(),hrPeriod);
    m_movement.clear();
    loadMovements(m_movement,m_C_BPartner_ID);
    for (    MHRPayrollConcept pc : linesConcept) {
      m_HR_Concept_ID=pc.getHR_Concept_ID();
      MHRConcept concept=MHRConcept.get(getCtx(),m_HR_Concept_ID);
      boolean printed=pc.isPrinted() || concept.isPrinted();
      MHRMovement movement=m_movement.get(concept.get_ID());
      if (movement == null) {
        movement=createMovementFromConcept(concept,printed);
        movement.setHR_Payroll_ID(pc.getHR_Payroll_ID());
        movement.setHR_PayrollConcept_ID(pc.getHR_PayrollConcept_ID());
        movement=m_movement.get(concept.get_ID());
      }
      if (movement == null) {
        throw new AdempiereException("Concept " + concept.getValue() + " not created");
      }
    }
    for (    MHRMovement m : m_movement.values()) {
      MHRConcept c=(MHRConcept)m.getHR_Concept();
      if (c.isManual() || m.isEmpty()) {
        log.fine("Skip saving " + m);
      }
 else {
        boolean saveThisRecord=m.isPrinted() || c.isPaid() || c.isPrinted();
        if (saveThisRecord)         m.saveEx();
      }
    }
  }
  if (getHR_Period_ID() > 0) {
    hrPeriod.setProcessed(true);
    hrPeriod.saveEx();
  }
}

private void createMovements ( ) throws Exception { m_scriptCtx . clear ( ) ; m_scriptCtx . put ( "process" , this ) ; m_scriptCtx . put ( "_Process" , getHR_Process_ID ( ) ) ; m_scriptCtx . put ( "_Period" , getHR_Period_ID ( ) ) ; m_scriptCtx . put ( "_Payroll" , getHR_Payroll_ID ( ) ) ; m_scriptCtx . put ( "_Department" , getHR_Department_ID ( ) ) ; log . info ( "info data - " + " Process: " + getHR_Process_ID ( ) + ", Period: " + getHR_Period_ID ( ) + ", Payroll: " + getHR_Payroll_ID ( ) + ", Department: " + getHR_Department_ID ( ) ) ; MHRPeriod hrPeriod = null ; if ( getHR_Period_ID ( ) > 0 ) { hrPeriod = MHRPeriod . get ( getCtx ( ) , getHR_Period_ID ( ) ) ; } else { hrPeriod = new MHRPeriod ( getCtx ( ) , 0 , get_TrxName ( ) ) ; MPeriod period = MPeriod . get ( getCtx ( ) , getDateAcct ( ) , getAD_Org_ID ( ) ) ; if ( period != null ) { hrPeriod . setStartDate ( period . getStartDate ( ) ) ; hrPeriod . setEndDate ( period . getEndDate ( ) ) ; } else { hrPeriod . setStartDate ( getDateAcct ( ) ) ; hrPeriod . setEndDate ( getDateAcct ( ) ) ; } } m_dateFrom = hrPeriod . getStartDate ( ) ; m_dateTo = hrPeriod . getEndDate ( ) ; m_scriptCtx . put ( "_From" , m_dateFrom ) ; m_scriptCtx . put ( "_To" , m_dateTo ) ; m_scriptCtx . put ( "_Period" , hrPeriod . getPeriodNo ( ) ) ; if ( getHR_Payroll_ID ( ) > 0 ) { m_HR_Payroll_ID = getHR_Payroll_ID ( ) ; } if ( getHR_Department_ID ( ) > 0 ) { m_HR_Department_ID = getHR_Department_ID ( ) ; } if ( getHR_Job_ID ( ) > 0 ) { m_HR_Job_ID = getHR_Job_ID ( ) ; } int no = DB . executeUpdateEx ( "DELETE FROM HR_Movement m WHERE HR_Process_ID=? AND IsManual<>?" , new Object [ ] { getHR_Process_ID ( ) , true } , get_TrxName ( ) ) ; log . info ( "HR_Movement deleted #" + no ) ; linesConcept = MHRPayrollConcept . getPayrollConcepts ( this ) ; MBPartner [ ] linesEmployee = MHREmployee . getEmployees ( this ) ; int count = 1 ; for ( MBPartner bp : linesEmployee ) { log . info ( "Employee " + count + "  ---------------------- " + bp . getName ( ) ) ; count ++ ; m_C_BPartner_ID = bp . get_ID ( ) ; m_employee = MHREmployee . getActiveEmployee ( getCtx ( ) , m_C_BPartner_ID , get_TrxName ( ) ) ; m_scriptCtx . remove ( "_DateStart" ) ; m_scriptCtx . remove ( "_DateEnd" ) ; m_scriptCtx . remove ( "_Days" ) ; m_scriptCtx . remove ( "_C_BPartner_ID" ) ; m_scriptCtx . put ( "_DateStart" , m_employee . getStartDate ( ) ) ; m_scriptCtx . put ( "_DateEnd" , m_employee . getEndDate ( ) == null ? TimeUtil . getDay ( 2999 , 12 , 31 ) : m_employee . getEndDate ( ) ) ; m_scriptCtx . put ( "_Days" , TimeUtil . getDaysBetween ( hrPeriod . getStartDate ( ) , hrPeriod . getEndDate ( ) ) + 1 ) ; m_scriptCtx . put ( "_C_BPartner_ID" , bp . getC_BPartner_ID ( ) ) ; if ( getHR_Period_ID ( ) > 0 ) createCostCollectorMovements ( bp . get_ID ( ) , hrPeriod ) ; m_movement . clear ( ) ; loadMovements ( m_movement , m_C_BPartner_ID ) ; for ( MHRPayrollConcept pc : linesConcept ) { m_HR_Concept_ID = pc . getHR_Concept_ID ( ) ; MHRConcept concept = MHRConcept . get ( getCtx ( ) , m_HR_Concept_ID ) ; boolean printed = pc . isPrinted ( ) || concept . isPrinted ( ) ; MHRMovement movement = m_movement . get ( concept . get_ID ( ) ) ; if ( movement == null ) { movement = createMovementFromConcept ( concept , printed ) ; movement . setHR_Payroll_ID ( pc . getHR_Payroll_ID ( ) ) ; movement . setHR_PayrollConcept_ID ( pc . getHR_PayrollConcept_ID ( ) ) ; movement = m_movement . get ( concept . get_ID ( ) ) ; } if ( movement == null ) { throw new AdempiereException ( "Concept " + concept . getValue ( ) + " not created" ) ; } } for ( MHRMovement m : m_movement . values ( ) ) { MHRConcept c = ( MHRConcept ) m . getHR_Concept ( ) ; if ( c . isManual ( ) || m . isEmpty ( ) ) { log . fine ( "Skip saving " + m ) ; } else { boolean saveThisRecord = m . isPrinted ( ) || c . isPaid ( ) || c . isPrinted ( ) ; if ( saveThisRecord ) m . saveEx ( ) ; } } } if ( getHR_Period_ID ( ) > 0 ) { hrPeriod . setProcessed ( true ) ; hrPeriod . saveEx ( ) ; } }
create movements for corresponding process , period
