Knowledgebase INSERT_LOCATION();

DECLARATIONS
  Static(location_id,String);
  Static(group_type,String);
  Static(name,String);
  Static(area_code,String);
  Static(phoneno,String);
  Static(faxno,STRING);
  Static(address,String);
  Static(mgr,String);
  Static(date_stamp,Date);
  Static(time_stamp,Time);

  Static(db_name,String);
  Static(file_name,String);
  Static(error_name,String);
  Static(s,String);
  Static(work_s,String);
  Static(save_s,String);
  Static(date_save,Date);
  Static(time_save,Time);
  Static(i,Integer);
  Static(j,Integer);
  Static(count,Integer);
  Static(insert_count,Integer);
  Static(update_count,Integer);
  Static(fail_count,Integer);
  Static(show_errors,Boolean);

ACTIONS
  db_name = 'C:\temp\LOCATION';      (* Modify for your environment *)
  file_name = 'C:\temp\INSLOC.DAT';  (* Modify for your environment *)
  error_name = 'C:\temp\INSLOC.ERR'; (* Modify for your environment *)
  date_save = $TODAY;
  time_save = $NOW;
  show_errors = True;
  count = 0;
  insert_count = 0;
  update_count = 0;
  fail_count = 0;

  i = DBUseIndex(db_name,'LOCATION_ID');
  If i <= 0 Then
    Messagebox('Error on DBUseIndex   Error: '+String(i),$ERROR,$OK);
    Exitgroup;
  End;
  DBCommand('','SHOWERRORS=FALSE');
  FCreate(error_name);

  Openwindow(1,1,80,20,YELLOW,BLUE,'Store Profile Insert Program',TRUE);
  GotoXY(3,3);
  Writeln('PROCESSING RECORDS');

  i = FReadln(file_name,s);
  If i <= 0 Then
    Messagebox('Error on FReadln   Error: '+String(i),$ERROR,$OK);
    Exitgroup;
  End;
  While i > 0 Do
    save_s = s;
    count = count + 1;
    GotoXY(3,5);
    Clreol;
    Writeln('Records: ',count);

    Strtoken(s,location_id,',');
    Strtrim(location_id);
    Strltrim(location_id);

    Strtoken(s,group_type,',');
    Strtrim(group_type);
    Strltrim(group_type);

    Strtoken(s,faxno,',');
    Strtrim(faxno);
    Strltrim(faxno);

    Strtoken(s,name,',');
    Strtrim(name);
    Strltrim(name);

    Strtoken(s,area_code,',');
    Strtrim(area_code);
    Strltrim(area_code);

    Strtoken(s,phoneno,',');
    Strtrim(phoneno);
    Strltrim(phoneno);

    Strtoken(s,address,',');
    Strtrim(address);
    Strltrim(address);

    Strtoken(s,mgr,',');
    Strtrim(mgr);
    Strltrim(mgr);

    j = DBGetEqual(db_name,location_id,date_stamp,time_stamp);
    If j > 0 Then
      If date_stamp = date_save and time_stamp = time_save Then
        If show_errors Then
          j = Messagebox('      Error updating store '+location_id+'  Error: '+
                         'Duplicate'+'

                 Continue to show errors?'
                         ,$ERROR,$YESNO);
          If j = 2 Then
            show_errors = False;
          End;
        End;
        FWriteln(error_name,save_s,'$*** Duplicate');
        fail_count = fail_count + 1;
      Else
        date_stamp = date_save;
        time_stamp = time_save;
        J = DBUpdate(db_name,j,location_id,group_type,name,area_code,
                     phoneno,faxno,address,mgr);
        If j < 0 Then
          If show_errors Then
            j = Messagebox('      Error updating store '+location_id+'  Error: '+
                           String(J)+'

                   Continue to show errors?'
                           ,$ERROR,$YESNO);
            If j = 2 Then
              show_errors = False;
            End;
          End;
          FWriteln(error_name,save_s,'$*** UpDate Error: '+String(j));
          fail_count = fail_count + 1;
        Else
          update_count = update_count + 1;
        End;
      End;
    Else
      date_stamp = date_save;
      time_stamp = time_save;
      J = DBInsert(db_name,location_id,group_type,name,area_code,
                   phoneno,faxno,address,mgr);
      If j < 0 Then
        If show_errors Then
          j = Messagebox('      Error inserting store '+location_id+'  Error: '+
                         String(J)+'

                 Continue to show errors?'
                         ,$ERROR,$YESNO);
          If j = 2 Then
            show_errors = False;
          End;
        End;
        FWriteln(error_name,save_s,'$*** Insert Error: '+String(j));
        fail_count = fail_count + 1;
      Else
        insert_count = insert_count + 1;
      End;
    End;
    i=FReadln(file_name,s);
  End;
  FClose(file_name);
  FClose(error_name);
  Writeln;
  Writeln('  Conversion Complete: ',insert_count,' record(s) inserted.');
  Writeln('                       ',update_count,' record(s) updated.');
  Writeln('                       ',fail_count,' record(s) failed.');
  Pause;
  Closewindow;
End;
