unit U_OrderInPut_New;

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, cxGridCustomPopupMenu, cxGridPopupMenu, cxDropDownEdit,
  cxCheckBox, Menus, cxPC, cxLookAndFeels, cxLookAndFeelPainters,
  dxBarBuiltInMenu, cxNavigator, cxButtons, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, jpeg, Buttons, IniFiles;

type
  TfrmOrderInPut_New = class(TForm)
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryTmp: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel1: TPanel;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label10: TLabel;
    Label3: TLabel;
    Label14: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label7: TLabel;
    MLConNo: TBtnEditC;
    MLOrderNo: TEdit;
    OrdDate: TDateTimePicker;
    MLCodeName: TBtnEditC;
    MLCodeNo: TEdit;
    MLBZNote: TMemo;
    YWY: TBtnEditC;
    Label2: TLabel;
    BZType: TComboBox;
    Label8: TLabel;
    MLNote: TMemo;
    Label9: TLabel;
    Label11: TLabel;
    MLMFNote: TEdit;
    JHDate: TDateTimePicker;
    N3: TMenuItem;
    Panel2: TPanel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1MLColor: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1MLColorNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1MLHX: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1MXNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    V2MLName: TcxGridDBColumn;
    V2Note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1Column10: TcxGridDBColumn;
    cxTabSheet3: TcxTabSheet;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    V2MLCode: TcxGridDBColumn;
    V2MF: TcxGridDBColumn;
    V2KZ: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Tv2Column1: TcxGridDBColumn;
    Panel3: TPanel;
    Label12: TLabel;
    ComboBox1: TComboBox;
    Button1: TButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel4: TPanel;
    Label6: TLabel;
    MTMBName: TEdit;
    Label13: TLabel;
    Label15: TLabel;
    MTMHName: TEdit;
    Label16: TLabel;
    KHName: TBtnEditC;
    Label17: TLabel;
    MTMHKZ: TEdit;
    Label18: TLabel;
    MTCPName: TEdit;
    Label19: TLabel;
    MTCPMF: TEdit;
    Label20: TLabel;
    MTCPKZ: TEdit;
    Label21: TLabel;
    MTNote1: TEdit;
    Label22: TLabel;
    MTNote2: TEdit;
    Label23: TLabel;
    MTNote3: TEdit;
    Label24: TLabel;
    MTNote4: TEdit;
    Label25: TLabel;
    MTNote5: TEdit;
    CPBQName: TcxButtonEdit;
    Label26: TLabel;
    CPBaoBQName: TcxButtonEdit;
    Label27: TLabel;
    cxButton1: TcxButton;
    cxButton2: TcxButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    Order_Print: TClientDataSet;
    Label28: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    cxTabSheet4: TcxTabSheet;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Panel5: TPanel;
    Image2: TImage;
    adoqueryPicture: TADOQuery;
    IdFTP1: TIdFTP;
    Panel6: TPanel;
    SpeedButton1: TSpeedButton;
    Image1: TImage;
    SpeedButton6: TSpeedButton;
    ODPat: TOpenDialog;
    Image22: TImage;
    Label29: TLabel;
    Tv2Column2: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure KHNameBtnUpClick(Sender: TObject);
    procedure KHNameBtnDnClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrdDefStr2BtnUpClick(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure YWYBtnUpClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure v1MLHXPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N3Click(Sender: TObject);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure MenuItem1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure CPBQNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CPBaoBQNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Image1Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ReadINIFile10();
    procedure SaveImage(maxnoWB: string; FIMNO: string);
    procedure Tv2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    lstPat, lstPat2: TStringList;
    FangXiang1, FangXiang2: string;
    procedure InitData();
    function SaveData(): Boolean;
    procedure InitPDJL();
    procedure LookImage(FileName: string);

    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo, ISPD: string;
    FXS: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmOrderInPut_New: TfrmOrderInPut_New;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_KHList_CX, U_ZDYHelpSel, U_MLMangeRSSel,
  U_ZdyAttachCP_HX_sel, U_BPZdy_LRM, U_LabelList, U_ZHCPList_Sel, U_GYSList;

{$R *.dfm}
procedure TfrmOrderInPut_New.InitPDJL();
var
  i: Integer;
begin

  i := 0;
  with ADOQueryTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from ML_ConMain A inner join ML_ConSub B on A.MLConID=B.MLConID ');
    sql.Add('where exists(select * from TbForBatch X where X.ConSubId=B.CSID and X.MainType=''生产排单'' and X.DName=' + QUOTEDSTR(Trim(DName)) + '  )');
    Open;
  end;
  KHName.Text := ADOQueryTmp.FieldByName('KHName').AsString;
  MLConNo.Text := ADOQueryTmp.FieldByName('ConNO').AsString;
  MLCodeNo.Text := ADOQueryTmp.FieldByName('ConCode').AsString;
  MLCodeName.Text := ADOQueryTmp.FieldByName('ConCodeName').AsString;
  MLOrderNo.Text := Trim(ADOQueryTmp.FieldByName('YWConNo').AsString) + '-' + Trim(ADOQueryTmp.FieldByName('XHNo').AsString);
  MLOrderNo.Hint := Trim(ADOQueryTmp.FieldByName('CSID').AsString);
  with ADOQueryTmp do
  begin
    First;
    while not Eof do
    begin
      with Order_Sub do
      begin
        Append;
        FieldByName('ConSubId').Value := ADOQueryTmp.FieldByName('CSID').Value;
        FieldByName('XHNO').Value := IntToStr(i + 1);
        FieldByName('MLColor').Value := ADOQueryTmp.FieldByName('ConColor').Value;
        FieldByName('MLColorNo').Value := ADOQueryTmp.FieldByName('ConColNo').Value;
        FieldByName('MLHX').Value := ADOQueryTmp.FieldByName('ConHX').Value;
        FieldByName('QtyUnit').Value := ADOQueryTmp.FieldByName('QtyUnit').Value;
        FieldByName('Qty').Value := ADOQueryTmp.FieldByName('Qty').Value;
        Post;
      end;
      Next;
    end;
  end;
end;

procedure TfrmOrderInPut_New.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('生产计划单1', Tv1, '生产指示单管理');
end;

procedure TfrmOrderInPut_New.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from ML_OrderSubidNew where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and NewMLID=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from ML_OrderZuHe where NewMLID=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQuery1, ClientDataSet2);
  SInitCDSData20(ADOQuery1, ClientDataSet2);

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ML_OrderMainNew where NewMLID=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOQuery1, ScrollBox1, 2);
  SCSHDataNew(ADOQuery1, Panel4, 2);
  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    JHDate.Date := OrdDate.Date;
    YWY.Text := Trim(DName);
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    JHDate.Date := OrdDate.Date;
    YWY.Text := Trim(DName);
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('NewMLID').Value := '';
        FieldByName('NewSubid').Value := '';
        Post;
        Next;
      end;
    end;
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('NewMLID').Value := '';
        FieldByName('ZHID').Value := '';
        Post;
        Next;
      end;
    end;
  end;
