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,
  cxCurrencyEdit, cxImage, Menus, cxLookAndFeelPainters, cxButtons, cxDBEdit,
  cxDropDownEdit, cxLookAndFeels, cxNavigator, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmOrderInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    OrderNo: TEdit;
    Label7: TLabel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1PRTColor: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1PRTOrderQty: TcxGridDBColumn;
    Label10: TLabel;
    ConNo: TEdit;
    RanFactory: TBtnEditC;
    v1XHNo: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1SOrddefstr2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1PRTHX: TcxGridDBColumn;
    Label3: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    OrdDate: TDateTimePicker;
    CustomerNoName: TBtnEditC;
    MPRTMF: TBtnEditC;
    MPRTKZ: TBtnEditC;
    Label2: TLabel;
    MPRTCodeName: TBtnEditC;
    Label6: TLabel;
    MPRTSpec: TBtnEditC;
    Label5: TLabel;
    MPRTDuiSeGY: TBtnEditC;
    Label11: TLabel;
    MPRTSL: TBtnEditC;
    Label9: TLabel;
    MPRTNiuDu: TBtnEditC;
    Label8: TLabel;
    MPRTPH: TBtnEditC;
    Label15: TLabel;
    HZLFactory: TBtnEditC;
    Label16: TLabel;
    PBFactory: TBtnEditC;
    Label17: TLabel;
    YCLFactory: TBtnEditC;
    Label18: TLabel;
    MPRTZHName: TEdit;
    Label19: TLabel;
    Label20: TLabel;
    MPRTKaiJian: TBtnEditC;
    Label21: TLabel;
    MPRTChuanY: TBtnEditC;
    Label22: TLabel;
    MPRTYaoFeng: TBtnEditC;
    Label23: TLabel;
    MPRTJiBenNote: TBtnEditC;
    Label24: TLabel;
    MPRTTeBieNote: TBtnEditC;
    Label25: TLabel;
    DLYDate: TDateTimePicker;
    Label26: TLabel;
    MPRTCode: TBtnEditC;
    Label27: TLabel;
    MPRTQtyNote: TBtnEditC;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    MPRTColorLaoDu1: TBtnEditC;
    MPRTColorLaoDu2: TBtnEditC;
    MPRTColorLaoDu3: TBtnEditC;
    MPRTColorLaoDu4: TBtnEditC;
    Label32: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    MPRTKQiQiu: TBtnEditC;
    MPRTDingPoLv: TBtnEditC;
    MPRTZuRanXing: TBtnEditC;
    Label35: TLabel;
    MPRTKuanNO: TBtnEditC;
    Label36: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    MPRTTangJin: TBtnEditC;
    Label39: TLabel;
    OrdDefDate1: TDateTimePicker;
    Label40: TLabel;
    OrdDefDate2: TDateTimePicker;
    Label41: TLabel;
    OrdPerson1: TBtnEditC;
    Label43: TLabel;
    OrdPerson2: TBtnEditC;
    LBNameNote: TMemo;
    MaiTouNote: TMemo;
    SYRName: TBtnEditC;
    MPRTBZNote: TMemo;
    MPRTSCTeBieNote: TMemo;
    CPCFBi: TEdit;
    Label45: TLabel;
    v1Column1: TcxGridDBColumn;
    Label46: TLabel;
    orddefnote1: TMemo;
    orddefstr1: TBtnEditC;
    Label47: TLabel;
    orddefstr2: TEdit;
    orddefstr4: TBtnEditC;
    orddefstr3: TEdit;
    Label48: TLabel;
    Label49: TLabel;
    EditCnt: TcxCurrencyEdit;
    v1PRTCodeName: TcxGridDBColumn;
    v1PRTCode: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    Label42: TLabel;
    Label4: TLabel;
    FileName: TcxButton;
    Files: TcxDBImage;
    ADOQueryImage: TADOQuery;
    DSIMage: TDataSource;
    Label44: TLabel;
    orddefnote2: TMemo;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    KHLevel: TEdit;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    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 RanFactoryBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure MPRTCodeNameBtnUpClick(Sender: TObject);
    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 YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure Label36DblClick(Sender: TObject);
    procedure Label37DblClick(Sender: TObject);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure MPRTBZNoteDblClick(Sender: TObject);
    procedure MPRTSCTeBieNoteDblClick(Sender: TObject);
    procedure v1PRTCodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FileNameClick(Sender: TObject);
    procedure v1PRTCodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PRTHXPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function SaveDataSubOne(): Boolean;
    function SaveDataMain(): Boolean;
    function SaveDataSubMore(): Boolean;
    procedure CJEWM();
    procedure InitImage();
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    { Public declarations }
  end;

