unit U_ProductOrderNewList_New;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu;

type
  TfrmProductOrderNewList_New = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    Cxgrid1Level1: TcxGridLevel;
    Cxgrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    MLOrderNo: TEdit;
    TBExport: TToolButton;
    v1MLOrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1MLConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1KHName: TcxGridDBColumn;
    Label4: TLabel;
    KHNameHZ: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label9: TLabel;
    MLConNo: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    v1Column2: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    MLCodeName: TEdit;
    Label5: TLabel;
    N1: TMenuItem;
    v1Ssel: TcxGridDBColumn;
    DataSource3: TDataSource;
    Panel2: TPanel;
    Order_Print: TClientDataSet;
    Label7: TLabel;
    status: TComboBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1MLColor: TcxGridDBColumn;
    v1MLColorNo: TcxGridDBColumn;
    v1MLHX: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1MLCodeName: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    DataSource4: TDataSource;
    Ord_GX: TClientDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    v1KHNameJC: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1ZQty: TcxGridDBColumn;
    v1SYRYWY: TcxGridDBColumn;
    v1ChkZT: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DS_Con: TDataSource;
    ADOQueryCon: TADOQuery;
    CDS_Con: TClientDataSet;
    Panel5: TPanel;
    Label2: TLabel;
    Label11: TLabel;
    Label15: TLabel;
    ConBegDate: TDateTimePicker;
    ConEndDate: TDateTimePicker;
    Edit4: TEdit;
    ComboBox1: TComboBox;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    CDS_HTSelInfo: TClientDataSet;
    CDS_HTSelInfo_MX: TClientDataSet;
    Panel6: TPanel;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    N3: TMenuItem;
    N4: TMenuItem;
    v1Column1: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    RMDBCJ: TRMDBDataSet;
    Panel7: TPanel;
    CheckBox1: TCheckBox;
    CDS_CJ: TClientDataSet;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    ToolButton5: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure BegDateChange(Sender: TObject);
    procedure EndDateChange(Sender: TObject);
    procedure MLCodeNameKeyPress(Sender: TObject; var Key: Char);
    procedure MLConNoKeyPress(Sender: TObject; var Key: Char);
    procedure MLOrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure KHNameHZChange(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitCon();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitSub();
    procedure SetStatus();
    function SaveData(): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, canshu3: string;
    fFlileFlag: string;
    canshuDY: string;
    { Public declarations }
  end;

var
  frmProductOrderNewList_New: TfrmProductOrderNewList_New;

implementation

uses
  U_DataLink, U_RTFun, U_Fun;
  //U_OrderWJGInPut;      //外加工    U_OrderWJGInPut
{$R *.dfm}

procedure TfrmProductOrderNewList_New.InitCon();
var
  fwhere, Pwhere: string;
begin
  try
    ADOQueryCon.DisableControls;
    with ADOQueryCon do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.add('select A.* ');
      sql.Add(',ZQty=(select Sum(B.Qty) from ML_ConSub B where B.MLConID=A.MLConID)');
      sql.Add(',ZMoney=(select Sum(B.ConMoney) from ML_ConSub B where B.MLConID=A.MLConID)');
      sql.Add(',KHNameJC=(select Top 1 B.KHNameJC from KH_Main B where B.KHName=A.KHName)');
      sql.Add(',SYRNameJC=(select Top 1 B.KHNameJC from KH_Main B where B.KHName=A.SYRName)');
      sql.Add(',KHNo=(select Top 1 B.KHNo from KH_Main B where B.KHName=A.KHName)');

      sql.Add(' from ML_ConMain A  where A.Chkstatus<>''作废''');

      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and  exists(select  X.MLConID from ML_ConSub X where X.MLConID=A.MLConID and ISPD=0)');
          end;
        1:
          begin
            sql.Add(' and not  exists(select  X.MLConID from ML_ConSub X where X.MLConID=A.MLConID and ISPD=0)');
            sql.Add(' and OrdDate>' + quotedstr(FormatDateTime('yyyy-MM-dd', ConBegDate.DateTime)));
            sql.Add(' and OrdDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', Conenddate.DateTime + 1)));
          end;
        2:
          begin

            sql.Add(' and OrdDate>' + quotedstr(FormatDateTime('yyyy-MM-dd', ConBegDate.DateTime)));
            sql.Add(' and OrdDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', Conenddate.DateTime + 1)));
          end;
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryCon, CDS_Con);
    SInitCDSData20(ADOQueryCon, CDS_Con);

  finally
    ADOQueryCon.EnableControls;
  end;
