unit U_QCRKList_SD;

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, cxButtonEdit, cxDropDownEdit, cxPC;

type
  TfrmQCRKList_SD = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    MYCode: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Label1: TLabel;
    Note: TEdit;
    v1GYLXName: TcxGridDBColumn;
    Label5: TLabel;
    GYLXName: TEdit;
    labMYType: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CheckBox1: TCheckBox;
    Label3: TLabel;
    MYColor: TEdit;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxMYColorNo: TcxGridDBColumn;
    cxGangQty: TcxGridDBColumn;
    cxPS: TcxGridDBColumn;
    cxKgQty: TcxGridDBColumn;
    cxQty: TcxGridDBColumn;
    cxNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxMYColor: TcxGridDBColumn;
    Order_MX: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuery1: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmQCRKList_SD: TfrmQCRKList_SD;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_QCRKInPut_SD,U_LLRKInPut_SD;

{$R *.dfm}

procedure TfrmQCRKList_SD.FormDestroy(Sender: TObject);
begin
  frmQCRKList_SD:=nil;
end;

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

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

procedure TfrmQCRKList_SD.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('期初入库RS21',Tv1,'贸易生产管理');
  WriteCxGrid('期初明细入库RS21',Tv2,'贸易生产管理');
  Close;
end;

procedure TfrmQCRKList_SD.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.CRTime,A.MYCode,A.MYName,B.GYLXName,Sum(A.GangQty) GangQty ');
      sql.Add(',Sum(PS) PS,Sum(KgQty) KgQty,Sum(Qty) Qty');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' inner join ML_Manage B on A.MLID=B.MLID');
      sql.Add(' where 1=1 ');
      if CheckBox1.Checked=False then
      begin
        sql.Add(' and CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date))+'''');
        sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
        BegDate.Visible:=false;
        EndDate.Visible:=false;
      end
      else
      begin
        sql.Add(' and CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
        BegDate.Visible:=true;
        EndDate.Visible:=true;
      end;
      sql.Add(' and isnull(CRType,'''')=''手动入库'' ');
      {if Trim(canshu1)<>'高权限' then
      begin
        sql.Add(' and FillerCode='''+Trim(DCode)+'''');
      end; }
      sql.Add('group by A.CRTime,A.MYCode,A.MYName,B.GYLXName');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmQCRKList_SD.InitForm();
begin
  BegDate.Date:=SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('期初入库RS21',Tv1,'贸易生产管理');
  ReadCxGrid('期初明细入库RS21',Tv2,'贸易生产管理');
  InitGrid();
end;

procedure TfrmQCRKList_SD.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 TfrmQCRKList_SD.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID='''+Trim(Order_Main.fieldbyname('MYID').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 TfrmQCRKList_SD.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' insert into CK_MYSC_CR_Log  ');
          sql.Add(' select * from CK_MYSC_CR where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode='''+Trim(DCode)+''',Deler='''+Trim(DName)+'''');
          sql.Add(' where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          sql.Add('Delete CK_MYSC_CR  where MYId='''+Trim(Order_Main.fieldbyname('MYId').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;


    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmQCRKList_SD.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

procedure TfrmQCRKList_SD.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmQCRKList_SD.TBAddClick(Sender: TObject);
var
  maxno:string;
begin
  try
    frmLLRKInPut_SD:=TfrmLLRKInPut_SD.Create(Application);
    with frmLLRKInPut_SD do
    begin
      PState:=0;
      FMainId:='';
      FRead:='';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_SD.Free;
  end;
end;

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

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

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

procedure TfrmQCRKList_SD.ToolButton1Click(Sender: TObject);
begin
  if Order_MX.IsEmpty then Exit;
  try
    frmQCRKInPut_SD:=TfrmQCRKInPut_SD.Create(Application);
    with frmQCRKInPut_SD do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_MX.fieldbyname('MYId').AsString);
      TBSave.Visible:=False;
      FRead:='';
      BtnDel.Visible:=False;
      cxGroupBox1.Visible:=False;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut_SD.Free;
  end;
end;

procedure TfrmQCRKList_SD.CustomerNoNameChange(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 TfrmQCRKList_SD.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID='''+Trim(Order_Main.fieldbyname('MYID').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;

procedure TfrmQCRKList_SD.N1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmQCRKInPut_SD:=TfrmQCRKInPut_SD.Create(Application);
    with frmQCRKInPut_SD do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MYId').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut_SD.Free;
  end;
end;

procedure TfrmQCRKList_SD.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmQCRKList_SD.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmQCRKList_SD.ToolButton4Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Chker').AsString)='' then exit;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检测报告.rmf';
  RMXLSExport2:= TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRId='''+Trim(Order_Main.fieldbyname('TRId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['OrderUnit']:=Order_Main.fieldbyname('OrderUnit').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检测报告.rmf'),'提示',0);
  end;
end;

procedure TfrmQCRKList_SD.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmQCRKInPut_SD:=TfrmQCRKInPut_SD.Create(Application);
    with frmQCRKInPut_SD do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MYId').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut_SD.Free;
  end;
end;


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

procedure TfrmQCRKList_SD.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.GYLXName ');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' inner join ML_Manage B on A.MLID=B.MLID');
      sql.Add(' where 1=1 ');
      SQL.Add(' and A.MYCode='''+trim(Order_Main.fieldbyname('MYCode').AsString)+'''');
      SQL.Add(' and A.MYName='''+trim(Order_Main.fieldbyname('MYName').AsString)+'''');
      SQL.Add(' and B.GYLXName='''+trim(Order_Main.fieldbyname('GYLXName').AsString)+'''');
      if CheckBox1.Checked=False then
      begin
        sql.Add(' and CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date))+'''');
        sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end
      else
      begin
        sql.Add(' and CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      sql.Add(' and isnull(CRType,'''')=''手动入库'' ');
      Open;
    end;
    SCreateCDS20(ADOQuery1,Order_MX);
    SInitCDSData20(ADOQuery1,Order_MX);
  finally;
    ADOQuery1.EnableControls;
  end;
end;

end.


