unit U_ProductOrderListNew;

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,
  cxGridBandedTableView, cxGridDBBandedTableView;

type
  TfrmProductOrderListNew = 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;
    Label2: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    Label5: TLabel;
    WFBCodeName: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1OrderDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1FactoryNo2Name: TcxGridDBColumn;
    v1FactoryNo3Name: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomNoName: TEdit;
    v1Column3: TcxGridDBColumn;
    TBCK: TToolButton;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    TBChk: TToolButton;
    TBCChk: TToolButton;
    v1Column6: TcxGridDBColumn;
    TBBQPrint: TToolButton;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    TBCopy: TToolButton;
    cxGrid2DBTableView1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBBandedTableView;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    V2Column1: TcxGridDBBandedColumn;
    V2Column2: TcxGridDBBandedColumn;
    V2Column3: TcxGridDBBandedColumn;
    V2Column4: TcxGridDBBandedColumn;
    V2Column5: TcxGridDBBandedColumn;
    V2Column6: TcxGridDBBandedColumn;
    V2Column7: TcxGridDBBandedColumn;
    V2Column8: TcxGridDBBandedColumn;
    V2Column9: TcxGridDBBandedColumn;
    V2Column10: TcxGridDBBandedColumn;
    V2Column11: TcxGridDBBandedColumn;
    V2Column12: TcxGridDBBandedColumn;
    V2Column13: TcxGridDBBandedColumn;
    V2Column14: TcxGridDBBandedColumn;
    V2Column15: TcxGridDBBandedColumn;
    V2Column16: TcxGridDBBandedColumn;
    V2Column17: TcxGridDBBandedColumn;
    V2Column18: TcxGridDBBandedColumn;
    V2Column19: TcxGridDBBandedColumn;
    cxGrid3: TcxGrid;
    cxGridDBTableView1: TcxGridDBTableView;
    Tv3: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    cxGridDBBandedColumn3: TcxGridDBBandedColumn;
    cxGridDBBandedColumn5: TcxGridDBBandedColumn;
    cxGridDBBandedColumn6: TcxGridDBBandedColumn;
    cxGridDBBandedColumn7: TcxGridDBBandedColumn;
    cxGridDBBandedColumn8: TcxGridDBBandedColumn;
    cxGridDBBandedColumn9: TcxGridDBBandedColumn;
    cxGridDBBandedColumn10: TcxGridDBBandedColumn;
    cxGridDBBandedColumn11: TcxGridDBBandedColumn;
    cxGridDBBandedColumn12: TcxGridDBBandedColumn;
    cxGridDBBandedColumn13: TcxGridDBBandedColumn;
    cxGridDBBandedColumn14: TcxGridDBBandedColumn;
    cxGridDBBandedColumn15: TcxGridDBBandedColumn;
    cxGridDBBandedColumn16: TcxGridDBBandedColumn;
    cxGridDBBandedColumn17: TcxGridDBBandedColumn;
    cxGridDBBandedColumn18: TcxGridDBBandedColumn;
    cxGridDBBandedColumn19: TcxGridDBBandedColumn;
    cxGridLevel1: TcxGridLevel;
    Panel3: TPanel;
    Label8: TLabel;
    Button1: TButton;
    Button2: TButton;
    Panel10: TPanel;
    Image2: TImage;
    RadioGroup1: TRadioGroup;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    CDS_ZP: TClientDataSet;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle3: TcxStyle;
    V2Column20: TcxGridDBBandedColumn;
    V2Column21: TcxGridDBBandedColumn;
    V2Column22: TcxGridDBBandedColumn;
    V2Column23: TcxGridDBBandedColumn;
    cxSplitter1: TcxSplitter;
    v3Column1: TcxGridDBBandedColumn;
    v3Column2: TcxGridDBBandedColumn;
    v3Column3: TcxGridDBBandedColumn;
    v3Column4: TcxGridDBBandedColumn;
    ToolButton2: TToolButton;
    V2Column24: TcxGridDBBandedColumn;
    V2Column25: TcxGridDBBandedColumn;
    v3Column5: TcxGridDBBandedColumn;
    v3Column6: TcxGridDBBandedColumn;
    V2Column26: TcxGridDBBandedColumn;
    v3Column7: TcxGridDBBandedColumn;
    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 TBViewClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBBQPrintClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TBCKClick(Sender: TObject);
    procedure TBChkClick(Sender: TObject);
    procedure TBCChkClick(Sender: TObject);
    procedure TBCopyClick(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TV2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    PPInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData(Order_Main10:TClientDataSet):Boolean;
    procedure InitGridFH();
    procedure CopyOrderData(Order_MainFF:TClientDataSet);
    { Private declarations }
  public
    FFInt:Integer;
    { Public declarations }
  end;

var
  frmProductOrderListNew: TfrmProductOrderListNew;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun,U_OrderInPutZP,U_OrderInPutNew,U_OrderInPutZPNew
  ,U_OrderInPutZPLNew,U_OrderAttachment;

{$R *.dfm}

procedure TfrmProductOrderListNew.FormDestroy(Sender: TObject);
begin
  frmProductOrderListNew:=nil;
end;

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

procedure TfrmProductOrderListNew.FormCreate(Sender: TObject);
begin
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  if DParameters1='审核' then
  begin
    TBChk.Visible:=True;
    TBCChk.Visible:=True;
    TBAdd.Visible:=False;
    TBEdit.Visible:=False;
    TBCopy.Visible:=False;
  end else
  begin
    TBChk.Visible:=False;
    TBCChk.Visible:=False;
    TBAdd.Visible:=True;
    TBEdit.Visible:=True;
    TBCopy.Visible:=True;
  end;
end;

procedure TfrmProductOrderListNew.TBCloseClick(Sender: TObject);
begin
  Close;
  //WriteCxGrid('无纺布指示单列表',Tv1,'指示单管理');
  WriteCxBandedGrid('无纺布指示单列表',Tv2,'指示单管理');
  WriteCxBandedGrid('无纺布指示单列表DD',Tv3,'指示单管理');
end;

procedure TfrmProductOrderListNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' exec P_WFBOrder_List :begdate,:endate,:MainId');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime));
      Parameters.ParamByName('endate').Value:=Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1));
      Parameters.ParamByName('MainId').Value:='';
      Open;
    end;
    SDofilter(ADOQueryMain,'OrderType=''卷材''');
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    SDofilter(ADOQueryMain,'OrderType=''制品''');
    SCreateCDS20(ADOQueryMain,CDS_ZP);
    SInitCDSData20(ADOQueryMain,CDS_ZP);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmProductOrderListNew.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 TfrmProductOrderListNew.InitForm();
