1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
4
|
|
|
|
5
|
|
|
use ControleOnline\Entity\Order; |
6
|
|
|
use ControleOnline\Entity\People; |
|
|
|
|
7
|
|
|
use ControleOnline\Entity\Status; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
9
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
10
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
|
|
|
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
|
|
12
|
|
|
|
13
|
|
|
class OrderService |
14
|
|
|
{ |
15
|
|
|
private $request; |
16
|
|
|
|
17
|
|
|
public function __construct( |
18
|
|
|
private EntityManagerInterface $manager, |
19
|
|
|
private Security $security, |
20
|
|
|
private PeopleService $peopleService, |
|
|
|
|
21
|
|
|
RequestStack $requestStack |
22
|
|
|
|
23
|
|
|
) { |
24
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getPurchasingSuggestion(People $company) |
28
|
|
|
{ |
29
|
|
|
return $this->manager->getRepository(Order::class)->getPurchasingSuggestion($company); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function calculateOrderPrice(Order $order) |
33
|
|
|
{ |
34
|
|
|
$sql = 'UPDATE orders O |
35
|
|
|
JOIN ( |
36
|
|
|
SELECT order_id, IFNULL(SUM(total), 0) AS new_total |
37
|
|
|
FROM order_product |
38
|
|
|
WHERE order_product_id IS NULL |
39
|
|
|
GROUP BY order_id |
40
|
|
|
) AS subquery ON O.id = subquery.order_id |
41
|
|
|
SET O.price = IFNULL(subquery.new_total, 0) |
42
|
|
|
WHERE O.id = :order_id; |
43
|
|
|
'; |
44
|
|
|
$connection = $this->manager->getConnection(); |
45
|
|
|
$statement = $connection->prepare($sql); |
46
|
|
|
$statement->execute(['order_id' => $order->getId()]); |
47
|
|
|
|
48
|
|
|
return $order; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
public function calculateGroupProductPrice(Order $order) |
53
|
|
|
{ |
54
|
|
|
$sql = 'UPDATE order_product OPO |
55
|
|
|
JOIN ( |
56
|
|
|
SELECT SUM(calculated_price) AS calculated_price,order_product_id FROM ( SELECT |
57
|
|
|
PG.product_group, |
58
|
|
|
P.product, |
59
|
|
|
PG.price_calculation, |
60
|
|
|
OP.order_product_id, |
61
|
|
|
(CASE |
62
|
|
|
WHEN PG.price_calculation = "biggest" THEN MAX(PGP.price) |
63
|
|
|
WHEN PG.price_calculation = "sum" THEN SUM(PGP.price) |
64
|
|
|
WHEN PG.price_calculation = "average" THEN AVG(PGP.price) |
65
|
|
|
WHEN PG.price_calculation = "free" THEN 0 |
66
|
|
|
ELSE NULL |
67
|
|
|
END) AS calculated_price |
68
|
|
|
|
69
|
|
|
FROM order_product OP |
70
|
|
|
INNER JOIN product_group PG ON OP.product_group_id = PG.id |
71
|
|
|
INNER JOIN product_group_product PGP ON PGP.product_group_id = OP.product_group_id AND PGP.product_child_id = OP.product_id |
72
|
|
|
INNER JOIN product P ON P.id = OP.product_id |
73
|
|
|
WHERE OP.parent_product_id IS NOT NULL AND OP.order_id = :order_id |
74
|
|
|
GROUP BY OP.order_product_id,PG.id |
75
|
|
|
) AS SBG GROUP BY SBG.order_product_id |
76
|
|
|
|
77
|
|
|
) AS subquery ON OPO.id = subquery.order_product_id |
78
|
|
|
SET OPO.price = subquery.calculated_price,OPO.total = (subquery.calculated_price * OPO.quantity) |
79
|
|
|
'; |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
$connection = $this->manager->getConnection(); |
83
|
|
|
$statement = $connection->prepare($sql); |
84
|
|
|
$statement->execute(['order_id' => $order->getId()]); |
85
|
|
|
return $order; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
public function createOrder(People $receiver, People $payer) |
90
|
|
|
{ |
91
|
|
|
$status = $this->manager->getRepository(Status::class)->findOneBy([ |
92
|
|
|
'status' => 'waiting payment', |
93
|
|
|
'context' => 'order' |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$order = new Order(); |
97
|
|
|
$order->setProvider($receiver); |
98
|
|
|
$order->setClient($payer); |
99
|
|
|
$order->setPayer($payer); |
100
|
|
|
$order->setOrderType('sale'); |
101
|
|
|
$order->setStatus($status); |
102
|
|
|
$order->setApp('Asaas'); |
103
|
|
|
|
104
|
|
|
$this->manager->persist($order); |
105
|
|
|
$this->manager->flush(); |
106
|
|
|
return $order; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$companies = $this->peopleService->getMyCompanies(); |
112
|
|
|
|
113
|
|
|
if ($invoice = $this->request->query->get('invoiceId', null)) { |
114
|
|
|
$queryBuilder->join(sprintf('%s.invoice', $rootAlias), 'OrderInvoice'); |
115
|
|
|
$queryBuilder->andWhere(sprintf('OrderInvoice.invoice IN(:invoice)', $rootAlias, $rootAlias)); |
116
|
|
|
$queryBuilder->setParameter('invoice', $invoice); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$queryBuilder->andWhere(sprintf('%s.client IN(:companies) OR %s.provider IN(:companies)', $rootAlias, $rootAlias)); |
120
|
|
|
$queryBuilder->setParameter('companies', $companies); |
121
|
|
|
|
122
|
|
|
if ($provider = $this->request->query->get('provider', null)) { |
123
|
|
|
$queryBuilder->andWhere(sprintf('%s.provider IN(:provider)', $rootAlias)); |
124
|
|
|
$queryBuilder->setParameter('provider', preg_replace("/[^0-9]/", "", $provider)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($client = $this->request->query->get('client', null)) { |
128
|
|
|
$queryBuilder->andWhere(sprintf('%s.client IN(:client)', $rootAlias)); |
129
|
|
|
$queryBuilder->setParameter('client', preg_replace("/[^0-9]/", "", $client)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths