unit U_QCRKList_CX;

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, Buttons;

type
  TfrmQCRKList_CX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: 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;
    v1PS: TcxGridDBColumn;
    Label34: TLabel;
    v1MYCode: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    MYCode: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1KgQty: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1Qty: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Label1: TLabel;
    KuWei: TEdit;
    Label5: TLabel;
    MXGYLXName: TEdit;
    labMYType: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    CheckBox1: TCheckBox;
    Label3: TLabel;
    MYColorNo: TEdit;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    Order_MX: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuery1: TADOQuery;
    v1MYColor: TcxGridDBColumn;
    v1MYColorNo: TcxGridDBColumn;
    Label4: TLabel;
    MYColor: TEdit;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxStyle3: TcxStyle;
    cxKgQty1: TcxGridDBColumn;
    cxQty1: TcxGridDBColumn;
    V2Filler: TcxGridDBColumn;
    Label6: TLabel;
    Filler: TEdit;
    cxSplitter1: TcxSplitter;
    cxMXGYLXName: TcxGridDBColumn;
    cxKuWei: TcxGridDBColumn;
    cxMPS: TcxGridDBColumn;
    N3: TMenuItem;
    V2filltime: TcxGridDBColumn;
    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 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 MXGYLXNameChange(Sender: TObject);
    procedure N3Click(Sender: TObject);
    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_CX: TfrmQCRKList_CX;

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

{$R *.dfm}

procedure TfrmQCRKList_CX.FormDestroy(Sender: TObject);
begin
  frmQCRKList_CX:=nil;
end;

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

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

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

procedure TfrmQCRKList_CX.InitGrid();
begin
  try
    self.ADOQueryMain.DisableControls;
    with self.ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select convert(varchar(10),A.CRTime,120) CRTime,A.MYCode,A.MYName,A.MYColor,A.MYColorNo,Sum(A.GangQty) GangQty ');
      sql.Add(',Sum(MXKCPiQty) PS,Sum(MXKCKgQty) KgQty,Sum(MXKCQty) Qty');
      sql.Add(' from CK_MYSC_CR A ');
      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,'''')=''期初入库'' ');
      sql.Add('group by convert(varchar(10),A.CRTime,120),A.MYCode,A.MYName,A.MYColor,A.MYColorNo');
      //ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(self.ADOQueryMain,self.Order_Main);
    SInitCDSData20(self.ADOQueryMain,self.Order_Main);
    TBFind.Click;
  finally
    self.ADOQueryMain.EnableControls;
  end;
end;


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

end;

procedure TfrmQCRKList_CX.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_CX.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 RKNO='''+Trim(Order_Main.fieldbyname('RKNO').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_CX.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_MX.DisableControls;
    with Order_MX 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 RKNo='''+Trim(Order_MX.fieldbyname('RKNo').AsString)+''' ');
        sql.Add(' and isnull(CRType,'''')=''期初入库'' ');
        sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode='''+Trim(DCode)+''',Deler='''+Trim(DName)+'''');
        sql.Add(' where RKNo='''+Trim(Order_MX.fieldbyname('RKNo').AsString)+'''');
        sql.Add(' Delete CK_MYSC_CR where RKNo='''+Trim(Order_MX.fieldbyname('RKNo').AsString)+'''');
        sql.Add(' and isnull(CRType,'''')=''期初入库'' ');
        ExecSQL;
      end;
      Order_MX.Delete;
    end;
    Order_MX.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

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

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

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

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

procedure TfrmQCRKList_CX.ToolButton1Click(Sender: TObject);
begin
  if Order_MX.IsEmpty then Exit;
  if Order_MX.FieldByName('YICeng').AsString='' then
  begin
    try
      frmQCRKInPut:=TfrmQCRKInPut.Create(Application);
      with frmQCRKInPut do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_MX.fieldbyname('RKNO').AsString);
        TBSave.Visible:=False;
        FRead:='';
        BtnDel.Visible:=False;
        cxGroupBox1.Visible:=False;
        if self.cxKgQty1.Visible=True then
        begin
          UnitKG.Text:='√';
        end
        else
        begin
          UnitKG.Text:='';
        end;
        if cxQty1.Visible=True then
        begin
          UnitM.Text:='√';
        end
        else
        begin
          UnitM.Text:='';
        end;
        if ShowModal=1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmQCRKInPut.Free;
    end;
  end
  else
  begin
    try
      frmQCRKInPut_CPNo:=TfrmQCRKInPut_CPNo.Create(Application);
      with frmQCRKInPut_CPNo do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_MX.fieldbyname('RKNO').AsString);
        TBSave.Visible:=False;
        FRead:='';
        BtnDel.Visible:=False;
        cxGroupBox1.Visible:=False;
        if self.cxKgQty1.Visible=True then
        begin
          UnitKG.Text:='√';
        end
        else
        begin
          UnitKG.Text:='';
        end;
        if cxQty1.Visible=True then
        begin
          UnitM.Text:='√';
        end
        else
        begin
          UnitM.Text:='';
        end;
        if ShowModal=1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmQCRKInPut_CPNo.Free;
    end;
  end;
