/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.entidad;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;

/**
 *
 * @author Administrador
 */
@Entity
@Table(name = "SECRETARIA")
@SequenceGenerator(name = "SECRETARIA_SEQ", sequenceName = "SECRETARIA_SEQ", allocationSize = 1)
public class Secretaria implements Serializable {

	public static final Short PRECIO_TOTAL = 1;
	public static final Short PRECIO_MENOS_DESCUENTO = 2;
	public static final Short DESCUENTO_COBRAR = 3;
	private static final long serialVersionUID = 1L;
	@Id
	@Basic(optional = false)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SECRETARIA_SEQ")
	@Column(name = "SECRETARIA_ID")
	private Integer secretariaId;
	@Column(name = "DESCSECRETARIA")
	private String descsecretaria;
	@Column(name = "ACTIVO")
	private Boolean activo;
	@Column(name = "FECMODIF")
	@Temporal(TemporalType.TIMESTAMP)
	private Date fecmodif;
	@Column(name = "USUARIO_ID")
	private Integer usuarioId;
	@Column(name = "CVESECRETARIA")
	private String cvesecretaria;
	@Column(name = "VALIDAFOLIO")
	private Boolean validafolio;
	@Column(name = "INDSERIESUBSERIE")
	private Boolean validaSerieSubSerie;
	@Column(name = "IMPRIMEPRECIO")
	private Short imprimeprecio;
	@OneToMany(cascade = CascadeType.ALL)
	@JoinColumn(name = "SECRETARIA_ID")
	private List<CuponSecretaria> lsCuponSecretaria;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "secretaria")
	@LazyCollection(LazyCollectionOption.FALSE)
	private List<SecretariaEmpresa> empresas = new ArrayList<SecretariaEmpresa>();
	@Column(name = "INDVOUCHERROD")
	private Boolean indVoucherRod;
	@Column(name = "RUT")
	private Long rut; 
	@Column (name = "DIRSECRETARIA")
	private String direccion;
	@Column (name = "CIUDAD")
	private String ciudad;
	
	@OneToOne
	@JoinColumn(name = "FORMAPAGO_ID")
	private FormaPago formaPago;
	
	@Column(name = "CNPJ")
	private String cnpj;
	
	public Secretaria() {
		this.indVoucherRod = false;
	}

	public SecretariaEmpresa addEmpresa(Empresa e) {
		SecretariaEmpresa se = new SecretariaEmpresa();
		se.setEmpresa(e);
		se.setSecretaria(this);
		se.setActivo(Boolean.TRUE);
		se.setFecmodif(Calendar.getInstance().getTime());
		se.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
		this.empresas.add(se);
		return se;
	}

	public void removeEmpresa(SecretariaEmpresa e) {
		this.empresas.remove(e);
	}

	public void removeEmpresa(Empresa e) {
		for (SecretariaEmpresa se : empresas) {
			if (se.getEmpresa().equals(e)) {
				this.empresas.remove(e);
				break;
			}
		}
	}

	public Secretaria(Integer secretariaId) {
		this.secretariaId = secretariaId;
		this.indVoucherRod = false;
	}

	public Integer getSecretariaId() {
		return secretariaId;
	}

	public void setSecretariaId(Integer secretariaId) {
		this.secretariaId = secretariaId;
	}

	public String getDescsecretaria() {
		return descsecretaria;
	}

	public void setDescsecretaria(String descsecretaria) {
		this.descsecretaria = descsecretaria;
	}

	public Boolean getActivo() {
		return activo;
	}

	public void setActivo(Boolean activo) {
		this.activo = activo;
	}

	public Date getFecmodif() {
		return fecmodif;
	}

	public void setFecmodif(Date fecmodif) {
		this.fecmodif = fecmodif;
	}

	public Integer getUsuarioId() {
		return usuarioId;
	}

	public void setUsuarioId(Integer usuarioId) {
		this.usuarioId = usuarioId;
	}

	public String getCvesecretaria() {
		return cvesecretaria;
	}

	public void setCvesecretaria(String cvesecretaria) {
		this.cvesecretaria = cvesecretaria;
	}

	public Boolean getValidafolio() {
		return validafolio;
	}

	public void setValidafolio(Boolean validafolio) {
		this.validafolio = validafolio;
	}

	public Short getImprimeprecio() {
		return imprimeprecio;
	}

	public void setImprimeprecio(Short imprimeprecio) {
		this.imprimeprecio = imprimeprecio;
	}

	public List<CuponSecretaria> getLsCuponSecretaria() {

		List<CuponSecretaria> tmp = new ArrayList<CuponSecretaria>();
		if (lsCuponSecretaria != null) {
			for (CuponSecretaria cp : this.lsCuponSecretaria) {
				if (cp.getActivo()) {
					tmp.add(cp);
				}
			}
		}

		return tmp;
	}

	public List<SecretariaEmpresa> getEmpresas() {
		List<SecretariaEmpresa> lista = new ArrayList<SecretariaEmpresa>();
		for (SecretariaEmpresa eb : empresas) {
			if (eb.getActivo()) {
				lista.add(eb);
			}
		}
		return lista;
	}

	public void setEmpresas(List<SecretariaEmpresa> empresas) {
		this.empresas = empresas;
	}

	public void setLsCuponSecretaria(List<CuponSecretaria> lsCuponSecretaria) {
		this.lsCuponSecretaria = lsCuponSecretaria;
	}

	public Boolean getValidaSerieSubSerie() {
		return validaSerieSubSerie;
	}

	public void setValidaSerieSubSerie(Boolean validaSerieSubSerie) {
		this.validaSerieSubSerie = validaSerieSubSerie;
	}	

	public Boolean getIndVoucherRod() {
		return indVoucherRod;
	}

	public void setIndVoucherRod(Boolean indVoucherRod) {
		this.indVoucherRod = indVoucherRod;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (secretariaId != null ? secretariaId.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		// TODO: Warning - this method won't work in the case the id fields are not set
		if (!(object instanceof Secretaria)) {
			return false;
		}
		Secretaria other = (Secretaria) object;
		if ((this.secretariaId == null && other.secretariaId != null) || (this.secretariaId != null && !this.secretariaId.equals(other.secretariaId))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return descsecretaria;
	}

	public Long getRut() {
		return rut;
	}

	public void setRut(Long rut) {
		this.rut = rut;
	}

	public String getDireccion() {
		return direccion;
	}

	public void setDireccion(String direccion) {
		this.direccion = direccion;
	}

	public String getCiudad() {
		return ciudad;
	}

	public void setCiudad(String ciudad) {
		this.ciudad = ciudad;
	}
	
	public FormaPago getFormaPago() {
		return formaPago;
	}

	public void setFormaPago(FormaPago formaPago) {
		this.formaPago = formaPago;
	}

	public String getCnpj() {
		return cnpj;
	}

	public void setCnpj(String cnpj) {
		this.cnpj = cnpj;
	}
	
	
}
