unit U_TradeQDList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Vcl.Printers, 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, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, dxScrollbarAnnotations, cxContainer,
  cxProgressBar, cxButtons, cxMaskEdit, dxSkinsCore, dxSkinsDefaultPainters,
  cxPC, System.Math, Clipbrd, FrameDateSel;

type
  TfrmTradeQDStkList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADO_2: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    PopupMenu1: TPopupMenu;
    ToolButton2: TToolButton;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADO_1: TADOQuery;
    N3: TMenuItem;
    Panel1: TPanel;
    Label3: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    TMMNo: TcxTextEdit;
    CustName: TcxTextEdit;
    C_Name: TcxTextEdit;
    ConNo: TcxTextEdit;
    frmFrameDateSel1: TfrmFrameDateSel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1Column16: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV2Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure FromCoNamePropertiesChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure FromCoNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure TV2DblClick(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    canshu1, FSTKName, canshu3, canshu4: string;
//    procedure InitGrid();
    procedure InitGrid(MWSql: string);
    procedure InitGrid1();
    procedure InitGrid2();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmTradeQDStkList: TfrmTradeQDStkList;

implementation

uses
  U_DataLink, U_RTFun, U_printPdf, U_LabelPrint;

{$R *.dfm}

procedure TfrmTradeQDStkList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeqdStkList := nil;
end;

procedure TfrmTradeQDStkList.cxPageControl1Change(Sender: TObject);
begin

  case cxPageControl1.ActivePageIndex of
    1:
      begin

        InitGrid1();
      end;
  end;

end;

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

procedure TfrmTradeQDStkList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(self.fParameters1);
  FSTKName := Trim(self.fParameters2);
  canshu3 := Trim(self.fParameters3);
  canshu4 := Trim(self.fParameters4);
end;

//procedure TfrmTradeClothTotalStkList.InitGrid();
//begin
//  case cxPageControl1.ActivePageIndex of
//    0:
//      begin
//        InitGrid2();
//      end;
//    1:
//      begin
//
//        InitGrid1();
//      end;
//  end;
//
//end;
procedure TfrmTradeQDStkList.InitGrid(MWSql: string);
var
  WSql, WDateSql: string;
  SqlStr: string;
begin

  case cxPageControl1.ActivePageIndex of
    0:
      begin
//        WSql := CommonFiltersByContainer(dxLayoutControl_query, '  {"EquTag": 1,"LikeTag": 2,"Fields": "OrderNo|FtyPCId|FtyPCId|C_Color|C_Code|C_Name"}');


        SqlStr := ' select *,kcqty=(select sum(stkqty) from bs_cloth_io g where g.c_code=b.c_code and g.c_colorno=b.c_colorno) from Trade_Market_main A INNER JOIN Trade_Market_SUB B ON A.TMMID=B.TMMID  ';

        SqlStr := SqlStr + ' where A.ORDTYPE=''欠单''  ';

        SqlStr := SqlStr + ' and TMMDate>=''' + FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.begdate.Date) + ''' ';
        SqlStr := SqlStr + ' and TMMDate<''' + FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.Enddate.Date + 1) + ''' ';


//  ShowMessage(SqlStr);
        InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, SGetFilters(Panel1, 1, 2), 'C_Code');

      end;

    1:
      begin
        SqlStr := ' select C_NAME,C_COLOR,C_ColorNo,SUM(TMSQTY)TMSQTY,TMSUNIT from Trade_Market_main A INNER JOIN Trade_Market_SUB B ON A.TMMID=B.TMMID  ';

        SqlStr := SqlStr + ' where A.ORDTYPE=''欠单''  ';

        SqlStr := SqlStr + ' GROUP BY    C_NAME,C_COLOR,C_ColorNo, TMSUNIT        ';
        InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, '', 'C_name');

      end;
  end;

end;

procedure TfrmTradeQDStkList.InitGrid2();
var
  SqlStr: string;
begin

  SqlStr := ' select A.C_Code,C_Name,C_Color,C_ColorNo,C_Degree,C_Pattern,QtyUnit,StkPieceint=sum(CEILING(A.StkPiece)),StkQty=sum(StkQty),QtyUnit,StkPosition ';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + ' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ';
  SqlStr := SqlStr + ' and isnull(stkName,'''')=''' + Trim(FstkName) + '''';

  if Trim(canshu4) <> '' then
    SqlStr := SqlStr + ' and isnull(StkCoName,'''')=''' + Trim(canshu4) + '''';

  SqlStr := SqlStr + ' group by C_Code,C_Name,C_Color,C_ColorNo,C_Degree,C_Pattern,QtyUnit,StkPosition';

//  ShowMessage(SqlStr);
  InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, SGetFilters(Panel1, 1, 2), 'C_Code');

end;

procedure TfrmTradeQDStkList.InitGrid1();
var
  SqlStr, WSqlStr: string;
begin

  SqlStr := ' select A.*,StkPieceint=CEILING(A.StkPiece) ';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + ' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ';
  SqlStr := SqlStr + ' and isnull(stkName,'''')=''' + Trim(FstkName) + '''';

  if Trim(canshu4) <> '' then
    SqlStr := SqlStr + ' and isnull(StkCoName,'''')=''' + Trim(canshu4) + '''';
  WSqlStr := CommonFiltersByTv(Tv2, '{"RemovalNull":true,"Fields":"C_Code|C_Name|C_Color|C_ColorNo|C_Pattern|C_Degree|QtyUnit"}');

  if TRIM(WSqlStr) <> '' then
    SqlStr := SqlStr + ' and ' + WSqlStr;
  SqlStr := SqlStr + ' order by IOTime desc';

  InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'BCIOID');

end;

procedure TfrmTradeQDStkList.TBRafreshClick(Sender: TObject);
begin
  InitGrid('');
//  InitGrid2();
end;

procedure TfrmTradeQDStkList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeQDStkList.FormShow(Sender: TObject);
begin
  inherited;
  CurrentPage := 1;
  RecordsNumber := 500;
  ReadCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '贸易欠单');
  ReadCxGrid(trim(Self.Caption) + 'Tv2', Tv2, '贸易欠单');
  frmFrameDateSel1.BegDate.Date := SGetServerDate(ADOQueryTemp) - 180;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);

  canshu4 := Trim(self.fParameters4);
  InitGrid('');
end;

procedure TfrmTradeQDStkList.FromCoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    TBFind.Click;
end;

procedure TfrmTradeQDStkList.FromCoNamePropertiesChange(Sender: TObject);
begin
  if Length(Trim(TcxTextEdit(Sender).Text)) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmTradeQDStkList.TBFindClick(Sender: TObject);
begin
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        CDSDataFilter(ADO_2, CDS_2, Tv2, '{"FilterStr":"' + CommonFiltersByContainer(Panel1, '  {"EquTag": 1,"LikeTag": 2,"Fields": "C_name|C_Color|C_ColorNo|StkPosition|FromCoName"}') + '"  }');
      end;
    1:
      begin
        CDSDataFilter(ADO_1, CDS_1, Tv1, '{"FilterStr":"' + CommonFiltersByContainer(Panel1, '  {"EquTag": 1,"LikeTag": 2,"Fields": ""}') + '"  }');
      end;
  end;

end;

procedure TfrmTradeQDStkList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeQDStkList.N1Click(Sender: TObject);
begin
  inherited;
  SelOKNo(CDS_1, True);
end;

procedure TfrmTradeQDStkList.N2Click(Sender: TObject);
begin
  inherited;
  SelOKNo(CDS_1, False);
end;

procedure TfrmTradeQDStkList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

procedure TfrmTradeQDStkList.ToolButton1Click(Sender: TObject);
begin
  if CDS_2.IsEmpty then
    exit;

//  if canshu1 <> '高权限' then
//  begin
//    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//    begin
//      Application.MessageBox('不能操作他人的数据!', '提示', 0);
//      Exit;
//    end;
//  end;

  if cxPageControl1.ActivePageIndex <> 0 then
  begin
    Application.MessageBox('只能在明细界面进行还原操作!', '提示', 0);
    Exit;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Market_Main SET oRDTYPE=''正常单''  ');
      sql.Add(' where TMMId=' + quotedstr(trim(CDS_2.fieldbyname('TMMId').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_2.FieldByName('TMMNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

    application.MessageBox(' 转为正常单成功！', '提示信息');

  except
    application.MessageBox('转为正常单失败！', '提示信息', 0);
  end;
  TBRafresh.Click;

end;

procedure TfrmTradeQDStkList.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '贸易欠单');
  WriteCxGrid(trim(Self.Caption) + 'Tv2', Tv2, '贸易欠单');
end;

procedure TfrmTradeQDStkList.ToolButton3Click(Sender: TObject);
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeLabVolume1';
      FFiltration1 := Trim(SelCDSKey(self.CDS_1, ['BCIOID'])[0]);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTradeQDStkList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult := 1;
end;

procedure TfrmTradeQDStkList.TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[cxGridDBColumn12.Index] <= AViewInfo.GridRecord.Values[TV2Column1.Index] then
    ACanvas.Brush.Color := $0080FF80;
end;

procedure TfrmTradeQDStkList.TV2DblClick(Sender: TObject);
begin
  cxPageControl1.ActivePageIndex := 1;

end;

end.

