unit U_JWLOrderInPut;

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, RM_Dataset, Menus,
  cxSplitter, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCheckBox,
  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
  TfrmjwlInPut = 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;
    v1OrderUnit: TcxGridDBColumn;
    Label3: TLabel;
    OrdDate: TDateTimePicker;
    Label2: TLabel;
    Label25: TLabel;
    DLYDate: TDateTimePicker;
    Label41: TLabel;
    GDY: TBtnEditC;
    Label42: TLabel;
    MPRTChuanY: TMemo;
    v1Column1: TcxGridDBColumn;
    Note: TMemo;
    Label23: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column2: TcxGridDBColumn;
    Label4: TLabel;
    KHConNo: TEdit;
    ADOQueryLabel: TADOQuery;
    RM2: TRMGridReport;
    ZongPS: TEdit;
    ZongQty: TEdit;
    Label17: TLabel;
    YWY: TBtnEditC;
    Label27: TLabel;
    bzyq: TEdit;
    Label30: TLabel;
    XGF: TEdit;
    CPNum: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    Label33: TLabel;
    Label38: TLabel;
    QtyNote: TEdit;
    Label39: TLabel;
    PCNote: TEdit;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    RM1: TRMGridReport;
    CDS_PRT: TClientDataSet;
    RMDBMain: TRMDBDataSet;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    CDS_Copy: TClientDataSet;
    Label52: TLabel;
    Label_Juan: TMemo;
    Label51: TLabel;
    Label54: TLabel;
    DSFact: TDataSource;
    CDS_Fact: TClientDataSet;
    PM_Fact: TPopupMenu;
    MenuItem1: TMenuItem;
    N1: TMenuItem;
    Label_Bao: TMemo;
    cxSplitter1: TcxSplitter;
    N3: TMenuItem;
    N4: TMenuItem;
    Label55: TLabel;
    JGFactoryName: TBtnEditC;
    ADOQuery1: TADOQuery;
    CXSHSTATUS: TEdit;
    Status: TEdit;
    Label18: TLabel;
    FILLER: TEdit;
    ADOQueryCmdSC: TADOQuery;
    ADOQueryCmdFileContent: TBlobField;
    ADOQueryCmdFtFileName: TStringField;
    ADOQueryCmdFileEditDate: TDateTimeField;
    ADOQueryCmdFileSize: TFloatField;
    ADOQueryCmdFiller: TStringField;
    ADOQueryCmdLastEditTime: TDateTimeField;
    ADOQueryCmdLastEditer: TStringField;
    ADOQueryCmdFileCreateDate: TDateTimeField;
    ADOQueryCmdchildPath: TStringField;
    ADOQueryCmdFileType: TStringField;
    Label40: TLabel;
    Label5: TLabel;
    JYBZNote: TMemo;
    SHNOTE: TMemo;
    Label6: TLabel;
    MPRTCode: TEdit;
    MPRTCODENAME: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    RMPreview1: TRMPreview;
    ToolButton3: TToolButton;
    Label8: TLabel;
    lbname: TBtnEditC;
    ToolButton4: TToolButton;
    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 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 MPRTChuanYDblClick(Sender: TObject);
    procedure MPRTCodeBtnUpClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ScrollBox1Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure V2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure KHCPCodeBtnUpClick(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    FSCXDH: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function UPLabel(LabelName, LabelPath: string): Boolean;
    function PostFileToData(LabelName, LabelPath: string): boolean;
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo, PXGQX: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    FXGNOTE, STR: string;
    { Public declarations }
  end;

var
  frmjwlInPut: TfrmjwlInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachGYS,
  U_CPManageSel, U_FLManageSEL, U_LabelList2;

{$R *.dfm}
function TfrmjwlInPut.UPLabel(LabelName, LabelPath: string): Boolean;
begin
  try
    ADOCmd.Connection.BeginTrans;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from P_Label');
      sql.Add('where LabelCaption=' + quotedstr(LabelName));
      Open;
      if ADOCmd.IsEmpty then
      begin
        Append;
        fieldByName('filler').value := DName;
        fieldByName('filltime').value := DServerDate;
        fieldByName('beizhu').value := '';
        fieldByName('LabelCaption').value := trim(LabelPath);
        fieldByName('LabelType').value := '中文标签';
        fieldByName('LabelFileName').value := trim(LabelName);
        RM1.SaveToBlobField(TBlobField(FieldByName('LabelFile')));
      end
      else
      begin
        Edit;
        fieldByName('Editer').value := DName;
        fieldByName('EditTime').value := DServerDate;
      end;
      RM1.SaveToBlobField(TBlobField(FieldByName('LabelFile')));
      Post;
    end;

    ADOCmd.Connection.CommitTrans;
    result := true;
  except
    ADOCmd.Connection.RollbackTrans;
    Result := false;
    application.MessageBox('保存标签模板出错!', '警告信息', 0);
  end;
end;

function TfrmjwlInPut.PostFileToData(LabelName, LabelPath: string): boolean;
var
  mFileName, fFileName, fpathFileName: string;
  Stream: TMemoryStream;
  mfileSize: integer;
  mCreationTime: TdateTime;
  mWriteTime: TdateTime;
//  ADOQueryCmdFileContent: TBlobField;
begin
  result := false;
  fFileName := Trim(LabelName) + '.rmf';
  fpathFileName := Trim(LabelPath);
  try
    ADOQueryCmdSC.Connection.BeginTrans;
    try
      with ADOQueryCmdSC do
      begin
        close;
        sql.Clear;
        sql.Add('delete  from RT_FileUpdate');
        sql.Add('where FileName=' + quotedStr(trim(fFileName)));
        execsql;
      end;
      with ADOQueryCmdSC do
      begin
        close;
        sql.Clear;
        sql.Add('select * from RT_FileUpdate');
        sql.Add('where FileName=' + quotedStr(trim(fFileName)));
        Open;
        //////////////////////////
        //获取文件信息
        GetFileInfo(Trim(fpathFileName), mfileSize, mCreationTime, mWriteTime);

        if RecordCount <= 0 then
        begin
          Append;
          fieldByName('FileName').AsString := trim(fFileName);
        end
        else
        begin
          edit;
        end;

        fieldByName('FileEditDate').Value := mWriteTime;
        fieldByName('FileCreateDate').Value := mCreationTime;
        fieldByName('FileSize').Value := mfileSize;
        fieldByName('Filler').Value := Dname;
        fieldByName('LastEditer').Value := Dname;
        fieldByName('LastEditTime').Value := Now();
//        if pos('.rmf',fFileName)>0 then
        begin
          fieldByName('FilePath').Value := 'report';
          fieldByName('FileType').Value := '公用';
        end;

        //将OLE数据存入数据库
        RM1.SaveToBlobField(TBlobField(FieldByName('Files')));

//        ADOQueryCmdFileContent.LoadFromFile(fpathFileName);
        //ADOQueryCmdFileContent.LoadFromStream(Stream);

        post;
      end;
    finally
    end;
    result := true;
    ADOQueryCmdSC.Connection.CommitTrans;
  except
    ADOQueryCmdSC.Connection.RollbackTrans;
    Result := False;
    application.MessageBox(pchar('提交文件[' + trim(fFileName) + ']失败!'), '提示信息', MB_ICONERROR);
  end;

end;

procedure TfrmjwlInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('指示单录入AA', Tv1, '生产指示单管理9');
  Close;

end;

procedure TfrmjwlInPut.InitData();
var
  FDate: TDateTime;
begin
  FDate := SGetServerDateTime(ADOTemp);
  OrdDate.DateTime := FDate;
  DlyDate.DateTime := FDate;

  if PState = 1 then
    OrderNo.Enabled := False;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from FLOrder_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 FLOrder_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;

  SCSHDataNew(ADOTemp, ScrollBox1, 2);
  CXSHSTATUS.Text := ADOTemp.FieldByName('CXSHSTATUS').AsString;
  STATUS.Text := ADOTemp.FieldByName('STATUS').AsString;

  FILLER.Text := ADOTemp.FieldByName('FILLER').AsString;

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_Fact 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, CDS_Fact);
  SInitCDSData20(ADOTemp, CDS_Fact);

  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := OrdDate.DateTime;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '';
    OrderNo.Enabled := True;

    OrdDate.DateTime := FDate;

    Label_Juan.Text := '';
    Label_Bao.Text := '';

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

    with CDS_Fact do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('FACTID').Value := '';

        Post;
        Next;
      end;
    end;
  end;

