//    Posics Saleculator - Billing System.
//    Copyright (C) 2009 Posics
//
//    This file is part of Posics Saleculator.
//
// Discount for receipts having more than certain qty

import com.posics.format.Formats;
import com.posics.pos.ticket.TicketLineInfo;
import com.posics.pos.ticket.TicketProductInfo;
import java.util.Properties;

if (ticket.getCustomer() != null){

//SET OFFER COUNT HERE

	double offercount = 12;

	double customerquantity = 0;
	double totalquantity = 0;
	double transactionamount = 0;
	double totaltransactionamount = 0;
	double discountamount = 0;

	double excessquantity = 0;
	double excessamount = 0;
	double loopquantity = 0;
	double discountquantity = 0;
	if(ticket.getCustomer().getTotalQuantity()!=null && ticket.getCustomer().getTransactionsAmount()!=null){
		customerquantity=ticket.getCustomer().getTotalQuantity();
		transactionamount=ticket.getCustomer().getTransactionsAmount();
	}

	totalquantity=customerquantity+ticket.getArticlesCount();
	totaltransactionamount=transactionamount+ticket.getTotal();

	if(totalquantity>=offercount){
		excessquantity = totalquantity % offercount;
		loopquantity = excessquantity;

		if(excessquantity>0){
			int numlines = ticket.getLinesCount();

			//start for loop
			for (int i = numlines - 1 ; loopquantity >= 1 ; i--) {
			 	current_ticketline = ticket.getLine(i);
				current_unit  = current_ticketline.getMultiply();
				//javax.swing.JOptionPane.showMessageDialog(null, current_unit + ", " + current_ticketline.getPrice() + ",  - "+loopquantity+", " + i , "Info", JOptionPane.WARNING_MESSAGE);
				if (current_unit != 0){
					if(current_unit <= loopquantity){
						loopquantity = loopquantity - current_unit;
						excessamount = excessamount + (current_unit * current_ticketline.getPrice());
					}
					else{
						excessamount = excessamount + (loopquantity * current_ticketline.getPrice());
						loopquantity =  loopquantity - current_unit;
					}
				}
 			}
			//end for loop
		}

		discountquantity = totalquantity - excessquantity;
		discountamount = totaltransactionamount - excessamount;
		discountamount = (discountamount / offercount)*-1;

		//javax.swing.JOptionPane.showMessageDialog(null,totalquantity+", "+totaltransactionamount+",  - "+excessquantity+", "+excessamount , "Info", JOptionPane.WARNING_MESSAGE);
		//javax.swing.JOptionPane.showMessageDialog(null,discountamount+", "+excessamount+",  - "+excessquantity, "Info", JOptionPane.WARNING_MESSAGE);


		//javax.swing.JOptionPane.showMessageDialog(this.getParent(), "Discount Applied.", "Member Discount - " + ticket.getCustomer().printName(), JOptionPane.INFORMATION_MESSAGE);

		ticket.getCustomer().setTotalQuantity(excessquantity);
		ticket.getCustomer().setTransactionsAmount(excessamount);

		ticket.insertLine(ticket.getLinesCount(), new TicketLineInfo("Member Discount", "000", 1, discountamount, null));

		newamount=totaltransactionamount - excessamount;
		javax.swing.JOptionPane.showMessageDialog(this.getParent(), "Discount applied for quantity " + discountquantity + " and amount " + newamount + "." , "Info", JOptionPane.WARNING_MESSAGE);
		//javax.swing.JOptionPane.showMessageDialog(null,ticket.getCustomer().getTotalQuantity(), "Info", JOptionPane.WARNING_MESSAGE);

	}
	else{
		javax.swing.JOptionPane.showMessageDialog(null, totalquantity + " numbers so far for customer " +ticket.getCustomer().printName()+". " + offercount + " required for an offer." , "Member Discount - " + ticket.getCustomer().printName(), JOptionPane.INFORMATION_MESSAGE);
	}

}

