unit U_OrderInPut;

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

type
  TfrmOrderInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label3: TLabel;
    OrderNo: TEdit;
    OrdDate: TDateTimePicker;
    Label4: TLabel;
    DlyDate: TDateTimePicker;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1PRTOrderQty: TcxGridDBColumn;
    Label9: TLabel;
    Label14: TLabel;
    CustomerNoName: TBtnEditC;
    v1XHNo: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    Label5: TLabel;
    ToolButton3: TToolButton;
    v1Column5: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Sorddefnote1: TcxGridDBColumn;
    Panel1: TPanel;
    Label22: TLabel;
    Label23: TLabel;
    OrdPerson1: TBtnEditA;
    ADO_KH: TADOQuery;
    DS_KH: TDataSource;
    Label29: TLabel;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    FromConNo: TBtnEditC;
    Label34: TLabel;
    BAnOrder: TEdit;
    Label35: TLabel;
    FromBuM: TBtnEditC;
    Label36: TLabel;
    WlFshi: TBtnEditC;
    Label37: TLabel;
    ShuoAddress: TBtnEditC;
    Label38: TLabel;
    SHPerson: TBtnEditC;
    v1Column1: TcxGridDBColumn;
    Picture4: TcxDBImage;
    Label40: TLabel;
    Note: TMemo;
    ADOQueryImage: TADOQuery;
    DS_Pic: TDataSource;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label2: TLabel;
    tybTel: TEdit;
    Label6: TLabel;
    OrderType: TComboBox;
    v1Column4: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure NoteDblClick(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 ToolButton3Click(Sender: TObject);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrdPerson1BtnClick(Sender: TObject);
    procedure OrdDateChange(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TV4CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TV4KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FromConNoBtnUpClick(Sender: TObject);
    procedure FromBuMBtnUpClick(Sender: TObject);
    procedure ShuoAddressBtnUpClick(Sender: TObject);
    procedure WlFshiBtnUpClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure SHPersonBtnUpClick(Sender: TObject);
  private
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    procedure InitImage();
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo, FDHCYID: string;
    FXS: Integer;
    fCustomerNoName: string;
    forderType, FBgroup: string;
    fsyrName, Fstatus: string;
    { Public declarations }
  end;

var
  frmOrderInPut: TfrmOrderInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachCP,
  U_MJEdit, U_ZdyAttachCP_CX, U_ZdyAttachSX, U_DDLYSel;

{$R *.dfm}
procedure TfrmOrderInPut.InitImage();
begin
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    if trim(Order_Sub.fieldbyname('FromOrdATID').AsString) <> '' then
    begin
      sql.Add('where WBID=' + quotedstr(trim(Order_Sub.fieldbyname('FromOrdATID').AsString)));
    end
    else
      sql.Add('where 1<>1');
//    ShowMessage(SQL.Text);
    open;
  end;
end;

procedure TfrmOrderInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('大货单录入1', Tv1, '大货单录入管理');
end;

procedure TfrmOrderInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,cast(0.0 as decimal(18,2)) as money from JYOrder_Sub A ');
    if PState = 1 then
    begin
      sql.Add('where MainId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  CustomerNoName.OnChange := nil;
  SCSHDataNew(ADOQuery1, ScrollBox1, 2);
  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDate10(ADOTemp);
    DlyDate.DateTime := SGetServerDate10(ADOTemp);
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '';
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
    end;
  end;

  if PState = 0 then
  begin
    OrderNo.Text := '自动生成';
    OrderNo.Enabled := false;
    FromBuM.Text := forderType;
  end;
  CustomerNoName.OnChange := CustomerNoNameChange;
end;

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

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  readCxGrid('大货单录入1', Tv1, '大货单录入管理');
  InitData();
  InitImage();
end;

function TfrmOrderInPut.SaveData(): Boolean;
var
  maxno: string;
  i: integer;
begin

  if Trim(FMainId) = '' then
  begin
    if GetLSNo(ADOTemp, maxno, 'JM', 'JYOrder_Main', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    maxno := Trim(FMainId);
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrder_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('FDHCYID').Value := Trim(FDHCYID);
      SSetsaveSqlNew(ADOCmd, 'JYOrder_Main', ScrollBox1, 2);
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('status').Value := '-1';
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      FieldByName('Note').Value := Trim(Note.Text);
      Post;
    end;
    FMainId := Trim(maxno);
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main where orderno=''' + Trim(OrderNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('申请单号重复!', '提示', 0);
      Exit;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('delete from JYOrder_Sub where MainId=''' + Trim(FMainId) + '''');
      execsql;
    end;
    ///保存子表
    with Order_Sub do
    begin
      DisableControls;
      First;
      while not Eof do
      begin

        with ADOQuery1 do
        begin
          Close;
          SQL.Clear;
          sql.Add('SELECT * FROM(');
          sql.Add(' SELECT A.QSFanW,A.JZFanW,A.YGuPrice,');
          sql.Add(' CASE WHEN A.QSFanW<=' + trim(Order_Sub.fieldbyname('PRTOrderQty').AsString));
          sql.Add(' AND A.JZFanW >=' + trim(Order_Sub.fieldbyname('PRTOrderQty').AsString));
          sql.Add(' and A.YGuPrice<=' + trim(Order_Sub.fieldbyname('PRTPrice').AsString));
          SQL.Add(' THEN ''自动''');
          sql.Add(' WHEN A.QSFanW<=' + trim(Order_Sub.fieldbyname('PRTOrderQty').AsString));
          sql.Add(' AND ISNULL(A.JZFanW, 0)=0 ');
          sql.Add(' and A.YGuPrice<=' + trim(Order_Sub.fieldbyname('PRTPrice').AsString));
          SQL.Add(' THEN ''自动''');
          sql.Add('  ELSE ''手动'' END as Statu');
          sql.Add(' FROM KH_Zdy_Attachment_Sub A');
          sql.Add(' WHERE A.ATID=' + quotedstr(trim(Order_Sub.fieldbyname('FromATID').AsString)));
          SQL.Add(' ) AA WHERE AA.Statu=''自动''');
            //ShowMessage(SQL.GetText);
          Open;
        end;
        if ADOQuery1.IsEmpty = False then
        begin
          with ADOTemp do
          begin
            Close;
            SQL.Clear;
            sql.Add('update  JYOrder_Main SET status=''1'',ColorFlag=''正常'' ');
            sql.Add('where MainId=' + quotedstr(trim(FMainId)));
            //ShowMessage(SQL.GetText);
            ExecSQL;
          end;

        end;

        if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
        begin
          if GetLSNo(ADOTemp, maxno, 'JS', 'JYOrder_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 JYOrder_Sub where MainId=''' + Trim(FMainId) + '''');
          sql.Add(' and SubId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOTemp do
        begin
          close;
          sql.Clear;
          sql.Add('select *  from KH_Zdy_Attachment A ');
          sql.Add('where zdyName=' + quotedstr(trim(Order_Sub.fieldbyname('PRTCodeName').AsString)));
          open;
        end;
        if not ADOTemp.IsEmpty then
        begin
          edit;
          fieldbyname('PRTPrice').Value := fieldbyname('PRTPrice').AsFloat + ADOTemp.fieldbyname('defflt1').AsFloat;
          post;
        end;

        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('orderNo').Value := Trim(orderNo.Text);
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          FieldByName('FromATID').Value := Trim(Order_Sub.fieldbyname('FromATID').AsString);
          FieldByName('FromOrdATID').Value := Trim(Order_Sub.fieldbyname('FromOrdATID').AsString);
          fieldbyname('PRTOrderQty').Value := Order_Sub.fieldbyname('PRTOrderQty').AsFloat;
          fieldbyname('PRTCode').Value := Order_Sub.fieldbyname('PRTCode').AsString;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
      Order_Sub.EnableControls;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    Order_Sub.EnableControls;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
    if GetLSNo(ADOTemp, maxno, 'JS', 'JYOrder_Sub', 4, 1) = False then
    begin
    end;
  end;
end;

procedure TfrmOrderInPut.TBSaveClick(Sender: TObject);
var
  forderNo: string;
begin
  OrdDate.SetFocus;

  if Trim(OrderNo.Text) = '' then
  begin
    Application.MessageBox('申请单号不能为空！', '提示', 0);
    Exit;
  end;

  if Trim(CustomerNoName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空！', '提示', 0);
    Exit;
  end;

  if Trim(FromConNo.Text) = '' then
  begin
    Application.MessageBox('订单来源不能为空！', '提示', 0);
    Exit;
  end;

  if Trim(OrdPerson1.Text) = '' then
  begin
    Application.MessageBox('申请人不能为空！', '提示', 0);
    Exit;
  end;

  if Trim(FromBuM.Text) = '' then
  begin
    Application.MessageBox('所在部门不能为空！', '提示', 0);
    Exit;
  end;

  if Order_Sub.Locate('PRTCodeName', null, []) = True then
  begin
    Application.MessageBox('产品名称不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTCodeName', '', []) = True then
  begin
    Application.MessageBox('产品名称不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTPrice', null, []) = True then
  begin
    Application.MessageBox('单价不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTPrice', 0, []) = True then
  begin
    Application.MessageBox('单价不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTOrderQty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTOrderQty', 0, []) = True then
  begin
    Application.MessageBox('数量不能为0!', '提示', 0);
    Exit;
  end;

//  with ADOTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from KH_Zdy_Attachment where zdyName=''' + Trim(CustomerNoName.Text) + '''');
//    Open;
//  end;
//  if ADOTemp.IsEmpty then
//  begin
//    Application.MessageBox('客户不存在!', '提示信息', 0);
//    Exit;
//  end;
  if PState = 1 then
  begin
    if trim(note.Text) = '' then
    begin
      Application.MessageBox('修改备注信息不能为空!', '提示', 0);
      Exit;
    end;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;
  if PState = 1 then
  begin
    if Trim(OrderNo.Text) <> Trim(forderNo) then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where MainId=''' + Trim(FMainId) + '''');
        Open;
      end;
      if ADOTemp.IsEmpty = False then
      begin
        Application.MessageBox('已产生数据，不能更改订单号!', '提示', 0);
        Exit;
      end;
    end;
  end
  else
  begin
    if trim(fSYRName) = '龙运纺织' then
    begin
      if GetLSNo(ADOCmd, forderNo, 'L', 'JYOrder_Main', 3, 1) = False then
      begin
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin

      if GetLSNo(ADOCmd, forderNo, '', 'JYOrder_Main', 3, 1) = False then
      begin
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;

    end;
    OrderNo.text := trim(forderNo);
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    if PState = 0 then
    begin
      FMainId := '';
      SClearData(ScrollBox1, 2);
      if trim(fsyrName) <> '' then
      begin

      end;

      InitData();
      Label29.Caption := '单价：';
      Label22.Caption := '匹数：';
      Label23.Caption := '数量：';
      Note.Text := '';
    end
    else
      ModalResult := 1;
  end;
end;

procedure TfrmOrderInPut.v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachCP := TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTCode').Value := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        Self.Order_Sub.FieldByName('PRTCodeName').Value := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('PRTColor').Value := Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmOrderInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;

  if Trim(Order_Sub.fieldbyname('SubId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where subID=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + ''' and CRType=''应收款登记''');
      Open;
    end;
    if ADOTemp.IsEmpty = False then
    begin
      Application.MessageBox('已产生数据，不能更改订单号!', '提示', 0);
      Exit;
    end;
  end;
  Order_Sub.Delete;

end;

procedure TfrmOrderInPut.CustomerNoNameBtnUpClick(Sender: TObject);
begin
  CustomerNoName.OnChange := nil;
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      fkhType := '';
      fkhType := self.fsyrName;
      if ShowModal = 1 then
      begin
        CustomerNoName.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
  CustomerNoName.OnChange := CustomerNoNameChange;
end;

procedure TfrmOrderInPut.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmOrderInPut.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 TfrmOrderInPut.NoteDblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'OrdNote';
      flagname := '备注及要求';
      if ShowModal = 1 then
      begin
        Note.Text := Note.Text + frmZDYHelpSel.ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  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('OrderUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PriceUnit';
      flagname := '价格单位';
      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.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 Trim(flag) = 'OrdDefStr2' then
      begin
        //flag:='OrdDefStr2';
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.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(FromConNo.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(FromConNo.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(FromConNo.Text) + '%' + '''');
        Open;
      end;
    end;
    begin
      FromConNo.Text := Trim(ADOTemp.fieldbyname('ConNo').AsString);
      CustomerNoName.Text := Trim(ADOTemp.fieldbyname('CustomerNoName').AsString);
      CustomerNoName.TxtCode := Trim(ADOTemp.fieldbyname('CustomerNo').AsString);
      ConMainId := Trim(ADOTemp.fieldbyname('MainId').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);
            Post;
          end;
          Next;
        end;
      end;
    end;

  end;
end;

procedure TfrmOrderInPut.ToolButton3Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  OneKeyPost(Tv1, Order_Sub);
end;

procedure TfrmOrderInPut.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTMF';
      flagname := '门幅';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
  //    MainType:=Trim(DName);
      fnote := FALSE;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTMF').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTKZ';
      flagname := '克重';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
   //   MainType:=Trim(DName);
      fnote := FALSE;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTKZ').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTSpec';
      flagname := '规格';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
   //   MainType:=Trim(DName);
      fnote := FALSE;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
      //  Self.Order_Sub.FieldByName('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYCode').AsString);
        Self.Order_Sub.FieldByName('PRTSpec').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.OrdPerson1BtnClick(Sender: TObject);
begin
  try

    frmMJEdit := TfrmMJEdit.Create(Application);
    with frmMJEdit do
    begin
      frmMJEdit.Caption := '人员名单';
      if ShowModal = 1 then
      begin

        OrdPerson1.Text := Trim(CDS_Glide.fieldbyname('YGName').AsString);

      end;
    end;
  finally
    frmMJEdit.Free;
  end;
end;

procedure TfrmOrderInPut.OrdDateChange(Sender: TObject);
begin
  if formatdateTime('yyyy-MM-dd', OrdDate.Date) > formatdateTime('yyyy-MM-dd', date()) then
  begin
    application.MessageBox('日期不能大于今天！', '提示信息', 0);
    OrdDate.Date := strtodate(formatdateTime('yyyy-MM-dd', date()));
    exit;
  end;
end;

procedure TfrmOrderInPut.CustomerNoNameChange(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := Trim(CustomerNoName.Text);
  if Length(Trim(mvalue)) > 1 then
  begin
    //mvalue:='%'+Trim(mvalue)+'%';
    with ADO_KH do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from KH_Zdy_Attachment ');
      sql.Add(' where (ZDYName like ' + quotedstr('%' + trim(mvalue) + '%'));
      sql.Add(' or dbo.getpinYin(zdyName) like ' + quotedstr('%' + trim(mvalue) + '%'));
      sql.Add(' ) ');
      sql.Add(' and Type=''KHName'' ');
      Open;
    end;
  end;
end;

procedure TfrmOrderInPut.TV4CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  CustomerNoName.OnChange := nil;
  CustomerNoName.Text := Trim(ADO_KH.fieldbyname('ZDYName').AsString);
  CustomerNoName.TxtCode := Trim(ADO_KH.fieldbyname('ZdyCode').AsString);
  CustomerNoName.OnChange := CustomerNoNameChange;
end;

procedure TfrmOrderInPut.TV4KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_RETURN then
  begin
    CustomerNoName.OnChange := nil;
    CustomerNoName.Text := Trim(ADO_KH.fieldbyname('ZDYName').AsString);
    CustomerNoName.TxtCode := Trim(ADO_KH.fieldbyname('ZdyCode').AsString);
    CustomerNoName.OnChange := CustomerNoNameChange;
  end;
end;

procedure TfrmOrderInPut.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if (not Order_Sub.IsEmpty) and (trim(FMainId) = '') then
  begin
    if application.MessageBox('您有数据未保存？您是否要退出？', '提示信息!', MB_YESNO + MB_ICONQUESTION) = IDYES then
      CanClose := True
    else
      CanClose := False;
  end;
end;

procedure TfrmOrderInPut.FromConNoBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  try
    frmDDLYSel := TfrmDDLYSel.Create(Application);
    with frmDDLYSel do
    begin
      flag := forderType;
      if ShowModal = 1 then
      begin
        FromConNo.Text := Trim(frmDDLYSel.ClientDataSet1.fieldbyname('DEFstr2').AsString);
      end;
    end;
  finally
    frmDDLYSel.Free;
  end;

//  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);
//      V1Name.Caption := '名称';
//
//      if ShowModal = 1 then
//      begin
//        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
end;

procedure TfrmOrderInPut.FromBuMBtnUpClick(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);

      V1Name.Caption := '部门名称';

      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.ShuoAddressBtnUpClick(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);

      V1Name.Caption := '地址';

      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.WlFshiBtnUpClick(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);

      V1Name.Caption := '名称';

      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.ToolButton1Click(Sender: TObject);
begin
  try
    frmZdyAttachXSC := TfrmZdyAttachSX.Create(Application);
    with frmZdyAttachXSC do
    begin
      forderType := Self.forderType;
      if ShowModal = 1 then
      begin
        while frmZdyAttachXSC.CDS_HZ.Locate('SSel', true, []) do
        begin
          with Self.Order_Sub do
          begin
            Append;
            Self.Order_Sub.FieldByName('PRTCode').Value := Trim(frmZdyAttachXSC.CDS_HZ.FieldByName('XSCode').AsString);
            Self.Order_Sub.FieldByName('PRTCodeName').Value := Trim(frmZdyAttachXSC.CDS_HZ.FieldByName('DEFstr3').AsString);
            Self.Order_Sub.FieldByName('PRTML').Value := Trim(frmZdyAttachXSC.CDS_HZ.FieldByName('DEFstr2').AsString);
            Self.Order_Sub.FieldByName('PRTCC').Value := Trim(frmZdyAttachXSC.CDS_HZ.FieldByName('DEFstr1').AsString);
            Self.Order_Sub.FieldByName('Sorddefnote1').Value := Trim(frmZdyAttachXSC.CDS_HZ.FieldByName('Note').AsString);
            Self.Order_Sub.FieldByName('FromATID').Value := Trim(frmZdyAttachXSC.CDS_HZ.FieldByName('ATID').AsString);
            Self.Order_Sub.FieldByName('FromOrdATID').Value := Trim(frmZdyAttachXSC.CDS_HZ.FieldByName('FOrdATID').AsString);
            Self.Order_Sub.FieldByName('OrderUnit').Value := '片';
            if trim(FromBuM.Text) = '国外销售部门' then
            begin
              Self.Order_Sub.FieldByName('PrtCurrency').Value := 'USD';
            end
            else
              Self.Order_Sub.FieldByName('PrtCurrency').Value := 'RMB';
            Post;
          end;
          frmZdyAttachXSC.CDS_HZ.Delete;
        end;
        Next;

      end;
    end;
  finally
    frmZdyAttachXSC.Free;
  end;
end;

procedure TfrmOrderInPut.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQuantity, FUnitPrice, FCourierCharge: double;
begin
  if TcxTextEdit(Sender).EditingText = '' then
  begin
    mvalue := '0';
  end
  else
    mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;

    FQuantity := FieldByName('PRTOrderQty').asfloat;
    FUnitPrice := FieldByName('PRTPrice').asfloat;
  end;

  with Order_Sub do
  begin
    Edit;
    FieldByName('PRTMoney').Value := (FQuantity * FUnitPrice);
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitImage();
end;

procedure TfrmOrderInPut.SHPersonBtnUpClick(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);

      V1Name.Caption := '名称';

      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

