unit U_BpCklist;

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, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmBpCklist = 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_Color: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1CPTYPE: TcxGridDBColumn;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Color: TcxGridDBColumn;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    v1customerNoname: TcxGridDBColumn;
    cust: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    factoryName: TEdit;
    Label9: TLabel;
    cxTabControl1: TcxTabControl;
    TBcdel: TToolButton;
    v1Column3: TcxGridDBColumn;
    Label10: TLabel;
    orderNo: TEdit;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    tocust: TEdit;
    Label11: TLabel;
    ADOPrint1: TADOQuery;
    RMDB_main: TRMDBDataSet;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    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 TBcdelClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetComboBox();
    procedure SetStatus();
    { Private declarations }
  public
    fCKName: string;
    { Public declarations }
  end;

var
  frmBpCklist: TfrmBpCklist;

implementation

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

procedure TfrmBpCklist.SetStatus();
begin
  TBedit.Visible := false;
  Tbdel.Visible := false;
  tbcdel.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        TBedit.Visible := true;
        Tbdel.Visible := true;
      end;
    1:
      begin
        tbcdel.Visible := true;
      end;
  end;
end;

procedure TfrmBpCklist.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 TfrmBpCklist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin                       
      close;//
      sql.Clear; //case when (select count(mxid) from CK_SXPB_CRMX A where A.crno=B.CRNO and A.CRID=B.CRID and A.crzt='出库')>0 then  crtime else  (select top 1 convert(varchar(100),MxCrtime,23) from CK_SXPB_CRMX C where C.CRNO=B.crno ORDER BY MxCrtime desc) end as crtime,
      sql.Add('select distinct B.crnos, C.MxCrtime  as crtime,C.MxFiller,');
      sql.Add(' B.*,');
      sql.Add('orderNo=(select Top 1 orderNo from JYOrder_Main X where X.MainID=B.MainID ),');
      SQL.Add('CKPS=(select count(mxid) from CK_SXPB_CRMX A where A.crno=B.CRNO and A.CRID=B.CRID and A.crzt=''出库'' ),');
      sql.Add('CKQTY=case when B.QtyUnit=''KG'' then isnull((select sum(isnull(mxqty,0)) from CK_SXPB_CRMX A where A.crno=B.CRNO  and A.CRID=B.CRID and A.crzt=''出库'' ),0)');
      sql.Add(' else isnull((select sum(isnull(mxqty2,0)) from CK_SXPB_CRMX A where A.crno=B.CRNO  and A.CRID=B.CRID and A.crzt=''出库'' ),0) end ');

      sql.Add('from BP_InOut B ');
      SQL.Add('right JOIN CK_SXPB_CRMX C on B.CRID=C.CRID  and B.crno=C.crno');
      sql.Add('where B.CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add('and B.CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add('and C.crzt=''出库'' ');
      if trim(FckName) <> '' then
        sql.Add('and ckName=' + quotedstr(trim(FckName)));
      if cxTabControl1.TabIndex = 0 then
        sql.Add('and valid=''Y'' ');
      if cxTabControl1.TabIndex = 1 then
        sql.Add('and valid=''N'' ');
//      ShowMessage(sql.text);
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
  finally
  end;

end;

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

procedure TfrmBpCklist.FormDestroy(Sender: TObject);
begin
  frmBpCklist := nil;
end;

procedure TfrmBpCklist.TBAddClick(Sender: TObject);
begin
  frmbpCk := TfrmbpCk.create(self);
  with frmbpCk 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;
  TBRafresh.Click;
end;

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

procedure TfrmBpCklist.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1, '布匹仓库');
  SetComboBox();
  SetStatus();
  InitGrid();
end;

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

end;

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

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

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from YF_Money_CR ');
    SQL.Add(' where  mainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
    sql.Add(' and status>=''1'' ');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('应付款没有撤销审核，不能修改出库!', '提示', 0);
      exit;
    end;
  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;  }

  frmbpCk := TfrmbpCk.create(self);
  with frmbpCk 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;
  TBRafresh.Click;
end;

procedure TfrmBpCklist.TBDelClick(Sender: TObject);
var
  YFID, CRID: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from YF_Money_CR ');
    SQL.Add(' where  mainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
    sql.Add(' and status>=''1'' ');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('应付款没有撤销审核，不能作废!', '提示', 0);
      exit;
    end;
  end;
  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('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('update BP_kc set RollNum=A.RollNum+B.RollNum, 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(' update BP_kc SET RollNum=RollNum+(select SUM(RollNUM) from BP_INout X where X.KCID=BP_kc.KCID and X.CRNo=''' + trim(CDS_Main.fieldbyname('CRNO').AsString) + '''  ), ');
      sql.Add('              Qty=Qty+(select SUM(Qty) from BP_INout X where X.KCID=BP_kc.KCID and X.CRNo=''' + trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ) ');
      sql.Add(' where exists(select KCID from BP_INout X where X.KCID=BP_kc.KCID and X.CRNO=''' + trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' )');
      sql.Add('delete  from CP_InOut ');
      SQL.Add('where  fromMainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      sql.Add('delete  from CP_KC ');
      SQL.Add('where  fromMainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');

      sql.Add('update  BP_InOut SET valid=''N''');
      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 TfrmBpCklist.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

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

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

  with AdoPrint1 do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Print_pbCkMd ');
    sql.Add('@inoutNo=' + 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 TfrmBpCklist.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

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

procedure TfrmBpCklist.TBcdelClick(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('delete from BP_InOut where CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      execsql;
    end;
    TBRafresh.Click;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

end.

