unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus,
  Vcl.ComCtrls, Vcl.ToolWin, Vcl.ImgList, Vcl.Grids, Vcl.DBGrids, Data.DB,
  IdHashMessageDigest, Vcl.StdCtrls, DBGridEhGrouping, ToolCtrlsEh,
  DBGridEhToolCtrls, DynVarsEh, EhLibVCL, GridsEh, DBAxisGridsEh, DBGridEh,
  frxClass, frxDBSet, Data.Win.ADODB, EhLibADO, DbUtilsEh, Registry;

type
  TfMain = class(TForm)
    mMain: TMainMenu;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N10: TMenuItem;
    N11: TMenuItem;
    N13: TMenuItem;
    N14: TMenuItem;
    N16: TMenuItem;
    N18: TMenuItem;
    N19: TMenuItem;
    N20: TMenuItem;
    N22: TMenuItem;
    N23: TMenuItem;
    N24: TMenuItem;
    ilMain: TImageList;
    StatusBar1: TStatusBar;
    dsMain: TDataSource;
    N1: TMenuItem;
    dsStock: TDataSource;
    frxInReport: TfrxReport;
    printMenu: TPopupMenu;
    N9: TMenuItem;
    N12: TMenuItem;
    N17: TMenuItem;
    qInReport: TADOQuery;
    qInReportid: TAutoIncField;
    qInReportdate: TDateTimeField;
    qInReportbrand_id: TSmallintField;
    qInReportdevtype_id: TSmallintField;
    qInReportstatus: TWideStringField;
    qInReportclient_id: TSmallintField;
    qInReportmodel: TWideStringField;
    qInReportserial: TWideStringField;
    qInReportuser_id: TSmallintField;
    qInReportdate_end: TDateTimeField;
    qInReportdefect: TWideMemoField;
    qInReportcomment: TWideMemoField;
    qInReportwarranty: TIntegerField;
    qInReportuser: TWideStringField;
    qInReportclient: TWideStringField;
    qInReportphone: TWideStringField;
    qInReportbrand: TWideStringField;
    qInReportdevtype: TWideStringField;
    InReport: TfrxDBDataset;
    frxOutReport: TfrxReport;
    qOutReport: TADOQuery;
    OutReport: TfrxDBDataset;
    qOutReportname: TWideStringField;
    qOutReportprice: TFloatField;
    qOutReportquantity: TIntegerField;
    qOutReportmain_id: TIntegerField;
    qOutReporttotal: TFloatField;
    frxFixReport: TfrxReport;
    pcMain: TPageControl;
    tsFix: TTabSheet;
    ToolBar1: TToolBar;
    tbAdd: TToolButton;
    tbEdit: TToolButton;
    Separator1: TToolButton;
    tbDelete: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    gFix: TDBGridEh;
    tsStock: TTabSheet;
    ToolBar3: TToolBar;
    ToolButton22: TToolButton;
    ToolButton23: TToolButton;
    Separator4: TToolButton;
    ToolButton24: TToolButton;
    gStock: TDBGridEh;
    procedure N7Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N24Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure N22Click(Sender: TObject);
    procedure N14Click(Sender: TObject);
    procedure N16Click(Sender: TObject);
    procedure N11Click(Sender: TObject);
    procedure N10Click(Sender: TObject);
    procedure N19Click(Sender: TObject);
    procedure tbEditClick(Sender: TObject);
    procedure ToolButton24Click(Sender: TObject);
    procedure ToolButton22Click(Sender: TObject);
    procedure ToolButton23Click(Sender: TObject);
    procedure tbAddClick(Sender: TObject);
    procedure N9Click(Sender: TObject);
    procedure N12Click(Sender: TObject);
    procedure N17Click(Sender: TObject);
    procedure tbDeleteClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure pcMainChange(Sender: TObject);
  private
    { Private declarations }
  public
    procedure DisplayHint(Sender: TObject);
    function md5(s: string): string;
    var db:string;
        user_name:string;
        user_id:integer;
  end;

var
  fMain: TfMain;

implementation

{$R *.dfm}

uses Unit3, Unit4, Unit2, inifiles, Unit6, Unit8, Unit10, Unit14, Unit16,
     Unit18, Unit19, Unit21, Unit22, Unit26;

function TfMain.md5(s: string): string;
begin
   Result := '';
   with TIdHashMessageDigest5.Create do begin
      try
         Result := AnsiLowerCase(HashStringAsHex(s));
      finally
         Free;
      end;
   end;