begin
  if SGetServerDate(ADOQueryTemp)>StrToDate('2013-06-19') then
  begin
    ToolBar1.Visible:=False;
    Application.MessageBox('软件需要升级，请联系供应商！','提示',0);
    Exit;
  end;
  ReadCxBandedGrid('无纺布指示单列表',Tv2,'指示单管理');
  ReadCxBandedGrid('无纺布指示单列表DD',Tv3,'指示单管理');
  if FFInt>0 then
  begin
    v1Column3.Hidden:=False;
    v1Column3.Visible:=True;
  end else
  begin
    v1Column3.Hidden:=True;
    v1Column3.Visible:=False;
  end;
  if Trim(DParameters1)='1' then
  begin
    v1CustomerNoName.Visible:=False;
    v1CustomerNoName.Hidden:=True;
  end else
  begin
    v1CustomerNoName.Visible:=True;
    v1CustomerNoName.Hidden:=False;
  end;
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
  {if Trim(DParameters1)='1' then
  begin
    TBChk.Visible:=False;
    TBCChk.Visible:=False;
  end;}
end;

procedure TfrmProductOrderListNew.TBFindClick(Sender: TObject);
var
  fsj:string;
begin
  if ADOQueryMain.Active=False then Exit;
  if Trim(SGetFilters(Panel1,1,2))<>'' then
  begin
    fsj:='OrderType=''卷材'' and '+Trim(SGetFilters(Panel1,1,2));
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    fsj:='OrderType=''制品'' and '+Trim(SGetFilters(Panel1,1,2));
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,CDS_ZP);
    SInitCDSData20(ADOQueryMain,CDS_ZP);
  end else
  begin
    fsj:='OrderType=''卷材''';
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    fsj:='OrderType=''制品''';
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,CDS_ZP);
    SInitCDSData20(ADOQueryMain,CDS_ZP);
  end;