var
  frmOrderInPut: TfrmOrderInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachCP,
  U_ZdyAttachGYS_help, U_ZdyCMZ_Sel, U_ZdyYS_Sel;

{$R *.dfm}

procedure TfrmOrderInPut.InitImage();
begin
//  IF Order_Sub.IsEmpty then exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where TFid=' + quotedstr(trim(OrderNO.Text)));
    open;
  end;
end;

procedure TfrmOrderInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption + TV1.Name, Tv1, '生产指示单管理');
end;

procedure TfrmOrderInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
//        sql.Add(' select * from JYOrder_Sub where 1=1 ');
    if PState = 1 then
    begin
//      sql.Add(' select * from JYOrder_Sub where 1=1 ');
//      sql.Add(' and MainId=''' + Trim(FMainId) + '''');
     { SQL.Add('select PRTPrice=( case when AA.PRTPrice>0 then  AA.PRTPrice else AA.PRTPriceC end ),AA.* from(');
      sql.Add(' select A.*,PRTPriceC=(( case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=B.CustomerNoName)=''一级代理商'' then (select fprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else   ');
      sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=B.CustomerNoName)=''二级代理商'' then (select Sprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else   ');
      sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=B.CustomerNoName)=''三级代理商'' then (select Tprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else  ');
      sql.Add('   (select DZprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) 	end 	end 	end ))');
      sql.Add('from JYOrder_Sub A  ');
      sql.Add(' inner join JYOrder_Main B on A.mainid=B.mainid ');
      sql.Add('where 1=1 and A.MainId=''' + Trim(FMainId) + ''' )AA'); }//查询价格
      sql.Add(' select PRTPrice=(( case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=B.CustomerNoName)=''定制代理商'' then (select DZprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else   ');
      sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=B.CustomerNoName)=''二级代理商'' then (select Sprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else   ');
      sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=B.CustomerNoName)=''三级代理商'' then (select Tprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else  ');
      sql.Add('(select fprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) 	end 	end 	end )),A.*');
      sql.Add('from JYOrder_Sub A  ');
      sql.Add(' inner join JYOrder_Main B on A.mainid=B.mainid ');
      sql.Add('where 1=1 and A.MainId=''' + Trim(FMainId) + ''' ');
//      ShowMessage(sql.text);
    end;
    if PState = 0 then
    begin
      sql.Add(' select * from JYOrder_Sub where 1=1 ');
      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 JYOrder_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOQuery1, ScrollBox1, 2);
  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := OrdDate.DateTime;
    OrdDefDate1.DateTime := OrdDate.DateTime;
    OrdDefDate2.DateTime := OrdDate.DateTime;
    OrdPerson2.Text := trim(dName);
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '';
    orddefnote1.Text := '';
    OrdPerson2.Text := trim(dName);
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Fieldbyname('SLBName').value := null;
        Post;
        Next;
      end;
    end;
  end;
  InitImage();
end;

procedure TfrmOrderInPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
begin

end;

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption + TV1.Name, Tv1, '生产指示单管理');
  InitData();
end;

procedure TfrmOrderInPut.CJEWM();
var
  Txt, fImagePath, maxNo: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(order_Sub.fieldbyname('Subid').AsString);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);

    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from TP_File ');
      sql.Add('where TFID=' + quotedstr(trim(Txt)));
      sql.Add('and TFType=''EWM''');
      open;
      if isempty then
      begin
        append;
        fieldbyname('TFID').Value := trim(Txt);
        fieldbyname('WBID').Value := trim(order_Sub.fieldbyname('Mainid').AsString);
        fieldbyname('TFType').Value := 'EWM';
        fieldbyname('FillTime').Value := SGetServerDateTime(ADOTemp);
        tblobfield(FieldByName('Filesother')).LoadFromFile(fImagePath);
        post;
      end;
    end;
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    order_Sub.EnableControls;
    exit;
  end;
end;

function TfrmOrderInPut.SaveData(): Boolean;
var
  maxno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'JM', 'JYOrder_Main', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    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);
      SSetsaveSqlNew(ADOCmd, 'JYOrder_Main', ScrollBox1, 2);
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('Filltime').Value := SGetServerDateTime(ADOTemp);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      FieldByName('LBNameNote').Value := Trim(LBNameNote.Text);
      FieldByName('MaiTouNote').Value := MaiTouNote.Text;
      FieldByName('editcnt').Value := strtointdef(trim(editcnt.Text), 0);
      Post;
    end;

    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;
    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, '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 ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
          begin
            Append;
          end
          else
            Edit;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          fieldbyname('PRTMF').Value := Order_Sub.fieldbyname('PRTMF').AsFloat;
          fieldbyname('PRTKZ').Value := Order_Sub.fieldbyname('PRTKZ').asfloat;
          if Trim(fieldbyname('PRTOrderQty').AsString) = '' then
          begin
            fieldbyname('PRTOrderQty').Value := 0
          end;
          if Trim(fieldbyname('PRTPrice').AsString) = '' then
          begin
            fieldbyname('PRTPrice').Value := 0
          end;
          FieldByName('Sorddefstr10').Value := Order_Sub.FieldByName('Sorddefstr10').AsString;
