unit U_SketchManageList;

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, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxContainer,
  cxImage, cxDBEdit, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, StrUtils, cxCheckBox, cxPC, cxMemo, Menus, Math, Clipbrd,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmSketchManageList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    M_HXCODE: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    RMDBPRT: TRMDBDataSet;
    TBView: TToolButton;
    RMDBDataSet1: TRMDBDataSet;
    ClientDataSet1: TClientDataSet;
    v1Column31: TcxGridDBColumn;
    ComboBox1: TComboBox;
    IdFTP1: TIdFTP;
    ToolButton1: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    ClientDataSet2: TClientDataSet;
    RMDBDataSet2: TRMDBDataSet;
    Label4: TLabel;
    COHRNAME: TEdit;
    edit2: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Label7: TLabel;
    XGGORDERNO: TEdit;
    v1Column10: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    edit1: TComboBox;
    Edit3: TEdit;
    ComboBox2: TComboBox;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label5: TLabel;
    Label6: TLabel;
    PopupMenu1: TPopupMenu;
    chkYDD: TCheckBox;
    chkWDD: TCheckBox;
    chkwzb: TCheckBox;
    chkDYZ: TCheckBox;
    chkFXPWWC: TCheckBox;
    chkFDPZ: TCheckBox;
    cbbISSaved: TComboBox;
    Label8: TLabel;
    v1Column19: TcxGridDBColumn;
    ToolButton4: TToolButton;
    TBCopy: TToolButton;
    Panel6: TPanel;
    Label9: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    cxTabControl1: TcxTabControl;
    CheckBox1: TCheckBox;
    N3: TMenuItem;
    v1Column28: TcxGridDBColumn;
    TCBNOR: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    lbl1: TLabel;
    ToolButton5: TToolButton;
    chkslt: TCheckBox;
    Tv1Column3: TcxGridDBColumn;
    Label1: TLabel;
    XGOrderNo: TEdit;
    CheckBox2: TCheckBox;
    Tv1Column4: TcxGridDBColumn;
    ComboBox3: TComboBox;
    Label10: TLabel;
    M_GONGYI: TComboBox;
    Tv1Column5: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure M_HXCODEChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure Picture4DblClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxTabControl1CanClose(Sender: TObject; var ACanClose: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure edit1Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure cbbISSavedChange(Sender: TObject);
    procedure chkYDDClick(Sender: TObject);
    procedure chkwzbClick(Sender: TObject);
    procedure chkWDDClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column15PropertiesCustomClick(Sender: TObject);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure TBCopyClick(Sender: TObject);
    procedure M_HXCODEKeyPress(Sender: TObject; var Key: Char);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TCBNORKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton5Click(Sender: TObject);
    procedure ComboBox3Change(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    FInt, PFInt: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    function DelData(): Boolean;
    procedure InitButton();
    procedure LookImage(FileName: string);
    procedure InitImage();
    { Private declarations }
  public
    canshu1, FCheJian, FCXMG: string;
    FChtType: string;
    fFlileFlag: string;
    CXTJ1, CXZ1: string;
    CXTJ2, CXZ2: string;
    { Public declarations }
  end;

var
  frmSketchManageList: TfrmSketchManageList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_FjList10, U_SketchManage, U_QXGL, U_OrderInPut; //U_ShuttleClothSCInPut2

{$R *.dfm}

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

end;

procedure TfrmSketchManageList.SetStatus();
begin
//  TBEdit.VISIBLE := False;
//  TBDel.Visible := False;
  ToolButton3.Visible := False;
  ToolButton2.Visible := False;
  case cxTabControl1.TabIndex of
    0:
      begin
        tbedit.Visible := true;
        TBDel.Visible := true;
//        ToolButton2.Visible := True;
      end;
    1:
      begin
//        ToolButton3.Visible := True;
      end;

  end;

end;

procedure TfrmSketchManageList.InitButton();
begin

end;

procedure TfrmSketchManageList.TBCloseClick(Sender: TObject);
begin

  WriteCxGrid(Trim(self.Caption), Tv1, '坯布生产管理');
  Close;
end;

procedure TfrmSketchManageList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  fwhere := '';
  Pwhere := SGetFilters(Panel1, 1, 2);
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' AND ' + trim(Pwhere);
  if (Edit1.Text <> '') and (edit2.Text <> '') then
  begin
    fwhere := fwhere + ' AND ' + trim(cxz1) + ' like ' + quotedstr('%' + edit2.Text + '%')
  end;
  if (ComboBox2.Text <> '') and (edit3.Text <> '') then
  begin
    fwhere := fwhere + ' AND ' + trim(cxz2) + ' like ' + quotedstr('%' + edit3.Text + '%');
  end;
  if Trim(cbbISSaved.Text) = '已存档' then
  begin
    fwhere := fwhere + ' AND AA.ISSaved=1  '
  end;
  if Trim(cbbISSaved.Text) = '未存档' then
  begin
    fwhere := fwhere + ' AND AA.ISSaved=0  ';
  end;

  if Trim(M_GONGYI.Text) = '网印' then
  begin
    fwhere := fwhere + ' AND AA.M_GONGYI=''网印''  '
  end;
  if Trim(M_GONGYI.Text) = '数码' then
  begin
    fwhere := fwhere + ' AND AA.M_GONGYI=''数码''  ';
  end;
  if chkYDD.Checked then
//    fwhere := fwhere + ' AND isnull(AA.ORDERNO,'''')<>''''  ';
    fwhere := fwhere + ' AND isnull(DDTYPE2,'''')=''订单''  ';
  if chkWDD.Checked then
   // fwhere := fwhere + ' AND isnull(AA.ORDERNO,'''')=''''  ';
    fwhere := fwhere + ' AND isnull(DDTYPE2,'''')=''打样''  ';
  if chkwzb.Checked then
    fwhere := fwhere + ' AND isnull(AA.G_ZBDATE,'''')=''''  ';

  if chkslt.Checked then
    fwhere := fwhere + ' AND not EXISTS(select X.FilesOther from TP_File X where   X.TFType=''MGSLT'' and  X.WBID=AA.MgId )  ';
  if chkDYZ.Checked then
    fwhere := fwhere + ' AND EXISTS(select X.XMNAME from Sketch_XMAP X where X.XMNAME=''小片'' and  X.MgId=AA.MgId and isnull(X.XMAPSJ,'''')<>''''  and isnull(X.XMWCSJ,'''')='''')  ';

  if chkFXPWWC.Checked then
    fwhere := fwhere + ' AND EXISTS(select X.XMNAME from Sketch_XMAP X where X.XMNAME=''小片'' and  X.MgId=AA.MgId and isnull(X.XMWCSJ,'''')='''' and DATEDIFF(dd,X.XMAPSJ,GETDATE() )>=5)  ';

  if chkFDPZ.Checked then
    fwhere := fwhere + ' AND EXISTS(select X.XMNAME from Sketch_XMAP X where X.XMNAME=''大片'' and  X.MgId=AA.MgId  and isnull(X.XMAPSJ,'''')<>''''  and isnull(X.XMWCSJ,'''')='''')  ';

  if CheckBox2.Checked then
  begin
    fwhere := fwhere + 'AND isnull(AA.G_ZBDATE,'''')<>'''' and  not exists(select *  from Sketch_XMAP X where  X.MgId=AA.MgId )  ';
  end;

  if FCXMG <> '' then
  begin
    fwhere := fwhere + ' AND MGID IN (' + FCXMG + ')    ';
  end;

  if Trim(XGOrderNo.Text) <> '' then
  begin
    fwhere := fwhere + ' AND MGID IN (select distinct Y.MGMainId  from JYOrder_Main X inner join JYOrder_Sub Y on X.MainId=Y.MainId  where X.OrderNo like ' + Quotedstr('%' + Trim(XGOrderNo.Text) + '%') + ')    ';
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select top ' + inttostr(RecordsNumber) + ' temp_row.*     ');                                                              // where Y.MGMainId=temp_row.MgId

      sql.Add(' ,XGOrderno=cast((select distinct X.OrderNO +'' '' from JYOrder_Main X inner join JYOrder_Sub Y on X.MainId=Y.MainId  AND  Y.MGMainId=temp_row.MgId for xml path('''') ) as varchar(1000))     ');
      sql.Add(' ,BQSD=(select top 1 ''查看'' from Sketch_BQSD X where X.HxCode=temp_row.M_HXCODE and  isnull(X.SQKHNAME,'''')<>'''' )   ');

      sql.Add(' ,DTP=(case when isnull(IMG_SJT,'''')<>'''' then ''设计稿'' else '''' end)          ');
      sql.Add(' ,SLT=(select top 1 FilesOther from TP_File X where X.WBID=temp_row.MGID and  X.TFType=''MGSLT'' )          ');

      sql.Add(' , XXMNOTE=Replace(cast((select  isnull(XMNOTE,'''')+'' ;'' ');
      sql.Add(' from  Sketch_XMAP X where X.XMNAME=''小片'' and  X.MgId=temp_row.MgId for xml path('''')) as varchar(500)) ,'';'',char(13))           ');

      sql.Add(' , DXMNOTE=Replace(cast((select  isnull(XMNOTE,'''')+'' ;'' ');
      sql.Add(' from  Sketch_XMAP X where X.XMNAME=''大片'' and  X.MgId=temp_row.MgId for xml path('''')) as varchar(500)) ,'';'',char(13))           ');

      sql.Add(' , XXMAPSJ=Replace(cast((select ''开始：''+convert(varchar(10),X.XMAPSJ,120)+'' 完成：''+isnull(convert(varchar(10),X.XMWCSJ,120),'''')+'' ;'' ');
      sql.Add(' from  Sketch_XMAP X where X.XMNAME=''小片'' and  X.MgId=temp_row.MgId for xml path('''')) as varchar(500)) ,'';'',char(13))           ');
      sql.Add(' , DXMAPSJ=Replace(cast((select ''开始：''+convert(varchar(10),X.XMAPSJ,120)+'' 完成：''+isnull(convert(varchar(10),X.XMWCSJ,120),'''')+'' ;'' ');
      sql.Add(' from  Sketch_XMAP X where X.XMNAME=''大片'' and  X.MgId=temp_row.MgId for xml path('''')) as varchar(500))  ,'';'',char(13))            ');

      sql.Add('from (select row_number() over(order by AA.FILLTIME DESC  ) as rownumber '); //--序号
      sql.Add(',COUNT(1) OVER() AS TotalCount '); //总记录数
      sql.Add(',* from (  ');
      sql.Add(' select A.*,M_DBSPEC1=(M_DBSPEC+''/''+isnull(M_DBNAME,''''))   ');
      sql.Add(' ,country= (select TOP 1 COAREA from Company C WHERE COTYPE=''客户'' and valid=''Y'' AND C.CONAME=A.M_KHNAME )  ');
      sql.Add(' ,COHRNAME=ISNULL((select top 1 COHRNAME from COMPANY X where X.CONAME=A.M_KHNAME and COTYPE=''客户'' AND VALID=''Y'' ),M_KHNAME)   ');
      sql.Add(' ,XGGOrderNo=orderno+'';''+cast((select distinct X.OrderNO +'' '' from JYOrder_Main X inner join JYOrder_Sub Y on X.MainId=Y.MainId  AND Y.MGMainId=A.MGID for xml path('''') ) as varchar(1000))     ');
      SQL.Add(',DDTYPE2=(CASE WHEN (SELECT COUNT(MGMainId) FROM JYOrder_Sub Y WHERE  Y.MGMainId=a.MgId AND MGMainId IS NOT NULL)>0 THEN ''订单'' else M_TYPE end)	');
      SQL.Add(' FROM Sketch_MAIN A');
      sql.Add(' where 1=1 ');
      if CheckBox1.Checked then
      begin
        if ComboBox3.Text = '制单时间' then
        begin
          sql.Add('AND A.FILLTIME>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
          sql.Add('and A.FILLTIME<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
        end
        else
        begin
          sql.Add('AND A.G_ZBDATE>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
          sql.Add('and A.G_ZBDATE<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
        end;

      end;
      sql.Add(' )  AA where 1=1 ');
      sql.Add(fwhere);
      sql.Add(') temp_row');
      sql.Add(' where rownumber> ' + inttostr((CurrentPage - 1) * RecordsNumber));
//     ShowMessage(SQL.Text);
      Open;
    end;
//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Filtered := False;
//      Close;
//      sql.Clear;
//      sql.Add(' select top ' + inttostr(RecordsNumber) + ' temp_row.*   FROM (  ');
//
//      SQL.Add('select a.*');
//                                                                  // where Y.MGMainId=temp_row.MgId
//      sql.Add(',DDTYPE2=(CASE WHEN (SELECT COUNT(MAINID) FROM JYOrder_Sub Y WHERE  Y.MGMainId=a.MgId)>0 THEN ''订单'' else M_TYPE end)');
//      sql.Add(' ,XGOrderno=cast((select distinct X.OrderNO +'' '' from JYOrder_Main X inner join JYOrder_Sub Y on X.MainId=Y.MainId  AND  Y.MGMainId=a.MgId for xml path('''') ) as varchar(1000))     ');
//      sql.Add(' ,XGGOrderNo=orderno+'';''+cast(ISNULL((select distinct X.OrderNO +'' '' from JYOrder_Main X inner join JYOrder_Sub Y on X.MainId=Y.MainId  AND Y.MGMainId=A.MGID for xml path('''') ),'''') as varchar(1000))     ');
//      sql.Add(' ,BQSD=(select top 1 ''查看'' from Sketch_BQSD X where X.HxCode=a.M_HXCODE and  isnull(X.SQKHNAME,'''')<>'''' )   ');
//      sql.Add(' ,DTP=(case when isnull(IMG_SJT,'''')<>'''' then ''设计稿'' else '''' end)          ');
//      sql.Add(' ,SLT=(select top 1 FilesOther from TP_File X where X.WBID=a.MGID and  X.TFType=''MGSLT'' )          ');
//
//      sql.Add(' , XXMNOTE=Replace(cast((select  isnull(XMNOTE,'''')+'' ;'' ');
//      sql.Add(' from  Sketch_XMAP X where X.XMNAME=''小片'' and  X.MgId=a.MgId for xml path('''')) as varchar(500)) ,'';'',char(13))           ');
//
//      sql.Add(' , DXMNOTE=Replace(cast((select  isnull(XMNOTE,'''')+'' ;'' ');
//      sql.Add(' from  Sketch_XMAP X where X.XMNAME=''大片'' and  X.MgId=a.MgId for xml path('''')) as varchar(500)) ,'';'',char(13))           ');
//
//      sql.Add(' , XXMAPSJ=Replace(cast((select ''开始：''+convert(varchar(10),X.XMAPSJ,120)+'' 完成：''+isnull(convert(varchar(10),X.XMWCSJ,120),'''')+'' ;'' ');
//      sql.Add(' from  Sketch_XMAP X where X.XMNAME=''小片'' and  X.MgId=a.MgId for xml path('''')) as varchar(500)) ,'';'',char(13))           ');
//      sql.Add(' , DXMAPSJ=Replace(cast((select ''开始：''+convert(varchar(10),X.XMAPSJ,120)+'' 完成：''+isnull(convert(varchar(10),X.XMWCSJ,120),'''')+'' ;'' ');
//      sql.Add(' from  Sketch_XMAP X where X.XMNAME=''大片'' and  X.MgId=a.MgId for xml path('''')) as varchar(500))  ,'';'',char(13))            ');
//      sql.Add(',row_number() over(order by A.FILLTIME DESC  ) as rownumber');
////      sql.Add('from (select row_number() over(order by A.FILLTIME DESC  ) as rownumber '); //--序号
//      sql.Add(',COUNT(1) OVER() AS TotalCount '); //总记录数
//      SQL.Add(' FROM Sketch_MAIN A');
//      sql.Add(' where 1=1 ');
//      SQL.Add(')temp_row ');
//      sql.Add(' where rownumber> ' + inttostr((CurrentPage - 1) * RecordsNumber));
//      if CheckBox1.Checked then
//      begin
//        sql.Add('AND FILLTIME>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
//        sql.Add('and FILLTIME<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
//      end;
//
//      sql.Add(fwhere);
////      sql.Add(') temp_row');
////      sql.Add(' where rownumber> ' + inttostr((CurrentPage - 1) * RecordsNumber));
//      ShowMessage(SQL.Text);
//      Open;
//    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);

    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    lbl1.Caption := '记录条数:' + Order_Main.FieldByName('TotalCount').AsString;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSketchManageList.InitForm();
begin
// InitButton();
  CurrentPage := 1;
  RecordsNumber := 30;
  TCBNOR.Text := '30';
  fFlileFlag := UserDataFlag + 'HX';
  ReadCxGrid(Trim(self.Caption), Tv1, '坯布生产管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 30;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  SetStatus();
  InitGrid();
end;

procedure TfrmSketchManageList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmSketchManage := TfrmSketchManage.Create(Application);
    with frmSketchManage do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MGID').AsString);
      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin
        InitGrid();
        Self.Order_Main.Locate('MGID', frmSketchManage.FMainId, []);
      end;
    end;
  finally
    frmSketchManage.Free;
  end;
end;

procedure TfrmSketchManageList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.FieldByName('G_ZBDATE').AsString <> '' then
  begin
    Application.MessageBox('已经有制版日期，不能删除！', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmSketchManageList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Sketch_main where MGID=''' + Trim(Order_Main.fieldbyname('MGID').AsString) + '''');

      ExecSQL;
    end;

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

procedure TfrmSketchManageList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel('坯布生产单', cxGrid1);
end;

procedure TfrmSketchManageList.TBPrintClick(Sender: TObject);
var
  fPrintFile, FWeiZhi: string;
  i, j, k: integer;
begin
  if Order_Main.IsEmpty then
    Exit;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(ComboBox1.Text) + '.rmf';

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Analysis_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet1);
  SInitCDSData20(ADOQueryTemp, ClientDataSet1);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Analysis_BJ where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, ClientDataSet2);
  SInitCDSData20(ADOQueryTemp, ClientDataSet2);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Analysis_YL where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['MainId'] := ClientDataSet1.fieldbyname('MainId').Value;
    RMVariables['BBID'] := ClientDataSet1.fieldbyname('BBID').Value;
    RMVariables['B_FactoryName'] := ClientDataSet1.fieldbyname('B_FactoryName').Value;
    RMVariables['B_YWY'] := ClientDataSet1.fieldbyname('B_YWY').Value;
    RMVariables['B_YWYBM'] := ClientDataSet1.fieldbyname('B_YWYBM').Value;
    RMVariables['B_QDTIME'] := ClientDataSet1.fieldbyname('B_QDTIME').Value;
    RMVariables['B_SZ'] := ClientDataSet1.fieldbyname('B_SZ').Value;
    RMVariables['B_GCPM'] := ClientDataSet1.fieldbyname('B_GCPM').Value;
    RMVariables['B_MLCF'] := ClientDataSet1.fieldbyname('B_MLCF').Value;
    RMVariables['B_CPKZ'] := ClientDataSet1.fieldbyname('B_CPKZ').Value;
    RMVariables['B_KCMF'] := ClientDataSet1.fieldbyname('B_KCMF').Value;
    RMVariables['B_PBPRICE'] := ClientDataSet1.fieldbyname('B_PBPRICE').Value;
    RMVariables['B_CPPRICE'] := ClientDataSet1.fieldbyname('B_CPPRICE').Value;
    RMVariables['FILLER'] := ClientDataSet1.fieldbyname('FILLER').Value;
    RMVariables['FILLTIME'] := ClientDataSet1.fieldbyname('FILLTIME').Value;
    RMVariables['B_NOTE'] := ClientDataSet1.fieldbyname('B_NOTE').Value;
    RMVariables['BBTYPE'] := ClientDataSet1.fieldbyname('BBTYPE').Value;
    RMVariables['YPFILE'] := ClientDataSet1.fieldbyname('YPFILE').Value;
    RMVariables['B_BYXX'] := ClientDataSet1.fieldbyname('B_BYXX').Value;
    RMVariables['B_kz'] := ClientDataSet1.fieldbyname('B_kz').Value;
    RMVariables['B_CF'] := ClientDataSet1.fieldbyname('B_CF').Value;
    RMVariables['B_SJJC'] := ClientDataSet1.fieldbyname('B_SJJC').Value;
    RMVariables['B_SYCF'] := ClientDataSet1.fieldbyname('B_SYCF').Value;
    RMVariables['B_SYKZ'] := ClientDataSet1.fieldbyname('B_SYKZ').Value;

    RMVariables['YL_NAME'] := CDS_PRT.fieldbyname('YL_NAME').Value;
    RMVariables['YL_BILI'] := CDS_PRT.fieldbyname('YL_BILI').Value;

    RMVariables['BJ_NAME'] := ClientDataSet2.fieldbyname('BJ_NAME').Value;
    RMVariables['BJ_PRICE'] := ClientDataSet2.fieldbyname('BJ_PRICE').Value;
    RMVariables['BJ_MF'] := ClientDataSet2.fieldbyname('BJ_MF').Value;
    RMVariables['BJ_QTY'] := ClientDataSet2.fieldbyname('BJ_QTY').Value;
    RMVariables['BJ_MONEY'] := ClientDataSet2.fieldbyname('BJ_MONEY').Value;

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

procedure TfrmSketchManageList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  SetStatus();
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmSketchManageList.TBAddClick(Sender: TObject);
begin

  try
    frmSketchManage := TfrmSketchManage.Create(Application);
    with frmSketchManage do
    begin
      PState := 0;
      FMainId := '';
      FSubId := '';
      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmSketchManage.Free;
  end;
end;

procedure TfrmSketchManageList.M_HXCODEChange(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 TfrmSketchManageList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmSketchManageList.TBViewClick(Sender: TObject);
begin
  try
    frmSketchManage := TfrmSketchManage.Create(Application);
    with frmSketchManage do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MGID').AsString);
      ftype := '查看';
      canshu1 := Self.canshu1;
      
      
//      TBSave.Visible := False;
//      ToolBar3.Visible := False;
//      ScrollBox1.Enabled := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmSketchManage.Free;
  end;
end;

procedure TfrmSketchManageList.Picture4DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(ADOQueryImage.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmSketchManageList.LookImage(FileName: string);
var
  sFieldName: string;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  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

//    Panel2.Caption := '正在下载数据，请稍等...';
//    Panel2.Visible := true;
    application.ProcessMessages;
    try
//      ShowMessage(PChar(fFlileFlag + '\' + Trim(FileName)));
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
//      Panel2.Visible := false;
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
//    Panel2.Visible := false;
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
//  Panel2.Visible := false;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmSketchManageList.InitImage();
begin

  ADOQueryImage.Close;
  if Order_Main.IsEmpty then
    exit;
//  with ADOQueryImage do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from TP_File A');
//    sql.Add('where mgid=' + quotedstr(trim(Order_Main.fieldbyname('mgid').AsString)));
//    sql.Add('and TFType=' + quotedstr(trim('寻样')));
//    open;
//  end;
end;

procedure TfrmSketchManageList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmFjList10 := TfrmFjList10.Create(Application);
    with frmFjList10 do
    begin

      fkeyno := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      fType := '分析单';
      if ShowModal = 1 then
      begin

      end;
      InitGrid();
    end;
  finally
    frmFjList10.Free;
  end;
end;

procedure TfrmSketchManageList.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if TV1.Controller.FocusedColumn.DataBinding.FilterFieldName = 'DTP' then
  begin

    if ACellViewInfo.Value = '设计稿' then
      LookImage(Trim(Self.Order_Main.fieldbyname('IMG_SJT').AsString));
  end;
  if TV1.Controller.FocusedColumn.DataBinding.FilterFieldName = 'BQSD' then
  begin
    if ACellViewInfo.Value = '查看' then
    begin

      try
        frmQXGL := TfrmQXGL.Create(Application);
        with frmQXGL do
        begin
          FM_HXCODE := Trim(Self.Order_Main.fieldbyname('M_HXCODE').AsString);

          if ShowModal = 1 then
          begin

          end;
        end;
      finally
        frmQXGL.Free;
      end;
    end;
  end;
end;

procedure TfrmSketchManageList.FormDestroy(Sender: TObject);
begin
  frmSketchManageList := nil;
end;

procedure TfrmSketchManageList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Sketch_main SET SHSTATUS=''已审核'',SHtime=getdate(),SHPERSON=' + quotedstr(Trim(DName)));
      sql.Add('where MGID=' + quotedstr(trim(Order_Main.fieldbyname('MGID').AsString)));

      execsql;
    end;
    application.MessageBox('已完成！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmSketchManageList.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Sketch_main SET SHSTATUS=''未审核'',SHtime=NULL,SHPERSON=NULL');
      sql.Add('where MGID=' + quotedstr(trim(Order_Main.fieldbyname('MGID').AsString)));

      execsql;
    end;
    application.MessageBox('操作成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmSketchManageList.cxTabControl1CanClose(Sender: TObject; var ACanClose: Boolean);
begin
  SetStatus();
  InitGrid();
end;

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

procedure TfrmSketchManageList.edit1Change(Sender: TObject);
begin
  CXTJ1 := '';
  CXZ1 := '';

  if edit1.Text <> '' then
  begin
    CXTJ1 := edit1.Text;
    if CXTJ1 = '客户花型' then
    begin
      CXZ1 := 'M_KHHX';
    end
    else if CXTJ1 = '工厂花型' then
    begin
      CXZ1 := 'G_FACTORYHX';
    end
    else if CXTJ1 = '工厂' then
    begin
      CXZ1 := 'G_FACTORYNAME';
    end
    else if CXTJ1 = '花型类型' then
    begin
      CXZ1 := 'M_HXTYPE';
    end
    else if CXTJ1 = '底布规格' then
    begin
      CXZ1 := 'M_DBSPEC';
    end
    else if CXTJ1 = '业务员' then
    begin
      CXZ1 := 'YWY';
    end
    else if CXTJ1 = '制版公司编码' then
    begin
      CXZ1 := 'G_ZBFACTORYCODE';
    end
    else if CXTJ1 = '制版公司' then
    begin
      CXZ1 := 'G_ZBFACTORYNAME';
    end
    else if CXTJ1 = '手刮样等级' then
    begin
      CXZ1 := 'G_SGYDJ';
    end
    else if CXTJ1 = '制单人' then
    begin
      CXZ1 := 'FILLER';
    end;

  end;

end;

procedure TfrmSketchManageList.ComboBox2Change(Sender: TObject);
begin
  CXTJ2 := '';
  CXZ2 := '';

  if ComboBox2.Text <> '' then
  begin
    CXTJ2 := ComboBox2.Text;
    if CXTJ2 = '客户花型' then
    begin
      CXZ2 := 'M_KHHX';
    end
    else if CXTJ2 = '工厂花型' then
    begin
      CXZ2 := 'G_FACTORYHX';
    end
    else if CXTJ2 = '工厂' then
    begin
      CXZ2 := 'G_FACTORYNAME';
    end
    else if CXTJ2 = '花型类型' then
    begin
      CXZ2 := 'M_HXTYPE';
    end
    else if CXTJ2 = '底布规格' then
    begin
      CXZ2 := 'M_DBSPEC';
    end
    else if CXTJ2 = '业务员' then
    begin
      CXZ2 := 'YWY';
    end
    else if CXTJ2 = '制版公司编码' then
    begin
      CXZ2 := 'G_ZBFACTORYCODE';
    end
    else if CXTJ2 = '制版公司' then
    begin
      CXZ2 := 'G_ZBFACTORYNAME';
    end
    else if CXTJ2 = '手刮样等级' then
    begin
      CXZ2 := 'G_SGYDJ';
    end
    else if CXTJ2 = '制单人' then
    begin
      CXZ2 := 'filler';
    end;

  end;
end;

procedure TfrmSketchManageList.cbbISSavedChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmSketchManageList.chkYDDClick(Sender: TObject);
begin
  if chkYDD.Checked then
    chkWDD.Checked := False;
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmSketchManageList.chkwzbClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmSketchManageList.chkWDDClick(Sender: TObject);
begin
  if chkWDD.Checked then
    chkYDD.Checked := False;
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmSketchManageList.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  try
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Sketch_main SET ISSaved=1,ISSaveDate=getdate()');
        sql.Add('where MGID=' + quotedstr(trim(Order_Main.fieldbyname('MGID').AsString)));
        execsql;
      end;
      with Order_Main do
      begin
        Edit;
        FieldByName('SSel').Value := False;
        FieldByName('ISSaved').Value := True;
        Post;
      end;
    end;
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmSketchManageList.v1Column15PropertiesCustomClick(Sender: TObject);
begin
  Application.MessageBox('已经有制版日期，不能删除！', '提示', 0);
end;

procedure TfrmSketchManageList.Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -1, -1);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmSketchManageList.TBCopyClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmSketchManage := TfrmSketchManage.Create(Application);
    with frmSketchManage do
    begin
      CopyInt := 99;
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MGID').AsString);
      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin
        InitGrid();
        Self.Order_Main.Locate('MGID', frmSketchManage.FMainId, []);
      end;
    end;
  finally
    frmSketchManage.Free;
  end;
end;

procedure TfrmSketchManageList.M_HXCODEKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmSketchManageList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmSketchManageList.BTNPClick(Sender: TObject);
begin
  if CurrentPage < Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmSketchManageList.N3Click(Sender: TObject);
begin

  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmSketchManageList.TCBNORKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    RecordsNumber := StrToIntDef(TCBNOR.Text, 10);
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmSketchManageList.ToolButton5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if order_Main.fieldbyname('frommainid').AsString = '' then
  begin
    application.MessageBox('无订单信息！', '提示信息', 0);
    exit;
  end;
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('frommainid').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ToolButton3.Visible := False;
      ScrollBox1.Enabled := False;
      cxGrid1.Enabled := False;
      if ShowModal = 1 then
      begin

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

procedure TfrmSketchManageList.ComboBox3Change(Sender: TObject);
begin
  InitGrid();
end;

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

end.

