unit CurvaABC;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, TelaComum, Vcl.StdCtrls, Vcl.Mask,
  IB_EditButton, IB_Controls, System.ImageList, Vcl.ImgList, Vcl.Buttons,
  Vcl.ExtCtrls, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxStyles, dxSkinsCore, dxSkinOffice2010Blue, cxCustomData, cxFilter, cxData,
  cxDataStorage, cxEdit, cxNavigator,
  cxDataControllerConditionalFormattingRulesManagerDialog, Data.DB, cxDBData,
  cxGridLevel, cxClasses, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, IBODataset, IB_Grid,
  IB_Components, IB_Access, ppBands, ppCache, ppClass, ppDesignLayer,
  ppParameter, ppComm, ppRelatv, ppProd, ppReport,cxGridExportLink, Vcl.Menus,
  cxButtons, ppDB, ppDBPipe, ppCtrls, ppPrnabl, Vcl.DBCtrls ;

type
  TfrmCurvaAbc = class(TfrmTelaComum)
    GroupBox1: TGroupBox;
    edtDataInicio: TIB_Date;
    edtDataFim: TIB_Date;
    cxGrid1: TcxGrid;
    cxGridDBTableView1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    dsrCurvaAbc: TDataSource;
    qrCurvaAbc: TIBOQuery;
    cxGridDBTableView1PRODUTO_CODIGO: TcxGridDBColumn;
    cxGridDBTableView1PRODUTO_DESCRICAO: TcxGridDBColumn;
    cxGridDBTableView1PRODUTO_VALOR_TOTAL: TcxGridDBColumn;
    cxGridDBTableView1PRODUTO_PERCENTUAL: TcxGridDBColumn;
    cxGridDBTableView1CLASSIFICACAO: TcxGridDBColumn;
    cxGridDBTableView1PRODUTO_TIPO_SAIDA: TcxGridDBColumn;
    qrCurvaAbcPRODUTO_CODIGO: TStringField;
    qrCurvaAbcPRODUTO_DESCRICAO: TMemoField;
    qrCurvaAbcPRODUTO_VALOR_TOTAL: TIBOBCDField;
    qrCurvaAbcPEDIDO_TOTAL: TIBOBCDField;
    qrCurvaAbcPRODUTO_PERCENTUAL: TIBOBCDField;
    qrCurvaAbcCLASSIFICACAO: TStringField;
    qrCurvaAbcPRODUTO_TIPO_SAIDA: TStringField;
    btnGerar: TcxButton;
    btnImprimir: TcxButton;
    btnExportarExcel: TcxButton;
    qrTipoSaida: TIB_Query;
    IB_DataSource1: TIB_DataSource;
    edtTipoSaida: TIB_LookupCombo;
    cxGridDBTableView1Column1: TcxGridDBColumn;
    qrCurvaAbcPROODUTO_QUANTIDADE: TIBOBCDField;
    pplCurvaAbc: TppDBPipeline;
    rbAbc: TppReport;
    ppParameterList1: TppParameterList;
    ppDesignLayers1: TppDesignLayers;
    ppDesignLayer1: TppDesignLayer;
    ppHeaderBand1: TppHeaderBand;
    ppDetailBand1: TppDetailBand;
    ppFooterBand1: TppFooterBand;
    ppDBText1: TppDBText;
    ppDBText2: TppDBText;
    ppDBText3: TppDBText;
    ppDBText4: TppDBText;
    ppDBText5: TppDBText;
    ppDBText6: TppDBText;
    ppLabel1: TppLabel;
    ppLabel2: TppLabel;
    ppLabel3: TppLabel;
    ppLabel4: TppLabel;
    ppLabel5: TppLabel;
    ppLabel6: TppLabel;
    ppLine1: TppLine;
    ppSummaryBand1: TppSummaryBand;
    ppDBCalc1: TppDBCalc;
    ppDBCalc2: TppDBCalc;
    ppShape1: TppShape;
    DBEdit1: TDBEdit;
    Label1: TLabel;
    qrCurvaAbcPRODUTO_VALOR_CUSTO_UNITARIO: TIBOBCDField;
    qrCurvaAbcPRODUTO_VALOR_CUSTO_TOTAL: TIBOBCDField;
    qrCurvaAbcPRODUTO_LUCRO: TIBOBCDField;
    qrCurvaAbcPRODUTO_DESCONTO: TIBOBCDField;
    lbltotal: TLabel;
    edtValorLista: TIB_Currency;
    procedure cxGridDBTableView1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure cxGridDBTableView1DblClick(Sender: TObject);
    procedure btnGerarClick(Sender: TObject);
    procedure btnExportarExcelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure edtTipoSaidaChange(Sender: TObject);
    procedure btnImprimirClick(Sender: TObject);
  private
    filtro : string;
    procedure Calcular_ValorLista;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCurvaAbc: TfrmCurvaAbc;

