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, cxGridCustomPopupMenu, cxGridPopupMenu, cxDropDownEdit,
  RM_Common, RM_Preview, RM_System, RM_Class, RM_GridReport, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinscxPCPainter, 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, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

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;
    v1PRTOrderQty: TcxGridDBColumn;
    Label10: TLabel;
    JGFactoryName: TBtnEditC;
    v1OrderUnit: TcxGridDBColumn;
    Label3: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    OrdDate: TDateTimePicker;
    CustomerNoName: TBtnEditC;
    Label2: TLabel;
    Label6: TLabel;
    Label19: TLabel;
    Label25: TLabel;
    DLYDate: TDateTimePicker;
    Label26: TLabel;
    MPRTCode: TBtnEditC;
    Label41: TLabel;
    GDY: TBtnEditC;
    Label42: TLabel;
    MPRTBZNote: TMemo;
    MPRTChuanY: TMemo;
    v1Column1: TcxGridDBColumn;
    Note: TMemo;
    Label23: TLabel;
    JGType: TBtnEditC;
    Label53: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column2: TcxGridDBColumn;
    Label60: TLabel;
    MPRTCodeName: TEdit;
    MPRTCF: TEdit;
    MPRTMF: TEdit;
    MPRTSpec: TEdit;
    MPRTKZ: TEdit;
    Label4: TLabel;
    KHConNo: TEdit;
    RMPreview1: TRMPreview;
    Label5: TLabel;
    LBName: TEdit;
    ToolButton3: TToolButton;
    ADOQueryLabel: TADOQuery;
    RMGridReport1: TRMGridReport;
    Label8: TLabel;
    lidan: TBtnEditC;
    Label9: TLabel;
    PBFactoryName: TBtnEditC;
    Label11: TLabel;
    DJType: TComboBox;
    ZongPS: TEdit;
    ZongQty: TEdit;
    Label17: TLabel;
    YWY: TBtnEditC;
    Label18: TLabel;
    YXJL: TComboBox;
    Label20: TLabel;
    LLXS: TEdit;
    Label21: TLabel;
    JHGZ: TComboBox;
    ConNo: TBtnEditC;
    ToolButton4: TToolButton;
    Label22: TLabel;
    Label24: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    XGF: TEdit;
    Label31: TLabel;
    ZBF: TEdit;
    Label32: TLabel;
    KHYQ: TMemo;
    CPNum: TEdit;
    Label34: TLabel;
    YTPNum: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    Label33: TLabel;
    Label35: TLabel;
    jhwcDate: TDateTimePicker;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label36: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    Label39: TLabel;
    Label40: TLabel;
    Label43: TLabel;
    PBNOTE: TMemo;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxgrdbclmnv1Column8: TcxGridDBColumn;
    cxgrdbclmnv1Column9: TcxGridDBColumn;
    cxgrdbclmnv1Column10: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    SLDYQ: TBtnEditC;
    TANGSUO: TBtnEditC;
    SCYQ: TBtnEditC;
    Feel: TBtnEditC;
    RSYQ: TBtnEditC;
    SSL: TBtnEditC;
    DSGY: TBtnEditC;
    KQQ: TBtnEditC;
    PHZ: TBtnEditC;
    GONGYI: TBtnEditC;
    v1Column9: TcxGridDBColumn;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label44: TLabel;
    DXGongYi: TBtnEditC;
    Label45: TLabel;
    JGFactoryName2: TBtnEditC;
    Label46: TLabel;
    JGFactoryName3: TBtnEditC;
    Label47: TLabel;
    BJTT: TBtnEditC;
    Label48: TLabel;
    BQKH: TBtnEditC;
    ToolButton5: TToolButton;
    Label49: TLabel;
    PBFactoryName2: TBtnEditC;
    Label50: TLabel;
    PBFactoryName3: TBtnEditC;
    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 JGFactoryNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure MPRTCodeNameBtnUpClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrdDefStr2BtnUpClick(Sender: TObject);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure MPRTBZNoteDblClick(Sender: TObject);
    procedure MPRTChuanYDblClick(Sender: TObject);
    procedure MPRTCodeBtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure PBFactoryNameBtnUpClick(Sender: TObject);
    procedure ConNoBtnUpClick(Sender: TObject);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure cxgrdbclmnv1Column9PropertiesEditValueChanged(Sender: TObject);
    procedure MPRTMFKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton4Click(Sender: TObject);
    procedure JGFactoryName2BtnUpClick(Sender: TObject);
    procedure JGFactoryName3BtnUpClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure PBFactoryName2BtnUpClick(Sender: TObject);
    procedure PBFactoryName3BtnUpClick(Sender: TObject);
  private
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    procedure GetLLXS(MF: string; KZ: string);
    function getOrderNo(fconNo: string): string;
    function Setstring(cString: string): string;
    procedure OpenLabel();
    procedure CPXX();
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmOrderInPut: TfrmOrderInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachGYS,
  U_CPManageSel, getpic, U_LabelList, U_ContractListSel;

{$R *.dfm}
function TfrmOrderInPut.Setstring(cString: string): string;   {提取数字}
var
  i: integer;
  str: string;
begin
  str := '';
  for i := 1 to length(cString) do
  begin
    if Pos(cString[i], '0123456789.') <= 0 then
      break
    else
      case cString[i] of
        '.':
          str := str + '.';

        '0':
          str := str + '0';
        '1':
          str := str + '1';
        '2':
          str := str + '2';
        '3':
          str := str + '3';
        '4':
          str := str + '4';
        '5':
          str := str + '5';
        '6':
          str := str + '6';
        '7':
          str := str + '7';
        '8':
          str := str + '8';
        '9':
          str := str + '9';
      end;
  end;
  Setstring := str;
