|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Order.php |
| 4 | + * @author Martin Appelmann <hello@martin-appelmann.de> |
| 5 | + * @copyright 2024 Martin Appelmann |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Exlo89\LaravelSevdeskApi\Api; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Exlo89\LaravelSevdeskApi\Api\Utils\ApiClient; |
| 12 | +use Exlo89\LaravelSevdeskApi\Api\Utils\DocumentHelper; |
| 13 | +use Exlo89\LaravelSevdeskApi\Api\Utils\Routes; |
| 14 | +use Exlo89\LaravelSevdeskApi\Constants\OrderStatus; |
| 15 | +use Exlo89\LaravelSevdeskApi\Constants\OrderType; |
| 16 | +use Exlo89\LaravelSevdeskApi\Models\SevOrder; |
| 17 | +use Illuminate\Support\Collection; |
| 18 | + |
| 19 | +/** |
| 20 | + * Sevdesk Order Api |
| 21 | + * |
| 22 | + * @see https://api.sevdesk.de/#tag/Order |
| 23 | + */ |
| 24 | +class Order extends ApiClient |
| 25 | +{ |
| 26 | + // =========================== all ==================================== |
| 27 | + |
| 28 | + /** |
| 29 | + * Return all orders. |
| 30 | + * |
| 31 | + * @param string|null $orderType |
| 32 | + * @param string|null $orderStatus |
| 33 | + * @return Collection |
| 34 | + */ |
| 35 | + public function all(?string $orderType = null, ?string $orderStatus = null): Collection |
| 36 | + { |
| 37 | + return Collection::make($this->_get(Routes::ORDER, $this->getParam($orderType, $orderStatus))); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Return all draft orders. |
| 42 | + * |
| 43 | + * @param string|null $orderType |
| 44 | + * @return Collection |
| 45 | + */ |
| 46 | + public function allDraft(?string $orderType = null): Collection |
| 47 | + { |
| 48 | + return Collection::make($this->_get(Routes::ORDER, $this->getParam($orderType, OrderStatus::DRAFT))); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Return all delivered orders. |
| 53 | + * |
| 54 | + * @param string|null $orderType |
| 55 | + * @return Collection |
| 56 | + */ |
| 57 | + public function allDelivered(?string $orderType = null): Collection |
| 58 | + { |
| 59 | + return Collection::make($this->_get(Routes::ORDER, $this->getParam($orderType, OrderStatus::DELIVERED))); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Return all cancelled orders. |
| 64 | + * |
| 65 | + * @param string|null $orderType |
| 66 | + * @return Collection |
| 67 | + */ |
| 68 | + public function allCancelled(?string $orderType = null): Collection |
| 69 | + { |
| 70 | + return Collection::make($this->_get(Routes::ORDER, $this->getParam($orderType, OrderStatus::CANCELLED))); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Return all accepted orders. |
| 75 | + * |
| 76 | + * @param string|null $orderType |
| 77 | + * @return Collection |
| 78 | + */ |
| 79 | + public function allAccepted(?string $orderType = null): Collection |
| 80 | + { |
| 81 | + return Collection::make($this->_get(Routes::ORDER, $this->getParam($orderType, OrderStatus::ACCEPTED))); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Return all partial accepted orders. |
| 86 | + * |
| 87 | + * @param string|null $orderType |
| 88 | + * @return Collection |
| 89 | + */ |
| 90 | + public function allPartialAccepted(?string $orderType = null): Collection |
| 91 | + { |
| 92 | + return Collection::make($this->_get(Routes::ORDER, $this->getParam($orderType, OrderStatus::PARTIAL_CALCULATED))); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Return all calculated orders. |
| 97 | + * |
| 98 | + * @param string|null $orderType |
| 99 | + * @return Collection |
| 100 | + */ |
| 101 | + public function allCalculated(?string $orderType = null): Collection |
| 102 | + { |
| 103 | + return Collection::make($this->_get(Routes::ORDER, $this->getParam($orderType, OrderStatus::CALCULATED))); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Return all orders filtered by contact id. |
| 108 | + * |
| 109 | + * @param $contactId |
| 110 | + * @return Collection |
| 111 | + */ |
| 112 | + public function allByContact($contactId): Collection |
| 113 | + { |
| 114 | + return Collection::make($this->_get(Routes::ORDER, [ |
| 115 | + 'contact' => [ |
| 116 | + 'id' => $contactId, |
| 117 | + 'objectName' => 'Contact' |
| 118 | + ], |
| 119 | + ])); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Return all orders filtered by a date equal or lower. |
| 124 | + * |
| 125 | + * @param int $timestamp |
| 126 | + * @return Collection |
| 127 | + */ |
| 128 | + public function allBefore(int $timestamp): Collection |
| 129 | + { |
| 130 | + return Collection::make($this->_get(Routes::ORDER, ['endDate' => $timestamp])); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Return all orders filtered by a date equal or higher. |
| 135 | + * |
| 136 | + * @param int $timestamp |
| 137 | + * @return Collection |
| 138 | + */ |
| 139 | + public function allAfter(int $timestamp): Collection |
| 140 | + { |
| 141 | + return Collection::make($this->_get(Routes::ORDER, ['startDate' => $timestamp])); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Return all orders filtered by a date range. |
| 146 | + * |
| 147 | + * @param int $startTimestamp |
| 148 | + * @param int $endTimestamp |
| 149 | + * @return Collection |
| 150 | + */ |
| 151 | + public function allBetween(int $startTimestamp, int $endTimestamp): Collection |
| 152 | + { |
| 153 | + return Collection::make($this->_get(Routes::ORDER, [ |
| 154 | + 'startDate' => $startTimestamp, |
| 155 | + 'endDate' => $endTimestamp, |
| 156 | + ])); |
| 157 | + } |
| 158 | + |
| 159 | + // =========================== create ==================================== |
| 160 | + |
| 161 | + /** |
| 162 | + * Create order. |
| 163 | + * |
| 164 | + * @param $contactId |
| 165 | + * @param array $items |
| 166 | + * @param array $parameters |
| 167 | + * @return SevOrder |
| 168 | + * @throws Exception |
| 169 | + */ |
| 170 | + public function create($contactId, $items, array $parameters = []): SevOrder |
| 171 | + { |
| 172 | + // create parameter array |
| 173 | + $parameters['orderType'] = OrderType::PROPOSAL; |
| 174 | + $orderParameters = DocumentHelper::getOrderParameters($contactId, $items, $parameters); |
| 175 | + $response = $this->_post(Routes::CREATE_ORDER, $orderParameters); |
| 176 | + |
| 177 | + // create model with relationships |
| 178 | + /** @var SevOrder $sevOrder */ |
| 179 | + $sevOrder = SevOrder::make($response['order']); |
| 180 | + $sevOrder->setRelation('orderPositions', collect($response['orderPos'])); |
| 181 | + return $sevOrder; |
| 182 | + } |
| 183 | + // ========================== update ================================== |
| 184 | + |
| 185 | + /** |
| 186 | + * Update an existing order. |
| 187 | + * |
| 188 | + * @param $orderId |
| 189 | + * @param array $parameters |
| 190 | + * @return SevOrder |
| 191 | + */ |
| 192 | + public function update($orderId, array $parameters = []): SevOrder |
| 193 | + { |
| 194 | + return SevOrder::make($this->_put(Routes::ORDER . '/' . $orderId, $parameters)); |
| 195 | + } |
| 196 | + |
| 197 | + // ========================== delete ================================== |
| 198 | + |
| 199 | + /** |
| 200 | + * Delete an existing order. |
| 201 | + * |
| 202 | + * @param $orderId |
| 203 | + * @return void |
| 204 | + */ |
| 205 | + public function delete($orderId): void |
| 206 | + { |
| 207 | + $this->_delete(Routes::ORDER . '/' . $orderId); |
| 208 | + } |
| 209 | + |
| 210 | + // ======================================================================= |
| 211 | + |
| 212 | + /** |
| 213 | + * Returns pdf file of the giving order id. |
| 214 | + * |
| 215 | + * @return void |
| 216 | + */ |
| 217 | + public function download($orderId) |
| 218 | + { |
| 219 | + $this->getPdf(Routes::ORDER . '/' . $orderId . '/getPdf'); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Send order per email. |
| 224 | + * |
| 225 | + * @return void |
| 226 | + */ |
| 227 | + public function sendByMail($orderId, $email, $subject, $text) |
| 228 | + { |
| 229 | + return $this->_post(Routes::ORDER . '/' . $orderId . '/sendViaEmail', [ |
| 230 | + 'toEmail' => $email, |
| 231 | + 'subject' => $subject, |
| 232 | + 'text' => $text, |
| 233 | + ]); |
| 234 | + } |
| 235 | + |
| 236 | + // ================== private functions ================== |
| 237 | + |
| 238 | + private function getParam($type, $status) |
| 239 | + { |
| 240 | + $params = []; |
| 241 | + if ($type !== null) { |
| 242 | + $params['orderType'] = $type; |
| 243 | + } |
| 244 | + if ($status !== null) { |
| 245 | + $params['status'] = $status; |
| 246 | + } |
| 247 | + return $params; |
| 248 | + } |
| 249 | +} |
0 commit comments