unit U_SGManage;

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;

type
  TfrmSGManage = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    TBEdit: TToolButton;
    Label1: TLabel;
    YCLSpec: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    v1Column12: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    v1Column15: TcxGridDBColumn;
    Label3: TLabel;
    YCLName: TEdit;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Button1: TButton;
    Button2: TButton;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label4: TLabel;
    YCLType: TEdit;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure YCLNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure cxGridDBColumn2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    procedure InitGridHZ();
    function  SaveData():Boolean;
  public
    { Public declarations }
  end;

var
  frmSGManage: TfrmSGManage;

implementation
uses
  U_DataLink,U_Fun,U_SGInput,U_FileUp, U_ZDYHelp;

{$R *.dfm}



procedure TfrmSGManage.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add('select AA.*,Case when XDQty>0 then cast(1 as bit) else cast(0 as bit) end as YXD  ');
      sql.Add(',Case when JHQty>0 then cast(1 as bit) else cast(0 as bit) end as YJH  ');
      SQL.Add('from(select A.*,XDQty=(select isnull(sum(isnull(OrdQty,0)),0) from YCL_SG_HZ where MainId=A.MainId )');
      SQL.Add(',JHQty=(select isnull(sum(isnull(SJQty,0)),0) from YCL_SG_HZ where MainId=A.MainId )');
      sql.Add('from YCL_SG A where A.SGDate>=:begdate and A.SGDate<:Enddate) AA');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
      Parameters.ParamByName('Enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmSGManage.InitGridHZ();
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      SQL.Add('select * from YCL_SG_HZ where MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_HZ);
    SInitCDSData20(ADOQueryTemp,CDS_HZ);
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmSGManage.FormDestroy(Sender: TObject);
begin
  frmSGManage:=nil;
end;

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

procedure TfrmSGManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('申购列表',Tv1,'申购管理');
  WriteCxGrid('申购回执',Tv2,'申购管理');
  Close;
end;
procedure TfrmSGManage.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_HZ.IsEmpty=False then
  begin
    Application.MessageBox('已有回执数据,不能删除数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_Main.fieldbyname('MainID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YCL_SG where MainID='''+Trim(CDS_Main.fieldbyname('MainID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_Main.Delete;
end;

procedure TfrmSGManage.FormShow(Sender: TObject);
begin
  ReadCxGrid('申购列表',Tv1,'申购管理');
  ReadCxGrid('申购回执',Tv2,'申购管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-7;
  InitGrid();
  if Trim(DParameters1)='申购' then
  begin
    Button1.Visible:=False;
    Button2.Visible:=False;
    Tv2.OptionsSelection.CellSelect:=False;
  end;
  if Trim(DParameters1)='回执' then
  begin
    TBAdd.Visible:=False;
    TBDel.Visible:=False;
    TBEdit.Visible:=False;
  end;
end;

procedure TfrmSGManage.TBAddClick(Sender: TObject);
begin
  try
    frmSGInput:=TfrmSGInput.Create(Application);
    with frmSGInput do
    begin
      FMainId:='';
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmSGInput.Free;
  end;
end;

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

procedure TfrmSGManage.TBEditClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty=False then
  begin
    Application.MessageBox('已有回执数据,不能修改数据!','提示',0);
    Exit;
  end;
  try
    frmSGInput:=TfrmSGInput.Create(Application);
    with frmSGInput do
    begin
      FMainId:=Trim(Self.CDS_Main.fieldbyname('MainID').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.CDS_Main.Locate('MainID',FMainId,[]);
      end;
    end;
  finally
    frmSGInput.Free;
  end;
end;

procedure TfrmSGManage.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmSGManage.YCLNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmSGManage.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('样品档案',cxGrid1);
end;

procedure TfrmSGManage.ToolButton4Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\样品标签.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\样品标签.rmf'),'提示',0);
  end;
end;


procedure TfrmSGManage.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitGridHZ();
end;

procedure TfrmSGManage.Button1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  CDS_HZ.Append;
  CDS_HZ.FieldByName('JBDate').Value:=SGetServerDate(ADOQueryTemp);
  CDS_HZ.FieldByName('CGPerson').Value:=Trim(DName);
  CDS_HZ.FieldByName('YCLName').Value:=Trim(CDS_Main.fieldbyname('YCLName').AsString);
  CDS_HZ.FieldByName('YCLCode').Value:=Trim(CDS_Main.fieldbyname('YCLCode').AsString);
  CDS_HZ.FieldByName('SGUnit').Value:=Trim(CDS_Main.fieldbyname('SGUnit').AsString);
  CDS_HZ.FieldByName('YCLSpec').Value:=Trim(CDS_Main.fieldbyname('YCLSpec').AsString);
  CDS_HZ.FieldByName('YCLTypeCode').Value:=Trim(CDS_Main.fieldbyname('YCLTypeCode').AsString);
  CDS_HZ.Post;

  SaveData();
end;

procedure TfrmSGManage.Button2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_HZ.fieldbyname('SubID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YCL_SG_HZ where SubID='''+Trim(CDS_HZ.fieldbyname('SubID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
  if CDS_HZ.IsEmpty then
  begin
    with CDS_Main do
    begin
      Edit;
      FieldByName('YXD').Value:=false;
      FieldByName('YJH').Value:=false;
      FieldByName('XDQty').Value:=0;
      FieldByName('JHQty').Value:=0;
      Post;
    end;
  end else
  begin
    with CDS_Main do
    begin
      Edit;
      FieldByName('XDQty').Value:=Tv2.DataController.Summary.FooterSummaryValues[0];
      FieldByName('JHQty').Value:=Tv2.DataController.Summary.FooterSummaryValues[1];
      if Tv2.DataController.Summary.FooterSummaryValues[1]>0 then
      begin
        FieldByName('YJH').Value:=True;
      end else
      begin
        FieldByName('YJH').Value:=False;
      end;
      Post;
    end;
  end;
end;

procedure TfrmSGManage.cxGridDBColumn2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GYS';
      flagname:='供应商名称';
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('GYSCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Self.CDS_HZ.FieldByName('GYS').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YCL_SG_HZ ');
          sql.Add(' Set GYS='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
          sql.Add(' ,GYSCode='''+Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where SubId='''+Trim(CDS_HZ.fieldbyname('SubId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;
function TfrmSGManage.SaveData():Boolean;
var
  maxId:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'HZ','YCL_SG_HZ',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YCL_SG_HZ where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MainId').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString);
      FieldByName('SubId').Value:=Trim(maxId);
      SSetSaveDataCDSNew(ADOQueryCmd,Tv2,CDS_HZ,'YCL_SG_HZ',3);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('YCLTypeCode').Value:=Trim(CDS_HZ.fieldbyname('YCLTypeCode').AsString);
      FieldByName('YCLCode').Value:=Trim(CDS_HZ.fieldbyname('YCLCode').AsString);
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('SubId').Value:=Trim(maxId);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmSGManage.cxGridDBColumn8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YCL_SG_HZ ');
    if Trim(mvalue)<>'' then
    begin
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    end else
    begin
      sql.Add(' Set '+FFieldName+'=NULL');
    end;
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where SubId='''+Trim(CDS_HZ.fieldbyname('SubId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmSGManage.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YCL_SG_HZ ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where SubId='''+Trim(CDS_HZ.fieldbyname('SubId').AsString)+'''');
    ExecSQL;
  end;
  with CDS_Main do
  begin
    Edit;
    FieldByName('XDQty').Value:=Tv2.DataController.Summary.FooterSummaryValues[0];
    FieldByName('JHQty').Value:=Tv2.DataController.Summary.FooterSummaryValues[1];
    if Tv2.DataController.Summary.FooterSummaryValues[1]>0 then
    begin
      FieldByName('YJH').Value:=True;
    end else
    begin
      FieldByName('YJH').Value:=False;
    end;
    if Tv2.DataController.Summary.FooterSummaryValues[0]>0 then
    begin
      FieldByName('YXD').Value:=True;
    end else
    begin
      FieldByName('YXD').Value:=False;
    end;
    Post;
  end;
end;

end.
