everyone! I have 3 tables: order_product, product and product.
I have this function:
public function getOrders($data = array()) {
$sql = "SELECT MIN(o.date_added) AS date_start, MAX(o.date_added) AS date_end, COUNT(*) AS `orders`, SUM((SELECT SUM(op.quantity) FROM `" . DB_PREFIX . "order_product` op WHERE op.order_id = o.order_id GROUP BY op.order_id)) AS products, SUM((SELECT SUM(ot.value) FROM `" . DB_PREFIX . "order_total` ot WHERE ot.order_id = o.order_id AND ot.code = 'tax' GROUP BY ot.order_id)) AS tax, SUM((SELECT SUM(ot.value) FROM `" . DB_PREFIX . "order_total` ot WHERE ot.order_id = o.order_id AND ot.code = 'shipping' GROUP BY ot.order_id)) AS shipping_fee ,SUM(o.total) AS `total` FROM `" . DB_PREFIX . "order` o";
if (!empty($data['filter_order_status_id'])) {
$sql .= " WHERE o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
} else {
$sql .= " WHERE o.order_status_id > '0'";
}
if (!empty($data['filter_date_start'])) {
$sql .= " AND DATE(o.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
}
if (!empty($data['filter_date_end'])) {
$sql .= " AND DATE(o.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
}
if (!empty($data['filter_group'])) {
$group = $data['filter_group'];
} else {
$group = 'week';
}
switch($group) {
case 'day';
$sql .= " GROUP BY YEAR(o.date_added), MONTH(o.date_added), DAY(o.date_added)";
break;
default:
case 'week':
$sql .= " GROUP BY YEAR(o.date_added), WEEK(o.date_added)";
break;
case 'month':
$sql .= " GROUP BY YEAR(o.date_added), MONTH(o.date_added)";
break;
case 'year':
$sql .= " GROUP BY YEAR(o.date_added)";
break;
}
$sql .= " ORDER BY o.date_added DESC";
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
I have a report page, where the results are groupd by day/month or week, depending on the switch value ( as seen in the code ).
What I need is to add a custom field "REP" which will sum the value of the rows under "REP_VALUE" in PRODUCT table.
I have REP_VALUE in PRODUCT table.
So the final need is to add this SUM value of REP_VALUE under the reports. I tried a few things, but it seems to be messed up, the code doesn't return proper things.
Here was my attempt, I added the following code inside the $sql query:
SUM((SELECT
SUM(myma_product.rep_value)
FROM
myma_product,
myma_order_product
WHERE
myma_product.product_id = myma_order_product.product_id)) as rep_values
But the result wasn't that I was looking for and the results were incorrect.
Tried a few other request but still unsuccessfully.