unit U_ProductOrderLYList;

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, cxDropDownEdit, cxCheckBox, RM_e_Graphic, RM_e_Jpeg,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, Math;

type
  TfrmProductOrderList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNoM: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomerNoName: TEdit;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    v1MPRTKZ: TcxGridDBColumn;
    Label10: TLabel;
    MPRTSpec: TEdit;
    Label11: TLabel;
    MPRTCode: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    Label13: TLabel;
    KHConNo: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    JGType: TEdit;
    Label2: TLabel;
    v1Column8: TcxGridDBColumn;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    v1Column4: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    RMDBMX: TRMDBDataSet;
    CDSPRTMX: TClientDataSet;
    CDSPRTHZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    RM2: TRMGridReport;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    N1: TMenuItem;
    v1Column16: TcxGridDBColumn;
    RMJPEGExport1: TRMJPEGExport;
    RMDB_Main2: TRMDBDataSet;
    CDS_Main2: TClientDataSet;
    CDS_Main3: TClientDataSet;
    RMDB_Main3: TRMDBDataSet;
    ADOQuery1: TADOQuery;
    v1Column17: TcxGridDBColumn;
    Label6: TLabel;
    YWY: TEdit;
    ORDER_SUB: TClientDataSet;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    Label25: TLabel;
    PRTHXHZ: TEdit;
    RM3: TRMGridReport;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    yin1: TMenuItem;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    ComboBox1: TComboBox;
    N5: TMenuItem;
    N6: TMenuItem;
    Tv1Column5: TcxGridDBColumn;
    ADOQuery2: TADOQuery;
    VCWTJL: TcxGridDBColumn;
    Label1: TLabel;
    FILLER: TEdit;
    Tv1Column6: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure TBCKClick(Sender: TObject);
    procedure btnCopyClick(Sender: TObject);
    procedure OrderNoMKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure TWCClick(Sender: TObject);
    procedure TNowcClick(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TRZClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TBTJClick(Sender: TObject);
    procedure TBTJCXClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CYBQPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Image3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure yin1Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Image4Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitGridFH();
    procedure InitSub();
    procedure inittj();
    procedure SetStatus();
    procedure InitImage(fwbid: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    FNOTE: string;
    { Public declarations }
  end;

//var   TV2
//  frmProductOrderList: TfrmProductOrderList;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun, U_FjList_RZ, U_FjList10, U_SysLogHelp,
  U_ZDYHelp, U_SketchManage, U_SCSJJDList, U_SketchManageList,
  U_SketchManageListSel;

{$R *.dfm}
procedure TfrmProductOrderList.InitImage(fwbid: string);
begin
  ADOQueryImage.close;
  if fwbid = '' then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(fwbid)));
    open;
  end;
end;

procedure TfrmProductOrderList.SetStatus();
begin
  btnCopy.Visible := false;
  tbadd.Visible := false;
  TBEdit.Visible := False;
  TBTJ.Visible := False;
  TBTJCX.Visible := False;
  tchk.Visible := false;
  tnochk.Visible := false;
  TWC.Visible := False;
  TNowc.Visible := False;
  TBDel.Visible := False;
  if Trim(canshu1) = '查询' then
    exit;
  case cxTabControl1.TabIndex of
    0:
      begin
        tbadd.Visible := True;
        TBEdit.Visible := True;
        TBDel.Visible := True;
        TBTJ.Visible := True;
        btnCopy.Visible := True;
      end;
    1:
      begin
        TBTJCX.Visible := True;
        btnCopy.Visible := True;
        TBEdit.Visible := True;
        if Trim(canshu1) = '高权限' then
        begin
          tchk.Visible := True;
        end;
      end;
    2:
      begin
        TWC.Visible := True;
        btnCopy.Visible := True;
        TBEdit.Visible := True;
        if Trim(canshu1) = '高权限' then
        begin
          tnochk.Visible := True;
        end;
      end;
    3:
      begin
        TNowc.Visible := True;
        btnCopy.Visible := True;
      end;
  end;

end;

procedure TfrmProductOrderList.InitTJ();
begin

  if Order_Main.IsEmpty then
    exit;
  ADOQuery1.DisableControls;
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('EXEC P_View_MGTJ2   ');

    sql.Add(' @mainid =' + quotedstr((Order_Main.fieldbyname('mainid').AsString)));

    open;
  end;
  ADOQuery1.EnableControls;

  if ADOQuery1.IsEmpty = FALSE then
  begin
    Label26.Caption := '设计稿数：' + ADOQuery1.fieldbyname('SS1').AsString;
    Label27.Caption := '打样中：' + ADOQuery1.fieldbyname('SS3').AsString;
    Label28.Caption := '描稿中：' + ADOQuery1.fieldbyname('SS2').AsString;
    Label29.Caption := '大片完成：' + ADOQuery1.fieldbyname('SS4').AsString;
  end;
end;

