unit U_OrderJSHZView; //27

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxButtonEdit, cxCalendar,
  cxDropDownEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, StdCtrls, ExtCtrls, ComCtrls, ToolWin, DBClient,
  ADODB, cxCheckBox, cxPC, BtnEdit, cxGridDBTableView,
  cxGridCustomPopupMenu, cxGridPopupMenu;

type
  TfrmOrderJSHZView = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label3: TLabel;
    OrderNo: TEdit;
    ClientDataSet6: TClientDataSet;
    DataSource6: TDataSource;
    ADOQuery1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label18: TLabel;
    MPRTCode: TEdit;
    Label19: TLabel;
    MPRTCodeName: TEdit;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label2: TLabel;
    Filler: TEdit;
    cxGrid4: TcxGrid;
    Tv0: TcxGridDBTableView;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    v0Column1: TcxGridDBColumn;
    v0Column2: TcxGridDBColumn;
    v0Column3: TcxGridDBColumn;
    v0Column5: TcxGridDBColumn;
    v0Column6: TcxGridDBColumn;
    Panel2: TPanel;
    CheckBox1: TCheckBox;
    v0Column4: TcxGridDBColumn;
    v0Column7: TcxGridDBColumn;
    v0Column8: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ComboBox1: TComboBox;
    v0Column9: TcxGridDBColumn;
    v0Column10: TcxGridDBColumn;
    v0Column11: TcxGridDBColumn;
    v0Column12: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v0Column13: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure TBFindClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure MPRTCodeChange(Sender: TObject);
    procedure Tv0CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    canshu1:String;
    procedure InitGrid();
    procedure GetData();
  public
    { Public declarations }
  end;

var
  frmOrderJSHZView: TfrmOrderJSHZView;

implementation
uses
  U_DataLink,U_RTFun, U_GYSList,U_ZDYHelp,U_OrdCostList, U_ModuleNote;
{$R *.dfm}

procedure TfrmOrderJSHZView.FormDestroy(Sender: TObject);
begin
  frmOrderJSHZView:=nil;
end;

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

procedure TfrmOrderJSHZView.TBRafreshClick(Sender: TObject);
begin
  Panel2.Visible:=True;
  Panel2.Refresh;
  InitGrid();
  GetData();
  Panel2.Visible:=False;
end;
procedure TfrmOrderJSHZView.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add(' exec P_View_OrderHZDate :begdate,:enddate,'''',:PState,:WSql ');
    Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date));
    Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1));
    Parameters.ParamByName('PState').Value:=ComboBox1.ItemIndex;
    if CheckBox1.Checked then
      Parameters.ParamByName('WSql').Value:=' and Filler in(select UserName from  SY_User where DPID in'+
                                              '(select DPID from SY_User where UserName='''+Trim(DName)+'''))'
    else
      Parameters.ParamByName('WSql').Value:='';
    Open;
  end;
  SCreateCDS20(ADOQueryMain,ClientDataSet6);
  SInitCDSData20(ADOQueryMain,ClientDataSet6);
end;
procedure TfrmOrderJSHZView.GetData();
begin
  ClientDataSet6.DisableControls;
  with ClientDataSet6 do
  begin
    First;
    while not Eof do
    begin
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select Top 1* from JYOrder_Main_MD where ORDMainID='''+Trim(ClientDataSet6.fieldbyname('MainId').AsString)+'''');
        Open;
      end;
      if ADOQuery1.IsEmpty=False then
      begin
        try
          with ADOQueryTemp do
          begin
            Close;
            SQL.Clear;
            //sql.Add(' exec F_View_PSNameSH :MainId');
            sql.Add(' exec P_View_PSNameSH :MainId');
            Parameters.ParamByName('MainId').Value:=Trim(ClientDataSet6.fieldbyname('MainId').AsString);
            Open;
          end;
          Edit;
          FieldByName('HZSH').Value:=ADOQueryTemp.fieldbyname('HZSH').Value;
          FieldByName('HZSHQty').Value:=ADOQueryTemp.fieldbyname('HZSHQty').Value;
          Post;
        except
          Edit;
          FieldByName('HZSH').Value:=NUll;
          FieldByName('HZSHQty').Value:=Null;
          FieldByName('HZNote').Value:='缺少工序码单，遇0除错误。';
          Post;
        end;
      end;

      Next;
    end;
  end;
  ClientDataSet6.EnableControls;
end;
procedure TfrmOrderJSHZView.FormShow(Sender: TObject);
begin
  ReadCxGrid('订单汇总查询',Tv0,'统计查询');
  EndDate.Date:=SGetServerDate(ADOQuery1);
  BegDate.Date:=EndDate.Date-30;
  if Trim(canshu1)='高权限' then
  begin
    ToolButton1.Visible:=True;
    ToolButton2.Visible:=True;
  end;
end;

procedure TfrmOrderJSHZView.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('订单汇总查询',Tv0,'统计查询');
  close;
end;

procedure TfrmOrderJSHZView.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(OrderNo.Text)='' then Exit;
    Panel2.Visible:=True;
    Panel2.Refresh;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' exec P_View_OrderHZDate '''','''',:OrderNo,1,'''' ');
      Parameters.ParamByName('OrderNo').Value:=Trim(OrderNo.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet6);
    SInitCDSData20(ADOQueryMain,ClientDataSet6);
    GetData();
    Panel2.Visible:=False;
  end;

