Skip to content

Commit

Permalink
Merge pull request #235 from magmodules/release/1.20.0
Browse files Browse the repository at this point in the history
Release/1.20.0
  • Loading branch information
Marvin-Magmodules authored Jan 9, 2025
2 parents 382aaf8 + 5dc281a commit 8f78d2e
Show file tree
Hide file tree
Showing 16 changed files with 580 additions and 453 deletions.
365 changes: 16 additions & 349 deletions Helper/Product.php

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Helper/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ public function getAttributes($type, $filters = [], $storeId = null)
];

if ($type != 'api') {
$attributes['updated_at'] = [
'label' => 'updated_at',
'source' => 'updated_at',
];
$attributes['created_at'] = [
'label' => 'created_at',
'source' => 'created_at',
Expand Down
38 changes: 38 additions & 0 deletions Plugin/AddDiscountToInvoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magmodules\Channable\Plugin;

use Magento\Sales\Model\Order\Invoice;

class AddDiscountToInvoice
{
/**
* Add channable discount total to the invoice.
*
* @param Invoice $invoice
* @param callable $proceed
* @return void
*/
public function aroundCollectTotals(Invoice $invoice, callable $proceed)
{
$invoice = $proceed();

$order = $invoice->getOrder();
if ($order->getPayment()->getMethod() == 'channable') {
$discountAmount = abs((float)$order->getDiscountAmount());
if ($discountAmount > 0) {
$invoice->setDiscountAmount(-$discountAmount);
$invoice->setBaseDiscountAmount(-$discountAmount);
$invoice->setGrandTotal($invoice->getGrandTotal() - $discountAmount);
$invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $discountAmount);
}
}

return $invoice;
}
}
19 changes: 16 additions & 3 deletions Service/Category/CategoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreManagerInterface;

class CategoryData
Expand Down Expand Up @@ -44,6 +45,7 @@ public function __construct(
* @param ProductCollection $parents
* @param int $storeId
* @return array
* @throws LocalizedException
*/
public function load(ProductCollection $products, ProductCollection $parents, int $storeId): array
{
Expand All @@ -68,27 +70,38 @@ public function load(ProductCollection $products, ProductCollection $parents, in
private function getCategoryIdsForCollection(array $productIds): array
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('catalog_category_product');
$ccpTable = $this->resourceConnection->getTableName('catalog_category_product');

$select = $connection->select()
->from($tableName, 'category_id')
->from($ccpTable, 'category_id')
->where('product_id IN (?)', $productIds)
->group('category_id');

$categoryIds = $connection->fetchCol($select);

$cceTable = $this->resourceConnection->getTableName('catalog_category_entity');
$select = $connection->select()
->from($cceTable, 'entity_id')
->where('entity_id IN (?) OR parent_id IN (?)', $categoryIds);

return $connection->fetchCol($select);
}

/**
* @param array $categoryIds
* @param int $storeId
* @return array
* @throws LocalizedException
*/
private function getCategoryTree(array $categoryIds, int $storeId): array
{
$collection = $this->categoryCollectionFactory->create()
->setStoreId($storeId)
->addAttributeToSelect(['name', 'level', 'path', 'url_path', self::EXCLUDE_ATTRIBUTE])
->addFieldToFilter('entity_id', ['in' => $categoryIds])
->addFieldToFilter([
['attribute' => 'entity_id', 'in' => $categoryIds],
['attribute' => 'parent_id', 'in' => $categoryIds]
])
->addFieldToFilter('is_active', ['eq' => 1]);

try {
Expand Down
6 changes: 3 additions & 3 deletions Service/Order/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ public function execute(ChannableOrderData $orderData): OrderInterface

if (isset($orderData['price']['discount']) && !empty((float)$orderData['price']['discount'])) {
$discountAmount = abs((float)$orderData['price']['discount']);
$order->setDiscountDescription(__('Channable discount'));
$order->setBaseDiscountAmount($discountAmount);
$order->setDiscountAmount($discountAmount);
$order->setDiscountDescription($orderData['channel_name']);
$order->setBaseDiscountAmount($discountAmount * -1);
$order->setDiscountAmount($discountAmount * -1);
$order->setGrandTotal($order->getGrandTotal() - $discountAmount);
$order->setBaseGrandTotal($order->getBaseGrandTotal() - $discountAmount);
}
Expand Down
1 change: 0 additions & 1 deletion Service/Order/ImportSimulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public function getTestData(array $params): array
"middle_name" => "From",
"last_name" => "Channable",
"company" => "Do Not Ship",
"vat_id" => 'NL0001',
"email" => "[email protected]",
"address_line_1" => "Billing Line 1",
"address_line_2" => "Billing Line 2",
Expand Down
7 changes: 3 additions & 4 deletions Service/Order/Items/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,10 @@ private function getProductPrice(
Quote $quote,
bool $isBusinessOrder
): float {
$price = (float)$item['price'];
if ($isBusinessOrder) {
return (float)$item['price'];
return $price;
}
$price = (float)$item['price'] - $this->getProductWeeTax($product, $quote);

if (!$this->configProvider->getNeedsTaxCalulcation('price', (int)$store->getId())) {
$request = $this->taxCalculation->getRateRequest(
$quote->getShippingAddress(),
Expand All @@ -231,7 +230,7 @@ private function getProductPrice(
$price = $price / (100 + $percent) * 100;
}

return $price;
return $price - $this->getProductWeeTax($product, $quote);
}

/**
Expand Down
34 changes: 30 additions & 4 deletions Service/Order/Quote/AddressHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,9 @@ public function getAddressData(string $type, array $orderData, Quote $quote): ar
: null,
'postcode' => $address['zip_code'],
'telephone' => $telephone,
'vat_id' => !empty($address['vat_id']) ? $address['vat_id'] : null,
'vat_id' => $this->getVatId($type, $orderData, $storeId),
'email' => $email
];
if (isset($address['vat_number']) && $this->configProvider->isBusinessOrderEnabled()) {
$addressData['vat_id'] = $address['vat_number'];
}

if ($this->configProvider->createCustomerOnImport((int)$storeId)) {
$this->saveAddress($addressData, $customerId, $type);
Expand All @@ -149,6 +146,35 @@ public function getAddressData(string $type, array $orderData, Quote $quote): ar
return $addressData;
}

/**
* Channable only sets VAT ID on billing address
* In some cases we also need this on shipping address (due to OSS/MOSS)
*
* @param string $type
* @param array $orderData
* @param int $storeId
* @return string|null
*/
private function getVatId(string $type, array $orderData, int $storeId): ?string
{
if (!$this->configProvider->isBusinessOrderEnabled($storeId)) {
return null;
}

$vatId = !empty($orderData['billing']['vat_number']) ? $orderData['billing']['vat_number'] : null;
if ($type == 'billing' || !$vatId) {
return $vatId;
}

if (empty($orderData['customer']['business_order']) || !$this->configProvider->importCompanyName($storeId)) {
return null;
}

return $orderData['billing']['company'] == $orderData['shipping']['company']
? $vatId
: null;
}

/**
* Removed unwanted characters from email.
* Some Marketplaces add ":" to email what can cause import to fail.
Expand Down
Loading

0 comments on commit 8f78d2e

Please sign in to comment.