unit U_SXCKNewList;

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
  TfrmSXCKNewList = 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;
    Label8: TLabel;
    Label12: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    CRType: TComboBox;
    Label6: TLabel;
    SPCF: TEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label9: TLabel;
    SSType: TComboBox;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column5: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Label10: TLabel;
    CKName: TEdit;
    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 SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure SPIDChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    function  YFData():Boolean;
    { Private declarations }
  public
    FCKName:string;
    { Public declarations }
  end;

var
  frmSXCKNewList: TfrmSXCKNewList;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}
function TfrmSXCKNewList.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('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_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('factoryName').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('factoryName').AsString)));
        sql.Add(' and mainID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
     //   sql.Add(' and subID='''+Trim(Order_Sub.fieldbyname('subID').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
      if GetLSNo(ADOQueryCmd,YFID,'YF','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
          Append
        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('factoryName').AsString);
        FieldByName('CRTime').Value:=CDS_Main.fieldbyname('CRTime').AsDateTime;
        if Trim(CDS_Main.fieldbyname('CRType').AsString)='退货出库' then
        begin
          FieldByName('Qty').Value:=-CDS_Main.fieldbyname('Qty').AsFloat;
          FieldByName('PS').Value:=-CDS_Main.fieldbyname('PiQty').AsFloat;
        end
        else
        begin
          FieldByName('Qty').Value:=CDS_Main.fieldbyname('Qty').AsFloat;
          FieldByName('PS').Value:=CDS_Main.fieldbyname('PiQty').AsFloat;
        end;
        FieldByName('YFType').Value:='自动生成';
        FieldByName('Price').Value:=CDS_Main.fieldbyname('Price').AsFloat;
        FieldByName('OtherFee').Value:=CDS_Main.fieldbyname('CostPrice').AsFloat;
        FieldByName('money').Value:=CDS_Main.fieldbyname('Price').AsFloat*CDS_Main.fieldbyname('Qty').AsFloat+CDS_Main.fieldbyname('CostPrice').AsFloat;
        FieldByName('BBmoney').Value:=CDS_Main.fieldbyname('Price').AsFloat*CDS_Main.fieldbyname('Qty').AsFloat+CDS_Main.fieldbyname('CostPrice').AsFloat;
        FieldByName('HuiLv').Value:=1;
        FieldByName('BZType').Value:='RMB';
        FieldByName('ComTaiTou').Value:='';
        FieldByName('QtyUnit').Value:=Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
        FieldByName('orderNo').Value:=Trim(CDS_Main.fieldbyname('OrderNo').AsString);
        FieldByName('P_CodeName').Value:=Trim(CDS_Main.fieldbyname('SPName').AsString);
       // FieldByName('P_Color').Value:=Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
        IF Trim(CDS_Main.fieldbyname('CRType').AsString)='采购入库' then
        begin
           FieldByName('YFName').Value:='坯布费'
        end
        else
        if Trim(CDS_Main.fieldbyname('CRType').AsString)='成品采购' then
        begin
          FieldByName('YFName').Value:='成品采购费';
        end
        else
        if Trim(CDS_Main.fieldbyname('CRType').AsString)='回修完成' then
        begin
          FieldByName('YFName').Value:='回修费';
        end
        else
        if Trim(CDS_Main.fieldbyname('CRType').AsString)='退货出库' then
        begin
          FieldByName('YFName').Value:='坯布退货';
        end
        else
        begin
          FieldByName('YFName').Value:='加工费';
        end;

        FieldByName('MainId').Value:=Trim(CDS_Main.fieldbyname('SPID').AsString);
      //  FieldByName('subId').Value:=Trim(Order_Sub.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_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 TfrmSXCKNewList.FormDestroy(Sender: TObject);
begin
  frmSXCKNewList:=nil;
end;

procedure TfrmSXCKNewList.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caHide;
end;

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

procedure TfrmSXCKNewList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      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 ZFFlag=0 and CKName='+quotedstr(trim(FCKName)));
      sql.Add(' and CRType in(''采购入库'',''蒸丝完成入库'',''染纱完成入库'',''并线完成入库'',''加捻完成入库'',''网络完成入库'',''成品采购'',''回修完成'',''退货'',''其他加工入库'')');
     
      IF cxTabControl1.TabIndex=0 then
      SQL.Add(' and not exists(select YFPZNO from YF_Money_CR_Sub X where X.YFPZNO=A.SPID ) ')
      else
      SQL.Add(' and  exists(select YFPZNO from YF_Money_CR_Sub X where X.YFPZNO=A.SPID )');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

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

procedure TfrmSXCKNewList.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();
end;

procedure TfrmSXCKNewList.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 TfrmSXCKNewList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

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

procedure TfrmSXCKNewList.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 TfrmSXCKNewList.SPIDChange(Sender: TObject);
begin
  if Length(Trim(SPID.Text))<4 then
  begin
    if Trim(SPID.Text)<>'' then Exit;
  end;
  TBFind.Click;
end;

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

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

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

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

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

end.
