unit CadPlanoConta;

interface

uses
  Windows, system.UItypes, IB_Access, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TelaComum, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxCustomData, cxStyles, cxTL, cxMaskEdit,
  cxTLdxBarBuiltInMenu, IB_Components, Menus, DB, IBODataset,
  cxInplaceContainer, cxDBTL, cxTLData, ImgList, StdCtrls, Buttons,
  ExtCtrls, MaskUtils, System.ImageList, dxSkinsCore, dxSkinOffice2010Blue;

type
  TfrmCadPlanoConta = class(TfrmTelaComum)
    cxPlanoConta: TcxDBTreeList;
    cxDBTreeList1DESCRICAO: TcxDBTreeListColumn;
    qrPlanoDeContas: TIBOQuery;
    dsPlanoContas: TDataSource;
    PopupMenu1: TPopupMenu;
    NovoItem1: TMenuItem;
    Alterar1: TMenuItem;
    NovoGrupo1: TMenuItem;
    qrMontaCodigo: TIB_Query;
    qrPlanoDeContasCOD_PLANO_CONTA: TIntegerField;
    qrPlanoDeContasCOD_PAI: TIntegerField;
    qrPlanoDeContasDESCRICAO: TStringField;
    qrPlanoDeContasENTRADA_SAIDA: TStringField;
    qrPlanoDeContasCODIGO_DESCRITIVO: TStringField;
    procedure NovoItem1Click(Sender: TObject);
    procedure Alterar1Click(Sender: TObject);
    procedure NovoGrupo1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    function MontaCodigo(codpai: Integer): string;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCadPlanoConta: TfrmCadPlanoConta;

implementation

uses Item_PlanoConta, Rotinas, DataModulo;

{$R *.dfm}

procedure TfrmCadPlanoConta.NovoItem1Click(Sender: TObject);
var codigo_pai : Integer;
    bookmark : TBookmark;
    Descricao, tipo, codigo : string;

begin
  inherited;
   frmItemPlanoConta := tfrmItemPlanoConta.Create(Self);
   frmItemPlanoConta.edtCodigo.Text := MontaCodigo(qrPlanoDeContas.FieldByName('COD_PLANO_CONTA').AsInteger);

   if frmItemPlanoConta.ShowModal <> mrok then
   begin
       frmItemPlanoConta.Destroy;
       Exit;
   end;

   Descricao := frmItemPlanoConta.edtDescricao.Text;
   codigo    :=  Trim(frmItemPlanoConta.edtCodigo.Text);

   if frmItemPlanoConta.rdbEntrada.Checked then
      tipo := 'E';

   if frmItemPlanoConta.rdbSaida.Checked then
      tipo := 'S';

   codigo_pai := qrPlanoDeContas.FieldByName('COD_PLANO_CONTA').AsInteger;
   bookmark   := qrPlanoDeContas.bookmark;

   qrPlanoDeContas.Insert;
   qrPlanoDeContas.FieldByName('COD_PLANO_CONTA').Value := ProximoCodigoChave('PLANO_DE_CONTAS', 'COD_PLANO_CONTA' , '');
   qrPlanoDeContas.FieldByName('COD_PAI').Value         := codigo_pai;
   qrPlanoDeContas.FieldByName('descricao').Value       := Descricao;
   qrPlanoDeContas.FieldByName('ENTRADA_SAIDA').Value   := tipo;
   qrPlanoDeContas.FieldByName('CODIGO_DESCRITIVO').Value   := codigo;

   qrPlanoDeContas.Post;

   qrPlanoDeContas.Bookmark := bookmark;

end;


procedure TfrmCadPlanoConta.Alterar1Click(Sender: TObject);
var bookmark : TBookmark;
    Descricao, tipo, codigo : string;

begin
  inherited;
   frmItemPlanoConta := tfrmItemPlanoConta.Create(Self);
   frmItemPlanoConta.edtDescricao.Text := qrPlanoDeContas.FieldByName('DESCRICAO').AsString;

   frmItemPlanoConta.edtCodigo.Text := qrPlanoDeContas.FieldByName('CODIGO_DESCRITIVO').AsString;

   if qrPlanoDeContas.FieldByName('ENTRADA_SAIDA').AsString = 'E' then
      frmItemPlanoConta.rdbEntrada.Checked := True;

   if qrPlanoDeContas.FieldByName('ENTRADA_SAIDA').AsString = 'S' then
      frmItemPlanoConta.rdbSaida.Checked := True;

   if frmItemPlanoConta.ShowModal <> mrok then
   begin
       frmItemPlanoConta.Destroy;
       Exit;
   end;

   Descricao := frmItemPlanoConta.edtDescricao.Text;
   codigo :=  Trim(frmItemPlanoConta.edtCodigo.Text);

   if frmItemPlanoConta.rdbEntrada.Checked then
      tipo := 'E';

   if frmItemPlanoConta.rdbSaida.Checked then
      tipo := 'S';

   bookmark   := qrPlanoDeContas.bookmark;

   qrPlanoDeContas.Edit;
   qrPlanoDeContas.FieldByName('descricao').Value       := Descricao;
   qrPlanoDeContas.FieldByName('ENTRADA_SAIDA').Value   := tipo;
   qrPlanoDeContas.FieldByName('CODIGO_DESCRITIVO').Value   := codigo;

   qrPlanoDeContas.Post;

   qrPlanoDeContas.Bookmark := bookmark;

end;
procedure TfrmCadPlanoConta.NovoGrupo1Click(Sender: TObject);
begin
  inherited;
   if (MessageDlg('Deseja realmente Excluir o item selecionado?', mtConfirmation, [mbYes, mbNo], 0) = mrNo) then
      Exit;

   qrPlanoDeContas.Delete;

end;

function TfrmCadPlanoConta.MontaCodigo(codpai : Integer) : string;
var
    ultimo_Codigo , proximo_codigo : string;
    primeiro_zero : Boolean;

    i : Integer;
begin
   qrMontaCodigo.Close;
   qrMontaCodigo.ParamByName('codigo').Value := codpai;
   qrMontaCodigo.Open;

   ultimo_Codigo := qrMontaCodigo.FieldByName('ultimocodigo').AsString;

   if ultimo_Codigo = '' then
      ultimo_Codigo := qrPlanoDeContas.FieldByName('CODIGO_DESCRITIVO').AsString + '.00';

   primeiro_zero := False;
   if ultimo_Codigo <> '' then
      if ultimo_Codigo[1] = '0' then
         primeiro_zero := True;


   proximo_codigo := LimpaTexto(ultimo_Codigo );
   if proximo_codigo = '' then
       proximo_codigo := '0';

   proximo_codigo := FloatToStr((( StrToFloat(proximo_codigo)) + 1 ));

   if primeiro_zero then
      proximo_codigo := '0' + proximo_codigo;

   proximo_codigo := FormatMaskText ('99.99.99.99.99.99.99.99.99;0', proximo_codigo);

   for i:= Length(proximo_codigo) downto 1 do
   begin
      proximo_codigo := Trim(proximo_codigo);

      if proximo_codigo[i] = '.' then
         Delete(proximo_codigo , i , 1);

      if  LimpaTexto(proximo_codigo[i]) <> '' then
         Break;
   end;

   Result := proximo_codigo
end;

procedure TfrmCadPlanoConta.FormCreate(Sender: TObject);
begin
  inherited;
   qrPlanoDeContas.Open;
end;

end.
