<modification>
        <id><![CDATA[MADNESS Tax and Shipping on Reports by Jason Aguilar]]></id>
        <version><![CDATA[1.0]]></version>
        <vqmver><![CDATA[1.1]]></vqmver>
        <author><![CDATA[Exel]]></author>
				
        <file name="admin/model/report/sale.php">
	        <operation error="log">
	        <search position="replace"><![CDATA[
			$sql = "SELECT MIN(date_added) AS date_start, MAX(date_added) AS date_end, COUNT(*) AS orders, SUM(total) AS total FROM `" . DB_PREFIX . "order`";
			]]></search>
	        <add><![CDATA[
			$sql = "SELECT MIN(date_added) AS date_start, MAX(date_added) AS date_end, payment_method, COUNT(*) AS orders, SUM(total) AS total, SUM(order_total.value) AS shipping, SUM((SELECT ot.value FROM order_total AS ot WHERE order.order_id = ot.order_id AND ot.title LIKE '%CA%' AND order.date_added BETWEEN '" . $this->db->escape($date_start) . " 0:00:00' AND '" . $this->db->escape($date_end) . " 23:59:59' GROUP BY DAY(order.date_added))) AS ca_tax, SUM((SELECT ot.value FROM order_total AS ot WHERE order.order_id = ot.order_id AND ot.title LIKE '%TX%' AND order.date_added BETWEEN '" . $this->db->escape($date_start) . " 0:00:00' AND '" . $this->db->escape($date_end) . " 23:59:59' GROUP BY DAY(order.date_added))) AS tx_tax, SUM((SELECT ot.value FROM order_total AS ot WHERE order.order_id = ot.order_id AND ot.title LIKE '%IL%' AND order.date_added BETWEEN '" . $this->db->escape($date_start) . " 0:00:00' AND '" . $this->db->escape($date_end) . " 23:59:59' GROUP BY DAY(order.date_added))) AS il_tax";
		    $sql .= " FROM `" . DB_PREFIX . "order`
			LEFT JOIN `" . DB_PREFIX . "order_total` AS order_total ON(order_total.order_id = order.order_id)
			";
			]]></add>
	        </operation>

	        <operation error="log">
	        <search position="before"><![CDATA[
			if (isset($data['filter_group'])) {
			]]></search>
	        <add><![CDATA[
			if (isset($data['filter_payment_method'])) {
				$sql .= " AND payment_method = '" . $data['filter_payment_method'] . "'";
			} else {
				$sql .= "";
			}
			]]></add>
	        </operation>

	        <operation error="log">
	        <search position="replace" index="1"><![CDATA[
			$sql .= " AND (DATE(date_added) >= '" . $this->db->escape($date_start) . "' AND DATE(date_added) <= '" . $this->db->escape($date_end) . "')";
			]]></search>
	        <add><![CDATA[
			$sql .= " AND order_total.title LIKE '%Shipping%' AND date_added BETWEEN '" . $this->db->escape($date_start) . " 0:00:00' AND '" . $this->db->escape($date_end) . " 23:59:59'";
			]]></add>
	        </operation>

	        <operation error="log">
	        <search position="before"><![CDATA[
			public function getSaleReportTotal($data = array()) {
			]]></search>
	        <add><![CDATA[
			public function getPaymentMethods() {

				$sql = "SELECT e.key AS payment_method FROM `" . DB_PREFIX . "setting` AS s LEFT JOIN `" . DB_PREFIX . "extension` as e ON (e.key = s.group) WHERE s.key LIKE '%_status' AND e.type = 'payment'"; 

				$query = $this->db->query($sql);

				return $query->rows;	

			}
			]]></add>
	        </operation>

        </file>
		
        <file name="admin/controller/report/sale.php">
        <operation error="log">
        <search position="after" offset="2"><![CDATA[
		$filter_order_status_id = 0;
		]]></search>
        <add><![CDATA[
		if (isset($this->request->get['filter_payment_method'])) {
			$filter_payment_method = $this->request->get['filter_payment_method'];
		}
		]]></add>
        </operation>

        <operation error="log">
        <search position="after" offset="2"><![CDATA[
		$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
		]]></search>
        <add><![CDATA[
		if (isset($this->request->get['filter_payment_method'])) {
			$url .= '&filter_payment_method=' . $this->request->get['filter_payment_method'];
		}
		]]></add>
        </operation>

        <operation error="log">
        <search position="after"><![CDATA[
		'filter_order_status_id' => $filter_order_status_id,
		]]></search>
        <add><![CDATA[
		'filter_payment_method'  => $filter_payment_method,
		]]></add>
        </operation>
		
        <operation error="log">
        <search position="replace" index="1"><![CDATA[
		foreach ($results as $result) {
		]]></search>
        <add><![CDATA[
        $auth_fee = 0;
        $auth_extra = 0;
        $pp_fee = 0;
        $pp_extra = 0;
        $filtered = FALSE;
		foreach ($results as $result) {

			$method = strtolower($result['payment_method']);


			if (strpos($method, 'authorize') !== false) {

				$other_fee = (12.90 / $result['orders']) + (0.5 * date('t'));
				$auth_extra = $result['orders'] * .05;
				$auth_fee = ($result['total'] * 0.008) + $auth_extra + $other_fee;
				$filtered = TRUE;
				$paypal = FALSE;
			} 

			if (strpos($method, 'paypal') !== false) {
				$pp_extra = $result['orders'] * .30;
				$pp_fee = ($result['total'] * 0.029) + $pp_extra;
				$filtered = TRUE;
				$paypal = TRUE;

			}

		$fees = 'Filter Method';

		if($filtered && $paypal) {

			$fees = $this->currency->format($pp_fee, $this->config->get('config_currency'));

		} 

		if($filtered && $this->request->get['filter_payment_method'] == 'Credit Card / Debit Card (Authorize.Net)') {

			$fees = $this->currency->format($auth_fee, $this->config->get('config_currency'));

		}

		if(!isset($this->request->get['filter_payment_method'])) {

			$fees = 'Filter Method';

		}

		]]></add>
        </operation>

        <operation error="log">
        <search position="replace"><![CDATA[
		'total'      => $this->currency->format($result['total'], $this->config->get('config_currency'))
		]]></search>
        <add><![CDATA[
		'total'      => $this->currency->format($result['total'], $this->config->get('config_currency')),
		'subtotal'      => $this->currency->format($result['total'] - $result['shipping'] - $result['tax'], $this->config->get('config_currency')),
		'payment_method' => $result['payment_method'],
		'shipping'   => $this->currency->format($result['shipping'], $this->config->get('config_currency')),
		'fees'			=> $fees,
		'ca_tax'        => $this->currency->format($result['ca_tax'], $this->config->get('config_currency')),
		'tx_tax'        => $this->currency->format($result['tx_tax'], $this->config->get('config_currency')),
		'il_tax'        => $this->currency->format($result['il_tax'], $this->config->get('config_currency')),
		]]></add>
        </operation>
		
        <operation error="log">
        <search position="before"><![CDATA[
		$pagination = new Pagination();
		]]></search>
        <add><![CDATA[
		if (isset($this->request->get['filter_payment_method'])) {
			$url .= '&filter_payment_method=' . $this->request->get['filter_payment_method'];
		}
		]]></add>
        </operation>
		
        <operation error="log">
        <search position="before"><![CDATA[
		$this->data['filter_order_status_id'] = $filter_order_status_id;
		]]></search>
        <add><![CDATA[
		$this->data['filter_payment_method'] = $filter_payment_method;
		]]></add>
        </operation>

        <operation error="log">
        <search position="before"><![CDATA[
		$this->load->model('localisation/order_status');
		]]></search>
        <add><![CDATA[
		$pmnt = $this->model_report_sale->getPaymentMethods();

		foreach ($pmnt as $result) {

			switch ($result['payment_method']) {
			    case 'free_checkout':
			        $result['payment_method'] = 'Free Checkout';
			        break;
			    case 'pp_standard':
			        $result['payment_method'] = 'PayPal';
			        break;
			    case 'authorizenet_aim':
			        $result['payment_method'] = 'Credit Card / Debit Card (Authorize.Net)';
			        break;
			    case 'affirm':
			        $result['payment_method'] = 'Monthly Payments with Affirm';
			        break;
			    default:
			        $result['payment_method'] = $result['payment_method'];
			}

			$this->data['payment_methods'][] = $result['payment_method'];
		}


		]]></add>
        </operation>

        </file>
		
        <file name="admin/view/template/report/sale.tpl">
        <operation error="log">
        <search position="before"><![CDATA[
		<td align="right"><a onclick="filter();" class="button"><span><?php echo $button_filter; ?></span></a></td>
		]]></search>
        <add><![CDATA[
	   <td>Payment Method<br />
		<select name="filter_payment_method" style="margin-top: 4px;">
			<option value=""><?php echo $text_all_status; ?></option>
			<?php foreach($payment_methods as $payment) { ?>

			<?php if($filter_payment_method == $payment) { ?>
			<option value="<?php echo $payment; ?>" selected="selected"><?php echo $payment; ?></option>
			<?php } else { ?>
			<option value="<?php echo $payment; ?>"><?php echo $payment; ?></option>
			<?php } ?>

			<?php } ?>


		</select></td>
		]]></add>
        </operation>
		
        <operation error="log">
        <search position="before"><![CDATA[
		<td class="right"><?php echo $column_total; ?></td>
		]]></search>
        <add><![CDATA[
        <td class="right">Sub Total</td>
		<td class="right">Total Shipping</td>
		<td class="right">CA Total Tax</td>
		<td class="right">TX Total Tax</td>
		<td class="right">IL Total Tax</td>
		<td class="right">Fees</td>
		  ]]></add>
        </operation>
		
        <operation error="log">
        <search position="before"><![CDATA[
		<td class="right"><?php echo $order['total']; ?></td>
		]]></search>
        <add><![CDATA[
        <td class="right"><?php echo $order['subtotal']; ?></td>
		<td class="right"><?php echo $order['shipping']; ?></td>
		<td class="right"><?php echo $order['ca_tax']; ?></td>
		<td class="right"><?php echo $order['tx_tax']; ?></td>
		<td class="right"><?php echo $order['il_tax']; ?></td>
		<td class="right"><?php echo $order['fees']; ?></td>
		  ]]></add>
        </operation>
		
        <operation error="log">
        <search position="after" offset="1"><![CDATA[
		url += '&filter_order_status_id=' + encodeURIComponent(filter_order_status_id);
		]]></search>
        <add><![CDATA[
		var filter_payment_method = $('select[name=\'filter_payment_method\']').attr('value');

		if (filter_payment_method) {
			url += '&filter_payment_method=' + encodeURIComponent(filter_payment_method);
		}
		]]></add>
        </operation>
        </file>
</modification>