unit U_ContractOldList;

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, cxPC, cxButtonEdit,
  cxTextEdit, cxDropDownEdit;

type
  TfrmContractOldList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Main: TDataSource;
    CDS_Sub: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    PopupMenu2: TPopupMenu;
    N11: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    BuyName: TEdit;
    MPRTCodeName: TEdit;
    ConNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    DS_Sub: TDataSource;
    Label2: TLabel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label3: TLabel;
    Filler: TEdit;
    V2Column3: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V2Column5: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column18: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    v2Column20: TcxGridDBColumn;
    v2Column21: TcxGridDBColumn;
    v2Column22: TcxGridDBColumn;
    N1: TMenuItem;
    CDS_PM: TClientDataSet;
    RMDB_Main: TRMDBDataSet;
    CDS_QZ: TClientDataSet;
    RMDB_QZ: TRMDBDataSet;
    CDS_PJS: TClientDataSet;
    RMDB_JS: TRMDBDataSet;
    CDS_PWS: TClientDataSet;
    RMDB_WS: TRMDBDataSet;
    v2Column23: TcxGridDBColumn;
    v2Column24: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BuyNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv2CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N11Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    FP_Code: string;
    { Public declarations }
  end;

var
  frmContractOldList: TfrmContractOldList;

implementation

uses
  U_DataLink, U_Fun,U_ShuttleClothSCList;

{$R *.dfm}

procedure TfrmContractOldList.FormDestroy(Sender: TObject);
begin
  frmContractOldList:=nil;
end;

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

procedure TfrmContractOldList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption)+'Tv2', Tv2, '产品明细');
  Close;

end;

procedure TfrmContractOldList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_View_ContractOrdList ');
      sql.Add('@P_Code =' + quotedstr(Trim(FP_Code)));
      //showmessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Sub);
    SInitCDSData20(ADOQueryMain, CDS_Sub);


  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmContractOldList.InitForm();
begin
  readCxGrid(Trim(Self.Caption)+'Tv2', Tv2, '产品明细');

  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 90;
  InitGrid();

end;

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

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

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

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

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

procedure TfrmContractOldList.Tv2CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 if AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName('status').Index] = '2' then
    ACanvas.Brush.Color := clLime;
end;

procedure TfrmContractOldList.N11Click(Sender: TObject);
var
  fPrintFile, fPrintFileQZ,fPrintFileYWB: string;
  i, j, k: integer;
begin
  if CDS_Sub.IsEmpty then
    Exit;

    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\织造工艺单.rmf';
    ExportFtErpFile('织造工艺单.rmf', ADOQueryTemp);

    with ADOQueryTemp do
    begin
     // Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select *  ');
      sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');
      sql.Add(' where B.P_Code='+ quotedstr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)));
      sql.Add(' and A.ConNo='+ quotedstr(Trim(CDS_Sub.fieldbyname('ConNO').AsString)));
     //   ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PM);
    SInitCDSData20(ADOQueryTemp, CDS_PM);

    if Trim(ADOQueryTemp.fieldbyname('YWBian').AsString)<> '' then
    begin
      fPrintFileYWB := ExtractFilePath(Application.ExeName) + 'Report\英文边.rmf';
      ExportFtErpFile('英文边.rmf', ADOQueryTemp);
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from PD_Plan_QZ where MainId=(select A.MainId from PD_Plan_Main A ');
      sql.Add(' inner join PD_Plan_Sub B on A.Mainid=B.MainId ');
      sql.Add(' where B.P_Code='+quotedstr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)));
      sql.Add(' and A.ConNo='+ quotedstr(Trim(CDS_Sub.fieldbyname('ConNO').AsString)));
      sql.Add(')');
      //ShowMessage(SQL.Text);
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      SCreateCDS20(ADOQueryTemp, CDS_QZ);
      SInitCDSData20(ADOQueryTemp, CDS_QZ);
      fPrintFileQZ := ExtractFilePath(Application.ExeName) + 'Report\起综工艺单.rmf';
      ExportFtErpFile('起综工艺单.rmf', ADOQueryTemp);
    end;


    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from PD_Plan_Yarn where PYType=''经丝'' and MainId=(select A.MainId from PD_Plan_Main A ');
      sql.Add(' inner join PD_Plan_Sub B on A.Mainid=B.MainId ');
      sql.Add(' where B.P_Code='+quotedstr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)));
      sql.Add(' and A.ConNo='+ quotedstr(Trim(CDS_Sub.fieldbyname('ConNO').AsString)));
      sql.Add(')');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PJS);
    SInitCDSData20(ADOQueryTemp, CDS_PJS);

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from PD_Plan_Yarn where PYType=''纬丝'' and MainId=(select A.MainId from PD_Plan_Main A ');
      sql.Add(' inner join PD_Plan_Sub B on A.Mainid=B.MainId ');
      sql.Add(' where B.P_Code='+quotedstr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)));
      sql.Add(' and A.ConNo='+ quotedstr(Trim(CDS_Sub.fieldbyname('ConNO').AsString)));
      sql.Add(')');
      //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PWS);
    SInitCDSData20(ADOQueryTemp, CDS_PWS);

    if FileExists(fPrintFile)then
    begin
      RM1.LoadFromFile(fPrintFile);
      RM1.ShowReport;
      if FileExists(fPrintFileQZ) then
      begin
        RM1.LoadFromFile(fPrintFileQZ);
        RM1.ShowReport;
      end ;

      if FileExists(fPrintFileYWB) then
        begin
          RM1.LoadFromFile(fPrintFileYWB);
          RM1.ShowReport;
        end;
    end
    else
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    end;

