unit U_FinishedClothOutList_MX;

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

type
  TfrmFinishedClothOutList_MX = 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;
    CKNo: TEdit;
    TBCKCX: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    P_CodeName: TEdit;
    P_Color: TEdit;
    OrderNo: TEdit;
    JYID: TEdit;
    Label8: TLabel;
    CRType: TComboBox;
    ADOQueryPrint: TADOQuery;
    Label12: TLabel;
    P_Code: TEdit;
    Label14: TLabel;
    KHName: TEdit;
    Label9: TLabel;
    JTH: TEdit;
    ComboBox1: TComboBox;
    ToolButton2: TToolButton;
    Label13: TLabel;
    BatchNo: TEdit;
    Panel2: TPanel;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label7: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Sel: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    cxgrdPS: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxgrdMJLen: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label11: TLabel;
    JYType: TComboBox;
    v1Column7: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: 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);
  private
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmFinishedClothOutList_MX: TfrmFinishedClothOutList_MX;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

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

    0:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;
        cxgrdPS.Visible := false;

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        if canshu1 = '管理' then
          TBCKCX.Visible := true;
      end;
  end;
end;

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

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

procedure TfrmFinishedClothOutList_MX.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_CKHZ ');
//            sql.add('''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + ''' ');
//            sql.Add(',''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
//            sql.Add(',' + QuotedStr(fwhere) + ' ');
//          end;
        0:      /////////////////////////明细//////////////////////////////
          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_CK ');
            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 TfrmFinishedClothOutList_MX.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  SetStatus();
  InitGrid();
end;

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

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

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

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

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

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

procedure TfrmFinishedClothOutList_MX.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
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('EXEC  P_CPCK_UP_CKRE   @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;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_FHSQ_UP_QTY1  ' + quotedstr(trim(CDS_Main.fieldbyname('CKNo').asstring)));
            ExecSQL;
          end;
          CDS_Main.Delete;
        end;
      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 TfrmFinishedClothOutList_MX.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
   // SetStatus();
    InitGrid();
  end;
end;

procedure TfrmFinishedClothOutList_MX.ToolButton2Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then
    EXIT;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  if ComboBox1.Text = '普通码单' then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDZZ1 ' + quotedstr(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString)));
      Open;
    end;
  end;

  if trim(ComboBox1.Text) = '普通机台码单' then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDZZ2 ' + quotedstr(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString)));
      Open;
    end;
  end;
  if ComboBox1.Text = '单页机台码单' then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDZZ3 ' + quotedstr(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString)));
      Open;
    end;
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.DefaultCopies := 2;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

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

procedure TfrmFinishedClothOutList_MX.Tv1DblClick(Sender: TObject);
var
  Tabint: integer;
  str: string;
begin
  Tabint := cxTabControl1.TabIndex;
  case Tabint of
    0:
      begin
        AssignmentControls(Panel1, CDS_Main, '0');
        cxTabControl1.TabIndex := 1;
      end;
  end;
//    str:=cds_Main.fieldbyname('orderno').Value;
//    ShowMessage(str);
end;

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

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

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

end.

