unit U_ProductOrderNewList_JD;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxCurrencyEdit, cxImage,
  cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu, cxPC, cxSplitter,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP,ShellAPI,
  cxCheckBox, cxLookAndFeelPainters, cxButtons, cxGridBandedTableView,
  cxGridDBBandedTableView;

type
  TfrmProductOrderNewList_JD = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Label4: TLabel;
    CustomerNoName: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label2: TLabel;
    JGFactoryName: TEdit;
    Label5: TLabel;
    OrdPerson2: TEdit;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label11: TLabel;
    OrdDefStr1: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    cxTabControl1: TcxTabControl;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    Label6: TLabel;
    ISPH: TComboBox;
    PrtColor: TEdit;
    Label7: TLabel;
    orderType: TComboBox;
    Label24: TLabel;
    ToolButton4: TToolButton;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    KHConNO: TEdit;
    Label13: TLabel;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    Panel4: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Column1: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1SOrddefstr2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    GroupBox1: TGroupBox;
    Picture4: TcxDBImage;
    IdFTP1: TIdFTP;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V2Column5: TcxGridDBColumn;
    MPRTCode: TEdit;
    Label17: TLabel;
    RanFactory: TEdit;
    Label18: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBBandedTableView;
    v1Column54: TcxGridDBBandedColumn;
    v1Column55: TcxGridDBBandedColumn;
    v1Column57: TcxGridDBBandedColumn;
    v1Column1: TcxGridDBBandedColumn;
    v1Column2: TcxGridDBBandedColumn;
    v1Column3: TcxGridDBBandedColumn;
    v1Column6: TcxGridDBBandedColumn;
    v1Column7: TcxGridDBBandedColumn;
    v1Column10: TcxGridDBBandedColumn;
    v1Column11: TcxGridDBBandedColumn;
    v1Column14: TcxGridDBBandedColumn;
    v1Column15: TcxGridDBBandedColumn;
    v1Column18: TcxGridDBBandedColumn;
    v1Column19: TcxGridDBBandedColumn;
    v1Column22: TcxGridDBBandedColumn;
    v1Column25: TcxGridDBBandedColumn;
    v1Column26: TcxGridDBBandedColumn;
    v1Column29: TcxGridDBBandedColumn;
    v1Column50: TcxGridDBBandedColumn;
    v1Column9: TcxGridDBBandedColumn;
    v1Column13: TcxGridDBBandedColumn;
    v1Column17: TcxGridDBBandedColumn;
    v1Column30: TcxGridDBBandedColumn;
    v1Column31: TcxGridDBBandedColumn;
    v1Column32: TcxGridDBBandedColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column4: TcxGridDBBandedColumn;
    v1Column5: TcxGridDBBandedColumn;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle3: TcxStyle;
    ToolButton2: TToolButton;
    V2Column6: TcxGridDBColumn;
    V2Column7: TcxGridDBColumn;
    V2Column8: 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 TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Picture4DblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    procedure SetStatus();
    procedure InitSub();
    procedure InitImage(fwbid:string);
        { Private declarations }
  public
    FFInt,FCloth:Integer;
    canshu1:string;
    canshu2:string;
    { Public declarations }
  end;

var
  frmProductOrderNewList_JD: TfrmProductOrderNewList_JD;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun, U_OrderJD;

{$R *.dfm}
procedure TfrmProductOrderNewList_JD.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 TfrmProductOrderNewList_JD.InitSub();
begin
  ADOQuerySub.Close;
  IF Order_Main.IsEmpty then exit;
  ADOQuerySub.DisableControls;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add(' from JYOrder_sub A');
    sql.Add('where mainID ='+quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
  ADOQuerySub.EnableControls;
end;

procedure TfrmProductOrderNewList_JD.SetStatus();
begin

end;

procedure TfrmProductOrderNewList_JD.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList_JD:=nil;
end;

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

procedure TfrmProductOrderNewList_JD.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderNewList_JD.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxBandedGrid(self.Caption+tv1.Name+'2',Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderNewList_JD.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add(' exec P_View_order_Jd') ;
      sql.add(' @begdate='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+''' ');
      sql.add(',@enddate='''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+''' ');
      sql.Add(',@flag='''+inttostr(cxTabControl1.TabIndex)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmProductOrderNewList_JD.InitGridFH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                           +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderNewList_JD.InitForm();
begin
  ReadCxBandedGrid(self.Caption+tv1.Name+'2',Tv1,'生产指示单管理');

  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-15;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
end;

procedure TfrmProductOrderNewList_JD.TBFindClick(Sender: TObject);
var
  strwhere :string;
begin
  if ADOQueryMain.Active=False then Exit;
  strwhere:=SGetFilters(Panel1,1,2);
  
  SDofilter(ADOQueryMain,strwhere);
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmProductOrderNewList_JD.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
{  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from Contract_Cloth_LL A where exists (select * from JYOrder_Sub B where B.SubId=A.OrdSubId  ');
    SQL.Add(' and B.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生数据不能删除!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生数据不能删除!','提示',0);
    Exit;
  end;
  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Cloth_LLMX where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已领料不能删除数据!','提示',0);
    Exit;
  end; }
{  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Sub_AnPai where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已回仓不能删除数据!','提示',0);
    Exit;
  end;              }
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    TBRafresh.Click;
  end;
end;

function TfrmProductOrderNewList_JD.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)+'''');
      ExecSQL;
    end;
    {with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
      if IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;   }
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderNewList_JD.TBExportClick(Sender: TObject);
begin
//  if ADOQueryMain.IsEmpty then Exit;
//  SelExportDataBand(Tv1,ADOQueryMain,'生产指示单列表');
end;

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

procedure TfrmProductOrderNewList_JD.OrderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmProductOrderNewList_JD.FormShow(Sender: TObject);
begin
  canshu1:=trim(DParameters1);
  canshu2:=trim(DParameters2);
  SetStatus();
  InitForm();
end;

procedure TfrmProductOrderNewList_JD.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,FMxQty,FPQty,FMxQtyS,FPQtyS:String;
begin
end;

procedure TfrmProductOrderNewList_JD.CheckBox1Click(Sender: TObject);
begin
   TBFind.Click;
end;

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

procedure TfrmProductOrderNewList_JD.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Enabled:=False;
      TBSave.Visible:=False;
      ScrollBox1.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderNewList_JD.cxTabControl1Change(Sender: TObject);
begin
  application.ProcessMessages;
  initGrid();
end;

procedure TfrmProductOrderNewList_JD.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderJD:=TfrmOrderJD.Create(Application);
    with frmOrderJD do
    begin
      OrderNo.Text:=Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderJD.Free;
  end;
end;

procedure TfrmProductOrderNewList_JD.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

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

procedure TfrmProductOrderNewList_JD.Picture4DblClick(Sender: TObject);
var
  sFieldName:string;
  fileName:string;
  fFlileFlag:string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin
  IF Picture4.Picture.Height=0 then exit;
    fFlileFlag:=UserDataFlag+'HX';
  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 TfrmProductOrderNewList_JD.ToolButton2Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec [P_Update_Order_JD] '+quotedstr(Trim(Order_Main.fieldbyname('mainid').AsString)));
    ExecSQL;
  end;
   InitGrid();
end;

end.
