unit U_PBRKMX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxButtonEdit,
  cxDropDownEdit, cxPC, Buttons, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator;

type
  TfrmPBRKMX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    V2MYCode: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    PB_MX: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    Label6: TLabel;
    OrderNoHZ: TEdit;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    Label1: TLabel;
    ConBegDate: TDateTimePicker;
    ConEndDate: TDateTimePicker;
    Label2: TLabel;
    TV2Column13: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    Label3: TLabel;
    PBBatchNo: TEdit;
    TV2Column11: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    Label4: TLabel;
    Label5: TLabel;
    NameHZ: TEdit;
    CodeHZ: TEdit;
    Label7: TLabel;
    KHNameHZ: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure OrderNoHZChange(Sender: TObject);
  private
    canshu1: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmPBRKMX: TfrmPBRKMX;

implementation

uses
  U_DataLink, U_RTFun, U_QCRKInPut, U_ZDYHelp, U_LLRKInPut, U_GYSList,
  U_RCCKInPut, U_LLRKInPutMH, U_LLRKInPutX, U_LLRKInPut_CG, U_LLRKInPutMH_CG,
  U_LLRKInPutMH_PBCG, U_LLRKInPutMH_PBKCCG, U_BPZdy_LRPB, U_QCRKInPut_KC,
  U_QCRKList_CX, U_ZDYCPName;

{$R *.dfm}

procedure TfrmPBRKMX.FormDestroy(Sender: TObject);
begin
  frmPBRKMX := nil;
end;

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

procedure TfrmPBRKMX.FormCreate(Sender: TObject);
begin
  canshu1 := Trim(DParameters1);
end;

procedure TfrmPBRKMX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('坯布明细3', Tv2, '贸易生产管理');
  Close;
end;