end;

procedure TfrmProductOrderNewList_New.SetStatus();
begin
  tchk.Visible := false;
  tnochk.Visible := false;
  tbedit.Visible := false;
  tbdel.Visible := false;
  TBPrint.Visible := false;
  v1Ssel.Visible := false;
  if ((Trim(canshu1) = '高权限') and (trim(canshu2) = '')) then
  begin
    tbedit.Visible := true;
    tbdel.Visible := true;
    tchk.Visible := true;
    TBPrint.Visible := true;
  end
  else if (Trim(canshu1) = '高权限') and (trim(canshu2) = '查询') then
  begin
    tbadd.Visible := false;
  end
  else
  begin
    tbedit.Visible := true;
    tbdel.Visible := true;
    TBPrint.Visible := true;
  end;
  if Trim(canshu1) = '高权限' then
  begin
    v1Ssel.Visible := true;
  end;
end;

procedure TfrmProductOrderNewList_New.InitSub();
begin
  if Order_Main.IsEmpty then
    exit;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select * from ML_OrderSubidNew ');
    sql.Add('where NewMLID =' + quotedstr((Order_Main.fieldbyname('NewMLID').AsString)));
    open;
  end;
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select * from ML_GX ');
    sql.Add('where NewMLID =' + quotedstr((Order_Main.fieldbyname('NewMLID').AsString)));
    open;
  end;
  SCreateCDS20(ADOQuery1, Ord_GX);
  SInitCDSData20(ADOQuery1, Ord_GX);
end;

procedure TfrmProductOrderNewList_New.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList_New := nil;
end;

procedure TfrmProductOrderNewList_New.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmProductOrderNewList_New.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
  canshu1 := trim(DParameters1);
  canshu2 := trim(DdataBase);
end;

procedure TfrmProductOrderNewList_New.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('生产指示单新YX', Tv1, '生产指示单管理');
end;

procedure TfrmProductOrderNewList_New.InitGrid();
var
  fwhere, Pwhere: string;
begin
  ToolBar1.SetFocus;
  Pwhere := SGetFilters(Panel1, 1, 2);
//  if canshu1 <> '高权限' then
//  begin
//    if Pwhere <> '' then
//      Pwhere := Pwhere + ' and filler=''' + trim(DName) + ''''
//    else
//      Pwhere := '  filler=''' + trim(DName) + '''';
//  end;

  fwhere := ' where OrdDate>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
  fwhere := fwhere + ' and OrdDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' and ' + trim(Pwhere);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('SELECT AA.* FROM(');
      sql.add('select A.*,KM.KHNameJC,KHNameHZ=isnull(A.KHNO,'''')+isnull(KM.KHName,'''')+isnull(KM.KHNameJC,'''')');
      sql.Add('+dbo.getpinyin(isnull(KM.KHName,''''))+dbo.getpinyin(isnull(KM.KHNameJC,''''))');
      SQL.Add(',ZQty=(select Sum(B.Qty) from ML_OrderSubidNew B where B.NewMLID=A.NewMLID) ');

      sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=A.ConMainId)');
      sql.Add(',B.NewSubid,B.MLColor,B.MLColorEng,B.kuanhao,B.MLColorNo,B.MLColorNoKH,B.PS,B.Qty,B.MLPrice,B.FeeName,B.FeeMoney   ');
      sql.Add(' from ML_OrderMainNew A ');
      sql.Add('  INNER JOIN ML_OrderSubidNew B on A.NewMLID=B.NewMLID');
      sql.Add(' left join KH_Main KM on KM.KHNO=A.KHNO ');
      sql.Add(')AA');
      sql.Add(fwhere);
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderNewList_New.InitForm();
begin
  Panel3.Visible := False;
  ReadCxGrid('生产指示单新YX', Tv1, '生产指示单管理');

  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 7;
  ConEndDate.DateTime := EndDate.DateTime;
  ConBegDate.DateTime := EndDate.DateTime - 7;
  if trim(DdataBase) = '查询' then
  begin
    TBadd.Visible := false;
    tbedit.Visible := false;
    ToolButton2.Visible := false;
    tbdel.Visible := false;
  end;
  //SetStatus();
  InitGrid();
  InitCon();
end;

procedure TfrmProductOrderNewList_New.TBDelClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Main.IsEmpty then
    Exit;

  if (UpperCase(Trim(Order_Main.fieldbyname('Filler').AsString)) <> UpperCase(Trim(DName))) and (Trim(canshu1) <> '高权限') then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JTJY_New where NewMLID=' + QuotedStr(Trim(Order_Main.fieldbyname('NewMLID').AsString)));
    Open;
  end;
  if ADOQueryTemp.RecordCount >= 1 then
  begin
    Application.MessageBox('已检验不能删除!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
  InitCon;
end;

function TfrmProductOrderNewList_New.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update ML_ConSub Set ISPD=0 where CSID in');
      sql.Add('(select ConSubID from ML_OrderSubidNew A where NewMLID=''' + Trim(Order_Main.fieldbyname('NewMLID').AsString) + ''')');
      sql.Add('delete ML_OrderMainNew where NewMLID=''' + Trim(Order_Main.fieldbyname('NewMLID').AsString) + '''');
      sql.Add('delete ML_OrderSubidNew where NewMLID=''' + Trim(Order_Main.fieldbyname('NewMLID').AsString) + '''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmProductOrderNewList_New.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '面料染色计划单');
end;

procedure TfrmProductOrderNewList_New.TBPrintClick(Sender: TObject);
begin
  //Panel4.Left:=(self.Width - Panel4.Width) div 2;
  //Panel4.Visible:=true;
  if CheckBox1.Checked = False then
  begin
    canshuDY := '正常';
  end
  else
  begin
    canshuDY := '层级';
  end;

  Button1.Click;
end;

procedure TfrmProductOrderNewList_New.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  InitCon();
end;

procedure TfrmProductOrderNewList_New.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmProductOrderNewList_New.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if ToolButton1.Visible = False then
    Exit;
  TBPrint.Click;
end;

procedure TfrmProductOrderNewList_New.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderNewList_New.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmProductOrderNewList_New.N1Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, false);
end;

procedure TfrmProductOrderNewList_New.N2Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, True);
end;

procedure TfrmProductOrderNewList_New.Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND, $F012, 0);
end;

procedure TfrmProductOrderNewList_New.Button1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('Ssel', True, []) = False then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('select A.*,B.*');
    sql.add(',KHNameJC1=(select KHNameJC  from KH_Main X where X.KHNo= A.KHNo and X.KHFlag=''KH'')');
    sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=A.ConMainId)');
    sql.add(',EWM=cast('''' as varchar(1000))  ');

    sql.Add(',TP=(select top 1 ImageFile from Image_Info X');
    sql.Add(' inner join Image_File Y on X.IMID=Y.IMID');
    sql.Add(' and isnull(X.IMNo,'''') in (''内容'',''生产'') and X.Valid=''Y'' ');
    sql.Add(' where X.WBID =B.TPID)');

    sql.Add('from ML_OrderMainNew A inner Join ML_OrderSubidNew B on A.NewMLID=B.NewMLID ');
    sql.Add('where A.NewMLID=' + quotedstr(Trim(Order_Main.fieldbyname('NewMLID').AsString)));
//    ShowMessage(sql.text);
    Open;
  end;
//  SCreateCDS20(ADOQueryPrint, Order_Print);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select *');
    sql.add(',KHNameJC1=(select KHNameJC  from KH_Main X where X.KHNo= B.KHNo and X.KHFlag=''KH'')');
    sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=B.ConMainId)');
    sql.add(',EWM=cast('''' as varchar(1000))  ');
    sql.Add(' from ML_OrderZuHe A inner join ML_OrderMainNew B on A.NewMLID=B.NewMLID   ');
    sql.Add(' where A.NewMLID=''' + Trim(Order_Main.fieldbyname('NewMLID').AsString) + '''');
    sql.Add(' order by A.XHNo');
    Open;
  end;
  SCreateCDS20(ADOQuery1, CDS_CJ);
  SInitCDSData20(ADOQuery1, CDS_CJ);
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('NewMLID').AsString);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp' + inttostr(i) + '.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;
  if Trim(canshuDY) = '正常' then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\生产指示单通用.rmf';
  end
  else if Trim(canshuDY) = '小' then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\生产指示单小.rmf';
  end
  else if Trim(canshuDY) = '层级' then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\生产指示单层级.rmf';
  end;

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
end;

procedure TfrmProductOrderNewList_New.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmProductOrderNewList_New.TchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Order_Main.Locate('Ssel', True, []) = false then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean = true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  ML_OrderMainNew SET status=''已审核'',chker=''' + trim(DName) + ''',chkTime=getdate() ');
            sql.Add('where NewMLID=' + quotedstr(trim(Order_Main.fieldbyname('NewMLID').AsString)));
            execsql;
          end;
          Order_Main.Delete;
        end
        else
          next;
      end;
    end;
    application.MessageBox('订单审核成功！', '提示信息');
    //TBRafresh.Click;
  except
    application.MessageBox('订单审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderNewList_New.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Order_Main.Locate('Ssel', True, []) = false then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean = true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update ML_OrderMainNew SET status=''未审核'',chker='''',chkTime=null ');
            sql.Add('where NewMLID=' + quotedstr(trim(Order_Main.fieldbyname('NewMLID').AsString)));
            execsql;
          end;
          Order_Main.Delete;
        end
        else
          next;
      end;
    end;
    application.MessageBox('订单撤销审核成功！', '提示信息');
    //TBRafresh.Click;
  except
    application.MessageBox('订单撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderNewList_New.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitSub();
end;

procedure TfrmProductOrderNewList_New.BegDateChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderNewList_New.EndDateChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderNewList_New.MLCodeNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmProductOrderNewList_New.MLConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmProductOrderNewList_New.MLOrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmProductOrderNewList_New.cxTabControl1Change(Sender: TObject);
begin
  InitCon();
end;

function TfrmProductOrderNewList_New.SaveData(): Boolean;
var
  maxno, MXmaxno, FConMainId: string;
  i: Integer;
begin
  if GetLSNo(ADOQueryCmd, maxno, 'ML', 'ML_OrderMainNew', 3, 1) = False then
  begin
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('取最大号失败!', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from ML_OrderMainNew where NewMLID=''' + Trim(maxno) + '''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    Append;
    FieldByName('NewMLID').Value := Trim(maxno);
    FieldByName('MLConNo').Value := CDS_HTSelInfo.fieldbyname('ConNo').Value;
    FieldByName('ConMainId').Value := CDS_HTSelInfo.fieldbyname('MLConId').Value;
    FieldByName('KHNO').Value := CDS_HTSelInfo.fieldbyname('KHNO').Value;
    FieldByName('KHName').Value := CDS_HTSelInfo.fieldbyname('KHName').Value;
    FieldByName('MLCodeName').Value := CDS_HTSelInfo.fieldbyname('ConCodeName').Value;
    FieldByName('MLCodeNo').Value := CDS_HTSelInfo.fieldbyname('ConCode').Value;
    FieldByName('YWConNo').Value := CDS_HTSelInfo.fieldbyname('YWConNo').Value;
    FieldByName('BZType').Value := CDS_HTSelInfo.fieldbyname('BZType').Value;
    FieldByName('Status').Value := '未审核';
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('OrdDate').Value := SGetServerDate(ADOQueryTemp);
    Post;
  end;
  FConMainId := Trim(CDS_HTSelInfo.fieldbyname('MLConId').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update ML_OrderMainNew Set ConXH=isnull((select max(ConXH) from ML_OrderMainNew where ConMainId=''' + Trim(FConMainId) + '''),0)+1');
    sql.Add(' where NewMLID=''' + Trim(maxno) + '''');
    ExecSQL;
  end;
  //更新标签
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update ML_OrderMainNew Set CPBQName=(select Top 1 CPBQName from ML_OrderMainNew A where A.KHNO=ML_OrderMainNew.KHNO ');
    sql.Add(' and isnull(CPBQName,'''')<>'''' order by Filltime Desc)');
    sql.Add(',CPBaoBQName=(select Top 1 CPBaoBQName from ML_OrderMainNew A where A.KHNO=ML_OrderMainNew.KHNO ');
    sql.Add(' and isnull(CPBQName,'''')<>'''' order by Filltime Desc)');
    sql.Add(' where NewMLID=''' + Trim(maxno) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update ML_OrderMainNew Set MLOrderNo=RTrim(YWConNo)+''-''+Cast(ConXH as varchar(5))');
    sql.Add(' where NewMLID=''' + Trim(maxno) + '''');
    ExecSQL;
  end;
  ///保存子表
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.MLConId,B.CSID,A.KHNO,A.KHName,B.ConCode,B.ConCodeName,A.YWConNo,A.ConNo ');
    sql.Add(',B.ConColor,B.ConColNo,B.ConHX,B.QtyUnit,B.Qty,B.ConColorEng,B.ConKHColNo,B.ConPrice');
    sql.Add(',B.FeeName,B.FeeMoney,B.KuanHao');
    sql.Add(' from ML_ConMain A inner join ML_ConSub B on A.MLConID=B.MLConID ');
    sql.Add(' where exists(select * from TbForBatch X where ');
    sql.Add(' X.ConSubId=B.CSID and X.MainType=''生产排单'' and X.DName=' + QUOTEDSTR(Trim(DName)) + ')');
    sql.Add(' and isnull(B.ConCode,'''')=''' + Trim(CDS_HTSelInfo.fieldbyname('ConCode').AsString) + '''');
    sql.Add(' and isnull(B.ConCodeName,'''')=''' + Trim(CDS_HTSelInfo.fieldbyname('ConCodeName').AsString) + '''');
    sql.Add(' and A.MLConId=''' + Trim(FConMainId) + ''' order by B.XHNO');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_HTSelInfo_MX);
  SInitCDSData20(ADOQueryTemp, CDS_HTSelInfo_MX);
  i := 0;
  with CDS_HTSelInfo_MX do
  begin
    First;
    while not Eof do
    begin
      i := i + 1;
      if GetLSNo(ADOQueryCmd, MXmaxno, 'MS', 'ML_OrderSubidNew', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('取子明细流水号失败！', '提示', 0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from ML_OrderSubidNew where NewMLID=''' + Trim(maxno) + '''');
        sql.Add(' and NewSubid=''' + Trim(MXmaxno) + '''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('NewMLID').Value := Trim(maxno);
        FieldByName('NewSubid').Value := Trim(MXmaxno);
        FieldByName('ConSubId').Value := CDS_HTSelInfo_MX.FieldByName('CSID').Value;
        FieldByName('MLColor').Value := CDS_HTSelInfo_MX.FieldByName('ConColor').Value;
        FieldByName('MLColorEng').Value := CDS_HTSelInfo_MX.FieldByName('ConColorEng').Value;
        FieldByName('MLColorNo').Value := CDS_HTSelInfo_MX.FieldByName('ConColNo').Value;
        FieldByName('MLColorNoKH').Value := CDS_HTSelInfo_MX.FieldByName('ConKHColNo').Value;
        FieldByName('MLHX').Value := CDS_HTSelInfo_MX.FieldByName('ConHX').Value;
        FieldByName('QtyUnit').Value := CDS_HTSelInfo_MX.FieldByName('QtyUnit').Value;
        fieldbyname('Qty').Value := CDS_HTSelInfo_MX.FieldByName('Qty').Value;
        fieldbyname('MLPrice').Value := CDS_HTSelInfo_MX.FieldByName('ConPrice').Value;
        fieldbyname('FeeName').Value := CDS_HTSelInfo_MX.FieldByName('FeeName').Value;
        fieldbyname('FeeMoney').Value := CDS_HTSelInfo_MX.FieldByName('FeeMoney').Value;
        fieldbyname('KuanHao').Value := CDS_HTSelInfo_MX.FieldByName('KuanHao').Value;
        fieldbyname('XHNO').Value := IntToStr(i);
        Post;
      end;
      Next;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update ML_ConSub set ISPD=( case when (select  COUNT(1)  from ML_OrderSubidNew X  where X.ConSubId=CSID)>0 then 1 else 0 end)');
    sql.Add('where exists(select X.MLConID from ML_ConMain X where X.MLConID=ML_ConSub.MLConID and X.MLConId=' + quotedstr(FConMainId) + ') ');
    ExecSQL;
  end;
  Result := True;
end;

procedure TfrmProductOrderNewList_New.KHNameHZChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmProductOrderNewList_New.N3Click(Sender: TObject);
begin
  Cxgrid1.Align := alLeft;
  Cxgrid1.Visible := False;
  Panel3.Align := alClient;
  Panel3.Visible := True;
  Panel3.Refresh;
end;

procedure TfrmProductOrderNewList_New.N4Click(Sender: TObject);
begin
  Panel3.Visible := False;
  Cxgrid1.Align := alClient;
  Cxgrid1.Visible := True;
end;

procedure TfrmProductOrderNewList_New.ToolButton3Click(Sender: TObject);
begin
  Cxgrid1.Align := alLeft;
  Cxgrid1.Visible := False;
  Panel3.Align := alClient;
  Panel3.Visible := True;
  Panel3.Refresh;
end;

procedure TfrmProductOrderNewList_New.ToolButton4Click(Sender: TObject);
begin
  canshuDY := '小';
  Button1.Click;
end;

procedure TfrmProductOrderNewList_New.ToolButton5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

procedure TfrmProductOrderNewList_New.Tv1DblClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

end.