end;

procedure TfrmOrderInPut_New.FormShow(Sender: TObject);
begin
  readCxGrid('生产计划单1', Tv1, '生产指示单管理');
  InitData();
  if ISPD = '排单' then
    InitPDJL();
end;

function TfrmOrderInPut_New.SaveData(): Boolean;
var
  maxno, MXmaxno, forderNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOTemp, maxno, 'ML', 'ML_OrderMainNew', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      {if GetLSNo(ADOCmd,forderNo,'','ML_OrderMainNew',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
     MLorderNo.Text:=forderNo;}
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ML_OrderMainNew where NewMLID=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
      end;
      FieldByName('NewMLID').Value := Trim(maxno);
      SSetsaveSqlNew(ADOCmd, 'ML_OrderMainNew', ScrollBox1, 2);
      SSetsaveSqlNew(ADOCmd, 'ML_OrderMainNew', Panel4, 2);
      if Trim(MLOrderNo.Hint) <> '' then
      begin
        FieldByName('ConSubId').Value := Trim(MLOrderNo.Hint);
      end;
      FieldByName('Status').Value := '未审核';
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;
    FMainId := Trim(maxno);
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ML_OrderMainNew where MLOrderNo=''' + Trim(MLOrderNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      FMainId := '';
      application.MessageBox('指示单号重复,不能保存', '提示');
      exit;
    end;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('NewSubid').AsString) = '' then
        begin
          if GetLSNo(ADOTemp, maxno, 'MS', 'ML_OrderSubidNew', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('NewSubid').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from ML_OrderSubidNew where NewMLID=''' + Trim(FMainId) + '''');
          sql.Add(' and NewSubid=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('NewSubid').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('NewMLID').Value := Trim(FMainId);
          FieldByName('NewSubid').Value := Trim(maxno);

          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'ML_OrderSubidNew', 0);
          FieldByName('ConSubId').Value := Trim(Order_Sub.FieldByName('ConSubId').AsString);
          fieldbyname('Qty').Value := Order_Sub.FieldByName('Qty').AsFloat;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('NewSubid').Value := Trim(maxno);
        Order_Sub.FieldByName('NewMLID').Value := Trim(FMainId);
        Next;
      end;
    end;

    ///保存层级表
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('ZHID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'ZH', 'ML_OrderZuHe', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(ClientDataSet2.fieldbyname('ZHID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from ML_OrderZuHe where ZHID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('ZHID').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;

          FieldByName('NewMLID').Value := Trim(FMainId);
          FieldByName('ZHID').Value := Trim(maxno);
          FieldByName('ZHDate').Value := OrdDate.Date;
          RTSetSaveDataCDS(ADOCmd, Tv2, ClientDataSet2, 'ML_OrderZuHe', 0);
          Post;
        end;
        ClientDataSet2.Edit;
        ClientDataSet2.FieldByName('ZHID').Value := Trim(maxno);
        Next;
      end;
    end;

    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update ML_ConSub set ISPD=( case when (select  COUNT(1)  from ML_OrderSubidNew X  where X.ConSubId=CSID)>0 then 1 else 0 end)');
      sql.Add('where exists(select X.MLConID from ML_ConMain X where X.MLConID=ML_ConSub.MLConID and X.ConNO=' + quotedstr(MLConNo.Text) + ') ');
      ExecSQL;
    end;
    //更新棉&无纺布耗用
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      //理论
      sql.Add('Update ML_OrderZuHe Set MLJHQty=(select sum(Qty) from ML_OrderSubidNew A where A.NewMLID=ML_OrderZuHe.NewMLID) ');
      sql.Add(' where NewMLID=''' + Trim(FMainId) + '''');
      sql.Add(' and isnull(MLName,'''')<>'''' and SSGS=''自己'' ');
      //实际
      sql.Add('Update ML_OrderZuHe Set MLJYQty=(select sum(JYLen) from JTJY_New A where A.NewMLID=ML_OrderZuHe.NewMLID and A.JYType=''成品'') ');
      sql.Add(' where NewMLID=''' + Trim(FMainId) + '''');
      sql.Add(' and isnull(MLName,'''')<>'''' and SSGS=''自己'' ');

      ExecSQL;
    end;
    //更新棉&无纺布耗用
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmOrderInPut_New.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if MLOrderNo.Text = '' then
  begin
    Application.MessageBox('指示单号不能为空', '提示', 0);
    Exit;
  end;
  if Trim(BZType.Text) = '' then
  begin
    Application.MessageBox('币种不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;
  if ClientDataSet2.IsEmpty then
  begin
    Application.MessageBox('工艺信息不能为空!', '提示', 0);
    Exit;
  end;

  if ClientDataSet2.Locate('MLType;SSGS;MLCode', VarArrayOf(['棉', '自己', null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('棉的编号不能为空！', '提示', 0);
    Exit;
  end;

  if ClientDataSet2.Locate('MLType;SSGS;GYSName', VarArrayOf(['棉', '自己', null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('供应商不能为空！', '提示', 0);
    Exit;
  end;

  if ClientDataSet2.IsEmpty then
  begin
    Application.MessageBox('工艺信息不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
end;

procedure TfrmOrderInPut_New.v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdColor';
      flagname := '颜色';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
      fnote := True;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('MLColor').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('MLColorNo').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_New.KHNameBtnUpClick(Sender: TObject);
begin
  try
    frmKHList_CX := TfrmKHList_CX.Create(Application);
    with frmKHList_CX do
    begin
      if self.canshu1 <> '高权限' then
        canshu1 := '业务员';
      if ShowModal = 1 then
      begin
        KHName.Text := Trim(CDS_HZ.fieldbyname('KHName').AsString);
        KHName.TxtCode := Trim(CDS_HZ.fieldbyname('KHNo').AsString);
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select Top 1 * from ML_OrderMainNew where KHNO=''' + Trim(KHName.TxtCode) + ''' and isnull(CPBQName,'''')<>'''' order by Filltime desc');
          Open;
        end;
        Self.CPBQName.Text := Trim(ADOTemp.fieldbyname('CPBQName').AsString);
        Self.CPBaoBQName.Text := Trim(ADOTemp.fieldbyname('CPBaoBQName').AsString);
      end;
    end;
  finally
    frmKHList_CX.Free;
  end;
end;

procedure TfrmOrderInPut_New.KHNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmOrderInPut_New.v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalues: string;
begin
  mvalues := tv1.Columns[tv1.Controller.FocusedColumn.index].DataBinding.FieldName;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'NewOrderUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName(mvalues).Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_New.v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PriceUnit';
      flagname := '价格单位';
      if Trim(DParameters1) <> '高权限' then
      begin
        TBAdd.Visible := False;
        TBEdit.Visible := False;
        TBDel.Visible := False;
      end;

      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PriceUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_New.OrdDefStr2BtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_New.ConNoKeyPress(Sender: TObject; var Key: Char);
var
  ConMainId: string;
begin
  if Key = #13 then
  begin

    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderCon_Main where ConNo like ''' + '%' + Trim(MLConNo.Text) + '%' + '''');
      Open;
    end;
    if ADOTemp.RecordCount > 1 then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select Top 1* from JYOrderCon_Main where ConNo=''' + Trim(MLConNo.Text) + '''');
        Open;
      end;
      if ADOTemp.IsEmpty then
        Exit;
    end
    else if ADOTemp.RecordCount = 1 then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrderCon_Main where ConNo like ''' + '%' + Trim(MLConNo.Text) + '%' + '''');
        Open;
      end;
    end;
    begin
      MLConNo.Text := Trim(ADOTemp.fieldbyname('ConNo').AsString);
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,ColorName=(select ZdyName from KH_Zdy B where B.Note=A.PRTColor and B.Type=''OrdColor'' ) ');
        SQL.Add(' from JYOrderCon_Sub A where MainId=''' + Trim(ConMainId) + '''');
        Open;
      end;
      with ADOTemp do
      begin
        First;
        while not Eof do
        begin
          with Order_Sub do
          begin
            Append;
            FieldByName('PRTColor').Value := Trim(ADOTemp.fieldbyname('PRTColor').AsString);
            FieldByName('SOrdDefStr4').Value := Trim(ADOTemp.fieldbyname('SOrdDefStr4').AsString);
            FieldByName('SOrdDefStr3').Value := Trim(ADOTemp.fieldbyname('SOrdDefStr3').AsString);
            FieldByName('PRTOrderQty').Value := Trim(ADOTemp.fieldbyname('PRTOrderQty').AsString);
            FieldByName('OrderUnit').Value := Trim(ADOTemp.fieldbyname('OrderUnit').AsString);
            FieldByName('PRTPrice').Value := Trim(ADOTemp.fieldbyname('PRTPrice').AsString);
            FieldByName('PriceUnit').Value := Trim(ADOTemp.fieldbyname('PriceUnit').AsString);
            FieldByName('PRTHX').Value := Trim(ADOTemp.fieldbyname('PRTHX').AsString);
            FieldByName('XHNO').Value := Trim(ADOTemp.fieldbyname('XHNO').AsString);
            FieldByName('Sorddefstr10').Value := Trim(ADOTemp.fieldbyname('Subid').AsString);
            Post;
          end;
          Next;
        end;
      end;
    end;

  end;
end;

procedure TfrmOrderInPut_New.YCLFactoryBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      //MainType:=TEdit(Sender).Name;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_New.MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut_New.YWYBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YWY';
      flagname := '业务员';
      if Trim(canshu1) <> '高权限' then
      begin
        TBAdd.Visible := False;
        TBEdit.Visible := False;
        TBDel.Visible := False;
      end;
      if ShowModal = 1 then
      begin
        YWY.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_New.N1Click(Sender: TObject);
var
  i: Integer;
begin
  i := Order_Sub.RecordCount;
  CopyAddRow(Tv1, Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('XHNO').Value := IntToStr(i + 1);
    FieldByName('TPID').Value := '';
    Post;
  end;
end;

procedure TfrmOrderInPut_New.N2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('NewSubid').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from JTJY_New where NewSubid=''' + Trim(Order_Sub.fieldbyname('NewSubid').AsString) + '''');
      Open;
    end;
    if ADOTemp.IsEmpty = False then
    begin
      Application.MessageBox('已产生数据，不能删除!', '提示', 0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ML_OrderSubidNew where NewSubid=''' + Trim(Order_Sub.fieldbyname('NewSubid').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmOrderInPut_New.v1MLHXPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachCP_HX_Sel := TfrmZdyAttachCP_HX_Sel.Create(Application);
    with frmZdyAttachCP_HX_Sel do
    begin
      if ShowModal = 1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('MLHX').Value := Trim(frmZdyAttachCP_HX_Sel.CDS_HZ.fieldbyname('zdyName').AsString);
        end;
      end;
    end;
  finally
    frmZdyAttachCP_HX_Sel.Free;
  end;
end;

procedure TfrmOrderInPut_New.N3Click(Sender: TObject);
var
  FName, mvalue, FCaption: string;
begin
  ToolBar1.SetFocus;
  FName := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FCaption := Tv1.Controller.FocusedColumn.Caption;
  mvalue := Trim(Order_Sub.fieldbyname(FName).AsString);
  if Trim(FCaption) = '颜色' then
  begin
    Application.MessageBox('颜色不能一键替换！', '提示', 0);
    Exit;
  end;
  if Trim(FCaption) = 'COLOR' then
  begin
    Application.MessageBox('COLOR不能一键替换！', '提示', 0);
    Exit;
  end;
  if Application.MessageBox(PChar('确定要把所有的<' + Trim(FCaption) + '> 更改为：' + Trim(mvalue) + ' 吗？'), '提示', 32 + 4) <> IDYES then
    Exit;
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName(FName).Value := Trim(mvalue);
      Post;
      Next;
    end;
  end;
end;

procedure TfrmOrderInPut_New.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FeeName';
      flagname := '费用名称';
      if ShowModal = 1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('FeeName').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_New.Tv2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if Trim(ClientDataSet2.fieldbyname('MLType').AsString) = '' then
  begin
    Application.MessageBox('类型不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(ClientDataSet2.fieldbyname('MLType').AsString) = '棉' then
  begin
    try
      frmBPZDY_LRM := TfrmBPZDY_LRM.Create(Application);
      with frmBPZDY_LRM do
      begin
        if ShowModal = 1 then
        begin
          with Self.ClientDataSet2 do
          begin
            Edit;
            Self.ClientDataSet2.FieldByName('MLCode').Value := Trim(frmBPZDY_LRM.ClientDataSet1.fieldbyname('BPCode').AsString);
            Self.ClientDataSet2.FieldByName('MLName').Value := Trim(frmBPZDY_LRM.ClientDataSet1.fieldbyname('BPName').AsString);
            Self.ClientDataSet2.FieldByName('MLMF').Value := Trim(frmBPZDY_LRM.ClientDataSet1.fieldbyname('BPMF').AsString);
            Self.ClientDataSet2.FieldByName('MLKZ').Value := Trim(frmBPZDY_LRM.ClientDataSet1.fieldbyname('BPKZ').AsString);
          end;
        end;
      end;
    finally
      frmBPZDY_LRM.Free;
    end;
  end
  else if Trim(ClientDataSet2.fieldbyname('MLType').AsString) = '无纺布' then
  begin
    try
      frmBPZDY_LRM := TfrmBPZDY_LRM.Create(Application);
      with frmBPZDY_LRM do
      begin
        if ShowModal = 1 then
        begin
          with Self.ClientDataSet2 do
          begin
            Edit;
            Self.ClientDataSet2.FieldByName('MLCode').Value := Trim(frmBPZDY_LRM.ClientDataSet1.fieldbyname('BPCode').AsString);
            Self.ClientDataSet2.FieldByName('MLName').Value := Trim(frmBPZDY_LRM.ClientDataSet1.fieldbyname('BPName').AsString);
            Self.ClientDataSet2.FieldByName('MLMF').Value := Trim(frmBPZDY_LRM.ClientDataSet1.fieldbyname('BPMF').AsString);
            Self.ClientDataSet2.FieldByName('MLKZ').Value := Trim(frmBPZDY_LRM.ClientDataSet1.fieldbyname('BPKZ').AsString);
          end;
        end;
      end;
    finally
      frmBPZDY_LRM.Free;
    end;
  end;
end;

procedure TfrmOrderInPut_New.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Trim(ClientDataSet2.fieldbyname('SSGS').AsString) = '' then
  begin
    V2MLCode.Options.Focusing := false;
    V2MLName.Options.Focusing := false;
    V2MF.Options.Focusing := false;
    V2KZ.Options.Focusing := false;
    V2Note.Options.Focusing := false;
  end
  else
  begin
    V2MLCode.Options.Focusing := True;
    V2MLName.Options.Focusing := True;
    V2MF.Options.Focusing := True;
    V2KZ.Options.Focusing := True;
    V2Note.Options.Focusing := True;
  end;
  if (Trim(ClientDataSet2.fieldbyname('MLType').AsString) = '棉') or (Trim(ClientDataSet2.fieldbyname('MLType').AsString) = '无纺布') then
  begin
    if Trim(ClientDataSet2.fieldbyname('SSGS').AsString) = '自己' then
    begin
      V2MLCode.Properties.ReadOnly := True;
      V2MLName.Options.Focusing := False;
      V2MF.Options.Focusing := False;
      V2KZ.Options.Focusing := False;
    end
    else if Trim(ClientDataSet2.fieldbyname('SSGS').AsString) = '客户' then
    begin
      V2MLCode.Properties.ReadOnly := False;
      V2MLName.Options.Focusing := True;
      V2MF.Options.Focusing := True;
      V2KZ.Options.Focusing := True;
    end
    else
    begin
      V2MLCode.Properties.ReadOnly := True;
    end;
  end
  else
  begin
    V2MLCode.Properties.ReadOnly := False;
  end;

end;

procedure TfrmOrderInPut_New.MenuItem1Click(Sender: TObject);
begin
  Panel3.Visible := True;
end;

procedure TfrmOrderInPut_New.Button1Click(Sender: TObject);
var
  i, j: Integer;
begin
  i := ClientDataSet2.RecordCount;
  with ClientDataSet2 do
  begin
    for j := 1 to StrToInt(ComboBox1.Text) do
    begin
      Append;
      FieldByName('XHNO').Value := IntToStr(i + j);
      Post;
    end;
  end;
  Panel3.Visible := False;
end;

procedure TfrmOrderInPut_New.MenuItem2Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then
    Exit;
  if Trim(ClientDataSet2.fieldbyname('ZHID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ML_OrderZuHe where ZHID=''' + Trim(ClientDataSet2.fieldbyname('ZHID').AsString) + '''');
      ExecSQL;
    end;
  end;
  ClientDataSet2.Delete;
end;

procedure TfrmOrderInPut_New.cxButton1Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Trim(FMainId) = '' then
  begin
    Application.MessageBox('没有保存数据!', '提示', 0);
    exit;
  end;
  if trim(CPBQName.Text) = '' then
    exit;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Print_New_Roll ');
    sql.Add('@mainID=''' + trim(FMainId) + ''' ');
    Open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('JYID').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\' + Trim(CPBQName.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示信息', 0);
  end;
end;

procedure TfrmOrderInPut_New.cxButton2Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Trim(FMainId) = '' then
  begin
    Application.MessageBox('没有保存数据!', '提示', 0);
    exit;
  end;
  if trim(CPBaoBQName.Text) = '' then
    exit;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Print_New_Bao ');
    sql.Add('@mainID=''' + trim(FMainId) + ''' ');
    Open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('JYID').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\' + Trim(CPBaoBQName.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示信息', 0);
  end;
end;

procedure TfrmOrderInPut_New.CPBQNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  labname: string;
begin
  try
    frmLabelList := TfrmLabelList.Create(Application);
    with frmLabelList do
    begin
      if ShowModal = 1 then
      begin
        labname := Trim(ADOQueryLabel.fieldbyname('labelCaption').AsString);
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ML_OrderMainNew Set CPBQName=''' + Trim(labname) + '''');
          sql.Add(' where NewMLID=''' + Trim(FMainId) + '''');
          ExecSQL;
        end;
        Self.CPBQName.Text := Trim(labname);
      end;
    end;
  finally
    frmLabelList.Free;
  end;
end;

procedure TfrmOrderInPut_New.CPBaoBQNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  labname: string;
begin
  try
    frmLabelList := TfrmLabelList.Create(Application);
    with frmLabelList do
    begin
      if ShowModal = 1 then
      begin
        labname := Trim(ADOQueryLabel.fieldbyname('labelCaption').AsString);
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ML_OrderMainNew Set CPBaoBQName=''' + Trim(labname) + '''');
          sql.Add(' where NewMLID=''' + Trim(FMainId) + '''');
          ExecSQL;
        end;
        Self.CPBaoBQName.Text := Trim(labname);
      end;
    end;
  finally
    frmLabelList.Free;
  end;
end;

procedure TfrmOrderInPut_New.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  Panel6.Visible := True;
//  try
//    frmZHCPList_Sel := TfrmZHCPList_Sel.Create(Application);
//    with frmZHCPList_Sel do
//    begin
//      if ShowModal = 1 then
//      begin
//        with Order_Sub do
//        begin
//          Edit;
//          FieldByName('TPID').Value := Trim(frmZHCPList_Sel.Order_Main.fieldbyname('SCTP').AsString);
//        end;
//      end;
//    end;
//  finally
//    frmZHCPList_Sel.Free;
//  end;
end;

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

end;

procedure TfrmOrderInPut_New.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  jpg: TJpegImage;
  myStream: TADOBlobStream;
  FStr: string;
begin
  with adoqueryPicture do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Image_Info A');
    sql.Add(' inner join Image_File B on A.IMID=B.IMID');
    sql.Add(' and A.WBID=''' + Trim(Order_Sub.fieldbyname('TPID').AsString) + '''');
    sql.Add(' and isnull(A.IMNo,'''') in (''内容'',''生产'') and A.Valid=''Y'' ');
//    ShowMessage(sql.text);
    open;
  end;

  if adoqueryPicture.IsEmpty = False then
  begin
    Panel5.Visible := true;
    FStr := Trim(adoqueryPicture.fieldbyname('ImageFile').AsString);
    jpg := TJpegImage.Create();
    myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('ImageFile')), bmread);
    jpg.LoadFromStream(myStream);
    Image2.Picture.Assign(jpg);
    Image2.Width := adoqueryPicture.fieldbyname('ImageWidth').Value;
    Image2.Height := adoqueryPicture.fieldbyname('ImageHeight').Value;
    if Trim(adoqueryPicture.fieldbyname('FangXiang').AsString) = '垂直' then
    begin
      Image2.Top := 1;
      Image2.Left := Round((Panel5.Width - Image2.Width) / 2);
    end
    else
    begin
      Image2.Left := 1;
      Image2.Top := Round((Panel5.Height - Image2.Height) / 2);
    end;
    Image2.Visible := True;
  end;
end;

procedure TfrmOrderInPut_New.Image1Click(Sender: TObject);
begin
  Panel6.Visible := false;
end;

procedure TfrmOrderInPut_New.SpeedButton6Click(Sender: TObject);
begin
  try
    frmZHCPList_Sel := TfrmZHCPList_Sel.Create(Application);
    with frmZHCPList_Sel do
    begin
      if ShowModal = 1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('TPID').Value := Trim(frmZHCPList_Sel.Order_Main.fieldbyname('SCTP').AsString);
        end;
      end;
    end;
  finally
    frmZHCPList_Sel.Free;
  end;
  Panel6.Visible := false;
end;

procedure TfrmOrderInPut_New.SpeedButton1Click(Sender: TObject);
var
  AJpeg: TJPEGImage;
  Bitmap, Bitmap22: TBitmap;
  ARect, ARect22: TRect;
  TPHeight, AHeightOffset: Integer;
  TPWidth, AWidthOffset: Integer;
  WZShuiPing, WZChuiZhi: Integer;
  WZFlag, TPID: string;
begin
  lstPat2.Clear;
  if ODPat.Execute then
  begin
    lstPat2.AddStrings(ODPat.Files);
  end
  else
  begin
    Exit;
  end;
  if lstPat2.Count > 1 then
  begin
    lstPat2.Clear;
    Application.MessageBox('不能上传多个图片!', '提示', 0);
    Exit;
  end
  else if lstPat2.Count < 1 then
  begin
    Exit;
  end;

  AJpeg := TJpegImage.Create();
  AJpeg.LoadFromFile(ExtractFileName(ODPat.FileName));
  Image2.Hint := ExtractFileName(ODPat.FileName);
  //ShowMessage(Copy(Image2.Hint,Length(Image2.Hint)-2,Length(Image2.Hint)));
  if Copy(Image2.Hint, Length(Image2.Hint) - 2, Length(Image2.Hint)) <> 'jpg' then
  begin
    Application.MessageBox('图片必须是jpg格式', '提示', 0);
    Exit;
  end;
  TPWidth := AJpeg.Width;
  TPHeight := AJpeg.Height;
  Bitmap := TBitmap.Create;
  Bitmap22 := TBitmap.Create;
  Bitmap.Width := Panel5.Width - 2;
  Bitmap.Height := Round(Panel5.Width / TPWidth * TPHeight);
  if Bitmap.Height > Panel5.Height then
  begin
    WZFlag := '垂直';
    Bitmap.Height := Panel5.Height - 2;
    Bitmap.Width := Round(Panel5.Height / TPHeight * TPWidth);
  end;
  Bitmap22.Width := Bitmap.Width * 2;
  Bitmap22.Height := Bitmap.Height * 2;
  Bitmap.Canvas.FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));
  ARect := Rect(0, 0, Bitmap.Width, Bitmap.Height);
  Bitmap.Canvas.StretchDraw(ARect, AJpeg);
  Image2.Height := Bitmap.Height;
  Image2.Width := Bitmap.Width;
  Image2.Picture.Assign(Bitmap);
  if Trim(WZFlag) = '垂直' then
  begin
    Image2.Top := 1;
    Image2.Left := Round((Panel5.Width - Image2.Width) / 2);
  end
  else
  begin
    Image2.Left := 1;
    Image2.Top := Round((Panel5.Height - Image2.Height) / 2);
  end;
  FangXiang2 := WZFlag;
  Bitmap22.Canvas.FillRect(Rect(0, 0, Bitmap22.Width, Bitmap22.Height));
  ARect22 := Rect(0, 0, Bitmap22.Width, Bitmap22.Height);
  Bitmap22.Canvas.StretchDraw(ARect22, AJpeg);
  Image22.Height := Bitmap22.Height;
  Image22.Width := Bitmap22.Width;
  Image22.Picture.Assign(Bitmap22);
  Image2.Visible := True;
//  Edit2.Text := Copy(Image2.Hint, 1, Length(Image2.Hint) - 4);
  if GetLSNo(ADOCmd, TPID, 'TP', 'Image_Info', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取图片信息最大号失败！', '提示', 0);
    Exit;
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('TPID').Value := TPID;
  end;
  if Pos('TP', Trim(Order_Sub.fieldbyname('TPID').AsString)) > 0 then
  begin
    SaveImage(Trim(Order_Sub.fieldbyname('TPID').AsString), '生产');
  end;
  Panel6.Visible := false;
end;

procedure TfrmOrderInPut_New.FormCreate(Sender: TObject);
begin
  lstPat := TStringList.Create;
  lstPat2 := TStringList.Create;
end;

procedure TfrmOrderInPut_New.ReadINIFile10();
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  programIni := Tinifile.create(FileName);
  server := programIni.ReadString('SERVER', '服务器地址', '127.0.0.1');
  programIni.Free;
end;

procedure TfrmOrderInPut_New.SaveImage(maxnoWB: string; FIMNO: string);
var
  ImagePath, ImagName, MaxNo, MaxNoFile: string;
  AJpeg: TJPEGImage;
  myStream: TADOBlobStream;
begin
  try

    ImagePath := Image2.Hint;
    ImagName := Image2.Hint;

    AJpeg := TJpegImage.Create();
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate Image_File Set Valid=''N'' ');
      sql.Add(' where IMID=(select IMID from Image_Info');
      Sql.Add('where WBID=''' + Trim(maxnoWB) + ''' and Valid=''Y'' and IMNO=''' + Trim(FIMNO) + ''')');
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate Image_Info Set Editer=''' + Trim(DName) + ''',EditerCode=''' + Trim(DCode) + ''',EditTime=getdate()');
      sql.Add(',Valid=''N''  where WBID=''' + Trim(maxnoWB) + ''' and Valid=''Y'' and IMNO=''' + Trim(FIMNO) + ''' ');
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    if GetLSNo(ADOCmd, MaxNo, 'IM', 'Image_Info', 4, 1) = False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取图片信息最大号失败！', '提示', 0);
      Exit;
    end;
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from Image_Info where 1=2');
      Open;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('IMID').Value := Trim(MaxNo);
      FieldByName('IMNo').Value := FIMNO;
      FieldByName('WBID').Value := Trim(maxnoWB);
      FieldByName('ImageName').Value := Trim(ImagName);
      FieldByName('ImagePath').Value := Trim(MaxNo) + Trim(ImagName);
      FieldByName('ImageDate').Value := SGetServerDate(ADOTemp);
      Fieldbyname('ImageType').value := Trim('YP');
      Fieldbyname('Valid').value := Trim('Y');
      Post;
    end;
    if GetLSNo(ADOCmd, MaxNoFile, 'IF', 'Image_File', 4, 1) = False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取图片文件最大号失败！', '提示', 0);
      Exit;
    end;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' select *  from Image_File where 1=2');
      open;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('IMID').Value := Trim(MaxNo);
      FieldByName('IFID').Value := Trim(MaxNoFile);
      Fieldbyname('Valid').value := Trim('Y');

      FieldByName('ImageWidth').Value := Image2.Width;
      FieldByName('ImageHeight').Value := Image2.Height;
      FieldByName('FangXiang').Value := FangXiang2;

      FieldByName('IFType').Value := '小';
      myStream := TADOBlobStream.Create(TBlobField(ADOCmd.FieldByName('ImageFile')), bmWrite);
      AJpeg.Assign(Image2.Picture.Graphic);
      AJpeg.SaveToStream(myStream);
      myStream.Free;
      Post;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('IMID').Value := Trim(MaxNo);
      FieldByName('IFID').Value := Trim(MaxNoFile) + 'D';
      Fieldbyname('Valid').value := Trim('Y');

      FieldByName('ImageWidth').Value := Image22.Width;
      FieldByName('ImageHeight').Value := Image22.Height;
      FieldByName('FangXiang').Value := FangXiang2;

      FieldByName('IFType').Value := '大';
      myStream := TADOBlobStream.Create(TBlobField(ADOCmd.FieldByName('ImageFile')), bmWrite);

      AJpeg.Assign(Image22.Picture.Graphic);
      AJpeg.SaveToStream(myStream);
      myStream.Free;
      Post;
    end;
    try
      ReadINIFile10();
      server := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
      IdFTP1.Host := server; //PicSvr;
      IdFTP1.Username := 'three';
      IdFTP1.Password := '641010';
      IdFTP1.Connect();
    except
      IdFTP1.Quit;
      Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
      Exit;
    end;
    if IdFTP1.Connected then
    begin
      IdFTP1.Put(lstPat2[0], 'YP' + '\' + Trim(MaxNo) + Trim(ImagName));
    end;
    if IdFTP1.Connected then
      IdFTP1.Quit;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('图片保存失败！', '提示', 0);
  end;
end;

procedure TfrmOrderInPut_New.Tv2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal = 1 then
      begin
        with Self.ClientDataSet2 do
        begin
          Edit;
          Self.ClientDataSet2.FieldByName('GYSName').Value := Trim(Order_Main.fieldbyname('KHNameJC').AsString);
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

end.

