Skip to content

Commit

Permalink
Feature/ajuste no envio de informacoes (#52)
Browse files Browse the repository at this point in the history
* Envia valores de desconto e acréscimos para a Vindi
  • Loading branch information
Maikco authored and laerte-guimaraes committed Aug 12, 2019
1 parent 70b6c42 commit 2beef3b
Showing 1 changed file with 62 additions and 10 deletions.
72 changes: 62 additions & 10 deletions Model/Payment/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@ public function __construct(

public function findOrCreateProducts($order)
{
$list = [];
foreach ($order->getItems() as $item) {
$productType = $item->getProduct()->getTypeId();
$vindiProductId = $this->findOrCreateProduct($item->getSku(), $item->getName(), $productType);

for ($i = 0; $i < $item->getQtyOrdered(); $i++) {
$itemPrice = $this->getItemPrice($item, $productType);

if (false === $itemPrice)
if (! $itemPrice)
continue;

$list[] = [
array_push($list, [
'product_id' => $vindiProductId,
'amount' => $itemPrice
];
]);
}
}

if ($order->getShippingAmount() > 0) {
$shippingId = $this->findOrCreateProduct('frete', 'frete');
$list[] = [
'product_id' => $shippingId,
'amount' => $order->getShippingAmount()
];
}
$list = $this->buildTax($list, $order);
$list = $this->buildDiscount($list, $order);
$list = $this->buildShipping($list, $order);

return $list;
}

Expand All @@ -52,6 +50,60 @@ private function getItemPrice($item, $productType)
return $item->getPrice();
}

/**
* @param array $list
* @param $order
* @return array
*/
private function buildTax(array $list, $order)
{
if ($order->getTaxAmount() > 0) {
$productId = $this->findOrCreateProduct('taxa', 'Taxa');
array_push($list, [
'product_id' => $productId,
'amount' => $order->getTaxAmount()
]);
}

return $list;
}

/**
* @param $list
* @param $order
* @return array
*/
private function buildDiscount(array $list, $order)
{
if ($order->getDiscountAmount() < 0) {
$productId = $this->findOrCreateProduct('cupom', 'Cupom de Desconto');
array_push($list, [
'product_id' => $productId,
'amount' => $order->getDiscountAmount()
]);
}

return $list;
}

/**
* @param array $list
* @param $order
* @return array
*/
private function buildShipping(array $list, $order)
{
if ($order->getShippingAmount() > 0) {
$productId = $this->findOrCreateProduct('frete', 'frete');
array_push($list, [
'product_id' => $productId,
'amount' => $order->getShippingAmount()
]);
}

return $list;
}

private function findOrCreateProduct($itemSku, $itemName, $itemType = 'simple')
{
$itemName = $itemType == 'configurable' ? $itemSku : $itemName;
Expand Down

0 comments on commit 2beef3b

Please sign in to comment.