end;

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

procedure TfrmjwlInPut.FormShow(Sender: TObject);
var
  i: Integer;
begin
  readCxGrid('指示单录入AA', Tv1, '生产指示单管理9');
  {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();

end;

function TfrmjwlInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno, MaxFNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'FL', 'FLOrder_Main', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
//      if GetLSNo(ADOCmd, maxorderno, 'RT', 'JYOrder_Main', 3, 1) = False then
//      begin
//        ADOCmd.Connection.RollbackTrans;
//        Application.MessageBox('取最大号失败!', '提示', 0);
//        Exit;
//      end;
//      OrderNo.Text := TRIM(maxorderno);
//      OrderNo.Text:=getOrderNo(trim(conNo.Text));
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from FLOrder_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('SCXDH').Value := FSCXDH;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);

      SSetsaveSqlNew(ADOCmd, 'FLOrder_Main', ScrollBox1, 2);

      FieldByName('JHGZ').Value := '按缸排序';
      FieldByName('YXJL').Value := '米数';

      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
//      if jhgz.Text = '' then
//      begin
//        FieldByName('jhgz').Value := '按缸生成';
//      end;

      Post;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from FLOrder_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, 'FS', 'FLOrder_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 FLOrder_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;

          if Trim(Order_Sub.fieldbyname('SubId').AsString) <> '' then
          begin
            with ADOQuery1 do
            begin
              Close;
              SQL.Clear;
              sql.Add('select * from FLOrder_Sub where MainId=''' + Trim(maxno) + '''');
              sql.Add(' and SubId=''' + Trim(Smaxno) + '''');
              Open;
            end;
            if Order_Sub.fieldbyname('PRTOrderQty').Value <> ADOQuery1.fieldbyname('PRTOrderQty').Value then
            begin
              with ADOTemp do
              begin
                Close;
                SQL.Clear;
                sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
                sql.Add(' ' + quotedstr(trim(DName)));
                sql.Add(',getdate() ');
                sql.Add(',' + quotedstr(trim(self.Caption)));
                sql.Add(',' + quotedstr(trim('数量修改')));
                sql.Add(',' + quotedstr(trim('子表ID：' + trim(Smaxno) + ';原数量:' + ADOQuery1.fieldbyname('PRTOrderQty').AsString + ';现数量:' + Order_Sub.fieldbyname('PRTOrderQty').AsString)));
                sql.Add(',' + quotedstr(trim('成功')));
                sql.Add(')');
                ExecSQL;

              end;

            end;
          end;
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(Smaxno);

          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'FLOrder_Sub', 0);
          FieldByName('MGMainId').Value := Trim(Order_Sub.fieldbyname('MGMainId').AsString);
//          FieldByName('ConSubId').Value := Trim(Order_Sub.fieldbyname('ConSubId').AsString);
          Post;
        end;
        Next;
      end;
    end;

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

procedure TfrmjwlInPut.TBSaveClick(Sender: TObject);
var
  MAXNO1: string;
begin
  if PState <> 1 then
  begin
    OrdDate.SetFocus;
  end;

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

  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from FLOrder_Main where ltrim(rtrim(MPRTCode))=''' + Trim(MPRTCode.Text) + '''');
    Open;
  end;
  if ADOCmd.IsEmpty then
  begin
    FSCXDH := '首次下大货';
  end;


//  if Trim(conNo.Text) = '' then
//  begin
//    Application.MessageBox('合同号不能为空！', '提示', 0);
//    Exit;
//  end;
  {if Trim(JGFactoryName.Text) = '' then
  begin
    Application.MessageBox('加工厂不能为空！', '提示', 0);
    Exit;
  end; }
  if Status.Text = '1' then
  begin
    if Trim(GDY.Text) = '' then
    begin
      Application.MessageBox('跟单员不能为空！', '提示', 0);
      Exit;
    end;
  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('TPPS', null, []) then
  begin
    Application.MessageBox('投匹匹数不能为空!', '提示', 0);
    Exit;
  end;
  if FMainId <> '' then
  begin
    if (FXGNOTE = '已修改') and (CXSHSTATUS.Text = '99') then
    begin
      if InputQuery('修改备注', '修改备注：', str) then
      begin

      end
      else
      begin
        Exit;
      end;

    end;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmjwlInPut.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 TfrmjwlInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
  FE: Boolean;
begin
  FE := False;
  try
    frmflManageSEL := TfrmflManageSEL.Create(Application);
    with frmflManageSEL do
    begin
      if ShowModal = 1 then
      begin
        if Self.MPRTCODENAME.Text = '' then
        begin
          CDS_Main.Locate('SSel', True, []);
          Self.MPRTCODENAME.Text := trim(CDS_Main.FieldByName('CYNoLX').AsString);
          Self.MPRTCode.Text := trim(CDS_Main.FieldByName('CYEName').AsString);
          Self.PCNote.Text := trim(CDS_Main.FieldByName('MC').AsString);
          Self.bzyq.Text := trim(CDS_Main.FieldByName('BZCC').AsString);
        end;
        while CDS_Main.Locate('SSel', True, []) do
        begin

//          if Trim(Self.MPRTCODENAME.Text) <> trim(CDS_Main.FieldByName('CYNoLX').AsString) then
//          begin
//            FE := True;
//            Break;
//          end;
//          if Trim(Self.MPRTCode.Text) <> trim(CDS_Main.FieldByName('CYEName').AsString) then
//          begin
//            FE := True;
//            Break;
//          end;
//          if Trim(Self.PCNote.Text) <> trim(CDS_Main.FieldByName('MC').AsString) then
//          begin
//            FE := True;
//            Break;
//          end;
//          if Trim(Self.bzyq.Text) <> trim(CDS_Main.FieldByName('BZCC').AsString) then
//          begin
//            FE := True;
//            Break;
//          end;
          with Self.Order_Sub do
          begin
            Append;
            FieldByName('PRTColorNoKH').Value := CDS_Main.FieldByName('CYHX').Value;
            FieldByName('PRTEColor').Value := CDS_Main.FieldByName('CYColor').Value;
            FieldByName('CYID').Value := CDS_Main.FieldByName('CYMJID').Value;
            FieldByName('CYNo').Value := CDS_Main.FieldByName('CYNo').Value;
            FieldByName('hxFile').Value := '';
            FieldByName('TPPS').Value := 0;
            Post;
          end;
          CDS_Main.Delete;
        end;

      end;
    end;
  finally
    frmflManageSEL.Free;
  end;
//  if FE then
//  begin
//    Application.MessageBox('请选择相同产品!', '提示', 0);
//  end;
  FXGNOTE := '已修改';
end;

procedure TfrmjwlInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Order_Sub.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除选择的数据吗？', '提示', 32 + 4) <> IDYES then
    exit;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    while Locate('SSel', True, []) do
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete FLOrder_Sub where subid=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + '''');
        ExecSQL;
      end;
      Delete;
    end;
  end;
  Order_Sub.EnableControls;
end;

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

procedure TfrmjwlInPut.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;

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

procedure TfrmjwlInPut.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 TfrmjwlInPut.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 TfrmjwlInPut.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);
//       ShowMessage(flag);
      if Trim(flag) = 'FLNAME' then
      begin
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end
      else
      begin
        fnote := false;
      end;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        if Trim(flag) = 'FLNAME' then
        begin
          MPRTCode.Text := Trim(ClientDataSet1.fieldbyname('note').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmjwlInPut.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 TfrmjwlInPut.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 TfrmjwlInPut.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 TfrmjwlInPut.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;

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

procedure TfrmjwlInPut.N2Click(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmjwlInPut.ScrollBox1Click(Sender: TObject);
begin
  OrdDate.SetFocus;
end;

procedure TfrmjwlInPut.ToolButton6Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Order_Sub.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要复制选择的数据吗？', '提示', 32 + 4) <> IDYES then
    exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from FLOrder_Sub where 1=2');
    Open;
  end;
  SCreateCDS20(ADOTemp, CDS_Copy);
  SInitCDSData20(ADOTemp, CDS_Copy);
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean = True then
      begin
        with CDS_Copy do
        begin
          Append;
          CDS_Copy.FieldByName('PRTColor').Value := Order_Sub.FieldByName('PRTColor').Value;
          CDS_Copy.FieldByName('PRTColorNo').Value := Order_Sub.FieldByName('PRTColorNo').Value;
          CDS_Copy.FieldByName('PRTHX').Value := Order_Sub.FieldByName('PRTHX').Value;
          CDS_Copy.FieldByName('PRTEColor').Value := Order_Sub.FieldByName('PRTEColor').Value;
          CDS_Copy.FieldByName('PRTPs').Value := Order_Sub.FieldByName('PRTPs').Value;
          CDS_Copy.FieldByName('PRTOrderQty').Value := Order_Sub.FieldByName('PRTOrderQty').Value;
          CDS_Copy.FieldByName('OrderUnit').Value := Order_Sub.FieldByName('OrderUnit').Value;
          CDS_Copy.FieldByName('SubNote').Value := Order_Sub.FieldByName('SubNote').Value;
          CDS_Copy.FieldByName('TPPS').Value := Order_Sub.FieldByName('TPPS').Value;
          CDS_Copy.FieldByName('TPQty').Value := Order_Sub.FieldByName('TPQty').Value;
          CDS_Copy.FieldByName('TPUnit').Value := Order_Sub.FieldByName('TPUnit').Value;
          CDS_Copy.FieldByName('PRTHXKH').Value := Order_Sub.FieldByName('PRTHXKH').Value;
          CDS_Copy.FieldByName('PRTColorNoKH').Value := Order_Sub.FieldByName('PRTColorNoKH').Value;
          Post;
        end;
      end;
      Next;
    end;
  end;
  Order_Sub.EnableControls;
  CDS_Copy.DisableControls;
  with CDS_Copy do
  begin
    First;
    while not Eof do
    begin
      with Order_Sub do
      begin
        Append;
        Order_Sub.FieldByName('PRTColor').Value := CDS_Copy.FieldByName('PRTColor').Value;
        Order_Sub.FieldByName('PRTColorNo').Value := CDS_Copy.FieldByName('PRTColorNo').Value;
        Order_Sub.FieldByName('PRTHX').Value := CDS_Copy.FieldByName('PRTHX').Value;
        Order_Sub.FieldByName('PRTEColor').Value := CDS_Copy.FieldByName('PRTEColor').Value;
        Order_Sub.FieldByName('PRTPs').Value := CDS_Copy.FieldByName('PRTPs').Value;
        Order_Sub.FieldByName('PRTOrderQty').Value := CDS_Copy.FieldByName('PRTOrderQty').Value;
        Order_Sub.FieldByName('OrderUnit').Value := CDS_Copy.FieldByName('OrderUnit').Value;
        Order_Sub.FieldByName('SubNote').Value := CDS_Copy.FieldByName('SubNote').Value;
        Order_Sub.FieldByName('TPPS').Value := CDS_Copy.FieldByName('TPPS').Value;
        Order_Sub.FieldByName('TPQty').Value := CDS_Copy.FieldByName('TPQty').Value;
        Order_Sub.FieldByName('TPUnit').Value := CDS_Copy.FieldByName('TPUnit').Value;
        Order_Sub.FieldByName('PRTHXKH').Value := CDS_Copy.FieldByName('PRTHXKH').Value;
        Order_Sub.FieldByName('PRTColorNoKH').Value := CDS_Copy.FieldByName('PRTColorNoKH').Value;
        Post;
      end;
      Next;
    end;
  end;
  CDS_Copy.EnableControls;
  FXGNOTE := '已修改';
end;

procedure TfrmjwlInPut.MenuItem1Click(Sender: TObject);
var
  i: Integer;
begin

  i := CDS_Fact.RecordCount;
  i := i + 1;

  with CDS_Fact do
  begin
    append;
    FieldByName('SJGXH').Value := i;
    FieldByName('SJGJQ').Value := trim(FormatDateTime('yyyy-MM-dd', OrdDate.Date + 6));
    Post;
  end;
end;

procedure TfrmjwlInPut.N1Click(Sender: TObject);
begin
  if CDS_Fact.IsEmpty then
    Exit;

  if Trim(CDS_Fact.fieldbyname('FactId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Fact where FactId=''' + Trim(CDS_Fact.fieldbyname('FactId').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_Fact.Delete;
end;

procedure TfrmjwlInPut.V2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        CDS_Fact.Edit;
        CDS_Fact.FieldByName('SJGFactory').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        CDS_Fact.FieldByName('SJGFactoryCode').Value := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        CDS_Fact.FieldByName('SJGFactoryJC').Value := Trim(CDS_HZ.fieldbyname('CoHRName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmjwlInPut.N3Click(Sender: TObject);
begin
  SelOKNo(Order_Sub, True);
end;

procedure TfrmjwlInPut.N4Click(Sender: TObject);
begin
  SelOKNo(Order_Sub, False);
end;

procedure TfrmjwlInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if PXGQX = '已审核' then
  begin
    v1PRTOrderQty.Options.Editing := False;

  end;
end;

procedure TfrmjwlInPut.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FPRTOrderQty, Fmj: Double;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    FPRTOrderQty := FieldByName('PRTOrderQty').ASFLOAT;
    Fmj := FieldByName('mj').ASFLOAT;

    Edit;
    FieldByName('MS').Value := FPRTOrderQty * Fmj;
    Post;
  end;

  FXGNOTE := '已修改';
end;

procedure TfrmjwlInPut.v1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    FieldByName('MGMainId').Value := '';
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmjwlInPut.KHCPCodeBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
//      frmZdyAttachGYS.FactType.ItemIndex := FactType.Items.IndexOf('坯布厂');
      if ShowModal = 1 then
      begin
        JGFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmjwlInPut.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FLOrderUnit';
      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 TfrmjwlInPut.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Trim(FMainId) = '' then
    exit;
  if trim(LBName.Text) = '' then
    exit;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Print_Cs_FLRoll ');
    sql.Add('@mainID=''' + trim(FMainId) + ''' ');
    Open;
  end;
  SCreateCDS20(ADOTemp, CDS_PRT);
  SInitCDSData20(ADOTemp, CDS_PRT);

//  try
//    Moudle := LoadLibrary('MakeQRBarcode.dll');
//    @Makebar := GetProcAddress(Moudle, 'Make');
//    @Mixtext := GetProcAddress(Moudle, 'MixText');
//    Txt := Trim(ADOTemp.fieldbyname('MJID').AsString);
////    Txt := '123';
//    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);
//  except
//    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
//    exit;
//  end;
  ExportFtErpFile(Trim(LBName.Text) + '.rmf', ADOTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    PostFileToData(trim(LBName.TEXT), fPrintFile);
    UPLabel(trim(LBName.TEXT), fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示信息', 0);
  end;
end;

procedure TfrmjwlInPut.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Trim(FMainId) = '' then
    exit;
  if trim(LBName.Text) = '' then
    exit;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Print_Cs_FLRoll ');
    sql.Add('@mainID=''' + trim(FMainId) + ''' ');
    Open;
  end;
  SCreateCDS20(ADOTemp, CDS_PRT);
  SInitCDSData20(ADOTemp, CDS_PRT);

//  try
//    Moudle := LoadLibrary('MakeQRBarcode.dll');
//    @Makebar := GetProcAddress(Moudle, 'Make');
//    @Mixtext := GetProcAddress(Moudle, 'MixText');
//    Txt := Trim(ADOTemp.fieldbyname('MJID').AsString);
////    Txt := '123';
//    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);
//  except
//    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
//    exit;
//  end;
  ExportFtErpFile(Trim(LBName.Text) + '.rmf', ADOTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName.Text) + '.rmf';

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    PostFileToData(trim(LBName.TEXT), fPrintFile);
    UPLabel(trim(LBName.TEXT), fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示信息', 0);
  end;
end;

end.

