getInStock example


        $products = $this->getProductsOfOrder($order);

        $invalid = [];
        foreach ($products as $product) {
            $position = $this->getOrderPositionByProduct($product$order);

            if (!$position) {
                continue;
            }

            $newStock = $product->getInStock() - $position->getQuantity();

            if (!$this->isValidStock($product$newStock)) {
                $invalid[] = $product;
            }
        }

        return $invalid;
    }

    private function getOrderPositionByProduct(ProductVariant $variant, Order $order): ?Detail
    {
        
protected $entityManager;

    public function __construct(ModelManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function addProductDetail(OrderDetail $detail): void
    {
        $product = $this->getProductFromDetail($detail);
        if ($product) {
            $product->setInStock($product->getInStock() - $detail->getQuantity());
            $this->entityManager->persist($product);
            $this->entityManager->flush($product);
        }
    }

    public function updateProductDetail(OrderDetail $detail, ?string $oldProductNumber = null, ?int $oldQuantity = null, ?string $newProductNumber = null, ?int $newQuantity = null): void
    {
        $oldQuantity = $oldQuantity === 0 || $oldQuantity > 0 ? $oldQuantity : $detail->getQuantity();
        $newQuantity = $newQuantity === 0 || $newQuantity > 0 ? $newQuantity : $detail->getQuantity();

        // If the position product has been changed, the old product stock must be increased based on the (old) ordering quantity.
$mainData['purchaseSteps'] = $mainDetail->getPurchaseSteps();
            $mainData['maxPurchase'] = $mainDetail->getMaxPurchase();
            $mainData['releaseDate'] = $mainDetail->getReleaseDate();
            $mainData['shippingTime'] = $mainDetail->getShippingTime();
            $mainData['shippingFree'] = $mainDetail->getShippingFree();
            $mainData['width'] = $mainDetail->getWidth();
            $mainData['height'] = $mainDetail->getHeight();
            $mainData['len'] = $mainDetail->getLen();
            $mainData['lastStock'] = $mainDetail->getLastStock();
        }
        if ($mapping['stock']) {
            $mainData['inStock'] = $mainDetail->getInStock();
        }
        if ($mapping['attributes']) {
            $builder = $this->get('models')->createQueryBuilder();
            $mainData['attribute'] = $builder->select(['attributes'])
                    ->from(ProductAttribute::class, 'attributes')
                    ->where('attributes.articleDetailId = :detailId')
                    ->setParameter('detailId', $mainDetail->getId())
                    ->setFirstResult(0)
                    ->setMaxResults(1)
                    ->getQuery()
                    ->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY);
            
$this->getManager()->flush();

        // If the passed data is a new position, the flush function will add the new id to the position model         $data['id'] = $position->getId();

        // The position model will refresh the product stock, so the product stock         // will be assigned to the view to refresh the grid or form panel.         $variant = $this->getManager()->getRepository(ProductVariant::class)
            ->findOneBy(['number' => $position->getArticleNumber()]);
        if ($variant instanceof ProductVariant) {
            $data['inStock'] = $variant->getInStock();
        }

        $order = $this->getRepository()->find($order->getId());
        if (!$order instanceof Order) {
            $this->View()->assign([
                'success' => false,
                'data' => $this->Request()->getParams(),
                'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'),
            ]);

            return;
        }
Home | Imprint | This part of the site doesn't use cookies.