Skip to content

Instantly share code, notes, and snippets.

@drmcarvalho
Last active May 14, 2017 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmcarvalho/7d3660ad901137030b8075447ad416f3 to your computer and use it in GitHub Desktop.
Save drmcarvalho/7d3660ad901137030b8075447ad416f3 to your computer and use it in GitHub Desktop.
Classe que controla Abertura e Fechamento do Caixa.
using Awesome.Data.Sql.Builder;
using Dapper;
using Dapper.Contrib.Extensions;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SQLite;
using System.Linq;
using System;
namespace Models
{
[Table("Caixa")]
public class Caixa
{
[Dapper.Contrib.Extensions.Key]
public int IdCaixa { get; set; } = 0;
public DateTime DataAbertura { get; set; } = DateTime.Now;
public DateTime DataFechamento { get; set; }
public string Status { get; set; } = "A";
[Range(0, double.MaxValue, ErrorMessage = "Oops... o troco não pode ser um valor negativo.")]
public decimal ValorTroco { get; set; } = 0.0m;
[Range(1, double.MaxValue, ErrorMessage = "Oops... parece que não houve valores para este dia.")]
public decimal ValorTotalDia { get; set; } = 0.0m;
public string FlgDeletado { get; set; } = "N";
[Computed]
public List<string> ErrosValidacao { get; private set; } = new List<string>();
public void LimparErrosValidacao() => ErrosValidacao = new List<string>();
private bool CaixaEstaAberto()
{
return false;
}
public void Validar()
{
Caixa caixa = this;
ValidationContext context = new ValidationContext(caixa, null, null);
IList<ValidationResult> erros = new List<ValidationResult>();
if (!Validator.TryValidateObject(caixa, context, erros, true))
{
foreach (ValidationResult resultado in erros)
{
ErrosValidacao.Add(resultado.ErrorMessage);
}
}
}
public bool GerarEAbrirCaixa()
{
SQLiteConnection conexao = null;
using (conexao = new SQLiteConnection(UtilStringConexao.StringConexao))
{
return SqlMapperExtensions.Insert(conexao, this) != 0;
}
}
public bool FecharCaixa()
{
DataFechamento = DateTime.Now;
Status = "F";
SQLiteConnection conexao = null;
using (conexao = new SQLiteConnection(UtilStringConexao.StringConexao))
{
return (IdCaixa > 0) ? SqlMapperExtensions.Update(conexao, this) : false;
}
}
public static Caixa ObterPorId(int id)
{
SQLiteConnection conexao = null;
using (conexao = new SQLiteConnection(UtilStringConexao.StringConexao))
{
var caixa = conexao.Get<Caixa>(id);
return caixa;
}
}
private decimal ObterTotalDoDia()
{
return 0m;
}
public static bool ExisteRegistroDeAberturaNoDia()
{
SQLiteConnection conexao = null;
using (conexao = new SQLiteConnection(UtilStringConexao.StringConexao))
{
conexao.Open();
var statement = SqlStatements.Select(new[] { "c.IdCaixa" }).From("Caixa c").Where("c.DataAbertura = date('now', 'localtime')").Where("c.Status = 'A'");
var resultado = conexao.Query<int?>(statement.ToSql()).Single() ?? 0;
return resultado > 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment