unit U_YFCRMXchildList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxDropDownEdit, Menus, cxLookAndFeels, cxLookAndFeelPainters,
  dxBarBuiltInMenu, cxNavigator;

type
  TfrmYFCRMXchildList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    MXOrderNo: TEdit;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column6: TcxGridDBColumn;
    Label4: TLabel;
    FACTTYPE: TComboBox;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    chk1: TCheckBox;
    v1Column16: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label5: TLabel;
    FactoryName: TEdit;
    lbl1: TLabel;
    lbl2: TLabel;
    CPName: TEdit;
    CPCode: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    Panel2: TPanel;
    Label7: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    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 MXOrderNoChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure FACTTYPEChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure chk1Click(Sender: TObject);
    procedure FACTTYPEDropDown(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v1PricePropertiesEditValueChanged(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure v1notePropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public

    { Public declarations }
    FCYID, fmanage: string;
    FCRTime, FFactoryName, FKXType, FMXOrderNo, FCPCode, FCPName, FCPSpec, Fqtyunit: string;
  end;

var
  frmYFCRMXchildList: TfrmYFCRMXchildList;

implementation

uses
  U_DataLink, U_Fun, U_SXCKNewList, U_YSSCList, U_YsScXsList, U_YSKInput,
  U_XXFPHXList;

{$R *.dfm}
procedure TfrmYFCRMXchildList.SetStatus();
var
  i: integer;
begin
  Tchk.Visible := false;
  TNochk.Visible := false;
//  TBDel.Visible := false;

  case cxTabControl1.TabIndex of
    0:
      begin
        Tchk.Visible := true;
//        TBDel.Visible := true;
      end;
    1:
      begin
        TNochk.Visible := true;
      end;
  end;
end;

procedure TfrmYFCRMXchildList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.*,b.qtyunit as qtyunit1,FACTtYPE=(SELECT TOP 1 FACTTYPE FROM Company  C WHERE A.FactoryName=C.CoName) from YF_Money_CR A');
      sql.Add(' inner join  YF_Money_CR_SUB B on A.yfid=B.yfid ');
//      sql.Add(' where A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
//      sql.Add(' and CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      sql.Add(' where A.CRType=''应付款登记''  ');
      if chk1.Checked then
      begin
        sql.Add(' and isnull(B.FPHX,0)=0  ');
      end;
      if cxTabControl1.TabIndex < 2 then
        sql.Add(' and A.status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
      sql.Add(' and CRTime=''' + FCRTime + ''' ');
      sql.Add(' and isnull(A.FactoryName,'''')=' + Quotedstr(Trim(FFactoryName)));
      sql.Add(' and isnull(B.KXType,'''')=' + Quotedstr(Trim(FKXType)));
      sql.Add(' and isnull(B.MXOrderNo,'''')=' + Quotedstr(Trim(FMXOrderNo)));
      sql.Add(' and isnull(B.CPCode,'''')=' + Quotedstr(Trim(FCPCode)));
      sql.Add(' and isnull(B.CPName,'''')=' + Quotedstr(Trim(FCPName)));
      sql.Add(' and isnull(B.CPSpec,'''')=' + Quotedstr(Trim(FCPSpec)));
      sql.Add(' and isnull(B.qtyunit,'''')=' + Quotedstr(Trim(Fqtyunit)));

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_hz);
    SInitCDSData20(ADOQueryMain, CDS_hz);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmYFCRMXchildList.FormDestroy(Sender: TObject);
begin
  frmYFCRMXchildList := nil;
end;

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

procedure TfrmYFCRMXchildList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');

  Close;
end;

procedure TfrmYFCRMXchildList.FormShow(Sender: TObject);
var
  fsj: string;
begin


//  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应付款登记'' ';
//  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);

  SetStatus();
  InitGrid();
end;

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

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

procedure TfrmYFCRMXchildList.Button2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('SubID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YCL_SG_HZ where SubID=''' + Trim(CDS_HZ.fieldbyname('SubID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmYFCRMXchildList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '应收款列表');
end;

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

procedure TfrmYFCRMXchildList.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := TRUE;
      post;
      next;
    end;
    First;
    EnableControls;
  end;

end;

procedure TfrmYFCRMXchildList.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := FALSE;
      post;
      next;
    end;
    First;
    EnableControls;
  end;

end;

procedure TfrmYFCRMXchildList.FACTTYPEChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYFCRMXchildList.Tv1DblClick(Sender: TObject);
begin
//  frmXXFPHXList := TfrmXXFPHXList.create(self);
//  with frmXXFPHXList do
//  begin
//    FTONO := TRIM(CDS_YFS.Fieldbyname('YFSID').AsString);
//    FFromNo := '';
//    if showmodal = 1 then
//    begin
//
//    end;
//    free;
//  end;
end;

procedure TfrmYFCRMXchildList.chk1Click(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmYFCRMXchildList.FACTTYPEDropDown(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct facttype from Company where CoType=''供应商''  ');
    Open;
  end;
  FACTTYPE.Items.Clear;
  while not ADOQueryTemp.eof do
  begin
    FACTTYPE.Items.Add(ADOQueryTemp.fieldbyname('facttype').AsString);
    ADOQueryTemp.next;
  end;

end;

procedure TfrmYFCRMXchildList.FormCreate(Sender: TObject);
begin
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');

  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;

end;

procedure TfrmYFCRMXchildList.v1PricePropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
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('exec P_YFFK_UP_Money @OderNo='''' , @YFID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
    ExecSQL;
  end;

  with CDS_HZ do
  begin
    Edit;
    FieldByName('MxMoney').Value := RoundFloat((FieldByName('price').AsFloat * FieldByName('qty').AsFloat + FieldByName('OrtherFee').AsFloat), 2);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmYFCRMXchildList.TchkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update 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 TfrmYFCRMXchildList.TnochkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          if Fieldbyname('HZFPHX').AsFloat > 0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            application.MessageBox('不能撤销已核销数据！', '提示信息', 0);
            exit;
          end;
          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 TfrmYFCRMXchildList.Button1Click(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  price=' + (Trim(Edit1.Text)));
            sql.Add('where YFSID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));

            sql.Add('exec P_YFFK_UP_Money @OderNo='''' , @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 TfrmYFCRMXchildList.v1notePropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
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 + '=' + QuotedStr(Trim(mvalue)));
    sql.Add('where YFSID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));


//     ShowMessage(sql.Text);
    ExecSQL;
  end;

//  with CDS_HZ do
//  begin
//    Edit;
//    FieldByName('MxMoney').Value := RoundFloat((FieldByName('price').AsFloat * FieldByName('qty').AsFloat), 2);
//    Post;
//  end;
  tv1.Controller.EditingController.ShowEdit();
end;

end.

