Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OrderBundle, CoreBundle] Serialize OrderInvoice, OrderShipment, OrderInvoiceItem and OrderShipmentItem with JMS Serializer #1129

Merged
merged 9 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ CoreShop\Component\Core\Model\Order:
carrier:
serialized_name: carrier
exp: object.getCarrier()
groups: [Detailed]
groups: [Detailed]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CoreShop\Component\Core\Model\OrderShipment:
exclusion_policy: ALL
xml_root_name: order_shipment
virtual_properties:
carrierName:
serialized_name: carrierName
exp: 'object.getCarrier() != null ? object.getCarrier().getIdentifier() : null'
type: string
groups: [Detailed]
weight:
serialized_name: weight
exp: object.getWeight()
type: double
groups: [Detailed]

Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function findSaleAction(Request $request)
*/
protected function prepareSale(SaleInterface $sale)
{
$date = (int) $sale->getSaleDate()->getTimestamp();
$date = (int)$sale->getSaleDate()->getTimestamp();

$element = [
'o_id' => $sale->getId(),
Expand Down Expand Up @@ -211,23 +211,22 @@ protected function prepareAddress($address, $type)
*/
protected function getDetails(SaleInterface $sale)
{
if ($this->getParameter('coreshop.order.legacy_serialization')) {
if ($this->useLegacySerialization()) {
$jsonSale = $this->getDataForObject($sale);
$jsonSale['o_id'] = $sale->getId();
$jsonSale['saleNumber'] = $sale->getSaleNumber();
$jsonSale['saleDate'] = $sale->getSaleDate()->getTimestamp();
$jsonSale['customer'] = $sale->getCustomer() instanceof CustomerInterface ? $this->getDataForObject($sale->getCustomer()) : null;
$jsonSale['currency'] = $this->getCurrency($sale->getCurrency() ?: $sale->getStore()->getCurrency());
$jsonSale['store'] = $sale->getStore() instanceof StoreInterface ? $this->getStore($sale->getStore()) : null;
}
else {
$jsonSale = $this->get('jms_serializer')->toArray($sale);
} else {
$jsonSale = $this->getSerializer()->toArray($sale);
}

if ($jsonSale['items'] === null) {
$jsonSale['items'] = [];
}

$jsonSale['details'] = $this->getItemDetails($sale);
$jsonSale['summary'] = $this->getSummary($sale);
$jsonSale['mailCorrespondence'] = $this->getMailCorrespondence($sale);
Expand Down Expand Up @@ -461,8 +460,24 @@ abstract protected function getSaleNumberField();
/**
* @return AddressFormatterInterface
*/
private function getAddressFormatter()
protected function getAddressFormatter()
{
return $this->get('coreshop.address.formatter');
}

/**
* @return \JMS\Serializer\Serializer
*/
protected function getSerializer()
{
return $this->get('jms_serializer');
}

/**
* @return bool
*/
protected function useLegacySerialization()
{
return $this->getParameter('coreshop.order.legacy_serialization') === true;
}
}
54 changes: 30 additions & 24 deletions src/CoreShop/Bundle/OrderBundle/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,12 @@ protected function getDetails(SaleInterface $sale)
$order['availableOrderTransitions'] = $availableTransitions;
$order['statesHistory'] = $this->getStatesHistory($sale);


$invoices = $this->getInvoices($sale);

$order['editable'] = count($invoices) > 0 ? false : true;
$order['invoices'] = $invoices;
$order['payments'] = $this->getPayments($sale);
$order['editable'] = count($this->getInvoices($sale)) > 0 ? false : true;
$order['invoices'] = $this->getInvoices($sale);
$order['shipments'] = $this->getShipments($sale);
$order['paymentCreationAllowed'] = !in_array($sale->getOrderState(), [OrderStates::STATE_CANCELLED, OrderStates::STATE_COMPLETE]);
$order['invoiceCreationAllowed'] = $this->getInvoiceProcessableHelper()->isProcessable($sale);
Expand Down Expand Up @@ -300,16 +303,19 @@ protected function getInvoices($order)
'cancel',
], false);

$data = $this->getDataForObject($invoice);
if ($this->useLegacySerialization()) {
$data = $this->getDataForObject($invoice);

foreach ($invoice->getItems() as $index => $item) {
$data['items'][$index]['_itemName'] = $item->getOrderItem()->getName();
}
} else {
$data = $this->getSerializer()->toArray($invoice);
}

$data['stateInfo'] = $this->getWorkflowStateManager()->getStateInfo('coreshop_invoice', $invoice->getState(), false);
$data['transitions'] = $availableTransitions;

// better solution?
foreach ($invoice->getItems() as $index => $item) {
$data['items'][$index]['_itemName'] = $item->getOrderItem()->getName();
}

$invoiceArray[] = $data;
}

Expand All @@ -327,27 +333,26 @@ protected function getShipments($order)
$shipmentArray = [];

