unit U_FanYangOther_List;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, Menus, RM_Common,
  RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, DBClient,
  ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxSplitter, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, StdCtrls, ComCtrls, ExtCtrls,
  ToolWin, cxPC;

type
  TfrmFanYangOther_List = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    Tlook: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    OrderNo: TEdit;
    CustomerNoName: TEdit;
    MPRTCodeName: TEdit;
    ConNo: TEdit;
    MPRTspec: TEdit;
    MPRTCode: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNO: TcxGridDBColumn;
    v1MPRTCode: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1OrdDefStr1: TcxGridDBColumn;
    v1OrdDefStr2: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    v1PBFactory: TcxGridDBColumn;
    v1RanFactory: TcxGridDBColumn;
    v1OrdQty1: TcxGridDBColumn;
    OrdDefStr1: TEdit;
    Label2: TLabel;
    ADOQuerySub: TADOQuery;
    DSSUB: TDataSource;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    cxTabControl1: TcxTabControl;
    RMXLSExport1: TRMXLSExport;
    TCopy: TToolButton;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    ToolButton1: TToolButton;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TlookClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBPrintClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Image2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure initGrid();
    procedure initsub();
    procedure SetStatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmFanYangOther_List: TfrmFanYangOther_List;

implementation

uses U_DataLink,U_FanYangList_Other,U_Fun, U_FjList_RZ;

{$R *.dfm}
procedure TfrmFanYangOther_List.SetStatus();
begin
  tbedit.Visible:=false;
  tbdel.Visible:=false;

    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;

  IF DParameters2='查询'then
  begin
    tbADD.Visible:=false;
    tbedit.Visible:=false;
    tbdel.Visible:=false;
    Tlook.Visible:=false;
    Tcopy.Visible:=false;
  end;