//          FieldByName('khOrderNO').Value:=trim(Order_Sub.FieldByName('khOrderNO').AsString);
          FieldByName('PRTprice').Value := Order_Sub.FieldByName('PRTprice').AsFloat;
          FieldByName('huilv').Value := Order_Sub.FieldByName('huilv').AsFloat;
       //   FieldByName('orderNo').Value:=trim(orderNo.text);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);
        Order_Sub.FieldByName('Mainid').Value := Trim(FMainId);
        CJEWM();
        Next;
      end;
    end;
    {if Trim(Order_Sub.FieldByName('PRTPrice').AsString)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR ');
        sql.Add('where MainId='''+Trim(FMainId)+'''');
        SQL.Add(' and CRType=''应收款登记'' ');
        SQL.Add(' and YFType=''自动生成'' ');
        Open;
      end;
      if ADOTemp.RecordCount=1 then
      begin
        
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR set Price='+Order_Sub.FieldByName('PRTPrice').AsString);
          sql.Add(',BZType='''+Trim(Order_Sub.FieldByName('PriceUnit').AsString)+'''');
          SQL.Add(' where YFID='''+Trim(ADOTemp.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
          SQL.Add(' where YFID='''+Trim(ADOTemp.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_KC Set KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
          sql.Add(' ,KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
          sql.Add(' where CRID='+ADOTemp.fieldbyname('CRID').AsString);
          ExecSQL;
        end;
      end;

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

function TfrmOrderInPut.SaveDataMain(): Boolean;
var
  maxno: string;
begin
  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;
        maxno := Trim(FFMainId);
      end
      else
      begin
        maxno := Trim(FMainId);
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      SSetsaveSqlNew(ADOCmd, 'JYOrder_Main', ScrollBox1, 2);
      if PState = 1 then
      begin
        FieldByName('OrdUpDate').Value := SGetServerDateTime(ADOTemp);
      end;
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;
    FMainId := Trim(maxno);
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

function TfrmOrderInPut.SaveDataSubOne(): Boolean;
var
  maxno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      //First;
      //while not Eof do
      begin
        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 ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);
        //Order_Sub.Post;
        //Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！', '提示', 0);
  end;
end;

function TfrmOrderInPut.SaveDataSubMore(): Boolean;
var
  maxno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        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 ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！', '提示', 0);
  end;
end;

procedure TfrmOrderInPut.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if Trim(OrderNo.Text) = '' then
  begin
    Application.MessageBox('指示单号不能为空！', '提示', 0);
    Exit;
  end;
//  if Order_Sub.Locate('PriceUnit', null, []) = True then
//  begin
//    Application.MessageBox('币种不能为空!', '提示', 0);
//    Exit;
//  end;
//  if Order_Sub.Locate('PriceUnit', '', []) = True then
//  begin
//    Application.MessageBox('币种不能为空!', '提示', 0);
//    Exit;
//  end;
  if order_Sub.Locate('OrderUnit', null, []) = true then
  begin
    application.messagebox('数量单位不能为空', '提示');
    exit;
  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;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmOrderInPut.v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmZDYHelp := TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag := 'OrdColor';
//      flagname := '颜色';
//      V1Name.Caption := '中文名称';
//      V1Note.Caption := '英文名称';
// //     MainType:=Trim(DName);
//      fnote := True;
//      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;
//  frmZdyYS_sel := TfrmZdyYS_sel.create(self);
//  with frmZdyYS_sel do
//  begin
//    if showmodal = 1 then
//    begin
//      with Order_Sub do
//      begin
//        Edit;
//        FieldByName('PRTColor').value := CDS_HZ.fieldbyname('CodeName').AsString;
//        Post;
//      end;
//    end;
//    free;
//  end;
  frmZdyYS_sel := TfrmZdyYS_sel.create(self);
  with frmZdyYS_sel do
  begin
    if showmodal = 1 then
    begin
      CDS_HZ.First;
      while not CDS_HZ.Eof do
      begin
        if CDS_HZ.fieldbyname('ssel').AsBoolean then
        begin
          with Order_Sub do
          begin
            Edit;
            FieldByName('PRTColor').value := CDS_HZ.fieldbyname('CodeName').AsString;
            Post;
          end;
        end;
        CDS_HZ.Next;
      end;

    end;
    free;
  end;
end;

procedure TfrmOrderInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  if Trim(OrderNo.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('PRTColor').Value := '';
    FieldByName('PRTOrderQty').Value := null;
    FieldByName('PRTPrice').Value := 0;
    FieldByName('SOrddefstr4').Value := null;
    FieldByName('SOrddefstr2').Value := null;
    FieldByName('SOrddefstr10').Value := null;
    Post;
  end;
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 ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TP_File where WBID=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + ''' and TFType=''EWM'' ');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Sub where SubId=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmOrderInPut.RanFactoryBtnUpClick(Sender: TObject);
begin
  frmZdyAttachGYS_help := TfrmZdyAttachGYS_help.create(self);
  with frmZdyAttachGYS_help do
  begin
    if showmodal = 1 then
    begin
      RanFactory.Text := trim(cds_hz.fieldbyname('zdyName').asstring);
    end;
    free;
  end;
end;

procedure TfrmOrderInPut.CustomerNoNameBtnUpClick(Sender: TObject);
begin
{  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CustomerNoName';
      flagname:='客户名称';
      if Trim(DParameters1)<>'高权限' then
        MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;  }
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        CustomerNoName.Text := Trim(CDS_HZ.fieldbyname('zdyName').AsString);
        CustomerNoName.TxtCode := Trim(CDS_HZ.fieldbyname('ZDYCode').AsString);
        KHLevel.Text := Trim(CDS_HZ.fieldbyname('DEFStr6').AsString);
        if KHLevel.Text = '' then
        begin
          KHLevel.Text := '一级代理商';
        end;
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
  if Trim(CustomerNoName.Text) <> '' then
  begin

    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOQuery1 do
        begin
          Close;
          SQL.Clear;
          if PState = 1 then
          begin
            sql.Add(' select PRTPrice=(( case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''定制代理商'' then (select DZprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else   ');
            sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''二级代理商'' then  ');
            SQL.Add('case when  ((left(''' + Order_Sub.FieldByName('SOrddefstr5').AsString + ''',1)=''A'' ) or (''' + Order_Sub.FieldByName('SOrddefstr5').AsString + '''=''CXXL''))   then (select Sprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode )   else   (select Seprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) end  else');
            sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''三级代理商'' then (select Tprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else  ');
            sql.Add('(select fprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) 	end 	end 	end ))');
            sql.Add('from JYOrder_Sub A  ');
            sql.Add(' inner join JYOrder_Main B on A.mainid=B.mainid ');
            sql.Add('where  A.MainId=''' + Trim(FMainId) + ''' and A.PRTCode=''' + Order_Sub.FieldByName('Prtcode').AsString + ''' ');
          end;
          if PState = 0 then
          begin
            sql.Add(' select PRTPrice=(( case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''定制代理商'' then (select DZprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else   ');
            sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''二级代理商'' then  ');
            SQL.Add('case when  ((left(''' + Order_Sub.FieldByName('SOrddefstr5').AsString + ''',1)=''A'' ) or (''' + Order_Sub.FieldByName('SOrddefstr5').AsString + '''=''CXXL''))   then (select Sprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode )   else   (select Seprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) end  else');
            sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''三级代理商'' then (select Tprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else  ');
            sql.Add('(select fprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) 	end 	end 	end ))');
            sql.Add('from JYOrder_Sub A  ');
            sql.Add(' inner join JYOrder_Main B on A.mainid=B.mainid ');
            sql.Add('where   A.PRTCode=''' + Order_Sub.FieldByName('Prtcode').AsString + ''' ');
          end;
//          ShowMessage(sql.text);
          Open;
        end;

        with ADOCmd do
        begin
//          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
//          begin
//            Append;
//          end
//          else
          Order_Sub.Edit;
          Order_Sub.fieldbyname('PrtPrice').Value := ADOQuery1.fieldbyname('PRTPrice').AsFloat;
          Order_Sub.Post;
        end;

        Next;
      end;
    end;

//    Order_Sub.Edit;
//    Order_Sub.fieldbyname('PrtPrice').Value := ADOQuery1.fieldbyname('PRTPrice').AsFloat;
//    Order_Sub.Post;
    tv1.Controller.EditingController.ShowEdit();
  end;
end;

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

procedure TfrmOrderInPut.MPRTCodeNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      //fnote:=True;
      //V1Note.Caption:='产品编号';
      if ShowModal = 1 then
      begin
        MPRTCodeName.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        //MPRTCode.Text:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  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 (Trim(DParameters1) <> '高权限') then     // and (Trim(DParameters1)<>'合并权限')
      begin
        TBAdd.Visible := False;
        TBEdit.Visible := False;
        TBDel.Visible := False;
      end;

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

procedure TfrmOrderInPut.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
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        if Trim(flag) = 'MPRTCode' then
        begin
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1 * from JYOrder_Main where MPRTCode=''' + Trim(ClientDataSet1.fieldbyname('ZDYName').AsString) + '''');
            sql.Add(' order by FillTime desc');
            Open;
          end;
          MPRTCodeName.Text := ADOTemp.fieldbyname('MPRTCodeName').AsString;
          MPRTMF.Text := ADOTemp.fieldbyname('MPRTMF').AsString;
          MPRTKZ.Text := ADOTemp.fieldbyname('MPRTKZ').AsString;
          MPRTSpec.Text := ADOTemp.fieldbyname('MPRTSpec').AsString;
        end;
      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(ConNo.Text) + '%' + '''');
      Open;
    end;

    if ADOTemp.IsEmpty then
    begin
      Application.MessageBox('合同号不存在，请确认后重试！', '提示', 0);
      Exit;
    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(ConNo.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(ConNo.Text) + '%' + '''');
        Open;
      end;
    end;
    begin
      ConNo.Text := Trim(ADOTemp.fieldbyname('ConNo').AsString);
      //OrdDefStr1.Text:=Trim(ADOTemp.fieldbyname('ConDefStr1').AsString);
      MPRTChuanY.Text := Trim(ADOTemp.fieldbyname('ChuanYangNote').AsString);
      MPRTQtyNote.Text := Trim(ADOTemp.fieldbyname('QtyNote').AsString);
      CustomerNoName.Text := Trim(ADOTemp.fieldbyname('CustomerNoName').AsString);
      CustomerNoName.TxtCode := Trim(ADOTemp.fieldbyname('CustomerNo').AsString);
      DLYDate.DateTime := ADOTemp.fieldbyname('DLYDate').Value;
      ConMainId := Trim(ADOTemp.fieldbyname('MainId').AsString);
     // ConGS:=Trim(ADOTemp.fieldbyname('SYRName').AsString);
      //OrdDefStr2.Text:=Trim(ADOTemp.fieldbyname('ConDefStr2').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('PRTCode').Value := Trim(ADOTemp.fieldbyname('PRTCode').AsString);
            FieldByName('PRTCodeName').Value := Trim(ADOTemp.fieldbyname('PRTCodeName').AsString);
            FieldByName('PRTspec').Value := Trim(ADOTemp.fieldbyname('PRTspec').AsString);
            FieldByName('PRTColor').Value := Trim(ADOTemp.fieldbyname('PRTColor').AsString);
            FieldByName('SOrdDefStr4').Value := Trim(ADOTemp.fieldbyname('SOrdDefStr4').AsString);
            FieldByName('SOrdDefStr3').Value := Trim(ADOTemp.fieldbyname('SOrdDefStr3').AsString);
            FieldByName('PRTOrderQty').Value := Trim(ADOTemp.fieldbyname('PRTOrderQty').AsString);
            FieldByName('OrderUnit').Value := Trim(ADOTemp.fieldbyname('OrderUnit').AsString);
            FieldByName('PRTPrice').Value := Trim(ADOTemp.fieldbyname('PRTPrice').AsString);
            FieldByName('PriceUnit').Value := Trim(ADOTemp.fieldbyname('PriceUnit').AsString);
            FieldByName('PRTHX').Value := Trim(ADOTemp.fieldbyname('PRTHX').AsString);
            FieldByName('XHNO').Value := Trim(ADOTemp.fieldbyname('XHNO').AsString);
            FieldByName('Sorddefstr10').Value := Trim(ADOTemp.fieldbyname('SUBID').AsString);
            FieldByName('huilv').Value := Trim(ADOTemp.fieldbyname('huilv').AsString);
            FieldByName('Sorddefstr5').Value := Trim(ADOTemp.fieldbyname('Sorddefstr5').AsString);
//            FieldByName('PRTECodeName').Value := Trim(ADOTemp.fieldbyname('PRTECodeName').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.YCLFactoryBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      MainType := TEdit(Sender).Name;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmOrderInPut.Label36DblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'LbNameNote';
      flagname := '标签内容';
      if ShowModal = 1 then
      begin
        ClientDataSet1.DisableControls;
        //LBNameNote.Text:='';
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if ClientDataSet1.FieldByName('SSel').AsBoolean = True then
            begin
              if Trim(Self.LBNameNote.Text) = '' then
              begin
                Self.LBNameNote.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end
              else
              begin
                Self.LBNameNote.Text := Self.LBNameNote.Text + #13 + #10 + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end;

            end;
            Next;
          end;
        end;
        ClientDataSet1.EnableControls;
      end;
    end;
  finally
  end;
end;

procedure TfrmOrderInPut.Label37DblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'MaitouNote';
      flagname := '唛头内容';
      if ShowModal = 1 then
      begin
        ClientDataSet1.DisableControls;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if ClientDataSet1.FieldByName('SSel').AsBoolean = True then
            begin
              if Trim(Self.MaitouNote.Text) = '' then
              begin
                Self.MaitouNote.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end
              else
              begin
                Self.MaitouNote.Text := Self.MaitouNote.Text + #13 + #10 + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end;

            end;
            Next;
          end;
        end;
        ClientDataSet1.EnableControls;
      end;
    end;
  finally
  end;
end;

procedure TfrmOrderInPut.SYRNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdDefStr2';
      flagname := '公司抬头';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
      fnote := True;
    {  if Trim(DParameters1)<>'高权限' then
      begin
        TBAdd.Visible:=False;
        TBEdit.Visible:=false;
        TBDel.Visible:=false;
        TBSave.Visible:=false;
      end;   }
      if ShowModal = 1 then
      begin
        SYRName.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTBZNoteDblClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
  fsj := Trim(TMemo(Sender).Hint);
  FWZ := Pos('/', fsj);
  i := 0;
  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
        MPRTBZNote.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              MPRTBZNote.Lines.Add(inttostr(i) + '.' + FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTSCTeBieNoteDblClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
  fsj := Trim(TMemo(Sender).Hint);
  FWZ := Pos('/', fsj);
  i := 0;
  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
        MPRTSCTeBieNote.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              MPRTSCTeBieNote.Lines.Add(inttostr(i) + '.' + FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.v1PRTCodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then                              
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTCodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.FileNameClick(Sender: TObject);
begin
 { IF trim(OrderNo.Text)='' then
  begin
    application.MessageBox('订单号不能为空！','提示信息',MB_ICONERROR);
    exit;
  end;

  try
	  FormGetPic := TFormGetPic.Create(Application);
    with FormGetPic do
    begin
     // fkeyNO:=trim(Order_Sub.fieldbyname('subID').AsString);
      fkeyNO:=trim(OrderNo.Text);
      pat1:=TCXbutton(Sender).Name;
      pic1:=TCXbutton(Sender).Hint;
      FTFType:='ORDERTY';
 //   pat1:='PatFile1';
//   pic1:='Picture1';
      IF ShowModal = 1 then
      begin
     // self.Timage(Tbutton(Sender).Hint).Picture.Assign(FormGetPic.Image2.Picture.Bitmap);
        OrderNo.Enabled:=false;
      end;

      Release;
    end;
    Initimage();
  except
    if FormGetPic<>nil then FormGetPic.Release;
  end;     }
end;

procedure TfrmOrderInPut.v1PRTCodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachCP := TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin

      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTCode').Value := Trim(CDS_HZ.fieldbyname('ZDYCode').AsString);
        Order_Sub.fieldbyname('PRTCodeName').Value := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        Order_Sub.fieldbyname('prtspec').Value := Trim(CDS_HZ.fieldbyname('DEFstr1').AsString);
        Order_Sub.fieldbyname('PRTColor').Value := Trim(CDS_HZ.fieldbyname('CYys').AsString);
      //  Order_Sub.fieldbyname('prtmf').Value:=Trim(CDS_HZ.fieldbyname('DEFstr7').AsString);
     //   Order_Sub.fieldbyname('prtkz').Value:=Trim(CDS_HZ.fieldbyname('DEFstr8').AsString);
        if KHLevel.Text = '定制代理商' then
        begin
          Order_Sub.fieldbyname('PrtPrice').Value := CDS_HZ.fieldbyname('DZPrice').AsFloat;
        end
        else if KHLevel.Text = '二级代理商' then
        begin
          if ((LeftStr((Order_Sub.fieldbyname('SOrddefstr5').AsString), 1) = 'A') or ((Order_Sub.fieldbyname('SOrddefstr5').AsString) = 'CXXL')) then
          begin
            Order_Sub.fieldbyname('PrtPrice').Value := CDS_HZ.fieldbyname('SPrice').AsFloat;
          end
          else
          begin
            Order_Sub.fieldbyname('PrtPrice').Value := CDS_HZ.fieldbyname('SePrice').AsFloat;
          end;
        end
        else if KHLevel.Text = '三级代理商' then
        begin
          Order_Sub.fieldbyname('PrtPrice').Value := CDS_HZ.fieldbyname('TPrice').AsFloat;
        end
        else
        begin
          Order_Sub.fieldbyname('PrtPrice').Value := CDS_HZ.fieldbyname('FPrice').AsFloat;
        end;
        Order_Sub.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.v1PRTHXPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyCMZ_sel := TfrmZdyCMZ_sel.create(self);
  with frmZdyCMZ_sel do
  begin
    if showmodal = 1 then
    begin
      with order_sub do
      begin
        Edit;
        FieldByName('SOrddefstr5').value := cdssub.fieldbyname('SizeSpec').AsString;
        Post;
      end;
    end;
    free;
  end;
  if Trim(CustomerNoName.Text) <> '' then
  begin

    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOQuery1 do
        begin
          Close;
          SQL.Clear;
          if PState = 1 then
          begin
            sql.Add(' select PRTPrice=(( case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''一级代理商'' then (select fprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else   ');
            sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''二级代理商'' then  ');
            SQL.Add('case when  ((left(''' + Order_Sub.FieldByName('SOrddefstr5').AsString + ''',1)=''A'' ) or (''' + Order_Sub.FieldByName('SOrddefstr5').AsString + '''=''CXXL''))   then (select Sprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode )   else   (select Seprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) end  else');
            sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''三级代理商'' then (select Tprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else  ');
            sql.Add('(select DZprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) 	end 	end 	end ))');
            sql.Add('from JYOrder_Sub A  ');
            sql.Add(' inner join JYOrder_Main B on A.mainid=B.mainid ');
            sql.Add('where  A.MainId=''' + Trim(FMainId) + ''' and A.PRTCode=''' + Order_Sub.FieldByName('Prtcode').AsString + ''' ');
          end;
          if PState = 0 then
          begin
            sql.Add(' select PRTPrice=(( case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''一级代理商'' then (select fprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else   ');
            sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''二级代理商'' then  ');
            SQL.Add('case when  ((left(''' + Order_Sub.FieldByName('SOrddefstr5').AsString + ''',1)=''A'' ) or (''' + Order_Sub.FieldByName('SOrddefstr5').AsString + '''=''CXXL''))   then (select Sprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode )   else   (select Seprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) end  else');
            sql.Add(' case when (select DEFStr6 from  KH_Zdy_Attachment X where  X.ZDYName=''' + Trim(CustomerNoName.Text) + ''')=''三级代理商'' then (select Tprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) else  ');
            sql.Add('(select DZprice from KH_Zdy_Attachment X where   X.zdycode=A.PRTCode ) 	end 	end 	end ))');
            sql.Add('from JYOrder_Sub A  ');
            sql.Add(' inner join JYOrder_Main B on A.mainid=B.mainid ');
            sql.Add('where   A.PRTCode=''' + Order_Sub.FieldByName('Prtcode').AsString + ''' ');
          end;
//          ShowMessage(sql.text);
          Open;
        end;

        with ADOCmd do
        begin
          Order_Sub.Edit;
          Order_Sub.fieldbyname('PrtPrice').Value := ADOQuery1.fieldbyname('PRTPrice').AsFloat;
          Order_Sub.Post;
        end;

        Next;
      end;
    end;

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

end.