end;

procedure TfrmOrderInPut.OpenLabel();
begin
  with ADOQueryLabel do
  begin
    close;
    sql.Clear;
    sql.Add('select * from P_Label');
    sql.Add('where valid=''Y''');
    sql.Add('and labelCaption=' + quotedstr(trim(LBName.Text)));
    Open;
  end;
  if ADOQueryLabel.IsEmpty then
    exit;
  with RMGridReport1 do
  begin
    LoadFromBlobField(tblobfield(ADOQueryLabel.fieldbyname('labelFile')));

//    RMVariables['MPRTCodeName'] := MPRTCodeName.Text;
//    RMVariables['KHCONNO'] := CONNO.Text;
    //Preview :=RMPreview1;
    ShowReport;
  end;
end;

procedure TfrmOrderInPut.CPXX();
begin
  if MPRTCode.Text = '' then
    Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select *   from CP_YDang where CYNO=''' + Trim(MPRTCode.Text) + '''');
    Open;
  end;
  if ADOTemp.IsEmpty = FALSE then
  begin
//    RSYQ.Text := ADOTemp.fieldbyname('CYRSGY').asstring;
    DXGongYi.Text := ADOTemp.fieldbyname('CYDXGY').asstring;
    GONGYI.Text := ADOTemp.fieldbyname('CYrsGY').asstring;
  end;
end;

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

procedure TfrmOrderInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单录入SD', Tv1, '生产指示单管理3');
end;

