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,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmProductOrderLYList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: 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;
    OrderNo: TEdit;
    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;
    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;
    cxTabControl1: TcxTabControl;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxStyle3: TcxStyle;
    VC_LYSL: TcxGridDBColumn;
    cxStyle4: TcxStyle;
    VC_ISRK: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Label5: TLabel;
    LYNOTE: TEdit;
    Tv1Column9: TcxGridDBColumn;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(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 OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TWCClick(Sender: TObject);
    procedure TNowcClick(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitGridFH();
    procedure InitImage(fwbid: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    FNOTE: string;
    { Public declarations }
  end;

var
  frmProductOrderLYList: TfrmProductOrderLYList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}
procedure TfrmProductOrderLYList.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 TfrmProductOrderLYList.FormDestroy(Sender: TObject);
begin
//  frmProductOrderList:=nil;
end;

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

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

end;

procedure TfrmProductOrderLYList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '生产指示单管理7');

  Close;

end;

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

  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.add('SELECT A.*,A.OrderNo OrderNoM ,   ');
      sql.Add('LYSL=(SELECT SUM(Qty)FROM CK_Sample_CR B WHERE b.orderno=a.orderno )');
      SQL.Add(',PRTHXHZ=cast((select B.PRTHX+'';'' from JYOrder_Sub B where B.MainId=A.MainId for xml path('''')) as varchar(200)),');
      SQL.Add('WTJL=(select COUNT(X.MainId) from JyOrder_Main_Jd X where X.MainId=A.MainId and X.hasProblem=1),');
      SQL.Add('OrderQty=(select Sum(PRTOrderQty) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId),');
      SQL.Add('ZQty=(select Sum(PRTOrderQty) from JYOrder_Sub B where B.MainId=A.MainId group by B.MainId),');
      SQL.Add('OrderUnit=(select Top 1 OrderUnit  from JYOrder_Sub B where B.MainId=A.MainId),');
      SQL.Add('PriceUnit=(select Top 1 PriceUnit  from JYOrder_Sub B where B.MainId=A.MainId),');
      SQL.Add('PRTPrice=(select Top 1 PRTPrice  from JYOrder_Sub B where B.MainId=A.MainId),');
//      SQL.Add('JdName=(select top 1   X.JdName from JyOrder_Main_Jd X where X.MainId=A.MainId order by X.idx desc ),');
      sql.Add(' JdName= [dbo].[F_Get_OrdJD](A.MainId ), ');
      SQL.Add('CKDATE=(SELECT TOP 1 FHDate FROM FHSQ_Main F INNER JOIN FHSQ_SUB G ON F.FSID=G.FSID WHERE SCCONNO=A.ORDERNO ORDER BY FHDATE DESC),');
      SQL.Add('RKDATE=(SELECT TOP 1 CRTIME FROM CK_BanCP_CR G WHERE G.MAINID=A.MAINID ORDER BY CRTIME DESC),');
      SQL.Add('YSSL=(select SUM(1)    from JYOrder_Sub B where B.MainId=A.MainId),');
      SQL.Add('ISRK=CASE when(SELECT count(1) FROM CK_BanCP_CR B where B.MainId=A.MainId and B.CRFlag=''入库'' )>0 then ''是'' else ''否'' end,');
      SQL.Add(' SLBName=(select Top 1 SLBName  from JYOrder_Sub B where B.MainId=A.MainId)');
      SQL.Add('FROM JYORDER_MAIN A');
      SQL.Add('WHERE LYNOTE IS NOT NULL  ');
      SQL.Add(STRwhere);
      if cxTabControl1.TabIndex = 0 then
      begin
        SQL.Add('and not exists(select mainid from CK_Sample_CR b where b.orderno=a.orderno) AND LYQK<>1');
      end;

      if cxTabControl1.TabIndex = 1 then
      begin
        SQL.Add('and  exists(select mainid from CK_Sample_CR b where b.orderno=a.orderno) OR LYQK=1  ');
      end;
      if ComboBox1.Text = '制单日期' then
      begin
        sql.Add('and OrdDate>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
        sql.Add('and OrdDate<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
        SQL.Add('ORDER BY OrdDate DESC');
      end;
      if ComboBox1.Text = '交货日期' then
      begin
        sql.Add('and DlyDate>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
        sql.Add('and DlyDate<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
        SQL.Add('ORDER BY DlyDate DESC');
      end;



//        ShowMessage(SQL.Text);
      Open;
    end;

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

procedure TfrmProductOrderLYList.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 TfrmProductOrderLYList.InitForm();
begin
  ReadCxGrid(Self.Caption, Tv1, '生产指示单管理7');

  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 180;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  if cxTabControl1.TabIndex = 0 then
  begin
    ToolButton1.Visible := True;
    ToolButton2.Visible := false;
  end
  else if cxTabControl1.TabIndex = 1 then
  begin
    ToolButton2.Visible := True;
    ToolButton1.Visible := false;
  end;
  InitGrid();
end;

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

function TfrmProductOrderLYList.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 TfrmProductOrderLYList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderLYList.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 TfrmProductOrderLYList.FormShow(Sender: TObject);
begin
  fFlileFlag := UserDataFlag + 'HX';

  InitForm();
end;

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

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

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

procedure TfrmProductOrderLYList.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 TfrmProductOrderLYList.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 TfrmProductOrderLYList.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 TfrmProductOrderLYList.OrderNoKeyPress(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 TfrmProductOrderLYList.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 TfrmProductOrderLYList.cxTabControl1Change(Sender: TObject);
begin
  if cxTabControl1.TabIndex = 0 then
  begin
    ToolButton1.Visible := True;
    ToolButton2.Visible := false;
  end
  else if cxTabControl1.TabIndex = 1 then
  begin
    ToolButton2.Visible := True;
    ToolButton1.Visible := false;
  end;
  InitGrid();
end;

procedure TfrmProductOrderLYList.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 TfrmProductOrderLYList.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 TfrmProductOrderLYList.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
//  initImage(ADOQuerySub.fieldbyname('HXFile').AsString);
end;

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

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

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

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

procedure TfrmProductOrderLYList.ToolButton10Click(Sender: TObject);
begin
  WriteCxGrid('指示单列表FF1', Tv1, '生产指示单管理');

  application.MessageBox('保存成功！', '提示信息', 0);
end;

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

         //ISRK

  if AViewInfo.GridRecord.Values[VC_ISRK.Index] = '是' then
  begin
    if AViewInfo.GridRecord.Values[VC_LYSL.Index] <= 0 then
      ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmProductOrderLYList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Order_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try

    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  JYORDER_MAIN SET LYQK=1');
            sql.Add('where mainid=' + quotedstr(trim(Order_Main.fieldbyname('MAINID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('标记成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('标记失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderLYList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Order_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try

    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  JYORDER_MAIN SET LYQK=0');
            sql.Add('where mainid=' + quotedstr(trim(Order_Main.fieldbyname('MAINID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销标记成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销标记失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderLYList.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

end.

