modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 51"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Mittch Clarke'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Mittch Clarke'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 51"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 51"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 51 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 51"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /**********/ /* Karlos */ /**********/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 49"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Karlos'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Karlos'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 49"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 49"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 49 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 49"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /***************/ /* Dave Watson */ /***************/ /*$startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 45"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Dave Watson'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Dave Watson'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 45"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 45"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 45 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 45"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; }*/ /*************/ /* Ray Evans */ /*************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 13"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Ray Evans'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Ray Evans'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 13"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 13"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 13 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 13"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 2000) { $lowerComm = ($results[$i]['payment_total'] - 2000); if ($lowerComm > 8000) { $lowerComm = 8000; } $lowerComm = $lowerComm / 100; $lowerComm = $lowerComm * 1.5; if ($results[$i]['payment_total'] > 10000) { $higherComm = ($results[$i]['payment_total'] - 10000); $higherComm = $higherComm / 100; $higherComm = $higherComm * 2; $lowerComm = $lowerComm + $higherComm; } $results[$i]['commission'] = number_format($lowerComm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /**************/ /* Dave Wells */ /**************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 14"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Dave Wells'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Dave Wells'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 14"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 14"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 14 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 14"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 2000) { $lowerComm = ($results[$i]['payment_total'] - 2000); if ($lowerComm > 8000) { $lowerComm = 8000; } $lowerComm = $lowerComm / 100; $lowerComm = $lowerComm * 1.5; if ($results[$i]['payment_total'] > 10000) { $higherComm = ($results[$i]['payment_total'] - 10000); $higherComm = $higherComm / 100; $higherComm = $higherComm * 2; $lowerComm = $lowerComm + $higherComm; } $results[$i]['commission'] = number_format($lowerComm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /***************/ /* Pat Monahan */ /***************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 15"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Pat Monahan'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Pat Monahan'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 15"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 15"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 15 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 15"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 2000) { $lowerComm = ($results[$i]['payment_total'] - 2000); if ($lowerComm > 8000) { $lowerComm = 8000; } $lowerComm = $lowerComm / 100; $lowerComm = $lowerComm * 1.5; if ($results[$i]['payment_total'] > 10000) { $higherComm = ($results[$i]['payment_total'] - 10000); $higherComm = $higherComm / 100; $higherComm = $higherComm * 2; $lowerComm = $lowerComm + $higherComm; } $results[$i]['commission'] = number_format($lowerComm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /********************/ /* Steve Harrington */ /********************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 20"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Steve Harrington'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Steve Harrington'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 20"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 20"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 20 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 20"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 2000) { $lowerComm = ($results[$i]['payment_total'] - 2000); if ($lowerComm > 8000) { $lowerComm = 8000; } $lowerComm = $lowerComm / 100; $lowerComm = $lowerComm * 1.5; if ($results[$i]['payment_total'] > 10000) { $higherComm = ($results[$i]['payment_total'] - 10000); $higherComm = $higherComm / 100; $higherComm = $higherComm * 2; $lowerComm = $lowerComm + $higherComm; } $results[$i]['commission'] = number_format($lowerComm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /****************/ /* Andy Jeffery */ /****************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 22"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Andy Jeffery'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Andy Jeffery'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 22"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 22"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 22 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 22"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 2000) { $lowerComm = ($results[$i]['payment_total'] - 2000); if ($lowerComm > 8000) { $lowerComm = 8000; } $lowerComm = $lowerComm / 100; $lowerComm = $lowerComm * 1.5; if ($results[$i]['payment_total'] > 10000) { $higherComm = ($results[$i]['payment_total'] - 10000); $higherComm = $higherComm / 100; $higherComm = $higherComm * 2; $lowerComm = $lowerComm + $higherComm; } $results[$i]['commission'] = number_format($lowerComm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /********/ /* Dale */ /********/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 34"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Dale Hubbard'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Dale Hubbard'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 34"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 34"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 34 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 34"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /**********/ /* Isobel */ /**********/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 35"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Isobel Mason'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Isobel Mason'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 35"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 35"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 35 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 35"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 50; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /********/ /* Tony */ /********/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 36"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Tony Mason'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Tony Mason'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 36"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 36"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 36 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 36"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 2000) { $lowerComm = ($results[$i]['payment_total'] - 2000); if ($lowerComm > 8000) { $lowerComm = 8000; } $lowerComm = $lowerComm / 100; $lowerComm = $lowerComm * 1.5; if ($results[$i]['payment_total'] > 10000) { $higherComm = ($results[$i]['payment_total'] - 10000); $higherComm = $higherComm / 100; $higherComm = $higherComm * 2; $lowerComm = $lowerComm + $higherComm; } $results[$i]['commission'] = number_format($lowerComm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /*****************/ /* Michael Mason */ /*****************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 42"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Michael Mason'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Michael Mason'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 42"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 42"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 42 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 42"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /****************/ /* Dave Russell */ /****************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 37"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Dave Russell'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Dave Russell'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 37"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 37"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 37 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 37"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 2000) { $lowerComm = ($results[$i]['payment_total'] - 2000); if ($lowerComm > 8000) { $lowerComm = 8000; } $lowerComm = $lowerComm / 100; $lowerComm = $lowerComm * 1.5; if ($results[$i]['payment_total'] > 10000) { $higherComm = ($results[$i]['payment_total'] - 10000); $higherComm = $higherComm / 100; $higherComm = $higherComm * 2; $lowerComm = $lowerComm + $higherComm; } $results[$i]['commission'] = number_format($lowerComm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /*****************/ /* Jack O'Rourke */ /*****************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 44"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Jack O\'Rourke'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Jack O\'Rourke'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 44"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 44"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 44 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 44"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /****************/ /* Charlie Peel */ /****************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 46"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Charlie Peel'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Charlie Peel'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 46"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 46"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 46 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 46"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /*****************/ /* Brett Freeman */ /*****************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 48"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Brett Freeman'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Brett Freeman'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 48"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 48"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 48 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 48"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /*****************/ /* Kieron Hodges */ /*****************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 50"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Kieron Hodges'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Kieron Hodges'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 50"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 50"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 50 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 50"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } /*****************/ /* Jordan Panter */ /*****************/ $startDate = new DateTime($s); $endDate = new DateTime($e); $current_date = new DateTime(); for ($j = $startDate, $k = $endDate; $j < $current_date; $j->modify('+1 week'), $k->modify('+1 week')) { $sql = "SELECT SUM(comm_value) AS amount_subtract FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_old_resource = users.id AND users.id = 53"; foreach ($dbh->query($sql) as $row) { if ($row['amount_subtract'] != null) { $results[$i]['id'] = 'Jordan Panter'; $results[$i]['amount_subtract'] = $row['amount_subtract']; } else { $results[$i]['id'] = 'Jordan Panter'; $results[$i]['amount_subtract'] = '0.00'; } } $sql = "SELECT SUM(comm_value) AS amount_add FROM commission_split, users WHERE DATE(comm_date) >= '".$j->format('Y-m-d')."' AND DATE(comm_date) <= '".$k->format('Y-m-d')."' AND comm_new_resource = users.id AND users.id = 53"; foreach ($dbh->query($sql) as $row) { if ($row['amount_add'] > 0) { $results[$i]['amount_add'] = $row['amount_add']; } else { $results[$i]['amount_add'] = '0.00'; } } $sql = "SELECT SUM(payments.payment_total) AS refund FROM payments, users, invoices WHERE DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_assigned = users.id AND payments.payment_invoice = invoices.invoice_id AND payments.payment_type = 'Refund' AND payments.payment_status = 'Paid' AND invoices.invoice_assigned = users.id AND users.id = 53"; foreach ($dbh->query($sql) as $row) { if ($row['refund'] < 0) { $results[$i]['refund'] = $row['refund']; } else { $results[$i]['refund'] = '0.00'; } } $sql = "SELECT COUNT(invoice_id) AS deals FROM invoices WHERE invoice_assigned = 53 AND DATE(invoices.invoice_paid_date) >= '".$j->format('Y-m-d')."' AND DATE(invoices.invoice_paid_date) <= '".$k->format('Y-m-d')."' AND invoices.invoice_status = 'Paid' AND invoices.invoice_total > 20"; foreach ($dbh->query($sql) as $row) { $results[$i]['deals'] = $row['deals']; } $sql = "SELECT COUNT(payments.payment_id) AS payment_id, SUM(payments.payment_total) AS payment_total FROM payments, invoices, users WHERE payments.payment_invoice = invoices.invoice_id AND invoices.invoice_assigned = users.id AND payments.payment_status = 'Paid' AND DATE(payments.payment_date) >= '".$j->format('Y-m-d')."' AND DATE(payments.payment_date) <= '".$k->format('Y-m-d')."' AND users.id = 53"; foreach ($dbh->query($sql) as $row) { $weekDate = strtotime($j->format('Y-m-d')); $newWeekFormat = date('d/m/Y', $weekDate); $results[$i]['start_date'] = $newWeekFormat; $results[$i]['payment_id'] = $row['payment_id']; if ($row['payment_total'] > 0) { $results[$i]['payment_total'] = $row['payment_total']; } else { $results[$i]['payment_total'] = '0.00'; } $results[$i]['payment_total'] = $results[$i]['payment_total'] - $results[$i]['amount_subtract'] + $results[$i]['amount_add']; if ($results[$i]['payment_total'] > 0) { $comm = $results[$i]['payment_total'] / 100; $results[$i]['commission'] = number_format($comm, 2); } else { $results[$i]['commission'] = '0.00'; } $results[$i]['payment_total'] = number_format($results[$i]['payment_total'], 2); } $i = $i + 1; } $dbh = null; } catch(PDOException $e) { echo $e->getMessage(); } $json_object = array("data" => $results); echo json_encode($json_object, JSON_INVALID_UTF8_SUBSTITUTE); ?>