aimeos / ai-controller-jobs

Aimeos e-commerce job controllers
https://aimeos.org
GNU Lesser General Public License v3.0
28 stars 17 forks source link

Stock csv import more stock type bug #48

Open gtpt23 opened 11 months ago

gtpt23 commented 11 months ago

Hello!

There is a bug in this file if you have several stocks and run import several times.

namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Stock; class Standard

Here is the problem: if( ( $item = $items->pop() ) === null ) {

The pop() sometimes one, sometimes the other.

This is my improved code:

try
        {
            $stock = 0;
            $map = $this->getMappedChunk( $data, $this->getMapping() );

            **// delete this line
            //$items = $manager->search( $manager->filter()->add( ['stock.productid' => $product->getId()] ) );**

            foreach( $map as $pos => $list )
            {
                if( !array_key_exists( 'stock.stocklevel', $list ) ) {
                    continue;
                }

                $list['stock.productid'] = $product->getId();
                $list['stock.type'] = $this->val( $list, 'stock.type', 'default' );

                $this->addType( 'stock/type', 'product', $list['stock.type'] );

                **// add this line, filter for stock.type too
                $items = $manager->search( $manager->filter()->add( [
                    'stock.productid' => $product->getId(),
                    'stock.type' => $list['stock.type']
                ] ) );**

                if( ( $item = $items->pop() ) === null ) {
                    $item = $manager->create();
                }

                $manager->save( $item->fromArray( $list ), false );

Best regards

TGergo

aimeos commented 11 months ago

Thanks a lot! Can you create a PR with your change so we can merge it?

gtpt23 commented 11 months ago

This is a quick fix. Maybe you should refactor it. It would be worth checking if there is an error with the XML import as well.

aimeos commented 11 months ago

We've optimized your fix and merged it into dev-master and 2023.10.x-dev so it will be part of the next release: https://github.com/aimeos/ai-controller-jobs/commit/e06814740224c932fe1e7d8de90119b8a7550624

There's no stock import using XML files and the stock CSV importer doesn't share the problem.