unit U_GKManage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxCheckComboBox, cxDropDownEdit, Menus, RM_e_Xls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, RM_BarCode, dxScrollbarAnnotations, cxContainer,
  cxProgressBar, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmGKManage = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    TbFilter: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    GPM_1: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    GKBH: TcxTextEdit;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column10: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    Tv2Column7: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column8: TcxGridDBColumn;
    Tv2Column9: TcxGridDBColumn;
    Tv2Column11: TcxGridDBColumn;
    Tv2Column12: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure MCNOPropertiesChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v2Column12PropertiesEditValueChanged(Sender: TObject);
    procedure cxgrdbclmnTv2Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmGKManage: TfrmGKManage;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_MachineInPut, U_EmployeeSel,
  U_GKInput;

{$R *.dfm}

procedure TfrmGKManage.InitGrid();
begin
  GKBH.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      SQL.Add(', d1=(SELECT COUNT(1) FROM V_JT_ZC e where ce<=7 and e.gkbh=a.gkbh) ');
      SQL.Add(', d2=(SELECT COUNT(1) FROM V_JT_ZC e where ce>7 and ce<=15 and e.gkbh=a.gkbh) ');
      SQL.Add(', d3=(SELECT COUNT(1) FROM V_JT_ZC e where ce>15 and ce<=30 and  e.gkbh=a.gkbh) ');
      sql.Add(', kcqty = ISNULL((SELECT SUM(e.IOQtyFlag * e.qty) FROM Bs_Product_IO e WHERE e.stkname = ''物料'' AND e.p_code = a.GKBH), 0) ');
      SQL.Add(' from  BS_GK A   ');
//            showmessage(Sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    TbFilter.Click;
  end;
end;

procedure TfrmGKManage.MCNOPropertiesChange(Sender: TObject);
var
  sql: string;
begin
  if ADOQueryMain.Active then
  begin
    sql := SGetFilters(Panel1, 1, 2);
    SDofilter(ADOQueryMain, sql);
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmGKManage.FormDestroy(Sender: TObject);
begin
  inherited;
  frmGKManage := nil;
end;

procedure TfrmGKManage.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmGKManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv2, '汇率管理');
  Close;
end;

procedure TfrmGKManage.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(Trim(Self.Caption), Tv2, '汇率管理');
  InitGrid();
end;

procedure TfrmGKManage.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmGKManage.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;

  TcxGridToExcel(self.Caption, cxgrid2);
end;

procedure TfrmGKManage.ToolButton4Click(Sender: TObject);
begin
  try
    frmGKInput := TfrmGKInput.Create(Application);
    with frmGKInput do
    begin
      FGKID := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmGKInput.Free;
  end;
end;

procedure TfrmGKManage.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete BS_GK where GKID=' + QuotedStr(CDS_HZ.FieldByName('GKID').AsString));
    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('钢筘删除')));
    sql.Add(',' + quotedstr(trim('GKID：' + trim(CDS_HZ.FieldByName('GKID').AsString) + ';GKno:' + trim(CDS_HZ.FieldByName('GKBH').AsString))));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    ExecSQL;
  end;

  InitGrid();
end;

procedure TfrmGKManage.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  try
    frmGKInput := TfrmGKInput.Create(Application);
    with frmGKInput do
    begin
      FGKID := Trim(Self.CDS_HZ.fieldbyname('GKID').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmGKInput.Free;
  end;
end;

procedure TfrmGKManage.v2Column12PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate BS_HL ');
      sql.Add(' Set Begdate = ''' + mvalue + '''');
      sql.Add(' where HLID=' + quotedstr(CDS_HZ.fieldbyname('HLID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmGKManage.v2Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate BS_HL ');
      sql.Add(' Set HL = ' + mvalue + '');
      sql.Add(' where HLID=' + quotedstr(CDS_HZ.fieldbyname('HLID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmGKManage.cxgrdbclmnTv2Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate BS_HL ');
      sql.Add(' Set EndDate = ''' + mvalue + '''');
      sql.Add(' where HLID=' + quotedstr(CDS_HZ.fieldbyname('HLID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmGKManage.cxTabControl1Change(Sender: TObject);
begin
  InitGrid;
end;

procedure TfrmGKManage.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmGKManage.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  SelOKNo(CDS_HZ, False);
end;

procedure TfrmGKManage.CustomerChange(Sender: TObject);
begin
  TbFilter.Click;
end;

procedure TfrmGKManage.FormCreate(Sender: TObject);
begin
  inherited;
  fmanage := Trim(DParameters1);
end;

end.

