vendor/roothirsch/shop-bundle/Entity/Estimate.php line 38

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