end;

procedure TfMain.DisplayHint(Sender: TObject);
   var
      Counter, NumComps: integer;
begin
   with Screen.ActiveForm do begin
      NumComps := ControlCount - 1;
      for Counter := 0 to NumComps do begin
         if (TControl(Controls[Counter]).Name = 'StatusBar1') then begin
            if (Application.Hint = '') then
               TStatusBar(Controls[Counter]).SimpleText := ''
            else
               TStatusBar(Controls[Counter]).SimpleText := Application.Hint;
            break;
         end;
      end;
   end;
end;

procedure SaveSettings;
   var
      reg: TRegistry;
begin
   reg := TRegistry.Create;
   reg.RootKey := HKEY_CURRENT_USER;
   reg.OpenKey('\SOFTWARE\SCAudit', true);
   reg.WriteInteger('Left', fMain.Left);
   reg.WriteInteger('Top', fMain.Top);
   reg.WriteInteger('Width', fMain.Width);
   reg.WriteInteger('Height', fMain.Height);
   reg.Free;
end;

procedure LoadSettings;
   var
      reg: TRegistry;
begin
   reg := TRegistry.Create;
   reg.RootKey := HKEY_CURRENT_USER;
   reg.OpenKey('\SOFTWARE\SCAudit', true);
   if reg.ValueExists('Left') then
      fMain.Left := reg.ReadInteger('Left');
   if reg.ValueExists('Top') then
      fMain.Top := reg.ReadInteger('Top');
   if reg.ValueExists('Width') then
      fMain.Width := reg.ReadInteger('Width');
   if reg.ValueExists('Height') then
      fMain.Height := reg.ReadInteger('Height');
   reg.free;
end;

procedure TfMain.FormCreate(Sender: TObject);
begin
   Application.OnHint := DisplayHint;
   DbUtilsEh.SQLFilterMarker := '1=1 AND'
end;

procedure TfMain.FormDestroy(Sender: TObject);
begin
   SaveSettings;
end;

procedure TfMain.FormShow(Sender: TObject);
   var
      Ini: TInifile;
      port:integer;
      user, pass, host, driver : string;
begin
   Ini:=TiniFile.Create(extractfilepath(paramstr(0))+'Config.ini');

   LoadSettings;

   user:=Ini.ReadString('MySQL','User','root');
   pass:=Ini.ReadString('MySQL','Password','000123zxc');
   host:=Ini.ReadString('MySQL','Host','127.0.0.1');
   port:=Ini.ReadInteger('MySQL','Port',3306);
   db:=Ini.ReadString('MySQL','DB','scbest');
   driver:=Ini.ReadString('MySQL','Driver','MySQL ODBC 5.3 Unicode Driver');
   DM.Conn.ConnectionString := 'Provider=MSDASQL.1;Password='+pass+';Persist Security Info=True;User ID='+user+';Extended Properties="DRIVER='+driver+';UID='+user+';PWD='+pass+';SERVER='+host+';DATABASE='+db+';PORT='+IntToStr(port)+';COLUMN_SIZE_S32=1;";Initial Catalog='+db;
   Ini.Free;
   DM.Conn.Open();
   DM.qMain.Open;
   DM.tUsers.Open;
   DM.qStock.Open;
   DM.tBrand.Open;
   DM.tClient.Open;
   DM.qDevType.Open;
   if fLogin.ShowModal = mrOk then begin
      if md5(fLogin.Edit1.Text) = DM.tUserspassword.AsString then begin
         user_id := DM.tUsersid.AsInteger;
         user_name := DM.tUsersname.AsString;
         fMain.Caption := fMain.Caption+' - ['+user_name+']';
      end
      else begin
         Application.MessageBox('Неверный пароль', 'Ошибка', MB_ICONERROR);
         fMain.Close;
      end;
   end
   else begin
      fMain.Close;
   end;
end;

procedure TfMain.N10Click(Sender: TObject);
begin
   DM.tConfig.Open;
   DM.tConfig.Edit;
   if fConfig.ShowModal = mrOk then begin
      if DM.tConfig.Modified then
         DM.tConfig.Post;
   end;
   DM.tConfig.Close;
end;

procedure TfMain.N11Click(Sender: TObject);
begin
   fClient.Caption := fMain.Caption + ' - Клиенты';
   DM.tClient.Open;
   fClient.ShowModal;
   DM.tClient.Close;
end;

