unit U_KKView;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit,
  cxSplitter, cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, cxPC, StdCtrls, ComCtrls, ExtCtrls,
  ToolWin, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, DBClient;

type
  TfrmKKView = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label3: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label11: TLabel;
    MPRTCodeName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    MDNO: TEdit;
    MPRTCode: TEdit;
    MDFactoryName: TEdit;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ScrollBox1: TScrollBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBBandedTableView;
    v2Fty: TcxGridDBBandedColumn;
    cxGridDBBandedColumn7: TcxGridDBBandedColumn;
    cxGridDBBandedColumn8: TcxGridDBBandedColumn;
    v2Pon: TcxGridDBBandedColumn;
    v2Column2: TcxGridDBBandedColumn;
    v2Column3: TcxGridDBBandedColumn;
    cxGridLevel3: TcxGridLevel;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBBandedTableView;
    v3Fty: TcxGridDBBandedColumn;
    cxGridDBBandedColumn13: TcxGridDBBandedColumn;
    cxGridDBBandedColumn14: TcxGridDBBandedColumn;
    v3Pon: TcxGridDBBandedColumn;
    cxGridDBBandedColumn16: TcxGridDBBandedColumn;
    cxGridDBBandedColumn17: TcxGridDBBandedColumn;
    cxGridLevel4: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource4: TDataSource;
    ClientDataSet4: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ClientDataSet3: TClientDataSet;
    DataSource3: TDataSource;
    ADOQuery3: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQuery1: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Label4: TLabel;
    TJXSInfo: TEdit;
    Label8: TLabel;
    TJJGInfo: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Label9: TLabel;
    TJXSPonInfo: TEdit;
    Label10: TLabel;
    TJJGPonInfo: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure MDNOKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmKKView: TfrmKKView;

implementation
uses
  U_DataLink,U_RTFun, U_ModuleNote,U_TJCKRKOK;

{$R *.dfm}

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

procedure TfrmKKView.FormDestroy(Sender: TObject);
begin
  frmKKView:=nil;
end;

procedure TfrmKKView.TBRafreshClick(Sender: TObject);
var
  fsj:string;
