unit U_CYGGRklist;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmCYGGRklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    P_CodeName: TEdit;
    P_Code: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1CPType: TcxGridDBColumn;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Color: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1INoutNo: TcxGridDBColumn;
    Label7: TLabel;
    ywy: TEdit;
    v1ywy: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    kuwei: TEdit;
    Label8: TLabel;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1editer: TcxGridDBColumn;
    v1editTime: TcxGridDBColumn;
    v1DefStr6: TcxGridDBColumn;
    Label9: TLabel;
    factoryName: TEdit;
    P_Color: TEdit;
    Label10: TLabel;
    v1Column8: TcxGridDBColumn;
    Label11: TLabel;
    P_HX: TEdit;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    v1Column9: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Label12: TLabel;
    FromOrderNo: TEdit;
    btnDelCX: TToolButton;
    btnDelData: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure btnDelDataClick(Sender: TObject);
    procedure btnDelCXClick(Sender: TObject);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
  private
    procedure InitGrid();
    procedure SetComboBox();
    procedure Setstatus();
    { Private declarations }
  public
    fCKName: string;
    canshu1: string;
    { Public declarations }
  end;

var
  frmCYGGRklist: TfrmCYGGRklist;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_CYGGRk;
{$R *.dfm}

procedure TfrmCYGGRklist.SetStatus();
begin
  TBedit.Visible := false;
  Tbdel.Visible := false;
  Tchk.Visible := false;
  Tnochk.Visible := false;
  btnDelData.Visible := false;
  btnDelCX.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        TBedit.Visible := true;
        Tbdel.Visible := true;
        if trim(canshu1) <> '' then
        begin
          Tchk.Visible := true;
        end;
      end;
    1:
      begin
        if trim(canshu1) <> '' then
        begin
          Tnochk.Visible := true;
        end;
      end;
    2:
      begin
     //  tbcdel.Visible:=true;
      end;
    3:
      begin
        btnDelData.Visible := true;
        btnDelCX.Visible := true;
      end;
  end;

end;

procedure TfrmCYGGRklist.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPCK'' ');
    if trim(fCKName) <> '' then
      sql.Add('and zdyName=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count > 0 then
    ckName.ItemIndex := 0;

  CRType.Items.Clear;
  CRType.Items.Add('');
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPRKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count > 0 then
    CRType.ItemIndex := 0;
end;

procedure TfrmCYGGRklist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*  from CYGG_InOut A');

      sql.Add('where CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add('and CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add('and CRFlag=''入库'' ');
      sql.Add('and valid=''Y'' ');

      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add('and valid=''Y'' and A.status=''0'' ');
          end;
        1:
          begin
            sql.Add('and valid=''Y'' and A.status=''1'' ');
          end;
        2:
          begin
            sql.Add('and valid=''Y''  ');
          end;
        3:
          begin
            sql.Add('and valid=''N''  ');
          end;

      end;
      sql.add('order by filltime desc');
//      ShowMessage(SQL.Text);
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
  finally
  end;
end;

procedure TfrmCYGGRklist.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action := cafree;
end;

procedure TfrmCYGGRklist.FormDestroy(Sender: TObject);
begin
  frmcyggRklist := nil;
end;

procedure TfrmCYGGRklist.TBAddClick(Sender: TObject);
begin
  frmCYGGRk := TfrmCYGGRk.create(self);
  with frmCYGGRk do
  begin
    fkeyNo := '';
    fType := 0;
    fckName := self.fckName;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

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

procedure TfrmCYGGRklist.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1, '样品仓库');
  SetComboBox();
  SetStatus();
  InitGrid();
end;

procedure TfrmCYGGRklist.FormCreate(Sender: TObject);
begin
  BegDate.Date := DServerDate - 7;
  EndDate.Date := DServerDate;
  cxGrid1.Align := alclient;
end;

procedure TfrmCYGGRklist.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(self.Caption, Tv1, '样品仓库');
  close;
end;

procedure TfrmCYGGRklist.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  frmCYGGRk := TfrmCYGGRk.create(self);
  with frmCYGGRk do
  begin
    fkeyNo := self.cds_main.fieldbyname('CRNO').AsString;
    fType := 1;
    fckName := self.fckName;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmCYGGRklist.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Application.MessageBox('是否要整单删除？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update  CYGG_InOut SET valid=''N'' ');
      sql.Add(',editer=' + quotedstr(trim(DName)));
      sql.Add(',edittime=getdate()');
      SQL.Add('where CRID=' + quotedstr(CDS_Main.fieldbyname('CRID').AsString));
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_CYGG_Judge_KC ');
      Open;
    end;

    if Trim(ADOQueryCmd.FieldByName('msg').AsString) = '异常' then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('已产生出库数据，不能删除！', '提示', 0);
      exit;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;

    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmCYGGRklist.TBPrintClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmCYGGRklist.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmCYGGRklist.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initGrid();
end;

procedure TfrmCYGGRklist.TchkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CYGG_InOut SET status=''1'' ');
      sql.Add('where CRNO=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
      execsql;
    end;
    application.MessageBox('审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmCYGGRklist.TnochkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  CYGG_InOut SET status=''0'' ');
      sql.Add('where CRNO=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
      execsql;
    end;
    application.MessageBox('撤销审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmCYGGRklist.btnDelDataClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Application.MessageBox('是否要删除数据？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete  CYGG_InOut SET where CRID=' + quotedstr(CDS_Main.fieldbyname('CRID').AsString));
      execsql;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;

    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmCYGGRklist.btnDelCXClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Application.MessageBox('是否撤销删除？', '提示', 32 + 4) <> IDYES then
    Exit;

  try

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;

      sql.Add('update  CYGG_InOut SET valid=''Y'' ');
      sql.Add(',editer=' + quotedstr(trim(DName)));
      sql.Add(',edittime=getdate()');
      SQL.Add('where  CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');

      execsql;
    end;
    TBRafresh.Click;
  except
    Application.MessageBox('数据撤销删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmCYGGRklist.Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -1, -1);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

end.

