unit U_DJBCKKCList_PBSH;

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;

type
  TfrmDJBCKKCList_PBSH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v2MPRTCode: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    v1TPPS: TcxGridDBColumn;
    v2TPQty: TcxGridDBColumn;
    v1HCPS: TcxGridDBColumn;
    v1HCQty: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    MPRTCodeName: TEdit;
    MPRTCode: TEdit;
    MPRTCF: TEdit;
    OrderNo: TEdit;
    v1PRTColorNo: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1PRTHX: TcxGridDBColumn;
    Label9: TLabel;
    ConNoHZ: TEdit;
    Label10: TLabel;
    PRTColor: TEdit;
    Label11: TLabel;
    StyleNo: TEdit;
    Label13: TLabel;
    PRTHX: TEdit;
    ToolButton2: TToolButton;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1ConNoHZ: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1PRTColorEng: TcxGridDBColumn;
    v1PBSH: TcxGridDBColumn;
    v1StyleNo: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure MPRTCodeChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
  private
    procedure InitGrid();
    { Private declarations }
  public
    canshu1:string;
    { Public declarations }
  end;

var
  frmDJBCKKCList_PBSH: TfrmDJBCKKCList_PBSH;

implementation
uses
  U_DataLink,U_RTFun,U_SXCKInPut;

{$R *.dfm}

procedure TfrmDJBCKKCList_PBSH.FormDestroy(Sender: TObject);
begin
  frmDJBCKKCList_PBSH:=nil;
end;

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

procedure TfrmDJBCKKCList_PBSH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select AA.*,');
      sql.add(' PBSH=(case when TPQty<>0 then (TPQty-HCQty)/TPQty*100 else 0 end) ');
      sql.add(' from (select A.*,B.StyleNo,B.PRTColorNo,B.PRTHX,B.PRTColor,B.PRTColorEng,B.PRTOrderQty, ');
      sql.add(' TPPS=(select Sum(C.PiQty) from CK_SXPB_CR C left join CK_SXPB_CR G on G.SPID=C.FZSPID where C.ORDSubIdCK=B.SubId and C.CRFlag=''出库'' and C.CKName=''坯布'' and G.CRType<>''加工完成''),');
      sql.add(' TPQty=(select Sum(C.Qty) from CK_SXPB_CR C left join CK_SXPB_CR G on G.SPID=C.FZSPID where C.ORDSubIdCK=B.SubId and C.CRFlag=''出库'' and C.CKName=''坯布'' and G.CRType<>''加工完成''),');
      sql.add(' HCPS=(select Sum(C.PiQty) from CK_SXPB_CR C where C.OrdSubIdRK=B.SubId and C.CRFlag=''入库'' and C.CKName=''待检布''),');
      sql.add(' HCQty=(select Sum(C.Qty) from CK_SXPB_CR C where C.OrdSubIdRK=B.SubId and C.CRFlag=''入库'' and C.CKName=''待检布''),');
      sql.add(' KHConNoHZ=[dbo].[F_Get_Order_SubStr](A.MainId,''KHConNoHZ''), ');
      sql.add(' ConNoHZ=[dbo].[F_Get_Order_SubStr](A.MainId,''ConNoHZ'') ');
      sql.Add(' from JYOrder_Main A ');
      sql.add(' inner join JYOrder_Sub B on B.Mainid=A.Mainid ');
      sql.add(' where A.OrdDate>='''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+''' ');
      sql.Add(' and A.OrdDate<'''+trim(formatdatetime('yyyy-MM-dd',EndDate.DateTime+1))+''' ');
      if trim(canshu1)<>'高权限' then
      sql.add(' and (A.Filler='''+Trim(DName)+''' or A.LiDanPerson='''+Trim(DName)+''' or A.YWY='''+Trim(DName)+''') ');
      sql.add(') AA');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmDJBCKKCList_PBSH.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('坯布损耗',Tv1,'待检布仓库');
  Close;
end;

procedure TfrmDJBCKKCList_PBSH.FormShow(Sender: TObject);
begin
  ReadCxGrid('坯布损耗',Tv1,'待检布仓库');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  canshu1:=trim(DParameters1);
  InitGrid();
end;

procedure TfrmDJBCKKCList_PBSH.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 TfrmDJBCKKCList_PBSH.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

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

procedure TfrmDJBCKKCList_PBSH.SXBatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDJBCKKCList_PBSH.MPRTCodeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDJBCKKCList_PBSH.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult:=1;
end;

procedure TfrmDJBCKKCList_PBSH.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('待检布库存',cxGrid2);
end;

procedure TfrmDJBCKKCList_PBSH.OrderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select AA.*,');
      sql.add(' PBSH=(case when TPQty<>0 then (TPQty-HCQty)/TPQty*100 else 0 end) ');
      sql.add(' from (select A.*,B.StyleNo,B.PRTColorNo,B.PRTHX,B.PRTColor,B.PRTColorEng,B.PRTOrderQty, ');
      sql.add(' TPPS=(select Sum(C.PiQty) from CK_SXPB_CR C left join CK_SXPB_CR G on G.SPID=C.FZSPID where C.ORDSubIdCK=B.SubId and C.CRFlag=''出库'' and C.CKName=''坯布'' and G.CRType<>''加工完成''),');
      sql.add(' TPQty=(select Sum(C.Qty) from CK_SXPB_CR C left join CK_SXPB_CR G on G.SPID=C.FZSPID where C.ORDSubIdCK=B.SubId and C.CRFlag=''出库'' and C.CKName=''坯布'' and G.CRType<>''加工完成''),');
      sql.add(' HCPS=(select Sum(C.PiQty) from CK_SXPB_CR C where C.OrdSubIdRK=B.SubId and C.CRFlag=''入库'' and C.CKName=''待检布''),');
      sql.add(' HCQty=(select Sum(C.Qty) from CK_SXPB_CR C where C.OrdSubIdRK=B.SubId and C.CRFlag=''入库'' and C.CKName=''待检布''),');
      sql.add(' KHConNoHZ=[dbo].[F_Get_Order_SubStr](A.MainId,''KHConNoHZ''), ');
      sql.add(' ConNoHZ=[dbo].[F_Get_Order_SubStr](A.MainId,''ConNoHZ'') ');
      sql.Add(' from JYOrder_Main A ');
      sql.add(' inner join JYOrder_Sub B on B.Mainid=A.Mainid ');
      sql.add(' where A.OrderNo like '''+'%'+trim(OrderNo.Text)+'%'+''' ');
      if trim(canshu1)<>'高权限' then
      sql.add(' and (A.Filler='''+Trim(DName)+''' or A.LiDanPerson='''+Trim(DName)+''' or A.YWY='''+Trim(DName)+''') ');
      sql.add(') AA');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  end;
end;

end.
