Skip to content

Commit

Permalink
Bugfixes for order base manager (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvindice authored and aimeos committed Oct 8, 2019
1 parent 5e34c1b commit 6c3e2d3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
8 changes: 4 additions & 4 deletions lib/mshoplib/setup/unittest/data/order.php
Expand Up @@ -27,10 +27,10 @@
'order/base/product' => array(
'CNE/19.95' => array( 'baseid' => '19.95', 'type'=> 'default', 'prodid' => 'CNE', 'prodcode' => 'CNE', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type1', 'name' => 'Cafe Noire Expresso', 'mediaurl' => 'somewhere/thump1.jpg', 'amount' => 9, 'currencyid' => 'EUR', 'price' => '4.50', 'shipping' => '0.00', 'rebate' => '0.00', 'taxrate' => '0.00', 'flags' => '0', 'pos' => 1, 'status' => 1 ),
'CNC/19.95' => array( 'baseid' => '19.95', 'type'=> 'default', 'prodid' => 'CNC', 'prodcode' => 'CNC', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type2', 'name' => 'Cafe Noire Cappuccino', 'mediaurl' => 'somewhere/thump2.jpg', 'amount' => 3, 'currencyid' => 'EUR', 'price' => '6.00', 'shipping' => '0.50', 'rebate' => '0.00', 'taxrate' => '0.00', 'flags' => '0', 'pos' => 2, 'status' => 1 ),
'U:MD/19.95' => array( 'baseid' => '19.95', 'type'=> 'default', 'prodid' => 'U:MD', 'prodcode' => 'U:MD', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type3', 'name' => 'Unittest: Monetary rebate', 'mediaurl' => 'somewhere/thump3.jpg', 'amount' => 1, 'currencyid' => 'EUR', 'price' => '-5.00', 'shipping' => '0.00', 'rebate' => '5.00', 'taxrate' => '0.00', 'flags' => '0', 'pos' => 3, 'status' => 1 ),
'ABCD/19.95' => array( 'baseid' => '19.95', 'type'=> 'default', 'prodid' => 'ABCD', 'prodcode' => 'ABCD', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type1', 'name' => '16 discs', 'mediaurl' => 'somewhere/thump4.jpg', 'amount' => 1, 'currencyid' => 'EUR', 'price' => '0.00', 'shipping' => '0.00', 'rebate' => '4.50', 'taxrate' => '0.00', 'flags' => '0', 'pos' => 4, 'status' => 1 ),
'CNE/636.00' => array( 'baseid' => '636.00', 'type'=> 'default', 'prodid' => 'CNE', 'prodcode' => 'CNE', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type1', 'name' => 'Cafe Noire Expresso', 'mediaurl' => 'somewhere/thump5.jpg', 'amount' => 2, 'currencyid' => 'EUR', 'price' => '36.00', 'shipping' => '1.00', 'rebate' => '0.00', 'taxrate' => '19.00', 'flags' => '0', 'pos' => 1, 'status' => 1 ),
'CNC/636.00' => array( 'baseid' => '636.00', 'type'=> 'default', 'prodid' => 'CNC', 'prodcode' => 'CNC', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type2', 'name' => 'Cafe Noire Cappuccino', 'mediaurl' => 'somewhere/thump6.jpg', 'amount' => 1, 'currencyid' => 'EUR', 'price' => '600.00', 'shipping' => '30.00', 'rebate' => '0.00', 'taxrate' => '19.00', 'flags' => '0', 'pos' => 2, 'status' => 1 ),
'U:MD/19.95' => array( 'baseid' => '19.95', 'type'=> 'default', 'prodid' => 'U:MD', 'prodcode' => 'U:MD', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type3', 'name' => 'Unittest: Monetary rebate', 'mediaurl' => 'somewhere/thump3.jpg', 'amount' => 1, 'currencyid' => 'EUR', 'price' => '-5.00', 'shipping' => '0.00', 'rebate' => '5.00', 'taxrate' => '0.00', 'flags' => '1', 'pos' => 3, 'status' => 1 ),
'ABCD/19.95' => array( 'baseid' => '19.95', 'type'=> 'default', 'prodid' => 'ABCD', 'prodcode' => 'ABCD', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type1', 'name' => '16 discs', 'mediaurl' => 'somewhere/thump4.jpg', 'amount' => 1, 'currencyid' => 'EUR', 'price' => '0.00', 'shipping' => '0.00', 'rebate' => '4.50', 'taxrate' => '0.00', 'flags' => '1', 'pos' => 4, 'status' => 1 ),
'CNE/636.00' => array( 'baseid' => '636.00', 'type'=> 'default', 'prodid' => 'CNE', 'prodcode' => 'CNE', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type1', 'name' => 'Cafe Noire Expresso', 'mediaurl' => 'somewhere/thump5.jpg', 'amount' => 2, 'currencyid' => 'EUR', 'price' => '36.00', 'shipping' => '1.00', 'rebate' => '0.00', 'taxrate' => '19.00', 'flags' => '1', 'pos' => 1, 'status' => 1 ),
'CNC/636.00' => array( 'baseid' => '636.00', 'type'=> 'default', 'prodid' => 'CNC', 'prodcode' => 'CNC', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type2', 'name' => 'Cafe Noire Cappuccino', 'mediaurl' => 'somewhere/thump6.jpg', 'amount' => 1, 'currencyid' => 'EUR', 'price' => '600.00', 'shipping' => '30.00', 'rebate' => '0.00', 'taxrate' => '19.00', 'flags' => '1', 'pos' => 2, 'status' => 1 ),
// product bundle test data
'bdl:zyx/18.00' => array( 'baseid' => '18.00', 'type'=> 'bundle', 'prodid' => 'bdl:zyx', 'ordprodid' => null, 'prodcode' => 'bdl:zyx', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type1', 'name' => 'Bundle Unittest1', 'mediaurl' => 'somewhere/thump6.jpg', 'amount' => 1, 'currencyid' => 'EUR', 'price' => '1200.00', 'shipping' => '30.00', 'rebate' => '0.00', 'taxrate' => '17.00', 'flags' => '0', 'pos' => 1, 'status' => 1 ),
'bdl:EFG/18.00' => array( 'baseid' => '18.00', 'type'=> 'default', 'prodid' => 'bdl:EFG', 'ordprodid' => 'bdl:zyx/18.00', 'prodcode' => 'bdl:EFG', 'type'=> 'default', 'suppliercode' => 'unitsupplier', 'stocktype' => 'unit_type1', 'name' => 'Bundle Unittest1', 'mediaurl' => 'somewhere/thump6.jpg', 'amount' => 1, 'currencyid' => 'EUR', 'price' => '600.00', 'shipping' => '30.00', 'rebate' => '0.00', 'taxrate' => '16.00', 'flags' => '0', 'pos' => 2, 'status' => 1 ),
Expand Down
40 changes: 35 additions & 5 deletions lib/mshoplib/src/MShop/Order/Manager/Base/Base.php
Expand Up @@ -291,6 +291,24 @@ protected function getCoupons( array $baseIds, $fresh = false, array $products =
$map = [];
$manager = $this->getObject()->getSubManager( 'coupon' );

$productMap = [];
foreach( $products as $baseId => $arr )
{
if( !isset( $productMap[$baseId] ) ) {
$productMap[$baseId] = [];
}
foreach( $arr as $order => $product )
{
$productMap[$baseId][$product->getId()] = $product;
if( $fresh === true )
{
$product->setPosition( null );
$product->setBaseId( null );
$product->setId( null );
}
}
}

$criteria = $manager->createSearch()->setSlice( 0, 0x7fffffff );
$criteria->setConditions( $criteria->compare( '==', 'order.base.coupon.baseid', $baseIds ) );
$sort = [$criteria->sort( '+', 'order.base.coupon.baseid' ), $criteria->sort( '+', 'order.base.coupon.code' )];
Expand All @@ -302,8 +320,8 @@ protected function getCoupons( array $baseIds, $fresh = false, array $products =
$map[$item->getBaseId()][$item->getCode()] = [];
}

if( $item->getProductId() !== null && isset( $products[$item->getBaseId()][$item->getProductId()] ) ) {
$map[$item->getBaseId()][$item->getCode()][] = $products[$item->getBaseId()][$item->getProductId()];
if( $item->getProductId() !== null && isset( $productMap[$item->getBaseId()][$item->getProductId()] ) ) {
$map[$item->getBaseId()][$item->getCode()][] = $productMap[$item->getBaseId()][$item->getProductId()];
}
}

Expand Down Expand Up @@ -471,12 +489,19 @@ protected function loadItems( $id, \Aimeos\MShop\Price\Item\Iface $price,
protected function loadFresh( $id, \Aimeos\MShop\Price\Item\Iface $price,
\Aimeos\MShop\Locale\Item\Iface $localeItem, $row, $parts )
{
$products = $addresses = $services = [];
$products = $coupons = $addresses = $services = [];

if( $parts & \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT ) {
$products = $this->loadProducts( $id, true );
}

if( $parts & \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON ) {
// load coupons with product array containing product ids for coupon/product matching
// not very efficient, a better solution might be considered for 2020.01 release
// see https://github.com/aimeos/aimeos-core/pull/175 for discussion
$coupons = $this->loadCoupons( $id, true, $this->loadProducts( $id, false ) );
}

if( $parts & \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS ) {
$addresses = $this->loadAddresses( $id, true );
}
Expand All @@ -485,12 +510,17 @@ protected function loadFresh( $id, \Aimeos\MShop\Price\Item\Iface $price,
$services = $this->loadServices( $id, true );
}


$basket = $this->createItemBase( $price, $localeItem, $row );
$basket->setId( null );

foreach( $products as $item ) {
$basket->addProduct( $item );
if( !($item->getFlags() & \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ) ) {
$basket->addProduct( $item );
}
}

foreach( $coupons as $code => $item ) {
$basket->addCoupon( $code, $item );
}

foreach( $addresses as $item ) {
Expand Down
21 changes: 12 additions & 9 deletions lib/mshoplib/tests/MShop/Order/Manager/Base/StandardTest.php
Expand Up @@ -515,7 +515,7 @@ public function testLoadFresh()
$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, true );


$this->assertEquals( 0, count( $order->getCoupons() ) );
$this->assertEquals( 2, count( $order->getCoupons() ) );

foreach( $order->getAddresses() as $address )
{
Expand Down Expand Up @@ -583,8 +583,8 @@ public function testLoadFreshCoupon()
$order = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON, true );

$this->assertEquals( [], $order->getAddresses() );
$this->assertEquals( [], $order->getCoupons() );
$this->assertEquals( [], $order->getProducts() );
$this->assertGreaterThan( 0, count( $order->getCoupons() ) );
$this->assertGreaterThan( 0, count( $order->getProducts() ) );
$this->assertEquals( [], $order->getServices() );
}

Expand Down Expand Up @@ -818,22 +818,25 @@ public function testLoadStoreCoupons()
throw new \RuntimeException( 'No order found' );
}

$basket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, true );
$parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL ^ \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON;
$basket = $this->object->load( $item->getId(), $parts, true );

$this->assertEquals( '672.00', $basket->getPrice()->getValue() );
$this->assertEquals( '37.00', $basket->getPrice()->getCosts() );
$this->assertEquals( '0.00', $basket->getPrice()->getValue() );
$this->assertEquals( '5.00', $basket->getPrice()->getCosts() );
$this->assertEquals( 0, count( $basket->getCoupons() ) );

$productBasket = $this->object->load( $item->getId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, true );

$basket->addCoupon( 'CDEF' );
$basket->addCoupon( '5678', $basket->getProducts() );
$basket->addCoupon( '5678', $productBasket->getProducts() );
$this->assertEquals( 2, count( $basket->getCoupons() ) );

$this->object->store( $basket );
$newBasket = $this->object->load( $basket->getId() );
$this->object->deleteItem( $newBasket->getId() );

$this->assertEquals( '1344.00', $newBasket->getPrice()->getValue() );
$this->assertEquals( '64.00', $newBasket->getPrice()->getCosts() );
$this->assertEquals( '672.00', $newBasket->getPrice()->getValue() );
$this->assertEquals( '32.00', $newBasket->getPrice()->getCosts() );
$this->assertEquals( '5.00', $newBasket->getPrice()->getRebate() );
$this->assertEquals( 2, count( $newBasket->getCoupons() ) );
}
Expand Down

0 comments on commit 6c3e2d3

Please sign in to comment.