unit U_MLOrderInPutHZ;

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,
  cxGroupBox, cxCheckBox, ImgList, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, Menus, ShellAPI, cxSplitter, cxPC, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore,  
     dxSkinDarkRoom,
    
     
     
    
    
    
    
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter,
  dxBarBuiltInMenu, cxNavigator;

type
  TfrmMLOrderInPutHZ = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    Label10: TLabel;
    Label3: TLabel;
    Label14: TLabel;
    OrdDate: TDateTimePicker;
    KHName: TBtnEditC;
    CDS_CON: TClientDataSet;
    DSCON: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    ADOQuery2: TADOQuery;
    Label2: TLabel;
    SYRName: TBtnEditC;
    Panel1: TPanel;
    Label1: TLabel;
    Label5: TLabel;
    Panel21: TPanel;
    YWConNO: TEdit;
    IFHS: TCheckBox;
    IFBHS: TCheckBox;
    Label52: TLabel;
    KHConNo: TEdit;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    ImageList1: TImageList;
    PopupMenu3: TPopupMenu;
    MenuItem5: TMenuItem;
    N2: TMenuItem;
    ConNo: TEdit;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1ConCode: TcxGridDBColumn;
    v1ConCodeName: TcxGridDBColumn;
    v1ConGY: TcxGridDBColumn;
    v1ConColor: TcxGridDBColumn;
    v1ConColNo: TcxGridDBColumn;
    v1ConKHColNo: TcxGridDBColumn;
    v1ConHX: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1ConPrice: TcxGridDBColumn;
    v1ConMoney: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1ConNote: TcxGridDBColumn;
    v1IFFZ: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ScrollBox2: TScrollBox;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    Label35: TLabel;
    Label37: TLabel;
    QtyBiaoZhun: TBtnEditC;
    ZLBiaoZhun: TBtnEditC;
    JHType: TBtnEditC;
    BZYaoQiu: TBtnEditC;
    JSType: TBtnEditC;
    HZQiXian: TBtnEditC;
    WYZeRen: TBtnEditC;
    BCShuoMing: TBtnEditC;
    CheckBox1: TCheckBox;
    KPCodeName: TBtnEditC;
    CheckBox2: TCheckBox;
    SYRAdd: TEdit;
    SYRZJL: TEdit;
    SYRTel: TEdit;
    SYRYWTel: TEdit;
    SYRBankNo: TEdit;
    KHAdd: TEdit;
    KHZJL: TEdit;
    KHTel: TEdit;
    KHFDR: TEdit;
    KHYWFZR: TEdit;
    KHYWTel: TEdit;
    KHPhone: TEdit;
    KHBankName: TEdit;
    KHBankNo: TEdit;
    SYRFDR: TBtnEditA;
    SYRYWY: TBtnEditA;
    SYRYWFZR: TBtnEditA;
    Button13: TButton;
    SYRBankName: TEdit;
    SYRPhone: TEdit;
    ListView1: TListView;
    Panel12: TPanel;
    Label48: TLabel;
    Button4: TButton;
    Panel13: TPanel;
    Image1: TImage;
    WJName: TEdit;
    Button7: TButton;
    WJPach: TEdit;
    Button8: TButton;
    Panel2: TPanel;
    Panel4: TPanel;
    Image2: TImage;
    Button3: TButton;
    Button5: TButton;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Button14: TButton;
    ToolButton3: TToolButton;
    v1Column1: TcxGridDBColumn;
    Label36: TLabel;
    JHDate: TDateTimePicker;
    Button1: TButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    PriceTK: TBtnEditA;
    Label4: TLabel;
    ToPlace: TBtnEditA;
    Label38: TLabel;
    FromPlace: TBtnEditA;
    Label39: TLabel;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Label40: TLabel;
    YDZ: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    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 v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ListView1DblClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure IFHSClick(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure SYRFDRBtnClick(Sender: TObject);
    procedure SYRYWYBtnClick(Sender: TObject);
    procedure SYRYWFZRBtnClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure IFBHSClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1ConHXPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure v1Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure PriceTKBtnClick(Sender: TObject);
    procedure ToPlaceBtnClick(Sender: TObject);
    procedure FromPlaceBtnClick(Sender: TObject);
  private
    procedure InitData();
    function SaveData(): Boolean;
    function getOrderNo(fconNo: string): string;
    procedure CFSaveData();
    procedure Initimage();
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo,SYRYWYF,SYRYWFZRF: string;
    FXS: Integer;
    canshu1, canshu2: string;
    fFlileFlag,FConType: string;
    { Public declarations }
  end;

var
  frmMLOrderInPutHZ: TfrmMLOrderInPutHZ;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_RTFun, U_ZDYHelpSel, U_ZdyAttachment,
  U_ZdyAttachGYS, U_ZHCPList_CX, getpic, U_ContractList_CX,
  U_GSList_CX, U_KHList_CX, U_MLMangeRS_Sel1, U_BPZdy_LRPB, U_BPZdy_LRM,
  U_MCGRKList_XKC_CX, U_BPZdy_LRPB_M1, U_MLManage_LRRS, U_ZDYHelp_YW,
  U_MLMangeRS_Sel2,U_ZHCPListSel,U_ZdyAttachCP_HX_sel,U_MLMangeRS_Sel;

{$R *.dfm}
procedure TfrmMLOrderInPutHZ.CFSaveData();
var
  fCFID: string;
begin
  with ADOQuery2 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from JYOrder_Sub_CF where subID=''' + Trim(Order_Sub.fieldbyname('subID').asstring) + '''');
    open;
  end;
  if ADOQuery2.IsEmpty then
  begin
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      SQL.Add('select A.CengJI,A.MLName,A.MLSeHao,A.MLID ');
      sql.Add(',HXNo=(select Top 1 B.HXNo from CP_YDang_JG B where B.CengJi=A.CengJi and B.CYID=A.CYID and B.IFZY=1)');
      sql.Add(',GYDM=(select Top 1 B.GYDM from CP_YDang_JG B where B.CengJi=A.CengJi and B.CYID=A.CYID and B.IFZY=1)');
      sql.Add(' from CP_YDang_ML A ');
      SQL.Add('where A.CYID=''' + trim(Order_Sub.fieldbyname('PRTCode').AsString) + '''');
      sql.Add(' and A.IFSel=1 ');
      sql.Add(' order by A.Cengji,A.MLID');
      open;
    end;
    while not ADOQuery1.Eof do
    begin
      if GetLSNo(ADOQueryCmd, fCFID, 'JC', 'JYOrder_Sub_CF', 4, 1) = False then
      begin
        Application.MessageBox('取子流水号失败！', '提示', 0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from JYOrder_Sub_CF where CFID=''' + Trim(fCFID) + '''');
        Open;
        if IsEmpty then
          Append
        else
          edit;
        fieldbyname('CFID').Value := trim(fCFID);
        fieldbyname('PRTCode').Value := trim(Order_Sub.fieldbyname('PRTCode').AsString);
        fieldbyname('PrtCodeName').Value := trim(ADOQuery1.fieldbyname('MLName').AsString);
        fieldbyname('PrtSpec').Value := trim(Order_Sub.fieldbyname('PrtSpec').AsString);
        fieldbyname('SOrddefstr1').Value := trim(ADOQuery1.fieldbyname('MLSeHao').AsString);
        fieldbyname('PRTColor').Value := trim(ADOQuery1.fieldbyname('MLColor').AsString);
        fieldbyname('PRTHX').Value := trim(ADOQuery1.fieldbyname('HXName').AsString);
        fieldbyname('PRTOrderRollNum').Value := Order_Sub.fieldbyname('PRTOrderRollNum').AsFloat;
        fieldbyname('PrtOrderQty').Value := Order_Sub.fieldbyname('PRTOrderQty').AsFloat;
        fieldbyname('OrderUnit').Value := trim(Order_Sub.fieldbyname('OrderUnit').AsString);
        FieldByName('PRTType').Value := trim(ADOQuery1.fieldbyname('CengJi').AsString) + '层';
        fieldbyname('MainID').Value := trim(Order_Sub.fieldbyname('MainID').AsString);
        fieldbyname('SubID').Value := trim(Order_Sub.fieldbyname('SubID').AsString);
        post;
      end;
      ADOQuery1.Next;
    end;
  end
  else
  begin
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('update JYOrder_Sub_CF set ');
      sql.Add('PRTCode=''' + trim(Order_Sub.fieldbyname('PRTCode').AsString) + '''');
      sql.Add(',PrtSpec=''' + trim(Order_Sub.fieldbyname('PrtSpec').AsString) + '''');
      sql.Add(',OrderUnit=''' + trim(Order_Sub.fieldbyname('OrderUnit').AsString) + '''');
      sql.Add(' where Subid=''' + trim(Order_Sub.fieldbyname('Subid').AsString) + '''');
      ExecSQL;
    end;
  end;
end;

function TfrmMLOrderInPutHZ.getOrderNo(fconNo: string): string;
var
  str1: string;
  i: integer;
begin
  str1 := trim(fconNo) + '01';
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select MainID from JYOrder_Main where orderNo=' + quotedstr(trim(str1)));
    open;
  end;
  if not ADOTemp.IsEmpty then
  begin
    for i := 1 to 99 do
    begin
      str1 := trim(fconNo) + rightstr(inttostr(100 + i), 2);
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select MainID from JYOrder_Main where orderNo=' + quotedstr(trim(str1)));
        open;
      end;
      if ADOCmd.IsEmpty then
        break;
    end;
  end;
  result := str1;
end;

procedure TfrmMLOrderInPutHZ.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('面料购销合同YX', Tv1, '生产指示单管理');     
end;

procedure TfrmMLOrderInPutHZ.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *');
    sql.Add(',IFFZ=(select count(B.WBID) from TP_File B where B.WBID=A.CSID and B.TFType=''合同明细'')');
    SQL.Add(' from ML_ConSub A where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and MLConID=''' + 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_ConMain where MLConID=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOQuery1, ScrollBox1, 2);
  KHName.TxtCode:=Trim(ADOQuery1.fieldbyname('KHNO').AsString);
  SCSHDataNew(ADOQuery1, ScrollBox2, 2);
  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    JHDate.DateTime :=OrdDate.DateTime;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    YWConNO.Text:='';
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MLConID').Value := '';
        FieldByName('CSID').Value := '';
        Post;
        Next;
      end;
    end;
  end;
  if Trim(FMainId)='' then
  begin
    SYRYWY.Text:=Trim(SYRYWYF);
    SYRYWFZR.Text:=Trim(SYRYWFZRF);
    JHType.Text:='供方仓库需方自提。';
    ZLBiaoZhun.Text:='按我司提供的品质样。';
    QtyBiaoZhun.Text:='允许±10%以内。';
    BZYaoQiu.Text:='单卷装薄膜袋。';
    JSType.Text:='带款提货。';
    HZQiXian.Text:='按生产确认样为基准，如出现于基准品质不符时，自供方发货期起15天内提出异议，需方需提供书面件和产品不符证据，则供方不承担责任！未经过同意私自开剪同样供方不承担责任。';
    WYZeRen.Text:='双方本着诚信的原则，友好双方协商解决，协商不成由签约地法院仲裁，合同修改需提供书面或有效证明（传真件有效法律效益）！';
    with ADOQuery1 do
    begin
      close;
      sql.Clear;
      sql.Add('select Top 1 SYRYWTel,SYRPhone from ML_ConMain ');
      sql.Add('where SYRYWY=''' + trim(SYRYWY.Text) + ''' order by OrdDate desc');
      open;
    end;
    SYRYWTel.Text := Trim(ADOQuery1.fieldbyname('SYRYWTel').AsString);
    SYRPhone.Text := Trim(ADOQuery1.fieldbyname('SYRPhone').AsString);
  end;
end;

procedure TfrmMLOrderInPutHZ.FormShow(Sender: TObject);
var
  fsj1,fsj2:String;
begin
  readCxGrid('面料购销合同YX', Tv1, '生产指示单管理');
  fsj1:=Copy(FConType,1,4);
  fsj2:=Copy(FConType,5,4);
  Label1.Caption:='面料'+fsj1+'合同';
  Label5.Caption:=fsj2;
  InitData();
end;

function TfrmMLOrderInPutHZ.SaveData(): Boolean;
var
  maxno, FConNO, FUserID, FNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'ML', 'ML_ConMain', 3, 1) = False then      
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      if GetLSNo(ADOCmd, FConNO, 'HT', 'ML_ConMain', 4, 0) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      ConNO.Text := Trim(FConNO);
      with ADOQuery1 do
      begin
        close;
        sql.Clear;
        sql.Add('select UserID from SY_User ');
        sql.Add(' where UserName=''' + trim(SYRYWY.Text) + '''');
        open;
      end;
      FUserID := Trim(ADOQuery1.fieldbyname('UserID').AsString) + trim(formatdatetime('yy', OrdDate.Date)) + trim(formatdatetime('MM', OrdDate.Date));
      if GetLSNo(ADOCmd, FNo, FUserID, 'ML_ConMain', 2, 0) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      YWConNO.Text := Trim(FNo);
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ML_ConMain where MLConID=''' + 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('MLConID').Value := Trim(maxno);
      FieldByName('ConType').Value :=FConType;
      SSetsaveSqlNew(ADOCmd, 'ML_ConMain', ScrollBox1, 2);
      SSetsaveSqlNew(ADOCmd, 'ML_ConMain', ScrollBox2, 2);
      FieldByName('ChkStatus').Value := '未审核';
      FieldByName('KHNo').Value :=Trim(KHName.TxtCode);
      if Trim(Label5.Caption)='内销' then
      begin
        FieldByName('BZType').Value := '￥';
      end else
      if Trim(Label5.Caption)='外销' then
      begin
        FieldByName('BZType').Value := '$';
      end;
      Post;
    end;
    FMainId := Trim(maxno);
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('CSID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'MS', 'ML_ConSub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('CSID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from ML_ConSub where MLConID=''' + Trim(FMainId) + '''');
          sql.Add(' and CSID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('CSID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MLConID').Value := Trim(FMainId);
          FieldByName('CSID').Value := Trim(maxno);
          FieldByName('MLID').Value := Trim(Order_Sub.fieldbyname('MLID').AsString);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'ML_ConSub', 0);
          fieldbyname('Qty').Value := Order_Sub.FieldByName('Qty').AsFloat;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('CSID').Value := Trim(maxno);
        Order_Sub.FieldByName('MLConID').Value := Trim(FMainId);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmMLOrderInPutHZ.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if SYRName.Text = '' then
  begin
    Application.MessageBox('供方不能为空', '提示');
    exit;
  end;
  if KHName.Text = '' then
  begin
    Application.MessageBox('需方不能为空', '提示');
    exit;
  end;
  if SYRYWY.Text = '' then
  begin
    Application.MessageBox('业务员不能为空', '提示');
    exit;
  end;
  if SYRYWFZR.Text = '' then
  begin
    Application.MessageBox('业务助理不能为空', '提示');
    exit;
  end;
  if JHType.Text = '' then
  begin
    Application.MessageBox('交货方式不能为空', '提示');
    exit;
  end;
  if CheckBox2.Checked = True then
  begin
    if KPCodeName.Text = '' then
    begin
      Application.MessageBox('开票品名不能为空', '提示');
      exit;
    end;
  end;
  if SYRYWFZR.Text = '' then
  begin
    Application.MessageBox('业务助理不能为空', '提示');
    exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmMLOrderInPutHZ.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('ConColor').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('ConColorEng').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMLOrderInPutHZ.ToolButton1Click(Sender: TObject);
begin
  Panel4.Visible := true;
end;

procedure TfrmMLOrderInPutHZ.KHNameBtnUpClick(Sender: TObject);
begin
  try
    frmKHList_CX := TfrmKHList_CX.Create(Application);
    with frmKHList_CX do
    begin
      if ShowModal = 1 then
      begin
        KHName.Text := Trim(CDS_HZ.fieldbyname('KHName').AsString);
        KHName.TxtCode := Trim(CDS_HZ.fieldbyname('KHNo').AsString);
        KHAdd.Text := Trim(CDS_HZ.fieldbyname('KHAdd').asstring);
        KHTel.Text := Trim(CDS_HZ.fieldbyname('KHTel').asstring);
        KHYWFZR.Text := Trim(CDS_HZ.fieldbyname('KHLXR').asstring);
        KHPhone.Text := Trim(CDS_HZ.fieldbyname('KHTel').asstring);
        KHBankName.Text := Trim(CDS_HZ3.fieldbyname('BankName').asstring);
        KHBankNo.Text := Trim(CDS_HZ3.fieldbyname('BankNo').asstring);
      end;
    end;
  finally
    frmKHList_CX.Free;
  end;
end;

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

procedure TfrmMLOrderInPutHZ.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 := 'OrderUnit';
      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 TfrmMLOrderInPutHZ.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 TfrmMLOrderInPutHZ.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 TfrmMLOrderInPutHZ.ConNoKeyPress(Sender: TObject; var Key: Char);
var
  ConMainId: string;
begin

end;

procedure TfrmMLOrderInPutHZ.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 TfrmMLOrderInPutHZ.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 TfrmMLOrderInPutHZ.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyAttachCP_HX_Sel := TfrmZdyAttachCP_HX_Sel.create(self);
  with frmZdyAttachCP_HX_Sel do
  begin
    if showmodal = 1 then
    begin
      Order_Sub.Edit;
      Order_Sub.FieldByName('PRTHX').Value := trim(cds_Hz.FieldByName('ZdyName').asstring);
      Order_Sub.Post;
    end;
    frmZdyAttachCP_HX_Sel.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;


procedure TfrmMLOrderInPutHZ.Initimage();
var
  ListItem: TListItem;
  Flag: Cardinal;
  info: SHFILEINFOA;
  Icon: TIcon;
begin
  ListView1.Items.Clear;
  with adoqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select fileName from TP_File ');
    sql.Add('where WBID=''' + trim(Order_Sub.fieldbyname('CSID').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 TfrmMLOrderInPutHZ.ListView1DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
begin
  if listView1.SelCount < 1 then
  begin
    listView1.Visible := false;
    exit;
  end;
  if ListView1.Items.Count < 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 TfrmMLOrderInPutHZ.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_Sub.fieldbyname('CSID').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, 'NX', '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_Sub.fieldbyname('CSID').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_Sub.fieldbyname('CSID').AsString) + '''');
        sql.Add(' and TFType=''合同明细''');
        sql.Add(' and FileName=' + quotedstr(trim(fFileName)));
        open;
        append;
        fieldbyname('TFID').Value := trim(maxNo);
        fieldbyname('WBID').Value := trim(Order_Sub.fieldbyname('CSID').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();
    end;
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmMLOrderInPutHZ.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_Sub.fieldbyname('CSID').AsString) + '''');
    sql.Add(' and TFType=''合同明细''');
    sql.Add('and FileName=' + quotedstr(trim(fFileName)));
    execsql;
  end;
  Panel12.Visible := False;
  Initimage();
end;

procedure TfrmMLOrderInPutHZ.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_Sub.fieldbyname('CSID').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_Sub.fieldbyname('CSID').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_Sub.fieldbyname('CSID').AsString) + '''');
      sql.Add(' and TFType=''合同明细''');
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      open;
      append;
      fieldbyname('TFID').Value := trim(maxNo);
      fieldbyname('WBID').Value := trim(Order_Sub.fieldbyname('CSID').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 TfrmMLOrderInPutHZ.Image1Click(Sender: TObject);
begin
  Panel12.Visible := false;
end;

procedure TfrmMLOrderInPutHZ.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 TfrmMLOrderInPutHZ.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_Sub.fieldbyname('CSID').AsString) + '''');
    sql.Add(' and TFType=''合同明细''');
    sql.Add('and FileName=' + quotedstr(trim(fFileName)));
    execsql;
  end;
  Initimage();
