unit U_BpDYlist;

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, RM_Common,
  RM_Class, RM_e_Xls, RM_GridReport, RM_System, RM_Dataset, cxPC;

type
  TfrmBpDYlist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: 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;
    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;
    v1P_Color: TcxGridDBColumn;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    v1P_Code: TcxGridDBColumn;
    v1inoutNO: TcxGridDBColumn;
    ywy: TEdit;
    Label7: TLabel;
    v1custName: TcxGridDBColumn;
    TbPrint1: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    P_Color: TEdit;
    Label9: TLabel;
    custName: TEdit;
    Label2: TLabel;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    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 TBExportClick(Sender: TObject);
    procedure TbPrint1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    
    { Private declarations }
  public
    fCKName: string;
    canshu1: string;
    { Public declarations }
  end;

implementation

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

procedure TfrmBpDYlist.SetStatus();
begin
  TBAdd.Visible := false;
  TBedit.Visible := false;
  Tbdel.Visible := false;
  Tchk.Visible := false;
  Tnochk.Visible := false;
  if trim(canshu1) = '录入' then
  begin
    TBAdd.Visible := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          TBedit.Visible := true;
          Tbdel.Visible := true;
        end;
      1:
        begin
     //  tbcdel.Visible:=true;
        end;
    end;
  end
  else if trim(canshu1) = '管理' then
  begin
    TBAdd.Visible := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          TBedit.Visible := true;
          Tbdel.Visible := true;
          Tchk.Visible := true;

        end;
      1:
        begin
          Tnochk.Visible := true;
        end;
    end;
  end;
end;

procedure TfrmBpDYlist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*from YP_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'' ');
      SQL.Add('and crtype=''寄样'' ');
      if cxTabControl1.TabIndex < 2 then
        sql.Add('and A.status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
  finally
  end;
end;

procedure TfrmBpDYlist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption, Tv1, '成品仓库');
  Action := cafree;
end;

procedure TfrmBpDYlist.TBAddClick(Sender: TObject);
begin
  frmbpDY := TfrmbpDY.create(self);
  with frmbpDY do
  begin
    fkeyNo := '';
    fType := 0;
    fckName := self.fckName;
    canshu1 := self.canshu1;
    if trim(self.canshu1) = '录入' then
    begin
      ywy.text := trim(Dname);
      ywy.Enabled := false;
    end;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmBpDYlist.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 TfrmBpDYlist.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1, '成品仓库');
  SetStatus();
  InitGrid();
end;

procedure TfrmBpDYlist.FormCreate(Sender: TObject);
begin

  EndDate.Date := SGetServerDate(ADOQueryTmp);
  BegDate.Date := EndDate.Date - 7;
end;

procedure TfrmBpDYlist.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmBpDYlist.TBEditClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
//  with ADOQueryCmd do
//  begin
//    Close;
//    SQL.Clear;
//    SQL.Add(' select 1 from YF_Money_CR ');
//    SQL.Add(' where status=''1'' and CRTime=''' + CDS_Main.FieldByName('CRTime').AsString + ''' ');
//    SQL.Add(' and MainId=''' + Trim(CDS_Main.FieldByName('CRNO').AsString) + ''' ');
//    SQL.Add(' and SubId=''' + Trim(CDS_Main.FieldByName('CRID').AsString) + ''' ');
//    Open;
//  end;
//  if not ADOQueryCmd.IsEmpty then
//  begin
//    Application.MessageBox('应收款数据已审核，不能修改！', '提示', 0);
//    Exit;
//  end;

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

procedure TfrmBpDYlist.TBDelClick(Sender: TObject);
var
  YFID, CRID: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
//  with ADOQueryCmd do
//  begin
//    Close;
//    SQL.Clear;
//    SQL.Add(' select 1 from YF_Money_CR ');
//    SQL.Add(' where status=''1'' and CRTime=''' + CDS_Main.FieldByName('CRTime').AsString + ''' ');
//    SQL.Add(' and MainId=''' + Trim(CDS_Main.FieldByName('CRNO').AsString) + ''' ');
//    SQL.Add(' and SubId=''' + Trim(CDS_Main.FieldByName('CRID').AsString) + ''' ');
//    Open;
//  end;
//  if not ADOQueryCmd.IsEmpty then
//  begin
//    Application.MessageBox('应收款数据已审核，不能删除！', '提示', 0);
//    Exit;
//  end;

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

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;

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

      sql.Add('delete from YF_money_CR  ');
      SQL.Add('where mainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      SQL.Add('and subID=''' + Trim(CDS_Main.fieldbyname('CRID').AsString) + ''' ');

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

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

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

procedure TfrmBpDYlist.TbPrint1Click(Sender: TObject);
var
  filepath: string;
begin
  if CDS_Main.IsEmpty then
    exit;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add(' select A.*   ');
    sql.Add(' from YP_INout A ');
    sql.Add(' where A.inoutNo=' + quotedstr(trim(CDS_Main.fieldbyname('inoutNO').AsString)));
    open;
  end;

  try
    filepath := ExtractFilePath(Application.ExeName) + 'report\寄样单.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RMVariables['dtxz'] := FormatDateTime('yyyy-mm-dd', date());
    RMVariables['zdr'] := trim(DName);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally

  end;

end;

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

procedure TfrmBpDYlist.TchkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  YP_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 TfrmBpDYlist.TnochkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  YP_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;

end.

