proudcommerce / psArticleRequest

Notification tool if non-deliverable item is back in stock.
3 stars 9 forks source link

Sends all e-mails instead per article #10

Closed PacoHolliday closed 4 years ago

PacoHolliday commented 4 years ago

When I use setting auto1/2 it sends all e-mails when I change the stock of any article.

proudcommerce commented 4 years ago

Can you provide a fix/PR?

PacoHolliday commented 4 years ago

Yes, should I pull? ArticleStock.php needs a small fix, the ID of the article needs to be passed and included in the select:

`public function save() { $soxId = $this->getEditObjectId();

    if (Registry::getConfig()->getConfigParam('psArticleRequest_stockinfo') == "auto1") {

        /** @var Article $oArticle */
        $oArticle = oxNew(Article::class);
        $oArticle->load($soxId);
        $iOldStock = $oArticle->oxarticles__oxstock->value;
    }

    parent::save();

    if (Registry::getConfig()->getConfigParam('psArticleRequest_stockinfo') != "man") {
        $this->_getPsArticleRequests($soxId, $iOldStock);
    }

}

/**
 * Gets open article requests and sends email to requested user when stock is updated
 * @param int $iOldStock
 * @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
 * @throws \OxidEsales\Eshop\Core\Exception\DatabaseErrorException
 */
protected function _getPsArticleRequests($psArticle, $iOldStock = 0)
{
    $iPsStock = 999;   // psArticleRequest_stockinfo == auto2
    if (Registry::getConfig()->getConfigParam('psArticleRequest_stockinfo') == "auto1") {
        $aParams = Registry::getRequest()->getRequestParameter("editval");
        $iNewStock = $aParams['oxarticles__oxstock'];
        $iPsStock = $iNewStock - $iOldStock;
    }

    //$soxId = $this->getEditObjectId();

    $iCount = 1;
    $sSql = "SELECT oxid, oxemail FROM psarticlerequest WHERE oxstatus = 1 AND oxartid = '" . $psArticle . "' ORDER BY OXINSERT";
    $aRequests = DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC)->getAll($sSql);
    if ($iPsStock > 0 && count($aRequests) > 0) {
        foreach ($aRequests as $aRequest) {
            if ($iCount <= $iPsStock) {
                /** @var ArticleRequest $oPsArticleRequest */
                $oPsArticleRequest = oxNew(ArticleRequest::class);
                if ($oPsArticleRequest->load($aRequest["oxid"])) {
                    /** @var \ProudCommerce\ArticleRequest\Core\Email $oEmail */
                    $oEmail = oxNew(Email::class);
                    if ($oEmail->sendArticleRequestToCustomer($aRequest["oxemail"], $oPsArticleRequest)) {
                        $oPsArticleRequest->psarticlerequest__oxsended->setValue(date("Y-m-d H:i:s"));
                        if ($oPsArticleRequest->psarticlerequest__oxstatus >= ArticleRequest::STATUS_SENT_NOTIFICATION) {
                            $oPsArticleRequest->psarticlerequest__oxstatus->setValue(ArticleRequest::STATUS_RESENT_NOTIFICATION);
                        } else {
                            $oPsArticleRequest->psarticlerequest__oxstatus->setValue(ArticleRequest::STATUS_SENT_NOTIFICATION);
                        }
                        $oPsArticleRequest->save();
                    }
                    $iCount++;
                }
            }
        }
    }
}`
proudcommerce commented 4 years ago

A Pull Request would be great ;-)

proudcommerce commented 4 years ago

fixed with https://github.com/proudcommerce/psArticleRequest/pull/11