unit U_YSSCList;

interface

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

type
  TfrmYSSCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    FSId: TEdit;
    Label7: TLabel;
    KHConNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column16: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure FSIdKeyPress(Sender: TObject; var Key: Char);
    procedure FSIdChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    canshu1, canshu2: string;
    procedure InitGrid();
    procedure SetStatus();
    function YSData(): Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmYSSCList: TfrmYSSCList;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}
procedure TfrmYSSCList.SetStatus();
begin
  if cxTabControl1.TabIndex = 0 then
  begin
    ToolButton1.Visible := True;
  end
  else
  begin
    ToolButton1.Visible := False;
  end;
end;

function TfrmYSSCList.YSData(): 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 FactoryName=' + quotedstr(Trim(CDS_Main.fieldbyname('factoryName').AsString)));
    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_CR2ID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CR2ID ');
      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(CDS_Main.fieldbyname('factoryName').AsString);
      FieldByName('ZdyStr1').Value := '应收收';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR2 where FactoryName=' + quotedstr(Trim(CDS_Main.fieldbyname('factoryName').AsString)));
    sql.Add(' and YFTypeID=''' + Trim(CDS_Main.fieldbyname('FSId').AsString) + '''');
    sql.Add(' and MainId=''' + Trim(CDS_Main.fieldbyname('MainId').AsString) + '''');
    sql.Add(' and YFName=''' + Trim(CDS_Main.fieldbyname('YFName').AsString) + '''');
    SQL.Add(' and CRType=''应收款登记''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'CS', 'YF_Money_CR2', 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_CR2 where YFID=''' + Trim(YFID) + '''');
    Open;
  end;
  with ADOQueryCmd do
  begin
//    if ADOQueryTemp.IsEmpty then
//
//    else
    Append;
    FieldByName('YFID').Value := Trim(YFID);
    FieldByName('YFTypeId').Value := Trim(CDS_Main.fieldbyname('FSId').AsString);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应收款登记';
    FieldByName('CRFlag').Value := '应收收';
    FieldByName('QtyFlag').Value := 1;
    FieldByName('WFGYSName').Value := Trim(CDS_Main.fieldbyname('WFGYSName').AsString);
    FieldByName('FactoryName').Value := Trim(CDS_Main.fieldbyname('factoryName').AsString);
    FieldByName('CRTime').Value := CDS_Main.fieldbyname('FHDate').AsDateTime;
    FieldByName('Qty').Value := CDS_Main.fieldbyname('HZQty').AsFloat;
    FieldByName('PS').Value := CDS_Main.fieldbyname('HZPS').AsFloat;
    FieldByName('YFType').Value := '自动生成';
    FieldByName('ConPrice').Value := CDS_Main.fieldbyname('PRTPrice').AsFloat;
    FieldByName('Price').Value := CDS_Main.fieldbyname('PRTPrice').AsFloat;
    FieldByName('money').Value := CDS_Main.fieldbyname('money').AsFloat;
    FieldByName('BBmoney').Value := CDS_Main.fieldbyname('money').AsFloat;
    FieldByName('HuiLv').Value := 1;
    FieldByName('BZType').Value := 'RMB';
    FieldByName('ComTaiTou').Value := '';
    FieldByName('QtyUnit').Value := Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
    FieldByName('KHConNo').Value := Trim(CDS_Main.fieldbyname('KHConNo').AsString);
    FieldByName('ConNo').Value := Trim(CDS_Main.fieldbyname('ConNo').AsString);
    FieldByName('P_Code').Value := Trim(CDS_Main.fieldbyname('PrtCode').AsString);
    FieldByName('PingZhi').Value := Trim(CDS_Main.fieldbyname('PingZhi').AsString);
    FieldByName('P_CodeName').Value := Trim(CDS_Main.fieldbyname('PrtCodeName').AsString);
    FieldByName('P_Color').Value := Trim(CDS_Main.fieldbyname('PRTColor').AsString);
    FieldByName('YFName').Value := Trim(CDS_Main.FieldByName('YFName').AsString);
    FieldByName('MainId').Value := Trim(CDS_Main.fieldbyname('MainId').AsString);
    FieldByName('subId').Value := Trim(CDS_Main.fieldbyname('subId').AsString);
    FieldByName('status').Value := '0';
    Post;
  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_CR2 A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR2 A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_Up_YF ' + quotedstr(Trim(CDS_Main.fieldbyname('FSId').AsString)));
    ExecSQL;
  end;
  Result := True;
end;

procedure TfrmYSSCList.FormDestroy(Sender: TObject);
begin
  frmYSSCList := nil;
end;

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

procedure TfrmYSSCList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 1;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
end;

procedure TfrmYSSCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' exec P_View_YSSC_WSQCK ' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))) + ',' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))) + ',' + quotedstr(IntToStr(cxTabControl1.TabIndex)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYSSCList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmYSSCList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmYSSCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '坯布仓库');
  Close;
end;

procedure TfrmYSSCList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv1, '坯布仓库');
 { if Trim(canshu2)='查看' then
  begin
    TBAdd.Visible:=False;
    TBDel.Visible:=False;
    TBEdit.Visible:=False;
  end else
  begin
    TBAdd.Visible:=True;
    TBDel.Visible:=True;
    TBEdit.Visible:=True;
  end;}
  //InitGrid();
  SetStatus();
end;

procedure TfrmYSSCList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmYSSCList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmYSSCList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmYSSCList.FSIdKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        //sql.Add(' ,ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId )');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;  }
  end;
end;

procedure TfrmYSSCList.FSIdChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYSSCList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYSSCList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYSSCList.ToolButton1Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex <> 0 then
    exit;
  ModalResult := 1;
end;

procedure TfrmYSSCList.cxTabControl1Change(Sender: TObject);
begin
  INITGrid();
  SetStatus();
end;

procedure TfrmYSSCList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbright then

end;

end.

