unit U_ConInPutWX;

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,
  cxDropDownEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmConInPutWX = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    OrdDate: TDateTimePicker;
    Label4: TLabel;
    DlyDate: TDateTimePicker;
    Label7: TLabel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1PRTColor: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    v1PRTOrderQty: TcxGridDBColumn;
    Label9: TLabel;
    Label10: TLabel;
    ConNo: TEdit;
    Label14: TLabel;
    Label20: TLabel;
    FromPlace: TBtnEditC;
    BuyName: TBtnEditC;
    v1XHNo: TcxGridDBColumn;
    SellName: TBtnEditC;
    v1OrderUnit: TcxGridDBColumn;
    Label6: TLabel;
    Label15: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    SellBankNo: TBtnEditC;
    SellAddress: TEdit;
    Label19: TLabel;
    SellBankName: TEdit;
    Label21: TLabel;
    BuyAddress: TEdit;
    v1Column3: TcxGridDBColumn;
    Label8: TLabel;
    sellbankaddress: TEdit;
    Label23: TLabel;
    SellFax: TEdit;
    CDS_Type: TClientDataSet;
    Label5: TLabel;
    Label16: TLabel;
    Label28: TLabel;
    Label22: TLabel;
    Label24: TLabel;
    BuyTel: TEdit;
    BuyFax: TEdit;
    Label31: TLabel;
    Label34: TLabel;
    BuyConNo: TEdit;
    Label35: TLabel;
    Label36: TLabel;
    BuyBankName: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Salesman: TBtnEditC;
    Label39: TLabel;
    SellNOTE: TMemo;
    QtyNote: TBtnEditC;
    PZFYNote: TMemo;
    ChuanYangNote: TMemo;
    YSBiaoZhunNote: TMemo;
    Note: TMemo;
    v1Column1: TcxGridDBColumn;
    BuyBankNo: TEdit;
    v1Column8: TcxGridDBColumn;
    Label1: TLabel;
    DlyNote: TBtnEditC;
    Label2: TLabel;
    DlyPlace: TBtnEditC;
    v1Column9: TcxGridDBColumn;
    Label11: TLabel;
    BUYNOTE: TMemo;
    Label12: TLabel;
    Label13: TLabel;
    YiYiNote: TMemo;
    Label25: TLabel;
    v1Column5: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    ToolButton3: TToolButton;
    PAYMENT: TBtnEditC;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    Label26: TLabel;
    PACKING: TBtnEditC;
    Label27: TLabel;
    PICHANG: TBtnEditC;
    Label29: TLabel;
    AGENT: TBtnEditC;
    Label30: TLabel;
    CARRIER: TBtnEditC;
    Label32: TLabel;
    MILL: TDateTimePicker;
    Label33: TLabel;
    CLABLE: TBtnEditC;
    Label37: TLabel;
    Insurance: TBtnEditC;
    Label38: TLabel;
    SAMPLE: TBtnEditC;
    Label40: TLabel;
    YongjinQty: TEdit;
    Label41: TLabel;
    HSCODE: TBtnEditC;
    Tv1Column1: TcxGridDBColumn;
    CaID: TEdit;
    Label42: TLabel;
    DJBL: TEdit;
    Label43: TLabel;
    QTFYQty: TEdit;
    QTFYUnit: TComboBox;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label44: TLabel;
    SHADDRESS: TBtnEditC;
    Tv1Column4: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    Label45: TLabel;
    Label46: TLabel;
    YJSM: TEdit;
    JSFS: TComboBox;
    Label47: TLabel;
    JQNOTE: TBtnEditC;
    Label48: TLabel;
    KZYD: TEdit;
    Label49: TLabel;
    Label50: TLabel;
    CUSTPO: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure BuyNameBtnUpClick(Sender: TObject);
    procedure BuyNameBtnDnClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SalesmanBtnUpClick(Sender: TObject);
    procedure SellNOTEDblClick(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure SellBankNoBtnUpClick(Sender: TObject);
    procedure SellNameBtnUpClick(Sender: TObject);
    procedure BUYNOTEDblClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure AGENTBtnUpClick(Sender: TObject);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CLABLEBtnUpClick(Sender: TObject);
  private
    fuserName: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    procedure GetSell(MSellName: string);
    procedure GetBUY(MBUYName: string);
    function TestData(): Boolean;
    function SaveData(): Boolean;
    { Private declarations }
  public
    CopyInt: Integer;
    PState: string;
    FMainId, FFMainId: string;
    canshu1: string;
    fFlileFlag: string;
    MXNO: string;
    { Public declarations }
  end;

var
  frmConInPutWX: TfrmConInPutWX;
  newh: hwnd;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZDYHelpSel, U_CPManageFMSel, U_ZdyAttachment,
  getpic, U_WSZKSel, U_WSNameList;

{$R *.dfm}
procedure TfrmConInPutWX.GetSell(MSellName: string);
begin
//  with ADOTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select top 1 A.* ');
//    sql.Add(' from SalesContract_Main A  ');
//    SQL.Add('where  A.SCMType=''外销'' and A.SellName=' + quotedstr(trim(MSellName)));
//    sql.Add(' order by  A.OrdDate desc ');
//    Open;
//  end;
//  Sellnote.Text := trim(ADOTemp.FieldByName('SELLNOTE').AsString);
//  SellName.Text := trim(MSellName);
  //  SellTel.Text := trim(ADOTemp.FieldByName('SellTel').AsString);
//  SellBankNo.Text := trim(ADOTemp.FieldByName('SellBankNo').AsString);
//  SellBankName.Text := trim(ADOTemp.FieldByName('SellBankName').AsString);
//  SellFax.Text := trim(ADOTemp.FieldByName('SellFax').AsString);
end;

procedure TfrmConInPutWX.GetBUY(MBUYName: string);
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select top 1 A.* ');
    sql.Add(' from SalesContract_Main A  ');
    SQL.Add('where  A.SCMType=''外销'' and A.BUYName=' + quotedstr(trim(MBUYName)));
    sql.Add(' order by  A.OrdDate desc ');
    Open;
  end;
  BUYNOTE.Text := trim(ADOTemp.FieldByName('BUYNOTE').AsString);
//  SellTel.Text := trim(ADOTemp.FieldByName('SellTel').AsString);
//  SellBankNo.Text := trim(ADOTemp.FieldByName('SellBankNo').AsString);
//  SellBankName.Text := trim(ADOTemp.FieldByName('SellBankName').AsString);
//  SellFax.Text := trim(ADOTemp.FieldByName('SellFax').AsString);
  BuyName.Text := trim(MBUYName);
end;

function TfrmConInPutWX.TestData(): Boolean;
var
  FREAL: Double;
begin
  try
    if Trim(ConNo.Text) = '' then
      raise Exception.Create('合同号不能为空！');
//    if Trim(KHConNo.Text)='' then raise Exception.Create('客户合同号不能为空！');
//    if Trim(QtyNote.Text)='' then   raise Exception.Create('溢短装不能为空！');
    if Trim(SellName.Text) = '' then
      raise Exception.Create('供方不能为空！');
    if Trim(BuyName.Text) = '' then
      raise Exception.Create('需方不能为空！');

    if TryStrToFloat(YongjinQty.Text, FREAL) = False then
      raise Exception.Create('佣金非数字！');

    if TryStrToFloat(KZYD.Text, FREAL) = False then
      raise Exception.Create('克重溢短非数字！');

    if Order_Sub.IsEmpty then
      raise Exception.Create('明细不能为空！');
    if Order_Sub.Locate('SCSCode', null, []) then
      raise Exception.Create('产品编号不能为空！');
    if Order_Sub.Locate('SCSCode', '', []) then
      raise Exception.Create('产品编号不能为空！');
    if Order_Sub.Locate('SCSName', null, []) then
      raise Exception.Create('品名不能为空！');
    if Order_Sub.Locate('SCSName', '', []) then
      raise Exception.Create('品名不能为空！');
//    if Order_Sub.Locate('SCSColor', null, []) then
//      raise Exception.Create('颜色不能为空！');
//    if Order_Sub.Locate('SCSColor', '', []) then
//      raise Exception.Create('颜色不能为空！');
//    if Order_Sub.Locate('SCSKX',null,[]) then raise Exception.Create('款型不能为空！');
//    if Order_Sub.Locate('SCSKX','',[]) then raise Exception.Create('款型不能为空！');
    if Order_Sub.Locate('SCSMF', null, []) then
      raise Exception.Create('门幅不能为空！');
    if Order_Sub.Locate('SCSMF', '', []) then
      raise Exception.Create('门幅不能为空！');
    if Order_Sub.Locate('SCSKZ', null, []) then
      raise Exception.Create('克重不能为空！');
    if Order_Sub.Locate('SCSKZ', '', []) then
      raise Exception.Create('克重不能为空！');
    if Order_Sub.Locate('SCSQty', null, []) then
      raise Exception.Create('数量不能为空！');
    if Order_Sub.Locate('SCSQty', 0, []) then
      raise Exception.Create('数量不能为0！');
    if Order_Sub.Locate('SCSUnit', null, []) then
      raise Exception.Create('数量单位不能为空！');
    if Order_Sub.Locate('SCSUnit', '', []) then
      raise Exception.Create('数量单位不能为空！');
//    if Order_Sub.Locate('SCSCurrency', null, []) then
//      raise Exception.Create('币种不能为空！');
//    if Order_Sub.Locate('SCSCurrency', '', []) then
//      raise Exception.Create('币种不能为空！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmConInPutWX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(trim(self.Caption), Tv1, '外销合同录入2');
end;

procedure TfrmConInPutWX.InitData();
begin
  //刷新产品信息
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from SalesContract_Sub ');
    sql.Add('where MainId=''' + Trim(FMainId) + '''');
    sql.Add(' order by XHNo ');
    Open;
  end;
  SCreateCDS20(ADOTemp, Order_Sub);
  SInitCDSData20(ADOTemp, Order_Sub);
   //刷新条款信息
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SalesContract_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOTemp, ScrollBox1, 2);
  self.BuyName.TxtCode := Trim(ADOTemp.FieldByName('BuyNo').AsString);
  self.SellName.TxtCode := Trim(ADOTemp.FieldByName('SellNo').AsString);
  if PState = '新增' then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := OrdDate.DateTime;
    MILL.DateTime := OrdDate.DateTime;
//    ConNo.Text := '自动生成';

  end;

  if PState = '复制' then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    PState := '新增';
    FMainId := '';
    Salesman.Text := Trim(DName);
    ConNo.Text := '自动生成';

    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
    end;
  end;

end;

procedure TfrmConInPutWX.ZDYHelp(FButn: TcxButtonEdit; LType: string);
var
  FType, ZDYName, FText: string;
begin
end;

procedure TfrmConInPutWX.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.Caption), Tv1, '外销合同录入2');
  InitData();
end;

function TfrmConInPutWX.SaveData(): Boolean;
var
  maxno: string;
  fconNO, fmxType: string;
  FTISHI: string; //如果颜色修改则提示
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'SCM', 'SalesContract_Main', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      if trim(ConNo.Text) = '自动生成' then
      begin
        if GetLSNo(ADOCmd, fconNO, 'WX', 'SalesContract_Main', 2, 1) = False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('取最合同编号失败!', '提示', 0);
          Exit;
        end;
        ConNo.Text := fconNO;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from SalesContract_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('status').Value := '-1';
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      if (FMainId = '') and (CLABLE.Text <> '') then
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('UPDATE TP_FilE SET WBID=' + QUOTEDSTR(Trim(maxno)));
          SQL.Add('WHERE WBID=' + QUOTEDSTR(Trim(CLABLE.Text)));
          ExecSQL;
        end;
        CLABLE.Text := Trim(maxno);
      end;
      FieldByName('MainId').Value := Trim(maxno);
      SSetsaveSqlNew(ADOCmd, 'SalesContract_Main', ScrollBox1, 2);
      FieldByName('BuyNo').Value := self.BuyName.TxtCode;
      FieldByName('SellNo').Value := self.SellName.TxtCode;
      FieldByName('SCMType').Value := '外销';
      Post;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from SalesContract_Main where ConNo=''' + Trim(ConNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('合同号重复!', '提示', 0);
      Exit;
    end;

    FMainId := Trim(maxno);
    ///保存子表

    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'SCS', 'SalesContract_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from SalesContract_Sub where MainId=''' + Trim(FMainId) + '''');
          sql.Add(' and SubId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
          begin
            Edit;
            if ADOCmd.fieldbyname('SCSColor').AsString <> Order_Sub.FieldByName('SCSColor').AsString then
            begin
              FTISHI := '1';
            end;
          end;

          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'SalesContract_Sub', 0);
          fieldbyname('huilv').Value := Order_Sub.fieldbyname('huilv').AsFloat;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);


        
        //Order_Sub.Post;
        Next;
      end;
    end;

    ADOCmd.Connection.CommitTrans;
    if FTISHI = '1' then
    begin
      Application.MessageBox('颜色已修改，请手动修改指示单！', '提示', 0);
    end;
    Result := True;

  except

    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
    Result := False;
  end;
end;

procedure TfrmConInPutWX.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;

  if TestData() = false then
    exit;

  if SaveData() then
  begin

    with ADOCmd do
    begin
      close;
      sql.Clear;

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('外销合同修改')));
      sql.Add(',' + quotedstr(trim('合同号：' + trim(ConNo.Text))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

    Application.MessageBox('保存成功！', '提示', 0);
  end;
end;

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

procedure TfrmConInPutWX.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  if Trim(ConNo.Text) = '' then
  begin
    Application.MessageBox('合同号不能为空!', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount;
  i := i + 1;
  CopyAddRow(Tv1, Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('XHNO').Value := IntToStr(i);
    FieldByName('SCSColor').Value := '';
    FieldByName('SCSQty').Value := null;
    FieldByName('SCSCurrency').Value := 'USD';
    FieldByName('huilv').Value := 1;

    Post;
  end;
end;

procedure TfrmConInPutWX.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(Order_Sub.fieldbyname('SubId').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from  JYOrder_sub where conSubId=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + '''');
      open;
    end;
    if ADOTemp.IsEmpty = false then
    begin
      exit;
      application.MessageBox('该明细已经在生产！', '提示信息');
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SalesContract_Sub where SubId=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmConInPutWX.BuyNameBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if self.canshu1 = '业务员' then
        canshu1 := '业务员';
      if ShowModal = 1 then
      begin
        self.BuyName.TxtCode := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        self.BuyName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
//        self.BuyAddress.Text := Trim(CDS_HZ.fieldbyname('CoAddress').AsString);
//        self.BuyTel.Text := Trim(CDS_HZ.fieldbyname('Telephone').AsString);
//        self.BuyFax.Text := Trim(CDS_HZ.fieldbyname('Fax').AsString);
//        self.BuyBankNo.Text := Trim(CDS_HZ.fieldbyname('BankNo').AsString);
//        self.BuyBankName.Text := Trim(CDS_HZ.fieldbyname('BankName').AsString);
//        self.Salesman.Text := Trim(CDS_HZ.fieldbyname('Salesman').AsString);
      end;
      GetBUY(BuyName.Text);
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmConInPutWX.BuyNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmConInPutWX.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
type
  TMyFunc = function(App: Tapplication; FormH: hwnd; FormID: integer; Language: integer; WinStyle: integer; GCode: Pchar; GName: Pchar; DataBase: Pchar; Title: PChar; Parameters1: PChar; Parameters2: PChar; Parameters3: PChar; Parameters4: PChar; Parameters5: PChar; Parameters6: PChar; Parameters7: PChar; Parameters8: PChar; Parameters9: PChar; Parameters10: PChar; DataBaseStr: PChar): hwnd; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th: Thandle;
  LabInt, labname: string;
begin
  //if PPInt=2 then Exit;
  Ddatabase := StringOfChar(' ', 32);
  Th := LoadLibrary('LabelSet.dll');
  if Th > 0 then
  begin
    try
      Tp := GetProcAddress(Th, 'GetDllForm');
      if Tp <> nil then
      begin
        Tf := TMyFunc(Tp);
        newh := Tf(Application, 0, 2, 0, 0, PChar(DCode), PChar(DName), PChar(Ddatabase), PChar('标签模版设置'), PChar(''), PChar(''), '', '', '', '', '', '', '', '', PChar(DConString));
        if Trim(PChar(Ddatabase)) <> '' then
        begin
          Ddatabase := Trim(PChar(Ddatabase));
          LabInt := Trim(LeftBStr(Ddatabase, Pos('|', Ddatabase) - 1));
          labname := Trim(RightBStr(Ddatabase, Length(Ddatabase) - Pos('|', Ddatabase)));
          with Order_Sub do
          begin
            Edit;
            FieldByName('SLbName').Value := labname;
            FieldByName('SLbInt').Value := LabInt;
          end;
        end;
      end
      else
      begin
        ShowMessage('打印执行错误');
      end;
    finally
    // FreeLibrary();
    end;
  end
  else
  begin
    ShowMessage('找不到' + Trim('LabelSet.dll'));
  end;

end;

procedure TfrmConInPutWX.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 TfrmConInPutWX.v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTCodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Order_Sub.fieldbyname('PRTCode').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmConInPutWX.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTMF';
      flagname := '门幅信息';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTMF').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmConInPutWX.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTKZ';
      flagname := '克重信息';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTKZ').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmConInPutWX.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MCYNo: string;
begin
  frmCPManageFMSel := TfrmCPManageFMSel.Create(self);
  with frmCPManageFMSel do
  begin
    if showmodal = 1 then
    begin
      Order_Sub.Edit;
      MCYNo := CDS_Main.fieldbyname('CYNo').asstring;
      Order_Sub.fieldbyname('SCSCode').Value := MCYNo;
      Order_Sub.fieldbyname('SCSName').Value := CDS_Main.fieldbyname('CYName').asstring;
      Order_Sub.fieldbyname('SCSSpec').Value := CDS_Main.fieldbyname('CYSpec').asstring;
      Order_Sub.fieldbyname('SCSCF').Value := CDS_Main.fieldbyname('CYCF').asstring;
      Order_Sub.fieldbyname('SCSColor').Value := CDS_Main.fieldbyname('CYColor').asstring;
      Order_Sub.fieldbyname('SCSHX').Value := CDS_Main.fieldbyname('CYHX').asstring;
      Order_Sub.fieldbyname('SCSMF').Value := CDS_Main.fieldbyname('CYMF').asstring;
      Order_Sub.fieldbyname('SCSKZ').Value := CDS_Main.fieldbyname('CYKZ').asstring;
//      Order_Sub.fieldbyname('SCSNote').Value := CDS_Main.fieldbyname('CYNote').asstring;
      Order_Sub.Post;
    end;
    free;
  end;

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select top 1 SCSEName from SalesContract_Sub where SCSCode=' + quotedstr(trim(MCYNo)));
    sql.Add(' order by subid desc');
    open;
    Order_Sub.Edit;
    Order_Sub.fieldbyname('SCSEName').Value := ADOTemp.Fieldbyname('SCSEName').asstring;
    Order_Sub.Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmConInPutWX.SalesmanBtnUpClick(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 TfrmConInPutWX.SellNOTEDblClick(Sender: TObject);
begin
  try
    frmWSZKSel := TfrmWSZKSel.Create(Application);
    with frmWSZKSel do
    begin
      FKHNo := self.SellName.TxtCode;
      if ShowModal = 1 then
      begin
        self.SellNOTE.Text := Trim(CDS_HZ.fieldbyname('ACCName').AsString);
        self.CaID.Text := Trim(CDS_HZ.fieldbyname('CaID').AsString);
      end;
    end;
  finally
    frmWSZKSel.Free;
  end;
end;

procedure TfrmConInPutWX.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('YongJinPrice').Value := mvalue;
    FieldByName('YongJin').Value := FieldByName('YongJinPrice').AsFloat * FieldByName('PRTOrderQty').AsFloat;
    Post;
  end;
end;

procedure TfrmConInPutWX.SellBankNoBtnUpClick(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);
      flag := 'SellBankNo';
      V1Name.Caption := '银行账户';
      V1Note.Caption := '开户行';
      fnote := True;
      if ShowModal = 1 then
      begin
        SellBankNo.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        SellBankName.Text := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmConInPutWX.SellNameBtnUpClick(Sender: TObject);
begin
  try
    frmWSNameList := TfrmWSNameList.Create(Application);
    with frmWSNameList do
    begin
      if ShowModal = 1 then
      begin
        SellName.TxtCode := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        SellName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        SellNOTE.Text := Trim(CDS_HZ.fieldbyname('ACCName').AsString);
      end;
    end;
    GetSell(SellName.Text);
  finally
    frmWSNameList.Free;
  end;
end;

procedure TfrmConInPutWX.BUYNOTEDblClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if self.canshu1 = '业务员' then
        canshu1 := '业务员';
      if ShowModal = 1 then
      begin
        self.BuyName.TxtCode := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        self.BuyName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        self.BuyAddress.Text := Trim(CDS_HZ.fieldbyname('CoAddress').AsString);
        self.BuyTel.Text := Trim(CDS_HZ.fieldbyname('Telephone').AsString);
        self.BuyFax.Text := Trim(CDS_HZ.fieldbyname('Fax').AsString);
        self.BuyBankNo.Text := Trim(CDS_HZ.fieldbyname('BankNo').AsString);
        self.BuyBankName.Text := Trim(CDS_HZ.fieldbyname('BankName').AsString);
        self.Salesman.Text := Trim(CDS_HZ.fieldbyname('Salesman').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmConInPutWX.ToolButton3Click(Sender: TObject);
var
  MCYNo: string;
  i: Integer;
begin
  frmCPManageFMSel := TfrmCPManageFMSel.create(self);
  with frmCPManageFMSel do
  begin
    if showmodal = 1 then
    begin
      cds_main.DisableControls;
      cds_main.first;
      while not cds_main.Eof do
      begin
        if cds_main.FieldByName('ssel').AsBoolean then
        begin
          i := Order_Sub.RecordCount;
          i := i + 1;
          with self.Order_Sub do
          begin
            Append;
            FieldByName('XHNO').Value := i;
            Order_Sub.Edit;
            MCYNo := CDS_Main.fieldbyname('CYNo').asstring;
            Order_Sub.fieldbyname('SCSCode').Value := MCYNo;
            Order_Sub.fieldbyname('SCSName').Value := CDS_Main.fieldbyname('CYName').asstring;
            Order_Sub.fieldbyname('SCSSpec').Value := CDS_Main.fieldbyname('CYSpec').asstring;
            Order_Sub.fieldbyname('SCSCF').Value := CDS_Main.fieldbyname('CYCF').asstring;
            Order_Sub.fieldbyname('SCSColor').Value := CDS_Main.fieldbyname('CYColor').asstring;
            Order_Sub.fieldbyname('SCSHX').Value := CDS_Main.fieldbyname('CYHX').asstring;
            Order_Sub.fieldbyname('SCSMF').Value := CDS_Main.fieldbyname('CYMF').asstring;
            Order_Sub.fieldbyname('SCSKZ').Value := CDS_Main.fieldbyname('CYKZ').asstring;
//            Order_Sub.fieldbyname('SCSNote').Value := CDS_Main.fieldbyname('CYNote').asstring;
            Order_Sub.fieldbyname('SCSCurrency').Value := 'USD';
            Order_Sub.Post;
          end;
          with ADOTemp do
          begin
            close;
            sql.Clear;
            sql.Add('select top 1 SCSEName from SalesContract_Sub where SCSCode=' + quotedstr(trim(MCYNo)));
            sql.Add(' order by subid desc');
            open;
            Order_Sub.Edit;
            Order_Sub.fieldbyname('SCSEName').Value := ADOTemp.Fieldbyname('SCSEName').asstring;
            Order_Sub.Post;
          end;
        end;
        cds_main.next;
      end;
      cds_main.EnableControls;
    end;
    frmCPManageFMSel.free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmConInPutWX.AGENTBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if self.canshu1 = '业务员' then
        canshu1 := '业务员';
      if ShowModal = 1 then
      begin
        self.AGENT.TxtCode := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        self.AGENT.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);

//        self.Salesman.Text := Trim(CDS_HZ.fieldbyname('Salesman').AsString);
      end;
//      GetBUY(BuyName.Text);
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmConInPutWX.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  FormGetPic := TFormGetPic.create(self);
  with FormGetPic do
  begin
    fFlileFlag := self.fFlileFlag;

    if GetLSNo(ADOCmd, MXNO, 'TP', 'SalesContract_Main', 3, 1) = False then
    begin
//      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    fkeyNo := MXNO;
    pat1 := 'FileName';
    pic1 := 'FilesOther';
    FTFType := 'HX';
    if showmodal = 1 then
    begin
      CLABLE.Text := Trim(fkeyNo);
    end;
    free;
  end;
end;

procedure TfrmConInPutWX.CLABLEBtnUpClick(Sender: TObject);
begin
  FormGetPic := TFormGetPic.create(self);
  with FormGetPic do
  begin
    fFlileFlag := self.fFlileFlag;
    if FMainId = '' then
    begin
      if GetLSNo(ADOCmd, MXNO, 'TP', 'SalesContract_Main', 3, 1) = False then
      begin
//      ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      fkeyNo := MXNO;
    end
    else
    begin
      fkeyNo := FMainId;
    end;

    pat1 := 'FileName';
    pic1 := 'FilesOther';
    FTFType := 'HX';
    if showmodal = 1 then
    begin
      CLABLE.Text := Trim(fkeyNo);
    end;
    free;
  end;
end;

end.

