unit U_ProductOrderSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, cxButtonEdit,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, Menus,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxContainer,
  cxImage, cxDBEdit, StdCtrls, cxPC, cxSplitter, ExtCtrls, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ComCtrls, ToolWin ,ShellAPI,StrUtils,
  cxDropDownEdit, cxTextEdit;
type
  TfrmProductOrderSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomerNoName: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    Label8: TLabel;
    HJName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label11: TLabel;
    HJCode: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    ADOQueryPrint: TADOQuery;
    v1Column1: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    cxTabControl1: TcxTabControl;
    Panel2: TPanel;
    GroupBox1: TGroupBox;
    Picture4: TcxDBImage;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    v1Column11: TcxGridDBColumn;
    Order_Sub: TClientDataSet;
    Label5: TLabel;
    YWY: TEdit;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column6: TcxGridDBColumn;
    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 OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure N1Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Picture4DblClick(Sender: TObject);
    procedure TV3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv1DblClick(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitSub();
    procedure InitImage(fwbid:string);
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    canshu1:string;
    fFlileFlag:string;
    { Public declarations }
  end;

var
  frmProductOrderSel: TfrmProductOrderSel;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmProductOrderSel.InitImage(fwbid:string);
begin
  ADOQueryImage.close;
  IF fwbid='' then exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID='+quotedstr(trim(fwbid)));
    open;
  end;
end;

procedure TfrmProductOrderSel.InitSub();
begin
  ADOQuerySub.Close;
  IF Order_Main.IsEmpty then exit;
  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.* 	from JYOrder_Sub A where');
      sql.Add(' MainId='+quotedstr((Order_Main.fieldbyname('mainID').AsString)));
      open;
    end;
    SCreateCDS20(ADOQuerySub,Order_Sub);
    SInitCDSData20(ADOQuerySub,Order_Sub);
    if Trim(canshu1)='查询' then
    begin
      with Order_Sub do
      begin
        First;
        while not Eof do
        begin
          Edit;
          FieldByName('prtprice').Value:=0;
          Post;
          Next;
        end;
      end;
    end;
  finally
    ADOQuerySub.EnableControls;
  end;
end;

procedure TfrmProductOrderSel.FormDestroy(Sender: TObject);
begin
  frmProductOrderSel:=nil;
end;

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

procedure TfrmProductOrderSel.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
  canshu1:=trim(DParameters1);
end;

procedure TfrmProductOrderSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(trim(self.Caption),Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('select *') ;
      sql.Add(',HJCode=cast((select distinct '','' + X.MPRTCode from JYOrder_Sub X where X.MainId=A.MainId for xml path('''')) as varchar(200))');
      sql.Add(',HJName=cast((select distinct '','' + X.MPRTCodeName from JYOrder_Sub X where X.MainId=A.MainId for xml path('''')) as varchar(200))');
      sql.add(' from JYOrder_Main A') ;
      sql.add(' where Filltime>='+quotedstr(trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime)))) ;
      sql.add(' and Filltime<'+quotedstr(trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)))) ;
      if cxTabControl1.TabIndex<3 then
       sql.add(' and isnull(status,''0'')='+quotedstr(trim(inttostr(cxTabControl1.TabIndex)))) ;
      if Trim(self.Caption)='染整指示单录入'  then
       sql.add(' and Filler='+quotedstr(trim(DName))) ;
//      showmessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderSel.InitForm();
begin
  ReadCxGrid(trim(self.Caption),Tv1,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-30;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmProductOrderSel.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 TfrmProductOrderSel.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' '+quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(','+quotedstr(trim(self.Caption)));
      sql.Add(','+quotedstr(trim('指示单删除')));
      sql.Add(','+quotedstr(trim('指示单号：'+trim(Order_Main.FieldByName('OrderNO').AsString))));
      sql.Add(','+quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

procedure TfrmProductOrderSel.OrderNoChange(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 TfrmProductOrderSel.FormShow(Sender: TObject);
begin
  fFlileFlag:=UserDataFlag+'HX';
  InitForm();
end;

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

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

procedure TfrmProductOrderSel.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id,id10:Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmProductOrderSel.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 TfrmProductOrderSel.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

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

procedure TfrmProductOrderSel.TV2FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  initImage(ADOQuerySub.fieldbyname('HXFile').AsString);
end;

procedure TfrmProductOrderSel.Picture4DblClick(Sender: TObject);
var
  sFieldName:string;
  fileName:string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin
  IF Picture4.Picture.Height=0 then exit;
  sFieldName:=leftbstr(ExtractFilePath(Application.ExeName),1)+':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
  CreateDirectory(pchar(sFieldName),nil);
  fileName:=ADOQuerySub.fieldbyname('hxFile').AsString;
  sFieldName:=sFieldName+'\'+trim(fileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    IdFTP1.Username := 'three';
		IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    
  end;

  if IdFTP1.Connected then
  begin

      application.ProcessMessages;
    try
      IdFTP1.Get(fFlileFlag+'\'+ Trim(fileName), sFieldName,true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
   if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(sFieldName),'', '', SW_SHOWNORMAL);

end;

procedure TfrmProductOrderSel.TV3MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitSub();
end;

procedure TfrmProductOrderSel.Tv1DblClick(Sender: TObject);
begin
 ModalResult:=1;
end;

end.