//  try
//    frmShuttleClothSCList := TfrmShuttleClothSCList.Create(Application);
//    with frmShuttleClothSCList do
//    begin
//
//
//      if ShowModal = 1 then
//      begin
//
//      end;
//    end;
//  finally
//    frmShuttleClothSCList.Free;;
//  end;
end;

procedure TfrmContractOldList.N1Click(Sender: TObject);
var
  fPrintFile, fPrintFileQZ,fPrintFileYWB: string;
  i, j, k: integer;
begin
if CDS_Sub.IsEmpty then
    Exit;

    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\整经工艺单.rmf';
    ExportFtErpFile('整经工艺单.rmf', ADOQueryTemp);

    with ADOQueryTemp do
    begin
     // Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*  ');
      sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');
      sql.Add(' where B.P_Code='+ quotedstr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)));
      sql.Add(' and A.ConNo='+ quotedstr(Trim(CDS_Sub.fieldbyname('ConNO').AsString)));
        //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PM);
    SInitCDSData20(ADOQueryTemp, CDS_PM);

    if Trim(ADOQueryTemp.fieldbyname('YWBian').AsString)<> '' then
    begin
      fPrintFileYWB := ExtractFilePath(Application.ExeName) + 'Report\英文边.rmf';
      ExportFtErpFile('英文边.rmf', ADOQueryTemp);
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from PD_Plan_QZ where MainId=(select A.MainId from PD_Plan_Main A ');
      sql.Add(' inner join PD_Plan_Sub B on A.Mainid=B.MainId ');
      sql.Add(' where B.P_Code='+quotedstr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)));
      sql.Add(' and A.ConNo='+ quotedstr(Trim(CDS_Sub.fieldbyname('ConNO').AsString)));
      sql.Add(')');
      //ShowMessage(SQL.Text);
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      SCreateCDS20(ADOQueryTemp, CDS_QZ);
      SInitCDSData20(ADOQueryTemp, CDS_QZ);
      fPrintFileQZ := ExtractFilePath(Application.ExeName) + 'Report\起综工艺单.rmf';
      ExportFtErpFile('起综工艺单.rmf', ADOQueryTemp);
    end;


    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from PD_Plan_Yarn where PYType=''经丝'' and MainId=(select A.MainId from PD_Plan_Main A ');
      sql.Add(' inner join PD_Plan_Sub B on A.Mainid=B.MainId ');
      sql.Add(' where B.P_Code='+quotedstr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)));
      sql.Add(' and A.ConNo='+ quotedstr(Trim(CDS_Sub.fieldbyname('ConNO').AsString)));
      sql.Add(')');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PJS);
    SInitCDSData20(ADOQueryTemp, CDS_PJS);

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from PD_Plan_Yarn where PYType=''纬丝'' and MainId=(select A.MainId from PD_Plan_Main A ');
      sql.Add(' inner join PD_Plan_Sub B on A.Mainid=B.MainId ');
      sql.Add(' where B.P_Code='+quotedstr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)));
      sql.Add(' and A.ConNo='+ quotedstr(Trim(CDS_Sub.fieldbyname('ConNO').AsString)));
      sql.Add(')');
      //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PWS);                                               
    SInitCDSData20(ADOQueryTemp, CDS_PWS);

    if FileExists(fPrintFile)then
    begin
      RM1.LoadFromFile(fPrintFile);
      RM1.ShowReport;
      if FileExists(fPrintFileQZ) then
      begin
        RM1.LoadFromFile(fPrintFileQZ);
        RM1.ShowReport;
      end ;

      if FileExists(fPrintFileYWB) then
        begin
          RM1.LoadFromFile(fPrintFileYWB);
          RM1.ShowReport;
        end;
    end
    else
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    end;
end;

end.