end;

procedure TfrmMLOrderInPutHZ.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 TfrmMLOrderInPutHZ.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 TfrmMLOrderInPutHZ.IFHSClick(Sender: TObject);
begin
  if IFHS.Checked = True then
  begin
    IFBHS.Checked := false;
  end
  else
  begin
    IFBHS.Checked := true;
  end;
end;

procedure TfrmMLOrderInPutHZ.CheckBox2Click(Sender: TObject);
begin
  if checkbox2.Checked = true then
  begin
    KPCodeName.Visible := true;
  end
  else
  begin
    KPCodeName.Visible := false;
  end;
end;

procedure TfrmMLOrderInPutHZ.CheckBox1Click(Sender: TObject);
begin
  if checkbox1.Checked = true then
  begin
    BCShuoMing.Visible := true;
  end
  else
  begin
    BCShuoMing.Visible := false;
  end;
end;

procedure TfrmMLOrderInPutHZ.SYRNameBtnUpClick(Sender: TObject);
begin
  frmGSList_CX := TfrmGSList_CX.create(self);
  with frmGSList_CX do
  begin
    cxGrid3.Visible := false;
    if showmodal = 1 then
    begin
      SYRName.Text := Trim(CDS_HZ.fieldbyname('KHName').asstring);
      SYRAdd.Text := Trim(CDS_HZ.fieldbyname('KHAdd').asstring);
    end;
    free;
  end;