end;

procedure TfrmQCRKList_CX.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_CX.N2Click(Sender: TObject);
begin
  if Order_MX.IsEmpty then Exit;
  if Order_MX.FieldByName('PS').AsFloat<>0 then
  begin
    Application.MessageBox('已产生明细数据，不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin

  end;
end;

procedure TfrmQCRKList_CX.N1Click(Sender: TObject);
begin
  if Order_MX.IsEmpty then Exit;
  if Order_MX.FieldByName('YICeng').AsString='' then
  begin
    if Pos('W',Order_Main.FieldByName('MYCode').AsString)=1 then
    begin
      try
        frmQCRKInPut_SD:=TfrmQCRKInPut_SD.Create(Application);
        with frmQCRKInPut_SD do
        begin
          PState:=1;
          FMainId:=Trim(Self.Order_MX.fieldbyname('RKNo').AsString);
          cxGroupBox1.Visible:=false;
          TBSave.Caption:='修改保存';
          Panel6.PopupMenu:=nil;
          //Button3.Visible:=false;
          if ShowModal=1 then
          begin
            InitGrid();
          end;
        end;
      finally
        frmQCRKInPut_SD.Free;
      end;
    end
    else
    begin
      try
        frmQCRKInPut:=TfrmQCRKInPut.Create(Application);
        with frmQCRKInPut do
        begin
          PState:=1;
          FMainId:=Trim(Self.Order_MX.fieldbyname('RKNo').AsString);
          cxGroupBox1.Visible:=false;
          TBSave.Caption:='修改保存';
          //Button3.Visible:=false;
          if ShowModal=1 then
          begin
            InitGrid();
          end;
        end;
      finally
        frmQCRKInPut.Free;
      end;
    end;
  end
  else
  begin
    try
      frmQCRKInPut_CPNo:=TfrmQCRKInPut_CPNo.Create(Application);
      with frmQCRKInPut_CPNo do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_MX.fieldbyname('RKNo').AsString);
        cxGroupBox1.Visible:=false;
        TBSave.Caption:='修改保存';
        Panel6.PopupMenu:=nil;
        //Button3.Visible:=false;
        if ShowModal=1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmQCRKInPut_CPNo.Free;
    end;
  end;
end;

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

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

procedure TfrmQCRKList_CX.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_CX.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmQCRKInPut:=TfrmQCRKInPut.Create(Application);
    with frmQCRKInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('RKNO').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut.Free;
  end;
end;


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

procedure TfrmQCRKList_CX.MXGYLXNameChange(Sender: TObject);
begin
  if ADOQuery1.Active=False then Exit;
  SDofilter(ADOQuery1,SGetFilters(Panel1,3,4));
  SCreateCDS20(ADOQuery1,Order_MX);
  SInitCDSData20(ADOQuery1,Order_MX);
end;

procedure TfrmQCRKList_CX.N3Click(Sender: TObject);
begin
  if Order_MX.IsEmpty then Exit;
  try
    frmQCRKInPut:=TfrmQCRKInPut.Create(Application);
    with frmQCRKInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_MX.fieldbyname('RKNo').AsString);
      //Button3.Visible:=false;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut.Free;
  end;
end;

procedure TfrmQCRKList_CX.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 B.MXGYLXName,A.Filler,A.Filltime,A.RKNo,Count(distinct B.RCGangNo) GangQty,Count(B.JH) PS');
      sql.Add(',Sum(B.KgQty) MXKGQty,Sum(B.MQty) MXMQty,B.KuWei,B.MXNote,YICeng,ERCeng,SanCeng,SICeng,WUCeng,LIUCeng');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' inner join CK_MYSC_CR_MX B on B.MYId=A.MYId');
      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 isnull(A.MYColor,'''')='''+trim(Order_Main.fieldbyname('MYColor').AsString)+'''');
      SQL.Add(' and isnull(A.MYColorNo,'''')='''+trim(Order_Main.fieldbyname('MYColorNo').AsString)+'''');
      if Order_Main.IsEmpty=false then
      sql.Add(' and convert(varchar(10),A.CRTime,120)='''+trim(FormatDateTime('yyyy-MM-dd',Order_Main.fieldbyname('CRTime').AsDateTime))+'''');
      sql.Add(' and isnull(CRType,'''')=''期初入库'' ');
      SQL.Add(' group by B.MXGYLXName,A.Filler,A.Filltime,A.RKNo,B.KuWei,B.MXNote,YICeng,ERCeng,SanCeng,SICeng,WUCeng,LIUCeng');
      //showmessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQuery1,Order_MX);
    SInitCDSData20(ADOQuery1,Order_MX);
    if Order_Main.fieldbyname('KgQty').AsFloat=0 then
    begin
      cxKgQty1.Visible:=false;
    end
    else
    begin
      cxKgQty1.Visible:=true;
    end;
    if Order_Main.fieldbyname('Qty').AsFloat=0 then
    begin
      cxQty1.Visible:=false;
    end
    else
    begin
      cxQty1.Visible:=true;
    end;
  finally;
    ADOQuery1.EnableControls;
  end;
end;

end.


