Passed
Push — master ( a4de54...b733d2 )
by Luiz Kim
04:33 queued 02:04
created

OrderService::calculateOrderPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 5
c 4
b 0
f 0
dl 0
loc 17
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Order;
6
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use ControleOnline\Entity\Status;
8
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\Security\Core\Security;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Core\Security was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Doctrine\ORM\QueryBuilder;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\QueryBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\HttpFoundation\RequestStack;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\RequestStack was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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,
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PeopleService was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
        RequestStack $requestStack
22
23
    ) {
24
        $this->request  = $requestStack->getCurrentRequest();
25
    }
26
27
    public function calculateOrderPrice(Order $order)
28
    {
29
        $sql = 'UPDATE orders O
30
                JOIN (
31
                    SELECT order_id, IFNULL(SUM(total), 0) AS new_total
32
                    FROM order_product
33
                    WHERE order_product_id IS NULL
34
                    GROUP BY order_id
35
                ) AS subquery ON O.id = subquery.order_id
36
                SET O.price = IFNULL(subquery.new_total, 0)
37
                WHERE O.id = :order_id;
38
                ';
39
        $connection = $this->manager->getConnection();
40
        $statement = $connection->prepare($sql);
41
        $statement->execute(['order_id' =>  $order->getId()]);
42
43
        return $order;
44
    }
45
46
47
    public function calculateGroupProductPrice(Order $order)
48
    {
49
        $sql = 'UPDATE order_product OPO
50
                JOIN (
51
                        SELECT SUM(calculated_price) AS calculated_price,order_product_id FROM (	SELECT 
52
                                PG.product_group,
53
                                P.product,
54
                                PG.price_calculation,
55
                                OP.order_product_id,
56
                                (CASE 
57
                                    WHEN PG.price_calculation = "biggest" THEN MAX(PGP.price)
58
                                    WHEN PG.price_calculation = "sum" THEN SUM(PGP.price)
59
                                    WHEN PG.price_calculation = "average" THEN AVG(PGP.price) 
60
                                    WHEN PG.price_calculation = "free" THEN 0
61
                                    ELSE NULL
62
                                END)  AS calculated_price
63
                                
64
                            FROM order_product OP
65
                            INNER JOIN product_group PG ON OP.product_group_id = PG.id
66
                            INNER JOIN product_group_product PGP ON PGP.product_group_id = OP.product_group_id AND PGP.product_child_id = OP.product_id
67
                            INNER JOIN product P ON P.id = OP.product_id
68
                            WHERE OP.parent_product_id IS NOT NULL AND OP.order_id = :order_id
69
                            GROUP BY OP.order_product_id,PG.id
70
                        ) AS SBG GROUP BY SBG.order_product_id
71
                        
72
                ) AS subquery ON OPO.id = subquery.order_product_id
73
                SET OPO.price = subquery.calculated_price,OPO.total = (subquery.calculated_price * OPO.quantity)
74
                ';
75
76
77
        $connection = $this->manager->getConnection();
78
        $statement = $connection->prepare($sql);
79
        $statement->execute(['order_id' =>  $order->getId()]);
80
        return $order;
81
    }
82
83
84
    public function createOrder(People $receiver, People $payer)
85
    {
86
        $status = $this->manager->getRepository(Status::class)->findOneBy([
87
            'status' => 'waiting payment',
88
            'context' => 'order'
89
        ]);
90
91
        $order = new Order();
92
        $order->setProvider($receiver);
93
        $order->setClient($payer);
94
        $order->setPayer($payer);
95
        $order->setOrderType('sale');
96
        $order->setStatus($status);
97
        $order->setApp('Asaas');
98
99
        $this->manager->persist($order);
100
        $this->manager->flush();
101
        return $order;
102
    }
103
104
    public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $applyTo is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

104
    public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, /** @scrutinizer ignore-unused */ $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resourceClass is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

104
    public function securityFilter(QueryBuilder $queryBuilder, /** @scrutinizer ignore-unused */ $resourceClass = null, $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
    {
106
        $companies   = $this->peopleService->getMyCompanies();
107
108
        if ($invoice = $this->request->query->get('invoiceId', null)) {
109
            $queryBuilder->join(sprintf('%s.invoice', $rootAlias), 'OrderInvoice');
110
            $queryBuilder->andWhere(sprintf('OrderInvoice.invoice IN(:invoice)', $rootAlias, $rootAlias));
111
            $queryBuilder->setParameter('invoice', $invoice);
112
        }
113
114
        $queryBuilder->andWhere(sprintf('%s.client IN(:companies) OR %s.provider IN(:companies)', $rootAlias, $rootAlias));
115
        $queryBuilder->setParameter('companies', $companies);
116
117
        if ($provider = $this->request->query->get('provider', null)) {
118
            $queryBuilder->andWhere(sprintf('%s.provider IN(:provider)', $rootAlias));
119
            $queryBuilder->setParameter('provider', preg_replace("/[^0-9]/", "", $provider));
120
        }
121
122
        if ($client = $this->request->query->get('client', null)) {
123
            $queryBuilder->andWhere(sprintf('%s.client IN(:client)', $rootAlias));
124
            $queryBuilder->setParameter('client', preg_replace("/[^0-9]/", "", $client));
125
        }
126
    }
127
}
128