unit U_ProductOrderNewList;

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;

type
  TfrmProductOrderNewList = 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;
    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;
    ToolButton1: TToolButton;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    v1MPRTKZ: TcxGridDBColumn;
    Label10: TLabel;
    MPRTSpec: 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;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    cxSplitter1: TcxSplitter;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_Chk: TDataSource;
    CDS_Chk: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Chker: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label2: TLabel;
    OrdPerson2: TEdit;
    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 Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    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 OrderNoMKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmProductOrderNewList: TfrmProductOrderNewList;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun, U_ZDYHelp, U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmProductOrderNewList.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList:=nil;
end;

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

procedure TfrmProductOrderNewList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderNewList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单列表FF',Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderNewList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('exec P_View_Order :begdate,:enddate,:WSql') ;
      if Trim(DParameters1)<>'高权限'  then
      begin
        Parameters.ParamByName('WSql').Value:=' and Filler='''+Trim(DName)+'''';
      end else
      begin
        Parameters.ParamByName('WSql').Value:='';
      end;
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      ExecSQL;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmProductOrderNewList.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 TfrmProductOrderNewList.InitForm();
begin
  ReadCxGrid('指示单列表FF',Tv1,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 10* from JYOrder_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;
  //InitGrid();
end;

procedure TfrmProductOrderNewList.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 TfrmProductOrderNewList.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;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='审核通过' then
  begin
    Application.MessageBox('已审核不能修改!','提示',0);
    Exit;
  end else
  if Trim(ADOQueryTemp.FieldByName('ChkStatus').AsString)='' then
  begin
    if ADOQueryTemp.FieldByName('OrdFlag1').AsBoolean=True then
    begin
      Application.MessageBox('审核中不能修改!','提示',0);
      Exit;
    end;

  end;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo:=Trim(Self.Order_Main.fieldbyname('OrderNoM').AsString);
      if ShowModal=1 then
      begin

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

procedure TfrmProductOrderNewList.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;
  if Order_Main.FieldByName('OrdFlag1').AsBoolean=True then
  begin
    Application.MessageBox('已送审不能删除数据!','提示',0);
    Exit;
  end;
  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from Contract_Cloth_LL A where exists (select * from JYOrder_Sub B where B.SubId=A.OrdSubId  ');
    SQL.Add(' and B.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 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 Contract_Cloth_LLMX where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').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 JYOrder_Sub_AnPai 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 TfrmProductOrderNewList.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)+'''');
      ExecSQL;
    end;
    {with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
      if IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;   }
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

