unit U_BpGdCklist;

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

type
  TfrmBpGdCklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    v1CRNO: TcxGridDBColumn;
    v1CRID: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    P_CodeName: TEdit;
    P_Color: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Color: TcxGridDBColumn;
    v1cust: TcxGridDBColumn;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    cust: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    TOK: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(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 TOKClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    procedure InitGrid();
    procedure SetComboBox();
    { Private declarations }
  public
    fCKName:string;
    fManage:string;
    { Public declarations }
  end;

var
  frmBpGdCklist: TfrmBpGdCklist;

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

procedure TfrmBpGdCklist.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPCK'' ');
    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=''BPCKTYPE''');
    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 TfrmBpGdCklist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from BP_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=''出库'' ');
      if trim(FckName)<>'' then
      sql.Add('and ckName='+quotedstr(trim(FckName)) );
      open;
    end;
    SCreateCDS20(adoqueryTmp,CDS_Main);
    SInitCDSData20(adoqueryTmp,CDS_Main);
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select COUNT(distinct CRNo) as CRNo from BP_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=''出库'' ');
      if trim(FckName)<>'' then
      sql.Add('and ckName='+quotedstr(trim(FckName)) );
      open;
      IF IsEmpty then
      begin
        Label8.Caption:='出库单数：0';
      end
      else
      begin
        Label8.Caption:='出库单数：'+Fieldbyname('CRNo').AsString;
      end;
    end;
  finally
  end;
end;

procedure TfrmBpGdCklist.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Caption,Tv1,'布匹仓库');
  Action:=cahide;
end;

procedure TfrmBpGdCklist.FormDestroy(Sender: TObject);
begin
  frmBpGdCklist:=nil;
end;

procedure TfrmBpGdCklist.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 TfrmBpGdCklist.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption,Tv1,'布匹仓库');

  InitGrid();
end;

procedure TfrmBpGdCklist.FormCreate(Sender: TObject);
begin
  BegDate.Date:=date();
  EndDate.Date:=date();
  cxGrid1.Align:=alclient;

end;

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

procedure TfrmBpGdCklist.TBEditClick(Sender: TObject);
begin
  if cds_main.IsEmpty then exit;

 { with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BP_InOut A');
    SQL.Add('where A.kcid='''+Trim(CDS_Main.fieldbyname('kcid').AsString)+''' ');
    sQL.Add('and A.CRFlag=''出库''');
    Open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('已产生出库数据，不能修改！','提示',0);
    Exit;
  end;  }


end;

procedure TfrmBpGdCklist.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
 { if Trim(CDS_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end; }
{  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BP_InOut A');
    SQL.Add('where A.kcid='''+Trim(CDS_Main.fieldbyname('kcid').AsString)+''' ');
    sQL.Add('and A.CRFlag=''出库''');
    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 Contract_Cloth_DH set DHPS=A.DHPS - B.RollNum, DHQty=A.DHQty - B.Qty ');
      sql.Add('from  Contract_Cloth_DH  A');
      sql.Add('inner join BP_INout B on cast(B.kcid as varchar)=A.SXDHID and rtrim(B.FirstName)=rtrim(A.FirstName)');
      sql.Add('where B.CRNO='''+Trim(CDS_Main.fieldbyname('CRNO').AsString)+''' ');
      sql.Add('and B.CRType=''加工出库'' ');           }



      sql.Add('delete  from YF_Money_CR ');
      SQL.Add('where  maiNID='''+Trim(CDS_Main.fieldbyname('CRNO').AsString)+''' ');

      sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
      sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
      sql.Add(' where YFDefFlag1=1 ');

      sql.Add('update BP_kc set RollNum=1, Qty=A.Qty + B.Qty ');
      sql.Add('from  BP_kc  A');
      sql.Add('inner join BP_INout B on B.kcid=A.kcid');
      sql.Add('where B.CRNO='''+Trim(CDS_Main.fieldbyname('CRNO').AsString)+''' ');

      sql.Add('delete  from BP_InOut ');
      SQL.Add('where  CRNO='''+Trim(CDS_Main.fieldbyname('CRNO').AsString)+''' ');
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' '+quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(','+quotedstr(trim(self.Caption)));
      sql.Add(','+quotedstr(trim('出库单删除')));
      sql.Add(','+quotedstr(trim('出库单号：'+trim(CDS_Main.FieldByName('CRNO').AsString))));
      sql.Add(','+quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
      TBRafresh.Click;
  except
    Application.MessageBox('数据删除失败！','提示信息',0);
  end;
end;

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

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

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Do_PrintMd ');
    sql.Add('@mainID='+quotedstr(trim(CDS_Main.fieldbyname('cRNo').AsString)));
    sql.Add(',@flag='+quotedstr(trim('广东出库')));
    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);
    RMVariables['Filler'] :=trim(CDS_Main.fieldbyname('Filler').AsString);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally

  end;

end;

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

procedure TfrmBpGdCklist.TOKClick(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  ModalResult:=1;
end;

procedure TfrmBpGdCklist.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  tok.Click;
end;

end.
