unit U_FanYangList;

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
  TFrmFanYangList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    ToolButton1: 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;
    MPRTKuanNO: TEdit;
    MPRTCode: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNO: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1MPRTCode: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DlyDate: TcxGridDBColumn;
    v1CustomerNoName: 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;
    v1CPCFBi: TcxGridDBColumn;
    v1PBFactory: TcxGridDBColumn;
    v1RanFactory: TcxGridDBColumn;
    v1OrdQty1: TcxGridDBColumn;
    v1OrdQty3: TcxGridDBColumn;
    v1OrdQty5: TcxGridDBColumn;
    OrdDefStr1: TEdit;
    Label2: TLabel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQuerySub: TADOQuery;
    DSSUB: TDataSource;
    v1orddefstr7: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    cxTabControl1: TcxTabControl;
    v1Column1: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    ToolButton2: TToolButton;
    v1Column2: TcxGridDBColumn;
    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 ToolButton1Click(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 ToolButton2Click(Sender: TObject);
  private
    procedure initGrid();
    procedure initsub();
    procedure SetStatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmFanYangList: TFrmFanYangList;

implementation

uses U_DataLink,U_FanYangList_Sub,U_Fun;

{$R *.dfm}
procedure TFrmFanYangList.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;
end;
procedure TFrmFanYangList.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 TFrmFanYangList.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.* from JYOrderFy_Main A ');
  //  sql.add('inner join JYOrderFy_Sub B on B.Mainid=A.Mainid ');
    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 TFrmFanYangList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+Tv1.Name,Tv1,'放样指示单录入');
  ReadCxGrid(self.Caption+Tv2.Name,Tv2,'放样指示单录入');
  BegDate.DateTime:=SGetServerDate10(ADOQuerycmd)-30;
  EndDate.DateTime:=SGetServerDate10(ADOQuerycmd);
  SetStatus();
  initGrid();
end;

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

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

procedure TFrmFanYangList.FormDestroy(Sender: TObject);
begin
  FrmFanYangList:=nil;
end;

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

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

procedure TFrmFanYangList.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 TFrmFanYangList.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
    FrmFanYangListSub:=TFrmFanYangListSub.Create(Application);
    with FrmFanYangListSub 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
    FrmFanYangListSub.Free;
  end;
end;

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

procedure TFrmFanYangList.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 trim(order_Main.fieldbyname('Status').AsString)='1' then
  begin
    application.MessageBox('已审核不能删除','提示');
    exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    close;
    sql.clear;
    sql.Add('delete from JYOrderFy_Main where Mainid='''+trim(order_Main.fieldbyname('Mainid').AsString)+''' ');
    sql.Add('delete from JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    execsql;
  end;
  initGrid();
end;

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

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

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

end;

procedure TFrmFanYangList.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 TFrmFanYangList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  initsub();
end;

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

procedure TFrmFanYangList.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 TFrmFanYangList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initgrid();
end;

procedure TFrmFanYangList.ToolButton2Click(Sender: TObject);
begin
 if Order_Main.IsEmpty then Exit;

  try
    FrmFanYangListSub:=TFrmFanYangListSub.Create(Application);
    with FrmFanYangListSub do
    begin
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      CopyInt:=99;
   //   CustomerNo.Enabled:=False;
    //  CustomerNoName.Enabled:=False;
    //  OrdDefStr1.Enabled:=False;
      if ShowModal=1 then
      begin
        initGrid();
      end;
    end;
  finally
    FrmFanYangListSub.Free;
  end;
end;

end.
