unit U_MLConTract_CGJG;

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, cxGroupBox, ImgList;

type
  TfrmMLConTract_CGJG = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tv1: TcxGridDBTableView;
    Cxgrid1Level1: TcxGridLevel;
    Cxgrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    v1OrdDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1KHNameJC: TcxGridDBColumn;
    Label4: TLabel;
    PopupMenu1: TPopupMenu;
    ToolButton1: TToolButton;
    Label9: TLabel;
    ConNoHZ: TEdit;
    Label12: TLabel;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    v1SYRYWY: TcxGridDBColumn;
    v1ZQty: TcxGridDBColumn;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    DataSource3: TDataSource;
    Order_Print: TClientDataSet;
    v1Column3: TcxGridDBColumn;
    cxGroupBox2: TcxGroupBox;
    Image3: TImage;
    ListView1: TListView;
    Panel12: TPanel;
    Label8: TLabel;
    Button4: TButton;
    Panel13: TPanel;
    Image1: TImage;
    WJName: TEdit;
    Button7: TButton;
    WJPach: TEdit;
    Button8: TButton;
    Panel2: TPanel;
    ODPat: TOpenDialog;
    ImageList1: TImageList;
    IdFTP2: TIdFTP;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    CheckBox1: TCheckBox;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    v1ChkZT: TcxGridDBColumn;
    Label3: TLabel;
    Chkstatus: TComboBox;
    Panel3: TPanel;
    Image5: TImage;
    Button6: TButton;
    Button2: TButton;
    Button5: TButton;
    KHNameHZ: TEdit;
    v1Ssel: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure Image3Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Image4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Image5Click(Sender: TObject);
    procedure KHNameHZKeyPress(Sender: TObject; var Key: Char);
    procedure ChkstatusChange(Sender: TObject);
    procedure BegDateChange(Sender: TObject);
    procedure KHNameHZChange(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure SetStatus();
    procedure Initimage();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, canshu3: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmMLConTract_CGJG: TfrmMLConTract_CGJG;

implementation

uses
  U_DataLink, U_MLOrderInPut, U_Fun, U_FjList_RZ
  ,U_MLContractInPut_PB



  ,U_MLOrderInPut_SQJH,
  U_KHList_CX, U_ZDYHelp_YW, U_MLOrderInPut_JGNX, U_MLOrderInPut_XSWX,
  U_MLOrderInPut_JGWX;
{$R *.dfm}

procedure TfrmMLConTract_CGJG.Initimage();
var
  ListItem: TListItem;
  Flag: Cardinal;
  info: SHFILEINFOA;
  Icon: TIcon;
begin
  ListView1.Items.Clear;
  with adoqueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select fileName from TP_File ');
    sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('CJID').AsString) + ''' ');
    sql.Add('and TFType=''面料采购计划''');
    open;
    if not IsEmpty then
    begin
      while not eof do
      begin
        with ListView1 do
        begin
          LargeImages := ImageList1;
          Icon := TIcon.Create;
          ListItem := Items.Add;
          ListItem.Caption := trim(fieldbyname('fileName').AsString);
          Flag := (SHGFI_LARGEICON or SHGFI_ICON or SHGFI_USEFILEATTRIBUTES);
          SHGetFileInfo(Pchar(trim(fieldbyname('fileName').AsString)), 0, info, Sizeof(info), Flag);
          Icon.Handle := info.hIcon;
          ImageList1.AddIcon(Icon);
          ListItem.ImageIndex := ImageList1.Count - 1;
        end;
        next;
      end;
    end;
  end;
end;

procedure TfrmMLConTract_CGJG.SetStatus();
begin
  tchk.Visible := false;
  tnochk.Visible := false;
  TBPrint.Visible := false;
  N5.Visible := true;
  N7.Visible := true;
  if (Trim(canshu1) = '高权限') and (trim(canshu2) = '') then
  begin
    tchk.Visible := true;
    tnochk.Visible := true;
  end
  else if (Trim(canshu1) = '高权限') and (trim(canshu2) = '查询') then
  begin
    tbadd.Visible := false;
    N5.Visible := False;
    N7.Visible := false;
  end;
end;

procedure TfrmMLConTract_CGJG.FormDestroy(Sender: TObject);
begin
  frmMLConTract_CGJG := nil;
end;

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

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

procedure TfrmMLConTract_CGJG.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('采购加工计划YX', Tv1, '生产指示单管理');
end;