procedure TfrmOrderInPut.InitData();
begin
  OrdDate.DateTime := SGetServerDateTime(ADOTemp);
  DlyDate.DateTime := OrdDate.DateTime;

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_Sub where 1=1 ');

    if PState = 1 then
    begin
      sql.Add(' and MainId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;

    Open;
  end;
  SCreateCDS20(ADOTemp, Order_Sub);
  SInitCDSData20(ADOTemp, Order_Sub);
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;

  SCSHDataNew(ADOTemp, ScrollBox1, 2);

  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := OrdDate.DateTime;
    jhwcDate.DateTime := OrdDate.DateTime;
  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 := '';
        FieldByName('hxFile').Value := '';
        Post;
        Next;
      end;
    end;
  end;

  OpenLabel();
end;

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

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  readCxGrid('指示单录入SD', Tv1, '生产指示单管理3');
  {if PriceFlag=99 then
  begin
    v1PRTPrice.Visible:=False;
    v1PRTPrice.Hidden:=True;
  end;}

  OrderNo.Text := '';
  if PState = 1 then
  begin
//    Label46.Visible:=true;
//    orddefnote1.Visible:=true;
//    Label49.Visible:=true;
//    EditCnt.Visible:=true;
  end;
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
  if LBName.Text <> '' then
  begin
    ToolButton5.Click;
  end;
end;

function TfrmOrderInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno: 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;
      if orderno.Text = '' then
      begin
        if GetLSNo(ADOCmd, maxorderno, 'RT', 'JYOrder_Main', 3, 1) = False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
        OrderNo.Text := TRIM(maxorderno);
      end;

//      OrderNo.Text:=getOrderNo(trim(conNo.Text));
    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);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      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;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, Smaxno, 'JS', 'JYOrder_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Smaxno := Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub where MainId=''' + Trim(maxno) + '''');
          sql.Add(' and SubId=''' + Trim(Smaxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(Smaxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          FieldByName('ConSubId').Value := Trim(Order_Sub.fieldbyname('ConSubId').AsString);
          Post;
        end;
        Next;
      end;
    end;
    if Order_Sub.FieldByName('PRTBILI').AsString <> '' then
    begin
      with ADOTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('Update JYOrder_Main Set XS=' + (Order_Sub.FieldByName('PRTBILI').AsString));
        sql.Add(' where Mainid=''' + Trim(maxno) + '''');
        ExecSQL;
      end;
    end;

    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update CK_SXPB_CR Set SPCF=' + QuotedStr(MPRTCF.Text));
      sql.Add(' where ORDMainIdRK=''' + Trim(maxno) + '''');
      SQL.Add('AND CRFLAG=''入库''');

      sql.Add('Update CK_SXPB_CR Set SPCF=' + QuotedStr(MPRTCF.Text));
      sql.Add(' where ORDMainIdCK=''' + Trim(maxno) + '''');
      SQL.Add('AND CRFLAG=''出库''');
      ExecSQL;
    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 Trim(conNo.Text) = '' then
//  begin
//    Application.MessageBox('合同号不能为空！', '提示', 0);
//    Exit;
//  end;
  if Trim(JGFactoryName.Text) = '' then
  begin
    Application.MessageBox('加工厂不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(GDY.Text) = '' then
  begin
    Application.MessageBox('跟单员不能为空！', '提示', 0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;

  if Order_Sub.Locate('PRTOrderQty', null, []) then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
//  if Order_Sub.Locate('PRTKUANNO', null, []) then
//  begin
//    Application.MessageBox('款号不能为空!', '提示', 0);
//    Exit;
//  end;
//  if Order_Sub.Locate('PRTKUANNO', '', []) then
//  begin
//    Application.MessageBox('款号不能为空!', '提示', 0);
//    Exit;
//  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 := '英文名称';
      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('PRTColorNo').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
//  if Trim(OrderNo.Text)='' then
//  begin
//    Application.MessageBox('订单号不能为空!','提示',0);
//    Exit;
//  end;
  CopyAddRow(Tv1, Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('PRTColor').Value := '';
    FieldByName('PRTOrderQty').Value := null;
    FieldByName('PRTColorNo').Value := null;
    FieldByName('hxFile').Value := '';
    FieldByName('PRTOrderQty').Value := 0;
    FieldByName('PRTPs').Value := 0;
    FieldByName('OrderUnit').Value := 'KG';
    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 JYOrder_Sub where SubId=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmOrderInPut.JGFactoryNameBtnUpClick(Sender: TObject);
var
  FJGFactoryName: string;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
//      frmZdyAttachGYS.FactType.ItemIndex:=FactType.Items.IndexOf('加工厂');

      if ShowModal = 1 then
      begin
//        with CDS_HZ do
//        begin
//          First;
//          FJGFactoryName := '';
//          while CDS_HZ.Locate('SSel', true, []) do
//          begin
//            FJGFactoryName := FJGFactoryName + '/' + Trim(CDS_HZ.fieldbyname('CoName').AsString);
//            CDS_HZ.Delete;
//          end;
//        end;
        JGFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
//        JGFactoryName.Text := Copy(FJGFactoryName, 2, Length(FJGFactoryName));
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmOrderInPut.CustomerNoNameBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
 
//      IF self.canshu1<>'高权限' then canshu1:='业务员';
      if ShowModal = 1 then
      begin
        CustomerNoName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        CustomerNoName.TxtCode := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

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

procedure TfrmOrderInPut.MPRTCodeNameBtnUpClick(Sender: TObject);
begin
  frmCPManageSel := TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal = 1 then
    begin
      MPRTCode.Text := CDS_Main.fieldbyname('CYNo').asstring;
      MPRTCodeName.Text := CDS_Main.fieldbyname('CYName').asstring;
      MPRTSpec.Text := CDS_Main.fieldbyname('CYSpec').asstring;
      MPRTMF.Text := CDS_Main.fieldbyname('CYmf').asstring;
      MPRTKZ.Text := CDS_Main.fieldbyname('CYkz').asstring;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

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

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

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

procedure TfrmOrderInPut.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 Trim(flag) = 'BJTT' 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.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);
      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.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.MPRTChuanYDblClick(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
        TMemo(Sender).Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              TMemo(Sender).Lines.Add(inttostr(i) + '.' + FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTCodeBtnUpClick(Sender: TObject);
begin
  frmCPManageSel := TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal = 1 then
    begin
      MPRTCode.Text := CDS_Main.fieldbyname('CYNo').asstring;
      MPRTCodeName.Text := CDS_Main.fieldbyname('CYName').asstring;
      MPRTSpec.Text := CDS_Main.fieldbyname('CYSpec').asstring;
      MPRTCF.Text := CDS_Main.fieldbyname('CYCF').asstring;
      MPRTMF.Text := CDS_Main.fieldbyname('CYmf').asstring;
      MPRTKZ.Text := CDS_Main.fieldbyname('CYkz').asstring;

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

procedure TfrmOrderInPut.ToolButton3Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmLabelList := TfrmLabelList.Create(Application);
    with frmLabelList do
    begin
      if ShowModal = 1 then
      begin
        self.LBName.text := Trim(ADOQueryLabel.fieldbyname('labelCaption').AsString);
      end;
    end;
  finally
    frmLabelList.Free;
  end;
  OpenLabel();
end;

procedure TfrmOrderInPut.PBFactoryNameBtnUpClick(Sender: TObject);
var
  FPBFactoryName: string;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      frmZdyAttachGYS.FactType.ItemIndex := FactType.Items.IndexOf('坯布厂');
      if ShowModal = 1 then
      begin
//        with CDS_HZ do
//        begin
//          First;
//          FPBFactoryName := '';
//          while CDS_HZ.Locate('SSel', true, []) do
//          begin
//            FPBFactoryName := FPBFactoryName + '/' + Trim(CDS_HZ.fieldbyname('CoName').AsString);
//            CDS_HZ.Delete;
//          end;
//        end;
        PBFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

//  try
//    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
//    with frmZdyAttachGYS do
//    begin
//      frmZdyAttachGYS.FactType.ItemIndex := FactType.Items.IndexOf('坯布厂');
//      if ShowModal = 1 then
//      begin
//        with CDS_HZ do
//        begin
//          First;
//          FPBFactoryName := '';
//          while CDS_HZ.Locate('SSel', true, []) do
//          begin
//            FPBFactoryName := FPBFactoryName + '/' + Trim(CDS_HZ.fieldbyname('CoName').AsString);
//            CDS_HZ.Delete;
//          end;
//        end;
//        PBFactoryName.Text := Copy(FPBFactoryName, 2, Length(FPBFactoryName));
//      end;
//    end;
//  finally
//    frmZdyAttachGYS.Free;
//  end;
end;

procedure TfrmOrderInPut.ConNoBtnUpClick(Sender: TObject);
var
  FConMainId, FMPRTCode, fhtxh, fdw, fzt: string;
begin
  try
    frmContractListSel := TfrmContractListSel.Create(Application);
    with frmContractListSel do
    begin
      fconNo := trim(self.ConNo.Text);
      if ShowModal = 1 then
      begin
        Order_Main.locate('SSel', true, []);
        with adotemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select count(*) as SS from JYOrder_Main where conno=''' + Trim(frmContractListSel.Order_Main.fieldbyname('ConNo').AsString) + '''');
          Open;
//          SHOWMESSAGE(SQL.Text);
        end;
        fhtxh := FLOATTOSTR(adotemp.fieldbyname('ss').ASFLOAT + 1);
//        SHOWMESSAGE(fhtxh);
        if fhtxh = '1' then
        begin
          orderno.Text := Trim(frmContractListSel.Order_Main.fieldbyname('ConNo').AsString);
        end
        else
          orderno.Text := Trim(frmContractListSel.Order_Main.fieldbyname('ConNo').AsString) + '-' + fhtxh;
        FConMainId := Trim(frmContractListSel.Order_Main.fieldbyname('MainId').AsString);

        if CopyInt = 99 then
        begin
          if (CustomerNoName.Text = Trim(frmContractListSel.Order_Main.fieldbyname('BuyName').AsString)) and (MPRTCode.Text = Trim(frmContractListSel.Order_Main.fieldbyname('SCSCode').AsString)) then
          begin
            fzt := '1';
          end;
        end;
        self.ConNo.Text := Trim(frmContractListSel.Order_Main.fieldbyname('ConNo').AsString);
        self.KHConNo.Text := Trim(frmContractListSel.Order_Main.fieldbyname('BuyConNo').AsString);
        if fzt <> '1' then
        begin
          YWY.Text := Trim(frmContractListSel.Order_Main.fieldbyname('Salesman').AsString);
          lidan.Text := Trim(frmContractListSel.Order_Main.fieldbyname('lidan').AsString);
          FMPRTCode := Trim(frmContractListSel.Order_Main.fieldbyname('SCSCode').AsString);

          self.CustomerNoName.Text := Trim(frmContractListSel.Order_Main.fieldbyname('BuyName').AsString);
          self.CustomerNoName.TxtCode := Trim(frmContractListSel.Order_Main.fieldbyname('BuyNo').AsString);
//        self.YWY.Text := Trim(frmContractListSel.Order_Main.fieldbyname('SellName').AsString);
          self.MPRTCode.Text := trim(frmContractListSel.Order_Main.Fieldbyname('SCSCode').AsString);
          self.MPRTCodeName.Text := trim(frmContractListSel.Order_Main.Fieldbyname('cyname').AsString);
          self.MPRTSpec.Text := trim(frmContractListSel.Order_Main.Fieldbyname('CYSpec').AsString);
          self.MPRTCF.Text := trim(frmContractListSel.Order_Main.Fieldbyname('CYCF').AsString);
          self.MPRTMF.Text := trim(frmContractListSel.Order_Main.Fieldbyname('SCSMF').AsString);
          self.MPRTKZ.Text := trim(frmContractListSel.Order_Main.Fieldbyname('SCSKZ').AsString);
        end;

        while Order_Main.locate('SSel', true, []) do
        begin
          if FConMainId <> trim(frmContractListSel.Order_Main.Fieldbyname('MainId').AsString) then
          begin
            Application.MessageBox('不能选择多个合同！', '提示', 0);
            break;
          end;
          if self.MPRTCode.Text <> trim(frmContractListSel.Order_Main.Fieldbyname('SCSCode').AsString) then
          begin
            Application.MessageBox('不能选择多个产品！', '提示', 0);
            break;
          end;
          if self.MPRTMF.Text <> trim(frmContractListSel.Order_Main.Fieldbyname('SCSMF').AsString) then
          begin
            Application.MessageBox('不能选择多个产品！', '提示', 0);
            break;
          end;
          if self.MPRTKZ.Text <> trim(frmContractListSel.Order_Main.Fieldbyname('SCSKZ').AsString) then
          begin
            Application.MessageBox('不能选择多个产品！', '提示', 0);
            break;
          end;
          if self.Order_Sub.locate('ConSubId', trim(Order_Main.Fieldbyname('SubId').AsString), []) then
          begin
            Order_Main.Edit;
            Order_Main.fieldbyname('SSel').value := false;
            continue;
          end;
          with self.Order_Sub do
          begin
            append;

            Fieldbyname('PrtKuanNO').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSKX').AsString);
            Fieldbyname('PrtColorNo').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSColorNo').AsString);
            Fieldbyname('PrtColor').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSColor').AsString);
            Fieldbyname('PRTHX').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSHX').AsString);
            Fieldbyname('PRTOrderQty').value := frmContractListSel.Order_Main.Fieldbyname('SCSQty').AsFloat;
            Fieldbyname('OrderUnit').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSUnit').AsString);
            Fieldbyname('SubNote').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSNote').AsString);
            Fieldbyname('ConSubId').value := trim(frmContractListSel.Order_Main.Fieldbyname('SubId').AsString);
            Fieldbyname('QRCOLOR').value := trim(frmContractListSel.Order_Main.Fieldbyname('HTQRCOLOR').AsString);

            GetLLXS(frmContractListSel.Order_Main.Fieldbyname('SCSMF').AsString, frmContractListSel.Order_Main.Fieldbyname('SCSKZ').AsString);

            if order_sub.fieldbyname('PRTBILI').AsFloat <> 0 then
            begin
              edit;
              if (trim(frmContractListSel.Order_Main.Fieldbyname('SCSUnit').AsString) = 'KG') or (trim(frmContractListSel.Order_Main.Fieldbyname('SCSUnit').AsString) = 'Kg') then
              begin
                Fieldbyname('OrderUnit').value := 'KG';
                Fieldbyname('funit').value := 'M';
                Fieldbyname('fQty').value := frmContractListSel.Order_Main.Fieldbyname('SCSQty').AsFloat * order_sub.fieldbyname('PRTBILI').AsFloat;
              end
              else if trim(frmContractListSel.Order_Main.Fieldbyname('SCSUnit').AsString) = 'Y' then
              begin
                Fieldbyname('OrderUnit').value := 'Y';
                Fieldbyname('funit').value := 'KG';
                order_sub.fieldbyname('PRTBILI').Value := order_sub.fieldbyname('PRTBILI').AsFloat / 0.9144;
                Fieldbyname('fQty').value := frmContractListSel.Order_Main.Fieldbyname('SCSQty').AsFloat / order_sub.fieldbyname('PRTBILI').AsFloat;
              end
              else
              begin
                Fieldbyname('OrderUnit').value := 'M';
                Fieldbyname('funit').value := 'KG';
                Fieldbyname('fQty').value := frmContractListSel.Order_Main.Fieldbyname('SCSQty').AsFloat / order_sub.fieldbyname('PRTBILI').AsFloat;
              end;
              post;
            end;

          end;
          Order_Main.Edit;
          Order_Main.fieldbyname('SSel').value := false;
        end;
        if fzt <> '1' then
        begin
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1 * from JYOrder_Main where MPRTCode=''' + Trim(MPRTCode.Text) + '''');
            sql.Add(' order by FillTime desc');
//          ShowMessage(sql.Text);
            Open;
          end;
          if ADOTemp.IsEmpty = False then
          begin
            PBFactoryName.Text := ADOTemp.fieldbyname('PBFactoryName').asstring;
            JGFactoryName.Text := ADOTemp.fieldbyname('JGFactoryName').asstring;
            GDY.Text := ADOTemp.fieldbyname('GDY').asstring;

            lidan.Text := ADOTemp.fieldbyname('lidan').asstring;
            DSGY.Text := ADOTemp.fieldbyname('DSGY').asstring;
            SLDYQ.Text := ADOTemp.fieldbyname('SLDYQ').asstring;
            SSL.Text := ADOTemp.fieldbyname('SSL').asstring;
            PHZ.Text := ADOTemp.fieldbyname('PHZ').asstring;

            KQQ.Text := ADOTemp.fieldbyname('KQQ').asstring;
            TANGSUO.Text := ADOTemp.fieldbyname('TANGSUO').asstring;
            Feel.Text := ADOTemp.fieldbyname('Feel').asstring;
            RSYQ.Text := ADOTemp.fieldbyname('RSYQ').asstring;
            SCYQ.Text := ADOTemp.fieldbyname('SCYQ').asstring;
            JGType.Text := ADOTemp.fieldbyname('JGType').asstring;

//          GONGYI.Text := ADOTemp.fieldbyname('GONGYI').asstring;
            Note.Text := ADOTemp.fieldbyname('Note').asstring;
            PBNOTE.Text := ADOTemp.fieldbyname('PBNOTE').asstring;

            BQKH.Text := ADOTemp.fieldbyname('BQKH').asstring;
            BJTT.Text := ADOTemp.fieldbyname('BJTT').asstring;
            LBName.Text := ADOTemp.fieldbyname('LBName').asstring;


//        MPRTCF.Text := CDS_Main.fieldbyname('CYCF').asstring;
//        MPRTMF.Text := CDS_Main.fieldbyname('CYmf').asstring;
//        MPRTKZ.Text := CDS_Main.fieldbyname('CYkz').asstring;

          end;
        end;

      end;
    end;
  finally
    frmContractListSel.Free;
  end;
  CPXX();
end;

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

  with ORDER_SUB do
  begin
    Edit;
    if FieldByName('OrderUnit').Value = 'KG' then
    begin
      FieldByName('FQTY').Value := FieldByName('PRTOrderQty').ASFLOAT * FieldByName('PRTBILI').ASFLOAT;
    end;

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

procedure TfrmOrderInPut.cxgrdbclmnv1Column9PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FJQZS, FFQty, FQty, FBL: DOUBLE;
  FUnit, FFUnit: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with ORDER_SUB do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;

  end;
  with ORDER_SUB do
  begin
    if (FFieldName = 'OrderUnit') or (FFieldName = 'FUNIT') then
    begin
      GetLLXS(MPRTMF.Text, MPRTKZ.Text);
    end;

    FQty := StrToFloatDef(fieldbyname('PRTOrderQty').asstring, 0);
    FFQty := StrToFloatDef(fieldbyname('FQTY').asstring, 0);
    FBL := StrToFloatDef(fieldbyname('PRTBILI').asstring, 1);
    FUnit := fieldbyname('OrderUnit').asstring;
    FFUnit := fieldbyname('funit').asstring;
  end;

  if (FQty <> 0) and (FBL <> 0) then
  begin
    if trim(FUnit) = 'KG' then
    begin
      if Trim(FFUnit) = 'KG' then
      begin
        FFQty := FQty;
      end
      else if Trim(FFUnit) = 'Y' then
      begin
        FBL := RoundFloat(FBL / 0.9144, 2);
        FFQty := FQty * FBL;
      end
      else
        FFQty := FQty * FBL;

    end
    else if FUnit = 'Y' then
    begin
      if FFUnit = 'KG' then
      begin
        FBL := RoundFloat(FBL / 0.9144, 2);
        FFQty := FQty / FBL;
      end
      else if FFUnit = 'Y' then
      begin
        FFQty := FQty;
      end
      else
        FFQty := RoundFloat(FQty * 0.9144, 2);
    end
    else
    begin
      if FFUnit = 'KG' then
      begin
        FFQty := FQty / FBL;
      end
      else if FFUnit = 'Y' then
      begin
        FFQty := RoundFloat(FQty / 0.9144, 2);
      end
      else
        FFQty := FQty;
    end;
  end;

  with ORDER_SUB do
  begin
    Edit;
    FieldByName('PRTBILI').Value := FBL;
    FieldByName('PRTOrderQty').Value := FQty;
    FieldByName('fqty').Value := FFQty;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.MPRTMFKeyPress(Sender: TObject; var Key: Char);
var
  FKZ, FMF: string;
begin
  if Key = #13 then
  begin
    FKZ := Setstring(MPRTKZ.Text);
    FMF := Setstring(MPRTMF.Text);
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        if (Order_Sub.Fieldbyname('zsunit').AsString = 'KG') then
        begin
          if (Order_Sub.Fieldbyname('funit').AsString = 'M') and (StrToFloatdef(trim(FKZ), 0) <> 0) and (StrToFloatdef(trim(FMF), 0) <> 0) then
          begin
            Fieldbyname('PRTBILI').value := (format('%.2f', [1000 / strtofloat(FMF) / strtofloat(FKZ) * 100]));
            Fieldbyname('FQTY').value := Fieldbyname('PRTOrderQty').value * (format('%.2f', [1000 / strtofloat(FMF) / strtofloat(FKZ) * 100]));
          end
          else if (Order_Sub.Fieldbyname('funit').AsString = 'Y') and (StrToFloatdef(trim(FKZ), 0) <> 0) and (StrToFloatdef(trim(FMF), 0) <> 0) then
          begin
            Fieldbyname('PRTBILI').value := (format('%.2f', [1000 / strtofloat(FMF) / strtofloat(FKZ) * 100 / 0.9144]));
            Fieldbyname('FQTY').value := Fieldbyname('PRTOrderQty').value * (format('%.2f', [1000 / strtofloat(FMF) / strtofloat(FKZ) * 100 / 0.9144]));
          end;
        end
        else
        begin
          if (Order_Sub.Fieldbyname('funit').AsString = 'M') and (StrToFloatdef(trim(FKZ), 0) <> 0) and (StrToFloatdef(trim(FMF), 0) <> 0) then
          begin
            Fieldbyname('PRTBILI').value := (format('%.2f', [1000 / strtofloat(FMF) / strtofloat(FKZ) * 100]));
            Fieldbyname('PRTOrderQty').value := Fieldbyname('FQTY').value / (format('%.2f', [1000 / strtofloat(FMF) / strtofloat(FKZ) * 100]));
          end
          else if (Order_Sub.Fieldbyname('funit').AsString = 'Y') and (StrToFloatdef(trim(FKZ), 0) <> 0) and (StrToFloatdef(trim(FMF), 0) <> 0) then
          begin
            Fieldbyname('PRTBILI').value := (format('%.2f', [1000 / strtofloat(FMF) / strtofloat(FKZ) * 100 / 0.9144]));
            Fieldbyname('PRTOrderQty').value := Fieldbyname('fqty').value * (format('%.2f', [1000 / strtofloat(FMF) / strtofloat(FKZ) * 100 / 0.9144]));
          end;
        end;

        Post;
        Next;
      end;
    end;

  end;
end;

procedure TfrmOrderInPut.GetLLXS(MF: string; KZ: string);
var
  FMPizhong, FSSL, FYTNum, FQtyHZ, FMF1, FMF2, FMF, FKZ1, FKZ, FXS: string;
  Gint, Xint, CMInt, FInt, KZInt, FBeg1, FEnd1, FBeg2, FEnd2: Integer;
  FReal: Double;
begin
  FMF := Trim(MF);
  FKZ := Trim(KZ); //53-54"  160-0CM 240.00G/M2    152-153CM
  Gint := Pos('-', FMF);
  Xint := Pos('/', FMF);
  CMInt := Pos('C', FMF);
  FInt := Pos('"', FMF);
  KZInt := Pos('G', FKZ);
  if FInt > 0 then
  begin
    FMF := Copy(FMF, 1, FInt - 1);
    if Gint > 0 then
    begin
      FBeg1 := 1;
      FEnd1 := Gint - 1;
      FBeg2 := Gint + 1;
      FEnd2 := Length(FMF);
    end;
    if Xint > 0 then
    begin
      FBeg1 := 1;
      FEnd1 := Xint - 1;
      FBeg2 := Xint + 1;
      FEnd2 := Length(FMF);
    end;
    FMF1 := Copy(FMF, FBeg1, FEnd1);
    FMF2 := Copy(FMF, FBeg2, FEnd2);
  end;
  if CMInt > 0 then
  begin
    FMF := Copy(FMF, 1, CMInt - 1);
    FBeg1 := 1;
    FEnd1 := Gint - 1;
    FBeg2 := Gint + 1;
    FEnd2 := Length(FMF);
    FMF1 := Copy(FMF, 1, FEnd1);
    FMF2 := Copy(FMF, FBeg2, FEnd2);
  end;
  Edit1.Text := FMF1;
  Edit2.Text := FMF2;
  if KZInt > 0 then
  begin
    FKZ1 := Copy(FKZ, 1, KZInt - 1);
  end;
  Edit3.Text := FKZ1;
  if Trim(Edit1.Text) = '' then
    Edit1.Text := '0';
  if Trim(Edit2.Text) = '' then
    Edit2.Text := '0';
  if TryStrToFloat(Edit1.Text, FReal) = False then
  begin
    Edit1.Text := '0';
  end;
  if TryStrToFloat(Edit2.Text, FReal) = False then
  begin
    Edit2.Text := '0';
  end;
  if StrToInt(Edit1.Text) > StrToInt(Edit2.Text) then
  begin
    FMF := Edit1.Text;
  end
  else
  begin
    FMF := Edit2.Text;
  end;
  if Trim(Edit3.Text) = '' then
    Edit3.Text := '0';
  if TryStrToFloat(Edit3.Text, FReal) = False then
  begin
    Edit3.Text := '0';
  end;
  FKZ := Edit3.Text;
  if FInt > 0 then
  begin
    FMF := FloatToStr(StrToFloat(FMF) * 2.54);
  end;
  if (FMF = '0') or (FKZ = '0') then
  begin
    FXS := '0';
  end
  else
  begin
    FXS := FloatToStr(100000 / (StrToFloat(FMF) * StrToFloat(FKZ)));
  end;
//  if FUNIT = 'Y' then
//  begin
//    Order_Sub.FieldByName('PRTBILI').Value := FloatToStr(RoundFloat(StrToFloat(FXS) / 0.9144, 2));
//  end
//  else
  order_sub.edit;
  Order_Sub.FieldByName('PRTBILI').Value := FloatToStr(RoundFloat(StrToFloat(FXS), 2));
  order_sub.post;
  //以上取系数，根据门幅克重计算
//  FQtyHZ := Tv1.DataController.Summary.FooterSummaryTexts[0];
//  if Trim(FQtyHZ) = '' then
//    FQtyHZ := '0';
//  FSSL := Trim(SSL.Text);
//  if Trim(FSSL) = '' then
//    FSSL := '0';
end;

procedure TfrmOrderInPut.ToolButton4Click(Sender: TObject);
var
  FConMainId, FMPRTCode, fhtxh, fdw: string;
begin
  if ConNo.Text = '' then
  begin
    Application.MessageBox('合同号不能为空！', '提示', 0);
    Exit;
  end;
  try
    frmContractListSel := TfrmContractListSel.Create(Application);
    with frmContractListSel do
    begin
      fconNo := trim(self.ConNo.Text);
      if ShowModal = 1 then
      begin
        Order_Main.locate('SSel', true, []);
        while Order_Main.locate('SSel', true, []) do
        begin

//          if self.MPRTCode.Text <> trim(frmContractListSel.Order_Main.Fieldbyname('SCSCode').AsString) then
//          begin
//            Application.MessageBox('不能选择多个产品！', '提示', 0);
//            break;
//          end;
//          if self.MPRTMF.Text <> trim(frmContractListSel.Order_Main.Fieldbyname('SCSMF').AsString) then
//          begin
//            Application.MessageBox('不能选择多个产品！', '提示', 0);
//            break;
//          end;
//          if self.MPRTKZ.Text <> trim(frmContractListSel.Order_Main.Fieldbyname('SCSKZ').AsString) then
//          begin
//            Application.MessageBox('不能选择多个产品！', '提示', 0);
//            break;
//          end;
          if self.Order_Sub.locate('ConSubId', trim(Order_Main.Fieldbyname('SubId').AsString), []) then
          begin
            Order_Main.Edit;
            Order_Main.fieldbyname('SSel').value := false;
            continue;
          end;
          with self.Order_Sub do
          begin
            append;

            Fieldbyname('PrtKuanNO').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSKX').AsString);
            Fieldbyname('PrtColorNo').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSColorNo').AsString);
            Fieldbyname('PrtColor').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSColor').AsString);
            Fieldbyname('PRTHX').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSHX').AsString);
            Fieldbyname('PRTOrderQty').value := frmContractListSel.Order_Main.Fieldbyname('SCSQty').AsFloat;
            Fieldbyname('OrderUnit').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSUnit').AsString);
            Fieldbyname('SubNote').value := trim(frmContractListSel.Order_Main.Fieldbyname('SCSNote').AsString);
            Fieldbyname('ConSubId').value := trim(frmContractListSel.Order_Main.Fieldbyname('SubId').AsString);
            Fieldbyname('QRCOLOR').value := trim(frmContractListSel.Order_Main.Fieldbyname('HTQRCOLOR').AsString);

            GetLLXS(frmContractListSel.Order_Main.Fieldbyname('SCSMF').AsString, frmContractListSel.Order_Main.Fieldbyname('SCSKZ').AsString);

            if order_sub.fieldbyname('PRTBILI').AsFloat <> 0 then
            begin
              edit;
              if (trim(frmContractListSel.Order_Main.Fieldbyname('SCSUnit').AsString) = 'KG') or (trim(frmContractListSel.Order_Main.Fieldbyname('SCSUnit').AsString) = 'Kg') then
              begin
                Fieldbyname('OrderUnit').value := 'KG';
                Fieldbyname('funit').value := 'M';
                Fieldbyname('fQty').value := frmContractListSel.Order_Main.Fieldbyname('SCSQty').AsFloat * order_sub.fieldbyname('PRTBILI').AsFloat;
              end
              else if trim(frmContractListSel.Order_Main.Fieldbyname('SCSUnit').AsString) = 'Y' then
              begin
                Fieldbyname('OrderUnit').value := 'Y';
                Fieldbyname('funit').value := 'KG';
                order_sub.fieldbyname('PRTBILI').Value := order_sub.fieldbyname('PRTBILI').AsFloat / 0.9144;
                Fieldbyname('fQty').value := frmContractListSel.Order_Main.Fieldbyname('SCSQty').AsFloat / order_sub.fieldbyname('PRTBILI').AsFloat;
              end
              else
              begin
                Fieldbyname('OrderUnit').value := 'M';
                Fieldbyname('funit').value := 'KG';
                Fieldbyname('fQty').value := frmContractListSel.Order_Main.Fieldbyname('SCSQty').AsFloat / order_sub.fieldbyname('PRTBILI').AsFloat;
              end;
              post;
            end;

          end;
          Order_Main.Edit;
          Order_Main.fieldbyname('SSel').value := false;
        end;

      end;
    end;
  finally
    frmContractListSel.Free;
  end;
end;

procedure TfrmOrderInPut.JGFactoryName2BtnUpClick(Sender: TObject);
var
  FJGFactoryName: string;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
//      frmZdyAttachGYS.FactType.ItemIndex:=FactType.Items.IndexOf('加工厂');

      if ShowModal = 1 then
      begin
//        with CDS_HZ do
//        begin
//          First;
//          FJGFactoryName := '';
//          while CDS_HZ.Locate('SSel', true, []) do
//          begin
//            FJGFactoryName := FJGFactoryName + '/' + Trim(CDS_HZ.fieldbyname('CoName').AsString);
//            CDS_HZ.Delete;
//          end;
//        end;
        JGFactoryName2.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
//        JGFactoryName.Text := Copy(FJGFactoryName, 2, Length(FJGFactoryName));
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmOrderInPut.JGFactoryName3BtnUpClick(Sender: TObject);
var
  FJGFactoryName: string;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
//      frmZdyAttachGYS.FactType.ItemIndex:=FactType.Items.IndexOf('加工厂');

      if ShowModal = 1 then
      begin
//        with CDS_HZ do
//        begin
//          First;
//          FJGFactoryName := '';
//          while CDS_HZ.Locate('SSel', true, []) do
//          begin
//            FJGFactoryName := FJGFactoryName + '/' + Trim(CDS_HZ.fieldbyname('CoName').AsString);
//            CDS_HZ.Delete;
//          end;
//        end;
        JGFactoryName3.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
//        JGFactoryName.Text := Copy(FJGFactoryName, 2, Length(FJGFactoryName));
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmOrderInPut.ToolButton5Click(Sender: TObject);
begin
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from SalesContract_Main D WHERE  D.ConNo= ' + quotedstr(trim(ConNo.Text)));

    Open;
  end;
//  if ADOQueryLabel.IsEmpty then
//    exit;
  with RMGridReport1 do
  begin
//    LoadFromBlobField(tblobfield(ADOQueryLabel.fieldbyname('labelFile')));

//    RMVariables['MPRTCodeName'] := MPRTCodeName.Text;
    RMVariables['KHCONNO'] := KHConNo.Text;
    RMVariables['sellname'] := ADOTemp.FIELDBYNAME('sellname').AsString;
    RMVariables['MXKUANNO'] := Order_Sub.FIELDBYNAME('PRTKUANNO').AsString;

    RMVariables['PRTKUANNO'] := Order_Sub.FIELDBYNAME('PRTKUANNO').AsString;
    RMVariables['mxkh'] := CustomerNoName.Text;
    RMVariables['specolor'] := Order_Sub.FIELDBYNAME('PRTEColor').AsString;
    RMVariables['spcolor'] := Order_Sub.FIELDBYNAME('PRTColor').AsString;
    RMVariables['BQKH'] := BQKH.Text;

    RMVariables['PRTcolor'] := Order_Sub.FIELDBYNAME('PRTColor').AsString;
    RMVariables['MXGangHao'] := '缸号';
    RMVariables['MXMJXH'] := '1';
    RMVariables['mxqty'] := '10';
    RMVariables['mxqty2'] := '10';
    RMVariables['mjmaoz'] := '20';
    RMVariables['BQKH'] := BQKH.Text;
    RMVariables['MPRTCF'] := MPRTCF.Text;
    RMVariables['MPRTKZ'] := MPRTKZ.Text;
    RMVariables['MXSPEC'] := MprtsPEC.Text;
    RMVariables['mxtt'] := BJTT.Text;

    RMVariables['spCF'] := MPRTCF.Text;
    RMVariables['spmf'] := MPRTMF.Text;
    RMVariables['spKZ'] := MPRTKZ.Text;
    RMVariables['spname'] := MPRTCodeName.Text;
    RMVariables['spCODE'] := MPRTCode.Text;
    RMVariables['orderno'] := orderno.Text;
    //Preview :=RMPreview1;
    ShowReport;
  end;
end;

procedure TfrmOrderInPut.PBFactoryName2BtnUpClick(Sender: TObject);
var
  FPBFactoryName: string;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      frmZdyAttachGYS.FactType.ItemIndex := FactType.Items.IndexOf('坯布厂');
      if ShowModal = 1 then
      begin
//        with CDS_HZ do
//        begin
//          First;
//          FPBFactoryName := '';
//          while CDS_HZ.Locate('SSel', true, []) do
//          begin
//            FPBFactoryName := FPBFactoryName + '/' + Trim(CDS_HZ.fieldbyname('CoName').AsString);
//            CDS_HZ.Delete;
//          end;
//        end;
        PBFactoryName2.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmOrderInPut.PBFactoryName3BtnUpClick(Sender: TObject);
var
  FPBFactoryName: string;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      frmZdyAttachGYS.FactType.ItemIndex := FactType.Items.IndexOf('坯布厂');
      if ShowModal = 1 then
      begin
//        with CDS_HZ do
//        begin
//          First;
//          FPBFactoryName := '';
//          while CDS_HZ.Locate('SSel', true, []) do
//          begin
//            FPBFactoryName := FPBFactoryName + '/' + Trim(CDS_HZ.fieldbyname('CoName').AsString);
//            CDS_HZ.Delete;
//          end;
//        end;
        PBFactoryName3.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

end.

