1 <?php
2 /* --------------------------------------------------------------
3 OrderWriteService.php 2016-01-26
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2016 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('OrderWriteServiceInterface');
13
14 /**
15 * Class OrderWriteService
16 *
17 * @category System
18 * @package Order
19 */
20 class OrderWriteService implements OrderWriteServiceInterface
21 {
22 /**
23 * @var OrderRepository
24 */
25 protected $orderRepository;
26
27 /**
28 * @var OrderItemRepository
29 */
30 protected $orderItemRepository;
31
32 /**
33 * @var OrderItemAttributeRepositoryFactory
34 */
35 protected $orderItemAttributeRepositoryFactory;
36
37 /**
38 * @var OrderTotalRepository
39 */
40 protected $orderTotalRepository;
41
42 /**
43 * OrderStatusHistoryStorage
44 *
45 * @var OrderStatusHistoryStorage
46 */
47 protected $orderStatusHistoryStorage;
48
49 /**
50 * @var OrderServiceSettingsInterface
51 */
52 protected $orderServiceSettings;
53
54
55 /**
56 * Class Constructor
57 *
58 * @param OrderRepositoryInterface $orderRepository
59 * @param OrderItemRepositoryInterface $orderItemRepository
60 * @param OrderItemAttributeRepositoryFactoryInterface $orderItemAttributeRepositoryFactory
61 * @param OrderTotalRepository $orderTotalRepository
62 * @param OrderStatusHistoryStorage $orderStatusHistoryWriter
63 * @param OrderServiceSettingsInterface $orderServiceSetting
64 */
65 public function __construct(OrderRepositoryInterface $orderRepository,
66 OrderItemRepositoryInterface $orderItemRepository,
67 OrderItemAttributeRepositoryFactoryInterface $orderItemAttributeRepositoryFactory,
68 OrderTotalRepository $orderTotalRepository,
69 OrderStatusHistoryStorage $orderStatusHistoryWriter,
70 OrderServiceSettingsInterface $orderServiceSetting)
71 {
72 $this->orderRepository = $orderRepository;
73 $this->orderItemRepository = $orderItemRepository;
74 $this->orderItemAttributeRepositoryFactory = $orderItemAttributeRepositoryFactory;
75 $this->orderTotalRepository = $orderTotalRepository;
76 $this->orderStatusHistoryStorage = $orderStatusHistoryWriter;
77 $this->orderServiceSettings = $orderServiceSetting;
78 }
79
80
81 /**
82 * Create New Customer Order
83 *
84 * Creates a new customer order and returns
85 * the order ID as an integer.
86 *
87 * @param IdType $customerId Customer ID
88 * @param CustomerStatusInformation $customerStatusInfo Customer Status Information
89 * @param StringType $customerNumber Customer Number
90 * @param EmailStringType $customerEmail Customer Email
91 * @param StringType $customerTelephone Customer Telephone
92 * @param StringType $vatIdNumber VAT ID Number
93 * @param AddressBlockInterface $customerAddress Address of the customer
94 * @param AddressBlockInterface $billingAddress Billing address of the customer
95 * @param AddressBlockInterface $deliveryAddress Delivery address of the customer
96 * @param OrderItemCollection $orderItemCollection Collection of the order items
97 * @param OrderTotalCollection $orderTotalCollection Total collection of the order
98 * @param OrderShippingType $shippingType Shipping type of the order
99 * @param OrderPaymentType $paymentType Payment type of the order
100 * @param CurrencyCode $currencyCode Currency code of the order
101 * @param LanguageCode $languageCode Language code of the order
102 * @param StringType $comment Optional comment of the order (default = null)
103 * @param IdType $orderStatusId Optional id of the initial order status (default = null)
104 * @param KeyValueCollection $addonValues Optional key => value collection of addon values - e.g.
105 * cookies (default = null)
106 *
107 * @return int
108 */
109 public function createNewCustomerOrder(IdType $customerId,
110 CustomerStatusInformation $customerStatusInfo,
111 StringType $customerNumber,
112 EmailStringType $customerEmail,
113 StringType $customerTelephone,
114 StringType $vatIdNumber,
115 AddressBlockInterface $customerAddress,
116 AddressBlockInterface $billingAddress,
117 AddressBlockInterface $deliveryAddress,
118 OrderItemCollection $orderItemCollection,
119 OrderTotalCollection $orderTotalCollection,
120 OrderShippingType $shippingType,
121 OrderPaymentType $paymentType,
122 CurrencyCode $currencyCode,
123 LanguageCode $languageCode,
124 StringType $comment = null,
125 IdType $orderStatusId = null,
126 KeyValueCollection $addonValues = null)
127 {
128 $order = $this->orderRepository->createNew();
129
130 $order->setStatusId(new IdType($this->orderServiceSettings->getDefaultOrderStatusId()));
131
132 $order->setCustomerId($customerId);
133 $order->setCustomerStatusInformation($customerStatusInfo);
134 $order->setCustomerNumber($customerNumber);
135 $order->setCustomerEmail($customerEmail);
136 $order->setCustomerTelephone($customerTelephone);
137 $order->setVatIdNumber($vatIdNumber);
138 $order->setCustomerAddress($customerAddress);
139 $order->setBillingAddress($billingAddress);
140 $order->setDeliveryAddress($deliveryAddress);
141
142 foreach($orderItemCollection->getArray() as $orderItem)
143 {
144 $this->orderItemRepository->addToOrder(new IdType($order->getOrderId()), $orderItem);
145 }
146
147 foreach($orderTotalCollection->getArray() as $orderTotal)
148 {
149 $this->orderTotalRepository->addToOrder(new IdType($order->getOrderId()), $orderTotal);
150 }
151
152 $order->setShippingType($shippingType);
153 $order->setPaymentType($paymentType);
154 $order->setCurrencyCode($currencyCode);
155 $order->setLanguageCode($languageCode);
156
157 if($comment !== null)
158 {
159 $order->setComment($comment);
160 }
161
162 if($addonValues !== null)
163 {
164 $order->addAddonValues($addonValues);
165 }
166
167 $this->orderRepository->store($order);
168
169 $newOrderStatusId = ($orderStatusId
170 !== null) ? $orderStatusId : new IdType($this->orderServiceSettings->getDefaultOrderStatusId());
171 $statusComment = ($comment !== null) ? $comment : new StringType('');
172 $this->orderStatusHistoryStorage->addStatusUpdate(new IdType($order->getOrderId()), $newOrderStatusId,
173 $statusComment, new BoolType(true));
174
175 return $order->getOrderId();
176 }
177
178
179 /**
180 * Create New Standalone Order
181 *
182 * Creates a new standalone order (e.g. for guests) and returns
183 * the order ID as an integer.
184 *
185 * @param StringType $customerNumber Customer Number
186 * @param EmailStringType $customerEmail Customer Email
187 * @param StringType $customerTelephone Customer Telephone
188 * @param StringTYpe $vatIdNumber VAT ID number of the customer.
189 * @param AddressBlockInterface $customerAddress Address of the customer
190 * @param AddressBlockInterface $billingAddress Billing address of the customer
191 * @param AddressBlockInterface $deliveryAddress Delivery address of the customer
192 * @param OrderItemCollection $orderItemCollection Collection of the order items
193 * @param OrderTotalCollection $orderTotalCollection Total collection of the order
194 * @param OrderShippingType $shippingType Shipping type of the order
195 * @param OrderPaymentType $paymentType Payment type of the order
196 * @param CurrencyCode $currencyCode Currency code of the order
197 * @param LanguageCode $languageCode Language code of the order
198 * @param StringType $comment Optional comment of the order (default = null)
199 * @param IdType $orderStatusId Optional id of the initial order status (default = null)
200 * @param KeyValueCollection $addonValues Optional key => value collection of addon values - e.g.
201 * cookies (default = null)
202 *
203 * @return int
204 */
205 public function createNewStandaloneOrder(StringType $customerNumber,
206 EmailStringType $customerEmail,
207 StringType $customerTelephone,
208 StringType $vatIdNumber,
209 AddressBlockInterface $customerAddress,
210 AddressBlockInterface $billingAddress,
211 AddressBlockInterface $deliveryAddress,
212 OrderItemCollection $orderItemCollection,
213 OrderTotalCollection $orderTotalCollection,
214 OrderShippingType $shippingType,
215 OrderPaymentType $paymentType,
216 CurrencyCode $currencyCode,
217 LanguageCode $languageCode,
218 StringType $comment = null,
219 IdType $orderStatusId = null,
220 KeyValueCollection $addonValues = null)
221 {
222 $order = $this->orderRepository->createNew();
223
224 $order->setStatusId(new IdType($this->orderServiceSettings->getDefaultOrderStatusId()));
225
226 $customerStatusInformation = MainFactory::create('CustomerStatusInformation',
227 new IdType($this->orderServiceSettings->getDefaultGuestStatusId()),
228 new StringType(''), new StringType(''), new DecimalType(0.0),
229 new BoolType(true));
230 $order->setCustomerStatusInformation($customerStatusInformation);
231
232 $order->setCustomerNumber($customerNumber);
233 $order->setCustomerEmail($customerEmail);
234 $order->setCustomerTelephone($customerTelephone);
235 $order->setVatIdNumber($vatIdNumber);
236 $order->setCustomerAddress($customerAddress);
237 $order->setBillingAddress($billingAddress);
238 $order->setDeliveryAddress($deliveryAddress);
239
240 foreach($orderItemCollection->getArray() as $orderItem)
241 {
242 $this->orderItemRepository->addToOrder(new IdType($order->getOrderId()), $orderItem);
243 }
244
245 foreach($orderTotalCollection->getArray() as $orderTotal)
246 {
247 $this->orderTotalRepository->addToOrder(new IdType($order->getOrderId()), $orderTotal);
248 }
249
250 $order->setShippingType($shippingType);
251 $order->setPaymentType($paymentType);
252 $order->setCurrencyCode($currencyCode);
253 $order->setLanguageCode($languageCode);
254
255 if($comment !== null)
256 {
257 $order->setComment($comment);
258 }
259
260 if($addonValues !== null)
261 {
262 $order->addAddonValues($addonValues);
263 }
264
265 $this->orderRepository->store($order);
266
267 $newOrderStatusId = ($orderStatusId
268 !== null) ? $orderStatusId : new IdType($this->orderServiceSettings->getDefaultOrderStatusId());
269 $statusComment = ($comment !== null) ? $comment : new StringType('');
270 $this->orderStatusHistoryStorage->addStatusUpdate(new IdType($order->getOrderId()), $newOrderStatusId,
271 $statusComment, new BoolType(true));
272
273 return $order->getOrderId();
274 }
275
276
277 /**
278 * Update Customer Address
279 *
280 * Updates the customers address.
281 *
282 * @param IdType $orderId Order ID which holds the current address
283 * @param AddressBlockInterface $newAddress New address of the customer
284 */
285 public function updateCustomerAddress(IdType $orderId, AddressBlockInterface $newAddress)
286 {
287 $order = $this->orderRepository->getById($orderId);
288 $order->setCustomerAddress($newAddress);
289 $this->orderRepository->store($order);
290 }
291
292
293 /**
294 * Update Billing Address
295 *
296 * Updates the customers billing address.
297 *
298 * @param IdType $orderId Order ID which holds the current address
299 * @param AddressBlockInterface $newAddress New billing address
300 */
301 public function updateBillingAddress(IdType $orderId, AddressBlockInterface $newAddress)
302 {
303 $order = $this->orderRepository->getById($orderId);
304 $order->setBillingAddress($newAddress);
305 $this->orderRepository->store($order);
306 }
307
308
309 /**
310 * Update Delivery Address
311 *
312 * Updates the customers delivery address.
313 *
314 * @param IdType $orderId Order ID which holds the current address
315 * @param AddressBlockInterface $newAddress New delivery address
316 */
317 public function updateDeliveryAddress(IdType $orderId, AddressBlockInterface $newAddress)
318 {
319 $order = $this->orderRepository->getById($orderId);
320 $order->setDeliveryAddress($newAddress);
321 $this->orderRepository->store($order);
322 }
323
324
325 /**
326 * Add Order Item
327 *
328 * Adds an item to the order.
329 *
330 * @param IdType $orderId Order ID of the order to add the item
331 * @param OrderItemInterface $orderItem The order item to add
332 *
333 * @return int Id of the StoredOrderItem
334 */
335 public function addOrderItem(IdType $orderId, OrderItemInterface $orderItem)
336 {
337 return $this->orderItemRepository->addToOrder($orderId, $orderItem);
338 }
339
340
341 /**
342 * Update Order Item
343 *
344 * Updates an order item.
345 *
346 * @param StoredOrderItemInterface $orderItem The order item to update
347 */
348 public function updateOrderItem(StoredOrderItemInterface $orderItem)
349 {
350 $this->orderItemRepository->store($orderItem);
351 }
352
353
354 /**
355 * Remove Order Item
356 *
357 * Removes an item from an order.
358 *
359 * @param StoredOrderItemInterface $orderItem The order item to remove
360 */
361 public function removeOrderItem(StoredOrderItemInterface $orderItem)
362 {
363 $this->orderItemRepository->deleteItemById(new IdType($orderItem->getOrderItemId()));
364 }
365
366
367 /**
368 * Add Order Item Attribute
369 *
370 * Adds an order item attribute to the order
371 *
372 * @param IdType $orderItemId Order ID of the order item to add the attribute.
373 * @param OrderItemAttributeInterface $orderItemAttribute The order item attribute to add
374 *
375 * @return int Id of stored order item attribute.
376 */
377 public function addOrderItemAttribute(IdType $orderItemId, OrderItemAttributeInterface $orderItemAttribute)
378 {
379 $orderItemAttributeRepository = $this->orderItemAttributeRepositoryFactory->createRepositoryByAttributeObject($orderItemAttribute);
380 return $orderItemAttributeRepository->addToOrderItem($orderItemId, $orderItemAttribute);
381 }
382
383
384 /**
385 * Update Order Item Attribute
386 *
387 * Updates an item attribute of an order.
388 *
389 * @param StoredOrderItemAttributeInterface $orderItemAttribute The order item attribute to update
390 */
391 public function updateOrderItemAttribute(StoredOrderItemAttributeInterface $orderItemAttribute)
392 {
393 $orderItemAttributeRepository = $this->orderItemAttributeRepositoryFactory->createRepositoryByAttributeObject($orderItemAttribute);
394 $orderItemAttributeRepository->store($orderItemAttribute);
395 }
396
397
398 /**
399 * Remove Order Item Attribute
400 *
401 * Removes an item attribute of an order
402 *
403 * @param StoredOrderItemAttributeInterface $orderItemAttribute The order item attribute to remove
404 */
405 public function removeOrderItemAttribute(StoredOrderItemAttributeInterface $orderItemAttribute)
406 {
407 $orderItemAttributeRepository = $this->orderItemAttributeRepositoryFactory->createRepositoryByAttributeObject($orderItemAttribute);
408 $orderItemAttributeRepository->deleteItemAttributeById(new IdType($orderItemAttribute->getOrderItemAttributeId()));
409 }
410
411
412 /**
413 * Add Order Total
414 *
415 * Adds a total price to an order.
416 *
417 * @param IdType $orderId Order Id of the order to add the total price
418 * @param OrderTotalInterface $orderTotal Total price to add to the order
419 *
420 * @return int Id of stored order total.
421 */
422 public function addOrderTotal(IdType $orderId, OrderTotalInterface $orderTotal)
423 {
424 return $this->orderTotalRepository->addToOrder($orderId, $orderTotal);
425 }
426
427
428 /**
429 * Update Order Total
430 *
431 * Updates a total price of an order
432 *
433 * @param StoredOrderTotalInterface $orderTotal The total price of an order to update
434 */
435 public function updateOrderTotal(StoredOrderTotalInterface $orderTotal)
436 {
437 $this->orderTotalRepository->store($orderTotal);
438 }
439
440
441 /**
442 * Remove Order Total
443 *
444 * Removes a total price of an order
445 *
446 * @param StoredOrderTotalInterface $orderTotal The total price of an order to remove
447 */
448 public function removeOrderTotal(StoredOrderTotalInterface $orderTotal)
449 {
450 $this->orderTotalRepository->deleteTotalById(new IdType($orderTotal->getOrderTotalId()));
451 }
452
453
454 /**
455 * Update Shipping Type
456 *
457 * Updates the shipping type of an order.
458 *
459 * @param IdType $orderId Order ID of the order to update
460 * @param OrderShippingType $newShippingType The new shipping type
461 */
462 public function updateShippingType(IdType $orderId, OrderShippingType $newShippingType)
463 {
464 $order = $this->orderRepository->getById($orderId);
465 $order->setShippingType($newShippingType);
466 $this->orderRepository->store($order);
467 }
468
469
470 /**
471 * Update Payment Type
472 *
473 * Updates the payment type of an order.
474 *
475 * @param IdType $orderId Order ID of the order to update
476 * @param OrderPaymentType $newPaymentType The new payment type
477 */
478 public function updatePaymentType(IdType $orderId, OrderPaymentType $newPaymentType)
479 {
480 $order = $this->orderRepository->getById($orderId);
481 $order->setPaymentType($newPaymentType);
482 $this->orderRepository->store($order);
483 }
484
485
486 /**
487 * Update Comment
488 *
489 * Updates the comment of an order.
490 *
491 * @param IdType $orderId Order ID of the order to update
492 * @param StringType $newComment The new comment
493 */
494 public function updateComment(IdType $orderId, StringType $newComment)
495 {
496 $order = $this->orderRepository->getById($orderId);
497 $order->setComment($newComment);
498 $this->orderRepository->store($order);
499 }
500
501
502 /**
503 * Update Order Status
504 *
505 * Updates the order status of an order.
506 *
507 * @param IdType $orderId Order ID of the order to update
508 * @param IdType $newOrderStatusId The new status Id
509 * @param StringType $comment Comment of the order status history item
510 * @param BoolType $customerNotified Customer notified flag
511 */
512 public function updateOrderStatus(IdType $orderId,
513 IdType $newOrderStatusId,
514 StringType $comment,
515 BoolType $customerNotified)
516 {
517 $order = $this->orderRepository->getById($orderId);
518 $order->setStatusId($newOrderStatusId);
519 $this->orderRepository->store($order);
520
521 $this->orderStatusHistoryStorage->addStatusUpdate($orderId, $newOrderStatusId, $comment, $customerNotified);
522 }
523
524
525 /**
526 * Update Order
527 *
528 * Updates the provided order.
529 *
530 * @param \OrderInterface $order Order to update
531 *
532 * @return \OrderWriteService Same instance for chained method calls.
533 */
534 public function updateOrder(OrderInterface $order)
535 {
536 $this->orderRepository->store($order);
537
538 return $this;
539 }
540
541
542 /**
543 * Remove Order by ID
544 *
545 * Removes a specific order, depending on the provided order ID.
546 *
547 * @param IdType $orderId Order ID of the order to remove
548 */
549 public function removeOrderById(IdType $orderId)
550 {
551 $this->orderRepository->deleteById($orderId);
552 $this->orderStatusHistoryStorage->deleteHistory($orderId);
553 }
554 }
555