procedure TfrmProductOrderNewList.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
   i,j:Integer;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.FieldByName('ChkStatus').AsString)<>'审核通过' then
  begin
    RM1.PreviewButtons:=[pbExit];
  end else
  begin
    RM1.PreviewButtons:=[pbZoom,pbLoad,pbSave,pbPrint,pbFind,pbPageSetup,pbExit,pbExport,pbNavigator];
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,ColorCount=(select isnull(Count(*),0) from JYOrder_Sub where MainId=A.MainId),  ');
    sql.add('OrderUnit=(select Top 1 OrderUnit from JYOrder_Sub where MainId=A.MainId),');
    sql.add('ZQty=(select sum(PRTOrderQty) from JYOrder_Sub where MainId=A.MainId)');
   // sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId ');
   sql.Add(' from JYOrder_Main A  ');
    sql.Add(' where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  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;
end;

procedure TfrmProductOrderNewList.TBRafreshClick(Sender: TObject);
begin
  if FFInt=1 then
  begin
    InitGridFH();
  end else
  begin
    InitGrid();
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 10* from JYOrder_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.TBAddClick(Sender: TObject);
var
  maxno:string;
begin
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

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

procedure TfrmProductOrderNewList.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 TfrmProductOrderNewList.FormShow(Sender: TObject);
begin
  InitForm();
end;

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

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

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

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

procedure TfrmProductOrderNewList.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.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.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.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.ToolButton1Click(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;
      Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

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

procedure TfrmProductOrderNewList.ToolButton2Click(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);
      if ShowModal=1 then
      begin

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

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

procedure TfrmProductOrderNewList.OrderNoMKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(OrderNoM.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order :begdate,:enddate,:WSql') ;
        if Trim(DParameters1)<>'高权限'  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 TfrmProductOrderNewList.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(conno.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order :begdate,:enddate,:WSql') ;
        if Trim(DParameters1)<>'高权限'  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 TfrmProductOrderNewList.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmProductOrderNewList.Button1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno,LBName:string;
   i,j:Integer;
   OrderKg:Double;
begin
  if Order_Main.IsEmpty then Exit;
  LBName:=RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(LBName)+'.rmf' ;
  if RadioGroup1.ItemIndex=0 then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,ColorCount=(select isnull(Count(*),0) from JYOrder_Sub where MainId=A.MainId),  ');
      sql.add('ZQty=(select sum(PRTOrderQty) from JYOrder_Sub where MainId=A.MainId)');
      SQL.Add(', Case when B.OrderUnit=''M'' then Cast(Cast(dbo.F_Get_Order_MFKZ(A.MainId,''MF'') ');
      sql.Add(' *1.00/100*B.PRTOrderQty*dbo.F_Get_Order_MFKZ(A.MainId,''KZ'')/1000 as decimal(18,0)) as varchar(20))+''Kg'' ');
      SQL.Add(' when B.OrderUnit=''Y'' then Cast (Cast(dbo.F_Get_Order_MFKZ(A.MainId,''MF'')');
      sql.Add(' *1.00/100*B.PRTOrderQty*0.9144*dbo.F_Get_Order_MFKZ(A.MainId,''KZ'')/1000 as decimal(18,0)) as varchar(20))+''Kg'' ');
      sql.Add(' else '''' end as PRTOrderKgQtyStr ');
      sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId ');
      sql.Add(' and A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;

  end else
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_View_OrderSub :begdate,:enddate,:wsql  ');
      Parameters.ParamByName('WSql').Value:=' and A.MainId='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''';
      Parameters.ParamByName('begdate').Value:='1899-01-01';
      Parameters.ParamByName('enddate').Value:='2050-01-01';
      Open;
    end;
    if Trim(ADOQueryPrint.FieldByName('PRTHX').AsString)<>'' then
    begin
      if Trim(LBName)='颜色样' then
      begin
        fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\颜色花型样.rmf' ;
      end;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(PRTOrderKgQty),0) PRTOrderKgHZQty from(');
    sql.Add('select  ');
    SQL.Add(' Case when B.OrderUnit=''M'' then Cast(dbo.F_Get_Order_MFKZ(A.MainId,''MF'') ');
    sql.Add(' *1.00/100*B.PRTOrderQty*dbo.F_Get_Order_MFKZ(A.MainId,''KZ'')/1000 as decimal(18,0))');
    SQL.Add(' when B.OrderUnit=''Y'' then Cast(dbo.F_Get_Order_MFKZ(A.MainId,''MF'')');
    sql.Add(' *1.00/100*B.PRTOrderQty*0.9144*dbo.F_Get_Order_MFKZ(A.MainId,''KZ'')/1000 As decimal(18,0)) ');
    sql.Add(' else 0 end as PRTOrderKgQty');
    sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId ');
    sql.Add(' and A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''')AA');
    Open;
  end;
  OrderKg:=ADOQueryTemp.fieldbyname('PRTOrderKgHZQty').Value;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);

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

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

procedure TfrmProductOrderNewList.ToolButton4Click(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdConChker';
      flagname:='指示书审核人';
      MainType:=Trim(DName);
      if ShowModal=1 then
      begin

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

procedure TfrmProductOrderNewList.ToolButton5Click(Sender: TObject);
var
  maxno:string;
  FCount:Integer;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  if Trim(Order_Main.fieldbyname('ChkStatus').AsString)='审核通过' then
  begin
    Application.MessageBox('已审核通过!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Chker=Cast('''' as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Chker);
  SInitCDSData20(ADOQueryTemp,CDS_Chker);
  while CDS_Chker.IsEmpty=False do
  begin
    CDS_Chker.Delete;
  end;
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='OrdConChker';
      flagname:='指示单审核人';
      MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSEl').AsBoolean=True then
            begin
              CDS_Chker.Append;
              CDS_Chker.FieldByName('Chker').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              CDS_Chker.Post;
            end;
            Next;
          end;
        end;
        if CDS_Chker.IsEmpty then FCount:=9

      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
  if FCount=9 then
  begin
    if CDS_Chker.IsEmpty then
    begin
      Application.MessageBox('没有选择审核人!','提示',0);
      Exit;
    end;
  end;


  CDS_Chker.DisableControls;
  with CDS_Chker do
  begin
    First;
    while not eof do
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from SY_User where UserName='''+Trim(CDS_Chker.fieldbyname('Chker').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        CDS_Chker.EnableControls;
        Application.MessageBox('审核人定义错误!','提示',0);
        Exit;
      end;
      Next;
    end;
  end;
  CDS_Chker.EnableControls;
  if CDS_Chker.IsEmpty then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  //if CDS_Chk.IsEmpty then
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_Main Set OrdFlag1=1,OrdDefDate1=getdate() where MainId='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
        ExecSQL;
      end;
      with Order_Main do
      begin
        Edit;
        FieldByName('OrdFlag1').Value:=1;
        Post;
      end;
      with CDS_Chker do
      begin
        First;
        while not Eof do
        begin
          if CDS_Chk.Locate('Chker',Trim(CDS_Chker.fieldbyname('Chker').AsString),[])=False then
          begin
            if GetLSNo(ADOQueryCmd,maxno,'CK','OrdCon_Chk',4,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取最大号失败!','提示',0);
              Exit;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from OrdCon_Chk where 1<>1');
              Open;
            end;
            with ADOQueryCmd do
            begin
              Append;
              FieldByName('CKID').Value:=Trim(maxno);
              FieldByName('MainID').Value:=Trim(Order_Main.fieldbyname('MainID').AsString);
              FieldByName('Chker').Value:=Trim(CDS_Chker.fieldbyname('Chker').AsString);
              Post;
            end;
            with CDS_Chk do
            begin
              Append;
              FieldByName('Chker').Value:=Trim(CDS_Chker.fieldbyname('Chker').AsString);
              Post;
            end;
          end;
          Next;
        end;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('调用审核人失败!','提示',0);
    end;
  end;
end;

procedure TfrmProductOrderNewList.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OrdCon_Chk where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Chk);
  SInitCDSData20(ADOQueryTemp,CDS_Chk);
end;

end.
