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