package controller.payment;

import controller.Controller_Add_Dialog;
import controller.history.Application_Controller;
import org.sig.fw.db.corrida.dao.*;
import org.sig.fw.db.corrida.model.*;
import org.sig.fw.layout.pane.SOptionPane;
import org.sig.fw.utility.date.SDate;
import org.sig.fw.utility.number.NumberTools;
import view.payment.Add_Payment_View;


public class Add_Payment_Controller extends Controller_Add_Dialog
{
	public Add_Payment_Controller(Application_Controller ctrl_application, Facture facture)
	{
		super(ctrl_application);
		
		model = new Payment(facture);
		((Payment)model).setPaymentType(new Definition_DBAO("def").getWithFamily(Definition_DBAO.PAYMENT_TYPE, 0));
		((Payment)model).setDate(new SDate());
		((Payment)model).setCc(facture.getCommande().getCc());
		((Payment)model).setPaymentMode(facture.getPaymentMode());
		
		this.vw_add_dialog = new Add_Payment_View(this);
		this.vw_add_dialog.setVisible(true);
	}
	
	public Add_Payment_Controller(Application_Controller ctrl_application, Payment payment)
	{
		super(ctrl_application);
		
		this.model = payment;
		
		this.vw_add_dialog = new Add_Payment_View(this);
		this.vw_add_dialog.setTitle("Modifier / Visualiser un Paiement");
		this.vw_add_dialog.setVisible(true);
	}

	@Override
	public void save()
	{
		Payment payment = (Payment) model;
		if(payment.getDbId() == -1)
		{
			payment.setDbId(new Payment_DBAO("pay").insert(payment));
			
			History history = new History(-1, History_DBAO.PAYMENT, payment, ctrl_application.getConnectedAccount(), new SDate(), "Ajout d'un Paiement de " + NumberTools.getNumberFormat(2).format(payment.getAmount()) + " € pour la facture " + payment.getFacture().getFactureNumber() + ".");
			new History_DBAO("h").insert(history);
			
			// Ajout d'une note dans les notes CORRIDA
			Note_DBAO dbao_note = new Note_DBAO("n");
			Note note = new Note(-1, Note_DBAO.CATEGORY_DOSSIER, payment.getFacture().getCommande().getDossier(), this.ctrl_application.getConnectedAccount(), new SDate(), "Paiement de " + NumberTools.getNumberFormat(2).format(payment.getAmount()) + " € pour la facture " + payment.getFacture().getFactureNumber() + " (" + payment.getFacture().getCategory() + ")<br>Observation : " + payment.getObservation() + ".");
			dbao_note.insert(note);
			
		}
		else
		{
			new Payment_DBAO("pay").update(payment);
			
			History history = new History(-1, History_DBAO.PAYMENT, payment, ctrl_application.getConnectedAccount(), new SDate(), "Modification d'un Paiement de " + NumberTools.getNumberFormat(2).format(payment.getAmount()) + " € pour la facture " + payment.getFacture().getFactureNumber() + ".");
			new History_DBAO("h").insert(history);
		}
		
		if(new Payment_DBAO("pay").getSumWithDossierAndConstructionSite(payment.getFacture().getCommande().getDossier(), payment.getFacture().getCommande().getConstructionSite()) >= new Commande_DBAO("com").getSumOfCommandeWithDossierAndConstructionSite(payment.getFacture().getCommande().getDossier(), payment.getFacture().getCommande().getConstructionSite()))
		{
			removeAllRelance();
			int result = SOptionPane.showYesNoMessage("Soldé", "Le Dossier " + payment.getFacture().getCommande().getDossier().getReferenceNumber() + " est soldé par ce paiement. Voulez vous changer l'état de toutes ses Commandes en SOLDE ?");
			if(result == SOptionPane.YES_OPTION)
			{
				new Commande_DBAO("com").updateAllState(payment.getFacture().getCommande().getDossier(), payment.getFacture().getCommande().getConstructionSite(), State_DBAO.SOLDE);
				new Facture_DBAO("fact").updateAllState(payment.getFacture().getCommande().getDossier(), payment.getFacture().getCommande().getConstructionSite(), State_DBAO.SOLDE);
			}
		}
		else if(new Payment_DBAO("pay").getSumWithCommande(payment.getFacture().getCommande()) >= payment.getFacture().getCommande().getAmountNet())
		{
			removeAllRelance();
			int result = SOptionPane.showYesNoMessage("Soldé", "La Commande " + payment.getFacture().getCommande().getCommandeNumber() + " est soldée par ce paiement. Voulez vous changer l'état de la Commande (" + payment.getFacture().getCommande().getState().getLongValue() + ") en SOLDE ?");
			if(result == SOptionPane.YES_OPTION)
			{
				new Commande_DBAO("com").updateState(payment.getFacture().getCommande(), State_DBAO.SOLDE);
				new Facture_DBAO("fact").updateAllState(payment.getFacture().getCommande(), State_DBAO.SOLDE);
			}
		}
		else if(new Payment_DBAO("pay").getSumWithFacture(payment.getFacture()) >= payment.getFacture().getAmountNet())
		{
			removeAllRelance();
			int result = SOptionPane.showYesNoMessage("Soldé", "La Facture " + payment.getFacture().getFactureNumber() + " est soldée par ce paiement. Voulez vous changer l'état de la Facture (" + payment.getFacture().getState().getLongValue() + ") en SOLDE ?");
			if(result == SOptionPane.YES_OPTION)
			{
				new Facture_DBAO("fact").updateState(payment.getFacture(), State_DBAO.SOLDE);
			}
		}
		else {
			int result = SOptionPane.showYesNoMessage("Paiement", "La Facture " + payment.getFacture().getFactureNumber() + " n'est pas soldé par ce paiement. Voulez vous créer une relance ?");
			if(result == SOptionPane.YES_OPTION)
			{
				removeAllRelance();
				Relance r = Relance.createFacture(payment.getFacture(), payment.getCc());
				r.setEcheanceDate(new SDate());
				new Relance_DBAO("rel", -1).insert(r);
			}
		}
		
		new Payment_Pdf_Controller(payment);
		this.ctrl_application.activeUpdate("ADD_PAYMENT", payment.getFacture().getCommande().getDossier().getDbId(), payment.getFacture().getCommande().getConstructionSite().getDbId());
	}
	
		public void delete()
	{
		int result = SOptionPane.showYesNoCancelWarning("Suppression", "<html>Voulez vous vraiment supprimer ce paiement ?<br>Si la facture est à l'état SOLDEE, elle passera automatiquement à l'état VALIDEE !</html>");
		if(result == SOptionPane.YES_OPTION)
		{
			Payment payment = (Payment) model;
			new Payment_DBAO("pay").delete(payment);
			new Facture_DBAO("fact").updateState(payment.getFacture(), State_DBAO.VALIDEE);
			removeAllRelance();
			this.ctrl_application.activeUpdate("ADD_PAYMENT", payment.getFacture().getCommande().getDossier().getDbId(), payment.getFacture().getCommande().getConstructionSite().getDbId());
			this.vw_add_dialog.dispose();
		}
	}
		
	public void pdf_visupayment()
	{
		Payment payment = (Payment) model;
		new Payment_Pdf_Controller(payment);
	}
	
	private void removeAllRelance()
	{
		new Relance_DBAO("rel", -1).deleteWithFacture(((Payment) model).getFacture());
	}
}