end;

procedure TfrmMLOrderInPutHZ.SYRFDRBtnClick(Sender: TObject);
begin
  frmZDYHelp := TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    Flag := 'SYRFDR';
    flagname := '法定代表人';
    if ShowModal = 1 then
    begin
      SYRFDR.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

procedure TfrmMLOrderInPutHZ.SYRYWYBtnClick(Sender: TObject);
begin
  frmZDYHelpYW := TfrmZDYHelpYW.Create(self);
  with frmZDYHelpYW do
  begin
    if ShowModal = 1 then
    begin
      SYRYWY.Text := Trim(ClientDataSet1.fieldbyname('UserName').AsString);
      with ADOQuery1 do
      begin
        close;
        sql.Clear;
        sql.Add('select Top 1 SYRYWTel,SYRPhone from ML_ConMain ');
        sql.Add('where SYRYWY=''' + trim(SYRYWY.Text) + ''' order by OrdDate desc');
        open;
      end;
      SYRYWTel.Text := Trim(ADOQuery1.fieldbyname('SYRYWTel').AsString);
      SYRPhone.Text := Trim(ADOQuery1.fieldbyname('SYRPhone').AsString);
    end;
    free;
  end;
end;

procedure TfrmMLOrderInPutHZ.SYRYWFZRBtnClick(Sender: TObject);
begin
  frmZDYHelpYW := TfrmZDYHelpYW.Create(self);
  with frmZDYHelpYW do
  begin
    if ShowModal = 1 then
    begin
      SYRYWFZR.Text := Trim(ClientDataSet1.fieldbyname('UserName').AsString);
    end;
    free;
  end;
end;

procedure TfrmMLOrderInPutHZ.Button5Click(Sender: TObject);
var
  i:Integer;
begin
  self.Panel4.Visible := false;
  try
    frmZHCPListSel:=TfrmZHCPListSel.Create(Application);
    with frmZHCPListSel do
    begin
      if ShowModal=1 then
      begin
        i:=Self.Order_Sub.RecordCount;
        with Self.Order_Sub do
        begin
          Append;
          FieldByName('ConCode').Value:=Trim(frmZHCPListSel.Order_Main.fieldbyname('ZIID').AsString);
          FieldByName('ConCodeName').Value:=Trim(frmZHCPListSel.Order_Main.fieldbyname('ZIName').AsString);
          FieldByName('ConGY').Value:=Trim(frmZHCPListSel.Order_Main.fieldbyname('ZIGY').AsString);
          FieldByName('XHNo').Value:=i+1;
          Post;
        end;
      end;
    end;
  finally
    frmZHCPListSel.Free;
  end;
end;

procedure TfrmMLOrderInPutHZ.Image2Click(Sender: TObject);
begin
  Panel4.Visible := false;
end;

procedure TfrmMLOrderInPutHZ.Button13Click(Sender: TObject);
begin
  frmGSList_CX := TfrmGSList_CX.create(self);
  with frmGSList_CX do
  begin
    if showmodal = 1 then
    begin
      SYRBankName.Text := Trim(CDS_HZ3.fieldbyname('BankName').asstring);
      SYRBankNo.Text := Trim(CDS_HZ3.fieldbyname('BankNo').asstring);
      if Trim(CDS_HZ3.fieldbyname('BankType').asstring) = '现金账户' then
      begin
        CheckBox2.Checked := false;
        KPCodeName.Visible := False;
      end
      else
      begin
        CheckBox2.Checked := true;
        KPCodeName.Visible := true;
      end;
    end;
    free;
  end;
end;

procedure TfrmMLOrderInPutHZ.IFBHSClick(Sender: TObject);
begin
  if IFBHS.Checked = True then
  begin
    IFHS.Checked := false;
  end
  else
  begin
    IFHS.Checked := true;
  end;
end;

procedure TfrmMLOrderInPutHZ.ToolButton2Click(Sender: TObject);
var
  i:Integer;
begin
  if Order_Sub.IsEmpty then Exit;
  CopyAddRowCDS(Order_Sub);
  i:=Order_Sub.RecordCount;
  with Order_Sub do
  begin
    Edit;
    FieldByName('MLConId').Value:=null;
    FieldByName('CSID').Value:=null;
    FieldByName('ConColor').Value:=null;
    FieldByName('ConColNo').Value:=null;
    FieldByName('ConKHColNo').Value:=null;
    FieldByName('XHNo').Value:=i;
    Post;
  end;
end;

procedure TfrmMLOrderInPutHZ.v1QtyPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FBZFee,FFieldName,FFeeMoney:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  FPrice:=Trim(Order_Sub.fieldbyname('ConPrice').AsString);
  if Trim(FPrice)='' then FPrice:='0';
  FQty:=Trim(Order_Sub.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';
  FFeeMoney:=Trim(Order_Sub.fieldbyname('FeeMoney').AsString);
  if Trim(FFeeMoney)='' then FFeeMoney:='0';

  FMoney:=Trim(Order_Sub.fieldbyname('ConMoney').AsString);
  if Trim(FMoney)='' then FMoney:='0';

  if Trim(FFieldName)<>'ConMoney' then
  begin
    FMoney:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice)+StrToFloat(FFeeMoney));
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('ConMoney').Value:=StrToFloat(FMoney);
    Post;
  end;
  
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmMLOrderInPutHZ.ToolButton3Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('CSID').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ML_OrderSubidNew where isnull(ConSubId,'''')='''+Trim(Order_Sub.fieldbyname('CSID').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_ConSub where CSID=''' + Trim(Order_Sub.fieldbyname('CSID').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmMLOrderInPutHZ.ToolButton4Click(Sender: TObject);
var
  i:Integer;
begin
  i:=Self.Order_Sub.RecordCount;
  with Self.Order_Sub do
  begin
    Append;
    FieldByName('XHNo').Value:=i+1;
    Post;
  end;
end;


procedure TfrmMLOrderInPutHZ.v1ConHXPropertiesButtonClick(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('ConHX').Value:=Trim(frmZdyAttachCP_HX_Sel.CDS_HZ.fieldbyname('zdyName').AsString);
        end;
      end;
    end;
  finally
    frmZdyAttachCP_HX_Sel.Free;
  end;
end;

procedure TfrmMLOrderInPutHZ.ToolButton5Click(Sender: TObject);
var
  FName,mvalue,FCaption:string;
begin
  ToolBar2.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)='颜色(英文)' then
  begin
    Application.MessageBox('颜色(英文)不能一键替换！','提示',0);
    Exit;
  end;
  if Trim(FCaption)='COLOR' then
  begin
    Application.MessageBox('COLOR不能一键替换1','提示',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 TfrmMLOrderInPutHZ.v1Column2PropertiesButtonClick(
  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 TfrmMLOrderInPutHZ.PriceTKBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PriceTKYX';
      flagname:='价格术语';
      if ShowModal=1 then
      begin
        PriceTK.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMLOrderInPutHZ.ToPlaceBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ToPlaceYX';
      flagname:='目的地';
      if ShowModal=1 then
      begin
        ToPlace.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMLOrderInPutHZ.FromPlaceBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FromPlaceYX';
      flagname:='出运港口';
      if ShowModal=1 then
      begin
        FromPlace.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

