unit U_TradeXinYuOutTHList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxDropDownEdit, cxCheckBox, RM_e_Graphic, RM_e_Jpeg,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, Math,
  dxDateRanges, IdExplicitTLSClientServerBase, U_BaseList, Vcl.Clipbrd,
  dxScrollbarAnnotations, cxProgressBar, FrameDateSel, dxSkinsCore,
  dxSkinsDefaultPainters, Vcl.DBCtrls;

type
  TfrmTradeXinYuOutTHList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxgrdpmn1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    TMMNo: TcxTextEdit;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    cxTabControl1: TcxTabControl;
    TWC: TToolButton;
    TNowc: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    v1Column16: TcxGridDBColumn;
    ORDER_SUB: TClientDataSet;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    Label1: TLabel;
    CustName: TcxTextEdit;
    Tv1Column6: TcxGridDBColumn;
    cxgrdpmn2: TcxGridPopupMenu;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label2: TLabel;
    C_Name: TcxTextEdit;
    CDS_WXTS: TClientDataSet;
    Label4: TLabel;
    ConNo: TcxTextEdit;
    ToolButton2: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    GPM_3: TcxGridPopupMenu;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    TBFind: TToolButton;
    xxx1: TMenuItem;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TMMNoKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TWCClick(Sender: TObject);
    procedure TNowcClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
    procedure JGTypeChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, canshu4: string;
    fFlileFlag: string;
    FNOTE: string;
    FOrdType: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_RPFun, U_ZDYHelp, U_LabelPrint;

{$R *.dfm}

procedure TfrmTradeXinYuOutTHList.SetStatus();
begin

  case cxTabControl1.TabIndex of
    0:
      begin

      end;

  end;

end;

procedure TfrmTradeXinYuOutTHList.JGTypeChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradeXinYuOutTHList.FormDestroy(Sender: TObject);
begin
  inherited;
//  frmProductOrderList:=nil;
end;

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

procedure TfrmTradeXinYuOutTHList.FormCreate(Sender: TObject);
begin
  inherited;
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);

end;

procedure TfrmTradeXinYuOutTHList.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradeXinYuOutTHList.InitGrid();
var
  SqlStr, FilterStr: string;
begin
  FilterStr := SGetFilters(Panel1, 1, 2);

  SqlStr := ' exec  P_BS_Cloth_Xinyu_Out_TH  ';
  SqlStr := SqlStr + '  @BegDate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)));
  SqlStr := SqlStr + ' ,@EndDate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)));

  //  case cxTabControl1.TabIndex of
  //    0:
  //      begin
  //        SqlStr := SqlStr + ' , @Status=''9'' ';
  //      end;
  //    1:
  //      begin
  //        SqlStr := SqlStr + ' , @Status=''10'' ';
  //      end;
  //
  //  end;

  InitCDSData(ADOQueryMain, Order_Main, Tv1, SqlStr, FilterStr, 'TMMId');
end;

procedure TfrmTradeXinYuOutTHList.InitForm();
begin
  ReadCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '贸易汇总仓库');
  frmFrameDateSel1.BegDate.Date := SGetServerDate(ADOQueryTemp) - 180;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);

  SetStatus();
  InitGrid();
end;

procedure TfrmTradeXinYuOutTHList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradeXinYuOutTHList.OrderNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, Order_Main);
  SInitCDSData(ADOQueryMain, Order_Main);
end;

procedure TfrmTradeXinYuOutTHList.FormShow(Sender: TObject);
begin
  inherited;
  canshu1 := trim(self.fParameters1);
  canshu2 := trim(self.fParameters2);
  canshu4 := trim(self.fParameters4);
  fFlileFlag := UserDataFlag + 'HX';
  cxTabControl1.TabIndex := 0;
  InitForm();
end;

procedure TfrmTradeXinYuOutTHList.TBTPClick(Sender: TObject);
var
  FQty, FQty1, FMxQty, FPQty, FMxQtyS, FPQtyS: string;
begin
end;

procedure TfrmTradeXinYuOutTHList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradeXinYuOutTHList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmTradeXinYuOutTHList.Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmTradeXinYuOutTHList.v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmTradeXinYuOutTHList.TMMNoKeyPress(Sender: TObject; var Key: Char);
begin

  if Key = #13 then
  begin
    initgrid();
  end;
end;

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

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

procedure TfrmTradeXinYuOutTHList.TWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Market_Main SET status=''10'' ');
      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));

      sql.Add(' update  Trade_Market_Sub SET OutQty=(select sum(qty) from Trade_Market_Sub_Qty x where x.TMSId = Trade_Market_Sub.TMSID) ');
      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));

      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails) ');
      sql.Add('values(''贸易布坯出库''    ');
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(',''完成''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(')  ');

      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(Order_Main.FieldByName('TMMNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

    application.MessageBox('计划单完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('计划单完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradeXinYuOutTHList.TNowcClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Market_Main SET status=''9'' ');
      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));

      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails) ');
      sql.Add('values(''贸易布坯出库''    ');
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(',''完成''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(')  ');

      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(Order_Main.FieldByName('TMMNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('计划单撤销完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('计划单撤销完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradeXinYuOutTHList.N1Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradeXinYuOutTHList.N3Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True); //全选
end;

procedure TfrmTradeXinYuOutTHList.N4Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False); //全弃
end;

procedure TfrmTradeXinYuOutTHList.ComboBox1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradeXinYuOutTHList.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmTradeXinYuOutTHList.ToolButton10Click(Sender: TObject);
begin

  application.MessageBox('保存成功！', '提示信息', 0);
end;

procedure TfrmTradeXinYuOutTHList.ToolButton1Click(Sender: TObject);
begin
  TcxGridToExcel(Trim(Self.Caption), cxGrid1);
end;

procedure TfrmTradeXinYuOutTHList.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '贸易汇总仓库');
end;

procedure TfrmTradeXinYuOutTHList.ToolButton3Click(Sender: TObject);
var
  MFiltration: string;
  MPrintJson: string;
begin

  if Order_Main.IsEmpty then
    Exit;

  MFiltration := Trim(Order_Main.FieldByName('TMMNo').AsString);
  MPrintJson := '              {"LMType": "TradeXinYuOut1"    ';
  MPrintJson := MPrintJson + ' ,"PreviewPrint": true      ';
  MPrintJson := MPrintJson + ' ,"PrtArgs": [ { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '"  } ] }    ';
  FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson));
end;

procedure TfrmTradeXinYuOutTHList.TBFindClick(Sender: TObject);
begin

  CDSDataFilter(ADOQueryMain, Order_Main, Tv1, '{"FilterStr":"' + CommonFiltersByContainer(Panel1, '  {"EquTag": 1,"LikeTag": 2,"Fields": ""}') + '"  }');

end;

end.