end;
procedure TfrmFanYangOther_List.initsub();
begin
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.* from JYOrderFy_Main A ');
    sql.add('inner join JYOrderFy_Sub B on B.Mainid=A.Mainid ');
    sql.Add('where A.MainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));

    open;
  end;
end;

procedure TfrmFanYangOther_List.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.* from JYOrderFy_Other A ');
    sql.Add('where A.OrdDate>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+''' ');
    sql.add('and A.OrdDate<'''+FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1)+''' ');
    //sql.add('and status=''0''');
    if trim(DParameters1)<>'高权限' then
    begin
      sql.add('and filler='''+trim(DName)+''' ');
    end;
//    if cxTabControl1.TabIndex<2 then
//    sql.add(' and status ='''+inttostr(cxTabControl1.TabIndex)+''' ');
    open;
  end;
  SCreateCDS20(ADOQueryTmp,Order_Main);
  SInitCDSData20(ADOQueryTmp,Order_Main);
  TBFind.Click;
end;

procedure TfrmFanYangOther_List.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+Tv1.Name,Tv1,'放样指示单录入');
  BegDate.DateTime:=SGetServerDate10(ADOQuerycmd)-30;
  EndDate.DateTime:=SGetServerDate10(ADOQuerycmd);
  SetStatus();
  initGrid();
end;

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

procedure TfrmFanYangOther_List.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
end;

procedure TfrmFanYangOther_List.FormDestroy(Sender: TObject);
begin
  frmFanYangOther_List:=nil;
end;

procedure TfrmFanYangOther_List.TBAddClick(Sender: TObject);
begin
  try
    FrmFanYangList_Other:=TFrmFanYangList_Other.Create(Application);
    with FrmFanYangList_Other do
    begin
      FMainid:='';
  //    v1SOrdQty1.Options.Editing:=False;
      if ShowModal=1 then
      begin
        initGrid();
      end;
    end;
  finally
    FrmFanYangList_Other.Free;
  end;
end;

procedure TfrmFanYangOther_List.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmFanYangOther_List.TBFindClick(Sender: TObject);
begin
  if ADOQueryTmp.Active=False then Exit;
  SDofilter(ADOQueryTmp,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryTmp,Order_Main);
  SInitCDSData20(ADOQueryTmp,Order_Main);
end;

procedure TfrmFanYangOther_List.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  IF trim(order_Main.fieldbyname('Status').AsString)='1' then
  begin
    application.MessageBox('已审核不能修改','提示');
    exit;
  end;
{  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;  }
  try
    FrmFanYangList_Other:=TFrmFanYangList_Other.Create(Application);
    with FrmFanYangList_Other do
    begin
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
   //   CustomerNo.Enabled:=False;
    //  CustomerNoName.Enabled:=False;
      OrdDefStr1.Enabled:=False;
      if ShowModal=1 then
      begin
        initGrid();
      end;
    end;
  finally
    FrmFanYangList_Other.Free;
  end;
end;

procedure TfrmFanYangOther_List.TlookClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    FrmFanYangList_Other:=TFrmFanYangList_Other.Create(Application);
    with FrmFanYangList_Other do
    begin
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      TBSave.Visible:=False;
      Panel1.Enabled:=False;
      if ShowModal=1 then
      begin
        initGrid();
      end;
    end;
  finally
    FrmFanYangList_Other.Free;
  end;
end;

procedure TfrmFanYangOther_List.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 Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    close;
    sql.clear;
    sql.Add('delete from JYOrderFy_Other where Mainid='''+trim(order_Main.fieldbyname('Mainid').AsString)+''' ');
    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' '+quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(','+quotedstr(trim(self.Caption)));
    sql.Add(','+quotedstr(trim('放样单其他信息删除')));
    sql.Add(','+quotedstr(trim('放样单号：'+trim(Order_Main.FieldByName('OrderNO').AsString))));
    sql.Add(','+quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;
  initGrid();
end;

procedure TfrmFanYangOther_List.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption+Tv1.Name,Tv1,'放样指示单录入');
end;

procedure TfrmFanYangOther_List.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  Tlook.Click;
end;

procedure TfrmFanYangOther_List.TBPrintClick(Sender: TObject);
begin
  Panel4.Visible:=true;

end;

procedure TfrmFanYangOther_List.OrderNoChange(Sender: TObject);
begin
  if ADOQueryTmp.Active=False then Exit;
  SDofilter(ADOQueryTmp,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryTmp,Order_Main);
  SInitCDSData20(ADOQueryTmp,Order_Main);
end;

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

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

procedure TfrmFanYangOther_List.Button1Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if order_Main.IsEmpty then exit;
  with ADOQueryPrint do
  begin
    close;
    sql.clear;
    sql.add('select A.*,B.*, ');
    sql.add('ColorGS=(select count(AA.PRTColor) from ');
    sql.add('(select distinct C.PRTColor From JYOrderFY_Sub C where C.Mainid=A.Mainid) AA),');
    sql.Add('PYZJ=(select Sum(C.PRTOrderQty) from JYOrderFY_Sub C where C.Mainid=A.Mainid),');
    sql.Add('m_SOrdQty1=(select Sum(C.SOrdQty1) from JYOrderFY_Sub C where C.Mainid=A.Mainid),');
    sql.Add('m_SOrdQty2=(select Sum(C.SOrdQty2) from JYOrderFY_Sub C where C.Mainid=A.Mainid),');
    sql.Add('MLMoney=(select Sum(C.PRTOrderQty*PrtPrice) from JYOrderFY_Sub C where C.Mainid=A.Mainid)');
    sql.add('from JYOrderFY_Main A ');
    sql.add('inner join JYOrderFY_Sub B on B.Mainid=A.Mainid ');
    sql.Add('where A.Mainid='''+trim(Order_Main.fieldbyname('Mainid').asstring)+''' ');
    open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  IF RadioGroup1.ItemIndex=2 then
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\放样指示单发票.rmf'
  else
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\放样指示单.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RMVariables['FS'] :=RadioGroup1.ItemIndex;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\放样指示单.rmf'),'提示',0);
  end;
end;

procedure TfrmFanYangOther_List.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initgrid();
end;

procedure TfrmFanYangOther_List.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmFjList_RZ:=TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
    //  PState:=1;
   //   if Trim(canshu1)<>'高权限'  then cxButton1.Visible:=false;
      fkeyNO:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      fType:='放样采购';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFjList_RZ.Free;
  end;
end;

end.
