Passed
Push — master ( 7bc770...7daa7a )
by Luiz Kim
31:44 queued 29:20
created

ProductService::purchasingSuggestionPrintData()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
c 1
b 0
f 0
nc 18
nop 3
dl 0
loc 39
rs 8.5866
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Device;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Device 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\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\Product;
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
12
class ProductService
13
{
14
    public function __construct(
15
        private EntityManagerInterface $manager,
16
        private Security $security,
17
        private PrintService $printService,
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PrintService 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...
18
        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...
19
    ) {}
20
21
    public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
0 ignored issues
show
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

21
    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

21
    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...
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

21
    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 $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

21
    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...
22
    {
23
        // $this->PeopleService->checkCompany('company', $queryBuilder, $resourceClass, $applyTo, $rootAlias);
24
    }
25
26
    public function getPurchasingSuggestion(People $company)
27
    {
28
        return $this->manager->getRepository(Product::class)->getPurchasingSuggestion($company);
29
    }
30
31
    public function purchasingSuggestionPrintData(?People $provider, string $printType, string $deviceType)
32
    {
33
        $products = $this->getPurchasingSuggestion($provider);
0 ignored issues
show
Bug introduced by
It seems like $provider can also be of type null; however, parameter $company of ControleOnline\Service\P...tPurchasingSuggestion() does only seem to accept ControleOnline\Entity\People, maybe add an additional type check? ( Ignorable by Annotation )

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

33
        $products = $this->getPurchasingSuggestion(/** @scrutinizer ignore-type */ $provider);
Loading history...
34
35
        $groupedByCompany = [];
36
        foreach ($products as $product) {
37
            $companyName = $product['company_name'] ?? 'Empresa Desconhecida';
38
            if (!isset($groupedByCompany[$companyName])) {
39
                $groupedByCompany[$companyName] = [];
40
            }
41
            $groupedByCompany[$companyName][] = $product;
42
        }
43
44
        $this->printService->addLine("", "", "-");
45
        $this->printService->addLine("SUGESTAO DE COMPRA", "", " ");
46
        $this->printService->addLine("", "", "-");
47
48
        foreach ($groupedByCompany as $companyName => $items) {
49
            $this->printService->addLine($companyName, "", " ");
50
            $this->printService->addLine("", "", "-");
51
            $this->printService->addLine("Produto", "Necessario", " ");
52
            $this->printService->addLine("", "", "-");
53
54
            foreach ($items as $item) {
55
                $productName = substr($item['product_name'], 0, 20);
56
                if (!empty($item['description'])) {
57
                    $productName .= " " . substr($item['description'], 0, 10);
58
                }
59
                if (!empty($item['unity'])) {
60
                    $productName .= " (" . $item['unity'] . ")";
61
                }
62
                $needed = str_pad($item['needed'], 4, " ", STR_PAD_LEFT);
63
                $this->printService->addLine($productName, $needed, " ");
64
            }
65
66
            $this->printService->addLine("", "", "-");
67
        }
68
69
        return $this->printService->generatePrintData($printType, $deviceType);
70
    }
71
72
73
    public function updateProductInventory(): void
74
    {
75
76
        $sql = "INSERT INTO `product_inventory` (
77
                `inventory_id`, 
78
                `product_id`, 
79
                `product_unity_id`, 
80
                `available`, 
81
                `ordered`, 
82
                `transit`, 
83
                `minimum`, 
84
                `maximum`, 
85
                `sales`
86
            )
87
            SELECT 
88
                op.`inventory_id`,
89
                op.`product_id`,
90
                p.`product_unity_id`,
91
                (SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:purchasing_status) THEN op.`quantity` ELSE 0 END) - 
92
                 SUM(CASE WHEN o.`order_type` = 'sale' AND o.`status_id` IN (:sales_status) THEN op.`quantity` ELSE 0 END)) AS `available`,
93
                SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:ordered_status) THEN op.`quantity` ELSE 0 END) AS `ordered`,
94
                SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:transit_status) THEN op.`quantity` ELSE 0 END) AS `transit`,
95
                0 AS `minimum`,
96
                0 AS `maximum`,
97
                SUM(CASE WHEN o.`order_type` = 'sale' AND o.`status_id` IN (:sales_status) THEN op.`quantity` ELSE 0 END) AS `sales`
98
            FROM `orders` o
99
            JOIN `order_product` op ON o.`id` = op.`order_id`
100
            JOIN `product` p ON op.`product_id` = p.`id`
101
            WHERE o.`order_type` IN ('purchasing', 'sale')
102
              AND o.`status_id` IN (:all_status)
103
              AND p.`type` IN ('product', 'feedstock')
104
              AND (
105
                  (o.`order_type` = 'sale' AND o.`provider_id` IN (:provider_id))
106
                  OR
107
                  (o.`order_type` = 'purchasing' AND o.`client_id` IN (:client_id))
108
              )
109
            GROUP BY op.`inventory_id`, op.`product_id`, p.`product_unity_id`
110
            ON DUPLICATE KEY UPDATE
111
                `available` = VALUES(`available`),
112
                `ordered` = VALUES(`ordered`),
113
                `transit` = VALUES(`transit`),
114
                `sales` = VALUES(`sales`)
115
        ";
116
117
118
        $purchasingStatus = [7];
119
        $orderedStatus = [5];
120
        $transitStatus = [6];
121
        $salesStatus = [6];
122
        $companies = [1, 2, 3];
123
        $allStatus = array_unique(array_merge($purchasingStatus, $orderedStatus, $transitStatus, $salesStatus));
124
125
        try {
126
            $stmt = $this->manager->getConnection()->prepare($sql);
127
128
            $stmt->bindValue('purchasing_status', implode(',', $purchasingStatus), \PDO::PARAM_STR);
129
            $stmt->bindValue('sales_status', implode(',', $salesStatus), \PDO::PARAM_STR);
130
            $stmt->bindValue('ordered_status', implode(',', $orderedStatus), \PDO::PARAM_STR);
131
            $stmt->bindValue('transit_status', implode(',', $transitStatus), \PDO::PARAM_STR);
132
            $stmt->bindValue('all_status', implode(',', $allStatus), \PDO::PARAM_STR);
133
            $stmt->bindValue('provider_id', implode(',', $companies), \PDO::PARAM_STR);
134
            $stmt->bindValue('client_id', implode(',', $companies), \PDO::PARAM_STR);
135
136
            $stmt->executeQuery();
137
        } catch (\Exception $e) {
138
            throw new \Exception("Erro ao atualizar o estoque: " . $e->getMessage());
139
        }
140
    }
141
}
142