unit U_YSMXlistMonDayDZEY;

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, BtnEdit, cxPC, Menus, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmYSMXlistMonDayDZEY = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxCRTime: TcxGridDBColumn;
    cxMoneyYe: TcxGridDBColumn;
    cxQty: TcxGridDBColumn;
    cxQtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Price: TcxGridDBColumn;
    VMoney: TcxGridDBColumn;
    v2BBMoney: TcxGridDBColumn;
    v2MPRTCodeName: TcxGridDBColumn;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    v2gangNo: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2note: TcxGridDBColumn;
    Panetime: TPanel;
    v2Column2: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    Panel2: TPanel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Panel3: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    FBegDate: TDateTimePicker;
    FEndDate: TDateTimePicker;
    Panel4: TPanel;
    Label5: TLabel;
    factoryName: TBtnEditA;
    RadioGroup1: TRadioGroup;
    PopupMenu1: TPopupMenu;
    N6: TMenuItem;
    N7: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    v2Column8: TcxGridDBColumn;
    Panel7: TPanel;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    ToolButton2: TToolButton;
    PopupMenu2: TPopupMenu;
    Panel9: TPanel;
    LabBZ: TLabel;
    LabGYS: TLabel;
    Label22: TLabel;
    P_CodeName: TEdit;
    Label6: TLabel;
    P_Color: TEdit;
    Label7: TLabel;
    P_HX: TEdit;
    v2Column12: TcxGridDBColumn;
    Label10: TLabel;
    GYName: TEdit;
    Panel5: TPanel;
    Label15: TLabel;
    Label16: TLabel;
    Panel6: TPanel;
    Label8: TLabel;
    Label9: TLabel;
    Label11: TLabel;
    ConNo: TEdit;
    v2Column13: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure v2Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToFactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure cxQtyPropertiesEditValueChanged(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure N3Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N9Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v2notePropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    FFEndDate, FFBegDate: TDateTime;
    FLR: string;
    procedure InitGrid();
  public
    { Public declarations }
    RKFlag, FCYID: string;
    fType, FMX, FBZ: string;
    PState: Integer;
  end;

var
  frmYSMXlistMonDayDZEY: TfrmYSMXlistMonDayDZEY;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_RTFun, U_ZdyAttachGYS, U_KHListSelJJ,
  U_KHSKInPut, U_KHSKListView;

{$R *.dfm}

procedure TfrmYSMXlistMonDayDZEY.InitGrid();
var
  FYue, FDate: string;
  FInt: Integer;
begin
  if RadioGroup1.ItemIndex = 0 then
  begin
    if cxTabControl1.TabIndex < 11 then
    begin
      FInt := cxTabControl1.TabIndex + 2;
    end
    else
    begin
      FInt := 1;
    end;
    FYue := Trim(IntToStr(FInt));
    if FInt < 10 then
    begin
      FYue := '0' + Trim(IntToStr(FInt));
    end;
    if FInt <> 1 then
    begin
      FDate := Trim(FormatDateTime('yyyy', begdate.Date)) + '-' + FYue;

    end
    else
    begin
      FDate := Trim(FormatDateTime('yyyy', begdate.Date));
      FDate := IntToStr(strtoint(FDate) + 1);
      FDate := FDate + '-' + FYue;

    end;

    if FMX = '' then
    begin
      FBegDate.Date := StrToDate(FDate + '-01');

      if FInt = 12 then
        FEndDate.Date := StrToDate(Trim(FormatDateTime('yyyy', begdate.Date)) + '-12-31')
      else
      begin
        if FInt = 1 then
        begin
          FEndDate.Date := StrToDate(FDate + '-31');
        end
        else
        begin
          FEndDate.Date := StrToDate(Trim(FormatDateTime('yyyy', begdate.Date)) + '-' + Trim(IntToStr(FInt + 1)) + '-01') - 1;
        end;
      end;
    end;
  end;
//  ShowMessage('3');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if RadioGroup1.ItemIndex = 0 then
    begin
//      sql.Add('exec  P_YSSK_Month_BZ_HJ :FDate,:BZ,:HuiLv,:factoryNo');
//      Parameters.ParamByName('FDate').Value := Trim(FDate);
//      Parameters.ParamByName('factoryNo').Value := Trim(factoryName.TxtCode);
//      Parameters.ParamByName('BZ').Value := Trim(FBZ);
//      Parameters.ParamByName('HuiLv').Value := 1;
      sql.Add('exec  P_YSSK_Month_BZ_HJ ');
      sql.Add('@FDate=''' + Trim(FDate) + '''');
      sql.Add(',@BZType=''' + Trim(FBZ) + '''');
      sql.Add(',@HuiLv=''1''');
      sql.Add(',@factoryNo=''' + Trim(factoryName.TxtCode) + '''');
//      ShowMessage(sql.text);
    end
    else if RadioGroup1.ItemIndex = 1 then
    begin
//      sql.Add('exec  P_YSSK_Day_BZ_HJ :FBegDate,:FEndDate,:BZ,:HuiLv,:factoryNo');
//      Parameters.ParamByName('FBegdate').Value := Trim(FormatDateTime('yyyy-MM-dd', FBegDate.Date));
//      Parameters.ParamByName('FEndDate').Value := Trim(FormatDateTime('yyyy-MM-dd', FEndDate.Date + 1));
//      Parameters.ParamByName('factoryNo').Value := Trim(factoryName.TxtCode);
//      Parameters.ParamByName('BZ').Value := Trim(FBZ);
//      Parameters.ParamByName('HuiLv').Value := 1;
      sql.Add('exec  P_YSSK_Day_BZ_HJ ');
      sql.Add('@FBegdate=''' + Trim(FormatDateTime('yyyy-MM-dd', FBegDate.Date)) + '''');
      sql.Add(',@FEndDate=''' + Trim(FormatDateTime('yyyy-MM-dd', FEndDate.Date + 1)) + '''');
      sql.Add(',@BZType=''' + Trim(FBZ) + '''');
      sql.Add(',@HuiLv=''1''');
      sql.Add(',@factoryNo=''' + Trim(factoryName.TxtCode) + '''');
//      ShowMessage(sql.text);
    end;
    Open;
  end;
//  ShowMessage('4');      
 // SCSHDataNew(ADOQueryTemp,GroupBox1,2);
 // SCSHDataNew(ADOQueryTemp,GroupBox2,2);
 // SCSHDataNew(ADOQueryTemp,GroupBox3,2);
  //SCSHDataNew(ADOQueryTemp,GroupBox4,2);
  LabGYS.Caption := Trim(factoryName.Text) + '→ 总销售额：' + Trim(ADOQueryTemp.fieldbyname('YFMoneyLJ').AsString) + ' 今年销售额：' + Trim(ADOQueryTemp.fieldbyname('YFMoneyYear').AsString) + '  欠款系数：' + Trim(ADOQueryTemp.fieldbyname('QKXS').AsString) + '  总应收款：' + Trim(ADOQueryTemp.fieldbyname('HZMoney').AsString);

  //LabYFMoneyYear.Caption:='年销售额：'+Trim(ADOQueryTemp.fieldbyname('YFMoneyYear').AsString)
                     //  +'  欠款系数：'+Trim(ADOQueryTemp.fieldbyname('QKXS').AsString)+'%';
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      if RadioGroup1.ItemIndex = 0 then
      begin
//        sql.Add(' exec  P_Qry_YSk_Month :FDate,:factoryNo,:BZ');
//        Parameters.ParamByName('FDate').Value := Trim(FDate);
//        Parameters.ParamByName('factoryNo').Value := Trim(factoryName.TxtCode);
//        Parameters.ParamByName('BZ').Value := Trim(FBZ);
        sql.Add(' exec  P_Qry_YSk_Month ');
        sql.Add('@FDate=''' + Trim(FDate) + '''');
        sql.Add(',@BZ=''' + Trim(FBZ) + '''');
        sql.Add(',@factoryNo=''' + Trim(factoryName.TxtCode) + '''');
//        ShowMessage(sql.text);

      end
      else if RadioGroup1.ItemIndex = 1 then
      begin
//        sql.Add(' exec  P_Qry_YSk_Day :FBegDate,:FEndDate,:factoryNo,:BZ');
//        Parameters.ParamByName('FBegdate').Value := Trim(FormatDateTime('yyyy-MM-dd', FBegDate.Date));
//        Parameters.ParamByName('FEndDate').Value := Trim(FormatDateTime('yyyy-MM-dd', FEndDate.Date + 1));
//        Parameters.ParamByName('factoryNo').Value := Trim(factoryName.TxtCode);
//        Parameters.ParamByName('BZ').Value := Trim(FBZ);

        sql.Add(' exec  P_Qry_YSk_Day ');
        sql.Add('@FBegdate=''' + Trim(FormatDateTime('yyyy-MM-dd', FBegDate.Date)) + '''');
        sql.Add(',@FEndDate=''' + Trim(FormatDateTime('yyyy-MM-dd', FEndDate.Date + 1)) + '''');
        sql.Add(',@BZ=''' + Trim(FBZ) + '''');
        sql.Add(',@factoryNo=''' + Trim(factoryName.TxtCode) + '''');       
//        ShowMessage(sql.text);
      end;
      Open;
    end;
//    ShowMessage('5'); 
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);      
//    ShowMessage('6');
  finally
    ADOQueryMain.EnableControls;
    Panetime.Visible := false;
    screen.Cursor := crdefault;
  end;
//  ShowMessage('7');
end;

procedure TfrmYSMXlistMonDayDZEY.FormDestroy(Sender: TObject);
begin
  frmYSMXlistMonDayDZEY := nil;
end;

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

procedure TfrmYSMXlistMonDayDZEY.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收款对账单YX11', Tv2, '财务管理');
  Close;
end;

procedure TfrmYSMXlistMonDayDZEY.FormShow(Sender: TObject);
var
  fstr: string;
begin
  ReadCxGrid('应收款对账单YX11', Tv2, '财务管理');
  //cxMoneyYe.Visible:=False;
  LabBZ.Caption := Trim(FBZ);
  if Trim(FMX) = '' then
  begin
    begdate.DateTime := SGetServerDateMBeg(ADOQueryTemp);
    fstr := Trim(FormatDateTime('MM', begdate.Date));
    cxTabControl1.TabIndex := StrToInt(fstr) - 1;
    ShowMessage('2');
    Panel3.Visible := False;
  end
  else
  begin
    RadioGroup1.ItemIndex := PState;
    if RadioGroup1.ItemIndex = 0 then
    begin
      Panel2.Visible := True;
      Panel3.Visible := False;
      cxTabControl1.Visible := True;
    end
    else if RadioGroup1.ItemIndex = 1 then
    begin
      Panel2.Visible := False;
      Panel3.Visible := True;
      cxTabControl1.Visible := False;
    end;
    InitGrid();
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  cxTabControl1.Tabs[11].Caption := '  1月（' + inttostr(strtoint(FormatDateTime('yyyy', begdate.Date)) + 1) + '年)';
  InitGrid();
end;

procedure TfrmYSMXlistMonDayDZEY.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, self.Caption);
end;

procedure TfrmYSMXlistMonDayDZEY.FormCreate(Sender: TObject);
begin
  cxGrid2.Align := AlClient;
end;

procedure TfrmYSMXlistMonDayDZEY.ToolButton3Click(Sender: TObject);
var
  filepath, fsj: string;
  mBegdate, mEnddate, FFHTT: string;
begin

  FFHTT := Trim(CDS_HZ.fieldbyname('FHTT').AsString);
  fsj := cxTabControl1.Tabs[cxTabControl1.TabIndex].Caption;
  mBegdate := FormatDateTime('yyyy', begdate.Date) + '年' + Trim(Copy(fsj, 3, Length(fsj) - 2)) + '份';
  if CDS_HZ.IsEmpty then
    exit;
  try
    filepath := ExtractFilePath(Application.ExeName) + 'report\应收款对账单YX.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RMVariables['dtxz'] := FormatDateTime('yyyy-mm-dd', date());
    RMVariables['zdr'] := trim(DName);
    RmVariables['begDate'] := mBegdate;
    RmVariables['endNum'] := CDS_HZ.fieldbyname('MoneyYe').AsString;
    RmVariables['FHTT1'] := FFHTT;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;

  finally

  end;

end;

procedure TfrmYSMXlistMonDayDZEY.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmKHListSelJJ := TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2 := '高权限';
      if ShowModal = 1 then
      begin
        Self.factoryName.Text := Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
        Self.factoryName.TxtCode := Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
      end;
    end;
  finally
    frmKHListSelJJ.Free;
  end;
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmYSMXlistMonDayDZEY.v2Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: Boolean;
begin
  mvalue := TcxCheckBox(Sender).EditValue;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('DZFlag').Value := mvalue;
  end;
  if mvalue = True then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set DZFlag=1,DZTime=getdate(),DZPerson=''' + Trim(DName) + '''');
      sql.Add(' where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      ExecSQL;
    end;
  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set DZFlag=0,DZTime=getdate(),DZPerson=''' + Trim(DName) + '''');
      sql.Add(' where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      ExecSQL;
    end;
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.v2Column3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id: Integer;
begin
  id := TV2.GetColumnByFieldName('DZFlag').Index; //;-TV1.GroupedItemCount;
  if id < 0 then
    Exit;
  if AViewInfo.GridRecord.Values[id] = True then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.ToFactoryNameChange(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 TfrmYSMXlistMonDayDZEY.cxTabControl1Change(Sender: TObject);
begin

  if FLR = '99' then
  begin
    Panel4.SetFocus;
  end;
  InitGrid();
end;

procedure TfrmYSMXlistMonDayDZEY.RadioGroup1Click(Sender: TObject);
begin
  if RadioGroup1.ItemIndex = 0 then
  begin
    Panel2.Visible := True;
    Panel3.Visible := False;
    cxTabControl1.Visible := True;
  end
  else if RadioGroup1.ItemIndex = 1 then
  begin
    Panel2.Visible := False;
    Panel3.Visible := True;
    cxTabControl1.Visible := False;
  end;
  InitGrid();
end;

procedure TfrmYSMXlistMonDayDZEY.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmYSMXlistMonDayDZEY.N7Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, False);
end;

procedure TfrmYSMXlistMonDayDZEY.N4Click(Sender: TObject);
var
  FDate: TDateTime;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ToolBar1.SetFocus;
  try
    Panel2.Visible := True;
    Panel2.Refresh;
    FDate := SGetServerDateTime(ADOQueryTemp);
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while CDS_HZ.Locate('SSel', True, []) do
      begin
        if Trim(CDS_HZ.fieldbyname('ChkStatusP').AsString) <> '已审核' then
        begin
          if Trim(CDS_HZ.fieldbyname('YFID').AsString) <> '' then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from YS_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
              Open;
            end;
            with ADOQueryCmd do
            begin
              Edit;
              FieldByName('ChkerP').Value := Trim(DName);
              FieldByName('ChkTimeP').Value := FDate;
              FieldByName('ChkStatusP').Value := '已审核';
              FieldByName('ChkerQ').Value := Trim(DName);
              FieldByName('ChkTimeQ').Value := FDate;
              FieldByName('ChkStatusQ').Value := '已审核';
              if Trim(CDS_HZ.fieldbyname('CRType').AsString) = '应收款登记' then
              begin
                FieldByName('Money').Value := CDS_HZ.fieldbyname('YFMoney').Value;
                RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_HZ, 'YS_Money_CR', 7);
              end;

              Post;
            end;
            if Trim(CDS_HZ.fieldbyname('CRType').AsString) = '应收款登记' then
            begin
              with ADOQueryCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('Update YS_Money_CR Set ');
                sql.Add('HSMoney=Money');
                sql.Add(',JSMoney=Money');
                sql.Add(' where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
                ExecSQL;
              end;
            end;
            with CDS_HZ do
            begin
              Edit;
              FieldByName('ChkStatusP').Value := '已审核';
              FieldByName('SSel').Value := False;
              Post;
            end;
          end
          else
          begin
            with CDS_HZ do
            begin
              Edit;
              FieldByName('SSel').Value := False;
              Post;
            end;
          end;

        end
        else
        begin
          with CDS_HZ do
          begin
            Edit;
            FieldByName('SSel').Value := False;
            Post;
          end;
        end;
      end;
    end;
    CDS_HZ.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
    Panel2.Visible := False;
  except
    Panel2.Visible := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!', '提示', 0);
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.N5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ToolBar1.SetFocus;
  try
    Panel2.Visible := True;
    Panel2.Refresh;
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while CDS_HZ.Locate('SSel', True, []) do
      begin
        if Trim(CDS_HZ.fieldbyname('ChkStatusP').AsString) <> '未审核' then
        begin
          if Trim(CDS_HZ.fieldbyname('YFID').AsString) <> '' then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from YS_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
              Open;
            end;
            with ADOQueryCmd do
            begin
              Edit;
              FieldByName('ChkerP').Value := Null;
              FieldByName('ChkTimeP').Value := Null;
              FieldByName('ChkStatusP').Value := '未审核';
              FieldByName('ChkerQ').Value := Null;
              FieldByName('ChkTimeQ').Value := Null;
              FieldByName('ChkStatusQ').Value := '未审核';
              Post;
            end;
            {with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update YS_Money_CR Set ');
              sql.Add('HSMoney=Null');
              sql.Add(',JSMoney=Null');
              sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
              ExecSQL;
            end;}
            with CDS_HZ do
            begin
              Edit;
              FieldByName('ChkStatusP').Value := '未审核';
             { FieldByName('ChkerP').Value:=Null;
              FieldByName('ChkTimeP').Value:=Null;
              FieldByName('ChkStatusQ').Value:='未审核';
              FieldByName('ChkerQ').Value:=Null;
              FieldByName('ChkTimeQ').Value:=Null;}
              FieldByName('SSel').Value := False;
              Post;
            end;
          end
          else
          begin
            with CDS_HZ do
            begin
              Edit;
              FieldByName('SSel').Value := False;
              Post;
            end;
          end;
        end
        else
        begin
          with CDS_HZ do
          begin
            Edit;
            FieldByName('SSel').Value := False;
            Post;
          end;
        end;

      end;
    end;
    CDS_HZ.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
    Panel2.Visible := False;
  except
    Panel2.Visible := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!', '提示', 0);
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.cxQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FQty, FPrice, FMoney, FShuiDian, FHSPrice, FHSMoney, FJSMoney, FHZFee, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    if Trim(mvalue) <> '' then
      FieldByName(FFieldName).Value := Trim(mvalue)
    else
      FieldByName(FFieldName).Value := Null;
    Post;
  end;

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

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

  FHZFee := Trim(CDS_HZ.fieldbyname('HZFee').AsString);
  if Trim(FHZFee) = '' then
    FHZFee := '0';

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

  if Trim(FFieldName) <> 'YFMoney' then
  begin
    FMoney := FloatToStr(StrToFloat(FQty) * StrToFloat(FPrice) + StrToFloat(FHZFee));
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('YFMoney').Value := StrToFloat(FMoney);
    Post;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYSMXlistMonDayDZEY.N1Click(Sender: TObject);
var
  FDate: TDateTime;
  DWYFID: string;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Trim(CDS_HZ.fieldbyname('YFID').AsString) = '' then
    Exit;
  ToolBar1.SetFocus;
  DWYFID := Trim(CDS_HZ.fieldbyname('YFID').AsString);
  try
    FDate := SGetServerDateTime(ADOQueryTemp);
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      FieldByName('ChkerP').Value := Trim(DName);
      FieldByName('ChkTimeP').Value := FDate;
      FieldByName('ChkStatusP').Value := '已审核';
      FieldByName('ChkerQ').Value := Trim(DName);
      FieldByName('ChkTimeQ').Value := FDate;
      FieldByName('ChkStatusQ').Value := '已审核';
      if Trim(CDS_HZ.fieldbyname('CRType').AsString) = '应收款登记' then
      begin
        FieldByName('Money').Value := CDS_HZ.fieldbyname('YFMoney').Value;
        RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_HZ, 'YS_Money_CR', 7);
      end;
      Post;
    end;
    if Trim(CDS_HZ.fieldbyname('CRType').AsString) = '应收款登记' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CR Set ');
        sql.Add('HSMoney=Money');
        sql.Add(',JSMoney=Money');
        sql.Add(' where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
        ExecSQL;
      end;
    end;

    with CDS_HZ do
    begin
      Edit;
      FieldByName('ChkStatusP').Value := '已审核';
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
    CDS_HZ.Locate('YFID', DWYFID, []);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!', '提示', 0);
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.N2Click(Sender: TObject);
var
  DWYFID: string;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Trim(CDS_HZ.fieldbyname('YFID').AsString) = '' then
    Exit;
  ToolBar1.SetFocus;
  DWYFID := Trim(CDS_HZ.fieldbyname('YFID').AsString);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      FieldByName('ChkerP').Value := Null;
      FieldByName('ChkTimeP').Value := Null;
      FieldByName('ChkStatusP').Value := '未审核';
      FieldByName('ChkerQ').Value := Null;
      FieldByName('ChkTimeQ').Value := Null;
      FieldByName('ChkStatusQ').Value := '未审核';
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('ChkStatusP').Value := '未审核';
      {FieldByName('ChkStatusQ').Value:='未审核';
      FieldByName('ChkerQ').Value:=Null;
      FieldByName('ChkTimeQ').Value:=Null;
      FieldByName('ChkerP').Value:=Null;
      FieldByName('ChkTimeP').Value:=Null;}
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
    CDS_HZ.Locate('YFID', DWYFID, []);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!', '提示', 0);
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id: Integer;
begin
  id := Tv2.GetColumnByFieldName('ChkStatusP').Index;
  if ARecord.Values[id] = '已审核' then
    exit;
  AStyle := DataLink_Money.SkyBlue;
end;

procedure TfrmYSMXlistMonDayDZEY.N3Click(Sender: TObject);
var
  DWYFID: string;
begin
  try
    frmKHSKInPut := TfrmKHSKInPut.Create(Application);
    with frmKHSKInPut do
    begin
      FMainId := '';
      frmKHSKInPut.FactoryName.Text := Self.factoryName.Text;
      frmKHSKInPut.FactoryName.TxtCode := Self.factoryName.TxtCode;
      frmKHSKInPut.BZType.ItemIndex := frmKHSKInPut.BZType.Items.IndexOf(Self.LabBZ.Caption);
      if ShowModal = 1 then
      begin
        DWYFID := Trim(frmKHSKInPut.FMainId);
        Self.TBRafresh.Click;
        Self.CDS_HZ.Locate('YFID', DWYFID, []);
      end;
    end;
  finally
    frmKHSKInPut.Free;
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.N8Click(Sender: TObject);
var
  DWYFID: string;
begin
  if Trim(CDS_HZ.fieldbyname('CRType').AsString) <> '收款登记' then
    Exit;
  DWYFID := Trim(CDS_HZ.fieldbyname('YFID').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFID=''' + Trim(DWYFID) + ''' and  isnull(ChkerP,'''')<>'''' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('数据已审核不能修改!', '提示', 0);
    Exit;
  end;
  try
    frmKHSKInPut := TfrmKHSKInPut.Create(Application);
    with frmKHSKInPut do
    begin
      FMainId := Trim(Self.CDS_HZ.fieldbyname('YFID').AsString);
      if ShowModal = 1 then
      begin
        Self.TBRafresh.Click;
        Self.CDS_HZ.Locate('YFID', DWYFID, []);
      end;
    end;
  finally
    frmKHSKInPut.Free;
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.N9Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Trim(CDS_HZ.fieldbyname('CRType').AsString) <> '收款登记' then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFID=''' + Trim(Self.CDS_HZ.fieldbyname('YFID').AsString) + ''' and  isnull(ChkerP,'''')<>'''' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('数据已审核不能删除!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' insert into YS_Money_CR_DelLog ');
      sql.Add(' select * from YS_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      sql.Add(' Update YS_Money_CR_DelLog Set DelTime=getdate(),DelPerson=''' + Trim(DName) + '''');
      sql.Add(' where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      sql.Add(' delete YS_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.Delete;
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败!', '提示', 0);
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.ToolButton2Click(Sender: TObject);
var
  FName, FDate, FYue: string;
  FInt, FBYear, FEYear: Integer;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  begin
    FInt := StrToInt(FormatDateTime('yyyy', begdate.Date));
    FBYear := FInt;
    FEYear := FInt + 1;
    FFBegDate := StrToDate(IntToStr(FBYear) + '-02-01');
    FFEndDate := StrToDate(IntToStr(FEYear) + '-01-31');
    try
      frmKHSKListView := TfrmKHSKListView.Create(Application);
      with frmKHSKListView do
      begin
        FKHNO := Trim(factoryName.TxtCode);
        frmKHSKListView.begdate.Date := Self.FFBegDate;
        frmKHSKListView.Enddate.Date := Self.FFEndDate;
        FactoryNameHZ.Text := Trim(factoryName.Text);
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmKHSKListView.Free;
    end;
  end;

end;

procedure TfrmYSMXlistMonDayDZEY.P_CodeNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel4, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_HZ);
  SInitCDSData20(ADOQueryMain, CDS_HZ);
end;

procedure TfrmYSMXlistMonDayDZEY.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if cxGrid2.PopupMenu = PopupMenu1 then
  begin
    v2note.Options.Focusing := True;
    v2note.Options.Editing := True;
  end
  else
  begin
    v2note.Options.Focusing := False;
    v2note.Options.Editing := False;
  end;
end;

procedure TfrmYSMXlistMonDayDZEY.v2notePropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  ToolBar1.SetFocus;
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '';
  end;
  with CDS_HZ do
  begin
    edit;
    FieldByName('note').Value := mvalue;
  end;
  FLR := '99';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_Money_CR Set Note=''' + Trim(mvalue) + '''');
    sql.Add(' where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
    ExecSQL;
  end;
end;

end.

