TaxFactory.php
175 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @copyright © TMS-Plugins. All rights reserved. |
| 4 | * @licence See LICENCE.md for license details. |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Domain\Factory\Tax; |
| 8 | |
| 9 | use AmeliaBooking\Domain\Collection\Collection; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Entities; |
| 12 | use AmeliaBooking\Domain\Entity\Tax\Tax; |
| 13 | use AmeliaBooking\Domain\Factory\Bookable\Service\ExtraFactory; |
| 14 | use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory; |
| 15 | use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory; |
| 16 | use AmeliaBooking\Domain\Factory\Bookable\Service\PackageFactory; |
| 17 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 18 | use AmeliaBooking\Domain\ValueObjects\Number\Float\FloatValue; |
| 19 | use AmeliaBooking\Domain\ValueObjects\String\AmountType; |
| 20 | use AmeliaBooking\Domain\ValueObjects\String\Name; |
| 21 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 22 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 23 | |
| 24 | /** |
| 25 | * Class TaxFactory |
| 26 | * |
| 27 | * @package AmeliaBooking\Domain\Factory\Tax |
| 28 | */ |
| 29 | class TaxFactory |
| 30 | { |
| 31 | /** |
| 32 | * @param $data |
| 33 | * |
| 34 | * @return Tax |
| 35 | * @throws InvalidArgumentException |
| 36 | */ |
| 37 | public static function create($data) |
| 38 | { |
| 39 | $tax = new Tax(); |
| 40 | |
| 41 | if (isset($data['id'])) { |
| 42 | $tax->setId(new Id($data['id'])); |
| 43 | } |
| 44 | |
| 45 | if (isset($data['name'])) { |
| 46 | $tax->setName(new Name($data['name'])); |
| 47 | } |
| 48 | |
| 49 | if (isset($data['amount'])) { |
| 50 | $tax->setAmount(new FloatValue($data['amount'])); |
| 51 | } |
| 52 | |
| 53 | if (isset($data['status'])) { |
| 54 | $tax->setStatus(new Status($data['status'])); |
| 55 | } |
| 56 | |
| 57 | if (isset($data['type'])) { |
| 58 | $tax->setType(new AmountType($data['type'])); |
| 59 | } |
| 60 | |
| 61 | if (isset($data['excluded'])) { |
| 62 | $tax->setExcluded(new BooleanValueObject($data['excluded'])); |
| 63 | } |
| 64 | |
| 65 | $serviceList = new Collection(); |
| 66 | |
| 67 | if (isset($data['serviceList'])) { |
| 68 | foreach ($data['serviceList'] as $key => $value) { |
| 69 | $serviceList->addItem( |
| 70 | ServiceFactory::create($value), |
| 71 | $key |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | $eventList = new Collection(); |
| 77 | |
| 78 | if (isset($data['eventList'])) { |
| 79 | foreach ($data['eventList'] as $key => $value) { |
| 80 | $eventList->addItem( |
| 81 | EventFactory::create($value), |
| 82 | $key |
| 83 | ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $packageList = new Collection(); |
| 88 | |
| 89 | if (isset($data['packageList'])) { |
| 90 | foreach ($data['packageList'] as $key => $value) { |
| 91 | $packageList->addItem( |
| 92 | PackageFactory::create($value), |
| 93 | $key |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $extraList = new Collection(); |
| 99 | |
| 100 | if (isset($data['extraList'])) { |
| 101 | foreach ($data['extraList'] as $key => $value) { |
| 102 | $extraList->addItem( |
| 103 | ExtraFactory::create($value), |
| 104 | $key |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $tax->setServiceList($serviceList); |
| 110 | $tax->setEventList($eventList); |
| 111 | $tax->setPackageList($packageList); |
| 112 | $tax->setExtraList($extraList); |
| 113 | |
| 114 | return $tax; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param array $rows |
| 119 | * |
| 120 | * @return Collection |
| 121 | * @throws InvalidArgumentException |
| 122 | */ |
| 123 | public static function createCollection($rows) |
| 124 | { |
| 125 | $taxes = []; |
| 126 | |
| 127 | foreach ($rows as $row) { |
| 128 | $taxId = $row['tax_id']; |
| 129 | |
| 130 | if (empty($taxes[$taxId])) { |
| 131 | $taxes[$taxId] = [ |
| 132 | 'id' => $taxId, |
| 133 | 'name' => $row['tax_name'], |
| 134 | 'amount' => $row['tax_amount'], |
| 135 | 'type' => $row['tax_type'], |
| 136 | 'status' => $row['tax_status'], |
| 137 | 'serviceList' => [], |
| 138 | 'eventList' => [], |
| 139 | 'packageList' => [], |
| 140 | 'extraList' => [], |
| 141 | ]; |
| 142 | } |
| 143 | |
| 144 | if (isset($row['tax_entityId'], $row['tax_entityType']) && $row['tax_entityId']) { |
| 145 | if ($row['tax_entityType'] === Entities::SERVICE) { |
| 146 | $taxes[$taxId]['serviceList'][$row['tax_entityId']]['id'] = $row['tax_entityId']; |
| 147 | } |
| 148 | |
| 149 | if ($row['tax_entityType'] === Entities::EVENT) { |
| 150 | $taxes[$taxId]['eventList'][$row['tax_entityId']]['id'] = $row['tax_entityId']; |
| 151 | } |
| 152 | |
| 153 | if ($row['tax_entityType'] === Entities::PACKAGE) { |
| 154 | $taxes[$taxId]['packageList'][$row['tax_entityId']]['id'] = $row['tax_entityId']; |
| 155 | } |
| 156 | |
| 157 | if ($row['tax_entityType'] === Entities::EXTRA) { |
| 158 | $taxes[$taxId]['extraList'][$row['tax_entityId']]['id'] = $row['tax_entityId']; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $taxesCollection = new Collection(); |
| 164 | |
| 165 | foreach ($taxes as $taxKey => $taxArray) { |
| 166 | $taxesCollection->addItem( |
| 167 | self::create($taxArray), |
| 168 | $taxKey |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | return $taxesCollection; |
| 173 | } |
| 174 | } |
| 175 |