global class NFM105ErrorBatch implements Database.Batchable<sObject>, Database.Stateful {
  public string query;
  public list<string> IDlist;
  global NFM105ErrorBatch() {
  }
  global NFM105ErrorBatch( string str) {
    this.query = str;
  }
  global NFM105ErrorBatch(list<string> IDlist) {
    this.IDlist = IDlist;
  }
  global Database.QueryLocator start(Database.BatchableContext bc) {
    if (string.isNotBlank(query)) {
      return Database.getQueryLocator(query);
    } else if (IDlist != null) {
      query = 'select id from repair__c where id in : IDlist' ;
      return Database.getQueryLocator(query);
    } else {
      query = 'select id from repair__c ' +
              'where test__c = 0 and Repair_Quotation_Id__r.test__c > 0 ' +
              '  and NFM105__c >= 524898  ';
      return Database.getQueryLocator(query);
    }
  }
  global void execute(Database.BatchableContext BC, List<repair__c> repairlist) {
    list<id> RepairIDlist = new list<id>();
    for (repair__c tempRepair : repairlist) {
      RepairIDlist.add(tempRepair.id);
    }
    list<Repair_Quotation_reason__c> qrsnList =
      [ select id , LocationGroupName__c, LocationName__c,
        PhenomenonName__c, CauseName__c,
        DescriptionName__c, Repair_quotation__r.Repair__c, Repair_quotation__c
        , ActivityDescription__c
        from Repair_Quotation_reason__c where
        Repair_quotation__r.Repair__c in : RepairIDlist
        and Repair_quotation__r.LinkToRepairOrder__c = true
      ];
    list<Repair_reason__c> insertRepairList = new list<Repair_reason__c> ();
    for (Repair_Quotation_reason__c qrsn : qrsnList) {
      Repair_reason__c rrsn =
        new Repair_reason__c(Repair__c = qrsn.Repair_quotation__r.Repair__c,
                             Repair_quotation__c = qrsn.Repair_quotation__c );
      insertRepairList.add(rrsn);
      rrsn.LocationGroupName__c    = qrsn.LocationGroupName__c;
      rrsn.LocationName__c         = qrsn.LocationName__c;
      rrsn.PhenomenonName__c       = qrsn.PhenomenonName__c;
      rrsn.CauseName__c            = qrsn.CauseName__c;
      rrsn.DescriptionName__c      = qrsn.DescriptionName__c ;
      rrsn.ActivityDescription__c      = qrsn.ActivityDescription__c ;
      rrsn.isMannual__c = true;

    }
    if (insertRepairList.size() > 0) {
      insert insertRepairList;
    }

  }
  global void finish(Database.BatchableContext BC) {
  }
   @TestVisible
  private static void test11(){
    Integer i = 0;
   
  }

}