Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faturar ordens/pedidos pagos automaticamente #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/code/local/Gamuza/Itaushopline/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
* See the ChangeLog files for a list of changes.
* These files are distributed with Gamuza_Itaushopline at http://code.google.com/p/gamuzaopen/.
*/

class Gamuza_Itaushopline_Helper_Data
extends Mage_Core_Helper_Abstract
class Gamuza_Itaushopline_Helper_Data extends Mage_Core_Helper_Abstract
{
public function log($message)
{
Mage::log($this->__($message), Zend_Log::INFO, 'gamuza_itaushopline.log');
}
}
52 changes: 52 additions & 0 deletions app/code/local/Gamuza/Itaushopline/Helper/Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

class Gamuza_Itaushopline_Helper_Http
{
/**
* Do a GET request and return response content
*
* @param string $url URL to make request
* @param array|null $params Params to be passed to request
*
* @return string Response content
*/
public function get($url, $params = null)
{
return $this->getUrlContent(Zend_Http_Client::GET, $url, $params);
}

/**
* Do a POST request and return response content
*
* @param string $url URL to make request
* @param array|null $params Params to be passed to request
*
* @return string Response content
*/
public function post($url, $params = null)
{
return $this->getUrlContent(Zend_Http_Client::POST, $url, $params);
}

/**
* Make request and return response content
*
* @param string $method Valid Zend_Http_Client constant
* @param string $url URL to make request
* @param array|null $params Params to be passed to request
*
* @return string
*/
private function getUrlContent($method, $url, $params = null)
{
if (strpos($url, '://') === false) {
$url = "http://$url";
}

$request = new Zend_Http_Client($url);
$request->setParameterGet($params); // works for both GET and POST methods
$response = $request->request($method);

return $response->getBody();
}
}
85 changes: 85 additions & 0 deletions app/code/local/Gamuza/Itaushopline/Model/Cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

class Gamuza_Itaushopline_Model_Cron extends Mage_Core_Helper_Abstract
{
const CHECK_URL = 'https://shopline.itau.com.br/shopline/consulta.aspx';
const PAYMENT_METHOD_CODE = 'itaushopline_standard';

/**
* Query all pending orders and check if its bank slip has been payed
*/
public function checkPayments()
{
$orders = Mage::getModel('sales/order')->getCollection()
->join(
['payment' => 'sales/order_payment'],
'main_table.entity_id = payment.parent_id',
['payment_method' => 'payment.method']
);

$secondsInAWeek = 604800;
$oneWeekAgoTimestamp = time() - $secondsInAWeek;

$orders->addFieldToFilter('payment.method', self::PAYMENT_METHOD_CODE)
->addFieldToFilter('created_at', ['gteq' => Mage::getModel('core/date')->date(null, $oneWeekAgoTimestamp)])
->addFieldToFilter('status', ['in' => ['pending', 'pending_payment']]);

if (!$orders->count()) {
return;
}

foreach ($orders as $order) {
$this->invoiceOrder($order);
}
}

/**
* Check if given order has been payed
*
* @param Mage_Sales_Model_Order $order Order to check payment
*/
private function invoiceOrder(Mage_Sales_Model_Order $order)
{
$helper = Mage::helper('itaushopline');
$orderId = $order->getId();
$queryDc = Mage::getModel('itaushopline/transactions')
->getCollection()
->addFieldToFilter('order_id', $orderId)
->getFirstItem()
->getQueryDc();

$response = Mage::helper('itaushopline/http')->get(self::CHECK_URL, ['DC' => $queryDc]);
$isPayed = strpos($response, 'Pagamento efetuado') !== false;

if (!$isPayed) {
return;
}

if (!$order->canInvoice()) {
$helper->log("Não pôde faturar a ordem $orderId.");
Mage::throwException($this->__('Cannot create an invoice.'));
}

try {
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

if (!$invoice->getTotalQty()) {
Mage::throwException($this->__('Cannot create an invoice without products.'));
}

$invoice->addComment($this->__('Fatura criada automaticamente após pagamento do Boleto Itáu Shopline'));
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();

$transactionSave = Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder());
$transactionSave->save();

$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
$order->sendOrderUpdateEmail(true, $this->__('Pagamento aprovado! Estamos preparando sua entrega.'));

$helper->log("Pedido $orderId faturado.");
} catch (Mage_Core_Exception $e) {
$helper->log("Erro ao faturar pedido $orderId.");
}
}
}
Loading