procedure TfMain.N12Click(Sender: TObject);
begin
  if qInReport.Active then
     qInReport.Close;
  if qOutReport.Active then
     qOutReport.Close;
  qOutReport.Parameters[0].Value := DM.qMainid.AsInteger;
  qOutReport.Parameters[1].Value := DM.qMainid.AsInteger;
  qOutReport.Open;
  qInReport.Parameters[0].Value := DM.qMainid.AsInteger;
  qInReport.Open;
  frxOutReport.ShowReport;
  //qMain.Close;
end;

procedure TfMain.N14Click(Sender: TObject);
begin
  fDevType.Caption := fMain.Caption + ' - Тип устройства';
  DM.tDevType.Open;
  fDevType.ShowModal;
  DM.tDevType.Close;
end;

procedure TfMain.N16Click(Sender: TObject);
begin
  fBrand.Caption := fMain.Caption + ' - Бренды';
  DM.tBrand.Open;
  fBrand.ShowModal;
  DM.tBrand.Close;
end;

procedure TfMain.N17Click(Sender: TObject);
begin
  if qInReport.Active then
     qInReport.Close;
  if qOutReport.Active then
     qOutReport.Close;
  qOutReport.Parameters[0].Value := DM.qMainid.AsInteger;
  qOutReport.Parameters[1].Value := DM.qMainid.AsInteger;
  qOutReport.Open;
  qInReport.Parameters[0].Value := DM.qMainid.AsInteger;
  qInReport.Open;
  frxFixReport.ShowReport;
  //qMain.Close;
end;

procedure TfMain.N19Click(Sender: TObject);
begin
  fValue.Caption := fMain.Caption + ' - ТМЦ';
  DM.tDevType.Open;
  fValue.qValue.Open;
  fValue.ShowModal;
  fValue.qValue.Close;
  DM.tDevType.Close;
end;

procedure TfMain.N1Click(Sender: TObject);
begin
   fReport.ShowModal;
end;

procedure TfMain.N22Click(Sender: TObject);
begin
  fFixPrice.Caption := fMain.Caption + ' - Виды работ и цены';
  DM.tFixPrice.Open;
  fFixPrice.ShowModal;
  DM.tFixPrice.Close;
end;

procedure TfMain.N24Click(Sender: TObject);
begin
  fUsers.Caption := fMain.Caption + ' - Пользователи';
  DM.tUsers.Open;
  fUsers.ShowModal;
  DM.tUsers.Close;
end;

procedure TfMain.N7Click(Sender: TObject);
begin
   fMain.Close;
end;

procedure TfMain.N8Click(Sender: TObject);
begin
    fAbout.ShowModal;
end;

procedure TfMain.N9Click(Sender: TObject);
begin
  if qInReport.Active then
     qInReport.Close;
  qInReport.Parameters[0].Value := DM.qMainid.AsInteger;
  qInReport.Open;
  frxInReport.ShowReport;
  //qMain.Close;
end;

procedure TfMain.pcMainChange(Sender: TObject);
begin
  DM.qMain.Requery();
  DM.qStock.Requery();
end;

procedure TfMain.tbAddClick(Sender: TObject);
begin
  if fMainEdit.qMainEdit.Active then
     fMainEdit.qMainEdit.Close;
  fMainEdit.qMainEdit.Open;
  fMainEdit.qMainEdit.Append;
  fMainEdit.qMainEditdate.AsDateTime := now();
  fMainEdit.qMainEdituser_id.AsInteger := user_id;
  if fMainEdit.qStockMove.Active then
     fMainEdit.qStockMove.Close;
  fMainEdit.qStockMove.Parameters[0].Value := -1;
  fMainEdit.qStockMove.Open;
  if fMainEdit.qWorks.Active then
     fMainEdit.qWorks.Close;
  fMainEdit.qWorks.Parameters[0].Value := 0;
  fMainEdit.qWorks.Open;
  if fMainEdit.qSumma.Active then
     fMainEdit.qSumma.Close;
  fMainEdit.qSumma.Parameters[0].Value := 0;
  fMainEdit.qSumma.Open;
  if fMainEdit.qPhoto.Active then
     fMainEdit.qPhoto.Close;
  fMainEdit.qPhoto.Parameters[0].Value := 0;
  fMainEdit.qPhoto.Open;
  DM.tFixPrice.Open;
  fMainEdit.qDevType.Open;
  if fMainEdit.ShowModal = mrOk then begin
    if fMainEdit.qMainEdit.Modified then
      fMainEdit.qMainEdit.Post;
  end;
  fMainEdit.qMainEdit.Close;
  DM.qMain.Requery();
  DM.qMain.Last;
  DM.tFixPrice.Close;
  fMainEdit.qDevType.Close;
  fMainEdit.qStockMove.Close;
  fMainEdit.qSumma.Close;
  fMainEdit.qWorks.Close;
  fMainEdit.qPhoto.Close;
