unit CreateNewYearForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, Mask, DBCtrls, Buttons, Spin, Gauges,
  ExtCtrls, ComCtrls, Contnrs;

type
  TfrmCreateNewYear = class(TForm)
    Label1: TLabel;
    dsSystemParameters: TDataSource;
    taSystemParameters: TADOTable;
    taSystemParametersStartDate1: TWideStringField;
    taSystemParametersStartDate2: TWideStringField;
    taSystemParametersStartDate3: TWideStringField;
    taSystemParametersStartDate4: TWideStringField;
    taSystemParametersStartDate5: TWideStringField;
    taSystemParametersStartDate6: TWideStringField;
    taSystemParametersStartDate7: TWideStringField;
    taSystemParametersStartDate8: TWideStringField;
    taSystemParametersStartDate9: TWideStringField;
    taSystemParametersStartDate10: TWideStringField;
    taSystemParametersStartDate11: TWideStringField;
    taSystemParametersStartDate12: TWideStringField;
    taSystemParametersEndDate1: TWideStringField;
    taSystemParametersEndDate2: TWideStringField;
    taSystemParametersEndDate3: TWideStringField;
    taSystemParametersEndDate4: TWideStringField;
    taSystemParametersEndDate5: TWideStringField;
    taSystemParametersEndDate6: TWideStringField;
    taSystemParametersEndDate7: TWideStringField;
    taSystemParametersEndDate8: TWideStringField;
    taSystemParametersEndDate9: TWideStringField;
    taSystemParametersEndDate10: TWideStringField;
    taSystemParametersEndDate11: TWideStringField;
    taSystemParametersEndDate12: TWideStringField;
    taSystemParametersNightBegin: TWideStringField;
    taSystemParametersNightEnd: TWideStringField;
    taSystemParametersSysYear: TIntegerField;
    taSystemParametersClockType: TWideStringField;
    taSystemParametersRangeBegin: TWideStringField;
    taSystemParametersRangeEnd: TWideStringField;
    taSystemParametersCompanyName: TWideStringField;
    taSystemParametersCardInterval: TWordField;
    taSystemParametersPassword: TWideStringField;
    Label2: TLabel;
    bibSave: TBitBtn;
    bibCancel: TBitBtn;
    Button1: TBitBtn;
    progressPanel: TPanel;
    Label3: TLabel;
    ProgressBarAll: TProgressBar;
    ProgressBarSub: TProgressBar;
    QuCalendars: TADOQuery;
    QuCalDetail: TADOQuery;
    QuCalSamples: TADOQuery;
    QuCalSamDetail: TADOQuery;
    QuCalendarsCode: TIntegerField;
    QuCalendarsName: TWideStringField;
    QuCalendarsCalSamCode: TIntegerField;
    QuCalDetailCode: TIntegerField;
    QuCalDetailWorkBeginDate: TWideStringField;
    QuCalDetailWorkBeginTime: TWideStringField;
    QuCalDetailInterruptBeginTime: TWideStringField;
    QuCalDetailInterruptEndTime: TWideStringField;
    QuCalDetailWorkEndDate: TWideStringField;
    QuCalDetailWorkEndTime: TWideStringField;
    QuCalDetailAddBeginDate: TWideStringField;
    QuCalDetailAddBeginTime: TWideStringField;
    QuCalDetailAddEndDate: TWideStringField;
    QuCalDetailAddEndTime: TWideStringField;
    QuCalDetailEndOfDayDate: TWideStringField;
    QuCalDetailEndOfDayTime: TWideStringField;
    QuCalSamplesCode: TIntegerField;
    QuCalSamplesName: TWideStringField;
    QuCalSamplesStartDate: TWideStringField;
    QuCalSamplesPeriodLen: TSmallintField;
    QuCalSamplesCNewYear: TSmallintField;
    QuCalSamDetailCode: TIntegerField;
    QuCalSamDetailWorkBeginDate: TWideStringField;
    QuCalSamDetailWorkBeginTime: TWideStringField;
    QuCalSamDetailInterruptBeginTime: TWideStringField;
    QuCalSamDetailInterruptEndTime: TWideStringField;
    QuCalSamDetailWorkEndDate: TWideStringField;
    QuCalSamDetailWorkEndTime: TWideStringField;
    QuCalSamDetailAddBeginDate: TWideStringField;
    QuCalSamDetailAddBeginTime: TWideStringField;
    QuCalSamDetailAddEndDate: TWideStringField;
    QuCalSamDetailAddEndTime: TWideStringField;
    QuCalSamDetailEndOfDayDate: TWideStringField;
    QuCalSamDetailEndOfDayTime: TWideStringField;
    QuCalSamDetailHoliday: TSmallintField;
    QuCalDetailHoliday: TSmallintField;
    EditNewYear: TEdit;
    Label4: TLabel;
    EditAllYears: TEdit;
    EditYear: TEdit;
    procedure FormActivate(Sender: TObject);
    procedure bibCancelClick(Sender: TObject);
    procedure bibSaveClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
  public
    { Public declarations }
  end;