procedure TfrmMLConTract_CGJG.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Pwhere := SGetFilters(Panel1, 1, 2);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.add('select  * from ( ');
      sql.add('select A.*,B.KHNameJC ');
      sql.Add(',KHNameHZ=isnull(B.KHNameJC,'''')+'';''+isnull(B.KHName,'''')+'';''+isnull([dbo].[getPinYin](B.KHName),'''')+'';''+B.KHNo');
      sql.Add(',SYRNameJC=(select Top 1 B.KHNameJC from KH_Main B where B.KHNo=A.GSNO)');
      sql.Add(' from ML_CGJG_Plan A ');
      sql.Add(' left join KH_Main B on A.GYSNO=B.KHNO');
      SQL.Add(' where 1=1');
      if CheckBox1.Checked = true then
      begin
        BegDate.Visible := true;
        enddate.Visible := true;
        sql.Add('and A.CJDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + ''' ');
        sql.Add('and A.CJDate<''' + Trim(FormatDateTime('yyyy-MM-dd', endDate.DateTime + 1)) + ''' ');
      end
      else
      begin
        BegDate.Visible := false;
        enddate.Visible := false;
      end;
      if canshu1 <> '高权限' then
      begin
        SQL.Add(' and A.Filler=''' + trim(DName) +  '''');
      end;

      case cxTabControl1.TabIndex of
        1:
          begin
            sql.Add(' and isnull(A.CJType,'''')=''坯布采购''');
          end;
        2:
          begin
            sql.Add(' and isnull(A.CJType,'''')=''成品采购''');
          end;
        3:
          begin
            sql.Add(' and isnull(A.CJType,'''')=''库存调用''');
          end;
      end;

      sql.Add(' and A.ZFFlag=0 ');

      sql.add(' ) AA');
      if Pwhere <> '' then
        sql.Add(' where ' + Pwhere);

      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLConTract_CGJG.InitForm();
begin
  ReadCxGrid('采购加工计划YX', Tv1, '生产指示单管理');
  BegDate.DateTime := StrToDate(FormatDateTime('yyyy', SGetServerDate10(ADOQueryTemp)) + '-01-01');
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  if trim(DdataBase) = '查询' then
  begin
    TBadd.Visible := false;
  end;

  SetStatus();
  InitGrid();
end;

function TfrmMLConTract_CGJG.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ML_CGJG_Plan  where CJID=''' + Trim(Order_Main.fieldbyname('CJID').AsString) + '''');
      sql.Add('delete ML_CGJG_Plan_MX  where CJID=''' + Trim(Order_Main.fieldbyname('CJID').AsString) + '''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmMLConTract_CGJG.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '采购计划单');
end;

procedure TfrmMLConTract_CGJG.TBPrintClick(Sender: TObject);
begin
  Panel4.Left := (self.Width - Panel4.Width) div 2;
  Panel4.Visible := true;
end;

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

procedure TfrmMLConTract_CGJG.TBAddClick(Sender: TObject);
begin
  Panel3.Visible := true;
end;

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

procedure TfrmMLConTract_CGJG.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName = 'IFFZ' then
  begin
    cxGroupBox2.Visible := true;
    Initimage();
  end
  else
    ToolButton1.Click;
end;

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

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

procedure TfrmMLConTract_CGJG.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main, false);
end;

procedure TfrmMLConTract_CGJG.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmMLConTract_CGJG.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmMLOrderInPut_SQJH := TfrmMLOrderInPut_SQJH.Create(Application);
    with frmMLOrderInPut_SQJH do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MLConID').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      Tv1.OptionsSelection.CellSelect := False;
      canshu1 := self.canshu1;
      canshu2 := self.canshu2;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmMLOrderInPut_SQJH.Free;
  end;
end;

procedure TfrmMLConTract_CGJG.Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND, $F012, 0);
end;

procedure TfrmMLConTract_CGJG.Button1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('Ssel', True, []) = False then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('select A.*,B.* from ML_ConMain A ');
    sql.Add('inner Join ML_ConSub B on A.MLConID=B.MLConID ');
    sql.Add('where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint, Order_Print);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryPrint do
        begin
          Close;
          sql.Clear;
          sql.add('select A.*,B.* from ML_ConMain A ');
          sql.Add('inner Join ML_ConSub B on A.MLConID=B.MLConID ');
          sql.Add('where A.MLConID=''' + Trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
          Open;
        end;
        SInitCDSData20(ADOQueryPrint, Order_Print);
      end;
      next;
    end;
  end;
  Order_Main.EnableControls;

  {try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(ADOQueryPrint.fieldbyname('OrderNo').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp' ;
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;}

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\面料购销合同.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    //RMVariables['SYRName']:=Trim(SYRName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
end;

procedure TfrmMLConTract_CGJG.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

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

procedure TfrmMLConTract_CGJG.TchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Order_Main.Locate('Ssel', True, []) = false then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean = true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  ML_CGJG_Plan SET Chkstatus=''已审核'',chker=''' + trim(DName) + ''',chkTime=getdate() ');
            sql.Add('where CJID=' + quotedstr(trim(Order_Main.fieldbyname('CJID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
    end;
    InitGrid();
    application.MessageBox('订单审核成功！', '提示信息');

  except
    application.MessageBox('订单审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmMLConTract_CGJG.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Order_Main.Locate('Ssel', True, []) = false then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean = true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update ML_CGJG_Plan SET Chkstatus=''未审核'',chker='''',chkTime=null ');
            sql.Add('where CJID=' + quotedstr(trim(Order_Main.fieldbyname('CJID').AsString)));
            execsql;
          end;

        end;
        next;
      end;
    end;
    InitGrid();
    application.MessageBox('订单撤销审核成功！', '提示信息');

  except
    application.MessageBox('订单撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmMLConTract_CGJG.MenuItem1Click(Sender: TObject);
var
  OpenDiaLog: TOpenDialog;
  fFileName: string;
  fFilePath: string;
begin
  OpenDiaLog := TOpenDialog.Create(Self);
  if OpenDiaLog.Execute then
  begin
    fFilePath := OpenDiaLog.FileName;
    fFileName := ExtractFileName(OpenDiaLog.FileName);
    Panel12.Visible := True;
    WJName.Text := Trim(fFileName);
    WJName.Hint := Trim(fFileName);
    WJPach.Text := fFilePath;
    Button8.Visible := False;
    Button4.Visible := True;
    Button7.Visible := True;
    Panel12.Refresh;
  end;
end;

procedure TfrmMLConTract_CGJG.N3Click(Sender: TObject);
var
  fFileName: string;
begin
  if listView1.SelCount < 1 then
    exit;
  fFileName := ListView1.Selected.Caption;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete  from TP_File ');
    sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
    sql.Add('and TFType=''面料合同''');
    sql.Add('and FileName=' + quotedstr(trim(fFileName)));
    execsql;
  end;
  Initimage();
  InitGrid();
end;

procedure TfrmMLConTract_CGJG.N4Click(Sender: TObject);
var
  SaveDialog: TSaveDialog;
  fFileName: string;
  fFilePath: string;
begin
  if listView1.SelCount < 1 then
    exit;
  try
    fFileName := ListView1.Selected.Caption;
    SaveDialog := TSaveDialog.Create(Self);
    SaveDialog.FileName := fFileName;
    if SaveDialog.Execute then
    begin
      Panel2.Caption := '正在保存数据，请稍等...';
      Panel2.Visible := true;
      application.ProcessMessages;
      fFilePath := SaveDialog.FileName;
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', 'FTP地址', '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
          IdFTP1.Get('FJ\' + Trim(fFileName), fFilePath, false, true);
        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;
    end;
  except
    Panel2.Visible := false;
  end;
end;

procedure TfrmMLConTract_CGJG.MenuItem2Click(Sender: TObject);
begin
  if listView1.SelCount < 1 then
    exit;
  Panel12.Visible := True;
  Button4.Visible := False;
  Button7.Visible := False;
  Button8.Visible := True;
  Panel12.Refresh;
  WJName.Text := Trim(ListView1.Selected.Caption);
  WJName.Hint := Trim(ListView1.Selected.Caption);
end;

procedure TfrmMLConTract_CGJG.Image3Click(Sender: TObject);
begin
  cxGroupBox2.Visible := false;
end;

procedure TfrmMLConTract_CGJG.Image1Click(Sender: TObject);
begin
  Panel12.Visible := false;
end;

procedure TfrmMLConTract_CGJG.Button4Click(Sender: TObject);
var
  fFileName: string;
  fFilePath: string;
  maxNo: string;
begin
  try
    adoqueryCmd.Connection.BeginTrans;
    begin
      fFilePath := WJPach.Text;
      fFileName := WJName.Text;
      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select TFId from TP_File ');
        sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
        sql.Add(' and TFType=''面料合同''');
        sql.Add(' and FileName=' + quotedstr(trim(fFileName)));
        open;
      end;
      if ADOQueryCmd.IsEmpty = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('此附件名称已存在，请修改文件名，继续上传！', '提示信息', MB_ICONERROR);
        exit;
      end;
      Panel12.Visible := False;
      Panel2.Caption := '正在上传数据，请稍等...';
      Panel2.Visible := true;
      application.ProcessMessages;

      if GetLSNo(ADOQueryCmd, maxNo, 'MD', 'TP_File', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('delete from TP_File ');
        sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
        sql.Add('and TFType=''面料合同''');
        sql.Add('and FileName=' + quotedstr(trim(fFileName)));
        execsql;
      end;
      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from TP_File ');
        sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
        sql.Add('and TFType=''面料合同''');
        sql.Add('and FileName=' + quotedstr(trim(fFileName)));
        open;
        append;
        fieldbyname('TFID').Value := trim(maxNo);
        fieldbyname('WBID').Value := trim(Order_Main.fieldbyname('MLConID').AsString);
        fieldbyname('TFType').Value := '面料合同';
        fieldbyname('FileName').Value := trim(fFileName);
        post;
      end;
      if fFilePath <> '' then
      begin
        try
          IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
          IdFTP1.Username := 'three';
          IdFTP1.Password := '641010';
          IdFTP1.Connect();
          IdFTP1.Put(fFilePath, 'FJ\' + Trim(fFileName));
          IdFTP1.Quit;
        except
          IdFTP1.Quit;
          Application.MessageBox('上传文件失败，请检查文件服务器！', '提示', MB_ICONWARNING);
        end;
      end;
      IdFTP1.Quit;
      Panel2.Visible := false;
      Initimage();
      InitGrid();
    end;
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmMLConTract_CGJG.Button8Click(Sender: TObject);
var
  fFileName: string;
begin
  fFileName := WJName.Hint;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('UPdate TP_File Set FileName=''' + Trim(WJName.Text) + '''');
    sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('CJID').AsString) + '''');
    sql.Add('and TFType=''面料采购计划''');
    sql.Add('and FileName=' + quotedstr(trim(fFileName)));
    execsql;
  end;
  Panel12.Visible := False;
  Initimage();
end;

procedure TfrmMLConTract_CGJG.Button7Click(Sender: TObject);
var
  fFileName: string;
  fFilePath: string;
  maxNo: string;
begin
  try
    adoqueryCmd.Connection.BeginTrans;
    fFilePath := WJPach.Text;
    fFileName := WJName.Hint;
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select TFId from TP_File ');
      sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
      sql.Add('and TFType=''面料合同''');
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      open;
      if not adoqueryCmd.IsEmpty then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('此附件名称已存在，请修改文件名，继续上传！', '提示信息', MB_ICONERROR);
        exit;
      end;
    end;
    Panel12.Visible := False;
    Panel2.Caption := '正在上传数据，请稍等...';
    Panel2.Visible := true;
    application.ProcessMessages;

    if GetLSNo(ADOQueryCmd, maxNo, 'FJ', 'TP_File', 4, 1) = False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！', '提示', 0);
      Exit;
    end;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from TP_File ');
      sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
      sql.Add('and TFType=''面料合同''');
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      execsql;
    end;
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from TP_File ');
      sql.Add('where WBID=''' + trim(Order_Main.fieldbyname('MLConID').AsString) + '''');
      sql.Add('and TFType=''面料合同''');
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      open;
      append;
      fieldbyname('TFID').Value := trim(maxNo);
      fieldbyname('WBID').Value := trim(Order_Main.fieldbyname('MLConID').AsString);
      fieldbyname('TFType').Value := '面料合同';
      fieldbyname('FileName').Value := trim(fFileName);
      post;
    end;
    if fFilePath <> '' then
    begin
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
        IdFTP1.Username := 'three';
        IdFTP1.Password := '641010';
        IdFTP1.Connect();
        IdFTP1.Put(fFilePath, 'FJ\' + Trim(fFileName));
        IdFTP1.Quit;
      except
        IdFTP1.Quit;
        Application.MessageBox('上传文件失败，请检查文件服务器！', '提示', MB_ICONWARNING);
      end;
    end;
    IdFTP1.Quit;
    Panel2.Visible := false;
    Initimage();
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmMLConTract_CGJG.Image4Click(Sender: TObject);
begin
  Panel3.Visible := false;
end;

procedure TfrmMLConTract_CGJG.Button2Click(Sender: TObject);
begin
  try
    frmMLOrderInPut_JGNX := TfrmMLOrderInPut_JGNX.Create(Application);
    with frmMLOrderInPut_JGNX do
    begin
      PState := 0;
      FMainId := '';
      canshu1 := self.canshu1;
      canshu2 := self.canshu2;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmMLOrderInPut_JGNX.Free;
  end;
  Panel3.Visible := false;
end;

procedure TfrmMLConTract_CGJG.N5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if ((Trim(DParameters1) <> '高权限') and (UpperCase(Trim(Order_Main.fieldbyname('Filler').AsString)) <> UpperCase(Trim(DName)))) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  if Trim(Self.Order_Main.fieldbyname('CJType').AsString) = '坯布采购' then
  begin
    try
      frmMLContractInPut_PB := TfrmMLContractInPut_PB.Create(Application);
      with frmMLContractInPut_PB do
      begin
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('CJID').AsString);
        canshu1 := self.canshu1;
        canshu2 := self.canshu2;
        if ShowModal = 1 then
        begin
          initgrid();
        end;
      end;
    finally
      frmMLContractInPut_PB.Free;
    end;
  end else
  if Trim(Self.Order_Main.fieldbyname('ConType').AsString) = '加工内销' then
  begin
    try
      frmMLOrderInPut_JGNX := TfrmMLOrderInPut_JGNX.Create(Application);
      with frmMLOrderInPut_JGNX do
      begin
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('MLConID').AsString);
        canshu1 := self.canshu1;
        canshu2 := self.canshu2;
        if ShowModal = 1 then
        begin
          initgrid();
        end;
      end;
    finally
      frmMLOrderInPut_JGNX.Free;
    end;
  end else
  if Trim(Self.Order_Main.fieldbyname('ConType').AsString) = '销售外销' then
  begin
    try
      frmMLOrderInPut_XSWX := TfrmMLOrderInPut_XSWX.Create(Application);
      with frmMLOrderInPut_XSWX do
      begin
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('MLConID').AsString);
        canshu1 := self.canshu1;
        canshu2 := self.canshu2;
        if ShowModal = 1 then
        begin
          initgrid();
        end;
      end;
    finally
      frmMLOrderInPut_XSWX.Free;
    end;
  end else
  if Trim(Self.Order_Main.fieldbyname('ConType').AsString) = '加工外销' then
  begin
    try
      frmMLOrderInPut_JGWX := TfrmMLOrderInPut_JGWX.Create(Application);
      with frmMLOrderInPut_JGWX do
      begin
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('MLConID').AsString);
        canshu1 := self.canshu1;
        canshu2 := self.canshu2;
        if ShowModal = 1 then
        begin
          initgrid();
        end;
      end;
    finally
      frmMLOrderInPut_JGWX.Free;
    end;
  end;

end;

procedure TfrmMLConTract_CGJG.N6Click(Sender: TObject);
begin
  Panel4.Left := (self.Width - Panel4.Width) div 2;
  Panel4.Visible := true;
end;

procedure TfrmMLConTract_CGJG.N7Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if (UpperCase(Trim(Order_Main.fieldbyname('Filler').AsString)) <> UpperCase(Trim(DName))) and (Trim(canshu1) <> '高权限') then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    InitGrid();
  end;
end;

procedure TfrmMLConTract_CGJG.Button6Click(Sender: TObject);
begin
  try
    frmMLContractInPut_PB := TfrmMLContractInPut_PB.Create(Application);
    with frmMLContractInPut_PB do
    begin
      PState := 0;
      FMainId := '';
      canshu1 := self.canshu1;
      canshu2 := self.canshu2;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmMLContractInPut_PB.Free;
  end;
  Panel3.Visible := false;
end;

procedure TfrmMLConTract_CGJG.ListView1DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
begin
  if ListView1.Items.Count < 1 then
    EXIT;

  if listView1.SelCount < 1 then
    exit;
  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

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

  fileName := ListView1.Selected.Caption;

  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
      IdFTP1.Get('FJ\' + 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 TfrmMLConTract_CGJG.Button5Click(Sender: TObject);
begin
  try
    frmMLOrderInPut_XSWX := TfrmMLOrderInPut_XSWX.Create(Application);
    with frmMLOrderInPut_XSWX do
    begin
      PState := 0;
      FMainId := '';
      canshu1 := self.canshu1;
      canshu2 := self.canshu2;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmMLOrderInPut_XSWX.Free;
  end;
  Panel3.Visible := false;
end;

procedure TfrmMLConTract_CGJG.Image5Click(Sender: TObject);
begin
  Panel3.Visible := false;
end;

procedure TfrmMLConTract_CGJG.KHNameHZKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmMLConTract_CGJG.ChkstatusChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMLConTract_CGJG.BegDateChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMLConTract_CGJG.KHNameHZChange(Sender: TObject);
begin
  InitGrid();
end;

end.

