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;
    TBExport: 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;
    Label8: TLabel;
    Label12: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    Label6: TLabel;
    SPCF: TEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: 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;
    v1Column15: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    CRType: TEdit;
    v1Column5: TcxGridDBColumn;
    Tv1Column1: 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 TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure SPIDChange(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('Receiver').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_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(CDS_Main.fieldbyname('Receiver').AsString);
      FieldByName('ZdyStr1').Value := '应收收';
      Post;
    end;
  end;

  if GetLSNo(ADOQueryCmd, YFID, 'CS', 'YF_Money_CR', 3, 1) = False then
  begin
    Application.MessageBox('取应收最大号失败!', '提示', 0);
    Exit;
  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(CDS_Main.fieldbyname('crno').AsString);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应收款登记';
    FieldByName('CRFlag').Value := '应收收';
    FieldByName('QtyFlag').Value := 1;
    FieldByName('FactoryName').Value := Trim(CDS_Main.fieldbyname('Receiver').AsString);
    if (Trim(CDS_Main.fieldbyname('CRTime').AsString) <> '') then
      FieldByName('CRTime').Value := CDS_Main.fieldbyname('CRTime').AsDateTime
    else
      FieldByName('CRTime').Value := SGetServerDate10(ADOQueryTemp);
    FieldByName('Qty').Value := CDS_Main.fieldbyname('Qty').AsFloat;
    FieldByName('PS').Value := CDS_Main.fieldbyname('PiQty').AsFloat;
    FieldByName('YFType').Value := '自动生成';
    FieldByName('Price').Value := CDS_Main.fieldbyname('Price').AsFloat;
    FieldByName('money').Value := CDS_Main.fieldbyname('money').AsFloat;
    FieldByName('BBmoney').Value := CDS_Main.fieldbyname('money').AsFloat;
    FieldByName('HuiLv').Value := 1;
//    FieldByName('BZType').Value := Trim(CDS_Main.fieldbyname('PriceUnit').AsString);
    FieldByName('QtyUnit').Value := Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
//    FieldByName('orderNo').Value := Trim(CDS_Main.fieldbyname('OrderNo').AsString);
//    FieldByName('P_Code').Value := Trim(CDS_Main.fieldbyname('spcode').AsString);
//    FieldByName('P_CodeName').Value := Trim(CDS_Main.fieldbyname('SPName').AsString);
//    FieldByName('P_Spec').Value := Trim(CDS_Main.fieldbyname('SPSpec').AsString);
//    FieldByName('P_Color').Value := Trim(CDS_Main.fieldbyname('SPColor').AsString);
    FieldByName('YFName').Value := Trim(CDS_Main.FieldByName('FeeName').AsString);
    FieldByName('MainId').Value := Trim(CDS_Main.fieldbyname('crno').AsString);
    FieldByName('SubId').Value := Trim(CDS_Main.fieldbyname('crid').AsString);
//    FieldByName('KHName').Value := Trim(CDS_Main.fieldbyname('Receiver').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_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;

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 - 30;
  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_copy1 ');
      SQL.Add(' @BegDate=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + ''' ');
      SQL.Add(',@Enddate=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime)) + ''' ');
      SQL.Add(',@Flag=' + 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, '财务管理');
  SetStatus();
end;

procedure TfrmYSSCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('应收款信息列表', cxGrid2);
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.SPIDKeyPress(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.SPIDChange(Sender: TObject);
begin
  if Length(Trim(SPID.Text)) < 4 then
  begin
    if Trim(SPID.Text) <> '' then
      Exit;
  end;
  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 CDS_Main.IsEmpty then
    exit;
  if not CDS_Main.Locate('ssel', true, []) then
  begin
    application.MessageBox('没有选择数据！', '提示信息', 0);
    exit;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Main do
    begin
      DisableControls;               
      first;
      while not eof do
      begin
        if fieldbyname('ssel').AsBoolean then
        begin
          if not YSData() then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('生成应收款失败！', '提示信息', 0);
          end;
        end;
        next;
      end;
      first;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('生成应收款成功！', '提示信息');
    initGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('生成应收款失败！', '提示信息', 0);
  end;
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.