foreach ($shipments as $shipment) {
$data = $this->getDataForObject($shipment);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add BC param check here pls


//This should be in CoreBundle, but for BC reasons, we keep it here
if (method_exists($shipment, 'getCarrier')) {
$data['carrierName'] = $shipment->getCarrier()->getTitle();
}

$availableTransitions = $this->getWorkflowStateManager()->parseTransitions($shipment, 'coreshop_shipment', [
'create',
'ship',
'cancel',
], false);

$data['stateInfo'] = $this->getWorkflowStateManager()->getStateInfo('coreshop_shipment', $shipment->getState(), false);
$data['transitions'] = $availableTransitions;
if ($this->useLegacySerialization()) {
$data = $this->getDataForObject($shipment);

// better solution?
foreach ($shipment->getItems() as $index => $item) {
$data['items'][$index]['_itemName'] = $item->getOrderItem()->getName();
foreach ($shipment->getItems() as $index => $item) {
$data['items'][$index]['_itemName'] = $item->getOrderItem()->getName();
}
} else {
$data = $this->getSerializer()->toArray($shipment);
}

$data['stateInfo'] = $this->getWorkflowStateManager()->getStateInfo('coreshop_shipment', $shipment->getState(), false);
$data['transitions'] = $availableTransitions;

$shipmentArray[] = $data;
}

Expand All @@ -369,39 +374,39 @@ protected function getSummary(SaleInterface $sale)
/**
* @return ProcessableInterface
*/
private function getInvoiceProcessableHelper()
protected function getInvoiceProcessableHelper()
{
return $this->get('coreshop.order.invoice.processable');
}

/**
* @return ProcessableInterface
*/
private function getShipmentProcessableHelper()
protected function getShipmentProcessableHelper()
{
return $this->get('coreshop.order.shipment.processable');
}

/**
* @return OrderInvoiceRepositoryInterface
*/
private function getOrderInvoiceRepository()
protected function getOrderInvoiceRepository()
{
return $this->get('coreshop.repository.order_invoice');
}

/**
* @return OrderShipmentRepositoryInterface
*/
private function getOrderShipmentRepository()
protected function getOrderShipmentRepository()
{
return $this->get('coreshop.repository.order_shipment');
}

/**
* @return WorkflowStateInfoManagerInterface
*/
private function getWorkflowStateManager()
protected function getWorkflowStateManager()
{
return $this->get('coreshop.workflow.state_info_manager');
}
Expand Down Expand Up @@ -453,4 +458,5 @@ protected function getSaleNumberField()
{
return 'orderNumber';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ CoreShop\Component\Order\Model\Cart:
serialized_name: paymentProvider
exp: object.getPaymentProvider(true)
type: relation
groups: [Detailed]
groups: [Detailed]
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ CoreShop\Component\Order\Model\Order:
exp: object.getPaymentSettings()
type: array
groups: [Detailed]

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CoreShop\Component\Order\Model\OrderInvoice:
exclusion_policy: ALL
xml_root_name: order_invoice
virtual_properties:
o_id:
serialized_name: o_id
exp: object.getId()
type: integer
groups: [Detailed]
documentType:
serialized_name: documentType
exp: object.getDocumentType()
type: string
groups: [Detailed]
totalTax:
serialized_name: totalTax
exp: object.getTotalTax()
type: integer
groups: [Detailed]
invoiceDate:
serialized_name: invoiceDate
exp: object.getInvoiceDate()
type: integer
groups: [Detailed]
invoiceNumber:
serialized_name: invoiceNumber
exp: object.getInvoiceNumber()
type: string
groups: [Detailed]
totalNet:
serialized_name: totalNet
exp: object.getTotal(false)
type: integer
groups: [Detailed]
totalGross:
serialized_name: totalGross
exp: object.getTotal(true)
type: integer
groups: [Detailed]
items:
serialized_name: items
exp: object.getItems
type: array
groups: [Detailed]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CoreShop\Component\Order\Model\OrderInvoiceItem:
exclusion_policy: ALL
xml_root_name: order_invoice_item
virtual_properties:
_itemName:
serialized_name: _itemName
exp: 'object.getOrderItem() != null ? object.getOrderItem().getName() : null' # TODO: add interface check when upgrading to JMS Serializer > 3.0
type: string
groups: [Detailed]
name:
serialized_name: name
exp: 'object.getOrderItem() != null ? object.getOrderItem().getName() : null' # TODO: add interface check when upgrading to JMS Serializer > 3.0
type: string
groups: [Detailed]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CoreShop\Component\Order\Model\OrderShipment:
exclusion_policy: ALL
xml_root_name: order_shipment
virtual_properties:
o_id:
serialized_name: o_id
exp: object.getId()
type: integer
groups: [Detailed]
documentType:
serialized_name: documentType
exp: object.getDocumentType()
type: string
groups: [Detailed]
shipmentDate:
serialized_name: shipmentDate
exp: object.getShipmentDate()
type: integer
groups: [Detailed]
shipmentNumber:
serialized_name: shipmentNumber
exp: object.getShipmentNumber()
type: string
groups: [Detailed]
trackingCode:
serialized_name: trackingCode
exp: object.getTrackingCode()
type: integer
groups: [Detailed]
items:
serialized_name: items
exp: object.getItems()
type: array
groups: [Detailed]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CoreShop\Component\Order\Model\OrderShipmentItem:
exclusion_policy: ALL
xml_root_name: order_shipment_item
virtual_properties:
_itemName:
serialized_name: _itemName
exp: 'object.getOrderItem() != null ? object.getOrderItem().getName() : null' # TODO: add interface check when upgrading to JMS Serializer > 3.0
type: string
groups: [Detailed]
name:
serialized_name: name
exp: 'object.getOrderItem() != null ? object.getOrderItem().getName() : null' # TODO: add interface check when upgrading to JMS Serializer > 3.0
type: string
groups: [Detailed]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CoreShop\Component\Order\Model\QuoteItem:
exclusion_policy: ALL
xml_root_name: order_item
xml_root_name: quote_item

Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ CoreShop\Component\Order\Model\Sale:
exp: object.getTotal(false)
type: integer
groups: [Detailed]
totaGross:
totalGross:
serialized_name: totalGross
exp: object.getTotal(true)
type: integer
Expand All @@ -106,7 +106,7 @@ CoreShop\Component\Order\Model\Sale:
exp: object.getBaseTotal(false)
type: integer
groups: [Detailed]
baseTotaGross:
baseTotalGross:
serialized_name: baseTotalGross
exp: object.getBaseTotal(true)
type: integer
Expand Down