unit U_ContractListNXSel;

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

type
  TfrmContractListNXSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1MPRTCF: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CustomerNoName: TEdit;
    ConPerson1: TEdit;
    MPRTCodeName: TEdit;
    ConNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    v1Column2: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column8: TcxGridDBColumn;
    Label2: TLabel;
    KHConNO: TEdit;
    Label3: TLabel;
    v1Column10: TcxGridDBColumn;
    //RMllPDFExport1: TRMllPDFExport;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Note: TClientDataSet;
    v2Column2: TcxGridDBColumn;
    RM1: TRMGridReport;
    v1Column22: TcxGridDBColumn;
    Label6: TLabel;
    XSType: TComboBox;
    RMDBDataSet2: TRMDBDataSet;
    v1Column3: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Label7: TLabel;
    PrtCode: TEdit;
    v1Column12: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    ZhiYuan: TComboBox;
    Label35: TLabel;
    v2Column4: TcxGridDBColumn;
    //RMllPDFExport1: TRMllPDFExport;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    DQdate:TDateTime;
    fuserName,canshu1,canshu2:string;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure SetStatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmContractListNXSel: TfrmContractListNXSel;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_FjList_RZ;

{$R *.dfm}

procedure TfrmContractListNXSel.SetStatus();
begin
  
end;


procedure TfrmContractListNXSel.FormDestroy(Sender: TObject);
begin
  frmContractListNXSel:=nil;
end;

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

procedure TfrmContractListNXSel.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmContractListNXSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('内销合同',Tv1,'生产指示单管理');
  WriteCxGrid('内销合同11',Tv2,'生产指示单管理');
end;

procedure TfrmContractListNXSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*  ');
      sql.Add(',Case when isnull((select count(*) from TP_File where WBID=A.MainID and TFType=''合同'' ),0)>0 ');
      sql.Add('then Cast(1 as bit) else Cast(0 as bit) end as FJFlag');
      sql.Add(',PrtCode=cast((select distinct '' ''+ PrtCode from JYOrderCon_Sub_PB JCS where JCS.MainId=A.MainId for xml path('''')) as varchar(120))');
      sql.Add(',PrtCodeName=cast((select distinct '' ''+ PrtCodeName from JYOrderCon_Sub_PB JCS where JCS.MainId=A.MainId  for xml path('''')) as varchar(120))');
      sql.Add(',Case when isnull((select count(*) from Cloth_Main where ConMainId=A.MainID ),0)>0 ');
      sql.Add('then Cast(1 as bit) else Cast(0 as bit) end as JHDFlag');
      sql.Add(',ZdyNameZ=(select X.ZdyNameZ from KH_Zdy_Attachment X where X.Type=''KHName'' and X.ZdyCode=A.CustomerNo)');
      sql.Add(' from JYOrderCon_Main_PB A where status in (''4'',''1'') ');
      sql.Add(' and A.OrdDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and A.OrdDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      SQL.Add(' order by A.OrdDate desc');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;



procedure TfrmContractListNXSel.InitForm();
begin
  ReadCxGrid('内销合同',Tv1,'生产指示单管理');
  ReadCxGrid('内销合同11',Tv2,'生产指示单管理');

  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp);      //-30
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  //InitGrid();
  
end;

procedure TfrmContractListNXSel.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 TfrmContractListNXSel.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Sub_PB where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Main_PB where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmContractListNXSel.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
end;

procedure TfrmContractListNXSel.FormShow(Sender: TObject);
begin
  fuserName:=DCode;
  InitForm();
  SetStatus();
end;

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

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

procedure TfrmContractListNXSel.N1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmContractListNXSel.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 TfrmContractListNXSel.Tv1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  IF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('JHDFlag').Index]=True then
    ACanvas.Brush.Color:=clSkyBlue;
end;

procedure TfrmContractListNXSel.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if Order_Main.IsEmpty=False then
    begin
      sql.Add('select A.* ');
      sql.Add(',CYCL=(select top 1 CYCL from CP_YDang X where X.CYNo=A.PrtCode)');
      sql.Add(',CarType=(select top 1  ZhenXing from CP_YDang X where X.CYNo=A.PrtCode)');
      sql.Add(' from JYOrderCon_Sub_PB A where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    end
    else
      sql.Add('select *,CYCL='''',CarType='''' from JYOrderCon_Sub_PB A where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

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

end.
