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