unit U_MGFactory;

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, cxDBLookupComboBox, ComObj, cxGridBandedTableView,
  cxGridDBBandedTableView, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu,U_BaseList;

type
  FdDy = record
    inc: integer; //客户端套接字句柄
    FDdys: string[32]; //客户端套接字
    FdDysName: string[32]; //客户端套接字
  end;

  TfrmMGFactory = class(TFrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton1: TToolButton;
    OpenDialog1: TOpenDialog;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBBandedTableView;
    cxGridDBBandedColumn51: TcxGridDBBandedColumn;
    cxGridDBBandedColumn52: TcxGridDBBandedColumn;
    cxGridDBBandedColumn56: TcxGridDBBandedColumn;
    cxGridDBBandedColumn57: TcxGridDBBandedColumn;
    cxGridDBBandedColumn59: TcxGridDBBandedColumn;
    cxGridDBBandedColumn61: TcxGridDBBandedColumn;
    cxGridDBBandedColumn62: TcxGridDBBandedColumn;
    cxGridDBBandedColumn63: TcxGridDBBandedColumn;
    cxGridDBBandedColumn64: TcxGridDBBandedColumn;
    cxGridDBBandedColumn77: TcxGridDBBandedColumn;
    cxGridDBBandedColumn81: TcxGridDBBandedColumn;
    cxGridDBBandedColumn87: TcxGridDBBandedColumn;
    cxGridDBBandedColumn91: TcxGridDBBandedColumn;
    V1Column6: TcxGridDBBandedColumn;
    V1Column4: TcxGridDBBandedColumn;
    V1Column5: TcxGridDBBandedColumn;
    V1Column3: TcxGridDBBandedColumn;
    cxGridLevel2: TcxGridLevel;
    V1Column1: TcxGridDBBandedColumn;
    Panel1: TPanel;
    Label3: TLabel;
    CoName: TEdit;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CoNameChange(Sender: TObject);
    procedure TBuserClick(Sender: TObject);
    procedure cxGridDBBandedColumn64PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    dFdDy: array[0..30] of FdDy;
    { Private declarations }
    procedure InitGrid();
    procedure InsertMG(MCoid: string);
  public
  end;

var
  frmMGFactory: TfrmMGFactory;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ZdyAttachGYS;

{$R *.dfm}
procedure TfrmMGFactory.InsertMG(MCoid: string);
var
  MaxId: string;
begin

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from Company_MGFY A        ');
    sql.Add(' where FromCoID=' + quotedstr(Trim(MCoid)));
    Open;
  end;
  if ADOQueryTemp.isempty then
  begin
    if GetLSNo(ADOQueryCmd, MaxId, 'CO', 'Company', 4, 1) = False then
    begin
      Application.MessageBox('取最大号失败！', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add(' insert into Company_MGFY(FromCoID,FYID) ');
      sql.Add(' values(' + quotedstr(Trim(MCoid)) + ',' + quotedstr(Trim(MaxId)) + ') ');
      ExecSQL;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('FYID').Value := Trim(MaxId);
      Post;
    end;
  end;
end;

procedure TfrmMGFactory.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.*       ');
      sql.Add('  from Company A inner join  Company_MGFY B on A.COID=B.FromCoID      ');
      sql.Add('   where CoType=''供应商'' and Valid=''Y'' ');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMGFactory.FormDestroy(Sender: TObject);
begin
inherited;
  frmMGFactory := nil;
end;

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

procedure TfrmMGFactory.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid(trim(self.caption), Tv1, '供应商管理');
  Close;
end;

procedure TfrmMGFactory.FormShow(Sender: TObject);
begin
inherited;
  ReadCxBandedGrid(trim(self.Caption), Tv1, '供应商管理管理');
  InitGrid();
end;

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

procedure TfrmMGFactory.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmMGFactory.CoNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmMGFactory.TBuserClick(Sender: TObject);
var
  FuserName: string;
begin

end;

procedure TfrmMGFactory.cxGridDBBandedColumn64PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalue = '' then
    mvalue := '0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update Company_MGFY set  ' + FFieldName + '=' + (Trim(mvalue)));
    sql.Add('where FYID=' + quotedstr(Trim(CDS_HZ.fieldbyname('FYID').AsString)));
    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmMGFactory.ToolButton1Click(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        InsertMG(Trim(CDS_HZ.fieldbyname('COID').AsString));
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  InitGrid();
end;

procedure TfrmMGFactory.ToolButton3Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('COID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete  Company_MGFY    where FYID=''' + Trim(CDS_HZ.fieldbyname('FYID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

end.

