vendor/roothirsch/shop-bundle/Entity/Order.php line 51

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\ShopBundle\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  6. use \ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  7. use Roothirsch\CoreBundle\Behaviors\Attributable\Attribute\AttributeInterface;
  8. use Roothirsch\CoreBundle\Behaviors\Attributable\AttributeValue\AttributeValueAwareInterface;
  9. use Roothirsch\CoreBundle\Behaviors\Attributable\AttributeValue\AttributeValueAwareTrait;
  10. use Roothirsch\CoreBundle\Behaviors\Attributable\AttributeValue\AttributeValueInterface;
  11. use Roothirsch\CoreBundle\Behaviors\Attributable\MappedSuperclass\AbstractAttributable;
  12. use Roothirsch\CoreBundle\Entity\Traits\TimetrackedTrait;
  13. use Roothirsch\CoreBundle\UserAware\UserAwareInterface;
  14. use Roothirsch\CoreBundle\UserAware\UserAwareTrait;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\Common\Collections\Collection;
  17. use Doctrine\ORM\Mapping as ORM;
  18. use Gedmo\Mapping\Annotation as Gedmo;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Roothirsch\ShopBundle\Entity\Estimate\EstimateAttribute;
  21. use Roothirsch\ShopBundle\Entity\Estimate\EstimateAttributeValue;
  22. use Roothirsch\ShopBundle\Entity\Order\OrderAttributeValue;
  23. use Symfony\Component\Serializer\Annotation\SerializedName;
  24. /**
  25. * @ORM\Entity(repositoryClass="Roothirsch\ShopBundle\Repository\OrderRepository")
  26. * @ORM\Table(name="shop_order")
  27. * @ApiResource(
  28. * normalizationContext={"groups"={"list"}, "enable_max_depth"=true},
  29. * denormalizationContext={"groups"={"list"}},
  30. * shortName="Shop/Order",
  31. * itemOperations={
  32. * "get",
  33. * "put",
  34. * "delete"
  35. * },
  36. * collectionOperations={
  37. * "duplicate"={"method"="POST", "path"="/orders/{id}/duplicate", "requirements"={"id"=".+"}},
  38. * "get",
  39. * "post"
  40. * }
  41. * )
  42. * @ApiFilter(OrderFilter::class, properties={"createdAt", "orderedAt"}, arguments={"orderParameterName"="order"})
  43. * @ApiFilter(SearchFilter::class, properties={
  44. * "state": "exact",
  45. * "type": "exact"
  46. * })
  47. */
  48. class Order extends AbstractAttributable implements UserAwareInterface
  49. {
  50. use UserAwareTrait;
  51. use TimetrackedTrait;
  52. /**
  53. * @ORM\Id
  54. * @ORM\GeneratedValue
  55. * @ORM\Column(type="integer")
  56. */
  57. private $id;
  58. /**
  59. * @ORM\Column(type="string", length=255)
  60. * @Groups({"list"})
  61. */
  62. private $state = 'cart';
  63. /**
  64. * @ORM\Column(type="string", length=255, nullable=true)
  65. * @Groups({"list"})
  66. */
  67. private $name;
  68. /**
  69. * @ORM\Column(type="string", length=255, options={"default" : "configurator"})
  70. * @Groups({"list"})
  71. */
  72. private $type = 'configurator';
  73. /**
  74. * @ORM\OneToMany(targetEntity=OrderPosition::class, mappedBy="order", orphanRemoval=true, cascade={"persist"})
  75. * @Groups({"list"})
  76. */
  77. private $positions;
  78. /**
  79. * @ORM\Column(type="boolean")
  80. * @Groups({"list"})
  81. */
  82. private $applyDiscount = true;
  83. /**
  84. * @ORM\Column(type="boolean")
  85. * @Groups({"list"})
  86. */
  87. private $applyMwst = false;
  88. /**
  89. * @ORM\Column(type="string", nullable=true)
  90. * @Groups({"list"})
  91. */
  92. private $comment;
  93. /**
  94. * @var \DateTime
  95. * @ORM\Column(type="datetime", nullable=true)
  96. * @Groups({"list"})
  97. */
  98. private $orderedAt;
  99. /**
  100. * @ORM\OneToMany(targetEntity=OrderAttributeValue::class, mappedBy="order", orphanRemoval=true, cascade={"persist"})
  101. */
  102. private $attributeValues;
  103. public function __construct()
  104. {
  105. $this->positions = new ArrayCollection();
  106. $this->attributeValues = new ArrayCollection();
  107. }
  108. public function __clone() {
  109. $this->id = null;
  110. $this->setCreatedAt(new \DateTime());
  111. $this->setUpdatedAt(new \DateTime());
  112. $this->orderedAt = null;
  113. $oldPositions = $this->positions;
  114. $this->positions = new ArrayCollection();
  115. foreach($oldPositions as $oldPosition) {
  116. $this->addPosition(clone $oldPosition);
  117. }
  118. $oldAttributeValues = $this->attributeValues;
  119. $this->attributeValues = new ArrayCollection();
  120. foreach($oldAttributeValues as $oldAttributeValue) {
  121. $this->addAttributeValue(clone $oldAttributeValue);
  122. }
  123. }
  124. /**
  125. * @return mixed
  126. */
  127. public function getId()
  128. {
  129. return $this->id;
  130. }
  131. /**
  132. * @param mixed $id
  133. */
  134. public function setId($id)
  135. {
  136. $this->id = $id;
  137. }
  138. public function getName(): ?string
  139. {
  140. return $this->name;
  141. }
  142. public function setName(?string $name): self
  143. {
  144. $this->name = $name;
  145. return $this;
  146. }
  147. public function getAttributes()
  148. {
  149. return $this->attributes;
  150. }
  151. /**
  152. * @return Collection|OrderPosition[]
  153. */
  154. public function getPositions(): Collection
  155. {
  156. return $this->positions;
  157. }
  158. public function addPosition(OrderPosition $position): self
  159. {
  160. if (!$this->positions->contains($position)) {
  161. $this->positions[] = $position;
  162. $position->setOrder($this);
  163. }
  164. return $this;
  165. }
  166. public function removePosition(OrderPosition $position): self
  167. {
  168. if ($this->positions->removeElement($position)) {
  169. // set the owning side to null (unless already changed)
  170. if ($position->getOrder() === $this) {
  171. $position->setOrder(null);
  172. }
  173. }
  174. return $this;
  175. }
  176. /**
  177. * @return bool
  178. */
  179. public function isApplyDiscount(): bool
  180. {
  181. return $this->applyDiscount;
  182. }
  183. /**
  184. * @param bool $applyDiscount
  185. */
  186. public function setApplyDiscount(bool $applyDiscount): void
  187. {
  188. $this->applyDiscount = $applyDiscount;
  189. }
  190. /**
  191. * @return bool
  192. */
  193. public function isApplyMwst(): bool
  194. {
  195. return $this->applyMwst;
  196. }
  197. /**
  198. * @param bool $applyMwst
  199. */
  200. public function setApplyMwst(bool $applyMwst): void
  201. {
  202. $this->applyMwst = $applyMwst;
  203. }
  204. /**
  205. * @return mixed
  206. */
  207. public function getComment()
  208. {
  209. return $this->comment;
  210. }
  211. /**
  212. * @param mixed $comment
  213. */
  214. public function setComment($comment): void
  215. {
  216. $this->comment = $comment;
  217. }
  218. /**
  219. * @return \DateTime
  220. */
  221. public function getOrderedAt(): ?\DateTime
  222. {
  223. return $this->orderedAt;
  224. }
  225. /**
  226. * @param \DateTime $orderedAt
  227. */
  228. public function setOrderedAt(\DateTime $orderedAt): void
  229. {
  230. $this->orderedAt = $orderedAt;
  231. }
  232. /**
  233. * @return mixed
  234. */
  235. public function getState()
  236. {
  237. return $this->state;
  238. }
  239. /**
  240. * @param mixed $state
  241. */
  242. public function setState($state): void
  243. {
  244. $this->state = $state;
  245. }
  246. /**
  247. * @Groups({"list"})
  248. * @SerializedName("total")
  249. * @return int|null
  250. */
  251. public function getTotal(): int
  252. {
  253. $total = 0;
  254. foreach ($this->positions as $position) {
  255. if ($position->getState() == 'draft') {
  256. continue;
  257. }
  258. $total += $position->getTotalWithTax();
  259. }
  260. return $total;
  261. }
  262. public function getArticleCount()
  263. {
  264. $total = 0;
  265. /** @var OrderPosition $position */
  266. foreach ($this->positions as $position) {
  267. if ($position->getState() == 'added') {
  268. foreach ($position->getArticles() as $article) {
  269. $total += $article['amount'];
  270. }
  271. }
  272. }
  273. return $total;
  274. }
  275. public function newValue(AttributeInterface $attribute): AttributeValueInterface
  276. {
  277. return new OrderAttributeValue($attribute);
  278. }
  279. /**
  280. * @Groups({"list"})
  281. * @SerializedName("tax")
  282. * @return string|null
  283. */
  284. public function getTax()
  285. {
  286. if(!$this->isApplyMwst()){
  287. return 1;
  288. }
  289. /** @var EstimateAttribute $tax */
  290. $taxAttribute = $this->getAttribute('tax');
  291. if(!$taxAttribute){
  292. return 1;
  293. }
  294. /** @var EstimateAttributeValue $value */
  295. $value = $this->getAttributeValue($taxAttribute);
  296. $tax = $value->getValue();
  297. if(!is_float($tax)){
  298. $tax = floatval($tax);
  299. }
  300. return (100 + $tax) / 100;
  301. }
  302. public function getTotalPriceBrutto()
  303. {
  304. return $total / 100 * (100 + $tax);
  305. }
  306. public function setAttributeValues(Collection $attributeValues): AttributeValueAwareInterface
  307. {
  308. $this->attributeValues = $attributeValues;
  309. return $this;
  310. }
  311. /**
  312. * @return Collection|EstimateAttributeValue []
  313. */
  314. public function getAttributeValues(): Collection
  315. {
  316. return $this->attributeValues;
  317. }
  318. public function getHighlightedDiscounts() {
  319. $discounts = [];
  320. /** @var EstimatePosition $position */
  321. foreach($this->positions as $position) {
  322. foreach($position->getDiscounts() as $discount) {
  323. if (isset($discount['highlight']) && $discount['highlight'] == true) {
  324. if (!isset($discounts[$discount['description']])) {
  325. $discounts[$discount['description']] = $discount;
  326. } else {
  327. $discounts[$discount['description']]['total'] += $discount['total'];
  328. }
  329. }
  330. }
  331. }
  332. return $discounts;
  333. }
  334. public function isAllPositionsValid(){
  335. return $this->getPositions()->filter(function($position){
  336. return $position->isPositionInvalid();
  337. })->count() === 0;
  338. }
  339. /**
  340. * @return mixed
  341. */
  342. public function getType()
  343. {
  344. return $this->type;
  345. }
  346. /**
  347. * @param mixed $type
  348. */
  349. public function setType($type): void
  350. {
  351. $this->type = $type;
  352. }
  353. }