initial commit
[namibia] / module / PriceGuide / src / PriceGuide / Entity / PriceGuide.php
1 <?php
2 namespace PriceGuide\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7
8 /**
9  * A Price Guide item.
10  *
11  * @ORM\Entity
12  * @ORM\HasLifecycleCallbacks
13  * @ORM\Table(name="price_guide")
14  */
15 class PriceGuide
16 {
17
18         /**
19          * Can archive records.
20          */
21         const ARCHIVE = true;
22         /**
23          * Pull Synchronization Strategy for this table.
24          */
25         const PULL_SYNCH_STRATEGY = false;
26         /**
27          * Push Synchronization Strategy for this table.
28          */
29         const PUSH_SYNCH_STRATEGY = false;
30
31
32
33         /* ------------------------------------ Identification ------------------------------------ */
34         /**
35          * @ORM\Id
36          * @ORM\Column(type="integer");
37          * @ORM\GeneratedValue(strategy="AUTO")
38          */
39         protected $id;
40
41
42         /* ------------------------------------ Ownership ------------------------------------ */
43         /**
44          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
45          * @ORM\JoinColumn(nullable=false, name="created_by_profile_id")
46          **/
47         protected $createdBy;
48
49         /**
50          * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
51          * @ORM\JoinColumn(nullable=false, name="company_id")
52          **/
53         protected $company;
54
55         /**
56          * @ORM\ManyToOne(targetEntity="\Valuation\Entity\Valuation")
57          * @ORM\JoinColumn(nullable=true, name="stock_valuation_id")
58          **/
59         protected $valuation;
60
61         /**
62          * @ORM\ManyToOne(targetEntity="\Stock\Entity\Stock")
63          * @ORM\JoinColumn(nullable=false, name="stock_id")
64          **/
65         protected $stock;
66
67
68         /* ------------------------------------ Linkage ------------------------------------ */
69         /**
70          * @ORM\OneToMany(targetEntity="ClubStock", mappedBy="priceGuideStock", cascade={"all"}, fetch="EXTRA_LAZY")
71          **/
72         protected $clubs;
73
74         /**
75          * @ORM\OneToMany(targetEntity="MemberStock", mappedBy="priceGuideStock", cascade={"all"}, fetch="EXTRA_LAZY")
76          **/
77         protected $members;
78
79         /**
80          * @ORM\OneToMany(targetEntity="Offer", mappedBy="priceGuideStock", cascade={"all"}, fetch="EXTRA_LAZY")
81          **/
82         protected $offers;
83
84
85         /* ------------------------------------ Tracking ------------------------------------ */
86         /**
87          * @ORM\Column(type="string", length=25, nullable=true, name="previous_status");
88          */
89         protected $previousState;
90
91         /**
92          * @ORM\Column(nullable=true, type="string", length=25, name="status");
93          */
94         protected $jobState = 'Open4Offers';
95
96         /**
97          * @ORM\Column(type="datetime");
98          */
99         protected $created;
100
101         /**
102          * @ORM\Column(type="datetime", nullable=true);
103          */
104         protected $updated;
105
106         /**
107          * @ORM\Column(type="boolean");
108          */
109         protected $archived = false;
110
111
112
113         /**
114          * Initialize collections.
115          */
116         public function __construct()
117         {
118                 $this->clubs  = new \Doctrine\Common\Collections\ArrayCollection();
119                 $this->offers = new \Doctrine\Common\Collections\ArrayCollection();
120         }
121
122         /**
123          * Add a new ClubStock to this Stock Item.
124          * @param Damage $damage
125          * @return ClubStock
126          */
127         public function addClubStock(ClubStock $clubStock)
128         {
129                 $this->clubs[] = $clubStock;
130                 return $this;
131         }
132
133
134
135         /**
136          * Magic getter to expose protected properties.
137          *
138          * @param string $property
139          * @return mixed
140          */
141         public function __get($property)
142         {
143                 return $this->$property;
144         }
145
146         /**
147          * Magic setter to save protected properties.
148          *
149          * @param string $property
150          * @param mixed $value
151          */
152         public function __set($property, $value)
153         {
154                 $this->$property = $value;
155         }
156
157         /**
158          * @ORM\PrePersist
159          */
160         public function setCreateTime()
161         {
162                 $this->company   = \Utility\Registry::resolveCompanyContext($this->company);
163                 $this->createdBy = \Utility\Registry::resolveProfileContext($this->createdBy);
164                 $this->created = new \DateTime("now");
165         }
166
167         /**
168          * @ORM\PreUpdate
169          */
170         public function setUpdateTime()
171         {
172                 $this->updated = new \DateTime("now");
173         }
174
175         /**
176          * Convert the object to an array.
177          * @param array $expand
178          * @param array $intersect
179          * @param boolean $showIdentifiers
180          * @param integer $expandAll
181          * @return array
182          */
183         public function toArray(
184                         array $expand = array(), array $intersect = array(),
185                         $showIdentifiers = false, $expandAll = 0
186                         )
187         {
188                 $intersect = array_flip($intersect);
189                 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
190                 $includeAll = empty($intersect);
191                 $data = array();
192                 ($includeAll || isset($intersect['id']))
193                         && $data['id'] = $this->id;
194                 ($includeAll || isset($intersect['company']))
195                         && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
196                                                                         && !is_null($this->company)
197                                 ? (!$showIdentifiers ? $this->company->toArray(
198                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
199                                                 ) : $this->company->id)
200                                 : null;
201                 ($includeAll || isset($intersect['stock']))
202                         && $data['stock'] = (in_array('stock', $expand) || $expandAll || $showIdentifiers)
203                                                                         && !is_null($this->stock)
204                                 ? (!$showIdentifiers ? $this->stock->toArray(
205                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
206                                                 ) : $this->stock->id)
207                                 : null;
208                 ($includeAll || isset($intersect['clubs']))
209                         && $data['clubs'] = (in_array('clubs', $expand) || $expandAll || $showIdentifiers)
210                                                                                 && !is_null($this->clubs)
211                                 ? $this->clubsToArray($showIdentifiers)
212                                 : null;
213                 (($includeAll || isset($intersect['clubs'])) && in_array('club-ids', $expand))
214                         && $data['clubs'] = (in_array('club-ids', $expand) || $expandAll || $showIdentifiers)
215                                                                                 && !is_null($this->clubs)
216                                 ? $this->clubsToArray(true)
217                                 : null;
218                 ($includeAll || isset($intersect['offers']))
219                         && $data['offers'] = (in_array('offers', $expand) || $expandAll || $showIdentifiers)
220                                                                                 && !is_null($this->offers)
221                                 ? $this->offersToArray($showIdentifiers)
222                                 : null;
223                 ($includeAll || isset($intersect['jobState']))
224                         && $data['jobState'] = $this->jobState;
225                 ($includeAll || isset($intersect['created']))
226                         && $data['created'] = !is_null($this->created)
227                                 ? $this->created->format($dateTimeFormat)
228                                 : null;
229                 ($includeAll || isset($intersect['updated']))
230                         && $data['updated'] = !is_null($this->updated)
231                                 ? $this->updated->format($dateTimeFormat)
232                                 : null;
233                 return $data;
234         }
235
236         /**
237          * Internal utility to change clubs collection into array.
238          * @param boolean $showIdentifiers
239          * @return array
240          */
241         protected function clubsToArray($showIdentifiers = false)
242         {
243                 $data = array();
244                 $iterator = $this->clubs->getIterator();
245                 foreach ($iterator as $club)
246                 {
247                         $data[] = $showIdentifiers
248                                 ? array('id' => $club->id)
249                                 : $club->toArray();
250                 }
251                 return $data;
252         }
253
254         /**
255          * Internal utility to change stock offers collection into array.
256          * @param boolean $showIdentifiers
257          * @return array
258          */
259         protected function offersToArray($showIdentifiers = false)
260         {
261                 $data = array();
262                 $iterator = $this->offers->getIterator();
263                 foreach ($iterator as $offer)
264                 {
265                         $data[] = $showIdentifiers
266                                 ? array('id' => $offer->id)
267                                 : $offer->toArray();
268                 }
269                 return $data;
270         }
271
272         /**
273          * Populate from an array.
274          * @param array $data
275          */
276         public function fromArray($data = array())
277         {
278                 isset($data['id'])
279                         && $this->id = $data['id'];
280                 isset($data['company'])
281                         && $this->company = $data['company'];
282                 isset($data['valuation'])
283                         && $this->valuation = $data['valuation'];
284                 isset($data['stock'])
285                         && $this->stock = $data['stock'];
286                 if (isset($data['clubs']))
287                 {
288                         $em = \Utility\Registry::getEntityManager();
289                         foreach ($data['clubs'] as $cl)
290                         {
291                                 if (!is_numeric($cl['id']))
292                                 {
293                                         continue;
294                                 }
295                                 $clubStock = new \PriceGuide\Entity\ClubStock();
296                                 $clubStock->club = $em->getReference('\PriceGuide\Entity\Club', $cl['id']);
297                                 if (is_null($clubStock->club))
298                                 {
299                                         continue;
300                                 }
301                                 $clubStock->priceGuideStock = $this;
302                                 $em->persist($clubStock);
303                                 $this->clubs->add($clubStock);
304                         }
305                 }
306         }
307
308
309
310 }
311