begin
  try
    ADOQuery3.DisableControls;
    with ADOQuery3 do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
      if cxTabControl1.TabIndex<>0 then
      begin
        Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date));
        Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.Date+1));
      end;
      Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
     // Parameters.ParamByName('orderno').Value:='';
      Parameters.ParamByName('MDType').Value:='';
      if cxTabControl1.TabIndex=0 then
      begin
        fsj:=' and isnull(JMM.Status,'''')=''协商'' ';
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        fsj:=' and isnull(JMM.Status,'''')=''结果'' ';
      end else
      begin
        fsj:=' and isnull(JMM.Status,'''')<>'''' ';
      end;
      fsj:=fsj+' and isnull(JMM.CKName,'''')<>'''' and exists(select * from JYOrder_Main_MD_KK JMK where JMK.MDID=JMM.MDID)';
      if Trim(MDFactoryName.Text)<>'' then
      begin
        fsj:=fsj+' and isnull(JMM.FactoryName,'''') like '''+'%'+Trim(MDFactoryName.Text)+'%'+'''';
      end;
      Parameters.ParamByName('WSql').Value:=fsj;
      Open;
    end;
    SCreateCDS20(ADOQuery3,ClientDataSet3);
    SInitCDSData20(ADOQuery3,ClientDataSet3);
  finally
    ADOQuery3.EnableControls;
  end;
  TBFind.Click;
end;

procedure TfrmKKView.TBFindClick(Sender: TObject);
begin
  if ADOQuery3.Active=False then Exit;
  //SDofilter10(ClientDataSet3,SGetFilters(Panel1,1,2));
  SDofilter(ADOQuery3,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQuery3,ClientDataSet3);
  SInitCDSData20(ADOQuery3,ClientDataSet3);
end;

procedure TfrmKKView.orderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmKKView.orderNoKeyPress(Sender: TObject; var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Trim(orderNo.Text)='' then Exit;
    if Length(Trim(orderNo.Text))<4 then Exit;
    try
      ADOQuery3.DisableControls;
      with ADOQuery3 do
      begin
        Close;
        Filtered:=False;
        sql.Clear;
        sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
        Parameters.ParamByName('begdate').Value:='';
        Parameters.ParamByName('enddate').Value:='';
        Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
        Parameters.ParamByName('MDType').Value:='';
        fsj:=' and isnull(JMM.CKName,'''')<>'''' and exists(select * from JYOrder_Main_MD_KK JMK where JMK.MDID=JMM.MDID)';
        if cxTabControl1.TabIndex=0 then
        begin
          fsj:=fsj+' and isnull(JMM.Status,'''')=''协商'' ';
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          fsj:=fsj+' and isnull(JMM.Status,'''')=''结果'' ';
        end else
        begin
          fsj:=fsj+' and isnull(JMM.Status,'''')<>'''' ';
        end;
        Parameters.ParamByName('WSql').Value:=fsj;
        Open;
      end;
      SCreateCDS20(ADOQuery3,ClientDataSet3);
      SInitCDSData20(ADOQuery3,ClientDataSet3);
    finally
      ADOQuery3.EnableControls;
    end;
  end;

end;

procedure TfrmKKView.MDNOKeyPress(Sender: TObject; var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Trim(MDNO.Text)='' then Exit;
    if Length(Trim(MDNO.Text))<4 then Exit;
    try
      ADOQuery3.DisableControls;
      with ADOQuery3 do
      begin
        Close;
        Filtered:=False;
        sql.Clear;
        sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
        Parameters.ParamByName('begdate').Value:='';
        Parameters.ParamByName('enddate').Value:='';
        Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
        Parameters.ParamByName('MDType').Value:='';
        fsj:=' and JMM.MDNO like '''+'%'+Trim(MDNO.Text)+'%'+'''';
        fsj:=fsj+' and isnull(JMM.CKName,'''')<>'''' and exists(select * from JYOrder_Main_MD_KK JMK where JMK.MDID=JMM.MDID)';
        if cxTabControl1.TabIndex=0 then
        begin
          fsj:=fsj+' and isnull(JMM.Status,'''')=''协商'' ';
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          fsj:=fsj+' and isnull(JMM.Status,'''')=''结果'' ';
        end else
        begin
          fsj:=fsj+' and isnull(JMM.Status,'''')<>'''' ';
        end;
        Parameters.ParamByName('WSql').Value:=fsj;
        Open;
      end;
      SCreateCDS20(ADOQuery3,ClientDataSet3);
      SInitCDSData20(ADOQuery3,ClientDataSet3);
    finally
      ADOQuery3.EnableControls;
    end;
  end;

end;

procedure TfrmKKView.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmKKView.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_KK where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
    sql.Add(' and isnull(KKTYpe,'''')=''协商'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet2);
  SInitCDSData20(ADOQuery1,ClientDataSet2);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_KK where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
    sql.Add(' and isnull(KKTYpe,'''')=''结果'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet4);
  SInitCDSData20(ADOQuery1,ClientDataSet4);
end;

procedure TfrmKKView.FormShow(Sender: TObject);
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-60;
  cxTabControl1.TabIndex:=0;
  ReadCxGrid('tv1',Tv1,'扣款查询');
  ReadCxBandedGrid('tv2',Tv2,'扣款查询');
  ReadCxBandedGrid('tv4',Tv4,'扣款查询');
end;

procedure TfrmKKView.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('tv1',Tv1,'扣款查询');
  WriteCxBandedGrid('tv2',Tv2,'扣款查询');
  WriteCxBandedGrid('tv4',Tv4,'扣款查询');
  Close;
end;

procedure TfrmKKView.TBExportClick(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  TcxGridToExcel('调节仓库扣款码单',cxGrid1);
end;

procedure TfrmKKView.Tv1DblClick(Sender: TObject);
var
  fsj:string;
begin
  try
    frmTJCKRKOK:=TfrmTJCKRKOK.Create(Application);
    with frmTJCKRKOK do
    begin
      if Self.cxTabControl1.TabIndex=0 then
      begin
        frmTJCKRKOK.cxTabControl1.TabIndex:=2;
      end else
      if Self.cxTabControl1.TabIndex=1 then
      begin
        frmTJCKRKOK.cxTabControl1.TabIndex:=3;
      end;
      //frmTJCKRKOK.OrderNo.Text:=Trim(self.ClientDataSet3.fieldbyname('OrderNo').AsString);
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select JM.OrderNo, A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price, ');
        sql.Add(' JS.PRTHX,JS.PRTColor,JS.PRTColorNo,D.C_Code,D.C_CodeName,ClotheType10=A.ClotheType,');
        sql.Add(' JSKCPS=A.RKPS-A.CKPS,');
        sql.Add(' JSKCQty=A.RKQty-A.CKQty,');
        sql.Add(' JM.MPRTCodeName,MPRTCode=RTrim(JM.MPRTCode)+RTrim(JM.MPRTGY)');
        sql.Add(' from JYOrder_Main_MD  A');
        sql.Add(' inner join JYOrder_Main JM on A.OrdMainId=JM.MainId');
        sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
        sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
        sql.Add(' inner join Contract_Main C on A.ConMainid=C.MainID');
        sql.Add(' inner join Contract_Sub D on A.ConSubid=D.SubID');
        sql.Add(' where A.MDID='''+Trim(Self.ClientDataSet3.fieldbyname('MDID').AsString)+'''');
        SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');

        if Self.cxTabControl1.TabIndex=0 then
        begin
          sql.Add('  and isnull(A.Status,'''')=''协商'' ');
        end else
        if Self.cxTabControl1.TabIndex=1 then
        begin
          sql.Add('  and isnull(A.Status,'''')=''结果'' ');
        end;
        Open;
      end;
      SCreateCDS20(ADOQuery1,ClientDataSet6);
      SInitCDSData20(ADOQuery1,ClientDataSet6);
      ClientDataSet6.Locate('MDNO',Trim(Self.ClientDataSet3.fieldbyname('MDNO').AsString),[]);
      fsj:='select Code=FactoryNo,Name=FactoryName from JYOrder_Main_MD where ORDMainId='''+Trim(ClientDataSet6.fieldbyname('ORDMainId').AsString)+''''+
       ' group by FactoryNo,FactoryName';
      SInitCxBandGridComboBoxBySql(frmTJCKRKOK.ADOQueryTemp,frmTJCKRKOK.v2fty,fsj,1,True,'');
      with frmTJCKRKOK.ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      SCSHDataNew(frmTJCKRKOK.ADOQuery1,frmTJCKRKOK.ScrollBox1,2);
      frmTJCKRKOK.InitGridKK();
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main_MD_Out where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      {if ADOQueryTemp.IsEmpty=False then
      begin
        RKPS.ReadOnly:=True;
        RKQty.ReadOnly:=True;
      end;}
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmTJCKRKOK.Free;
  end;
end;

end.
