unit U_PBRKList;

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,IniFiles,jpeg,U_SLT;

type
  TfrmPBRKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBEdit: TToolButton;
    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;
    PBNO: 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;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label9: TLabel;
    ToFactoryName: TEdit;
    v1Column17: TcxGridDBColumn;
    Label10: TLabel;
    SXPinPai: TEdit;
    cxTabControl1: TcxTabControl;
    Label13: TLabel;
    BatchNo: TEdit;
    adoqueryPicture: TADOQuery;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    RMDBDataSet1: TRMDBDataSet;
    ClientDataSet1: TClientDataSet;
    v1Column24: TcxGridDBColumn;
    ToolButton6: TToolButton;
    RMGridReport1: TRMGridReport;
    btn1: TToolButton;
    btn2: TToolButton;
    btn3: TToolButton;
    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 TBDelClick(Sender: TObject);
    procedure TBEditClick(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 BatchNoKeyPress(Sender: TObject; var Key: Char);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
  private
    canshu1,canshu2,canshu3:string;
    procedure InitGrid();
 
    procedure ReadINIFile();
    { Private declarations }
  public
    { Public declarations }
  end;

//var
 // frmShaRKList: TfrmShaRKList;
 var
   Mach: array of TfrmSlt;

implementation
uses
  U_DataLink,U_RTFun,U_ShaRKInPutWSQ, U_FileUp,U_WechatAuthority; 

{$R *.dfm}

procedure TfrmPBRKList.FormDestroy(Sender: TObject);
begin
  //frmShaRKList:=nil;
end;

procedure TfrmPBRKList.FormClose(Sender: TObject;
  var Action: TCloseAction);
var
  i,j:integer;
begin
  j:=length(Mach);
  if j>0 then
  begin
    for i:=0 to j-1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  Action:=caFree;
end;
procedure TfrmPBRKList.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;
procedure TfrmPBRKList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime;
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  canshu3:=Trim(DParameters3);
end;

procedure TfrmPBRKList.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 isnull(CKName,'''')=''纱线''');
      SQL.Add(' and CRFlag=''入库''  ');
      case   cxTabControl1.TabIndex of
      0:begin
           Sql.Add(' and isnull(ChkStatus,''已发货'')=''已发货'' ');
        end;
      1:begin
           Sql.Add(' and isnull(ChkStatus,''已发货'')=''已收货'' ');
        end;
      2:begin
           Sql.Add(' and isnull(ChkStatus,''已发货'')=''已审核'' ');
        end;
      3:begin
           Sql.Add(' and isnull(ChkStatus,''已发货'')=''仓库已确认'' ');
        end;
      end;

      SQL.Add(' order by CRTime desc');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmPBRKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线入库列表',Tv1,'纱线仓库');
  Close;
end;

procedure TfrmPBRKList.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线入库列表',Tv1,'纱线仓库');
 
end;

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

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

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

procedure TfrmPBRKList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Trim(CDS_Main.fieldbyname('SPID').AsString)<>'' then
  begin

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR_Sub where YFPZNO='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已经生成应付款不能删除!','提示',0);
      Exit;
    end;
   
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;

  end else
  begin
    CDS_Main.Delete;
  end;
end;

procedure TfrmPBRKList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Trim(CDS_Main.fieldbyname('CRType').AsString)='平移入库' then
  begin
    Application.MessageBox('平移入库的数据为自动生成，不能修改!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR_Sub where YFPZNO='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已经生成应付款不能修改!','提示',0);
    Exit;
  end;
  try
    frmShaRKInPutWSQ:=TfrmShaRKInPutWSQ.Create(Application);
    with frmShaRKInPutWSQ do
    begin
      FBCId:=Trim(CDS_Main.fieldbyname('SPID').AsString);
      TBDel.Visible:=False;
      TBAdd.Visible:=False;

      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmShaRKInPutWSQ.Free;
  end;
end;

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

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

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

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

procedure TfrmPBRKList.BatchNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key<>#13 then Exit;
  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.BatchNo='''+Trim(BatchNo.Text)+'''');
      sql.Add(' and isnull(CKName,'''')=''纱线''');
      SQL.Add(' and CRFlag=''入库''  ');
      {if Trim(canshu2)<>'' then
      begin
        SQL.Add(' and isnull(DeptName,'''')='''+Trim(canshu2)+'''');
      end;}
      if cxTabControl1.TabIndex=0 then
      begin
        Sql.Add(' and ZFFlag=0 ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        Sql.Add(' and ZFFlag=1 ');
      end;
      SQL.Add(' order by CRTime desc');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmPBRKList.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_MX,True);
end;

procedure TfrmPBRKList.N4Click(Sender: TObject);
begin
  SelOKNo(CDS_MX,False);
end;

procedure TfrmPBRKList.ToolButton4Click(Sender: TObject);
var
  fPrintFile:string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_Print_SXRK'+quotedstr(Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))));
    SQL.Add(',''年'' ');
    Open;
//    ShowMessage(SQL.Text);
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\纱线入库年报表.rmf' ;
  if FileExists(fPrintFile) then
  begin
//    RMVariables['ConNo']:=Order_Main.fieldbyname('ConNo').Value;
    RMGridReport1.LoadFromFile(fPrintFile);
    RMGridReport1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\纱线入库年报表.rmf'),'提示',0);
  end;
end;

procedure TfrmPBRKList.ToolButton6Click(Sender: TObject);
begin
  try
    frmShaRKInPutWSQ:=TfrmShaRKInPutWSQ.Create(Application);
    with frmShaRKInPutWSQ do
    begin
      FBCId:='';
//      FCRType:='盘盈入库';
//      FHZCRType:='盘盈入库';
      ZHFlag:='1';
      frmShaRKInPutWSQ.canshu3:=Trim(Self.canshu3);
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmShaRKInPutWSQ.Free;
  end;
end;

procedure TfrmPBRKList.btn1Click(Sender: TObject);
begin
  try
    frmWechatAuthority:=TfrmWechatAuthority.Create(Application);
    with frmWechatAuthority do
    begin
      FQX:='纱线到货';
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmWechatAuthority.Free;
  end;
end;

procedure TfrmPBRKList.btn2Click(Sender: TObject);
begin
  try
    frmWechatAuthority:=TfrmWechatAuthority.Create(Application);
    with frmWechatAuthority do
    begin
      FQX:='纱线审核';
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmWechatAuthority.Free;
  end;
end;

procedure TfrmPBRKList.btn3Click(Sender: TObject);
begin
  try
    frmWechatAuthority:=TfrmWechatAuthority.Create(Application);
    with frmWechatAuthority do
    begin
      FQX:='纱线确认';
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmWechatAuthority.Free;
  end;
end;

end.