procedure TfrmProductOrderList.InitSub();
begin
  ADOQuerySub.Close;
  if Order_Main.IsEmpty then
    exit;
  ADOQuerySub.DisableControls;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select *  ');
    sql.Add(' from JYOrder_sub A');
    sql.Add('where mainid =' + quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
  SCreateCDS20(ADOQuerySub, ORDER_SUB);
  SInitCDSData20(ADOQuerySub, ORDER_SUB);
  if Order_Main.fieldbyname('HYTT').AsString <> '' then
  begin
    V2Column9.Caption := Order_Main.fieldbyname('HYTT').AsString;
  end
  else
  begin
    V2Column9.Caption := '海运';
  end;
  if Order_Main.fieldbyname('KYTT').AsString <> '' then
  begin
    V2Column10.Caption := Order_Main.fieldbyname('kYTT').AsString;
  end
  else
  begin
    V2Column10.Caption := '空运';
  end;

  ADOQuerySub.EnableControls;
end;

procedure TfrmProductOrderList.FormDestroy(Sender: TObject);
begin
//  frmProductOrderList:=nil;
end;

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

procedure TfrmProductOrderList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);

end;

procedure TfrmProductOrderList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  WriteCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理6');
  Close;

end;

procedure TfrmProductOrderList.InitGrid();
var
  STRwhere, Pwhere: string;
begin
  STRwhere := '';
  //if Trim(canshu1) = '录入' then
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      STRwhere := STRwhere + ' and ' + trim(Pwhere);
  end;
  if CheckBox1.Checked = True then
    STRwhere := STRwhere + ' and (Filler=' + quotedstr(Trim(DName)) + ' or YWY=' + quotedstr(Trim(DName)) + ' or lidan=' + quotedstr(Trim(DName)) + ')';

  if CheckBox2.Checked = True then
    STRwhere := STRwhere + ' and  SCXDH=''首次下大货'' ';
  if cxTabControl1.TabIndex < 4 then
  begin
    if cxTabControl1.TabIndex = 0 then
    begin
      STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''0'' ';
    end
    else if cxTabControl1.TabIndex = 1 then
    begin
      STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''99'' ';
    end
    else if cxTabControl1.TabIndex = 2 then
    begin
      STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''1'' ';
    end
    else if cxTabControl1.TabIndex = 3 then
    begin
      STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''2'' ';
    end;

    //strwhere := strwhere + ' and  isnull(A.status,''0'')=''' + inttostr(cxTabControl1.TabIndex) + '''';
  end;
  if OrderNoM.Text <> '' then
  begin
    STRwhere := STRwhere + ' and orderno like ''' + '%' + Trim(OrderNoM.Text) + '%' + '''';
//        Parameters.ParamByName('WSql').Value := ' and orderno like ''' + '%' + Trim(OrderNoM.Text) + '%' + '''';
  end;
  if PRTHXHZ.Text <> '' then
  begin
    STRwhere := STRwhere + ' and cast((select B.PRTHX+'';'' from JYOrder_Sub B where B.MainId=A.MainId for xml path('''')) as varchar(200)) like ''' + '%' + Trim(PRTHXHZ.Text) + '%' + '''';
//        Parameters.ParamByName('WSql').Value := ' and orderno like ''' + '%' + Trim(OrderNoM.Text) + '%' + '''';
  end;
  try
    ADOQueryMain.DisableControls;
    if ComboBox1.Text = '制单日期' then
    begin
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order  ');
        sql.add('   @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
        sql.add(' , @enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
        sql.add(' , @WSql=' + quotedstr(Trim(STRwhere)));
//        ShowMessage(SQL.Text);
        Open;
      end;
    end
    else
    begin
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order2  ');
        sql.add('   @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
        sql.add(' , @enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
        sql.add(' , @WSql=' + quotedstr(Trim(STRwhere)));
        Open;
      end;
    end;

    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderList.InitGridFH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value := ' and FillTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''' + ' and FillTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderList.InitForm();
begin
  ReadCxGrid('指示单列表FF1', Tv1, '生产指示单管理');

  ReadCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理6');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 180;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);

  SetStatus();
  InitGrid();
end;

