unit U_GYGL;

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;

type
  TfrmGYGL = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    GYName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    GYNO: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    TNochk: TToolButton;
    v2GYGX: TcxGridDBColumn;
    v2Ssel: TcxGridDBColumn;
    v2GYName: TcxGridDBColumn;
    v2GYNO: TcxGridDBColumn;
    ToolButton4: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    XUHAO: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DataSource1: TDataSource;
    Order_FZ: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure GYNOChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure VBBPropertiesEditValueChanged(Sender: TObject);
    procedure v2DerateMoneyPropertiesEditValueChanged(Sender: TObject);
    procedure v2fstr1PropertiesEditValueChanged(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }

    procedure InitGridHZ();
    procedure InitGrid();
    function  SaveData():Boolean;
    procedure SetStatus();
    function  SaveZL():Boolean;

  public
    fflag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmGYGL: TfrmGYGL;

implementation
uses
  //U_ProductOrderNewList,     U_PbProductList,
  U_DataLink,U_Fun,U_ZDYHelp,
  U_ZdyAttachGYS, U_ZdyAttachment,U_GXGL_List_DXCX;

{$R *.dfm}
function  TfrmGYGL.SaveZL():Boolean;
var
  LLNO:string;
begin
  if GetLSNo(ADOQueryCMD,LLNO,'YL','YF_Money_LL',3,1)=False then
  begin
    ADOQueryCMD.Connection.RollbackTrans;
    Application.MessageBox('取流水号失败!','提示');
    Exit;
  end;
  result:=false;
  try
    with ADOqueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('delete from YF_Money_LL where fromMainID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+''' ');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from YF_Money_LL where fromMainID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+''' ');
      Open;
      append;
      fieldbyname('filler').Value:=trim(Dname);
      FieldByName('LLNO').Value:=Trim(LLNO);
      fieldbyname('CRTime').Value:=formatdateTime('yyyy-MM-dd',CDS_HZ.fieldbyname('CRTime').AsDateTime);
      fieldbyname('CRType').Value:=Trim(CDS_HZ.fieldbyname('YFName').AsString);
      fieldbyname('factoryName').Value:=Trim(CDS_HZ.fieldbyname('bankNo').AsString);
      fieldbyname('TOfactoryName').Value:=Trim(CDS_HZ.fieldbyname('factoryName').AsString);
      fieldbyname('NOTE').Value:=Trim(CDS_HZ.fieldbyname('note').AsString);
      fieldbyname('CRZY').Value:=Trim(CDS_HZ.fieldbyname('orderNo').AsString);
      fieldbyname('inMoney').Value:=0;
      fieldbyname('OutMoney').Value:=CDS_HZ.fieldbyname('Money').AsFloat;
      fieldbyname('status').Value:='0';
      FieldByName('fromMainID').Value:=Trim(CDS_HZ.fieldbyname('YFID').AsString);
      Post;
    end;
    result:=true;
  except
  end;
end;

procedure TfrmGYGL.SetStatus();
var
  i:integer;
begin

  Tchk.Visible:=false;
  TNochk.Visible:=false;
  TBDel.Visible:=false;
  ToolButton4.Visible:=false;
  ToolButton3.Visible:=false;
  for i:=0 to tv2.ColumnCount-1 do
  begin
    IF uppercase(tv2.Columns[i].DataBinding.FieldName)<>'SSEL' then
    tv2.Columns[i].Options.Editing:=false;
  end;
  if cxTabControl1.TabIndex=0 then
  begin
    Tchk.Visible:=true;
    TBDel.Visible:=true;
    ToolButton3.Visible:=true;
    ToolButton4.Visible:=true;
    for i:=0 to tv2.ColumnCount-1 do
    begin
      tv2.Columns[i].Options.Editing:=true;
    end;
  end;
  if cxTabControl1.TabIndex=1 then
  begin
    TNochk.Visible:=true;
  end;
  if cxTabControl1.TabIndex=2 then
  begin

  end;
end;

procedure TfrmGYGL.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * FROM GY_Main');
      IF cxTabControl1.TabIndex<2 then
      sql.Add(' where status='''+inttostr(cxTabControl1.TabIndex)+''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
    //Order_FZ.EmptyDataSet;
  end;
end;
procedure TfrmGYGL.InitGridHZ();
begin
end;

procedure TfrmGYGL.FormDestroy(Sender: TObject);
begin
  frmGYGL:=nil;
end;

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

procedure TfrmGYGL.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('工艺登记',Tv2,'工艺管理');
  Close;
end;
procedure TfrmGYGL.TBDelClick(Sender: TObject);
var
  FBBMoney,CRID:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('delete GY_Main where GYID='''+Trim(CDS_HZ.fieldbyname('GYID').AsString)+'''');
            sql.Add('delete GY_GX where  GYID='''+Trim(CDS_HZ.fieldbyname('GYID').AsString)+'''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmGYGL.FormShow(Sender: TObject);
begin
  fflag:=0;
  ReadCxGrid('工艺登记',Tv2,'工艺管理');
  SetStatus();
  InitGrid();
end;

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

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

procedure TfrmGYGL.GYNOChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmGYGL.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;
end;

function TfrmGYGL.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'FK','YF_Money_CR',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 YF_Money_KC where FactoryName='''+Trim(CDS_HZ.fieldbyname('FactoryName').AsString)+''' ');
      Open;
    end;
    if ADOQueryCmd.IsEmpty=False then
    begin
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YF_Money_CRID ');
        Open;
      end;
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CRID').Value:=StrToInt(CRID);
        FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
        FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
        FieldByName('ZdyStr1').Value:='应付付';
        Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
      //SSetSaveDataCDSNew(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',3);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='付款登记';
      FieldByName('CRFlag').Value:='应付付';
      FieldByName('QtyFlag').Value:=-1;
      FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('IsTax').Value:=Trim(CDS_HZ.fieldbyname('IsTax').AsString);
      FieldByName('CRTime').Value:=formatdateTIme('yyyy-MM-dd',CDS_HZ.fieldbyname('CRTime').AsDateTime);
      FieldByName('YFDefFlag1').Value:=fFlag;
      FieldByName('status').Value:='0';
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('YFId').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmGYGL.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 TfrmGYGL.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FHuiLv,FBBMoney,FKCBB,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 ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(BBMoney,0) BBMoney from YF_Money_CR where YFID='''+cds_hz.fieldbyname('YFID').AsString+'''');
    Open;
  end;
  FKCBB:=ADOQueryTemp.fieldbyname('BBMoney').AsString;

  FQty:=Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';

  FPrice:=Trim(CDS_HZ.fieldbyname('Price').AsString);
  if Trim(FPrice)='' then FPrice:='0';
  FQty:=Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';

  FMoney:=Trim(CDS_HZ.fieldbyname('Money').AsString);
  if Trim(FMoney)='' then FMoney:='0';

  FHuiLv:=Trim(CDS_HZ.fieldbyname('HuiLv').AsString);
  if Trim(FHuiLv)='' then FHuiLv:='0';

  {FBBMoney:=Trim(CDS_HZ.fieldbyname('BBMoney').AsString);
  if Trim(FBBMoney)='' then FBBMoney:='0'; }

  if Trim(FFieldName)<>'Money' then
  begin
    if Trim(FFieldName)<>'HuiLv' then
    begin
      FBBMoney:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice)*StrToFloat(FHuiLv));
      FMoney:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice));
    end else
    begin
      FBBMoney:=FloatToStr(StrToFloat(FMoney)*StrToFloat(FHuiLv));
    end;

  end else
  begin
    FBBMoney:=FloatToStr(StrToFloat(FMoney)*StrToFloat(FHuiLv));
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FMoney);
    FieldByName('BBMoney').Value:=StrToFloat(FBBMoney);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    if Trim(FFieldName)<>'Money' then
    begin
      sql.Add(',Money='+FMoney);
    end;

    sql.Add(',BBMoney='+FBBMoney);
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    
    SQL.Add('Update YF_Money_KC Set KCBBMoney=KCBBMoney-'+FKCBB+'+'+FBBMoney);
    sql.Add(' where CRID='''+Trim(CDS_HZ.fieldbyname('CRID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmGYGL.cxGridDBColumn1PropertiesEditValueChanged(
  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 YF_Money_CR ');
    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 YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmGYGL.ToolButton3Click(Sender: TObject);
var
  maxId:string;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
    if GetLSNo(ADOQueryCmd,maxId,'GY','GY_Main',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
       Close;
       SQL.Clear;
       SQL.Add('select * from GY_Main where 1<>1');
       OPEN;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('GYID').Value:=Trim(maxId);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('Filler').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('Status').Value:='0';
      Post;
    end;
    with CDS_HZ do
    begin
      Append;
      FieldByName('GYID').Value:=Trim(maxId);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
   tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmGYGL.cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFName';
      flagname:='项目及摘要';
      if Trim(DParameters3)='项目分开' then
        MainType:='付款';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('YFCode').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('YFName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set YFCode='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
          sql.Add(' ,YFName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmGYGL.cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set QtyUnit='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmGYGL.v2Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin

        with Self.CDS_HZ do
        begin
          edit;
        //  FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
          FieldByName('fkFactoryName').Value:=Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set fkFactoryName='''+Trim(CDS_HZ.fieldbyname('ZdyName').AsString)+'''');
          SQL.Add(' where YFId='''+Trim(self.CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmGYGL.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'付款列表');
end;

procedure TfrmGYGL.VBBPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FKCBB:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(BBMoney,0) BBMoney from YF_Money_CR where YFID='''+cds_hz.fieldbyname('YFID').AsString+'''');
    Open;
  end;
  FKCBB:=ADOQueryTemp.fieldbyname('BBMoney').AsString;
  if Trim(mvalue)='' then mvalue:='0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('BBMoney').Value:=StrToFloat(mvalue);
    FieldByName('Money').Value:=StrToFloat(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' set BBMoney='+mvalue);
    sql.Add(' ,Money='+mvalue);
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    
    SQL.Add('Update YF_Money_KC Set KCBBMoney=KCBBMoney+'+FKCBB+'-'+mvalue);
    sql.Add(' where CRID='''+Trim(CDS_HZ.fieldbyname('CRID').AsString)+'''');
    ExecSQL;
  end;
    try
    with ADOqueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('delete from YF_Money_LL where fromMainID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+''' ');
      execsql;
    end;
    IF (CDS_HZ.FieldByName('money').AsFloat<>0) and
       (trim(CDS_HZ.fieldbyname('bankNo').AsString)<>'') then
    begin
      IF not saveZL() then
      begin
        application.MessageBox('生成账户流水信息失败','提示信息',0);
      end;
    end;
  except
  end;
end;

procedure TfrmGYGL.v2DerateMoneyPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  if Trim(mvalue)='' then mvalue:='0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('DerateMoney').Value:=StrToFloatdef(mvalue,0);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' set DerateMoney='+mvalue);
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;

end;


procedure TfrmGYGL.v2fstr1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' set fstr1='+quotedstr(trim(mvalue)));
    SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
    ExecSQL;
  end;
end;
procedure TfrmGYGL.TchkClick(Sender: TObject);
begin
 IF CDS_HZ.IsEmpty then exit;

  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update GY_Main SET status=''1'' ');
            sql.Add('where GYID ='+quotedstr(trim(CDS_HZ.fieldbyname('GYID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmGYGL.TNochkClick(Sender: TObject);
begin
 IF CDS_HZ.IsEmpty then exit;

  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update GY_Main SET status=''0'' ');
            sql.Add('where GYID ='+quotedstr(trim(CDS_HZ.fieldbyname('GYID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

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

procedure TfrmGYGL.v2Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='XJLLFLAG';
      flagname:='账户类型定义';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('bankNo').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set bankNo='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
          SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  try
    with ADOqueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('delete from YF_Money_LL where fromMainID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+''' ');
      execsql;
    end;
    IF (CDS_HZ.FieldByName('money').AsFloat<>0) and
       (trim(CDS_HZ.fieldbyname('bankNo').AsString)<>'') then
    begin
      IF not saveZL() then
      begin
        application.MessageBox('生成账户流水信息失败','提示信息',0);
      end;
    end;
  except
  end;
end;

procedure TfrmGYGL.v2Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:Boolean;
  svalue:string;
begin

   mvalue:= TcxCheckBox(Sender).EditingValue;
   with CDS_HZ do
     begin
       Edit;
       FieldByName('IsTax').Value:=mvalue;
       Post;
     end;
   if  mvalue then svalue:='1' else  svalue:='0';
   with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate YF_Money_CR ');
      sql.Add(' set IsTax='+ svalue);
      SQL.Add(' where YFId='''+Trim(CDS_HZ.fieldbyname('YFId').AsString)+'''');
      ExecSQL;
    end;

end;

procedure TfrmGYGL.v2Column6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
   FactoryNo,FactoryName:string;
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
          FactoryNo:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
          FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
          FactoryName:=Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
          Post;
        end;

        with Self.ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' set FactoryNo='+ QuotedStr(FactoryNo)+',FactoryName='+ QuotedStr(FactoryName));
          SQL.Add(' where YFId='''+Trim(Self.CDS_HZ.fieldbyname('YFId').AsString)+'''');
          ExecSQL;
          //showmessage(sql.Text);
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  Tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmGYGL.v2Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then mvalue:='';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from GY_Main where GYNO ='+quotedstr(Trim(mvalue)));
    Open;
  end;
  if ADOQueryCmd.IsEmpty=false then
  begin
    application.MessageBox('已存在工艺编号','提示');
    CDS_HZ.Edit;
    CDS_HZ.FieldByName('GYNo').AsString:='';
    exit;
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('GYNO').Value:=Trim(mvalue);
    //Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update GY_Main set GYNO='''+trim(mvalue)+''' ');
    sql.Add(',Editer='''+trim(DName)+'''');
    sql.Add(',Edittime=getdate()');
    sql.add(' where GYID='''+trim(CDS_HZ.fieldbyname('GYID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmGYGL.v2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then mvalue:='';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from GY_Main where GYName ='+quotedstr(Trim(mvalue)));
    Open;
  end;
  if ADOQueryCmd.IsEmpty=false then
  begin
    application.MessageBox('已存在工艺名称','提示');
    CDS_HZ.Edit;
    CDS_HZ.FieldByName('GYName').AsString:='';
    exit;
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('GYName').Value:=Trim(mvalue);
    //Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update GY_Main set GYName='''+trim(mvalue)+''' ');
    sql.Add(',Editer='''+trim(DName)+'''');
    sql.Add(',Edittime=getdate()');
    sql.add(' where GYID='''+trim(CDS_HZ.fieldbyname('GYID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmGYGL.ToolButton4Click(Sender: TObject);
var i:Integer;
    GYGX:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  try
    frmGXGL_List_DXCX:=TfrmGXGL_List_DXCX.Create(Application);
    with frmGXGL_List_DXCX do
    begin
      if ShowModal=1 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete GY_GX where GYID='+quotedstr(trim(Self.CDS_HZ.fieldbyname('GYID').AsString)));
          execsql;
        end;
        IF not Order_FZ.IsEmpty then
        begin
          with Order_FZ do
          begin
            First;
            I:=1;
            GYGX:='';
            while not Eof do
            begin
              with ADOQueryCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('SELECT * FROM GY_GX WHERE 1=2');
                Open;
              end;
              with ADOQueryCmd do
              begin
                Append;
                FieldByName('XUHAO').Value:=I;
                FieldByName('GYID').Value:=Trim(Self.CDS_HZ.fieldbyname('GYID').AsString);
                FieldByName('GXID').Value:=Trim(Order_FZ.fieldbyname('PSId').AsString);
                Post;
              end;
              if GYGX='' then
              begin
                GYGX :=IntToStr(i)+'.'+Trim(Order_FZ.fieldbyname('PSName').AsString);
              end
              else
              begin
                GYGX :=GYGX+'→'+IntToStr(I)+'.'+Trim(Order_FZ.fieldbyname('PSName').AsString);
              end;
              i:=i+1;
              Next;
            end;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from GY_Main where GYID ='+quotedstr(Trim(Self.CDS_HZ.fieldbyname('GYID').AsString)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            Edit;
            FieldByName('GYGX').Value:=Trim(GYGX);
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('Edittime').Value:=SGetServerDateTime(ADOQueryTemp);
            Post;
          end;
        end;
        Self.TBRafresh.Click;
      end;
    end;
  finally;
    frmGXGL_List_DXCX.Free;
  end;
end;

procedure TfrmGYGL.Tv2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
   try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.XUHAO,B.PSName,B.PSSH,B.PSNote,B.SYRName,B.LDPrice,B.Note FROM GY_GX A left join JYOrder_Process B on A.GXID=B.PSId ');
      sql.Add(' Where A.GYID='''+trim(CDS_HZ.fieldbyname('GYID').AsString)+'''');
      SQL.Add(' ORDER BY A.XUHAO');
       //SHOWMESSAGE(SQL.TEXT);
      Open;
    end;
    SCreateCDS20(ADOQuery1,Order_FZ);
    SInitCDSData20(ADOQuery1,Order_FZ);
  finally
    ADOQuery1.EnableControls;
  end;
end;

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

end.
