unit U_FinishedClothInList_KW;

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

type
  TfrmFinishedClothInList_KW = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Label10: TLabel;
    RKNo: TEdit;
    TBCKCX: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    P_CodeName: TEdit;
    P_Color: TEdit;
    OrderNo: TEdit;
    JYID: TEdit;
    Label8: TLabel;
    CRType: TComboBox;
    ADOQueryPrint: TADOQuery;
    v1Column4: TcxGridDBColumn;
    Label12: TLabel;
    P_Code: TEdit;
    Label14: TLabel;
    KHName: TEdit;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label9: TLabel;
    JTType: TEdit;
    ToolButton2: TToolButton;
    Label13: TLabel;
    BatchNo: TEdit;
    cxgrdPS: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Panel2: TPanel;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label7: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    cxgrdMJLen: TcxGridDBColumn;
    Label11: TLabel;
    JYType: TComboBox;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    APID: TEdit;
    lab1: TLabel;
    CDS_Print: TClientDataSet;
    RMDBPrint: TRMDBDataSet;
    v1Column13: TcxGridDBColumn;
    Label15: TLabel;
    btnOK: TToolButton;
    v1Sel: TcxGridDBColumn;
    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 OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmFinishedClothInList_KW: TfrmFinishedClothInList_KW;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmFinishedClothInList_KW.SetStatus();
var
  i: Integer;
begin
  TBCKCX.Visible := False;
  case cxTabControl1.TabIndex of
    0:
      begin
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);

        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);
        cxgrdPS.Visible := True;
        cxgrdMJLen.Visible := True;
        v1Column8.Visible := False;

        v1Column13.Visible := True;
      end;

    1:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;
        cxgrdPS.Visible := false;
        v1Column13.Visible := False;
        v1Sel.Visible := True;
        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        if canshu1 = '管理' then
          TBCKCX.Visible := true;
      end;
  end;
end;

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

procedure TfrmFinishedClothInList_KW.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
end;

