|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ControleOnline\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use ControleOnline\Entity\Product; |
|
6
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
|
|
|
|
|
7
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
|
|
|
|
|
8
|
|
|
use ControleOnline\Entity\People; |
|
|
|
|
|
|
9
|
|
|
use ControleOnline\Service\PeopleService; |
|
|
|
|
|
|
10
|
|
|
use Doctrine\DBAL\DBALException; |
|
|
|
|
|
|
11
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @method Product|null find($id, $lockMode = null, $lockVersion = null) |
|
15
|
|
|
* @method Product|null findOneBy(array $criteria, array $orderBy = null) |
|
16
|
|
|
* @method Product[] findAll() |
|
17
|
|
|
* @method Product[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
18
|
|
|
*/ |
|
19
|
|
|
class ProductRepository extends ServiceEntityRepository |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
private $inventory_exclude_types = ['custom', 'component', 'manufactured']; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct( |
|
25
|
|
|
private PeopleService $peopleService, |
|
26
|
|
|
ManagerRegistry $registry |
|
27
|
|
|
) { |
|
28
|
|
|
parent::__construct($registry, Product::class); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function updateProductInventory(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$companies = implode(',', array_map( |
|
34
|
|
|
fn($c) => $c->getId(), |
|
35
|
|
|
$this->peopleService->getMyCompanies() |
|
36
|
|
|
)); |
|
37
|
|
|
|
|
38
|
|
|
$purchasing_status = '7'; |
|
39
|
|
|
$ordered_status = '5'; |
|
40
|
|
|
$transit_status = '6'; |
|
41
|
|
|
$sales_status = '6,7'; |
|
42
|
|
|
$all_status = '5,6,7'; |
|
43
|
|
|
|
|
44
|
|
|
try { |
|
45
|
|
|
$conn = $this->getEntityManager()->getConnection(); |
|
46
|
|
|
$conn->executeStatement( |
|
47
|
|
|
'CALL update_product_inventory(?, ?, ?, ?, ?, ?)', |
|
48
|
|
|
[ |
|
49
|
|
|
$companies, |
|
50
|
|
|
$purchasing_status, |
|
51
|
|
|
$ordered_status, |
|
52
|
|
|
$transit_status, |
|
53
|
|
|
$sales_status, |
|
54
|
|
|
$all_status |
|
55
|
|
|
] |
|
56
|
|
|
); |
|
57
|
|
|
} catch (\Exception $e) { |
|
58
|
|
|
throw new \Exception($e->getMessage()); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getProductsInventory(?People $company): array |
|
63
|
|
|
{ |
|
64
|
|
|
$qb = $this->createQueryBuilder('p'); |
|
65
|
|
|
$qb->select([ |
|
66
|
|
|
'p.id as product_id', |
|
67
|
|
|
'p.product as product_name', |
|
68
|
|
|
'p.description', |
|
69
|
|
|
'pu.productUnit', |
|
70
|
|
|
'pi.available', |
|
71
|
|
|
'pi.minimum', |
|
72
|
|
|
'pi.maximum', |
|
73
|
|
|
'c.name as company_name', |
|
74
|
|
|
'i.inventory as inventory_name' |
|
75
|
|
|
]) |
|
76
|
|
|
->join('p.productUnit', 'pu') |
|
77
|
|
|
->join('p.company', 'c') |
|
78
|
|
|
->leftJoin('ControleOnline\Entity\ProductInventory', 'pi', 'WITH', 'pi.product = p.id') |
|
79
|
|
|
->leftJoin('pi.inventory', 'i') |
|
80
|
|
|
->orderBy('p.product', 'ASC') |
|
81
|
|
|
->andWhere('p.type NOT IN (:excludedTypes)') |
|
82
|
|
|
->setParameter('excludedTypes', $this->inventory_exclude_types); |
|
83
|
|
|
|
|
84
|
|
|
if ($company !== null) { |
|
85
|
|
|
$qb->andWhere('p.company = :company') |
|
86
|
|
|
->setParameter('company', $company); |
|
87
|
|
|
} |
|
88
|
|
|
return $qb->getQuery()->getArrayResult(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
public function getPurchasingSuggestion(?People $company): array |
|
93
|
|
|
{ |
|
94
|
|
|
$this->updateProductInventory(); |
|
95
|
|
|
$qb = $this->createQueryBuilder('p') |
|
96
|
|
|
->select([ |
|
97
|
|
|
'pe.id AS company_id', |
|
98
|
|
|
'pe.name AS company_name', |
|
99
|
|
|
'pe.alias AS company_alias', |
|
100
|
|
|
'p.id AS product_id', |
|
101
|
|
|
'p.sku AS sku', |
|
102
|
|
|
'p.product AS product_name', |
|
103
|
|
|
'p.description AS description', |
|
104
|
|
|
'p.type AS type', |
|
105
|
|
|
'SUM(pi.available + pi.ordered + pi.transit - pi.sales) AS stock', |
|
106
|
|
|
'SUM(pi.minimum) AS minimum', |
|
107
|
|
|
'CASE WHEN SUM(pi.available + pi.ordered + pi.transit - pi.minimum - pi.sales) * -1 < 0 THEN 0 ELSE SUM(pi.available + pi.ordered + pi.transit - pi.minimum - pi.sales) * -1 END AS needed', |
|
108
|
|
|
'pu.productUnit AS unity' |
|
109
|
|
|
]) |
|
110
|
|
|
->join('p.company', 'pe') |
|
111
|
|
|
->leftJoin('ControleOnline\Entity\ProductInventory', 'pi', 'WITH', 'pi.product = p.id') |
|
112
|
|
|
->join('p.productUnit', 'pu') |
|
113
|
|
|
->andWhere('p.type NOT IN (:excludedTypes)') |
|
114
|
|
|
->andWhere('pe IN (:companies)') |
|
115
|
|
|
->groupBy('p.id, p.product, p.description, p.sku, pe.id, pe.name, pe.alias, pu.productUnit') |
|
116
|
|
|
->having('needed > 0') |
|
117
|
|
|
->addOrderBy('p.product', 'ASC') |
|
118
|
|
|
->setParameter('excludedTypes', $this->inventory_exclude_types) |
|
119
|
|
|
->setParameter( |
|
120
|
|
|
'companies', |
|
121
|
|
|
$this->peopleService->getMyCompanies() |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
if ($company) |
|
125
|
|
|
$qb->andWhere('pe = :company')->setParameter('company', $company); |
|
126
|
|
|
|
|
127
|
|
|
return $qb->getQuery()->getResult(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function findProductBySkuAsInteger(int $sku, People $company): ?Product |
|
131
|
|
|
{ |
|
132
|
|
|
$conn = $this->getEntityManager()->getConnection(); |
|
133
|
|
|
$sql = 'SELECT * FROM product WHERE CAST(sku AS UNSIGNED) = :sku AND company_id = :company'; |
|
134
|
|
|
$stmt = $conn->prepare($sql); |
|
135
|
|
|
$stmt->bindValue('sku', $sku); |
|
136
|
|
|
$stmt->bindValue('company', $company->getId()); |
|
137
|
|
|
$result = $stmt->executeQuery(); |
|
138
|
|
|
|
|
139
|
|
|
$data = $result->fetchAssociative(); |
|
140
|
|
|
return $data ? $this->getEntityManager()->getRepository(Product::class)->find($data['id']) : null; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
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