package com.hatherly.pocketmoneytracker.actions;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.hatherly.pocketmoneytracker.model.Person;
import com.hatherly.pocketmoneytracker.model.PersonList;
import com.hatherly.pocketmoneytracker.model.Transaction;
import com.hatherly.pocketmoneytracker.mongodb.MongoPeople;
import com.hatherly.pocketmoneytracker.mongodb.MongoTransactions;

public class PayWeeklyPocketMoney {
	
	/**
	 * Check if a payment is due today and pay money into each account that is due
	 */
	public static String payPocketMoney() {
		return payPocketMoneyForDay(new Date()); 
	}

	/**
	 * Check payments due on date specified
	 * @param date_to_check Date to check for pocket money being due
	 */
	private static String payPocketMoneyForDay(Date date_to_check) {
		String messages = "";
		int day = DateToCalendar(date_to_check).get(Calendar.DAY_OF_WEEK);
		String date_key = new SimpleDateFormat("yyyy-MM-dd").format(date_to_check);
		
		PersonList l = MongoPeople.getPeople();
		for (Person p : l.getPeople()) {
			if (p.getDayOfWeekPocketMoneyPaid() == day) {
				Transaction existing_txn = MongoTransactions.getTransaction(date_key);
				if (existing_txn == null) {
					// Pocket money for today hasn't been added yet, so add it now.
					Transaction t = new Transaction(p.getId(),
													date_to_check,
													p.getWeeklyPocketMoneyAmount(),
													"Weekly Pockey Money",
													"weekly");
					t.setId(date_key);
					UpdateTransactions.addTransaction(p, t);
					messages = messages + date_to_check + " - paid weekly pocket money of "+
												p.getWeeklyPocketMoneyAmount()+" to "+
												p.getName() + "\n";
				}
			}
		}
		
		if (messages.length()==0) {
			messages = "No updates required.";
		}
		
		return messages;
	}
	
	/**
	 * Convert from date to calendar object
	 * @param date
	 * @return
	 */
	private static Calendar DateToCalendar(Date date){ 
	  Calendar cal = Calendar.getInstance();
	  cal.setTime(date);
	  return cal;
	}
}

@@ -1120,24 +1120,69 @@
 ) == day) %7B%0A
+%09%09%09%09String key = date_key + %22_%22 + p.getId();%0A
 %09%09%09%09Transact
@@ -1233,21 +1233,16 @@
 saction(
-date_
 key);%0A%09%09
@@ -1534,21 +1534,16 @@
 t.setId(
-date_
 key);%0A%09%09
@@ -1661,16 +1661,17 @@
 oney of 
+%EF%BF%BD
 %22+%0A%09%09%09%09%09

Fix for weekly payment