unit U_CKProductBCPOutList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxCheckBox, Menus, MovePanel, RM_E_llPDF, cxCalendar, Math,
  Clipbrd, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
      
  dxSkinDarkRoom,  
     
     
     
    
    
    
   dxSkinOffice2013White,  
    dxSkinSharpPlus, 
  dxSkinSpringTime,   
  dxSkinsDefaultPainters,   
   dxSkinscxPCPainter, BtnEdit;

type
  TfrmCKProductBCPOutList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    Label4: TLabel;
    MLCodeName: TEdit;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label5: TLabel;
    MLOrderNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label7: TLabel;
    v1Column3: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CkOrdNo: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    TBCKCX: TToolButton;
    v1Column11: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    v1Column12: TcxGridDBColumn;
    DS_JuanPRT: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label11: TLabel;
    v1Column15: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    v1rkplace: TcxGridDBColumn;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    RM1: TRMGridReport;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    Label22: TLabel;
    CXKH: TEdit;
    Label24: TLabel;
    CRType: TComboBox;
    CPType: TComboBox;
    Panel6: TPanel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    IsJYTime: TCheckBox;
    MLColor: TEdit;
    ganghao: TEdit;
    ToolButton2: TToolButton;
    ToolButton1: TToolButton;
    LBCPAP: TLabel;
    Label1: TLabel;
    v1Column13: TcxGridDBColumn;
    Label8: TLabel;
    MLConNo: TEdit;
    Panel4: TPanel;
    Label12: TLabel;
    MDName: TBtnEditA;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQueryPrint1: TADOQuery;
    RMDBHZ1: TRMDBDataSet;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure MLOrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure BTNPClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure CPTypeChange(Sender: TObject);
    procedure CRTypeChange(Sender: TObject);
    procedure edtSMNOKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure MDNameBtnClick(Sender: TObject);
  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

//var
//  frmCKProductBCPOutList: TfrmCKProductBCPOutList;

implementation

uses
  U_DataLink, U_RTFun, U_SysLogHelp, U_Fun, U_ZDYHelp;


{$R *.dfm}

procedure TfrmCKProductBCPOutList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmCKProductBCPOutList.FormCreate(Sender: TObject);
begin
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 20;
end;

procedure TfrmCKProductBCPOutList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  Pwhere := SGetFilters(Panel1, 1, 2);
  if IsJYTime.Checked then
  begin
    fwhere := ' where CRTime>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    fwhere := fwhere + ' and CRTime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
    if trim(Pwhere) <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere);
  end
  else
  begin
    if trim(Pwhere) <> '' then
      fwhere := fwhere + ' where ' + trim(Pwhere);
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select top ' + inttostr(RecordsNumber) + ' *  from  ');
      sql.Add('(select rownumber=cast((row_number() over(order by AA.filltime desc)) as int) ');
      sql.Add(',TotalCount=cast((COUNT(1) OVER()) AS int)');
      sql.Add(',* from (select A.*  ');
      sql.Add(' ,B.MLConNo,B.MLOrderNo,B.KHName,B.MLCodeName,B.OrdDate,CXKH=dbo.getPinYin(B.KHName)+B.KHName  ');
      sql.Add(' ,C.MLColor,C.MLColorNo,C.MLHX');
      sql.Add(' ,SHDanWei=( select SHDanWei from FHSQ_Main X where FSId=A.CKOrdNo)');
      sql.Add('  from CK_MLCP_CR A  inner join ML_OrderMainNew B on A.MainId=B.NewMLID inner join ML_OrderSubidNew C on A.SubId=C.NewSubid ');
      sql.Add(' where  A.CRFlag=''出库'' ) AA');
      sql.Add(fwhere + ' ) temp_row');
      sql.Add(' where rownumber> ' + inttostr((CurrentPage - 1) * RecordsNumber));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;

end;

procedure TfrmCKProductBCPOutList.TBRafreshClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmCKProductBCPOutList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('成品出库管理YX' + canshu1, Tv1, '成品仓库');
  Close;
end;

procedure TfrmCKProductBCPOutList.FormShow(Sender: TObject);
begin
  ReadCxGrid('成品出库管理YX' + canshu1, Tv1, '成品仓库');
  CurrentPage := 1;
  RecordsNumber := 500;
  if Trim(canshu1) = '管理' then
  begin
    TBCKCX.Visible := True;
  end
  else
  begin
    TBCKCX.Visible := false;
  end;
end;

procedure TfrmCKProductBCPOutList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '');
end;

procedure TfrmCKProductBCPOutList.N1Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, True);
end;

procedure TfrmCKProductBCPOutList.N2Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, False);
end;

