unit U_CKPDlist;

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, MovePanel;

type
  TfrmCKPDlist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRTime: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    C_CodeName: TEdit;
    P_Code: TEdit;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1CPTYPE: TcxGridDBColumn;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Color: TcxGridDBColumn;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    v1inoutNO: TcxGridDBColumn;
    kuwei: TEdit;
    Label8: TLabel;
    cxTabControl1: TcxTabControl;
    C_Color: TEdit;
    Label9: TLabel;
    custName: TEdit;
    Label2: TLabel;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    Label5: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    TBPD: TToolButton;
    MovePanel2: TMovePanel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure TBPDClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    fCKName:string;
    canshu1:string;
    { Public declarations }
  end;

var
  frmCKPDlist: TfrmCKPDlist;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp,U_PDSMEdit;
{$R *.dfm}
procedure TfrmCKPDlist.SetStatus();
begin

  Tchk.Visible:=false;
  Tnochk.Visible:=false;
  case cxTabControl1.TabIndex of
    0:begin
       Tchk.Visible:=true;
    //   Tnochk.Visible:=true;
      end;
    1:begin

      end;
  end;
end;

procedure TfrmCKPDlist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*  ');
      sql.Add('from CK_BanCP_PD A');
      sql.Add('where begTime>='''+formatdateTime('yyyy-MM-dd',begdate.Date)+''' ');
      sql.Add('and begTime<'''+formatdateTime('yyyy-MM-dd',enddate.Date+1)+''' ');
      if trim(FckName)<>'' then
      sql.Add('and ckName='+quotedstr(trim(FckName)) );
      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 TfrmCKPDlist.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Caption,Tv1,'成品仓库');
  Action:=cafree;
end;

procedure TfrmCKPDlist.FormDestroy(Sender: TObject);
begin
  frmCKPDlist:=nil;
end;

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

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

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

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

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

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

procedure TfrmCKPDlist.TchkClick(Sender: TObject);
var
  fPDId:string;
begin
  with adoqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add('from CK_BanCP_PD A');
    if trim(FckName)<>'' then
    sql.Add('where ckName='+quotedstr(trim(FckName)) );
    sql.Add('and A.status=0 ');
    open;
  end;
  IF not adoqueryTmp.IsEmpty then
  begin
    application.MessageBox('有未结束的盘点记录，请检查！','提示信息',MB_ICONERROR);
    exit;
  end;

  with adoqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.C_CodeName,A.C_Color,A.KCQtyUnit,B.CKName,COUNT(A.MJID) as RollNum,SUM(KCQty) as Qty  ');
    sql.Add('from CK_BanCP_KC A                                                                             ');
    sql.Add('inner join CK_BanCP_CR B  on B.BCID=A.BCID                                                     ');
    sql.Add('where A.KCQty>0                                                                                ');
    if trim(FckName)<>'' then                                                                              
    sql.Add('and B.ckName='+quotedstr(trim(FckName)) );
    sql.Add('group by A.C_CodeName,A.C_Color,A.KCQtyUnit,B.CKName ');
    sql.Add('order by B.CKName,A.C_CodeName,A.C_Color,A.KCQtyUnit ');
    open;
  end;

  if GetLSNo(ADOQueryCmd,fpdid,'PD','CK_BanCP_PD',3,1)=False then
  begin
    Application.MessageBox('取应收最大号失败!','提示',0);
    Exit;
  end;
  MovePanel2.Left:=(self.Width - MovePanel2 .Width ) div 2;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_PD where 1=2');
      open;
    end;

    while not adoqueryTmp.Eof do
    begin
      with ADOQueryCmd do
      begin
        append;
        fieldbyName('PDID').Value:=fpdid;
        fieldbyName('PDNO').Value:=adoqueryTmp.recNo;
        fieldbyName('CKName').Value:=FckName;
        fieldbyName('C_CodeName').Value:=trim(adoqueryTmp.fieldbyName('C_CodeName').AsString);
        fieldbyName('C_Color').Value:=trim(adoqueryTmp.fieldbyName('C_Color').AsString);
        fieldbyName('RollNum').Value:=adoqueryTmp.fieldbyName('RollNum').AsInteger;
        fieldbyName('Qty').Value:=adoqueryTmp.fieldbyName('Qty').AsFloat;
        fieldbyName('QtyUnit').Value:=trim(adoqueryTmp.fieldbyName('KCQtyUnit').AsString);
        fieldbyName('Filler').Value:=trim(DName);
        post;
      end;
      adoqueryTmp.Next;
    end;

    //说明:  CK_BanCP_KC    PDFlag:  0:初始入库状态，-1，已经出库的库存，1：有库存的数据，2:盘点的数据
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update CK_BanCP_KC SET PDFlag=-1 where KCQty<=0 and ckname='''+trim(FckName)+''' ');
      sql.Add('update CK_BanCP_KC SET PDFlag=1 where KCQty>0 and ckname='''+trim(FckName)+''' ');
      execsql;
    end;
    MovePanel2.Visible:=false;
    ADOQueryCmd.Connection.CommitTrans;
    TBRafresh.Click;
  except
    MovePanel2.Visible:=false;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('生成盘点数据错误!','提示信息',MB_ICONERROR);
  end;


end;

procedure TfrmCKPDlist.TnochkClick(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 CK_BanCP_KC SET PDFlag=0 where PDFlag<>0 ');
      sql.Add('update CK_BanCP_PD SET status=1 where pdid='''+trim(CDS_Main.fieldbyName('pdid').AsString)+''' ');
      execsql;
    end;
    TBRafresh.Click;
  except
    Application.MessageBox('盘点结束失败！','提示信息',0);
  end;
end;

procedure TfrmCKPDlist.TBPDClick(Sender: TObject);
begin
  frmPDSMEdit:=TfrmPDSMEdit.Create(self);
  with frmPDSMEdit do
  begin
    show;
  end;
end;

end.
