-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathklipmodule.php
More file actions
118 lines (95 loc) · 3.72 KB
/
klipmodule.php
File metadata and controls
118 lines (95 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class klipmodule extends Module
{
public function __construct()
{
$this-> name = 'klipmodule';
$this->tab = 'dashboard';
$this->author = 'Vilius, Dainius, Linas';
$this->version = '1.0.0';
$this->description = 'This module will display top selling products on the homepage';
$this->bootstrap = true;
parent:: __construct();
$this->description = $this->trans('Showing Top Selling products created by Dainius,Vilius and Linas', [], 'Modules.klipmodule.Admin');
}
public function getContent()
{
$message = null;
$isError = false;
if (Tools::isSubmit('send_top_selling')) {
$result = $this->sendTopSellingToApi();
$message = $result['message'];
$isError = $result['error'];
}
$this->smarty->assign([
'message' => $message,
'isError' => $isError
]);
$output = $this->display(__FILE__, 'views/templates/messages.tpl');
$output .= $this->renderTable();
$output .= $this->sendButton();
return $output;
}
public function renderTable()
{
$topSellingProducts = $this->getTopSellingProducts();
$this->context->smarty->assign('products', $topSellingProducts);
return $this->display(__FILE__, 'views/templates/table.tpl');
}
public function sendButton()
{
return $this->display(__FILE__, 'views/templates/button.tpl');
}
public function getTopSellingProducts($limit = 5)
{
// SQL query to get top-selling products
$query = new DbQuery();
$query->select('p.id_product, pl.name, p.reference, SUM(od.product_quantity) AS total_sales');
$query->from('orders', 'o');
$query->innerJoin('order_detail', 'od', 'o.id_order = od.id_order');
$query->innerJoin('product', 'p', 'od.product_id = p.id_product');
$query->innerJoin('product_lang', 'pl', 'p.id_product = pl.id_product');
$query->where('o.valid = 1');
$query->where('pl.id_lang = ' . (int)$this->context->language->id);
$query->groupBy('p.id_product');
$query->orderBy('total_sales DESC');
$query->limit((int)$limit);
// Return the result of the query
return Db::getInstance()->executeS($query);
}
public function sendTopSellingToApi()
{
$topProducts = $this->getTopSellingProducts();
$apiUrl = 'https://localhost/api/products/save';
$formattedProducts = [];
foreach ($topProducts as $product) {
$formattedProducts[] = [
'productId' => (int) $product['id_product'],
'name' => $product['name'],
'totalSold' => (int) $product['total_sales'],
];
}
$payload = json_encode($formattedProducts);
// Initialize cURL
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // disable SSL for development
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json',
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 200 && $httpCode < 300) {
return ['message' => 'All products sent successfully!', 'error' => false];
} else {
return ['message' => 'Failed to send products. HTTP status' . $httpCode, 'error' => true];
}
}
}