end;

procedure TfrmProductOrderListNew.TBEditClick(Sender: TObject);
begin
  if PPInt=1 then
  begin
    if Order_Main.IsEmpty then Exit;
    if Trim(Order_Main.fieldbyname('ChkStatus').AsString)='审核通过' then
    begin
      Application.MessageBox('此订单已审核通过，不能修改！','提示',0);
      Exit;
    end;
    try
      frmOrderInPutNew:=TfrmOrderInPutNew.Create(Application);
      with frmOrderInPutNew do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        //OrderType:='卷材';
        if ShowModal=1 then
        begin
          Self.InitGrid();
          Self.Order_Main.Locate('MainId',Trim(FMainId),[]);
        end;
      end;
    finally
      frmOrderInPutNew.Free;
    end;
  end else
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then Exit;
    if Trim(CDS_ZP.fieldbyname('ChkStatus').AsString)='审核通过' then
    begin
      Application.MessageBox('此订单已审核通过，不能修改！','提示',0);
      Exit;
    end;
    try
      frmOrderInPutZPLNew:=TfrmOrderInPutZPLNew.Create(Application);
      with frmOrderInPutZPLNew do
      begin
        PState:=1;
        FMainId:=Trim(Self.CDS_ZP.fieldbyname('MainId').AsString);
        OrderType:='制品';
        if ShowModal=1 then
        begin
          Self.InitGrid();
          Self.CDS_ZP.Locate('MainId',Trim(FMainId),[]);
        end;
      end;
    finally
      frmOrderInPutZPLNew.Free;
    end;
  end;
end;

