<?php

class ByCategory {

	const CATEGORIES = [
		'carshare'        => 1, /* no */
		'scooterrental'   => 1, /* n/a */
		'publictransport' => 1, /* n/a */
		'taxi'            => 1, /* yes */
		'parking'         => 1, /* n/a */
		'tollsfees'       => 1, /* n/a */
		'rideshare'       => 1, /* yes */
		'gas'             => 1, /* n/a */
		'carrental'       => 1  /* n/a */
	];

	public function andMemberId($db, $category, $member_id) {
		$db_category = pg_escape_string(strtolower($category));
		$q = "SELECT a.*,
 b.address AS location_start, b.latitude AS location_start_lat, b.longitude AS location_start_lng,
 c.address AS location_end, c.latitude AS location_end_lat, c.longitude AS location_end_lng,
 a.id AS parsedemail_item_id 
 FROM trackedemail_item t, parsedemail_item a 
 LEFT JOIN address b ON (b.id=a.location_start_id) 
 LEFT JOIN address c ON (c.id=a.location_end_id)
 LEFT JOIN transport_providers d ON (d.id=a.transport_provider_id)
 WHERE t.id=a.trackedemail_item_id AND lower(d.category) = '${db_category}' AND t.member_id=".((int)$member_id)." 
 AND a.dup_id IS NULL ORDER BY a.travel_date_end DESC";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r)) {
			$result = [];
			while ($f=pg_fetch_assoc($r)) {
      	$f["location_start"] = html_entity_decode ($f["location_start"],ENT_QUOTES|ENT_HTML5,"UTF-8");
      	$f["location_end"] = html_entity_decode ($f["location_end"],ENT_QUOTES|ENT_HTML5,"UTF-8");
				$result[] = $f;
			}
			return [$result, NULL];
		}
		return [NULL,pg_last_error()];
	}

	public function andMemberIdForDaysTotal($db, $category, $member_id, $days) {
		$result = [
			"category"=>$category,
			"member_id"=>$member_id,
			"days"=>$days,
			"count"=>0,
			"amount"=>0,
			"errors"=>[]
		];
		$db_category = pg_escape_string(strtolower($category));
		$q = "SELECT count(*) AS count, sum(b.cost) AS amount
 FROM trackedemail_item a, parsedemail_item b 
 LEFT JOIN transport_providers c ON (c.id=b.transport_provider_id)
 WHERE a.id=b.trackedemail_item_id AND lower(c.category) = '${db_category}' AND a.member_id=".((int)$member_id)." 
 AND b.dup_id IS NULL AND b.travel_date_end > (now() - interval '${days} days')";
 		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			$result["count"] += $f["count"];
			$result["amount"] += $f["amount"];
		} else {
			$result["errors"]["receipts"] = pg_last_error($db);
		}
		$q = "SELECT count(*) AS count, sum(amount)/100 AS amount FROM members_bankimport ";
		$q.= " WHERE member_id=${member_id} AND time > (now() - interval '${days} days') ";
		$q.= " AND lower(category) = '${db_category}'";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			$result["count"] += $f["count"];
			$result["amount"] += $f["amount"];
		} else {
			$result["errors"]["bank"] = pg_last_error($db);
		}
		return $result;
	}

	public function averageForDaysTotal($db, $category, $member_id, $days) {
		$result = [
			"category"=>$category,
			"member_id"=>$member_id,
			"days"=>$days,
			"count"=>0,
			"amount"=>0,
			"errors"=>[]
		];
		$db_category = pg_escape_string(strtolower($category));
		$q = "SELECT count(*) AS count, sum(d.amount) AS amount FROM  
(SELECT a.member_id, count(*) AS count, sum(b.cost) AS amount
 FROM trackedemail_item a, parsedemail_item b 
 LEFT JOIN transport_providers c ON (c.id=b.transport_provider_id)
 WHERE a.id=b.trackedemail_item_id AND lower(c.category) = '${db_category}' AND a.member_id<>".((int)$member_id)." 
 AND b.dup_id IS NULL AND b.travel_date_end > (now() - interval '${days} days') 
 GROUP BY a.member_id) AS d";
 		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			$result["count"] += $f["count"];
			$result["amount"] += $f["amount"]/$f["count"];
		} else {
			$result["errors"]["receipts"] = pg_last_error($db);
		}
		$q = "SELECT count(*) AS count, sum(a.amount) AS amount FROM ";
		$q.= " (SELECT member_id, count(*) AS count, sum(amount)/100 AS amount FROM members_bankimport ";
		$q.= " WHERE member_id<>${member_id} AND time > (now() - interval '${days} days') ";
		$q.= " AND lower(category) = '${db_category}' GROUP BY member_id) AS a";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			$result["count"] += $f["count"];
			$result["amount"] += $f["amount"]/$f["count"];
		} else {
			$result["errors"]["bank"] = pg_last_error($db);
		}
		return $result;
	}
}
// vi:ts=2