end;

procedure TfMain.tbDeleteClick(Sender: TObject);
begin
   if Application.MessageBox('Удалить запись?', 'Подтверждение', MB_ICONQUESTION+MB_YESNO) = idYes then begin
      DM.qRemove.SQL.Text := 'SELECT * FROM main WHERE id='+DM.qMainid.AsString;
      DM.qRemove.Open;
      DM.qRemove.Delete;
      DM.qRemove.Close;
      DM.qMain.Requery;
   end;
end;

procedure TfMain.tbEditClick(Sender: TObject);
  var
    id:integer;
begin
  id := DM.qMain.RecNo;
  DM.qMain.Requery();
  DM.qMain.MoveBy(id-1);
  fMainEdit.qStockMove.Parameters[0].Value := DM.qMainid.AsInteger;
  fMainEdit.qStockMove.Open;
  fMainEdit.qWorks.Parameters[0].Value := DM.qMainid.AsInteger;
  fMainEdit.qWorks.Open;
  fMainEdit.qSumma.Parameters[0].Value := DM.qMainid.AsInteger;
  fMainEdit.qSumma.Open;
  fMainEdit.qPhoto.Parameters[0].Value := DM.qMainid.AsInteger;
  fMainEdit.qPhoto.Open;
  fMainEdit.qMainEdit.Parameters[0].Value := DM.qMainid.AsInteger;
  fMainEdit.qMainEdit.Open;
  fMainEdit.qMainEdit.Edit;
  DM.tClient.Open;
  DM.tBrand.Open;
  DM.tFixPrice.Open;
  fMainEdit.qDevType.Open;
  if fMainEdit.ShowModal = mrOk then begin
     if (fMainEdit.DBComboBoxEh1.Text <> 'Выдан') AND (fMainEdit.qMainEditdate_end.AsString <> '') then begin
        fMainEdit.qMainEditdate_end.AsString := '';
     end;
     if fMainEdit.qMainEdit.Modified then
        fMainEdit.qMainEdit.Post;
  end;
  fMainEdit.qMainEdit.Close;
  DM.qMain.Requery();
  DM.qMain.MoveBy(id-1);
  fMainEdit.qStockMove.Close;
  DM.tFixPrice.Close;
  DM.tClient.Close;
  DM.tBrand.Close;
  fMainEdit.qDevType.Close;
  fMainEdit.qWorks.Close;
  fMainEdit.qSumma.Close;
  fMainEdit.qPhoto.Close;
end;

procedure TfMain.ToolButton22Click(Sender: TObject);
begin
  DM.qStock.Append;
  DM.qStockmoveIn.AsInteger := 1;
  DM.qStockuser_id.AsInteger := user_id;
  fStockEdit.qStockMove.Parameters[0].Value := 0;
  fStockEdit.qStockMove.Open;
  if fStockEdit.ShowModal = mrOk then begin
    if DM.qStock.Modified then
      DM.qStock.Post;
   end;
   DM.qStock.Cancel;
   DM.qStock.Requery();
   DM.qStock.Last;
end;

procedure TfMain.ToolButton23Click(Sender: TObject);
begin
  DM.qStock.Append;
  DM.qStockmoveIn.AsInteger := 0;
  DM.qStockuser_id.AsInteger := user_id;
  if fStockEdit.ShowModal = mrOk then begin
    if DM.qStock.Modified then
      DM.qStock.Post;
   end;
   DM.qStock.Cancel;
   DM.qStock.Requery();
   DM.qStock.Last;
end;

procedure TfMain.ToolButton24Click(Sender: TObject);
  var
    id:integer;
begin
  id := DM.qStock.RecNo;
  fStockEdit.qStockMove.Parameters[0].Value := DM.qStockid.Value;
  fStockEdit.qStockMove.Open;
  DM.qStock.Edit;
  if fStockEdit.ShowModal = mrOk then begin
    if DM.qStock.Modified then
      DM.qStock.Post;
  end;
  fStockEdit.qStockMove.Close;
  DM.qStock.Cancel;
  DM.qStock.Requery();
  DM.qStock.MoveBy(id-1);
end;

end.