procedure TfrmProductOrderListNew.TBDelClick(Sender: TObject);
begin
  if PPInt=1 then
  begin
    if Order_Main.IsEmpty then Exit;
    if Trim(Order_Main.fieldbyname('Chker').AsString)<>'' then
    begin
      Application.MessageBox('已经审核不能删除订单！','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    if DelData(Order_Main) then
    begin
      //TBRafresh.Click;
      //TBFind.Click;
      Order_Main.Delete;
    end;
  end else
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then Exit;
    if Trim(CDS_ZP.fieldbyname('Chker').AsString)<>'' then
    begin
      Application.MessageBox('已经审核不能删除订单！','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    if DelData(CDS_ZP) then
    begin
      //TBRafresh.Click;
      //TBFind.Click;
      CDS_ZP.Delete;
    end;
  end;


end;

function TfrmProductOrderListNew.DelData(Order_Main10:TClientDataSet):Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete WFBOrder_Main where MainId='''+Trim(Order_Main10.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete WFBOrder_Sub where MainId='''+Trim(Order_Main10.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete WFB_DB where MainId='''+Trim(Order_Main10.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete WFBYCL_PB where MainId='''+Trim(Order_Main10.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete WFBYCL_PBSub where MainId='''+Trim(Order_Main10.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderListNew.TBViewClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  ShowMessage(DCode);
  
end;

procedure TfrmProductOrderListNew.TBExportClick(Sender: TObject);
begin
  if PPInt=1 then
  begin
    if Order_Main.IsEmpty then Exit;
    TcxGridToExcel('指示单列表(卷材)',cxGrid2);
  end else
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then Exit;
    TcxGridToExcel('指示单列表(制品)',cxGrid3);
  end;


end;

procedure TfrmProductOrderListNew.TBPrintClick(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,[]);
  //SelPrintData(TV4,ADOQueryMain,'合同查询报表');
end;

procedure TfrmProductOrderListNew.TBRafreshClick(Sender: TObject);
begin
  if FFInt=1 then
  begin
    InitGridFH();
  end else
    InitGrid();
end;

procedure TfrmProductOrderListNew.TBAddClick(Sender: TObject);
begin
  Panel3.Visible:=True;
end;

procedure TfrmProductOrderListNew.OrderNoChange(Sender: TObject);
var
  fsj:string;
begin
  if ADOQueryMain.Active=False then Exit;
  if Trim(SGetFilters(Panel1,1,2))<>'' then
  begin
    fsj:='OrderType=''卷材'' and '+Trim(SGetFilters(Panel1,1,2));
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    fsj:='OrderType=''制品'' and '+Trim(SGetFilters(Panel1,1,2));
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,CDS_ZP);
    SInitCDSData20(ADOQueryMain,CDS_ZP);
  end else
  begin
    fsj:='OrderType=''卷材''';
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    fsj:='OrderType=''制品''';
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,CDS_ZP);
    SInitCDSData20(ADOQueryMain,CDS_ZP);
  end;
end;

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

procedure TfrmProductOrderListNew.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
 // if FFInt=1 then
    //ModalResult:=1;
 TBCK.Click;
end;

procedure TfrmProductOrderListNew.TBBQPrintClick(Sender: TObject);
var
  fPrintFile:String;
begin
  if PPInt=1 then
  begin
    if Order_Main.IsEmpty then  Exit;
    RMDB_Main.DataSet:=Order_Main;
  end;
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then  Exit;
    RMDB_Main.DataSet:=CDS_ZP;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\订单标签.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\订单标签.rmf'),'提示',0);
  end;
end;

procedure TfrmProductOrderListNew.ToolButton2Click(Sender: TObject);
 var
   fsj:string;
begin
  if PPInt=1 then
  begin
    if Order_Main.IsEmpty then Exit;
    fsj:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
  end else
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then Exit;
    fsj:=Trim(Self.CDS_ZP.fieldbyname('MainId').AsString);
  end else
  begin
    Application.MessageBox('未选中订单!','提示',0);
    Exit;
  end;
  try
    frmOrderAttachment:=TfrmOrderAttachment.Create(Application);
    with frmOrderAttachment do
    begin
      FAMainId:=Trim(fsj);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderAttachment.Free;
  end;
end;

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

procedure TfrmProductOrderListNew.ToolButton4Click(Sender: TObject);
begin
  if Application.MessageBox('确定要执行完成操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate Order_Main Set Status=''完成'' ');
    sql.Add('where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  //InitGrid();
end;

procedure TfrmProductOrderListNew.ToolButton5Click(Sender: TObject);
begin
  if Application.MessageBox('确定要执行完成撤销操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate Order_Main Set Status='''' ');
    sql.Add('where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
end;

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

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

procedure TfrmProductOrderListNew.TBCKClick(Sender: TObject);
begin
  if PPInt=1 then
  begin
    if Order_Main.IsEmpty then Exit;
    try
      frmOrderInPutNew:=TfrmOrderInPutNew.Create(Application);
      with frmOrderInPutNew do
      begin
        PState:=3;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        TBSave.Visible:=False;
        ToolBar2.Visible:=False;
        cxGrid4.Enabled:=False;
        //ScrollBox1.Enabled:=False;
        //ScrollBox2.Enabled:=False;
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmOrderInPutNew.Free;
    end;
  end else
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then Exit;
    try
      frmOrderInPutZPLNew:=TfrmOrderInPutZPLNew.Create(Application);
      with frmOrderInPutZPLNew do
      begin
        PState:=3;
        FMainId:=Trim(Self.CDS_ZP.fieldbyname('MainId').AsString);
        TBSave.Visible:=False;
        ToolBar2.Visible:=False;
        cxGrid4.Enabled:=False;
        //ScrollBox1.Enabled:=False;
        //ScrollBox2.Enabled:=False;
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmOrderInPutZPLNew.Free;
    end;
  end;

end;

procedure TfrmProductOrderListNew.TBChkClick(Sender: TObject);
begin

  if PPInt=1 then
  begin
    if Trim(Order_Main.fieldbyname('ChkStatus').AsString)='审核通过' then
    begin
      if Application.MessageBox('此订单已审核通过，是否再次审核？','提示',32+4)<>IDYES then Exit;
    end;
    if Order_Main.IsEmpty then Exit;
    try
      frmOrderInPutNew:=TfrmOrderInPutNew.Create(Application);
      with frmOrderInPutNew do
      begin
        PState:=2;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        //OrderType:='卷材';
        if ShowModal=1 then
        begin
          Self.InitGrid();
          Self.Order_Main.Locate('MainId',Trim(FMainId),[]);
        end;
      end;
    finally
      frmOrderInPutNew.Free;
    end;
  end else
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then Exit;
    if Trim(CDS_ZP.fieldbyname('ChkStatus').AsString)='审核通过' then
    begin
      if Application.MessageBox('此订单已审核通过，是否再次审核？','提示',32+4)<>IDYES then Exit;
    end;
    try
      frmOrderInPutZPLNew:=TfrmOrderInPutZPLNew.Create(Application);
      with frmOrderInPutZPLNew do
      begin
        PState:=2;
        FMainId:=Trim(Self.CDS_ZP.fieldbyname('MainId').AsString);
        OrderType:='制品';
        if ShowModal=1 then
        begin
          Self.InitGrid();
          Self.CDS_ZP.Locate('MainId',Trim(FMainId),[]);
        end;
      end;
    finally
      frmOrderInPutZPLNew.Free;
    end;
  end;

end;

procedure TfrmProductOrderListNew.TBCChkClick(Sender: TObject);
begin
  if PPInt=1 then
  begin
    if Order_Main.IsEmpty then Exit;
    if Application.MessageBox('确定要撤销审核吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFBOrder_Main Set ChkStatus='''' ');
      sql.Add(' ,Chker='''' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;
    with Order_Main do
    begin
      Edit;
      FieldByName('Chker').Value:='';
      FieldByName('ChkStatus').Value:='';
      Post;
    end;
  end else
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then Exit;
    if Application.MessageBox('确定要撤销审核吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFBOrder_Main Set ChkStatus='''' ');
      sql.Add(', Chker='''' where MainId='''+Trim(CDS_ZP.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;
    with CDS_ZP do
    begin
      Edit;
      FieldByName('Chker').Value:='';
      FieldByName('ChkStatus').Value:='';
      Post;
    end;
  end ;
  
end;
procedure TfrmProductOrderListNew.CopyOrderData(Order_MainFF:TClientDataSet);
var
  FFMainId:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      //sql.Add('exec P_Copy_WFBOrder :MainId,:Filler');
      sql.Add('exec P_Copy_WFBOrder_NoQty :MainId,:Filler');
      Parameters.ParamByName('MainId').Value:=Trim(Order_MainFF.fieldbyname('MainId').AsString);
      Parameters.ParamByName('Filler').Value:=Trim(DName);
      ExecSQL;
    end;
    FFMainId:=Trim(Order_MainFF.fieldbyname('MainId').AsString)+'FZ';
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update WFBYCL_PB Set CRID=(select isnull(Max(A.CRID),(select Max(AA.CRID)  from CK_YCL_Sub AA ');
      SQL.Add(' where AA.YCLCode=WFBYCL_PB.YCLCode) ) ');
      sql.Add(' from CK_YCL_Sub A ');
      sql.Add(' inner join CK_YCL_KC B on B.CRID=A.CRID  ');
      sql.Add('  where A.YCLCode=WFBYCL_PB.YCLCode and B.KCQty>0 )');
      sql.Add(' where MainId='''+Trim(FFMainId)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFBYCL_PB Set YLKC=(select KCQty from CK_YCL_KC A where A.CRID=WFBYCL_PB.CRID)');
      sql.Add(' where MainId='''+Trim(FFMainId)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFBYCL_PB Set YCLYJKC=(select  YJKC=AA.KCQty-isnull((select sum(isnull(YLQty,0)) from WFBYCL_PB WP where WP.CRID=AA.CRID ');
      sql.Add(' and not exists(select * from CK_YCL_Sub CS ');
      sql.Add(' inner join WFBOrder_Main WM on CS.OrderNo=WM.OrderNO');
      sql.Add(' where WM.MainId=WP.MainId and CS.CRID=WP.CRID )  ),0)');
      sql.Add(' FROM  CK_YCL_KC AA where AA.CRID=WFBYCL_PB.CRID)');
      sql.Add(' where MainId='''+Trim(FFMainId)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('复制失败！','提示',0);
  end;
end;
procedure TfrmProductOrderListNew.TBCopyClick(Sender: TObject);
var
  FFMainId:string;
begin

  if PPInt=1 then
  begin
    if Order_Main.IsEmpty then Exit;
    CopyOrderData(Self.Order_Main);
    try
      frmOrderInPutNew:=TfrmOrderInPutNew.Create(Application);
      with frmOrderInPutNew do
      begin
        PState:=1;
        CopyInt:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString)+'FZ';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmOrderInPutNew.Free;
    end;
  end else
  if PPInt=2 then
  begin
    if CDS_ZP.IsEmpty then Exit;
    CopyOrderData(Self.CDS_ZP);
    try
      frmOrderInPutZPLNew:=TfrmOrderInPutZPLNew.Create(Application);
      with frmOrderInPutZPLNew do
      begin
        PState:=1;
        CopyInt:=1;
        FMainId:=Trim(Self.CDS_ZP.fieldbyname('MainId').AsString)+'FZ';
        OrderType:='制品';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmOrderInPutZPLNew.Free;
    end;
  end;

end;

procedure TfrmProductOrderListNew.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel3).perform(WM_SYSCOMMAND, $F012, 0);
end;

procedure TfrmProductOrderListNew.Image2Click(Sender: TObject);
begin
  Panel3.Visible:=False;
end;

procedure TfrmProductOrderListNew.Button1Click(Sender: TObject);
begin
  if RadioGroup1.ItemIndex=0 then
  begin
    try
      frmOrderInPutNew:=TfrmOrderInPutNew.Create(Application);
      with frmOrderInPutNew do
      begin
        PState:=0;
        FMainId:='';
        if ShowModal=1 then
        begin
          Self.InitGrid();
          Self.Order_Main.Locate('MainId',Trim(FMainId),[]);
        end;
      end;
    finally
      frmOrderInPutNew.Free;
    end;
  end else
  begin
    try
      frmOrderInPutZPLNew:=TfrmOrderInPutZPLNew.Create(Application);
      with frmOrderInPutZPLNew do
      begin
        PState:=0;
        FMainId:='';
        OrderType:='制品';
        if ShowModal=1 then
        begin
          Self.InitGrid();
          Self.CDS_ZP.Locate('MainId',Trim(FMainId),[]);
        end;
      end;
    finally
      frmOrderInPutZPLNew.Free;
    end;
  end;
  Panel3.Visible:=False;
end;

procedure TfrmProductOrderListNew.Button2Click(Sender: TObject);
begin
  Panel3.Visible:=False;
end;

procedure TfrmProductOrderListNew.TV2CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if Trim(DParameters1)='审核' then
  begin
    TBChk.Click;
  end else
  begin
    TBEdit.Click;
  end;
end;

procedure TfrmProductOrderListNew.TV2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  PPInt:=1;
  Tv2.Bands[0].Caption:='卷材√';
  Tv3.Bands[0].Caption:='制品';

  Tv3.Bands[0].Styles.Header.TextColor:=clBlack;
  Tv2.Bands[0].Styles.Header.TextColor:=clBlue;
end;

procedure TfrmProductOrderListNew.Tv3MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  PPInt:=2;
  Tv2.Bands[0].Caption:='卷材';
  Tv3.Bands[0].Caption:='制品√';
  Tv2.Bands[0].Styles.Header.TextColor:=clBlack;
  Tv3.Bands[0].Styles.Header.TextColor:=clBlue;
end;

end.
