unit U_QCRKList;

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, cxLookAndFeels, cxLookAndFeelPainters,
  dxSkinsCore, dxSkinDarkRoom, dxSkinOffice2013White, dxSkinSharpPlus,
  dxSkinSpringTime, dxSkinsDefaultPainters, dxSkinscxPCPainter, cxNavigator,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSilver, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmQCRKList = 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;
    v1MYCode: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    MYCode: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1KgQty: TcxGridDBColumn;
    v1CMYName: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1Qty: TcxGridDBColumn;
    v1GangQty: TcxGridDBColumn;
    labMYType: TLabel;
    CheckBox1: TCheckBox;
    ToolButton1: TToolButton;
    Order_MX: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuery1: TADOQuery;
    Button1: TButton;
    Panel2: TPanel;
    Image2: TImage;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    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 TBAddClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
  private
    canshu1: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmQCRKList: TfrmQCRKList;

implementation

uses
  U_DataLink, U_RTFun, U_QCRKInPut, U_ZDYHelp, U_LLRKInPut_SD, U_QCRKInPut_SD,
  U_QCRKList_CX, U_ZDYCPName, U_QCRKInPut_CPNo, U_QCRKInPut_TJML,
  U_QCRKInPut_YHML;

{$R *.dfm}

procedure TfrmQCRKList.FormDestroy(Sender: TObject);
begin
  frmQCRKList := nil;
end;

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

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

procedure TfrmQCRKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('期初入库RS211', Tv1, '贸易生产管理');
  Close;
end;

procedure TfrmQCRKList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select convert(varchar(10),A.CRTime,120) CRTime,A.MYCode,A.MYName,Sum(A.GangQty) GangQty ');
      sql.Add(',Sum(A.PS) PS,Sum(A.KgQty) KgQty,Sum(A.Qty) Qty');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' where 1=1 ');
      if CheckBox1.Checked = False then
      begin
        sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', now)) + '''');
        sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', now + 1)) + '''');
        BegDate.Visible := false;
        EndDate.Visible := false;
      end
      else
      begin
        sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and A.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');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmQCRKList.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.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.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_MX.DisableControls;
    with Order_MX 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_MX.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_MX.fieldbyname('MYID').AsString) + '''');
          sql.Add('Delete CK_MYSC_CR  where MYId=''' + Trim(Order_MX.fieldbyname('MYId').AsString) + '''');
          ExecSQL;
        end;
        Order_MX.Delete;
      end;
    end;
    Order_MX.EnableControls;

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

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

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

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

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

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

procedure TfrmQCRKList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmQCRKList_CX := TfrmQCRKList_CX.Create(Application);
    with frmQCRKList_CX do
    begin
      frmQCRKList_CX.MYName.Text := Trim(Self.Order_Main.fieldbyname('MYName').AsString);
      frmQCRKList_CX.MYCode.Text := Trim(Self.Order_Main.fieldbyname('MYCode').AsString);
      frmQCRKList_CX.BegDate.Date := Self.Order_Main.fieldbyname('CRTime').AsDateTime;
      frmQCRKList_CX.EndDate.Date := Self.Order_Main.fieldbyname('CRTime').AsDateTime;
      if self.Order_Main.fieldbyname('KgQty').AsFloat = 0 then
      begin
        v1KgQty.Visible := false;
      end
      else
      begin
        v1KgQty.Visible := true;
      end;
      if self.Order_Main.fieldbyname('Qty').AsFloat = 0 then
      begin
        v1Qty.Visible := false;
      end
      else
      begin
        v1Qty.Visible := true;
      end;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmQCRKList_CX.Free;
  end;
end;

procedure TfrmQCRKList.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.N2Click(Sender: TObject);
begin
  if Order_MX.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID=''' + Trim(Order_MX.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.N1Click(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('MYId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut.Free;
  end;
end;

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

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

procedure TfrmQCRKList.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.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('MYId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut.Free;
  end;
end;

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

procedure TfrmQCRKList.TBAddClick(Sender: TObject);
begin
  Panel2.Visible := true;
end;

procedure TfrmQCRKList.SpeedButton1Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmQCRKInPut := TfrmQCRKInPut.Create(Application);
    with frmQCRKInPut do
    begin
      PState := 0;
      FMainId := '';
      FRead := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut.Free;
  end;
end;

procedure TfrmQCRKList.SpeedButton2Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmQCRKInPut_CPNo := TfrmQCRKInPut_CPNo.Create(Application);
    with frmQCRKInPut_CPNo do
    begin
      PState := 0;
      FMainId := '';
      FRead := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut_CPNo.Free;
  end;
end;

procedure TfrmQCRKList.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmQCRKList.Image2Click(Sender: TObject);
begin
  Panel2.Visible := false;
end;

procedure TfrmQCRKList.Button1Click(Sender: TObject);
begin
  frmZDYCPName := TfrmZDYCPName.create(self);
  with frmZDYCPName do
  begin
    flag := 'MYName';
    flagName := '品名';
    V1Code.Visible := true;
    if showmodal = 1 then
    begin
      MYName.text := Trim(ClientDataSet1.fieldbyname('BPName').AsString);
      TBFind.Click;
    end;
    free;
  end;
end;

procedure TfrmQCRKList.SpeedButton3Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmQCRKInPut_CPNo := TfrmQCRKInPut_CPNo.Create(Application);
    with frmQCRKInPut_CPNo do
    begin
      PState := 0;
      FMainId := '';
      FRead := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut_CPNo.Free;
  end;
end;

procedure TfrmQCRKList.SpeedButton4Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmQCRKInPut_TJML := TfrmQCRKInPut_TJML.Create(Application);
    with frmQCRKInPut_TJML do
    begin
      PState := 0;
      FMainId := '';
      FRead := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut_TJML.Free;
  end;
end;

procedure TfrmQCRKList.SpeedButton5Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmQCRKInPut_YHML := TfrmQCRKInPut_YHML.Create(Application);
    with frmQCRKInPut_YHML do
    begin
      PState := 0;
      FMainId := '';
      FRead := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmQCRKInPut_YHML.Free;
  end;
end;

end.

