Passed
Push — master ( 0be31e...6fb6f7 )
by Luiz Kim
02:22
created

ProductService::getPurchasingSuggestion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ControleOnline\Service;
4
5
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...
6
use ControleOnline\Entity\Product;
7
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...
8
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...
9
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...
10
11
class ProductService
12
{
13
    public function __construct(
14
        private EntityManagerInterface $manager,
15
        private Security $security,
16
        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...
17
    ) {}
18
19
    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

19
    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

19
    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...
Unused Code introduced by
The parameter $queryBuilder 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

19
    public function securityFilter(/** @scrutinizer ignore-unused */ QueryBuilder $queryBuilder, $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...
Unused Code introduced by
The parameter $rootAlias 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

19
    public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, /** @scrutinizer ignore-unused */ $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...
20
    {
21
        // $this->PeopleService->checkCompany('company', $queryBuilder, $resourceClass, $applyTo, $rootAlias);
22
    }
23
24
    public function getPurchasingSuggestion(People $company)
25
    {
26
        return $this->manager->getRepository(Product::class)->getPurchasingSuggestion($company);
27
    }
28
    
29
    public function updateProductInventory(): void
30
    {
31
32
        $sql = "INSERT INTO `product_inventory` (
33
                `inventory_id`, 
34
                `product_id`, 
35
                `product_unity_id`, 
36
                `available`, 
37
                `ordered`, 
38
                `transit`, 
39
                `minimum`, 
40
                `maximum`, 
41
                `sales`
42
            )
43
            SELECT 
44
                op.`inventory_id`,
45
                op.`product_id`,
46
                p.`product_unity_id`,
47
                (SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:purchasing_status) THEN op.`quantity` ELSE 0 END) - 
48
                 SUM(CASE WHEN o.`order_type` = 'sale' AND o.`status_id` IN (:sales_status) THEN op.`quantity` ELSE 0 END)) AS `available`,
49
                SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:ordered_status) THEN op.`quantity` ELSE 0 END) AS `ordered`,
50
                SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:transit_status) THEN op.`quantity` ELSE 0 END) AS `transit`,
51
                0 AS `minimum`,
52
                0 AS `maximum`,
53
                SUM(CASE WHEN o.`order_type` = 'sale' AND o.`status_id` IN (:sales_status) THEN op.`quantity` ELSE 0 END) AS `sales`
54
            FROM `orders` o
55
            JOIN `order_product` op ON o.`id` = op.`order_id`
56
            JOIN `product` p ON op.`product_id` = p.`id`
57
            WHERE o.`order_type` IN ('purchasing', 'sale')
58
              AND o.`status_id` IN (:all_status)
59
              AND p.`type` IN ('product', 'feedstock')
60
              AND (
61
                  (o.`order_type` = 'sale' AND o.`provider_id` IN (:provider_id))
62
                  OR
63
                  (o.`order_type` = 'purchasing' AND o.`client_id` IN (:client_id))
64
              )
65
            GROUP BY op.`inventory_id`, op.`product_id`, p.`product_unity_id`
66
            ON DUPLICATE KEY UPDATE
67
                `available` = VALUES(`available`),
68
                `ordered` = VALUES(`ordered`),
69
                `transit` = VALUES(`transit`),
70
                `sales` = VALUES(`sales`)
71
        ";
72
73
74
        $purchasingStatus = [7];
75
        $orderedStatus = [5];
76
        $transitStatus = [6];
77
        $salesStatus = [6];
78
        $companies = [1, 2, 3];
79
        $allStatus = array_unique(array_merge($purchasingStatus, $orderedStatus, $transitStatus, $salesStatus));
80
81
        try {
82
            $stmt = $this->manager->getConnection()->prepare($sql);
83
84
            $stmt->bindValue('purchasing_status', implode(',', $purchasingStatus), \PDO::PARAM_STR);
85
            $stmt->bindValue('sales_status', implode(',', $salesStatus), \PDO::PARAM_STR);
86
            $stmt->bindValue('ordered_status', implode(',', $orderedStatus), \PDO::PARAM_STR);
87
            $stmt->bindValue('transit_status', implode(',', $transitStatus), \PDO::PARAM_STR);
88
            $stmt->bindValue('all_status', implode(',', $allStatus), \PDO::PARAM_STR);
89
            $stmt->bindValue('provider_id', implode(',', $companies), \PDO::PARAM_STR);
90
            $stmt->bindValue('client_id', implode(',', $companies), \PDO::PARAM_STR);
91
92
            $stmt->executeQuery();
93
        } catch (\Exception $e) {
94
            throw new \Exception("Erro ao atualizar o estoque: " . $e->getMessage());
95
        }
96
    }
97
}
98