var
  frmCreateNewYear: TfrmCreateNewYear;

implementation

uses TasMainData, GeneralUtilities, TasMainForm, EOSDateTime, utils, Film,
  helpFileForm, SystemParametersForm, DataAccess, Types, EOSYearConversion,
  EOSDateParser;

{$R *.dfm}

procedure TfrmCreateNewYear.FormActivate(Sender: TObject);
var
  Years: TIntegerDynArray;
begin
  if taSystemParameters.Active then
    taSystemParameters.Close;
  taSystemParameters.Open;
  EditYear.Text := taSystemParametersSysYear.AsString;
  Years := frmTasMain.GetAllYearsFromRegistry_YYYY;
  if Length(Years) > 0 then
  begin
    if Length(Years) = 1 then
      EditAllYears.Text := IntToStr(Years[0])
    else
      EditAllYears.Text := Format('%d - %d', [Years[Length(Years) - 1], Years[0]]);

//  if Length(dbeYear.Text) = 4 then
    EditNewYear.Text := IntToStr(Years[Length(Years) - 1] +1);
  end
  else
  begin
    EditNewYear.Text := IntToStr(taSystemParametersSysYear.AsInteger + 1);
    EditAllYears.Text := '-';
  end;
end;

procedure TfrmCreateNewYear.bibCancelClick(Sender: TObject);
begin
  Self.Close;
end;

procedure TfrmCreateNewYear.bibSaveClick(Sender: TObject);
var
  //as 9101b2
  MyDBNameStr: String;
  i: Integer;
  //as
  //as 9201b2
  qTemp, QuTemp, QuHelp, Qhlp: TADOQuery;
  tmpWorkingDay: TWorkingDay;
  tmpObjectList: TObjectList;
  FromDateSrc: TDateStr;
  UntilDateSrc: TDateStr;
  FromDateDest: TDateStr;
  MyFromDateDest: TDateStr;
  UntilDateDest: TDateStr;
  Displacement, MyVarFor, MyCompYear, MyTmp: Integer;
  MyYear: String;
  NewYear_YYYY: Integer;
begin
  if frmTasMain.OpenRegistry('TasMain') then
    MyDBNameStr := frmTasMain.ReadValue('DataBaseNames', '');
  if Pos('TasDb_' + EditNewYear.Text, MyDBNameStr) > 0 then
  begin
    if not IsOtherLanguage then
      EOSMessageDlg('سال انتخابي قبلا ايجاد شده است .', mtWarning, [mbOK], 0)
    else
    begin
      if IsArabic then
        EOSMessageDlg('سنه المختاره تم انشائها مسبقا .', mtWarning, [mbOK], 0)
      else
        EOSMessageDlg(' Selected year already created . ', mtWarning, [mbOK], 0);
    end;
  end
  else
  begin
    NewYear_YYYY := TEOSDateParser.ConvertYear_YYYY_Int(frmTasMain.CalendarSystem, EditNewYear.Text);
    Label3.Left := Round((progressPanel.ClientWidth - Label3.Width) / 2);
    progressPanel.Visible := True;
    try
      Qhlp := TADOQuery.Create(Self);
      QuHelp := TADOQuery.Create(Self);
      QuTemp := TADOQuery.Create(Self);
      qTemp := TADOQuery.Create(Self);
      try
        Qhlp.Connection := dmTasMain.MainDb;
        QuHelp.Connection := dmTasMain.MainDb;
        QuTemp.Connection := dmTasMain.MainDb;
        qTemp.Connection := dmTasMain.MainDb;
        QuTemp.SQL.Clear;
        QuTemp.SQL.Add(' Select Code, CalSamCode ');
        QuTemp.SQL.Add(' From Calendars ');
        QuTemp.SQL.Add(' Where CalSamCode In ');
        QuTemp.SQL.Add('                   (Select Code From CalSamples ');
        QuTemp.SQL.Add('                    Where CNewYear = 1) ');
        QuTemp.Open;
        if not QuTemp.IsEmpty then
        begin
          ProgressBarAll.Min := 0;
          ProgressBarSub.Min := 0;
          ProgressBarAll.Position := 0;
          ProgressBarSub.Position := 0;
          ProgressBarAll.Max := QuTemp.RecordCount;
          QuTemp.First;
          while not QuTemp.Eof do
          begin
            QuCalendars.Close;
            QuCalendars.Open;
            QuCalendars.Locate('Code', QuTemp.FieldByName('Code').AsInteger, []);

            QuCalDetail.Close;
            QuCalDetail.Parameters.ParamByName('PCode1').Value := QuTemp.FieldByName('Code').AsInteger;
            QuCalDetail.Open;

            QuCalSamples.Close;
            QuCalSamples.Open;
            QuCalSamples.Locate('Code', QuTemp.FieldByName('CalSamCode').AsInteger, []);

            QuCalSamDetail.Close;
            QuCalSamDetail.Parameters.ParamByName('PCode2').Value := QuTemp.FieldByName('CalSamCode').AsInteger;
            QuCalSamDetail.Open;

            MyCompYear := NewYear_YYYY;

            MyVarFor := StrToIntDef(Copy(QuCalSamples.FieldByName('StartDate').AsString, 1, 4), 0);
            if MyVarFor > MyCompYear then
            begin
              QuTemp.Next;
              Continue;
            end;

            tmpObjectList := TObjectList.Create;
            try
              QuCalSamDetail.First;
              FromDateSrc := QuCalSamDetail.FieldByName('WorkBeginDate').AsString;
              QuCalSamDetail.Last;
              UntilDateSrc := QuCalSamDetail.FieldByName('WorkBeginDate').AsString;

              QuHelp.Close;
              QuHelp.SQL.Clear;
              QuHelp.SQL.Add(' Select Max(WorkBeginDate) MaxDate ');
              QuHelp.SQL.Add(' From CalendarDetail ');
              QuHelp.SQL.Add(' Where Code = ' + QuTemp.FieldByName('Code').AsString + ' ');
              QuHelp.Open;
              MyFromDateDest := IncDate(frmTasMain.CalendarSystem, QuHelp.FieldByName('MaxDate').AsString);  // OK
              if Trim(MyFromDateDest) = '' then
              begin
                Qhlp.SQL.Clear;
                Qhlp.SQL.Add(' Select SubString(Years, 1, 2) SubYear From systemparameters ');
                Qhlp.Open;
                MyYear := Qhlp.FieldByName('SubYear').AsString;
                MyTmp := StrToIntDef(MyYear, 0);
                if (Trim(MyYear) = '') or (MyTmp = 0) then
                begin
                  QuTemp.Next;
                  Continue;
                end;
                MyTmp := NewYear_YYYY;
                MyYear := IntToStr(MyTmp);
                MyFromDateDest := MyYear + '/01/01';
              end;

              FromDateDest := FromDateSrc;
              UntilDateDest := IntToStr(MyCompYear + 1) + Copy(FromDateSrc, 5, 6);

              qTemp.Close;
              qTemp.SQL.Clear;
              with qTemp.SQL do
              begin
                Clear;
                Add(' Select * From CalSamDetail ');
                Add(' Where (Code = ' + QuCalSamDetail.FieldByName('Code').AsString + ') ');
                Add(' Order By Code, WorkBeginDate ');
              end;
              qTemp.Open;
              qTemp.First;

              while not qTemp.Eof do
              begin
                tmpWorkingDay := TWorkingDay.Create;
                tmpWorkingDay.WorkBegin.Date := qTemp.FieldByName('WorkBeginDate').AsString;
                tmpWorkingDay.WorkBegin.Time := qTemp.FieldByName('WorkBeginTime').AsString;
                tmpWorkingDay.WorkEnd.Date := qTemp.FieldByName('WorkEndDate').AsString;
                tmpWorkingDay.WorkEnd.Time := qTemp.FieldByName('WorkEndTime').AsString;
                tmpWorkingDay.AddBegin.Date := qTemp.FieldByName('AddBeginDate').AsString;
                tmpWorkingDay.AddBegin.Time := qTemp.FieldByName('AddBeginTime').AsString;
                tmpWorkingDay.AddEnd.Date := qTemp.FieldByName('AddEndDate').AsString;
                tmpWorkingDay.AddEnd.Time := qTemp.FieldByName('AddEndTime').AsString;
                tmpWorkingDay.EndOfDay.Date := qTemp.FieldByName('EndOfDayDate').AsString;
                tmpWorkingDay.EndOfDay.Time := qTemp.FieldByName('EndOfDayTime').AsString;
                tmpWorkingDay.Holiday := qTemp.FieldByName('Holiday').AsInteger;
                tmpWorkingDay.BeginInterrupt.Time := qTemp.FieldByName('InterruptBeginTime').AsString;
                tmpWorkingDay.EndInterrupt.Time := qtemp.fieldbyname('InterruptEndTime').AsString;
                tmpObjectList.Add(tmpWorkingDay);
                qTemp.Next;
              end;

              QuCalDetail.Close;
              QuCalDetail.Open;

              Displacement := DateDifference(frmTasMain.CalendarSystem, UntilDateSrc, FromDateSrc) + 1; // OK
              MyVarFor := DateDifference(frmTasMain.CalendarSystem, UntilDateDest, FromDateDest) - 1; // OK

              if MyVarFor < 0 then
              begin
                QuTemp.Next;
                Continue;
              end;

              ProgressBarSub.Min := 0;
              ProgressBarSub.Position := 0;
              ProgressBarSub.Max := MyVarFor;

              for i := 0 to MyVarFor do
              begin
                tmpWorkingDay := TWorkingDay(tmpObjectList[i mod (tmpObjectList.Count)]);
                if IncDate(frmTasMain.CalendarSystem, tmpWorkingDay.WorkBegin.Date, Displacement + // OK
                            (i div (tmpObjectList.Count)) * (tmpObjectList.Count)) >= MyFromDateDest then
                begin
                  try
                    QuCalDetail.Append;
                  except

                  end;
                  QuCalDetail.FieldByName('Code').AsString := QuTemp.FieldByName('Code').AsString;
                  QuCalDetail.FieldByName('WorkBeginDate').AsString :=
                    IncDate(frmTasMain.CalendarSystem, tmpWorkingDay.WorkBegin.Date, Displacement + // OK
                            (i div (tmpObjectList.Count)) * (tmpObjectList.Count));
                  QuCalDetail.FieldByName('WorkBeginTime').AsString := tmpWorkingDay.WorkBegin.Time;
                  QuCalDetail.FieldByName('WorkEndDate').AsString :=
                    IncDate(frmTasMain.CalendarSystem, tmpWorkingDay.WorkEnd.Date, Displacement + // OK
                            (i div (tmpObjectList.Count)) * (tmpObjectList.Count));
                  QuCalDetail.FieldByName('WorkEndTime').AsString := tmpWorkingDay.WorkEnd.Time;
                  QuCalDetail.FieldByName('AddBeginDate').AsString :=
                    IncDate(frmTasMain.CalendarSystem, tmpWorkingDay.AddBegin.Date, Displacement + // OK
                    (i div (tmpObjectList.Count)) * (tmpObjectList.Count));
                  QuCalDetail.FieldByName('AddBeginTime').AsString := tmpWorkingDay.AddBegin.Time;
                  QuCalDetail.FieldByName('AddEndDate').AsString :=
                    IncDate(frmTasMain.CalendarSystem, tmpWorkingDay.AddEnd.Date, Displacement + // OK
                    (i div (tmpObjectList.Count)) * (tmpObjectList.Count));
                  QuCalDetail.FieldByName('AddEndTime').AsString := tmpWorkingDay.AddEnd.Time;
                  QuCalDetail.FieldByName('EndOfDayDate').AsString :=
                    IncDate(frmTasMain.CalendarSystem, tmpWorkingDay.EndOfDay.Date, Displacement + // OK
                    (i div (tmpObjectList.Count)) * (tmpObjectList.Count));
                  QuCalDetail.FieldByName('EndOfDayTime').AsString := tmpWorkingDay.EndOfDay.Time;
                  QuCalDetail.FieldByName('Holiday').AsInteger := tmpWorkingDay.Holiday;
                  QuCalDetail.FieldByName('InterruptBeginTime').AsString := tmpWorkingDay.BeginInterrupt.Time;
                  QuCalDetail.FieldByName('InterruptEndTime').AsString := tmpWorkingDay.EndInterrupt.Time;
                  try
                    QuCalDetail.Post;
                  except

                  end;
                end;
                ProgressBarSub.Position := ProgressBarSub.Position + 1;
              end;

              QuCalDetail.Close;

            finally
              FreeAndNil(tmpObjectList);
            end;
            QuTemp.Next;
            ProgressBarAll.Position := ProgressBarAll.Position + 1;
            ProgressBarAll.Refresh;
          end;
        end
        else
        begin
          for i := 0 to 100 do
          begin
            Application.ProcessMessages;
            Label3.Caption := 'چند لحظه صبر کنيد';
            if not IsOtherLanguage then
              Label3.Caption := 'چند لحظه صبر کنيد'
            else
            begin
              if IsArabic then
                Label3.Caption := 'الانتظار...'
              else
                Label3.Caption := 'Please wait...';
            end;
            ProgressBarSub.Position := i;
            ProgressBarAll.Position := i;
            Sleep(10);
          end;
        end;
      finally
        progressPanel.Visible := False;
        FreeAndNil(qTemp);
        FreeAndNil(QuTemp);
        FreeAndNil(QuHelp);
        FreeAndNil(Qhlp);
      end;
    except
      EOSMessageDlg('در زمان ايجاد تقويم سال جديد خطا رخ داد ، سال جديد ايجاد شد ولي جزئيات تقويم ها کنترل شود .',mtError,[mbOK],0);
      QuCalDetail.Close;
      QuCalDetail.Open;
    end;

    if (Pos('TasDb_' + EditNewYear.Text, frmTasMain.DataBaseNames) = 0) then
      frmTasMain.DataBaseNames := frmTasMain.DataBaseNames + 'TasDb_' + EditNewYear.Text + ',';
    frmTasMain.AddSystemYearsToDB(EditNewYear.Text);
    frmTasMain.WriteValue('DataBaseNames', frmTasMain.DataBaseNames);
    ModalResult := mrOk;

  end;
end;

procedure TfrmCreateNewYear.Button1Click(Sender: TObject);
begin
  film.SlideName := ExtractFilePath(ParamStr(0))+'MultiMedia\AutoPlay\Videos\'+ExtractFileName('31-ejade sale jadid.wmv') ;
  Application.CreateForm(TfmhelpFile, fmhelpFile);
  fmhelpFile.ShowFile('47', frmCreateNewYear.Caption);
end;

end.
