unit U_TradePlanLoss;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, RM_e_Xls, cxGridBandedTableView,
  cxGridDBBandedTableView, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, cxPC, Vcl.Menus, Clipbrd, dxSkinWXI,
  dxScrollbarAnnotations, cxContainer, cxProgressBar;

type
  TfrmTradePlanLoss = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ToolButton3: TToolButton;
    RMGridReport1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    Panetime: TPanel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ToolButton5: TToolButton;
    cxGrid1Level1: TcxGridLevel;
    Tv1: TcxGridDBTableView;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column32: TcxGridDBColumn;
    Tv1Column33: TcxGridDBColumn;
    Tv1Column34: TcxGridDBColumn;
    Tv1Column35: TcxGridDBColumn;
    Tv1Column36: TcxGridDBColumn;
    Tv1Column37: TcxGridDBColumn;
    Tv1Column38: TcxGridDBColumn;
    Tv1Column39: TcxGridDBColumn;
    Tv1Column40: TcxGridDBColumn;
    Tv1Column41: TcxGridDBColumn;
    Tv1Column42: TcxGridDBColumn;
    Tv1Column43: TcxGridDBColumn;
    Tv1Column44: TcxGridDBColumn;
    Tv1Column45: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv1Column46: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Panel1: TPanel;
    Label2: TLabel;
    Label6: TLabel;
    Label5: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label1: TLabel;
    Label11: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    CustName: TEdit;
    OrderNo: TEdit;
    BuyConNo: TEdit;
    C_Name: TEdit;
    cxTabControl1: TcxTabControl;
    Tv1Column47: TcxGridDBColumn;
    Tv1Column48: TcxGridDBColumn;
    Tv1DPPiece: TcxGridDBColumn;
    Tv1DPQty: TcxGridDBColumn;
    Tv1DPUnit: TcxGridDBColumn;
    Tv1HJGSPiece: TcxGridDBColumn;
    Tv1HJGSQty: TcxGridDBColumn;
    Tv1HJGSUnit: TcxGridDBColumn;
    Tv1HJGFPiece: TcxGridDBColumn;
    Tv1HJGFQty: TcxGridDBColumn;
    Tv1HJGFUnit: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure P_OrderNo_YSK(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure CustNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    { Private declarations }

    procedure InitGrid();
  public
    { Public declarations }
    RKFlag, FCYID, FZKType, FBZType: string;
    fType, Canshu2: string;
  end;

var
  frmTradePlanLoss: TfrmTradePlanLoss;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TradePlanScheduleDetail;

{$R *.dfm}

procedure TfrmTradePlanLoss.InitGrid();
var
  MCG, MDH, MTP, MFH, MLen, MWeight: Double;
  STRwhere: string;
begin

  screen.Cursor := crsqlwait;
  Panetime.Visible := true;
  Panetime.Left := (Width - Panetime.Width) div 2;
  Panetime.top := (Height - Panetime.Height - 200) div 2;
  application.ProcessMessages;

   case cxTabControl1.TabIndex of
    0:
      begin
        STRwhere := STRwhere + ' 0 ';
      end;
    1:
      begin
        STRwhere := STRwhere + ' 1 ';
      end;
    2:
      begin
        STRwhere := STRwhere + ' 9 ';
      end;
    3:
      begin
        STRwhere := STRwhere + ' 7 ';
      end;
    4:
      begin
        STRwhere := STRwhere + ' 11 ';
      end;
    5:
      begin
        STRwhere := STRwhere + ' 10 ';
      end;
    6:
      begin
        STRwhere := STRwhere + ' 12 ';
      end;
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' exec  P_Trade_Plan_loss  ');
      sql.Add('  @begdate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
      sql.Add(' ,@Enddate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
      sql.Add(' ,@status=' + QuotedStr(Trim(STRwhere)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
    Panetime.Visible := false;
    screen.Cursor := crdefault;
    TV1.DataController.Filter.Clear;
  end;
end;

procedure TfrmTradePlanLoss.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradePlanLoss := nil;
end;

procedure TfrmTradePlanLoss.OrderNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmTradePlanLoss.CustNameKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if Key = #13 then
  begin
    Key := #0;
    ToolButton2.Click;
  end;
end;

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

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

procedure TfrmTradePlanLoss.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradePlanLoss.FormShow(Sender: TObject);
begin
  inherited;
  readCxGrid(self.Caption + '3', Tv1, '损耗表查询');
  canshu2 := self.fParameters2;
  InitGrid();
end;

procedure TfrmTradePlanLoss.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmTradePlanLoss.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmTradePlanLoss.P_OrderNo_YSK(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmTradePlanLoss.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmTradePlanLoss.MPRTCodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmTradePlanLoss.N1Click(Sender: TObject);
begin

  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradePlanLoss.FormCreate(Sender: TObject);
begin
  inherited;
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 60;
end;

procedure TfrmTradePlanLoss.ToolButton3Click(Sender: TObject);
var
  filepath: string;
  mBegdate, mEnddate: string;
begin
  mBegdate := FormatDateTime('yyyy-MM-dd', begdate.Date);
  mEnddate := FormatDateTime('yyyy-MM-dd', enddate.Date);
  if CDS_1.IsEmpty then
    exit;

  try

    filepath := ExtractFilePath(Application.ExeName) + 'report\订单利润表.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;

    RMGridReport1.LoadFromFile(filepath);

    RMGridReport1.ShowReport;
  finally

  end;

end;

procedure TfrmTradePlanLoss.ToolButton4Click(Sender: TObject);
var
  useInput: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  useInput := CDS_1.FieldByName('LossNote').AsString;
  if InputQuery('订单总结备注输入框', '请输入订单总结备注', useInput) then
  begin
    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while CDS_1.Locate('SSel', True, []) do
      begin

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update Trade_Plan_Main set LossNote=' + quotedstr(Trim(useInput)));
          sql.Add(' where  mainId=   ' + quotedstr(CDS_1.FieldByName('mainId').asstring));
          ExecSQL;
        end;
        with CDS_1 do
        begin
          Edit;
          FieldByName('LossNote').Value := useInput;
          FieldByName('SSel').Value := False;
          Post;
        end;
        tv1.Controller.EditingController.ShowEdit();

      end;
    end;
    CDS_1.EnableControls;
  end;

end;

procedure TfrmTradePlanLoss.ToolButton5Click(Sender: TObject);
begin

  WriteCxGrid(self.Caption + '3', Tv1, '损耗表查询');
end;

//procedure TfrmTradePlanLoss.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
//var
//  val1: string;
//  val2: string;
//  val3: string;
//begin
//  inherited;
//  val1 := VarToStr(AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('SJMC').Index]);
//  val2 := VarToStr(AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('LossNote').Index]);
//  if not ((val1 = '') or (val1 = '0')) and (val2 = '') then
//  begin
//    if Abs(StrTofloat(val1)) > 5 then
//      ACanvas.Brush.Color := $4763FF;
//  end;
//
//  val3 := VarToStr(AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('RKPCL').Index]);
//  if not (val3 = '') and (val2 = '') then
//  begin
//    if Abs(StrTofloat(val3)) > 5 then
//      ACanvas.Brush.Color := $4763FF;
//  end;
//end;

procedure TfrmTradePlanLoss.Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var
  column: TcxGridDBColumn;
  tag: Integer;
  cellValue: string;
  lossNote: string;
  floatValue: Double;
begin
  inherited;
  column := AViewInfo.Item as TcxGridDBColumn;
  tag := column.Tag;

  if (tag >= 77) and (tag <= 81) then
  begin
    cellValue := VarToStr(AViewInfo.Value);
    lossNote := VarToStr(AViewInfo.GridRecord.Values[
      TcxGridDBTableView(Sender).GetColumnByFieldName('LossNote').Index]);

    if (lossNote = '') and (cellValue <> '') then
    begin
      try
        floatValue := StrToFloat(cellValue);
        if Abs(floatValue) > 5 then
          ACanvas.Brush.Color := $4763FF;
      except
      end;
    end;
  end;
end;

procedure TfrmTradePlanLoss.TV1DblClick(Sender: TObject);
begin

  try
    frmTradePlanScheduleDetail := TfrmTradePlanScheduleDetail.Create(Application);
    with frmTradePlanScheduleDetail do
    begin
      FMainId := self.CDS_1.FieldByName('MainId').AsString;
      FOrderNo := self.CDS_1.FieldByName('OrderNo').AsString;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmTradePlanScheduleDetail.Free;
  end;
end;

end.

