unit U_SXCKXSOutList;

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;

type
  TfrmSXCKXSOutList = 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;
    FactoryName10: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    CRType: TComboBox;
    Label6: TLabel;
    SPCF: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column15: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column1: TcxGridDBColumn;
    Label7: TLabel;
    SXPinPai10: TEdit;
    v1Column10: TcxGridDBColumn;
    Label9: TLabel;
    ToFactoryName: TEdit;
    Label10: TLabel;
    BatchNo: TEdit;
    v1Column13: 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 SPIDChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryName10Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    function  YFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSXCKXSOutList: TfrmSXCKXSOutList;

implementation
uses
  U_DataLink,U_Fun,U_RTFun;

{$R *.dfm}
function TfrmSXCKXSOutList.YFData():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('ToFactoryName').AsString)));
    sql.Add(' and KCType=''KH'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CRID');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    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;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into YF_Money_CRID select 0 ');
        sql.Add('select * from YF_Money_CRID ');
        Open;
      end;
    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('ToFactoryName').AsString);
      FieldByName('ZdyStr1').Value:='应收收';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='+quotedstr(Trim(CDS_Main.fieldbyname('ToFactoryName').AsString)));
    sql.Add(' and YFTypeId='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'YS','YF_Money_CR',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_CR where YFID='''+Trim(YFID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
    begin
      Append;
      FieldByName('SCQty').Value:=CDS_Main.fieldbyname('Qty10').AsFloat;
      FieldByName('SCPS').Value:=CDS_Main.fieldbyname('PiQty').AsFloat;
    end
    else
    Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('YFTypeId').Value:=Trim(CDS_Main.fieldbyname('SPID').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('ToFactoryName').AsString);
    FieldByName('CRTime').Value:=CDS_Main.fieldbyname('CRTime').AsDateTime;
    FieldByName('Qty').Value:=CDS_Main.fieldbyname('Qty10').AsFloat;
    FieldByName('PS').Value:=CDS_Main.fieldbyname('PiQty').AsFloat;
    FieldByName('YFType').Value:='自动生成';
    FieldByName('Price').Value:=CDS_Main.fieldbyname('Price').Value;
    FieldByName('money').Value:=CDS_Main.fieldbyname('money').Value;
    FieldByName('ComTaiTou').Value:=CDS_Main.fieldbyname('ComTaiTou').Value;;
    FieldByName('QtyUnit').Value:=Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
    FieldByName('P_CodeName').Value:=Trim(CDS_Main.fieldbyname('SPName').AsString);
    FieldByName('P_CF').Value:=Trim(CDS_Main.fieldbyname('SPCF').AsString);
    FieldByName('P_Spec').Value:=Trim(CDS_Main.fieldbyname('SPSpec').AsString);
    FieldByName('P_BatchNo').Value:=Trim(CDS_Main.fieldbyname('BatchNo').AsString);
    FieldByName('YFName').Value:='销售金额';
    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 TfrmSXCKXSOutList.FormDestroy(Sender: TObject);
begin
  frmSXCKXSOutList:=nil;
end;

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

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

procedure TfrmSXCKXSOutList.InitGrid();
begin
  ToolBar1.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(',FactoryName10=RTrim(dbo.getPinYin(A.FactoryName))+RTrim(A.FactoryName)');
      sql.Add(',SXPinPai10=RTrim(dbo.getPinYin(A.SXPinPai))+RTrim(A.SXPinPai)');
      sql.Add(',Qty10=Qty');
      sql.Add(' from CK_SXPB_CR A');
      sql.add(' where A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add(' and A.CRTime<='''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      sql.Add(' and CRType=''销售出库(应收款)'' ');
      SQL.Add('  and ZFFlag=0 '); //and CRFlag=''入库''
      IF cxTabControl1.TabIndex=0 then
        SQL.Add(' and not exists(select maiNID from YF_Money_CR X where X.YFTypeID=A.SPID ) ')
      else
        SQL.Add(' and  exists(select maiNID from YF_Money_CR X where X.YFTypeID=A.SPID )');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmSXCKXSOutList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv1,'应收款管理');
  Close;
end;

procedure TfrmSXCKXSOutList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption,Tv1,'应收款管理');
end;

procedure TfrmSXCKXSOutList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('纱线销售列表',cxGrid2);
end;

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

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

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

procedure TfrmSXCKXSOutList.SPIDChange(Sender: TObject);
begin
  if Length(Trim(SPID.Text))<4 then
  begin
    if Trim(SPID.Text)<>'' then Exit;
  end;
  TBFind.Click;
end;

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

procedure TfrmSXCKXSOutList.FactoryName10Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSXCKXSOutList.ToolButton1Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  BegDate.SetFocus;
  if cxTabControl1.TabIndex<>0 then Exit;
  IF not CDS_Main.Locate('ssel',true,[]) then
  begin
    application.MessageBox('没有选择数据！','提示信息',0);
    exit;
  end;
  if CDS_Main.Locate('SSel;Price', VarArrayOf([True, Null]), [loPartialKey])=True then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要生成应收款吗？','提示',32+4)<>IDYES then Exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Main do
    begin
      DisableControls;
      first;
      while Locate('SSel',True,[]) do
      begin
        IF not YFData() then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          application.MessageBox('生成应收款失败！','提示信息',0);
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
     ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('生成应收款失败！','提示信息',0);

  end;
end;

procedure TfrmSXCKXSOutList.cxTabControl1Change(Sender: TObject);
begin
  INITGrid();
  if cxTabControl1.TabIndex=0 then
  begin
    v1Price.Options.Editing:=True;
  end else
  if cxTabControl1.TabIndex=1 then
  begin
    v1Price.Options.Editing:=False;
  end;
end;

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

procedure TfrmSXCKXSOutList.v1Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FPrice,FQty:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingValue;
  with CDS_Main do
  begin
    Edit;
    if Trim(mvalue)='' then
    begin
      FieldByName('Price').Value:=Null;
    end else
    begin
      FieldByName('Price').Value:=mvalue;
    end;
  end;
  FQty:=CDS_Main.fieldbyname('Qty10').AsString;
  if Trim(mvalue)='' then
  begin
    with CDS_Main do
    begin
      Edit;
      FieldByName('Money').Value:=Null;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_SXPB_CR Set Price=Null,Money=Null ');
      sql.Add('where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
      ExecSQL;
    end;
  end else
  begin
    with CDS_Main do
    begin
      Edit;
      FieldByName('Money').Value:=StrToFloat(FQty)*StrToFloat(mvalue);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_SXPB_CR Set Price='+mvalue);
      sql.Add(',Money='+CDS_Main.fieldbyname('Money').AsString);
      sql.Add('where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
      ExecSQL;
    end;
  end;
end;

procedure TfrmSXCKXSOutList.ToolButton2Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  BegDate.SetFocus;
  if cxTabControl1.TabIndex<>1 then Exit;
  IF not CDS_Main.Locate('ssel',true,[]) then
  begin
    application.MessageBox('没有选择数据！','提示信息',0);
    exit;
  end;
  if Application.MessageBox('确定要撤销吗？','提示',32+4)<>IDYES then Exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Main do
    begin
      DisableControls;
      first;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' delete YF_Money_CR where YFTypeId='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
          sql.Add(' and FactoryName='''+Trim(CDS_Main.fieldbyname('FactoryName').AsString)+'''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
     ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销异常！','提示信息',0);

  end;
end;

end.