procedure TfrmPBRKMX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      sql.Add(' select  DISTINCT A.PBBatchNo   ');
      sql.Add(' ,OrderNo=(select  MLOrderNo from ML_OrderMainNew X where X.NewMLID=A.MainID )  ');
      sql.Add(' ,KHName=(select  KHName from ML_OrderMainNew X where X.NewMLID=A.MainID )  ');
      sql.Add(' ,KHNo=(select  KHNo from ML_OrderMainNew X where X.NewMLID=A.MainID )  ');
      sql.Add(' INTO #tab1 from CK_MYSC_CR A   ');
      sql.Add(' where isnull(A.CRType,'''')=''加工码单''  ');
      sql.Add(' and CRTime>' + quotedstr(FormatDateTime('yyyy-MM-dd', ConBegDate.DateTime)));
      sql.Add(' and CRTime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', Conenddate.DateTime + 1)));

      sql.Add(' select DISTINCT PBBatchNo,YGSunHao =(select  max(YGSunHao) from  CK_MYSC_CR X where X.PBBatchNo=A.PBBatchNo)  ');
      sql.Add(' ,OrderNoHZ=cast((select DISTINCT OrderNo +'';'' from #tab1 X where X.PBBatchNo=A.PBBatchNo FOR XML PATH('''')) as VARCHAR(600)) ');
      sql.Add(' ,KHNameHZ=cast((select DISTINCT KHName +'';'' from #tab1 X where X.PBBatchNo=A.PBBatchNo FOR XML PATH('''')) as VARCHAR(600)) ');
      sql.Add(' ,KHNoHZ=cast((select DISTINCT KHNo +'';'' from #tab1 X where X.PBBatchNo=A.PBBatchNo FOR XML PATH('''')) as VARCHAR(600)) ');

      sql.Add(' ,NameHZ=cast((select DISTINCT MYName +'' '' from CK_MYSC_CR Y  where isnull(Y.MYType,'''')=''采购坯布'' and isnull(Y.CRType,'''') in(''采购入库'',''坯布退货'',''坯布转厂'')  ');
      sql.Add(' and Y.PBBatchNo =A.PBBatchNo FOR XML PATH('''')) as VARCHAR(600))  ');

      sql.Add(' ,CodeHZ=cast((select DISTINCT MYCode +'' '' from CK_MYSC_CR Y  where isnull(Y.MYType,'''')=''采购坯布'' and isnull(Y.CRType,'''') in(''采购入库'',''坯布退货'',''坯布转厂'')  ');
      sql.Add(' and Y.PBBatchNo =A.PBBatchNo FOR XML PATH('''')) as VARCHAR(600))  ');

      sql.Add(' ,THKHHZ=cast((select DISTINCT KHName +'' '' from CK_MYSC_CR Y  where isnull(Y.MYType,'''')=''采购坯布'' and isnull(Y.CRType,'''') in(''坯布退货'',''坯布转厂'')  ');
      sql.Add(' and Y.PBBatchNo =A.PBBatchNo FOR XML PATH('''')) as VARCHAR(600))  ');

      sql.Add(' ,RanQty=isnull((select sum(Qty) from CK_MYSC_CR X  where A.PBBatchNo=X.PBBatchNo and isnull(X.CRType,'''')=''加工码单''  ');
      sql.Add(' and X.MYTypeFlag in (''染色'') ),0)  ');
      sql.Add(',PBQtyHZ=isnull((select  sum(Y.Qty) from  CK_MYSC_CR Y  where isnull(Y.MYType,'''')=''采购坯布'' and isnull(Y.CRType,'''') in(''采购入库'')   ');
      sql.Add('and Y.PBBatchNo =A.PBBatchNo),0) ');
      sql.Add(',PBTHQtyHZ=isnull((select  sum(Y.Qty) from  CK_MYSC_CR Y  where isnull(Y.MYType,'''')=''采购坯布'' and isnull(Y.CRType,'''') in(''坯布退货'',''坯布转厂'')   ');
      sql.Add('and Y.PBBatchNo =A.PBBatchNo),0) ');
      sql.Add(' into #tab2 from CK_MYSC_CR A  ');
      sql.Add(' where isnull(A.CRType,'''')=''加工码单''  ');
      sql.Add(' and CRTime>' + quotedstr(FormatDateTime('yyyy-MM-dd', ConBegDate.DateTime)));
      sql.Add(' and CRTime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', Conenddate.DateTime + 1)));
      sql.Add(' select *,CASE WHEN(isnull(RanQty,0)<>0 and isnull(PBQtyHZ,0)<>0 and ceiling((PBQtyHZ/RanQty-1)*100)>0 ) THEN (cast(ceiling((PBQtyHZ/RanQty-1)*100) AS varchar)+''%'') ELSE ''0%'' END AS RanSL  from  #tab2 ');
      sql.Add(' where isnull(OrderNoHZ,'''') <> '''' and PBQtyHZ>0  ');
      sql.Add(' Drop  table #tab1  ');
      sql.Add(' Drop  table #tab2  ');

//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPBRKMX.InitForm();
begin
  ReadCxGrid('坯布明细3', Tv2, '贸易生产管理');
  ConEndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  ConBegDate.DateTime := ConEndDate.DateTime - 15;
  InitGrid();
end;

procedure TfrmPBRKMX.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

function TfrmPBRKMX.DelData(): Boolean;
begin

end;

procedure TfrmPBRKMX.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid2);
end;

procedure TfrmPBRKMX.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPBRKMX.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmPBRKMX.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPBRKMX.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmPBRKMX.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

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

procedure TfrmPBRKMX.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBRKMX.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(Order_Main.fieldbyname('Chker').AsString) = '' then
    exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\检测报告.rmf';
  RMXLSExport2 := TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRId=''' + Trim(Order_Main.fieldbyname('TRId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['OrderUnit']:=Order_Main.fieldbyname('OrderUnit').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\检测报告.rmf'), '提示', 0);
  end;
end;

procedure TfrmPBRKMX.OrderNoHZChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

end.

