unit U_PBTPTJLIST;

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,
  cxCurrencyEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmPBTPTJList = 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;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: 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;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label7: TLabel;
    OrderNo: TEdit;
    ToolButton2: TToolButton;
    Label14: TLabel;
    SPCode: TEdit;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    ToolButton3: TToolButton;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column22: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: 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 FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPBTPTJList: TfrmPBTPTJList;

implementation

uses
  U_DataLink, U_RTFun, U_SXCKInPut;

{$R *.dfm}

procedure TfrmPBTPTJList.FormDestroy(Sender: TObject);
begin
  frmPBTPTJList := nil;
end;

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

procedure TfrmPBTPTJList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,ZORDERQTY=(select Sum(PRTOrderQty) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId) ');
      sql.Add(',ZTPPS=(select Sum(TPPS) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId)');
      sql.Add(',ZTPQTY=(select Sum(TPqty) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId)');
      sql.Add(',ZPBRKPS=(select Sum(PiQty) from CK_SXPB_CR C where C.ORDMainIdRK=A.MainId  and CKName=''坯布'' and CRFlag=''入库'' group by  C.ORDMainIdRK)');
      sql.Add(',ZPBRKqty=(select Sum(Qty) from CK_SXPB_CR C where C.ORDMainIdRK=A.MainId  and CKName=''坯布'' and CRFlag=''入库'' group by  C.ORDMainIdRK)');
      sql.Add(',KCPS=isnull((select Sum(PiQty) from CK_SXPB_CR C where C.ORDMainIdRK=A.MainId  and CKName=''坯布'' and CRFlag=''入库'' group by  C.ORDMainIdRK),0)-isnull((select Sum(TPPS) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId),0)');
      sql.Add(',kcqty=isnull((select Sum(Qty) from CK_SXPB_CR C where C.ORDMainIdRK=A.MainId  and CKName=''坯布'' and CRFlag=''入库'' group by  C.ORDMainIdRK),0)-isnull((select Sum(TPqty) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId),0)');
      sql.Add(' from JYOrder_Main A');

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

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

procedure TfrmPBTPTJList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv1, '坯布仓库0326');
  //InitGrid();
end;

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

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

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

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

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

procedure TfrmPBTPTJList.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

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

procedure TfrmPBTPTJList.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  TcxGridToExcel('坯布库存列表', cxGrid2);
end;

procedure TfrmPBTPTJList.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据/Please choose!', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('SPID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',ZdyNameZ=(select top 1 ZdyNameZ from KH_Zdy_Attachment X where X.ZdyName=A.FactoryName and X.Type=''GYS'' )');
    sql.Add(',ZdyNameZ1=(select top 1 ZdyNameZ from KH_Zdy_Attachment X where X.ZdyName=A.ToFactoryName and X.Type=''GYS'' )');
    sql.Add(',SPEName=(select Note from KH_ZDY X where X.Type=''PBName'' and X.ZDYName=A.SPName )');
    sql.Add(' ,OrderNo=(select OrderNo from JYOrder_Main JM where JM.MainId=A.OrdMainIdRK )');
    sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''坯布'' ');
    SQL.Add(' and A.CRFlag=''入库'' and MXKCQty<>0');
    sql.add(' and  A.SPID in (SELECT distinct SubId FROM TBSubID where Dname=' + quotedstr(Trim(DCode)) + ')');
//    ShowMessage(SQL.Text);
    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\坯布库存列表.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
//    RMVariables['SYRName']:=Trim(SYRName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找Report\坯布库存列表.rmf'), '提示', 0);
  end;
end;

end.

