unit U_CWMDOtherFeeList;  //17

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, ToolWin, cxPC, cxControls,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB,
  DBClient, cxCheckBox, cxSplitter, cxGridBandedTableView,
  cxGridDBBandedTableView, cxTextEdit, Menus, cxButtonEdit;

type
  TfrmCWMDOtherFeeList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label10: TLabel;
    MPRTCodeName: TEdit;
    PRTColor: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    AOrdDefStr1: TEdit;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v3Column3: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Label6: TLabel;
    MDNO: TEdit;
    Label7: TLabel;
    MPRTCode: TEdit;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQuery3: TADOQuery;
    v3Column12: TcxGridDBColumn;
    MDType: TComboBox;
    Label8: TLabel;
    PSName: TComboBox;
    Label9: TLabel;
    v3Column13: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    ComboBox1: TComboBox;
    v3Column14: TcxGridDBColumn;
    v3Column15: TcxGridDBColumn;
    v3Column16: TcxGridDBColumn;
    v3Column17: TcxGridDBColumn;
    Label11: TLabel;
    MDFactoryName: TEdit;
    ScrollBox1: TScrollBox;
    cxSplitter1: TcxSplitter;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBBandedTableView;
    v2Fty: TcxGridDBBandedColumn;
    cxGridDBBandedColumn7: TcxGridDBBandedColumn;
    cxGridDBBandedColumn8: TcxGridDBBandedColumn;
    v2Pon: TcxGridDBBandedColumn;
    v2Column2: TcxGridDBBandedColumn;
    v2Column3: TcxGridDBBandedColumn;
    cxGridLevel3: TcxGridLevel;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBBandedTableView;
    v3Fty: TcxGridDBBandedColumn;
    cxGridDBBandedColumn13: TcxGridDBBandedColumn;
    cxGridDBBandedColumn14: TcxGridDBBandedColumn;
    v3Pon: TcxGridDBBandedColumn;
    cxGridDBBandedColumn16: TcxGridDBBandedColumn;
    cxGridDBBandedColumn17: TcxGridDBBandedColumn;
    cxGridLevel4: TcxGridLevel;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource4: TDataSource;
    ClientDataSet4: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    ToolButton2: TToolButton;
    v3Column18: TcxGridDBColumn;
    v3Column19: TcxGridDBColumn;
    v3Column20: TcxGridDBColumn;
    v3Column21: TcxGridDBColumn;
    Panel2: TPanel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    Panel3: TPanel;
    v3Column22: TcxGridDBColumn;
    v3Column23: TcxGridDBColumn;
    cxTabSheet3: TcxTabSheet;
    cxGrid7: TcxGrid;
    Tv7: TcxGridDBTableView;
    cxGridDBColumn34: TcxGridDBColumn;
    v7Column1: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    v7Column2: TcxGridDBColumn;
    v7Column3: TcxGridDBColumn;
    v7Column4: TcxGridDBColumn;
    v7Column5: TcxGridDBColumn;
    v7Column6: TcxGridDBColumn;
    v7Column7: TcxGridDBColumn;
    v7Column8: TcxGridDBColumn;
    v7Column9: TcxGridDBColumn;
    ADOQuery2: TADOQuery;
    cxGridPopupMenu7: TcxGridPopupMenu;
    ClientDataSet7: TClientDataSet;
    DataSource7: TDataSource;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    v7Column10: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure MDNOKeyPress(Sender: TObject; var Key: Char);
    procedure TBExportClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv3CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure v3Column19PropertiesEditValueChanged(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
  private
    { Private declarations }
    canshu1,canshu2:string;
    function  UPdateData():Boolean;
    function  JSMDData():Boolean;
    function  JSTJMDData(FJSType:String):Boolean;
    function  UPdateDataKK():Boolean;
    function  YFData(YFTYpeId,CWFactoryNo,CWFactoryName,KFType:string):Boolean;
    function  DelYFData(CRID:String):Boolean;
    function  JSMDHZData():Boolean;
    function YFHZData(YFTYpeId,CWFactoryNo,CWFactoryName,KFType:string):Boolean;
    function DelYFHZData(CRID:String):Boolean;
    procedure InitGridMD();
    procedure InitGridOtherFee();
    function  JSOtherFeeData():Boolean;
    function  YFOtherFeeData(YFTYpeId,CWFactoryNo,CWFactoryName,KFType:string):Boolean;
  public
    { Public declarations }
    FFPInt:Integer;
  end;

var
  frmCWMDOtherFeeList: TfrmCWMDOtherFeeList;

implementation
uses
  U_DataLink,U_RTFun, U_ModuleNote,U_TJCKRKOK;

{$R *.dfm}

procedure TfrmCWMDOtherFeeList.FormDestroy(Sender: TObject);
begin
  frmCWMDOtherFeeList:=nil;
end;

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

procedure TfrmCWMDOtherFeeList.TBRafreshClick(Sender: TObject);
var
  fsj:string;
begin
  Panel2.Visible:=True;
  Panel2.Refresh;
  if cxPageControl1.ActivePageIndex<>2 then
    InitGridMD()
  else
    InitGridOtherFee();
  TBFind.Click;
  Panel2.Visible:=False;
end;
procedure TfrmCWMDOtherFeeList.InitGridMD();
var
  fsj:string;
begin
  try
    ADOQuery3.DisableControls;
    with ADOQuery3 do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      if cxPageControl1.ActivePageIndex=0 then
        sql.Add('exec P_View_MD_JS :begdate,:enddate,:orderno,:MDType,:WSql')
      else
        sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
      //if cxTabControl1.TabIndex<>0 then
      begin
        Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date));
        Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.Date+1));
      end;
      Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
     // Parameters.ParamByName('orderno').Value:='';
      Parameters.ParamByName('MDType').Value:='';
      if cxPageControl1.ActivePageIndex=0 then
      begin
        fsj:=' and  isnull(PF.CKName,'''')<>''调节仓库'' and isnull(JMM.CKName,'''')='''' and JMM.FactoryName<>''长阳仓库'' ';
      end else
      begin
        fsj:=' and isnull(JMM.CKName,'''')<>'''' and exists(select * from JYOrder_Main_MD_KK JMK where JMK.MDID=JMM.MDID)';
      end;
      if cxTabControl1.TabIndex=0 then
      begin
        fsj:=fsj+' and isnull(JMM.CWStatus,'''')='''' ';
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        fsj:=fsj+' and isnull(JMM.CWStatus,'''')=''对账完结'' ';
      end else
      if cxTabControl1.TabIndex=2 then
      begin
        fsj:=fsj+' and isnull(JMM.CWStatus,'''')=''有争议'' ';
      end;
      if Trim(MDFactoryName.Text)<>'' then
      begin
        fsj:=fsj+' and isnull(JMM.FactoryName,'''') like '''+'%'+Trim(MDFactoryName.Text)+'%'+'''';
      end;
      Parameters.ParamByName('WSql').Value:=fsj;
      Open;
    end;
    SCreateCDS20(ADOQuery3,ClientDataSet3);
    SInitCDSData20(ADOQuery3,ClientDataSet3);
  finally
    ADOQuery3.EnableControls;
  end;
end;
procedure TfrmCWMDOtherFeeList.InitGridOtherFee();
var
  fsj:string;
begin
  try
    ADOQuery2.DisableControls;
    with ADOQuery2 do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add('exec P_View_OtherFee_JS :begdate,:enddate,:orderno,:WSql');
      //if cxTabControl1.TabIndex<>0 then
      begin
        Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date));
        Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.Date+1));
      end;
      Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
      if cxTabControl1.TabIndex=0 then
      begin
        fsj:=fsj+' and isnull(JMF.CWStatus,'''')='''' ';
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        fsj:=fsj+' and isnull(JMF.CWStatus,'''')=''对账完结'' ';
      end else
      if cxTabControl1.TabIndex=2 then
      begin
        fsj:=fsj+' and isnull(JMF.CWStatus,'''')=''有争议'' ';
      end;
      if Trim(MDFactoryName.Text)<>'' then
      begin
        fsj:=fsj+' and isnull(JMF.FactoryName,'''') like '''+'%'+Trim(MDFactoryName.Text)+'%'+'''';
      end;
      Parameters.ParamByName('WSql').Value:=fsj;
      Open;
    end;
    SCreateCDS20(ADOQuery2,ClientDataSet7);
    SInitCDSData20(ADOQuery2,ClientDataSet7);
  finally
    ADOQuery3.EnableControls;
  end;
end;
procedure TfrmCWMDOtherFeeList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-60;
  cxPageControl1.ActivePageIndex:=0;
  cxTabControl1.TabIndex:=0;
end;

procedure TfrmCWMDOtherFeeList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('正常码单查询CW',Tv3,'码单管理');
  WriteCxGrid('调节仓库码单查询CW',Tv1,'码单管理');
  WriteCxGrid('其它费用',Tv7,'码单管理');
  Close;
end;

procedure TfrmCWMDOtherFeeList.cxPageControl1Change(Sender: TObject);
begin
  //TBRafresh.Click;
end;

procedure TfrmCWMDOtherFeeList.TBFindClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex=2 then Exit;
  if ADOQuery3.Active=False then Exit;
  //SDofilter10(ClientDataSet3,SGetFilters(Panel1,1,2));
  SDofilter(ADOQuery3,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQuery3,ClientDataSet3);
  SInitCDSData20(ADOQuery3,ClientDataSet3);
end;

procedure TfrmCWMDOtherFeeList.orderNoKeyPress(Sender: TObject; var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Trim(orderNo.Text)='' then Exit;
    if Length(Trim(orderNo.Text))<4 then Exit;
    if cxPageControl1.ActivePageIndex<>2 then
    begin
      try
        ADOQuery3.DisableControls;
        with ADOQuery3 do
        begin
          Close;
          Filtered:=False;
          sql.Clear;
          if cxPageControl1.ActivePageIndex=0 then
            sql.Add('exec P_View_MD_JS :begdate,:enddate,:orderno,:MDType,:WSql')
          else
            sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
          //sql.Add('exec P_View_MD_JS :begdate,:enddate,:orderno,:MDType,:WSql');
          Parameters.ParamByName('begdate').Value:='';
          Parameters.ParamByName('enddate').Value:='';
          Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
          Parameters.ParamByName('MDType').Value:='';
          if cxPageControl1.ActivePageIndex=0 then
          begin
            fsj:=' and  isnull(PF.CKName,'''')<>''调节仓库'' and isnull(JMM.CKName,'''')='''' and JMM.FactoryName<>''长阳仓库'' ';
          end else
          begin
            fsj:=' and isnull(JMM.CKName,'''')<>'''' and exists(select * from JYOrder_Main_MD_KK JMK where JMK.MDID=JMM.MDID)';
          end;
          if cxTabControl1.TabIndex=0 then
          begin
            fsj:=fsj+' and isnull(JMM.CWStatus,'''')='''' ';
          end else
          if cxTabControl1.TabIndex=1 then
          begin
            fsj:=fsj+' and isnull(JMM.CWStatus,'''')=''对账完结'' ';
          end else
          if cxTabControl1.TabIndex=2 then
          begin
            fsj:=fsj+' and isnull(JMM.CWStatus,'''')=''有争议'' ';
          end;
          if Trim(MDFactoryName.Text)<>'' then
          begin
            fsj:=fsj+' and isnull(JMM.FactoryName,'''') like '''+'%'+Trim(MDFactoryName.Text)+'%'+'''';
          end;
          Parameters.ParamByName('WSql').Value:=fsj;
          Open;
        end;
        SCreateCDS20(ADOQuery3,ClientDataSet3);
        SInitCDSData20(ADOQuery3,ClientDataSet3);
      finally
        ADOQuery3.EnableControls;
      end;
    end else
    begin
      try
        ADOQuery2.DisableControls;
        with ADOQuery2 do
        begin
          Close;
          Filtered:=False;
          sql.Clear;
          sql.Add('exec P_View_OtherFee_JS :begdate,:enddate,:orderno,:WSql');
          //if cxTabControl1.TabIndex<>0 then
          begin
            Parameters.ParamByName('begdate').Value:='';
            Parameters.ParamByName('enddate').Value:='';
          end;
          Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
          if cxTabControl1.TabIndex=0 then
          begin
            fsj:=fsj+' and isnull(JMF.CWStatus,'''')='''' ';
          end else
          if cxTabControl1.TabIndex=1 then
          begin
            fsj:=fsj+' and isnull(JMF.CWStatus,'''')=''对账完结'' ';
          end else
          if cxTabControl1.TabIndex=2 then
          begin
            fsj:=fsj+' and isnull(JMF.CWStatus,'''')=''有争议'' ';
          end;
          if Trim(MDFactoryName.Text)<>'' then
          begin
            fsj:=fsj+' and isnull(JMF.FactoryName,'''') like '''+'%'+Trim(MDFactoryName.Text)+'%'+'''';
          end;
          Parameters.ParamByName('WSql').Value:=fsj;
          Open;
        end;
        SCreateCDS20(ADOQuery2,ClientDataSet7);
        SInitCDSData20(ADOQuery2,ClientDataSet7);
      finally
        ADOQuery3.EnableControls;
      end;
    end;

  end;

end;

procedure TfrmCWMDOtherFeeList.MDNOKeyPress(Sender: TObject; var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Trim(MDNO.Text)='' then Exit;
    if Length(Trim(MDNO.Text))<4 then Exit;
    try
      ADOQuery3.DisableControls;
      with ADOQuery3 do
      begin
        Close;
        Filtered:=False;
        sql.Clear;
        sql.Add('exec P_View_MD_JS :begdate,:enddate,:orderno,:MDType,:WSql');
        Parameters.ParamByName('begdate').Value:='';
        Parameters.ParamByName('enddate').Value:='';
        Parameters.ParamByName('orderno').Value:='';
        Parameters.ParamByName('MDType').Value:=Trim(canshu1);
        fsj:=' and JMM.MDNO like '''+'%'+Trim(MDNO.Text)+'%'+'''';
        if cxPageControl1.ActivePageIndex=0 then
        begin
          fsj:=fsj+' and isnull(JMM.CKName,'''')='''' ';
        end else
        begin
          fsj:=fsj+' and isnull(JMM.CKName,'''')<>'''' ';
        end;
        if cxTabControl1.TabIndex=0 then
        begin
          fsj:=fsj+' and isnull(JMM.CWStatus,'''')='''' ';
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          fsj:=fsj+' and isnull(JMM.CWStatus,'''')=''对账完结'' ';
        end else
        if cxTabControl1.TabIndex=2 then
        begin
          fsj:=fsj+' and isnull(JMM.CWStatus,'''')=''有争议'' ';
        end;
        if Trim(MDFactoryName.Text)<>'' then
        begin
          fsj:=fsj+' and isnull(JMM.FactoryName,'''') like '''+'%'+Trim(MDFactoryName.Text)+'%'+'''';
        end;
        Parameters.ParamByName('WSql').Value:=fsj;
        Open;
      end;
      SCreateCDS20(ADOQuery3,ClientDataSet3);
      SInitCDSData20(ADOQuery3,ClientDataSet3);
    finally
      ADOQuery3.EnableControls;
    end;
  end;

end;

procedure TfrmCWMDOtherFeeList.TBExportClick(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  if cxPageControl1.ActivePageIndex=0 then
    TcxGridToExcel('正常码单',cxGrid3)
  else
    TcxGridToExcel('调节仓库码单',cxGrid1);
end;

procedure TfrmCWMDOtherFeeList.FormShow(Sender: TObject);
var
  fsj:string;
begin
  READCxGrid('正常码单查询CW',Tv3,'码单管理');
  READCxGrid('调节仓库码单查询CW',Tv1,'码单管理');
  READCxGrid('其它费用',Tv7,'码单管理');
  if FFPInt=9 then
  begin
    InitGridMD();
    TBFind.Click;
  end;
end;

procedure TfrmCWMDOtherFeeList.Tv3CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fsj:string;
begin
  if ClientDataSet3.IsEmpty then Exit;
  fsj:='select distinct(PSName) Name, Cast('''' as varchar(20)) code from JYOrder_Process where Mainid='''+Trim(ClientDataSet3.fieldbyname('ORDMainId').AsString)+'''';
  SInitComBoxBySql(ADOQueryTemp,PSName,False,fsj);
end;

procedure TfrmCWMDOtherFeeList.cxTabControl1Change(Sender: TObject);
begin
  //TBRafresh.Click;
  
end;

procedure TfrmCWMDOtherFeeList.ToolButton1Click(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex<>2 then
  begin
    if ClientDataSet3.IsEmpty then Exit;
  end else
  begin
    if ClientDataSet7.IsEmpty then Exit;
  end;

  if Trim(ComboBox1.Text)='' then
  begin
    Application.MessageBox('状态未选择!','提示',0);
    Exit;
  end;
  if cxTabControl1.TabIndex=0 then
  begin
    if Trim(ComboBox1.Text)='待对账' then Exit;
  end;
  if cxTabControl1.TabIndex=3 then exit;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    if ClientDataSet3.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据!','提示',0);
      Exit;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    if ClientDataSet7.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据!','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  Panel3.Visible:=True;
  Panel3.Refresh;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    if JSMDHZData() then
    begin
      Panel3.Visible:=False;
      Application.MessageBox('操作成功!','提示',0);
      Exit;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    if UPdateDataKK() then
    begin
      Panel3.Visible:=False;
      Application.MessageBox('操作成功!','提示',0);
      Exit;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    if JSOtherFeeData() then
    begin
      Panel3.Visible:=False;
      Application.MessageBox('操作成功!','提示',0);
      Exit;
    end;
  end;
  Panel3.Visible:=False;
end;
function  TfrmCWMDOtherFeeList.UPdateData():Boolean;
var
  maxno:string;
begin
  Result:=False;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ClientDataSet3 do
    begin
      First;
      while Locate('SSel',True,[])=True do
      begin
        //更新码单财务状态
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD Set CWStatusDate=getdate(), CWStatusFiller='''+Trim(DName)+'''');
          sql.Add(',CWStatusNote='''+Trim(ClientDataSet3.fieldbyname('JSNote').AsString)+'''');
          if ComboBox1.ItemIndex=0 then
          begin
            sql.Add(',CWStatus='''' ');
          end else
          begin
            sql.Add(',CWStatus='''+Trim(ComboBox1.Text)+'''');
          end;
          sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
          ExecSQL;
        end;
        //更新码单财务状态

        //更新应付款数据
        if ComboBox1.ItemIndex=1 then  //1 对账完结生成应付款， 其它删除已生成的应付款
        begin
          //生成应付款
          if cxPageControl1.ActivePageIndex=0 then
          begin
            if YFData(Trim(ClientDataSet3.fieldbyname('MDID').AsString),
                      Trim(ClientDataSet3.fieldbyname('FactoryNo').AsString),
                      Trim(ClientDataSet3.fieldbyname('FactoryName').AsString),
                      '')=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('生成应付款失败!','提示',0);
              Exit;
            end;
          end;

        end else
        begin
          //删除应付款
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
            sql.Add(' and YFTYpe=''自动生成'' ');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if DelYFData(ADOQueryTemp.fieldbyname('CRID').AsString)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('删除应付款失败!','提示',0);
              Exit;
            end;
          end;

        end;
        //更新应付款数据

        //更新表格状态
        if cxTabControl1.TabIndex<>ComboBox1.ItemIndex then
        begin
          ClientDataSet3.Delete;
        end else
        begin
          ClientDataSet3.Edit;
          ClientDataSet3.FieldByName('SSel').Value:=False;
          ClientDataSet3.Post;
        end;
        //更新表格状态
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
    Exit;
  end;
end;
function  TfrmCWMDOtherFeeList.JSMDData():Boolean;
var
  maxno:string;
begin
  Result:=False;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ClientDataSet3 do
    begin
      First;
      while Locate('SSel',True,[])=True do
      begin
        //更新码单财务状态
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD Set CWStatusDate=getdate(), CWStatusFiller='''+Trim(DName)+'''');
          sql.Add(',CWStatusNote='''+Trim(ClientDataSet3.fieldbyname('JSNote').AsString)+'''');
          if ComboBox1.ItemIndex=0 then
          begin
            sql.Add(',CWStatus='''' ');
          end else
          begin
            sql.Add(',CWStatus='''+Trim(ComboBox1.Text)+'''');
          end;
          sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
          ExecSQL;
        end;
        //更新码单财务状态
        //往结算表中写数据
        if ComboBox1.ItemIndex=1 then
        begin
          if Trim(ClientDataSet3.fieldbyname('JSID').AsString)='' then
          begin
            if GetLSNo(ADOQueryCmd,maxno,'JS','JYOrder_Main_MD_JS',4,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取结算表流水号失败!','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(ClientDataSet3.fieldbyname('JSID').AsString);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from JYOrder_Main_MD_JS where JSID='''+Trim(ClientDataSet3.fieldbyname('JSID').AsString)+'''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(ClientDataSet3.fieldbyname('JSID').AsString)='' then
            begin
              Append;
              FieldByName('Filler').Value:=Trim(DName);
            end else
            begin
              Edit;
              FieldByName('Editer').Value:=Trim(DName);
              FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
            end;
            FieldByName('JSID').Value:=Trim(maxno);
            FieldByName('MDID').Value:=Trim(ClientDataSet3.fieldbyname('MDID').AsString);
            FieldByName('JSQtyUnit').Value:=Trim(ClientDataSet3.fieldbyname('QtyUnit').AsString);
            RTSetSaveDataCDS(ADOQueryCmd,Tv3,ClientDataSet3,'JYOrder_Main_MD_JS',9);
            Post;
          end;
        end else
        //if ComboBox1.ItemIndex=0 then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete JYOrder_Main_MD_JS where JSID='''+Trim(ClientDataSet3.fieldbyname('JSID').AsString)+'''');
            ExecSQL;
          end;
        end;

        //往结算表中写数据
        //
        //更新应付款数据
        if ComboBox1.ItemIndex=1 then  //1 对账完结生成应付款， 其它删除已生成的应付款
        begin
          //生成应付款
          if cxPageControl1.ActivePageIndex=0 then
          begin
            if YFData(Trim(ClientDataSet3.fieldbyname('MDID').AsString),
                      Trim(ClientDataSet3.fieldbyname('MDFactoryNo').AsString),
                      Trim(ClientDataSet3.fieldbyname('MDFactoryName').AsString),
                      '')=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('生成应付款失败!','提示',0);
              Exit;
            end;
          end;

        end else
        begin
          //删除应付款
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
            sql.Add(' and YFTYpe=''自动生成'' ');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if DelYFData(ADOQueryTemp.fieldbyname('CRID').AsString)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('删除应付款失败!','提示',0);
              Exit;
            end;
          end;

        end;
        //更新应付款数据

        //更新表格状态
        if cxTabControl1.TabIndex<>ComboBox1.ItemIndex then
        begin
          ClientDataSet3.Delete;
        end else
        begin
          ClientDataSet3.Edit;
          ClientDataSet3.FieldByName('SSel').Value:=False;
          ClientDataSet3.Post;
        end;
        //更新表格状态
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
    Exit;
  end;
end;
function  TfrmCWMDOtherFeeList.JSMDHZData():Boolean;
var
  maxno:string;
begin
  Result:=False;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      First;
      while Locate('SSel',True,[])=True do
      begin
        //更新码单财务状态
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD Set CWStatusDate=getdate(), CWStatusFiller='''+Trim(DName)+'''');
          sql.Add(',CWStatusNote='''+Trim(ClientDataSet3.fieldbyname('JSNote').AsString)+'''');
          if ComboBox1.ItemIndex=0 then
          begin
            sql.Add(',CWStatus='''' ');
          end else
          begin
            sql.Add(',CWStatus='''+Trim(ComboBox1.Text)+'''');
          end;
          sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
          ExecSQL;
        end;
        //更新码单财务状态
        //往结算表中写数据
        if ComboBox1.ItemIndex=1 then
        begin
          if Trim(ClientDataSet3.fieldbyname('JSID').AsString)='' then
          begin
            if GetLSNo(ADOQueryCmd,maxno,'JS','JYOrder_Main_MD_JS',4,1)=False then
            begin
              ClientDataSet3.EnableControls;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取结算表流水号失败!','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(ClientDataSet3.fieldbyname('JSID').AsString);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from JYOrder_Main_MD_JS where JSID='''+Trim(ClientDataSet3.fieldbyname('JSID').AsString)+'''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(ClientDataSet3.fieldbyname('JSID').AsString)='' then
            begin
              Append;
              FieldByName('Filler').Value:=Trim(DName);
            end else
            begin
              Edit;
              FieldByName('Editer').Value:=Trim(DName);
              FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
            end;
            FieldByName('JSID').Value:=Trim(maxno);
            FieldByName('MDID').Value:=Trim(ClientDataSet3.fieldbyname('MDID').AsString);
            FieldByName('JSQtyUnit').Value:=Trim(ClientDataSet3.fieldbyname('QtyUnit').AsString);
            FieldByName('JSDate').Value:=ClientDataSet3.fieldbyname('MDDate').Value;
            FieldByName('ORDMainId').Value:=ClientDataSet3.fieldbyname('ORDMainId').Value;
            RTSetSaveDataCDS(ADOQueryCmd,Tv3,ClientDataSet3,'JYOrder_Main_MD_JS',9);
            Post;
          end;
        end else
        //if ComboBox1.ItemIndex=0 then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete JYOrder_Main_MD_JS where JSID='''+Trim(ClientDataSet3.fieldbyname('JSID').AsString)+'''');
            ExecSQL;
          end;
        end;

        //往结算表中写数据
        //
        //更新应付款数据
        if ComboBox1.ItemIndex=1 then  //1 对账完结生成应付款， 其它删除已生成的应付款
        begin
          //生成应付款
          if cxPageControl1.ActivePageIndex=0 then
          begin
            if YFHZData(Trim(ClientDataSet3.fieldbyname('MDID').AsString),
                      Trim(ClientDataSet3.fieldbyname('MDFactoryNo').AsString),
                      Trim(ClientDataSet3.fieldbyname('MDFactoryName').AsString),
                      '')=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('生成应付款失败!','提示',0);
              Exit;
            end;
          end;

        end else
        begin
          //删除应付款
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR where YFID='''+Trim(ClientDataSet3.fieldbyname('YFID').AsString)+'''');
            sql.Add(' and YFTYpe=''自动生成'' ');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if DelYFHZData(ADOQueryTemp.fieldbyname('CRID').AsString)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('删除应付款失败!','提示',0);
              Exit;
            end;
          end;

        end;
        //更新应付款数据

        //更新表格状态
        if cxTabControl1.TabIndex<>ComboBox1.ItemIndex then
        begin
          ClientDataSet3.Delete;
        end else
        begin
          ClientDataSet3.Edit;
          ClientDataSet3.FieldByName('SSel').Value:=False;
          ClientDataSet3.Post;
        end;
        //更新表格状态
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet3.EnableControls;
    Result:=True;
  except
    Result:=False;
    ClientDataSet3.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
    Exit;
  end;
end;
function  TfrmCWMDOtherFeeList.JSOtherFeeData():Boolean;
var
  maxno:string;
begin
  Result:=False;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet7.DisableControls;
    with ClientDataSet7 do
    begin
      First;
      while Locate('SSel',True,[])=True do
      begin
        //更新其它费财务状态
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_Fee Set CWStatusDate=getdate(), CWStatusFiller='''+Trim(DName)+'''');
          sql.Add(',CWStatusNote='''+Trim(ClientDataSet7.fieldbyname('JSNote').AsString)+'''');
          if ComboBox1.ItemIndex=0 then
          begin
            sql.Add(',CWStatus='''' ');
          end else
          begin
            sql.Add(',CWStatus='''+Trim(ComboBox1.Text)+'''');
          end;
          sql.Add(' where JFID='''+Trim(ClientDataSet7.fieldbyname('JFID').AsString)+'''');
          ExecSQL;
        end;
        //更新其它费财务状态
        //往结算表中写数据
        if ComboBox1.ItemIndex=1 then
        begin
          if Trim(ClientDataSet7.fieldbyname('JSID').AsString)='' then
          begin
            if GetLSNo(ADOQueryCmd,maxno,'JS','JYOrder_Main_MD_JS',4,1)=False then
            begin
              ClientDataSet7.EnableControls;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取结算表流水号失败!','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(ClientDataSet7.fieldbyname('JSID').AsString);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from JYOrder_Main_MD_JS where JSID='''+Trim(ClientDataSet7.fieldbyname('JSID').AsString)+'''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(ClientDataSet7.fieldbyname('JSID').AsString)='' then
            begin
              Append;
              FieldByName('Filler').Value:=Trim(DName);
            end else
            begin
              Edit;
              FieldByName('Editer').Value:=Trim(DName);
              FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
            end;
            FieldByName('JSID').Value:=Trim(maxno);
            FieldByName('JFID').Value:=Trim(ClientDataSet7.fieldbyname('JFID').AsString);
            FieldByName('JSQtyUnit').Value:=Trim(ClientDataSet7.fieldbyname('JFQtyUnit').AsString);
            FieldByName('JSDate').Value:=ClientDataSet7.fieldbyname('JFDate').Value;
            FieldByName('ORDMainId').Value:=ClientDataSet7.fieldbyname('MainId').Value;
            RTSetSaveDataCDS(ADOQueryCmd,Tv7,ClientDataSet7,'JYOrder_Main_MD_JS',9);
            Post;
          end;
        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete JYOrder_Main_MD_JS where JSID='''+Trim(ClientDataSet7.fieldbyname('JSID').AsString)+'''');
            ExecSQL;
          end;
        end;

        //往结算表中写数据
        //
        //更新应付款数据
        if ComboBox1.ItemIndex=1 then  //1 对账完结生成应付款， 其它删除已生成的应付款
        begin
          //生成应付款
          if cxPageControl1.ActivePageIndex=2 then
          begin
            if YFOtherFeeData(Trim(ClientDataSet7.fieldbyname('JFID').AsString),
                      Trim(ClientDataSet7.fieldbyname('MDFactoryNo').AsString),
                      Trim(ClientDataSet7.fieldbyname('MDFactoryName').AsString),
                      '')=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('生成应付款失败!','提示',0);
              Exit;
            end;
          end;

        end else
        begin
          //删除应付款
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR where YFID='''+Trim(ClientDataSet7.fieldbyname('YFID').AsString)+'''');
            sql.Add(' and YFTYpe=''自动生成'' ');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if DelYFHZData(ADOQueryTemp.fieldbyname('CRID').AsString)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('删除应付款失败!','提示',0);
              Exit;
            end;
          end;

        end;
        //更新应付款数据

        //更新表格状态
        if cxTabControl1.TabIndex<>ComboBox1.ItemIndex then
        begin
          ClientDataSet7.Delete;
        end else
        begin
          ClientDataSet7.Edit;
          ClientDataSet7.FieldByName('SSel').Value:=False;
          ClientDataSet7.Post;
        end;
        //更新表格状态
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet7.EnableControls;
    Result:=True;
  except
    Result:=False;
    ClientDataSet7.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
    Exit;
  end;
end;
function  TfrmCWMDOtherFeeList.JSTJMDData(FJSType:String):Boolean;
var
  maxno,FJSID:string;
begin
  Result:=False;
  //往结算表中写数据
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_JS where KKID='''+Trim(ClientDataSet4.fieldbyname('KKID').AsString)+'''');
    sql.Add(' and JSType='''+Trim(FJSType)+'''');
    Open;
  end;
  FJSID:=Trim(ADOQueryTemp.fieldbyname('JSID').AsString);
  if ComboBox1.ItemIndex=1 then
  begin
    if Trim(FJSID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'JS','JYOrder_Main_MD_JS',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取结算表流水号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FJSID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_JS where JSID='''+Trim(FJSID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FJSID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('JSID').Value:=Trim(maxno);
      FieldByName('MDID').Value:=Trim(ClientDataSet3.fieldbyname('MDID').AsString);
      FieldByName('KKID').Value:=Trim(ClientDataSet4.fieldbyname('KKID').AsString);
      FieldByName('JSType').Value:=Trim(FJSType);
      if FJSType='责任人' then
      begin
        FieldByName('JSQty').Value:=-1;
        FieldByName('JSPrice').Value:=ClientDataSet4.fieldbyname('ZZPonMoney').Value;
        FieldByName('JSMoney').Value:=ClientDataSet4.fieldbyname('ZZPonMoney').AsFloat*-1;
        FieldByName('FactoryNo').Value:=ClientDataSet4.fieldbyname('ZZPerson').Value;
        FieldByName('FactoryName').Value:=ClientDataSet4.fieldbyname('ZZPerson').Value;
      end else
      if FJSType='责任工厂' then
      begin
        FieldByName('JSQty').Value:=-1;
        FieldByName('JSPrice').Value:=ClientDataSet4.fieldbyname('ZZFtyMoney').Value;
        FieldByName('JSMoney').Value:=ClientDataSet4.fieldbyname('ZZFtyMoney').AsFloat*-1;
        FieldByName('FactoryNo').Value:=ClientDataSet4.fieldbyname('ZZFactoryNo').Value;
        FieldByName('FactoryName').Value:=ClientDataSet4.fieldbyname('ZZFactoryName').Value;
      end;
      Post;
    end;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main_MD_JS where JSID='''+Trim(FJSID)+'''');
      ExecSQL;
    end;
  end;
  Result:=True;

end;
function  TfrmCWMDOtherFeeList.UPdateDataKK():Boolean;
var
  maxno,maxjsno,maxkkno:string;
begin
  Result:=False;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //更新码单财务状态
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD Set CWStatusDate=getdate(), CWStatusFiller='''+Trim(DName)+'''');
     // sql.Add(',CWStatusNote='''+Trim(ClientDataSet3.fieldbyname('CWStatusNote').AsString)+'''');
      if ComboBox1.ItemIndex=0 then
      begin
        sql.Add(',CWStatus='''' ');
      end else
      begin
        sql.Add(',CWStatus='''+Trim(ComboBox1.Text)+'''');
      end;
      sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
      ExecSQL;
    end;
    //更新码单财务状态
    //往结算表中写数
    with ClientDataSet4 do
    begin
      First;
      while not Eof do
      begin
        if ClientDataSet4.FieldByName('ZZFtyMoney').Value>0 then
        begin
          JSTJMDData('责任工厂');
        end;
        if ClientDataSet4.FieldByName('ZZPonMoney').Value>0 then
        begin
          JSTJMDData('责任人');
        end;
        Next;
      end;
    end;
    //往结算表中写数据
    //更新应付款数据
    if ComboBox1.ItemIndex=1 then  //1 对账完结生成应付款， 其它删除已生成的应付款
    begin
      //生成应付款
      if ClientDataSet4.IsEmpty then Exit;
      with ClientDataSet4 do
      begin
        First;
        while not Eof do
        begin
          if ClientDataSet4.FieldByName('ZZFtyMoney').Value>0 then
          begin
            if YFData(Trim(ClientDataSet4.fieldbyname('KKID').AsString),
                    Trim(ClientDataSet4.fieldbyname('ZZFactoryNo').AsString),
                    Trim(ClientDataSet4.fieldbyname('ZZFactoryName').AsString),
                    '责任工厂')=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('生成应付款失败!','提示',0);
              Exit;
            end;
          end;
          if ClientDataSet4.FieldByName('ZZPonMoney').Value>0 then
          begin
            if YFData(Trim(ClientDataSet4.fieldbyname('KKID').AsString),
                    Trim(ClientDataSet4.fieldbyname('ZZPerson').AsString),
                    Trim(ClientDataSet4.fieldbyname('ZZPerson').AsString),
                    '责任人')=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('生成应付款失败!','提示',0);
              Exit;
            end;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('Update JYOrder_Main_MD_KK Set CWHX=1,CWHXDate=getdate(),CWHXPerson='''+Trim(DName)+'''');
            sql.Add(' where KKID='''+Trim(ClientDataSet4.fieldbyname('KKID').AsString)+'''');
            ExecSQL;
          end;
          Next;
        end;
      end;
    end else
    begin
      //删除应付款
      with ClientDataSet4 do
      begin
        First;
        while not Eof do
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR where YFTypeID='''+Trim(ClientDataSet4.fieldbyname('KKID').AsString)+'''');
            sql.Add(' and YFTYpe=''自动生成'' ');
            sql.Add(' and KFType=''责任人'' ');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if DelYFData(ADOQueryTemp.fieldbyname('CRID').AsString)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('删除应付款失败!','提示',0);
              Exit;
            end;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR where YFTypeID='''+Trim(ClientDataSet4.fieldbyname('KKID').AsString)+'''');
            sql.Add(' and YFTYpe=''自动生成'' ');
            sql.Add(' and KFType=''责任工厂'' ');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if DelYFData(ADOQueryTemp.fieldbyname('CRID').AsString)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('删除应付款失败!','提示',0);
              Exit;
            end;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('Update JYOrder_Main_MD_KK Set CWHX=0,CWHXDate=Null,CWHXPerson=''''');
            sql.Add(' where KKID='''+Trim(ClientDataSet4.fieldbyname('KKID').AsString)+'''');
            ExecSQL;
          end;
          Next;
        end;
      end;


    end;
    //更新应付款数据

    //更新表格状态
    if cxTabControl1.TabIndex<>ComboBox1.ItemIndex then
    begin
      ClientDataSet3.Delete;
    end else
    begin
      ClientDataSet3.Edit;
      ClientDataSet3.FieldByName('SSel').Value:=False;
      ClientDataSet3.Post;
    end;
    //更新表格状态
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
    Exit;
  end;
end;
function TfrmCWMDOtherFeeList.YFHZData(YFTYpeId,CWFactoryNo,CWFactoryName,KFType:string):Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryNo='''+Trim(CWFactoryNo)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.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('FactoryName').Value:=Trim(CWFactoryName);
      FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryNo='''+Trim(CWFactoryNo)+'''');
    sql.Add(' and MainId='''+Trim(ClientDataSet3.fieldbyname('ORDMainId').AsString)+'''');
    sql.Add(' and CRTime='''+Trim(ClientDataSet3.fieldbyname('MDDate').AsString)+'''');
    SQL.Add(' and Price='+Trim(ClientDataSet3.fieldbyname('JSPrice').AsString));
    sql.Add(' and KFType=''正常'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'ZC','YF_Money_CR',3,1)=False then
    begin
      Application.MessageBox('取正常码单应付最大号失败!','提示',0);
      Exit;
    end;
  end else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
      Append
    else
      Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('MainId').Value:=Trim(ClientDataSet3.fieldbyname('OrdMainId').AsString);
    FieldByName('CRID').Value:=StrToInt(CRID);
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('CRType').Value:='应付款登记';
    FieldByName('CRFlag').Value:='应付付';
    FieldByName('QtyFlag').Value:=1;
    FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('FactoryName').Value:=Trim(CWFactoryName);
    FieldByName('HZFactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('HZFactoryName').Value:=Trim(CWFactoryName);
    FieldByName('CRTime').Value:=ClientDataSet3.fieldbyname('MDDate').Value;
    //FieldByName('Qty').Value:=ClientDataSet3.fieldbyname('Qty').Value;
    //FieldByName('PS').Value:=ClientDataSet3.fieldbyname('PS').Value;
    FieldByName('Price').Value:=ClientDataSet3.fieldbyname('Price').Value;
    FieldByName('QtyUnit').Value:=Trim(ClientDataSet3.fieldbyname('QtyUnit').AsString);
    FieldByName('YFType').Value:='自动生成';
    FieldByName('HuiLv').Value:=1;
    FieldByName('BZType').Value:='人民币';
    FieldByName('YFName').Value:=Trim(ClientDataSet3.fieldbyname('PSName').AsString)+'费用';
    FieldByName('PSName').Value:=Trim(ClientDataSet3.fieldbyname('PSName').AsString);
    FieldByName('KFType').Value:='正常';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_JS Set YFID='''+Trim(YFID)+'''');
    sql.Add(' where MDID='''+Trim(YFTYpeId)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set ');
    sql.Add(' PS=(select isnull(Sum(JSPS),0) from JYOrder_Main_MD_JS A where A.YFID=YF_Money_CR.YFID)');
    sql.Add(' ,Qty=(select isnull(Sum(JSQty),0) from JYOrder_Main_MD_JS A where A.YFID=YF_Money_CR.YFID)');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;

  Result:=True;
end;
function TfrmCWMDOtherFeeList.YFData(YFTYpeId,CWFactoryNo,CWFactoryName,KFType:string):Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryNo='''+Trim(CWFactoryNo)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.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('FactoryName').Value:=Trim(CWFactoryName);
      FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryNo='''+Trim(CWFactoryNo)+'''');
    sql.Add(' and YFTypeId='''+Trim(YFTYpeId)+'''');
    if cxPageControl1.ActivePageIndex=1 then
    begin
      sql.Add(' and isnull(KFType,'''')='''+Trim(KFType)+'''');
    end;
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if cxPageControl1.ActivePageIndex=0 then
    begin
      if GetLSNo(ADOQueryCmd,YFID,'ZC','YF_Money_CR',3,1)=False then
      begin
        Application.MessageBox('取正常码单应付最大号失败!','提示',0);
        Exit;
      end;
    end else
    if cxPageControl1.ActivePageIndex=1 then
    begin
      if GetLSNo(ADOQueryCmd,YFID,'KK','YF_Money_CR',3,1)=False then
      begin
        Application.MessageBox('取调节仓库码单应付最大号失败!','提示',0);
        Exit;
      end;
    end;
  end else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
      Append
    else
      Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('YFTypeId').Value:=Trim(YFTYpeId);
    FieldByName('MainId').Value:=Trim(ClientDataSet3.fieldbyname('OrdMainId').AsString);
    FieldByName('MDID').Value:=Trim(ClientDataSet3.fieldbyname('MDID').AsString);
    FieldByName('CRID').Value:=StrToInt(CRID);
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('CRType').Value:='应付款登记';
    FieldByName('CRFlag').Value:='应付付';
    FieldByName('QtyFlag').Value:=1;
    FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('FactoryName').Value:=Trim(CWFactoryName);
    FieldByName('HZFactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('HZFactoryName').Value:=Trim(CWFactoryName);
    if cxPageControl1.ActivePageIndex=0 then
      FieldByName('CRTime').Value:=ClientDataSet3.fieldbyname('MDDate').Value
    else
      FieldByName('CRTime').Value:=ClientDataSet4.fieldbyname('KKDate').Value;
    if cxPageControl1.ActivePageIndex=0 then
    begin
      FieldByName('Qty').Value:=ClientDataSet3.fieldbyname('Qty').Value;
      FieldByName('PS').Value:=ClientDataSet3.fieldbyname('PS').Value;
      FieldByName('Price').Value:=ClientDataSet3.fieldbyname('Price').Value;
      FieldByName('QtyUnit').Value:=Trim(ClientDataSet3.fieldbyname('QtyUnit').AsString);
    end else
    begin
      FieldByName('Qty').Value:=1;
      if Trim(KFType)='责任工厂' then
        FieldByName('Price').Value:=ClientDataSet4.fieldbyname('ZZFtyMoney').Value*-1
      else
        FieldByName('Price').Value:=ClientDataSet4.fieldbyname('ZZPonMoney').Value*-1;
    end;
    FieldByName('YFType').Value:='自动生成';
    FieldByName('HuiLv').Value:=1;
    FieldByName('BZType').Value:='人民币';
    if cxPageControl1.ActivePageIndex=0 then
    begin
      FieldByName('YFName').Value:=Trim(ClientDataSet3.fieldbyname('PSName').AsString)+'费用';
    end else
    begin
      FieldByName('YFName').Value:='扣费';
      FieldByName('KFType').Value:=Trim(KFType);
    end;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  if cxPageControl1.ActivePageIndex=1 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYorder_Main_MD_JS Set YFID='''+Trim(YFID)+'''');
      sql.Add(' where KKID='''+Trim(YFTYpeId)+'''');
      sql.Add(' and FactoryNo='''+Trim(CWFactoryNo)+'''');
      ExecSQL;
    end;
  end;

  Result:=True;
end;
function TfrmCWMDOtherFeeList.YFOtherFeeData(YFTYpeId,CWFactoryNo,CWFactoryName,KFType:string):Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryNo='''+Trim(CWFactoryNo)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.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('FactoryName').Value:=Trim(CWFactoryName);
      FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryNo='''+Trim(CWFactoryNo)+'''');
    sql.Add(' and YFTypeId='''+Trim(YFTYpeId)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'OF','YF_Money_CR',3,1)=False then
    begin
      Application.MessageBox('取其它费应付最大号失败!','提示',0);
      Exit;
    end;
  end else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
      Append
    else
      Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('YFTypeId').Value:=Trim(YFTYpeId);
    FieldByName('MainId').Value:=Trim(ClientDataSet7.fieldbyname('MainId').AsString);
    FieldByName('JFID').Value:=Trim(ClientDataSet7.fieldbyname('JFID').AsString);
    FieldByName('CRID').Value:=StrToInt(CRID);
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('CRType').Value:='应付款登记';
    FieldByName('CRFlag').Value:='应付付';
    FieldByName('QtyFlag').Value:=1;
    FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('FactoryName').Value:=Trim(CWFactoryName);
    FieldByName('HZFactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('HZFactoryName').Value:=Trim(CWFactoryName);
    FieldByName('CRTime').Value:=ClientDataSet7.fieldbyname('JFDate').Value;
    FieldByName('Qty').Value:=ClientDataSet7.fieldbyname('JFQty').Value;
    //FieldByName('PS').Value:=ClientDataSet3.fieldbyname('PS').Value;
    FieldByName('Price').Value:=ClientDataSet7.fieldbyname('JFPrice').Value;
    FieldByName('QtyUnit').Value:=Trim(ClientDataSet7.fieldbyname('JFQtyUnit').AsString);
    FieldByName('YFType').Value:='自动生成';
    FieldByName('HuiLv').Value:=1;
    FieldByName('BZType').Value:='人民币';
    FieldByName('YFName').Value:=Trim(ClientDataSet7.fieldbyname('JFName').AsString);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_JS Set YFID='''+Trim(YFID)+'''');
    sql.Add(' where JFID='''+Trim(YFTYpeId)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update ');
    ExecSQL;
  end;}
  Result:=True;
end;
function TfrmCWMDOtherFeeList.DelYFHZData(CRID:String):Boolean;
begin
  Result:=False;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+Trim(CRID));
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR where not exists(select * from JYOrder_Main_MD_JS A where ');
    sql.Add(' A.YFID=YF_Money_CR.YFID and YF_Money_CR.YFType=''自动生成'' )');
    sql.Add(' and YFType=''自动生成'' ');
    ExecSQL;
  end;
  Result:=True;
end;
function TfrmCWMDOtherFeeList.DelYFData(CRID:String):Boolean;
begin
  Result:=False;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR where ');
    sql.Add(' MDId='''+Trim(ClientDataSet3.fieldbyname('MDId').AsString)+'''');
    sql.Add(' and YFType=''自动生成'' ');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+Trim(CRID));
    ExecSQL;
  end;
  Result:=True;
end;

procedure TfrmCWMDOtherFeeList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_KK where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
    sql.Add(' and isnull(KKTYpe,'''')=''协商'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet2);
  SInitCDSData20(ADOQuery1,ClientDataSet2);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_KK where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
    sql.Add(' and isnull(KKTYpe,'''')=''结果'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet4);
  SInitCDSData20(ADOQuery1,ClientDataSet4);
end;

procedure TfrmCWMDOtherFeeList.ToolButton2Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='对账管理';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmCWMDOtherFeeList.v3Column19PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,FPrice,FQty:string;
begin
  FName:=Trim(Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  FPrice:=Trim(ClientDataSet3.fieldbyname('JSPrice').AsString);
  FQty:=Trim(ClientDataSet3.fieldbyname('JSQty').AsString);
  if Trim(FPrice)='' then
  begin
    FPrice:='0';
  end;
  if Trim(FQty)='' then
  begin
    FQty:='0';
  end;
  if Trim(FName)<>'JSMoney' then
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('JSMoney').Value:=StrToFloat(FPrice)*StrToFloat(FQty);
      Post;
    end;
  end else
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('JSMoney').Value:=mvalue;
      Post;
    end;
  end;
end;

procedure TfrmCWMDOtherFeeList.N1Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet3,True);
end;

procedure TfrmCWMDOtherFeeList.N2Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet3,False);
end;

procedure TfrmCWMDOtherFeeList.N3Click(Sender: TObject);
begin
  ClientDataSet3.DisableControls;
  with ClientDataSet3 do
  begin
    while ClientDataSet3.Locate('SSel',True,[])=True do
    begin
      Edit;
      FieldByName('JSPS').Value:=FieldByName('PS').Value;
      FieldByName('JSQty').Value:=FieldByName('Qty').Value;
      FieldByName('JSPrice').Value:=FieldByName('Price').Value;
      FieldByName('JSMoney').Value:=FieldByName('Money').Value;
      FieldByName('SSel').Value:=False;
      Post;
    end;
  end;
  ClientDataSet3.EnableControls;
end;

procedure TfrmCWMDOtherFeeList.Tv1DblClick(Sender: TObject);
var
  fsj:string;
begin
  try
    frmTJCKRKOK:=TfrmTJCKRKOK.Create(Application);
    with frmTJCKRKOK do
    begin

      if Trim(Self.ClientDataSet3.fieldbyname('Status').AsString)='协商' then
        frmTJCKRKOK.cxTabControl1.TabIndex:=2
      else
      if Trim(Self.ClientDataSet3.fieldbyname('Status').AsString)='结果' then
        frmTJCKRKOK.cxTabControl1.TabIndex:=3;
      //frmTJCKRKOK.OrderNo.Text:=Trim(self.ClientDataSet3.fieldbyname('OrderNo').AsString);
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select JM.OrderNo, A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price, ');
        sql.Add(' JS.PRTHX,JS.PRTColor,JS.PRTColorNo,D.C_Code,D.C_CodeName,ClotheType10=A.ClotheType,');
        sql.Add(' JSKCPS=A.RKPS-A.CKPS,');
        sql.Add(' JSKCQty=A.RKQty-A.CKQty,');
        sql.Add(' JM.MPRTCodeName,MPRTCode=RTrim(JM.MPRTCode)+RTrim(JM.MPRTGY)');
        sql.Add(' from JYOrder_Main_MD  A');
        sql.Add(' inner join JYOrder_Main JM on A.OrdMainId=JM.MainId');
        sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
        sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
        sql.Add(' inner join Contract_Main C on A.ConMainid=C.MainID');
        sql.Add(' inner join Contract_Sub D on A.ConSubid=D.SubID');
        sql.Add(' where A.MDID='''+Trim(Self.ClientDataSet3.fieldbyname('MDID').AsString)+'''');
        SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
        if cxTabControl1.TabIndex=2 then
        begin
          sql.Add('  and isnull(A.Status,'''')=''协商'' ');
        end else
        if cxTabControl1.TabIndex=3 then
        begin
          sql.Add('  and isnull(A.Status,'''')=''结果'' ');
        end;
        Open;
      end;
      SCreateCDS20(ADOQuery1,ClientDataSet6);
      SInitCDSData20(ADOQuery1,ClientDataSet6);
      fsj:='select Code=FactoryNo,Name=FactoryName from JYOrder_Main_MD where ORDMainId='''+Trim(ClientDataSet6.fieldbyname('ORDMainId').AsString)+''''+
       ' group by FactoryNo,FactoryName';
      SInitCxBandGridComboBoxBySql(frmTJCKRKOK.ADOQueryTemp,frmTJCKRKOK.v2fty,fsj,1,True,'');
      with frmTJCKRKOK.ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      SCSHDataNew(frmTJCKRKOK.ADOQuery1,frmTJCKRKOK.ScrollBox1,2);
      frmTJCKRKOK.InitGridKK();
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main_MD_Out where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmTJCKRKOK.Free;
  end;
end;

procedure TfrmCWMDOtherFeeList.orderNoChange(Sender: TObject);
var
  fsj:string;
begin
  if cxPageControl1.ActivePageIndex<>2 then
  begin
    if ADOQuery3.Active=False then Exit;
    SDofilter(ADOQuery3,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQuery3,ClientDataSet3);
    SInitCDSData20(ADOQuery3,ClientDataSet3);
  end else
  begin
    if ADOQuery2.Active=False then Exit;
    if Trim(orderNo.Text)<>'' then
    begin
      fsj:=' orderno like '''+'%'+Trim(orderNo.Text)+'%'+'''';
      if Trim(MDFactoryName.Text)<>'' then
      begin
        fsj:=fsj+' and MDFactoryName like '''+'%'+Trim(MDFactoryName.Text)+'%'+'''';
      end;
    end else
    begin
      if Trim(MDFactoryName.Text)<>'' then
      begin
        fsj:=' MDFactoryName like '''+'%'+Trim(MDFactoryName.Text)+'%'+'''';
      end;
    end;
    SDofilter(ADOQuery2,fsj);
    SCreateCDS20(ADOQuery2,ClientDataSet7);
    SInitCDSData20(ADOQuery2,ClientDataSet7);
  end;

end;

procedure TfrmCWMDOtherFeeList.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet7,True);
end;

procedure TfrmCWMDOtherFeeList.MenuItem2Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet7,False);
end;

procedure TfrmCWMDOtherFeeList.MenuItem3Click(Sender: TObject);
begin
  ClientDataSet7.DisableControls;
  with ClientDataSet7 do
  begin
    while ClientDataSet7.Locate('SSel',True,[])=True do
    begin
      Edit;
      //FieldByName('JSPS').Value:=FieldByName('PS').Value;
      FieldByName('JSQty').Value:=FieldByName('JFQty').Value;
      FieldByName('JSPrice').Value:=FieldByName('JFPrice').Value;
      FieldByName('JSMoney').Value:=FieldByName('JFMoney').Value;
      FieldByName('SSel').Value:=False;
      Post;
    end;
  end;
  ClientDataSet7.EnableControls;
end;

end.
