Completed
Push — master ( 3ecbe1...f7c2f1 )
by Jan
04:05
created

OrderTrait::getOrderQuantity()   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 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Entity\Parts\PartTraits;
33
34
35
use App\Entity\Parts\Part;
36
use App\Entity\PriceInformations\Orderdetail;
37
use App\Security\Annotations\ColumnSecurity;
38
use Doctrine\Common\Collections\Collection;
39
40
/**
41
 * This trait collects all aspects of a part related to orders and priceinformations.
42
 * @package App\Entity\Parts\PartTraits
43
 */
44
trait OrderTrait
45
{
46
    /**
47
     * @var Orderdetail[] The details about how and where you can order this part.
48
     * @ORM\OneToMany(targetEntity="App\Entity\PriceInformations\Orderdetail", mappedBy="part", cascade={"persist", "remove"}, orphanRemoval=false)
49
     * @Assert\Valid()
50
     * @ColumnSecurity(prefix="orderdetails", type="collection")
51
     */
52
    protected $orderdetails;
53
54
    /**
55
     * @var int
56
     * @ORM\Column(type="integer")
57
     * @ColumnSecurity(prefix="order", type="integer")
58
     */
59
    protected $order_quantity = 0;
60
61
    /**
62
     * @var bool
63
     * @ORM\Column(type="boolean")
64
     * @ColumnSecurity(prefix="order", type="boolean")
65
     */
66
    protected $manual_order = false;
67
68
    /**
69
     * @var Orderdetail
70
     * @ORM\OneToOne(targetEntity="App\Entity\PriceInformations\Orderdetail")
71
     * @ORM\JoinColumn(name="order_orderdetails_id", referencedColumnName="id")
72
     *
73
     * @ColumnSecurity(prefix="order", type="object")
74
     */
75
    protected $order_orderdetail;
76
77
    /**
78
     * Get the selected order orderdetails of this part.
79
     * @return Orderdetail the selected order orderdetails
80
     */
81
    public function getOrderOrderdetails(): ?Orderdetail
82
    {
83
        return $this->order_orderdetail;
84
    }
85
86
    /**
87
     * Get the order quantity of this part.
88
     * @return int the order quantity
89
     */
90
    public function getOrderQuantity(): int
91
    {
92
        return $this->order_quantity;
93
    }
94
95
    /**
96
     *  Check if this part is marked for manual ordering.
97
     *
98
     * @return bool the "manual_order" attribute
99
     */
100
    public function isMarkedForManualOrder(): bool
101
    {
102
        return $this->manual_order;
103
    }
104
105
    /**
106
     *  Get all orderdetails of this part.
107
     *
108
     * @param bool $hide_obsolete If true, obsolete orderdetails will NOT be returned
109
     *
110
     * @return Collection|Orderdetail[] * all orderdetails as a one-dimensional array of Orderdetails objects
111
     *                        (empty array if there are no ones)
112
     *                        * the array is sorted by the suppliers names / minimum order quantity
113
     *
114
     */
115
    public function getOrderdetails(bool $hide_obsolete = false) : Collection
116
    {
117
        //If needed hide the obsolete entries
118
        if ($hide_obsolete) {
119
            $orderdetails = $this->orderdetails;
120
            foreach ($orderdetails as $key => $details) {
121
                if ($details->getObsolete()) {
122
                    unset($orderdetails[$key]);
123
                }
124
            }
125
126
            return $orderdetails;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $orderdetails returns the type App\Entity\PriceInformations\Orderdetail[] which is incompatible with the type-hinted return Doctrine\Common\Collections\Collection.
Loading history...
127
        }
128
129
        return $this->orderdetails;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->orderdetails returns the type App\Entity\PriceInformations\Orderdetail[] which is incompatible with the type-hinted return Doctrine\Common\Collections\Collection.
Loading history...
130
    }
131
132
    /**
133
     * Adds the given orderdetail to list of orderdetails.
134
     * The orderdetail is assigned to this part.
135
     * @param Orderdetail $orderdetail The orderdetail that should be added.
136
     * @return self
137
     */
138
    public function addOrderdetail(Orderdetail $orderdetail) : self
139
    {
140
        $orderdetail->setPart($this);
141
        $this->orderdetails->add($orderdetail);
142
        return $this;
143
    }
144
145
    /**
146
     * Removes the given orderdetail from the list of orderdetails.
147
     * @param Orderdetail $orderdetail
148
     * @return OrderTrait
149
     */
150
    public function removeOrderdetail(Orderdetail $orderdetail) : self
151
    {
152
        $this->orderdetails->removeElement($orderdetail);
153
        return $this;
154
    }
155
156
    /**
157
     *  Set the "manual_order" attribute.
158
     *
159
     * @param bool     $new_manual_order          the new "manual_order" attribute
160
     * @param int      $new_order_quantity        the new order quantity
161
     * @param int|null $new_order_orderdetails_id * the ID of the new order orderdetails
162
     *                                            * or Zero for "no order orderdetails"
163
     *                                            * or NULL for automatic order orderdetails
164
     *                                            (if the part has exactly one orderdetails,
165
     *                                            set this orderdetails as order orderdetails.
166
     *                                            Otherwise, set "no order orderdetails")
167
     *
168
     * @return self
169
     */
170
    public function setManualOrder(bool $new_manual_order, int $new_order_quantity = 1, ?Orderdetail $new_order_orderdetail = null): Part
171
    {
172
        //Assert::greaterThan($new_order_quantity, 0, 'The new order quantity must be greater zero. Got %s!');
173
174
        $this->manual_order = $new_manual_order;
175
176
        //TODO;
177
        $this->order_orderdetail = $new_order_orderdetail;
178
        $this->order_quantity = $new_order_quantity;
179
180
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type App\Entity\Parts\PartTraits\OrderTrait which includes types incompatible with the type-hinted return App\Entity\Parts\Part.
Loading history...
181
    }
182
183
    /**
184
     * Check if this part is obsolete.
185
     *
186
     * A Part is marked as "obsolete" if all their orderdetails are marked as "obsolete".
187
     * If a part has no orderdetails, the part isn't marked as obsolete.
188
     *
189
     * @return bool true, if this part is obsolete. false, if this part isn't obsolete
190
     */
191
    public function isObsolete(): bool
192
    {
193
        $all_orderdetails = $this->getOrderdetails();
194
195
        if (0 === count($all_orderdetails)) {
196
            return false;
197
        }
198
199
        foreach ($all_orderdetails as $orderdetails) {
200
            if (!$orderdetails->getObsolete()) {
201
                return false;
202
            }
203
        }
204
205
        return true;
206
    }
207
208
}