package controller.facture;

import controller.Controller;
import controller.history.Application_Controller;
import org.sig.fw.db.corrida.dao.Facture_DBAO;
import org.sig.fw.db.corrida.dao.Payment_DBAO;
import org.sig.fw.db.corrida.model.Facture;
import view.facture.Facture_View;

public class Facture_Controller extends Controller
{
	private Facture_View vw_facture;
	private Facture facture;
	
	public Facture_Controller(Application_Controller ctrl_application, Facture facture)
	{
		super(ctrl_application);
		this.facture = (Facture) new Facture_DBAO("fact").getWithId(facture.getDbId(), true);
		
		this.vw_facture = new Facture_View(this);
		this.vw_facture.setVisible(true);
	}

	public Facture getModel()
	{
		return this.facture;
	}
	
	public Facture_View getView()
	{
		return this.vw_facture;
	}
	
	public void computeCanDelete()
	{
		boolean can_delete = true;
		can_delete = new Payment_DBAO("pay").getSumWithFacture(facture) == 0.0 ? can_delete : false;
		can_delete = new Facture_DBAO("fact").getWithCommande(facture.getCommande()).size() == 1 ? can_delete : false;
		can_delete = new Facture_DBAO("fact").getLock(facture) == null ? can_delete : false;
		this.vw_facture.setDeleteEnabled(can_delete);
	}
	
	public void delete()
	{
		new Facture_DBAO("fact").delete(facture);
		this.ctrl_application.activeUpdate("ADD_FACTURE", facture.getCommande().getDossier().getDbId(), facture.getCommande().getConstructionSite().getDbId());
		this.vw_facture.dispose();
	}
}