procedure TfrmProductOrderList.TBFindClick(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 TfrmProductOrderList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if canshu1 = '录入' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select FILLER from JYOrder_Main A  ');
      SQL.Add('where MAINID=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + ''' ');
      SQL.Add('and   filler in ( select username from SY_User where UType=(select UType from SY_User where UserName=' + quotedstr(Trim(dname)));
      SQL.Add('))');
      Open;
    end;

    if ADOQueryTemp.ISEMPTY = True then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;

  { with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CK_SXPB_CR A  ');
    SQL.Add('where ORDMainIdCK='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
    SQL.Add('and  ckName<>''坯布'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生数据不能修改!','提示',0);
    Exit;
  end; }
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      if cxTabControl1.TabIndex = 2 then
        PXGQX := '已审核';
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo := Trim(Self.Order_Main.fieldbyname('OrderNoM').AsString);
      canshu1 := self.canshu1;
//      conNo.Enabled := false;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
//  self.TBRafresh.Click;
end;

procedure TfrmProductOrderList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    SQL.Add('select * from CK_SXPB_CR A  ');
//    SQL.Add('where ORDMainIdCK='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
//    SQL.Add('or ORDMainIdRK='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty=False then
//  begin
//    Application.MessageBox('已产生数据不能删除!','提示',0);
//    Exit;
//  end;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    SQL.Add('select * from CK_PBMX_CR A  ');
//    SQL.Add('where ToMainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
//    SQL.Add('and CKName=''坯布'' ');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty=False then
//  begin
//    Application.MessageBox('已产生数据不能删除!','提示',0);
//    Exit;
//  end;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from YF_Money_CR where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty=False then
//  begin
//    Application.MessageBox('已产生数据不能删除!','提示',0);
//    Exit;
//  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已回仓不能删除数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmProductOrderList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete JYOrder_Sub where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      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('指示单号：' + trim(Order_Main.FieldByName('OrderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmProductOrderList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '生产指示单列表');
end;

procedure TfrmProductOrderList.TBPrintClick(Sender: TObject);
begin

  if Order_Main.IsEmpty then
    Exit;
//  if Trim(Order_Main.fieldbyname('Status').AsString) = '0' then
//    Exit;
  Panel4.Left := (self.Width - Panel4.Width) div 2;
  Panel4.Visible := true;
end;

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

procedure TfrmProductOrderList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 0;

      FMainId := '';
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderList.OrderNoMChange(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 TfrmProductOrderList.FormShow(Sender: TObject);
begin
  fFlileFlag := UserDataFlag + 'HX';
  cxTabControl1.TabIndex := 2;
  InitForm();
end;

procedure TfrmProductOrderList.TBTPClick(Sender: TObject);
var
  FQty, FQty1, FMxQty, FPQty, FMxQtyS, FPQtyS: string;
begin
end;

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

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

procedure TfrmProductOrderList.Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmProductOrderList.v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate10(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate10(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmProductOrderList.N1Click(Sender: TObject);
begin
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请选择数据!', '提示', 0);
    Exit;
  end;

  try
    while Order_Main.Locate('SSel', True, []) do
    begin

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  JYOrder_Main SET ZOrderNo=null ');
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
        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(Order_Main.FieldByName('orderNO').AsString) + '合并计划单号：' + trim(Order_Main.FieldByName('ZorderNO').AsString)));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;

      with Order_Main do
      begin
        Edit;
        FieldByName('SSel').Value := False;
        FieldByName('ZOrderNo').Value := '';
        post;
      end;
    end;

    application.MessageBox('取消合并计划单成功！', '提示信息');

  except
    application.MessageBox('取消合并计划单失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderList.TBCKClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      cxGrid1.Enabled := False;
      ToolButton3.Visible := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderList.btnCopyClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 1;
      CopyInt := 99;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderList.OrderNoMKeyPress(Sender: TObject; var Key: Char);
var
  STRwhere, Pwhere: string;
begin
  STRwhere := '';
  //if Trim(canshu1) = '录入' then
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      STRwhere := STRwhere + ' and ' + trim(Pwhere);
  end;
  if Key = #13 then
  begin
    initgrid();
//    try
//      ADOQueryMain.DisableControls;
//      with ADOQueryMain do
//      begin
//        Filtered := False;
//        Close;
//        sql.Clear;
//        sql.add('exec P_View_Order :begdate,:enddate,:WSql');
//        if Trim(canshu1) <> '高权限' then
//        begin
//          Parameters.ParamByName('WSql').Value := ' and Filler=''' + Trim(DName) + '''';
//        end;
//        begin
//          Parameters.ParamByName('WSql').Value := ' and orderno like ''' + '%' + Trim(OrderNoM.Text) + '%' + '''';
//        end;
//
//        Parameters.ParamByName('begdate').Value := '1899-01-01';
//        Parameters.ParamByName('enddate').Value := '2050-01-01';
//        ExecSQL;
//        Open;
//      end;
//      SCreateCDS20(ADOQueryMain, Order_Main);
//      SInitCDSData20(ADOQueryMain, Order_Main);
//    finally
//      ADOQueryMain.EnableControls;
//    end;
  end;
end;

procedure TfrmProductOrderList.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order :begdate,:enddate,:WSql');
        if Trim(canshu1) <> '高权限' then
        begin
          Parameters.ParamByName('WSql').Value := ' and A.Filler=''' + Trim(DName) + '''';
        end;
        begin
          Parameters.ParamByName('WSql').Value := ' and conno like ''' + '%' + Trim(conno.Text) + '%' + '''';
        end;
        Parameters.ParamByName('begdate').Value := '1899-01-01';
        Parameters.ParamByName('enddate').Value := '2050-01-01';
        ExecSQL;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
//  V2Column9.Caption := '1234';
//  inittj();
end;

procedure TfrmProductOrderList.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmFjList10 := TfrmFjList10.Create(Application);
    with frmFjList10 do
    begin
//      PState:=1;
      if Trim(canshu1) <> '高权限' then
        cxButton1.Visible := false;
      fkeyNO := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      fType := '指示单';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList10.Free;
  end;
end;

procedure TfrmProductOrderList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmProductOrderList.TchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
  begin
    Application.MessageBox('只能在待审核状态下进行审核操作!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Main SET status=''1'',chker=''' + trim(DName) + ''',Chktime=getdate() ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      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('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

  except
    application.MessageBox('订单审核失败！', '提示信息', 0);
  end;
  TBRafresh.Click;
end;

procedure TfrmProductOrderList.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 2 then
  begin
    Application.MessageBox('只能在已审核状态下进行审核操作!', '提示', 0);
    Exit;
  end;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Main SET status=''0'',CXSHSTATUS=''99'',chker=null,Chktime=null ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      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('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    TBRafresh.Click;
  except
    application.MessageBox('订单撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderList.TWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Main SET status=''2'' ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      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('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('订单完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderList.TNowcClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Main SET status=''1'' ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      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('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('订单撤销完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单撤销完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderList.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
//  initImage(ADOQuerySub.fieldbyname('HXFile').AsString);
end;

procedure TfrmProductOrderList.TRZClick(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmProductOrderList.Button1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if Order_Main.IsEmpty then
    Exit;

  if (trim(Order_Main.fieldbyname('status').AsString) = '1') or (trim(Order_Main.fieldbyname('status').AsString) = '2') then
    RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator]
  else
    RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator];

  LBName := RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTemp);
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Print_Order');
    sql.add('@mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,Zqty=(select sum(PRTOrderQty) from JYOrder_sub B where A.MAINID=B.MAINID ) ');
    SQL.Add(',Orderunit=(SELECT TOP 1 OrderUnit FROM JYOrder_sub B where A.MAINID=B.MAINID )');
    sql.add('from JYOrder_Main A where A.mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryTemp, CDS_Main2);
  SInitCDSData20(ADOQueryTemp, CDS_Main2);

//  with ADOQuery1 do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from JYOrder_Main');
//    sql.add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
//    Open;
//  end;
//
//  SCreateCDS20(ADOQuery1, CDS_Main3);
//  SInitCDSData20(ADOQuery1, CDS_Main3);
//  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['SYRName'] := Trim(SYRName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
  Panel4.Visible := false;
end;

procedure TfrmProductOrderList.Image2Click(Sender: TObject);
begin
  Panel4.Visible := false;
end;

procedure TfrmProductOrderList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  fPrintFile, LBName, SYRName: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if (trim(Order_Main.fieldbyname('status').AsString) = '1') or (trim(Order_Main.fieldbyname('status').AsString) = '2') then
    RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator]
  else
    RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator];
//  if Trim(Order_Main.fieldbyname('Status').AsString) = '0' then
//    Exit;
  if Trim(Order_Main.fieldbyname('JGType').AsString) = '' then
  begin
    Panel4.Visible := True;
    Exit;
  end;
  LBName := Trim(Order_Main.fieldbyname('JGType').AsString) + '生产指示单';
  if Trim(Order_Main.fieldbyname('JGType').AsString) = '数码印花' then
  begin
    LBName := '印花生产指示单';
  end;
  //LBName := RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTemp);
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Print_Order');
    sql.add('@mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,Zqty=(select sum(PRTOrderQty) from JYOrder_sub B where A.MAINID=B.MAINID ) ');
    SQL.Add(',Orderunit=(SELECT TOP 1 OrderUnit FROM JYOrder_sub B where A.MAINID=B.MAINID )');
    sql.add('from JYOrder_Main A where A.mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryTemp, CDS_Main2);
  SInitCDSData20(ADOQueryTemp, CDS_Main2);

//  with ADOQuery1 do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from JYOrder_Main');
//    sql.add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
//    Open;
//  end;
//
//  SCreateCDS20(ADOQuery1, CDS_Main3);
//  SInitCDSData20(ADOQuery1, CDS_Main3);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['SYRName'] := Trim(SYRName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
end;

procedure TfrmProductOrderList.TBTJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  if canshu1 <> '高权限' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;

  if Trim(Order_Main.fieldbyname('JGType').AsString) = '印花' then
  begin
    if ORDER_SUB.Locate('MGMainId', '', []) then
    begin
      Application.MessageBox('花型ID不能为空!', '提示', 0);
      Exit;
    end;
    if ORDER_SUB.Locate('MGMainId', NULL, []) then
    begin
      Application.MessageBox('花型ID不能为空!', '提示', 0);
      Exit;
    end;
  end;

  if canshu1 = '录入' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select FILLER from JYOrder_Main A  ');
      SQL.Add('where MAINID=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + ''' ');
      SQL.Add('and   filler in ( select username from SY_User where UType=(select UType from SY_User where UserName=' + quotedstr(Trim(dname)));
      SQL.Add('))');
      Open;
    end;

    if ADOQueryTemp.ISEMPTY = True then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;
  if cxTabControl1.TabIndex <> 0 then
  begin
    Application.MessageBox('只能在待提交状态下进行提交操作!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要提交数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Main SET status=''99'',TJPerson=''' + trim(DName) + ''',TJTime=getdate() ');
      sql.Add(' where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      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('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('订单提交成功！', '提示信息');

  except
    application.MessageBox('订单提交失败！', '提示信息', 0);
  end;
  TBRafresh.Click;
end;

procedure TfrmProductOrderList.TBTJCXClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if canshu1 <> '高权限' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;
  if canshu1 = '录入' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select FILLER from JYOrder_Main A  ');
      SQL.Add('where MAINID=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + ''' ');
      SQL.Add('and   filler in ( select username from SY_User where UType=(select UType from SY_User where UserName=' + quotedstr(Trim(dname)));
      SQL.Add('))');
      Open;
    end;

    if ADOQueryTemp.ISEMPTY = True then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;
  if cxTabControl1.TabIndex <> 1 then
  begin
    Application.MessageBox('只能在待审核状态下进行提交撤销操作!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销提交数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Main SET status=''0'',TJPerson=''' + trim(DName) + ''',TJTime=getdate() ');
      sql.Add(' where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add(' and isnull(status,''0'')=''99'' ');
      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('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('订单提交撤销成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单提交撤销失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
  str: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator];
  LBName := '颜色小标签';
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTemp);

  str := '1';

  if InputQuery('输入份数', '份数: ', str) then
  begin
    ORDER_SUB.DisableControls;
    with ORDER_SUB do
    begin
      First;
      while not Eof do
      begin
        if ORDER_SUB.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryPrint do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from');
            SQL.Add(' JYOrder_Main A inner join JYOrder_Sub B on  A.MainId=B.MainId ');
            sql.Add(' inner join CP_YDang C on C.CYNo=A.MPRTCode');
            sql.add(' WHERE SubId=' + quotedstr(trim(Order_sub.fieldbyname('subid').AsString)));
            Open;
          end;

          SCreateCDS20(ADOQueryPrint, CDS_Print);
          SInitCDSData20(ADOQueryPrint, CDS_Print);
          ExportFtErpFile(trim(LBName) + '.rmf', ADOQueryTemp);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(LBName) + '.rmf';
          if FileExists(fPrintFile) then
          begin
            RM2.LoadFromFile(fPrintFile);
            RMVariables['SYRName'] := Trim(SYRName);
            Rm2.DefaultCopies := Ceil(strtointdef(trim(str), 1) / 2);
            RM2.PrintReport;
          end
          else
          begin
            Order_sub.EnableControls;
            Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
            Exit;
          end;

        end;
        Next;
      end;

    end;
    Order_sub.EnableControls;
  end;
end;

procedure TfrmProductOrderList.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if Order_Main.IsEmpty then
    Exit;

  LBName := '总计划单';
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTemp);
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Print_ZOrder');
    sql.add('@ZOrderNo=' + quotedstr(trim(Order_Main.fieldbyname('ZOrderNo').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM3.LoadFromFile(fPrintFile);
    RMVariables['SYRName'] := Trim(SYRName);
    RM3.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
  Panel4.Visible := false;
end;

procedure TfrmProductOrderList.N2Click(Sender: TObject);
var
  maxno: string;
begin
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请选择数据!', '提示', 0);
    Exit;
  end;
  RadioGroup2.Items.Clear;
  with Order_Main do
  begin
    First;
    while not eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean then
      begin
        RadioGroup2.Items.Add(Trim(Order_Main.FieldByName('OrderNo').AsString));
      end;
      Next;
    end;
  end;

  Panel3.Visible := True;
end;

procedure TfrmProductOrderList.Button2Click(Sender: TObject);
var
  maxno: string;
begin
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请选择数据!', '提示', 0);
    Exit;
  end;
  maxno := RadioGroup2.Items.Strings[RadioGroup2.ItemIndex];

  try
    while Order_Main.Locate('SSel', True, []) do
    begin

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  JYOrder_Main SET ZOrderNo=' + quotedstr(trim(maxno)));
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
        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('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString) + '合并计划单号：' + trim(maxno))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;

      with Order_Main do
      begin
        Edit;
        FieldByName('SSel').Value := False;
        FieldByName('ZOrderNo').Value := maxno;
        post;
      end;
    end;

    application.MessageBox('合并计划单成功！', '提示信息');

  except
    application.MessageBox('合并计划单失败！', '提示信息', 0);
  end;
  Panel3.Visible := False;
end;

procedure TfrmProductOrderList.Image1Click(Sender: TObject);
begin
  Panel3.Visible := False;
end;

procedure TfrmProductOrderList.ToolButton2Click(Sender: TObject);
var
  fPrintFile, fImagePath2: string;
  i, j, k: integer;
  LBNAME: string;
begin

  if Order_Main.IsEmpty then
    Exit;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Print_Order');
    sql.add('@mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main');
    sql.add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryTemp, CDS_Main2);
  SInitCDSData20(ADOQueryTemp, CDS_Main2);
  LBNAME := Trim(Order_Main.fieldbyname('JGType').AsString) + '生产指示单';
  if Trim(Order_Main.fieldbyname('JGType').AsString) = '数码印花' then
  begin
    LBNAME := '印花生产指示单';
  end;
  ExportFtErpFile(Trim(LBNAME) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBNAME) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);

    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label0001.xls';
    if FileExists(fImagePath2) then
      DeleteFile(fImagePath2);
    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label.xls';
    RM1.PrepareReport;
    RM1.ExportTo(RMXLSExport1, fImagePath2);
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmProductOrderList.CYBQPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CYBQ';
      flagname := '船样标签名称';

      if ShowModal = 1 then
      begin
        CYBQ.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderList.ToolButton5Click(Sender: TObject);
var
  fPrintFile, fImagePath2: string;
  i, j, k: integer;
  str: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if ORDER_SUB.IsEmpty then
    Exit;
  if cybq.Text = '' then
  begin
    Application.MessageBox('没有选择船样标签', '提示', 0);
    Exit;
  end;
  str := '1';

  if InputQuery('输入份数', '份数: ', str) then
  begin
    ORDER_SUB.DisableControls;
    with ORDER_SUB do
    begin
      First;
      while not Eof do
      begin
        if ORDER_SUB.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryPrint do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from');
            SQL.Add(' JYOrder_Main A inner join JYOrder_Sub B on  A.MainId=B.MainId ');
            sql.Add(' inner join CP_YDang C on C.CYNo=A.MPRTCode');
            sql.add(' WHERE SubId=' + quotedstr(trim(Order_sub.fieldbyname('subid').AsString)));
            Open;
          end;

          SCreateCDS20(ADOQueryPrint, CDS_Print);
          SInitCDSData20(ADOQueryPrint, CDS_Print);
          ExportFtErpFile(trim(CYBQ.Text) + '.rmf', ADOQueryTemp);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(CYBQ.Text) + '.rmf';
          if FileExists(fPrintFile) then
          begin
            RM2.LoadFromFile(fPrintFile);

            Rm2.DefaultCopies := strtointdef(trim(str), 1);
            RM2.PrintReport;
          end
          else
          begin
            Order_sub.EnableControls;
            Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
            Exit;
          end;

        end;
        Next;
      end;

    end;
    Order_sub.EnableControls;
  end;

end;

procedure TfrmProductOrderList.ToolButton6Click(Sender: TObject);
begin
  try
    frmSketchManage := TfrmSketchManage.Create(Application);
    with frmSketchManage do
    begin
      PState := 0;
      FDDID := Trim(Self.Order_SUB.fieldbyname('SUBID').AsString);
      FMainId := '';
      canshu1 := '录入';
      if ShowModal = 1 then
      begin
        InitGrid();
//        Self.Order_Main.Locate('MGID', frmSketchManage.FMainId, []);
      end;
    end;
  finally
    frmSketchManage.Free;
  end;
end;

procedure TfrmProductOrderList.ToolButton7Click(Sender: TObject);
begin
  Panel6.Visible := True;
end;

procedure TfrmProductOrderList.Button3Click(Sender: TObject);
var
  maxno: string;
begin

  try
    ORDER_SUB.DisableControls;
    with ORDER_SUB do
    begin
      First;
      while not Eof do
      begin
        if ORDER_SUB.FieldByName('SSel').AsBoolean = True then
        begin
          if GetLSNo(ADOQueryCmd, maxno, 'MG', 'Sketch_main', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('生成流水号异常！', '提示', 0);
            exit;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            SQL.Add('insert into Sketch_main(MGID)');
            sql.Add('values(' + quotedstr(maxno) + ')');

            SQL.Add('UPDATE Sketch_main SET orderno=' + quotedstr(Trim(Order_Main.fieldbyname('orderno').AsString)));
            SQL.Add(',M_KHNAME=' + quotedstr(Trim(Order_Main.fieldbyname('CustomerNoName').AsString)));
            SQL.Add(',frommainid=' + quotedstr(Trim(Order_Main.fieldbyname('mainid').AsString)));
            SQL.Add(',M_DBSPEC=' + quotedstr(Trim(Order_Main.fieldbyname('MPRTCODENAME').AsString)));

            SQL.Add(',M_COLOR=' + quotedstr(Trim(ORDER_SUB.fieldbyname('PRTColor').AsString)));
            SQL.Add(',M_COLORNO=' + quotedstr(Trim(ORDER_SUB.fieldbyname('PRTColorNo').AsString)));
            SQL.Add(',M_KHHX=' + quotedstr(Trim(ORDER_SUB.fieldbyname('PRTHXKH').AsString)));
            SQL.Add(',M_HXCODE=' + quotedstr(Trim(ORDER_SUB.fieldbyname('PRTHx').AsString)));
            SQL.Add(',fromsubid=' + quotedstr(Trim(ORDER_SUB.fieldbyname('subid').AsString)));

            SQL.Add(',ywy=' + quotedstr(Trim(Order_Main.fieldbyname('YWY').AsString)));
            SQL.Add(',filler=' + QUOTEDSTR(Trim(DNAME)));
            SQL.Add(',m_type=' + QUOTEDSTR(M_TYPE.Text));
            SQL.Add(',M_HXTYPE=' + QUOTEDSTR(M_HXTYPE.Text));
            SQL.Add(',m_GONGYI=' + QUOTEDSTR(m_GONGYI.Text));
            SQL.Add(',m_note=' + QUOTEDSTR(m_note.Text));
            SQL.Add('WHERE MGID=' + QUOTEDSTR(maxno));

//            sql.Add('update  JYOrder_Sub SET  MGMainId=''' + trim(MMGMainId) + '''');
//            sql.Add('where mainid=' + quotedstr(trim(ADOQuery2.fieldbyname('mainid').AsString)));
//            sql.Add('and prthx=' + quotedstr(trim(ADOQuery2.fieldbyname('prthx').AsString)));
            sql.Add('update JYOrder_sub set MGMainId=' + quotedstr(Trim(maxno)));
            sql.Add('where mainid=' + quotedstr(trim(Order_Main.fieldbyname('mainid').AsString)));
            sql.Add('and prthx=' + quotedstr(trim(ORDER_SUB.fieldbyname('prthx').AsString)));
//            SQL.Add('inner join JYOrder_Main B  on A.ORDERNO=B.Orderno where B.Mainid=JYOrder_sub.Mainid and A.M_HXCODE=JYOrder_sub.PRTHX )');
//            SQL.Add('WHERE SUBID=' + quotedstr(Trim(ORDER_SUB.fieldbyname('subid').AsString)));
//            ShowMessage(sql.Text);
            EXECSQL;
          end;

        end;
        Next;
      end;

    end;
    Order_sub.EnableControls;
    application.MessageBox('生成描稿成功！', '提示信息');
    panel6.Visible := False;

  except
    application.MessageBox('生成描稿失败！', '提示信息', 0);
  end;
  TBRafresh.Click;

end;

procedure TfrmProductOrderList.Button4Click(Sender: TObject);
begin
  panel6.Visible := false;
end;

procedure TfrmProductOrderList.Image3Click(Sender: TObject);
begin
  panel6.Visible := false;
end;

procedure TfrmProductOrderList.Button5Click(Sender: TObject);
var
  fPrintFile, fImagePath2: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if Order_Main.IsEmpty then
    Exit;

  if (trim(Order_Main.fieldbyname('status').AsString) = '1') or (trim(Order_Main.fieldbyname('status').AsString) = '2') then
    RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator]
  else
    RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator];

  LBName := RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTemp);
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Print_Order');
    sql.add('@mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main');
    sql.add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryTemp, CDS_Main2);
  SInitCDSData20(ADOQueryTemp, CDS_Main2);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);

    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label0001.xls';
    if FileExists(fImagePath2) then
      DeleteFile(fImagePath2);
    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label.xls';
    RM1.PrepareReport;
    RM1.ExportTo(RMXLSExport1, fImagePath2);
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
  Panel4.Visible := false;
end;

procedure TfrmProductOrderList.N3Click(Sender: TObject);
begin
  SelOKNo(ORDER_SUB, True);
end;

procedure TfrmProductOrderList.N4Click(Sender: TObject);
begin
  SelOKNo(ORDER_SUB, False);
end;

procedure TfrmProductOrderList.ToolButton8Click(Sender: TObject);
begin
  try
    frmSCSJJDList := TfrmSCSJJDList.Create(Application);
    with frmSCSJJDList do
    begin

      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmSCSJJDList.Free;
  end;
end;

procedure TfrmProductOrderList.yin1Click(Sender: TObject);
begin
  if ORDER_SUB.IsEmpty = True then
  begin
    Exit;
  end;

  try
    frmSketchManageList := TfrmSketchManageList.Create(Application);
    with frmSketchManageList do
    begin

      caption := '印花描稿过滤';
      canshu1 := '查询';
      FCXMG := QuotedStr(Trim(Self.ORDER_SUB.fieldbyname('MGMainId').AsString));
      TBAdd.Visible := False;
      TBEdit.Visible := False;
      TBCopy.Visible := False;
      ToolButton4.Visible := False;
      TBDel.Visible := False;
      if ShowModal = 1 then
      begin
      end;
    end;
  finally
    frmSketchManageList.Free;
  end;
end;

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

procedure TfrmProductOrderList.Image4Click(Sender: TObject);
begin
  Panel8.Visible := False;
end;

procedure TfrmProductOrderList.ToolButton9Click(Sender: TObject);
begin
  if Order_Main.IsEmpty = True then
  begin
    Exit;
  end;
  Panel8.Visible := True;
  xgnote.Text := '';
  XGNote.Text := Trim(Order_Main.fieldbyname('lYNOTE').AsString);
  FNOTE := '留样';
end;

procedure TfrmProductOrderList.Button6Click(Sender: TObject);
begin
  if trim(XGNote.Text) = '' then
  begin
    Application.MessageBox('内容不能为空！', '提示', 0);
    Exit;
  end;
  try
    if fnote = '留样' then
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  JYOrder_MAIN SET LYNOTE=' + QUOTEDSTR(Trim(XGNOTE.Text)));

        sql.Add('where MAINID=' + quotedstr(trim(Order_Main.fieldbyname('MAINID').AsString)));

        execsql;
      end;
    end
    else
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  JYOrder_MAIN SET zyNOTE=' + QUOTEDSTR(Trim(XGNOTE.Text)));

        sql.Add('where MAINID=' + quotedstr(trim(Order_Main.fieldbyname('MAINID').AsString)));

        execsql;
      end;
    end;

    application.MessageBox('修改成功！', '提示信息');
    TBRafresh.Click;
    Panel8.Visible := False;
  except
    application.MessageBox('修改失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderList.ComboBox1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderList.N5Click(Sender: TObject);
var
  MCXMG: string;
begin
  if ORDER_SUB.IsEmpty = True then
  begin
    Exit;
  end;
  with ORDER_SUB do
  begin
    MCXMG := '';
    First;
    while not Eof do
    begin
      if MCXMG = '' then
        MCXMG := QuotedStr(Trim(Self.ORDER_SUB.fieldbyname('MGMainId').AsString))
      else
        MCXMG := MCXMG + ',' + QuotedStr(Trim(Self.ORDER_SUB.fieldbyname('MGMainId').AsString));
      Next;
    end;
  end;
  try
    frmSketchManageList := TfrmSketchManageList.Create(Application);
    with frmSketchManageList do
    begin

      caption := '印花描稿过滤';
      canshu1 := '查询';
      FCXMG := MCXMG;
      TBAdd.Visible := False;
      TBEdit.Visible := False;
      TBCopy.Visible := False;
      ToolButton4.Visible := False;
      TBDel.Visible := False;
      if ShowModal = 1 then
      begin
      end;
    end;
  finally
    frmSketchManageList.Free;
  end;
end;

procedure TfrmProductOrderList.N6Click(Sender: TObject);
var
  MMGMainId: string;
begin
  try
    frmSketchManageListSel := TfrmSketchManageListSel.Create(Application);
    with frmSketchManageListSel do
    begin
      M_HXCODE.Text := Trim(Self.Order_Sub.fieldbyname('PRTHX').AsString);
      if ShowModal = 1 then
      begin

        MMGMainId := Trim(frmSketchManageListSel.Order_Main.fieldbyname('MGID').AsString);
      end;
    end;
  finally
    frmSketchManageListSel.Free;
  end;
  if Trim(MMGMainId) <> '' then
  begin
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Sub SET  MGMainId=''' + trim(MMGMainId) + '''');
      sql.Add('where mainid=' + quotedstr(trim(Order_Main.fieldbyname('mainid').AsString)));
      sql.Add('and prthx=' + quotedstr(trim(ORDER_SUB.fieldbyname('prthx').AsString)));

      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('订单号：' + trim(Order_Sub.FieldByName('SubID').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    InitSub();
  end;

end;

procedure TfrmProductOrderList.btn2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty = True then
  begin
    Exit;
  end;
  Panel8.Visible := True;
  xgnote.Text := '';
  XGNote.Text := Trim(Order_Main.fieldbyname('ZYNOTE').AsString);
  FNOTE := '做样';
end;

procedure TfrmProductOrderList.Button7Click(Sender: TObject);
var
  MAXNO: string;
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select  max(subid) as subid from JYOrder_sub  where MAINID=' + QUOTEDSTR(Order_Main.fieldbyname('MAINID').AsString));
    SQL.Add('group by PRTHX');
    Open;
  end;

  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      First;
      while not Eof do
      begin

        if GetLSNo(ADOQueryCmd, MAXNO, 'MG', 'Sketch_main', 4, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('生成流水号异常！', '提示', 0);
          exit;
        end;
        with ADOQuery2 do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from jyorder_main a INNER JOIN JYORDER_SUB b on a.mainid=b.mainid');
          sql.Add('where subid=' + quotedstr(Trim(ADOQuery1.fieldbyname('SUBID').AsString)));
          Open;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('insert into Sketch_main(MGID)');
          sql.Add('values(' + quotedstr(MAXNO) + ')');

          SQL.Add('UPDATE Sketch_main SET orderno=' + quotedstr(Trim(ADOQuery2.fieldbyname('orderno').AsString)));
          SQL.Add(',M_KHNAME=' + quotedstr(Trim(ADOQuery2.fieldbyname('CustomerNoName').AsString)));
          SQL.Add(',frommainid=' + quotedstr(Trim(ADOQuery2.fieldbyname('mainid').AsString)));
          SQL.Add(',M_DBSPEC=' + quotedstr(Trim(ADOQuery2.fieldbyname('MPRTCODENAME').AsString)));

          SQL.Add(',M_COLOR=' + quotedstr(Trim(ADOQuery2.fieldbyname('PRTColor').AsString)));
          SQL.Add(',M_COLORNO=' + quotedstr(Trim(ADOQuery2.fieldbyname('PRTColorNo').AsString)));
          SQL.Add(',M_KHHX=' + quotedstr(Trim(ADOQuery2.fieldbyname('PRTHXKH').AsString)));
          SQL.Add(',M_HXCODE=' + quotedstr(Trim(ADOQuery2.fieldbyname('PRTHx').AsString)));
          SQL.Add(',fromsubid=' + quotedstr(Trim(ADOQuery2.fieldbyname('subid').AsString)));

          SQL.Add(',ywy=' + quotedstr(Trim(ADOQuery2.fieldbyname('YWY').AsString)));
          SQL.Add(',filler=' + QUOTEDSTR(Trim(DNAME)));
          SQL.Add(',m_type=' + QUOTEDSTR(M_TYPE.Text));
          SQL.Add(',M_HXTYPE=' + QUOTEDSTR(M_HXTYPE.Text));
          SQL.Add(',m_GONGYI=' + QUOTEDSTR(m_GONGYI.Text));
          SQL.Add(',m_note=' + QUOTEDSTR(m_note.Text));
          SQL.Add('WHERE MGID=' + QUOTEDSTR(MAXNO));


//          sql.Add('update JYOrder_sub set MGMainId=(select TOP 1 MGID from Sketch_MAIN A ');
//          SQL.Add('inner join JYOrder_Main B  on A.ORDERNO=B.Orderno where B.Mainid=JYOrder_sub.Mainid and A.M_HXCODE=JYOrder_sub.PRTHX )');
//          SQL.Add('WHERE SUBID=' + quotedstr(Trim(ADOQuery1.fieldbyname('subid').AsString)));
//            ShowMessage(sql.Text);
          EXECSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('update  JYOrder_Sub SET  MGMainId=''' + trim(MAXNO) + '''');
          sql.Add('where mainid=' + quotedstr(trim(ADOQuery2.fieldbyname('mainid').AsString)));
          sql.Add('and prthx=' + quotedstr(trim(ADOQuery2.fieldbyname('prthx').AsString)));
          ExecSQL;
        end;

        Next;
      end;

    end;

    ADOQuery1.EnableControls;
    application.MessageBox('生成描稿成功！', '提示信息');
    panel6.Visible := False;

  except
    application.MessageBox('生成描稿失败！', '提示信息', 0);
  end;
  TBRafresh.Click;

end;

procedure TfrmProductOrderList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin

  if AViewInfo.GridRecord.Values[VCWTJL.Index] <> null then
  begin
    if AViewInfo.GridRecord.Values[VCWTJL.Index] > 0 then
      ACanvas.Brush.Color := $4763FF;
  end;
end;

procedure TfrmProductOrderList.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    INITGRID();
  end;
end;

procedure TfrmProductOrderList.ToolButton10Click(Sender: TObject);
begin
  WriteCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  WriteCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理6');
  application.MessageBox('保存成功！', '提示信息', 0);
end;

end.