end;

procedure TfrmOrderJSHZView.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter10(ClientDataSet6,SGetFilters(Panel1,1,2));
  //SCreateCDS20(ADOQueryMain,ClientDataSet6);
  //SInitCDSData20(ADOQueryMain,ClientDataSet6);
end;

procedure TfrmOrderJSHZView.OrderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmOrderJSHZView.MPRTCodeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmOrderJSHZView.Tv0CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmOrdCostList:=TfrmOrdCostList.Create(Application);
    with frmOrdCostList do
    begin
      //frmOrdCostList.Align:=alClient;
      frmOrdCostList.orderNo.Text:=Trim(Self.ClientDataSet6.fieldbyname('OrderNo').AsString);
      with frmOrdCostList.ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.MainId,MPRTCode=RTrim(A.MPRTCode)+RTrim(A.MPRTGY),A.MPRTCodeName,A.OrderNo,A.Filler,A.SGJSSH from JYOrder_Main A where ');
        sql.Add(' A.orderno like '''+'%'+Trim(frmOrdCostList.orderNo.Text)+'%'+'''');
        Open;
      end;
      SCreateCDS20(frmOrdCostList.ADOQueryMain,frmOrdCostList.ClientDataSet0);
      SInitCDSData20(frmOrdCostList.ADOQueryMain,frmOrdCostList.ClientDataSet0);
      if Trim(canshu1)='高权限' then
      begin
        Button1.Visible:=True;
      end;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrdCostList.Free;
  end;
end;

procedure TfrmOrderJSHZView.ToolButton1Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  if Application.MessageBox('确定要执行完结操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_Main Set JSFlag=1,JSTime=getdate(),JSPerson='''+Trim(DName)+'''');
    if Trim(ClientDataSet6.FieldByName('HZSH').AsString)<>'' then
    begin
      sql.Add(',HZSH='+ClientDataSet6.FieldByName('HZSH').AsString);
    end else
    begin
      SQL.Add(',HZSH=NUll');
    end;
    if Trim(ClientDataSet6.FieldByName('HZSHQty').AsString)<>'' then
    begin
      sql.Add(',HZSHQty='+ClientDataSet6.FieldByName('HZSHQty').AsString);
    end else
    begin
      SQL.Add(',HZSHQty=NUll');
    end;
    sql.Add(' where MainId='''+Trim(ClientDataSet6.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  with ClientDataSet6 do
  begin
    Edit;
    FieldByName('JSFlag').Value:=True;
    FieldByName('JSPerson').Value:=Trim(DName);
    FieldByName('JSTime').Value:=Now;
    Post;
  end;
end;

procedure TfrmOrderJSHZView.ToolButton2Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  if Application.MessageBox('确定要执行完结撤销操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_Main Set JSFlag=0,JSTime=Null,JSPerson=Null,HZSH=Null,HZSHQty=Null ');
    sql.Add(' where MainId='''+Trim(ClientDataSet6.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  with ClientDataSet6 do
  begin
    Edit;
    FieldByName('JSFlag').Value:=False;
    FieldByName('JSPerson').Value:='';
    FieldByName('JSTime').Value:=Null;
    Post;
  end;
end;

procedure TfrmOrderJSHZView.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

procedure TfrmOrderJSHZView.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='订单汇总分析';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

end.