procedure TfrmCKProductBCPOutList.TBCKCXClick(Sender: TObject);
var
  FFMainId, FPrice, strsql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      while CDS_Main.Locate('SSel', True, []) do
      begin

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select CKNO from CK_YCL_CR_Main where CKNo=' + quotedstr(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString)));
          open;
        end;

        if ADOQueryCmd.RecordCount > 0 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          MovePanel2.Visible := False;
          CDS_Main.EnableControls;
          Application.MessageBox('已经销售记录不能撤销', '提示', 0);
          Exit;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_MLCP_CR Set CRFlag=''待出库'',CRType='''',CRTime=NULL,CKOrdNo='''' ');
          sql.Add(' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');
          sql.Add(' and CRFlag=''出库''');

          sql.Add(' UPdate CK_MLCP_KC Set KCValid=''Y'' ');
          SQL.Add(' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');

          sql.Add(' Update JTJY_New Set RKType=''已入库'' where JYID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + ''' ');

          ExecSQL;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec P_Update_OrderSub :SubId');
          Parameters.ParamByName('SubId').Value := Trim(CDS_Main.fieldbyname('SubId').AsString);
          ExecSQL;
        end;

        strsql := '卷ID：' + trim(CDS_Main.FieldByName('MJID').AsString) + ' 出库单号：' + trim(CDS_Main.FieldByName('CKOrdNo').AsString);

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          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(strsql)));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          ExecSQL;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Do_FHSQ @CKOrdNo=' + quotedstr(trim(CDS_Main.FieldByName('CKOrdNo').AsString)));
          ExecSQL;
        end;
        CDS_Main.Delete;

      end;
    end;
    CDS_Main.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible := False;
    Exit;
  except
    MovePanel2.Visible := False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！', '提示', 0);
  end;
end;

procedure TfrmCKProductBCPOutList.MLOrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmCKProductBCPOutList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmCKProductBCPOutList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmCKProductBCPOutList.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.CPTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.CRTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.edtSMNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

  end;
end;

procedure TfrmCKProductBCPOutList.ToolButton2Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmCKProductBCPOutList.ToolButton1Click(Sender: TObject);
var
  fPrintFile, FCKNO: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
 { if CustomerNoName.Text = '' then
  begin
    application.MessageBox('查询条件客户不能为空', '提示');
    exit;
  end; }
  if CDS_Main.Locate('ssel', True, []) = false then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  if Trim(MDName.Text) = '' then
  begin
    Application.MessageBox('码单名称不能为空!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  {if CheckBox1.Checked=False then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\销售码单新.rmf';
  end else
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\销售码单新无单价.rmf';
  end;     }
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(MDName.Text) + '.rmf';

  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  FCKNO := '';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TbForBatch where DName=' + quotedstr(Trim(DName)));
    SQL.Add('and MainType=' + quotedstr('样品码单'));
    ExecSQL;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if FieldByName('Ssel').AsBoolean = true then
      begin
        if Trim(FCKNO) = '' then
        begin
          FCKNO := Trim(CDS_Main.fieldbyname('CKOrdNo').AsString);
        end
        else
        begin
          if FCKNO <> Trim(CDS_Main.fieldbyname('CKOrdNo').AsString) then
          begin
            if Trim(CDS_Main.fieldbyname('FeeName').AsString) <> '单面料' then
            begin
              Application.MessageBox('不能选择不同出库单号!', '提示', 0);
              Exit;
            end;
          end;
        end;
        with ADOQuery1 do
        begin
          Filtered := False;
          Close;
          sql.Clear;
          sql.Add(' select distinct D.MJID');
         // sql.Add(' from CK_YCL_CR A ');
          sql.Add(' from CK_MLCP_CR D ');
         // sql.Add(' inner join CK_YCL_CR_Main B on A.SMID=B.SMID ');
         // sql.Add(' left join CK_MLCP_CR D on D.CKOrdNo=B.CKNo');
         // sql.add(' where B.CKNO=''' + Trim(FCKNO) + '''');
           sql.add(' where D.CKOrdNo=''' + Trim(FCKNO) + '''');
         // ShowMessage(SQL.Text);
          Open;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('SELECT * FROM TBSubID where 1=2 ');
          open;
        end;
        with ADOQuery1 do
        begin
          first;
          while not eof do
          begin
            ADOQueryCmd.append;
            ADOQueryCmd.fieldbyname('SubId').Value := Trim(ADOQuery1.fieldbyname('MJID').AsString);
            ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
            ADOQueryCmd.post;
            next;
          end;
        end;

//        if Trim(CDS_Main.fieldbyname('FeeName').AsString) = '单面料' then
//        begin
//          with ADOQuery2 do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add(' insert into TbForBatch(DName,MainType,Value1,Filltime) ');
//            sql.Add(' values( ' + quotedstr(Trim(DName)) + ',' + quotedstr('样品码单') + ',' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)) + ',getdate()' + ')');
//            ExecSQL;
//          end;
//        end;
      end;
      next;
    end;
  end;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD4 ');
    sql.add('@mainID=' + quotedstr(Trim('')));
    sql.add(',@DName=' + quotedstr(Trim(DCode)));
    sql.add(',@CKOrdNO=' + quotedstr(Trim(FCKNO)));
   // ShowMessage(SQL.Text);
    Open;
  end;
  with ADOQueryPrint1 do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD7 ');
    sql.add('@DName=' + quotedstr(Trim(DName)));
    Open;
  end;
//  SCreateCDS20(ADOQueryTemp, CDS_PRT);
//  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['ZJJE'] := ADOQueryPrint.FIELDBYNAME('sumMoney').ASFLOAT + ADOQueryPrint1.FIELDBYNAME('MoneyHZ').ASFLOAT;
    RMVariables['ZJQty'] := ADOQueryPrint.FIELDBYNAME('sumQty').ASFLOAT + ADOQueryPrint1.FIELDBYNAME('QtyHZ').ASFLOAT;
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\销售码单新.rmf'), '提示', 0);
  end;
end;

procedure TfrmCKProductBCPOutList.MDNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MDName';
      flagname := '码单名称';
      if ShowModal = 1 then
      begin
        Self.MDName.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

