unit U_YSMXlist;

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, cxDropDownEdit, RM_e_Xls, cxGridBandedTableView,
  cxGridDBBandedTableView, BtnEdit, cxPC, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu, MovePanel, Menus, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmYSMXlist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    MPRTCodeName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    Label3: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    Label5: TLabel;
    ToolButton3: TToolButton;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    Panetime: TPanel;
    TV1: TcxGridDBBandedTableView;
    V1Column1: TcxGridDBBandedColumn;
    V1Column2: TcxGridDBBandedColumn;
    V1Column4: TcxGridDBBandedColumn;
    V1Column5: TcxGridDBBandedColumn;
    V1Column6: TcxGridDBBandedColumn;
    V1Column7: TcxGridDBBandedColumn;
    V1Column8: TcxGridDBBandedColumn;
    V1Column9: TcxGridDBBandedColumn;
    V1Column12: TcxGridDBBandedColumn;
    V1Column15: TcxGridDBBandedColumn;
    V1Column3: TcxGridDBBandedColumn;
    factoryName: TBtnEditA;
    V1Column10: TcxGridDBBandedColumn;
    V1Column11: TcxGridDBBandedColumn;
    V1Column13: TcxGridDBBandedColumn;
    V1Column14: TcxGridDBBandedColumn;
    V1Column16: TcxGridDBBandedColumn;
    cxTabControl1: TcxTabControl;
    Label7: TLabel;
    YFGSType: TComboBox;
    V1Column18: TcxGridDBBandedColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label8: TLabel;
    ishs: TComboBox;
    V1Column21: TcxGridDBBandedColumn;
    V1Column22: TcxGridDBBandedColumn;
    V1Column23: TcxGridDBBandedColumn;
    V1Column24: TcxGridDBBandedColumn;
    V1Column25: TcxGridDBBandedColumn;
    V1Column26: TcxGridDBBandedColumn;
    cxStyle2: TcxStyle;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    TV1Column1: TcxGridDBBandedColumn;
    TV1Column2: TcxGridDBBandedColumn;
    ToolButton8: TToolButton;
    ComboBox1: TComboBox;
    MovePanel1: TMovePanel;
    dzdate: TDateTimePicker;
    Label9: TLabel;
    Button1: TButton;
    Button2: TButton;
    TV1Column3: TcxGridDBBandedColumn;
    MXORDERNO: TEdit;
    lbl1: TLabel;
    TV1Column4: TcxGridDBBandedColumn;
    TV1Column5: TcxGridDBBandedColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TV1Column6: TcxGridDBBandedColumn;
    TV1Column7: TcxGridDBBandedColumn;
    Label10: TLabel;
    CPColor: TEdit;
    Label11: TLabel;
    cpcode: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure P_OrderNo_YSK(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure V1Column9CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure V1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure V1Column11PropertiesEditValueChanged(Sender: TObject);
    procedure V1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure MXORDERNOChange(Sender: TObject);
    procedure V1Column13PropertiesEditValueChanged(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure MXORDERNOKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }

    procedure InitGrid();
  public
    { Public declarations }
    RKFlag, FCYID: string;
    fType: string;
    CANSHU2, canshu1: string;
  end;

var
  frmYSMXlist: TfrmYSMXlist;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ProductOrderNewList, U_ZdyAttachment;

{$R *.dfm}

procedure TfrmYSMXlist.InitGrid();
begin
  if trim(factoryName.Text) = '' then
  begin
    application.MessageBox('请输入客户，以便提高查询速度！', '提示信息', MB_ICONERROR);
    exit;
  end;
//  if trim(YFGSType.Text) = '' then
//  begin
//    application.MessageBox('请选择应收类别，以便提高查询速度！', '提示信息', MB_ICONERROR);
//    exit;
//  end;
  screen.Cursor := crsqlwait;
  Panetime.Visible := true;
  Panetime.Left := (Width - Panetime.Width) div 2;
  Panetime.top := (Height - Panetime.Height - 200) div 2;
  application.ProcessMessages;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;

      if CANSHU1 = '业务' then
      begin
        sql.Add(' exec  P_YSSK_List_mx_yw  ');
        sql.Add(' @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
        sql.Add(',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
        sql.Add(',@factoryName=' + quotedstr(Trim(factoryName.Text)));
        case cxTabControl1.TabIndex of
          0:
            begin
              sql.Add(',@flag=''全部'' ');
            end;
          1:
            begin
              sql.Add(',@flag=''0'' ');
            end;
          2:
            begin
              sql.Add(',@flag=''1'' ');
            end;
        end;
        sql.Add(',@YFGSType=' + quotedstr(Trim(YFGSType.Text)));
        sql.Add(',@ISHS=' + quotedstr(Trim(ISHS.Text)));
        sql.Add(',@dname=' + quotedstr(Trim(dname)));
        case cxTabControl1.TabIndex of
          0:
            begin
              sql.Add(',@ISDZ=''2'' ');
            end;
          1:
            begin
              sql.Add(',@ISDZ=''0'' ');
            end;
          2:
            begin
              sql.Add(',@ISDZ=''0'' ');
            end;
          3:
            begin
              sql.Add(',@ISDZ=''1'' ');
            end;
        end;
      end
      else
      begin
        if CANSHU2 <> '' then
        begin
          sql.Add(' exec  P_YSSK_List_mx_SYRNAME  ');
          sql.Add(' @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
          sql.Add(',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
          sql.Add(',@factoryName=' + quotedstr(Trim(factoryName.Text)));
          case cxTabControl1.TabIndex of
            0:
              begin
                sql.Add(',@flag=''全部'' ');
              end;
            1:
              begin
                sql.Add(',@flag=''0'' ');
              end;
            2:
              begin
                sql.Add(',@flag=''1'' ');
              end;
          end;
          sql.Add(',@YFGSType=' + quotedstr(Trim(YFGSType.Text)));
          sql.Add(',@ISHS=' + quotedstr(Trim(ISHS.Text)));
          sql.Add(',@SYRNAME=' + quotedstr(Trim(CANSHU2)));
          case cxTabControl1.TabIndex of
            0:
              begin
                sql.Add(',@ISDZ=''2'' ');
              end;
            1:
              begin
                sql.Add(',@ISDZ=''0'' ');
              end;
            2:
              begin
                sql.Add(',@ISDZ=''0'' ');
              end;
            3:
              begin
                sql.Add(',@ISDZ=''1'' ');
              end;
          end;
        end
        else
        begin
          sql.Add(' exec  P_YSSK_List_mx  ');
          sql.Add(' @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
          sql.Add(',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
          sql.Add(',@factoryName=' + quotedstr(Trim(factoryName.Text)));
          case cxTabControl1.TabIndex of
            0:
              begin
                sql.Add(',@flag=''全部'' ');
              end;
            1:
              begin
                sql.Add(',@flag=''0'' ');
              end;
            2:
              begin
                sql.Add(',@flag=''1'' ');
              end;
            3:
              begin
                sql.Add(',@flag=''1'' ');
              end;
          end;
          sql.Add(',@YFGSType=' + quotedstr(Trim(YFGSType.Text)));
          sql.Add(',@ISHS=' + quotedstr(Trim(ISHS.Text)));
          case cxTabControl1.TabIndex of
            0:
              begin
                sql.Add(',@ISDZ=''2'' ');
              end;
            1:
              begin
                sql.Add(',@ISDZ=''0'' ');
              end;
            2:
              begin
                sql.Add(',@ISDZ=''0'' ');
              end;
            3:
              begin
                sql.Add(',@ISDZ=''1'' ');
              end;
          end;
        end;

      end;


//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
    Panetime.Visible := false;
    screen.Cursor := crdefault;
  end;
end;

procedure TfrmYSMXlist.FormDestroy(Sender: TObject);
begin
  frmYSMXlist := nil;
end;

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

procedure TfrmYSMXlist.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid(self.Caption + '2', Tv1, '应收对账单');
  Close;
end;

procedure TfrmYSMXlist.FormShow(Sender: TObject);
begin
  readCxBandedGrid(self.Caption + '2', Tv1, '应收对账单');

end;

procedure TfrmYSMXlist.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmYSMXlist.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 TfrmYSMXlist.P_OrderNo_YSK(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSMXlist.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption + '-' + trim(factoryName.Text), cxgrid2);
end;

procedure TfrmYSMXlist.OrderNoChange(Sender: TObject);
begin
  if Length(Trim(OrderNo.Text)) < 3 then
    Exit;
  ToolButton2.Click;
end;

procedure TfrmYSMXlist.MPRTCodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSMXlist.FormCreate(Sender: TObject);
var
  fsj: string;
begin
  cxGrid2.Align := AlClient;
  canshu2 := DParameters2;
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;
  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应收款登记'' ';
  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);
end;

procedure TfrmYSMXlist.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
  wsql, wsql2: string;
  FMAINID: string;
begin

//  if CDS_HZ.IsEmpty then
//    exit;
//  try
//
//    filepath := ExtractFilePath(Application.ExeName) + 'report\应收款对账单.rmf';
//
//    if not FileExists(Pchar(filepath)) then
//    begin
//      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
//      exit;
//    end;
//
//    RmVariables['begDate'] := FormatDateTime('yyyy-mm-dd', begDate.Date);
//    RmVariables['enddate'] := FormatDateTime('yyyy-mm-dd', enddate.Date);
//    RmVariables['ishs'] := Trim(ishs.Text);
//    RMVariables['zdr'] := trim(DName);
//    RmVariables['begDate'] := FormatDateTime('yymmdd', begDate.Date) + '-' + FormatDateTime('yymmdd', enddate.Date);
//
//    RMGridReport1.LoadFromFile(filepath);
//   // RMGridReport1.PrintReport;
//    RMGridReport1.ShowReport;
//
//  finally
//
//  end;
  if CDS_HZ.IsEmpty then
    Exit;
  //if Trim(CDS_Main.fieldbyname('CRType').AsString)<>'检验出库' then Exit;
  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ExportFtErpFile('应收款对账单.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'report\应收款对账单.rmf';

  with CDS_HZ do
  begin

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT * FROM TBSubID where 1=2 ');
      open;
    end;
    CDS_HZ.DisableControls;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_HZ do
      begin
        First;
        while not Eof do
        begin
          if Fieldbyname('Ssel').AsBoolean then
          begin

            ADOQueryCmd.append;
            ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_HZ.fieldbyname('YFSID').AsString);
            ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
            ADOQueryCmd.post;
          end;
          Next;
        end;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
    end;
    CDS_HZ.EnableControls;
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;

      sql.Add(' exec  P_PRINT_YSSK_List_mx  ');
      sql.Add(' @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
      sql.Add(',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
      sql.Add(',@factoryName=' + quotedstr(Trim(factoryName.Text)));
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(',@flag=''全部'' ');
          end;
        1:
          begin
            sql.Add(',@flag=''已审核'' ');
          end;
      end;
      sql.Add(',@YFGSType=' + quotedstr(Trim(YFGSType.Text)));
      sql.Add(',@ISHS=' + quotedstr(Trim(ISHS.Text)));
      sql.add(',@spid=' + quotedstr(Trim(DCode)));
//      ShowMessage(SQL.Text);
      Open;
    end;
//    SCreateCDS20(ADOQueryPrint, CDS_HZ);
//    SInitCDSData20(ADOQueryPrint, CDS_HZ);

    RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
    if FileExists(fPrintFile) then
    begin
      RmVariables['GSTT'] := Trim(ComboBox1.Text);
//      RmVariables['djmoney'] := Trim(CDS_HZ.fieldbyname('djmoney').AsString);


      RM1.LoadFromFile(fPrintFile);

      RM1.ShowReport;
    end
    else
    begin
      CDS_HZ.EnableControls;
      Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\应收款对账单.rmf'), '提示', 0);
      Exit;
    end;
//    Edit;
//    FieldByName('SSel').Value := False;
//    Post;
//    end;
  end;
  CDS_HZ.EnableControls;

end;

procedure TfrmYSMXlist.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        InitGrid();
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmYSMXlist.V1Column9CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if ARow1.Values[V1Column10.Index] = ARow2.Values[V1Column10.Index] then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmYSMXlist.TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('keyNO').Index]) mod 2 = 0 then
  begin
    ACanvas.Brush.Color := $EEF5FF;
  end
  else
  begin
    ACanvas.Brush.Color := $FFF8F0;
  end;

  if AViewInfo.GridRecord.Values[TV1Column2.Index] = '1' then
  begin
    ACanvas.Font.Color := clRed;
  end;

end;

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

procedure TfrmYSMXlist.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if cxTabControl1.TabIndex = 2 then
    Exit;

//  if CDS_HZ.Locate('ssel', true, []) = false then
//  begin
//    Application.MessageBox('没有选择数据!', '提示', 0);
//    Exit;
//  end;


  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 YF_Money_CR SET status=''1'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;

end;

procedure TfrmYSMXlist.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  if cxTabControl1.TabIndex = 1 then
    Exit;
//  if CDS_HZ.Locate('ssel', true, []) = false then
//  begin
//    Application.MessageBox('没有选择数据!', '提示', 0);
//    Exit;
//  end;
  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 YF_Money_CR SET status=''0'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSMXlist.V1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if CDS_HZ.FieldByName('crtype').AsString = '应收款登记' then
  begin
    mvalue := TcxTextEdit(Sender).EditingText;
    FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := mvalue;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update YF_Money_CR_Sub set  ' + FFieldName + '=' + (Trim(mvalue)));
      sql.Add('where YFSID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));

      sql.Add('update YF_Money_CR_Sub set  ');
      sql.Add('MXMONEY=ISNULL(SHANGJIFEI,0)+ISNULL(XIAOGANGFEI,0)+ISNULL(PRICE,0)*QTY-ISNULL(YSK,0)-ISNULL(KKMONEY,0)');
      sql.Add('where YFSID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));

      sql.Add('update YF_Money_CR set  ');
      sql.Add('MONEY=(SELECT SUM(MXMONEY) FROM YF_Money_CR_Sub A WHERE A.YFID=YF_Money_CR.YFID GROUP BY YFID) ');
      sql.Add(',BBMONEY=HUILV*(SELECT SUM(MXMONEY) FROM YF_Money_CR_Sub A WHERE A.YFID=YF_Money_CR.YFID GROUP BY YFID) ');
      sql.Add('where YFID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
      ExecSQL;
    end;
    tv1.Controller.EditingController.ShowEdit();
    InitGrid();
  end;
end;

procedure TfrmYSMXlist.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  with CDS_HZ do
  begin
    First;
    while not Eof do
    begin
      if CDS_HZ.fieldbyname('status').AsString <> '1' then
      begin
        Application.MessageBox('请先对账!', '提示', 0);
        Exit;
      end;
      Next;
    end;
  end;
  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  MovePanel1.Visible := True;
  dzdate.DateTime := Now;

end;

procedure TfrmYSMXlist.ToolButton7Click(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 YF_Money_CR_SUB SET DZFLAG=NULL,DZDATE=NULL ');
            sql.Add('where YFSID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFSID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSMXlist.ToolButton8Click(Sender: TObject);
var
  F1, F2, F3, F4: string;
  S1: Double;
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  S1 := 0;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if (Fieldbyname('ssel').AsBoolean) and (CDS_HZ.Fieldbyname('CRType').ASSTRING = '应收款登记') then
        begin
          F1 := factoryName.Text;
          S1 := CDS_HZ.Fieldbyname('YMxMoney').ASFLOAT + S1;
          F3 := F3 + '/' + TRIM(CDS_HZ.Fieldbyname('YFSID').ASSTRING);
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;

  end;
  if S1 <> 0 then
  begin
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add(' exec  P_Do_SCFK  ');
      sql.Add(' @DNAME=' + quotedstr(Trim(DName)));
      sql.Add(',@MONEY=' + quotedstr(Trim(FloatToStr(S1))));
      sql.Add(' ,@DGCM=' + quotedstr(Trim(factoryName.Text)));
      sql.Add(',@FROMYFSID=' + quotedstr(Trim(F3)));
//      ShowMessage(SQL.Text);
      execsql;
    end;
    InitGrid();
  end;

end;

procedure TfrmYSMXlist.V1Column11PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if CDS_HZ.FieldByName('crtype').AsString = '收款登记' then
  begin
    mvalue := TcxTextEdit(Sender).EditingText;
    FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := mvalue;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update YF_Money_CR_Sub set  ' + FFieldName + '=' + (Trim(mvalue)));
      sql.Add('where YFSID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));

      sql.Add('update YF_Money_CR set  ');
      sql.Add('MONEY=(SELECT SUM(MXMONEY) FROM YF_Money_CR_Sub A WHERE A.YFID=YF_Money_CR.YFID GROUP BY YFID) ');
      sql.Add(',BBMONEY=HUILV*(SELECT SUM(MXMONEY) FROM YF_Money_CR_Sub A WHERE A.YFID=YF_Money_CR.YFID GROUP BY YFID) ');
      sql.Add('where YFID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
      ExecSQL;
    end;
    tv1.Controller.EditingController.ShowEdit();
    InitGrid();
  end;
end;

procedure TfrmYSMXlist.V1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if CDS_HZ.FieldByName('crtype').AsString = '收款登记' then
  begin
    mvalue := TcxTextEdit(Sender).EditingText;
    FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := mvalue;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update YF_Money_CR set  ' + FFieldName + '=' + QuotedStr(Trim(mvalue)));
      sql.Add('where YFID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));

      ExecSQL;
    end;
    tv1.Controller.EditingController.ShowEdit();
    InitGrid();
  end;
end;

procedure TfrmYSMXlist.Button1Click(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  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 YF_Money_CR_SUB SET DZFLAG=''1'' ');
            sql.Add(',dzdate=' + quotedstr(FormatDateTime('yyyy-MM-dd', dzdate.DateTime)));
            sql.Add('where YFSID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFSID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
    MovePanel1.Visible := False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSMXlist.Button2Click(Sender: TObject);
begin
  MovePanel1.Visible := False;
end;

procedure TfrmYSMXlist.MXORDERNOChange(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 TfrmYSMXlist.V1Column13PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if CDS_HZ.FieldByName('crtype').AsString = '应收款登记' then
  begin
    mvalue := TcxTextEdit(Sender).EditingText;
    FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := mvalue;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update YF_Money_CR_Sub set  MXMONEY=' + (Trim(mvalue)));
      sql.Add('where YFSID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));

      sql.Add('update YF_Money_CR set  ');
      sql.Add('MONEY=(SELECT SUM(MXMONEY) FROM YF_Money_CR_Sub A WHERE A.YFID=YF_Money_CR.YFID GROUP BY YFID) ');
      sql.Add(',BBMONEY=HUILV*(SELECT SUM(MXMONEY) FROM YF_Money_CR_Sub A WHERE A.YFID=YF_Money_CR.YFID GROUP BY YFID) ');
      sql.Add('where YFID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
      ExecSQL;
    end;
    tv1.Controller.EditingController.ShowEdit();
    InitGrid();
  end;
end;

procedure TfrmYSMXlist.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmYSMXlist.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, False);
end;

procedure TfrmYSMXlist.MXORDERNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select top 1 * from jyorder_main A WHERE A.ORDERNO LIKE ' + QuotedStr('%' + trim(mxorderno.Text) + '%'));
      open;
    end;
    if ADOQueryTemp.IsEmpty = false then
    begin
      factoryName.Text := ADOQueryTemp.fieldbyname('CustomerNoName').AsString;
      InitGrid();
      ToolButton2.Click();
    end;
  end;
end;

end.

