Tag: programatically

Magento: crear Credito en Tienda manualmente

store-credit

Magento Enterprise cuenta con una característica muy particular, los store credits o creditos en tienda.

Desafortunadamente el modelo del store credit solo funciona para crearlos a través del backend.

Aquí les pongo un método desarrollado por mí para poder crearlos manualmente:


public function manuallyStoreCredit($customer, $amount)
{
    $customerId = $customer->getId();
    $websiteId = $customer->getWebsiteId();
    try{
        $products = Mage::getSingleton('core/resource');
        $writeConnection = $products->getConnection('core_write');
        $readConnection = $products->getConnection('core_write');
        $writeConnection->beginTransaction();
        //Validamos que no exista una nota de credito
        $queryValidate = "SELECT COUNT(*) FROM enterprise_customerbalance WHERE         customer_id = '{$customerId}'";
        $cant = $readConnection->fetchOne($queryValidate);

        if ($cant > 0) {
            $updateBalance = "UPDATE enterprise_customerbalance SET amount = '{$amount}'  WHERE customer_id = '{$customerId}'";
            $writeConnection->query($updateBalance);
            $queryBalanceId = "SELECT balance_id FROM enterprise_customerbalance WHERE customer_id = '{$customerId}'";
            $balanceId = $readConnection->fetchOne($queryBalanceId);
            $queryHistory = "INSERT INTO enterprise_customerbalance_history (balance_id, updated_at, action, balance_amount, balance_delta, additional_info, is_customer_notified)";
            $queryHistory .= " VALUES ('{$balanceId}', CURRENT_TIMESTAMP, 1, '{$amount}', '{$amount}', 'SE ACTUALIZA NOTA DE CREDITO POR WS', '0')";
            $writeConnection->query($queryHistory);
        } else {
            $query = "INSERT INTO enterprise_customerbalance (customer_id, website_id, amount ) VALUES ('{$customerId}', '{$websiteId}', '{$amount}')";
            $writeConnection->query($query);
            $balanceId = $writeConnection->lastInsertId();
            $queryHistory = "INSERT INTO enterprise_customerbalance_history (balance_id, updated_at, action, balance_amount, balance_delta, additional_info, is_customer_notified)";
            $queryHistory .= " VALUES ('{$balanceId}', CURRENT_TIMESTAMP, 2, '{$amount}', '{$amount}', 'SE CREA NOTA DE CREDITO MANUALMENTE', '0')";
            $writeConnection->query($queryHistory);
        }
        $writeConnection->commit();
    }
    catch(Exception $e) {
        Mage::log($e);
    }
}