implementation

{$R *.dfm}

uses DataModulo, Rotinas, CadProduto;

procedure TfrmCurvaAbc.btnExportarExcelClick(Sender: TObject);
var dlgSalvar : TSaveDialog;
begin
   dlgSalvar := TSaveDialog.Create(self);
   if not dlgSalvar.Execute then
     exit;

   ExportGridDataToExcel( dlgSalvar.FileName , cxgrid1, true, true, true);
end;

procedure TfrmCurvaAbc.btnGerarClick(Sender: TObject);
begin
  inherited;
   if edtDataInicio.Value = 0 then
   begin
      MessageDlg('Informe o perído', mtWarning, [mbOK], 0);
      edtDataInicio.SetFocus;
      exit;
   end;

   qrCurvaAbc.close;
   qrCurvaAbc.ParamByName('DataInicio').Value := edtDataInicio.Value;
   qrCurvaAbc.ParamByName('DataFim').Value    := edtDataFim.Value;
   qrCurvaAbc.open;

   qrCurvaAbc.Filtered := False;
   qrCurvaAbc.Filter   := '';

   if edtTipoSaida.Text <> '' then
   begin
      qrCurvaAbc.Filter   := ' PROC_CURVA_ABC.PRODUTO_TIPO_SAIDA  = ' + QuotedStr(edtTipoSaida.Text);
      qrCurvaAbc.Filtered := True;
      filtro := 'Período: ' + edtDataInicio.Text + ' até ' + edtDataFim.Text
   end;


   Calcular_ValorLista;

end;

procedure TfrmCurvaAbc.btnImprimirClick(Sender: TObject);
begin
  inherited;
   qrCurvaAbc.DisableControls;

   ImpressaoBaseadaNoModelo(rbAbc , true, 'Curva ABC', filtro);

   qrCurvaAbc.EnableControls;
end;

procedure TfrmCurvaAbc.cxGridDBTableView1CustomDrawCell( Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
   Codigo     : string;

begin
  codigo:='';

   if AViewInfo.GridRecord.Values[4] = 'A' then
      ACanvas.Brush.Color := $00B7F2B8;

   if AViewInfo.GridRecord.Values[4] = 'B' then
      ACanvas.Brush.Color := $00FAC6C5;

   if AViewInfo.GridRecord.Values[4] = 'C' then
      ACanvas.Brush.Color := $00CEFFFF;

   if AViewInfo.GridRecord.Selected then
   begin
      ACanvas.Brush.Color := ACanvas.Font.Color;
      ACanvas.Font.Color  := clblack;
   end;

end;

procedure TfrmCurvaAbc.cxGridDBTableView1DblClick(Sender: TObject);
begin
  inherited;
  frmCadProduto  := TfrmCadProduto.Create(Application);
  frmCadProduto.TbPrincipal.close;
  frmCadProduto.TbPrincipal.SQLWhere.clear;
  frmCadProduto.TbPrincipal.SQLWhere.Add( ' where PRODUTO_ESTOQUE.COD_PRODUTO = ' + QuotedStr (qrCurvaAbc.FieldByName('PRODUTO_CODIGO').AsString) );
  frmCadProduto.TbPrincipal.Open();
  frmCadProduto.OutraTela := true;
  frmCadProduto.BtEditar.click;
  frmCadProduto.ShowModal;
  frmCadProduto.Destroy;

end;

procedure TfrmCurvaAbc.Calcular_ValorLista;
var Valor : Currency;
begin
   if not qrCurvaAbc.IsEmpty then
      qrCurvaAbc.first;

   Valor := 0;

   qrCurvaAbc.DisableControls;

   while not qrCurvaAbc.eof do
   begin
      Valor := valor +  qrCurvaAbc.FieldByName('PRODUTO_VALOR_TOTAL').AsCurrency;
      qrCurvaAbc.Next;
   end;

   qrCurvaAbc.EnableControls;

   edtValorLista.Value := Valor;

   if not qrCurvaAbc.IsEmpty then
      qrCurvaAbc.first;

end;

procedure TfrmCurvaAbc.edtTipoSaidaChange(Sender: TObject);
begin
  inherited;
   qrCurvaAbc.Filtered := False;
   qrCurvaAbc.Filter   := '';

   if edtTipoSaida.Text <> '' then
   begin
      qrCurvaAbc.Filter   := ' PROC_CURVA_ABC.PRODUTO_TIPO_SAIDA  = ' + QuotedStr(edtTipoSaida.Text);
      qrCurvaAbc.Filtered := True;
   end;

   Calcular_ValorLista;
end;

procedure TfrmCurvaAbc.FormCreate(Sender: TObject);
begin
  inherited;
   InicioDaTabela(qrTipoSaida);
end;

end.
