unit U_ProductOrderNewList_FB;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxButtonEdit, cxTextEdit, cxPC, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter,
  cxNavigator, dxBarBuiltInMenu;

type
  TfrmProductOrderNewList_FB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    invoiceNo: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    orddefstr10: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label10: TLabel;
    MPRTSpec: TEdit;
    Label11: TLabel;
    MPRTCode: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    Label13: TLabel;
    MPRTMF: TEdit;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    ComboBox1: TComboBox;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    PMFJ: TPopupMenu;
    NFJ: TMenuItem;
    v1OrdPerson2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    TChk: TToolButton;
    TNOChk: TToolButton;
    RMDB_SK: TRMDBDataSet;
    CDS_SK: TClientDataSet;
    v1Column10: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1invoceNO: TcxGridDBColumn;
    v2prtmoney: TcxGridDBColumn;
    v2SordQty4: TcxGridDBColumn;
    v2SordQty5: TcxGridDBColumn;
    v2SordQty3: TcxGridDBColumn;
    v1OrdDefDate1: TcxGridDBColumn;
    Label2: TLabel;
    OrdPerson2: TEdit;
    RadioGroup2: TRadioGroup;
    v1OrdDefStr14: TcxGridDBColumn;
    v1OrdDefStr13: TcxGridDBColumn;
    v2SordQty7: TcxGridDBColumn;
    CDS_Print1: TClientDataSet;
    RMDBMainZX: TRMDBDataSet;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    TBNodel: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    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 invoiceNoChange(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 N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2DblClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure TChkClick(Sender: TObject);
    procedure TNOChkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure RadioGroup2Click(Sender: TObject);
    procedure TBNodelClick(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;

    procedure InitSub();
    procedure SetStatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    ftype: string;

    { Public declarations }
  end;

var
  frmProductOrderNewList_FB: TfrmProductOrderNewList_FB;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun,  U_OrderInPut_CY, U_OrderInPut_FB;

{$R *.dfm}
procedure TfrmProductOrderNewList_FB.SetStatus();
begin
  tchk.Visible:=false;
  tnochk.Visible:=false;
  TBDel.Visible:=false;
  TBNodel.Visible:=false;
  TBEdit.Visible:=false;
    case cxTabControl1.TabIndex of
    0:begin
        TBDel.Visible:=true;
        TBEdit.Visible:=true;
      end;
    1:begin
        TBNodel.Visible:=true;
      end;
    2:begin
      end;
    end;
{  if Trim(DParameters1)<>'高权限'  then
  begin
    case cxTabControl1.TabIndex of
    0:begin
       tbedit.Visible:=true;
       tbdel.Visible:=true;
      end;
    1:begin
      end;
    2:begin
      end;
    end;
  end
  else
  begin
    case cxTabControl1.TabIndex of
    0:begin
       tchk.Visible:=true;
       tbedit.Visible:=true;
       tbdel.Visible:=true;
      end;
    1:begin
       tnochk.Visible:=true;
      end;
    2:begin
      end;
    end;
  end; }
end;

procedure TfrmProductOrderNewList_FB.InitSub();
begin
  ADOQuerySub.Close;
  IF Order_Main.IsEmpty then exit;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JYOrderFB_sub ');
    sql.Add('where mainID ='+quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
end;

procedure TfrmProductOrderNewList_FB.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList_FB:=nil;
end;

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

procedure TfrmProductOrderNewList_FB.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
  cxTabControl1.TabIndex:=0;
  SetStatus();
end;

procedure TfrmProductOrderNewList_FB.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption+tv1.Name,Tv1,'生产指示单管理');
  WriteCxGrid(self.Caption+tv2.Name,Tv2,'生产指示单管理');
end;

procedure TfrmProductOrderNewList_FB.InitGrid();
var
  fwsql:string;
begin
  fwsql:='';
 IF cxTabControl1.TabIndex<2 then
  begin
    fwsql:=fwsql+' and  isnull(A.status,''0'')='''+inttostr(cxTabControl1.TabIndex)+'''';
  end;
  if Trim(DParameters1)<>'高权限'  then
  begin
   //  fwsql:=fwsql+' and Filler='''+Trim(DName)+'''';
  end;
  if Trim(DParameters1)='业务员'  then
  begin
     fwsql:=fwsql+' and OrdPerson2='''+Trim(DName)+'''';
  end;
  try
  //  ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('exec P_View_Order_FB :begdate,:enddate,:WSql');
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      Parameters.ParamByName('WSql').Value:=fwsql;
     // ExecSQL;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
   // ADOQueryMain.EnableControls;
  end;
  TBFind.Click;
end;


procedure TfrmProductOrderNewList_FB.InitForm();
begin
  ReadCxGrid(self.Caption+tv1.Name,Tv1,'生产指示单管理');
  readCxGrid(self.Caption+tv2.Name,Tv2,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  if Trim(ftype)='' then
  begin
    TBAdd.Visible:=false;
    TBEdit.Visible:=False;
    TBDel.Visible:=False;
  end;
{  if Trim(ftype)<>'' then
  begin
    RadioGroup2.ItemIndex:=RadioGroup2.Items.IndexOf(Trim(Ftype));
    if RadioGroup2.ItemIndex>=0 then
    begin
      RadioGroup2.Enabled:=false;
    end
    else
    RadioGroup2.ItemIndex:=2;
  end;  }
  InitGrid();
end;

procedure TfrmProductOrderNewList_FB.TBFindClick(Sender: TObject);
var strwhere: string;
begin
  if ADOQueryMain.Active=False then Exit;
  strwhere:=SGetFilters(Panel1,1,2);
  if strwhere='' then
  begin
    IF RadioGroup2.ItemIndex<2 then
    strwhere:=strwhere+' OrdDefStr15='''+trim(RadioGroup2.Items.Strings[RadioGroup2.itemindex])+'''';
  end
  else
  begin
     IF RadioGroup2.ItemIndex<2 then
    strwhere:=strwhere+' and OrdDefStr15='''+trim(RadioGroup2.Items.Strings[RadioGroup2.itemindex])+'''';
  end;
  SDofilter(ADOQueryMain,strwhere);
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmProductOrderNewList_FB.TBEditClick(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;  }
  try
    frmOrderInPut_FB:=TfrmOrderInPut_FB.Create(Application);
    with frmOrderInPut_FB do
    begin
      PState:=1;
      ftype:=Self.ftype;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo:=Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmOrderInPut_FB.Free;
  end;
end;

procedure TfrmProductOrderNewList_FB.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;

  if Application.MessageBox('确定要作废数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmProductOrderNewList_FB.DelData():Boolean;
var
  CRID:string;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from  YF_Money_CR where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      open;
      IF not isempty then
      begin
        CRID:=fieldbyname('CRID').AsString;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update JYOrderFB_Main SET status=''1'' ');
      sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
    //  sql.Add('delete JYOrderFB_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    //  sql.Add('delete JYOrderFB_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
      sql.Add('delete YF_Money_CR 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('invoiceNo').AsString))));
      sql.Add(','+quotedstr(trim('成功')));
      sql.Add(')');
      sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID) ');
      sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID) ');
      sql.Add(' where CRID='+CRID);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderNewList_FB.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,self.Caption);
end;

procedure TfrmProductOrderNewList_FB.TBPrintClick(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  {with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.*,E.MPRTType,E.BankSelfFastNo,E.BankAddress,');
    SQL.Add('OrderUnit=(select Top 1 OrderUnit from JYOrderFB_Sub B where B.MainId=A.MainId),');
    SQL.Add('ZSordQty1=(select Sum(SordQty1) from JYOrderFB_Sub B where B.MainId=A.MainId group by B.MainId) ');
    sql.Add('from JYOrderFB_Main A inner join JYOrderFB_Sub B on A.MainId=B.MainId ');
    sql.Add('left join JYOrderCY_sub C on C.Subid=B.Sorddefstr10 ');
    sql.Add('left join  JYOrderCY_Main D on D.mainID=C.mainID ');
    sql.Add('left  join JYOrderCon_Main E on E.conNO=D.conNO ');
    sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  if Trim(ADOQueryPrint.FieldByName('MPRTType').AsString)='内销' then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\合同发票1.rmf';
  end
  else
  fPrintFile:= ExtractFilePath(Application.ExeName)+'Report\合同发票2.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\合同发票.rmf'),'提示',0);
  end;}
  //Panel4.Visible:=True;
  button1.Click;
end;

procedure TfrmProductOrderNewList_FB.TBRafreshClick(Sender: TObject);
begin
  if FFInt=1 then
  begin

  end
  else
  begin
    InitGrid();
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 10* from JYOrderFB_Main Order by FillTime desc');
      Open;
    end;
    ComboBox1.Clear;
    with ADOQueryTemp do
    begin
      First;
      while not Eof do
      begin
        ComboBox1.Items.Add(Trim(ADOQueryTemp.fieldbyname('OrderNO').AsString));
        Next;
      end;
    end;
  end;
end;

procedure TfrmProductOrderNewList_FB.TBAddClick(Sender: TObject);
begin
  try
    frmOrderInPut_FB:=TfrmOrderInPut_FB.Create(Application);
    with frmOrderInPut_FB do
    begin
      PState:=0;
      FMainId:='';
      ftype:=Self.ftype;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmOrderInPut_FB.Free;
  end;
end;

procedure TfrmProductOrderNewList_FB.invoiceNoChange(Sender: TObject);
begin
  TBFind.Click
end;

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

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

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

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

procedure TfrmProductOrderNewList_FB.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 TfrmProductOrderNewList_FB.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 TfrmProductOrderNewList_FB.N1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderNewList_FB.N2Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderNewList_FB.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut_FB:=TfrmOrderInPut_FB.Create(Application);
    with frmOrderInPut_FB do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      Panel1.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut_FB.Free;
  end;
end;

procedure TfrmProductOrderNewList_FB.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut_FB:=TfrmOrderInPut_FB.Create(Application);
    with frmOrderInPut_FB do
    begin
      PState:=1;
      CopyInt:=99;
      
      ftype:=Self.ftype;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo:=Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut_FB.Free;
  end;
end;

procedure TfrmProductOrderNewList_FB.ToolButton3Click(Sender: TObject);
begin
  ModalResult:=1;
end;

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

procedure TfrmProductOrderNewList_FB.Button1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno,LBName,SYRName:string;
   i,j:Integer;
   OrderKg:Double;
begin
  if Order_Main.IsEmpty then Exit;
  {LBName:=RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];
  if LBName='装箱单' then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\装箱单.rmf' ;
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.Add('select AA.OrdDefStr14,AA.OrdDefStr13,AA.CustomerNoName,AA.InvoiceNo,AA.ConNo,AA.Orddate,AA.OrdDefStr4,AA.orddefstr3,AA.OrdDefStr15,AA.PRTColor,AA.SordQty7,');
      SQL.Add('AA.PrtCodeName,AA.PRTspec,AA.KHConNO,AA.Mainid,AA.OrderUnit,Sum(AA.SordQty1) SL,SUM(AA.Sordqty4) MZ,SUM(AA.SordQty5) JZ,SUM(AA.SordQty3) LFS from ( ');
      SQL.Add('select A.OrdDefStr14,A.CustomerNoName,A.OrdDefStr13,A.InvoiceNo,A.ConNo,A.Orddate,A.OrdDefStr4,A.orddefstr3,A.OrdDefStr15,');
      SQL.Add('B.PrtCodeName,B.PRTspec,A.KHConNO,A.Mainid,B.SordQty1,B.Sordqty4,B.SordQty5,B.SordQty3,B.PRTColor,B.SordQty7,');
      SQL.Add('OrderUnit=(select Top 1 OrderUnit from JYOrderFB_Sub B where B.MainId=A.MainId) ');
      SQL.Add('from JYOrderFB_Main A inner join JYOrderFB_Sub B on A.MainId=B.MainId where B.PRTColor<>'''') AA ');
      sql.Add('where AA.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');

      sql.Add('group by AA.OrdDefStr14,AA.OrdDefStr13,AA.CustomerNoName,AA.InvoiceNo,AA.ConNo,AA.Orddate,AA.OrdDefStr4,AA.orddefstr3,AA.OrdDefStr15,AA.PRTColor,AA.SordQty7,');
      sql.Add('AA.PrtCodeName,AA.PRTspec,AA.KHConNO,AA.Mainid,AA.OrderUnit ');
      sql.add('order by AA.PrtCodeName,AA.PRTspec,AA.PRTColor,AA.KHConNO');
      Open;
    end;
    SCreateCDS20(ADOQueryPrint,CDS_Print);
    SInitCDSData20(ADOQueryPrint,CDS_Print);
  end
  else
  begin
    with ADOQueryPrint do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,B.*,D.MaiTouNote,E.MPRTType,E.BankSelfFastNo,E.BankAddress,');
      SQL.Add('OrderUnit=(select Top 1 OrderUnit from JYOrderFB_Sub B where B.MainId=A.MainId),');
      SQL.Add('ZSordQty1=(select Sum(SordQty1) from JYOrderFB_Sub B where B.MainId=A.MainId group by B.MainId) ');
      sql.Add('from JYOrderFB_Main A inner join JYOrderFB_Sub B on A.MainId=B.MainId ');
      sql.Add('left join JYOrderCY_sub C on C.Subid=B.Sorddefstr10 ');
      sql.Add('left join  JYOrderCY_Main D on D.mainID=C.mainID ');
      sql.Add('left  join JYOrderCon_Main E on E.conNO=D.conNO ');
      sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryPrint,CDS_Print);
    SInitCDSData20(ADOQueryPrint,CDS_Print);
    if Trim(CDS_Print.fieldbyname('OrdDefStr15').AsString)='内销' then
    begin
      fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\合同发票1.rmf';
    end
    else
    fPrintFile:= ExtractFilePath(Application.ExeName)+'Report\合同发票2.rmf';
  end;}
  with ADOQueryTemp do
  begin
      Close;
      sql.Clear;
      sql.Add('select AA.OrdDefStr14,AA.OrdDefStr13,AA.CustomerNoName,AA.InvoiceNo,AA.ConNo,AA.Orddate,AA.OrdDefStr4,AA.orddefstr3,AA.OrdDefStr15,AA.FPTT,AA.FPdz,AA.PRTColor,AA.SordQty7,');
      SQL.Add('AA.PrtCodeName,AA.PRTspec,AA.KHConNO,AA.Mainid,AA.OrderUnit,Sum(AA.SordQty1) SL,SUM(AA.Sordqty4) MZ,SUM(AA.SordQty5) JZ,SUM(AA.SordQty3) LFS from ( ');
      SQL.Add('select A.OrdDefStr14,A.CustomerNoName,A.OrdDefStr13,A.InvoiceNo,A.ConNo,A.Orddate,A.OrdDefStr4,A.orddefstr3,A.OrdDefStr15,A.FPTT,A.FPdz,');
      SQL.Add('B.PrtCodeName,B.PRTspec,A.KHConNO,A.Mainid,B.SordQty1,B.Sordqty4,B.SordQty5,B.SordQty3,B.PRTColor,B.SordQty7,');
      SQL.Add('OrderUnit=(select Top 1 OrderUnit from JYOrderFB_Sub B where B.MainId=A.MainId) ');
      SQL.Add('from JYOrderFB_Main A inner join JYOrderFB_Sub B on A.MainId=B.MainId where B.PRTColor<>'''') AA ');
      sql.Add('where AA.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('group by AA.OrdDefStr14,AA.OrdDefStr13,AA.CustomerNoName,AA.InvoiceNo,AA.ConNo,AA.Orddate,AA.OrdDefStr4,AA.orddefstr3,AA.OrdDefStr15,AA.PRTColor,AA.SordQty7,AA.FPTT,AA.FPdz,');
      sql.Add('AA.PrtCodeName,AA.PRTspec,AA.KHConNO,AA.Mainid,AA.OrderUnit ');
      sql.add('order by AA.PrtCodeName,AA.PRTspec,AA.PRTColor,AA.KHConNO');
      Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Print1);
  SInitCDSData20(ADOQueryTemp,CDS_Print1);
  with ADOQueryPrint do
  begin
      close;
      sql.Clear;
      sql.Add('select A.*,B.*,D.MaiTouNote,E.MPRTType,E.BankSelfFastNo,E.BankAddress,');
      SQL.Add('OrderUnit=(select Top 1 OrderUnit from JYOrderFB_Sub B where B.MainId=A.MainId),');
      SQL.Add('ZSordQty1=(select Sum(SordQty1) from JYOrderFB_Sub B where B.MainId=A.MainId group by B.MainId) ');
      sql.Add('from JYOrderFB_Main A inner join JYOrderFB_Sub B on A.MainId=B.MainId ');
      sql.Add('left join JYOrderCY_sub C on C.Subid=B.Sorddefstr10 ');
      sql.Add('left join  JYOrderCY_Main D on D.mainID=C.mainID ');
      sql.Add('left  join JYOrderCon_Main E on E.conNO=D.conNO ');
      sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.add('order by B.PrtCodeName,B.PRTspec,B.PRTColor,A.KHConNO');
      Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  if Trim(CDS_Print.fieldbyname('OrdDefStr15').AsString)='内销' then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\合同发票1.rmf';
  end
  else
  fPrintFile:= ExtractFilePath(Application.ExeName)+'Report\合同发票2.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\合同发票.rmf'),'提示',0);
  end;
end;

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

procedure TfrmProductOrderNewList_FB.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

procedure TfrmProductOrderNewList_FB.Tv2DblClick(
  Sender: TObject);
var
  fNO:string;
begin
  IF ADOQuerySub.IsEmpty then exit;
  ToolButton2.Click;
 { with  ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select ATID from KH_Zdy_Attachment A');
    sql.Add('where zdyCode='+quotedstr(trim(ADOQuerySub.fieldbyname('prtCode').AsString)));
    sql.Add('and Type='+quotedstr(trim('CP')));
    open;
    if not IsEmpty then
    begin
      fNO:=trim(fieldbyname('ATID').AsString);
    end;
  end;

  try
    frmZdyAttInputCP10:=TfrmZdyAttInputCP10.Create(Application);
    with frmZdyAttInputCP10 do
    begin
      FATID:=Trim(fNO);
      frmZdyAttInputCP10.Tsave.Enabled:=false;
      if ShowModal=1 then
      begin
      //  TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttInputCP10.Free;
  end;
   }
end;

procedure TfrmProductOrderNewList_FB.Tv1DblClick(Sender: TObject);
begin
//    ToolButton1.Click;
end;

procedure TfrmProductOrderNewList_FB.TChkClick(Sender: TObject);
begin
 if Order_Main.IsEmpty then exit;
 ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update JYOrderFB_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('invoiceNo').AsString))));
      sql.Add(','+quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('审核成功！','提示信息');
    TBRafresh.Click;
  except
   ADOQueryCmd.Connection.RollbackTrans;
   application.MessageBox('审核失败！','提示信息',0);
  end;
end;

procedure TfrmProductOrderNewList_FB.TNOChkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrderCY_Main SET status=''0'' ');
      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('invoiceNo').AsString))));
      sql.Add(','+quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
     ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销审核成功！','提示信息');
    TBRafresh.Click;
  except
     ADOQueryCmd.Connection.RollbackTrans;
   application.MessageBox('撤销审核失败！','提示信息',0);
  end;
end;

procedure TfrmProductOrderNewList_FB.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  TBRafresh.Click;
end;

procedure TfrmProductOrderNewList_FB.RadioGroup2Click(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmProductOrderNewList_FB.TBNodelClick(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;  }
  try
    frmOrderInPut_FB:=TfrmOrderInPut_FB.Create(Application);
    with frmOrderInPut_FB do
    begin
      PState:=1;
      ftype:=Self.ftype;
      TBSave.Caption:='撤销作废';
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo:=Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;

    end;
  finally
    frmOrderInPut_FB.TBSave.Caption:='保存';
    frmOrderInPut_FB.Free;
  end;
end;

procedure TfrmProductOrderNewList_FB.Tv1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  IF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('prtmoney').Index]<0 then
  begin
  //  ACanvas.Brush.Color:=Clgreen;
    ACanvas.Brush.Color:=clLime;
  end;
end;

end.
