unit U_BpckXjList;

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

type
  TfrmBpckXjList = 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;
    cxGrid1Level1: TcxGridLevel;
    v1CRTime: TcxGridDBColumn;
    v1CRNO: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    P_CodeName: TEdit;
    custName: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    ckName: TComboBox;
    Label6: TLabel;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    v1P_Code: TcxGridDBColumn;
    TBsmADD: TToolButton;
    inoutNo: TEdit;
    Label7: TLabel;
    v1custName: TcxGridDBColumn;
    v1TocustName: TcxGridDBColumn;
    TbPrint1: TToolButton;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    CheckBox1: TCheckBox;
    v1Column1: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    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 TBsmADDClick(Sender: TObject);
    procedure TbPrint1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetComboBox();
    { Private declarations }
  public
    fCKName:string;
    { Public declarations }
  end;

var
  frmBpckXjList: TfrmBpckXjList;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp, U_BpCk, U_BpSmCk;
{$R *.dfm}
procedure TfrmBpckXjList.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''CPCK'' ');
    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=''CPCKTYPE''');
    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 TfrmBpckXjList.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.CRNO,A.CRTime,A.CustName,packFee,YBfee,OtherFee,A.IsYjx,A.xjNum,status,SUM(Money) as Money,SUM(Money)+packFee+YBfee+OtherFee as YsXj ');
      sql.Add('from CP_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=''出库'' and IsYjx=''是'' ');
      IF cxTabControl1.TabIndex<2 then
      sql.Add('and  isnull(A.status,''0'')='''+inttostr(cxTabControl1.TabIndex)+''' ');
      if trim(FckName)<>'' then
      sql.Add('and ckName='+quotedstr(trim(FckName)) );
      sql.Add('group by  A.CRNO,A.CRTime,A.CustName,packFee,YBfee,OtherFee,A.IsYjx,A.xjNum,status  ');
      open;
    end;
    SCreateCDS20(adoqueryTmp,CDS_Main);
    SInitCDSData20(adoqueryTmp,CDS_Main);
  finally
  end;
end;

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

procedure TfrmBpckXjList.FormDestroy(Sender: TObject);
begin
  frmBpckXjList:=nil;
end;

procedure TfrmBpckXjList.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;
end;

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

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

end;

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

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

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR A');
    SQL.Add('where A.mainid='''+Trim(CDS_Main.fieldbyname('CRNO').AsString)+''' ');
    sQL.Add('and A.hxNum<>0 ');
    Open;
  end;
  if not ADOQueryCmd.IsEmpty 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;  }

  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;
end;

procedure TfrmBpckXjList.TBDelClick(Sender: TObject);
var
  YFID,CRID:string;
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 YF_Money_CR A');
    SQL.Add('where A.mainid='''+Trim(CDS_Main.fieldbyname('CRNO').AsString)+''' ');
    sQL.Add('and A.hxNum<>0 ');
    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 CP_InOut ');
      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('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)');
      execsql;
    end;
      TBRafresh.Click;
  except
    Application.MessageBox('数据删除失败！','提示信息',0);
  end;
end;

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

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

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

procedure TfrmBpckXjList.TBsmADDClick(Sender: TObject);
begin
  frmbpsmck:=Tfrmbpsmck.create(self);
  with frmbpsmck do
  begin
    fkeyNo:='';
    fType:=0;
    fckName:=self.fckName;
    if showmodal=1 then
    begin
      TBRafresh.Click;
   //   self.CDS_Main.Locate('crNo',trim(fkeyNo),[]) ;
    end;
    TBRafresh.Click;
    free;
  end;
end;

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

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select * from CP_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);
    RMVariables['Filler'] :=trim(CDS_Main.fieldbyname('Filler').AsString);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally

  end;

end;

procedure TfrmBpckXjList.cxTabControl1Change(Sender: TObject);
begin
  Tchk.Visible:=false;
  TNochk.Visible:=false;
  if cxTabControl1.TabIndex=0 then
  begin
    Tchk.Visible:=true;
  end;
  if cxTabControl1.TabIndex=1 then
  begin
    TNochk.Visible:=true;
  end;
InitGrid();

end;

procedure TfrmBpckXjList.TchkClick(Sender: TObject);
begin
 IF CDS_Main.IsEmpty then exit;

  if CDS_Main.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update CP_InOut SET status=''1'' ');
            sql.Add('where CRNO ='+quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmBpckXjList.TnochkClick(Sender: TObject);
begin
 IF CDS_Main.IsEmpty then exit;

  if CDS_Main.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update CP_InOut SET status=''0'' ');
            sql.Add('where CRNO ='+quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmBpckXjList.CheckBox1Click(Sender: TObject);
begin
 IF  CDS_Main.IsEmpty then exit;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=checkbox1.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

end.