procedure TfrmFinishedClothInList_KW.InitGrid();
var
  fwhere, Pwhere: string;
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      case cxTabControl1.TabIndex of
        0:   /////////////////////////订单汇总//////////////////////////////
          begin
            Pwhere := SGetFilters(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' where ' + trim(Pwhere);

            sql.Add('exec P_CPCK_Get_RKHZ ');
            sql.add('''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + ''' ');
            sql.Add(',''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
            sql.Add(',' + QuotedStr(fwhere) + ' ');
          end;
        1:      /////////////////////////明细//////////////////////////////
          begin
            fwhere := ' where CRTime>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
            fwhere := fwhere + ' and CRTime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));

            Pwhere := SGetFilters(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            sql.Add('exec P_CPCK_Get_RK ');
            sql.Add('@pageIndex=' + inttostr(CurrentPage));
            sql.Add(',@pageSize=' + inttostr(RecordsNumber));
            sql.Add(',@criteria= ' + quotedstr(fwhere));
          end;
      end;
      //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmFinishedClothInList_KW.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  SetStatus();
  InitGrid();
end;

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

procedure TfrmFinishedClothInList_KW.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '成品仓库');
  Close;
end;

procedure TfrmFinishedClothInList_KW.FormShow(Sender: TObject);
var
  fsj: string;
begin
  CurrentPage := 1;
  RecordsNumber := 500;
  fsj := 'select distinct Code=Null,Name=CRType from CK_Cloth_CR where CRFlag=''入库'' order by CRType ';
  SInitComBoxBySql(ADOQueryTemp, CRType, False, fsj);
  ReadCxGrid(Self.Caption, Tv1, '成品仓库');
  SetStatus();
  InitGrid();
end;

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

procedure TfrmFinishedClothInList_KW.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmFinishedClothInList_KW.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmFinishedClothInList_KW.TBCKCXClick(Sender: TObject);
var
  FFMainId, FPrice, FHZ, FCKOrderNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
//  CDS_Main.DisableControls;
//  with CDS_Main do
//  begin
//    First;
//    while not Eof do
//    begin
//      if CDS_Main.FieldByName('SSel').AsBoolean = True then
//      begin
//        if FCKOrderNo = '' then
//        begin
//          FCKOrderNo := Trim(CDS_Main.fieldbyname('CKOrdNo').AsString);
//          with ADOQueryTemp do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add('select * from YF_Money_CR_Sub where yfpzno=''' + Trim(CDS_Main.fieldbyname('CKOrdNo').AsString) + '''');
//            Open;
//          end;
//          if ADOQueryTemp.IsEmpty = False then
//          begin
//            CDS_Main.EnableControls;
//            Application.MessageBox('已生成应收款，不能撤销出库!', '提示', 0);
//            Exit;
//          end;
//        end;
//      end;
//      Next;
//    end;
//  end;
//  CDS_Main.EnableControls;
  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
      First;
      while CDS_Main.Locate('SSel', True, []) = True do
      begin

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_Cloth_CR where JYID=''' + Trim(CDS_Main.fieldbyname('JYID').AsString) + '''');
          sql.Add('and CRFlag=''出库'' ');
          Open;
        end;
        if ADOQueryTemp.RecordCount >= 1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          MovePanel2.Visible := False;
          Application.MessageBox('已出库不能撤销入库!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('EXEC  P_CPCK_UP_RKRE   @JYID=' + quotedstr(trim(CDS_Main.fieldbyname('JYID').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('CKNo').AsString) + ' 条码：' + trim(CDS_Main.FieldByName('JYID').AsString))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          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 TfrmFinishedClothInList_KW.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
//    SetStatus();
    InitGrid();
  end;
end;

procedure TfrmFinishedClothInList_KW.ToolButton2Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath, CNSql, FNSql, BNSql, CTSql: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  CNSql := '';
  BNSql := '';
  FNSql := '';
  CTSql := '';
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while CDS_Main.Locate('SSel', True, []) do
    begin
      if CNSql <> '' then
        CNSql := CNSql + '' + ',' + '' + QuotedStr(Trim(CDS_Main.fieldbyname('P_CodeName').AsString))
      else
        CNSql := 'and A.P_CodeName in (' + QuotedStr(Trim(CDS_Main.fieldbyname('P_CodeName').AsString));

      if FNSql <> '' then
        FNSql := FNSql + '' + ',' + '' + QuotedStr(Trim(CDS_Main.fieldbyname('P_Code').AsString))
      else
        FNSql := 'and A.P_Code in (' + QuotedStr(Trim(CDS_Main.fieldbyname('P_Code').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_Main.EnableControls;
  CNSql := CNSql + ')';
  FNSql := FNSql + ')';
  BNSql := BNSql + ')';
  CTSql := CTSql + ')';
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select AA.*,C.P_CodeName,C.P_Code from(select A.MainId,A.SubId,A.CRType,COUNT(A.JYID) PS ,SUM(A.JYLen) JYLen ');
    sql.Add(' from CK_Cloth_CR A where A.CRFlag=''入库'' ');
    sql.add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
    sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
    SQL.Add(CNSql);
    SQL.Add(FNSql);
    SQL.Add(' GROUP BY A.MainId,A.SubId,A.CRType )AA ');
    SQL.Add(' inner join PD_Plan_Sub C on AA.SubId=C.SubId ');
    // ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryCmd, CDS_Print);
  SInitCDSData20(ADOQueryCmd, CDS_Print);

  if ADOQueryCmd.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;
  ExportFtErpFile('坯布入库报表.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\坯布入库报表.rmf';

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
     // RM1.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

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

procedure TfrmFinishedClothInList_KW.Tv1DblClick(Sender: TObject);
var
  Tabint: integer;
begin
  Tabint := cxTabControl1.TabIndex;
  case Tabint of
    0:
      begin
        AssignmentControls(Panel1, CDS_Main, '0');
        cxTabControl1.TabIndex := 1;
      end;
  end;
end;

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

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

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

procedure TfrmFinishedClothInList_KW.btnOKClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;

  if cds_main.FieldByName('ssel').AsBoolean = False then
  begin
    Application.MessageBox('请先选择数据！', '提示', 0);
    Exit;
  end;
  ModalResult := 1;
end;

end.

