magento create unpaid invoice programmatically

孤街醉人 提交于 2019-12-13 19:03:17

问题


I'm creating invoices programmatically by an own observer for the sales_order_save_after event.

Unfortunately, the invoice is immediately marked as paid.

How can I achieve that the new invoice is still open and a admin has to set it to paid state?

My code so far:

$invoiceId = Mage::getModel('sales/order_invoice_api')
                    ->create($order->getIncrementId(), array());
$invoice = Mage::getModel('sales/order_invoice')
                    ->loadByIncrementId($invoiceId);
$invoice->capture()->save();

EDIT: To make my comment to urfusion's answer understandable, here the code snippet:

  public function order_placed($observer) {
    $event = $observer->getEvent();

    // ....

    $emailInvoice = false;
    $captureInvoice = false;

    $order = Mage::getModel("sales/order")->load($data['entity_id']);
    if($order->canInvoice() and $order->getIncrementId())
    {
        $invoiceApi = Mage::getModel('sales/order_invoice_api');
        $invoiceId = $invoiceApi->create(
                                $order->getIncrementId(),
                                array(),
                                Mage::Helper('sales')->__('Pending Invoice created!'),
                                $emailInvoice,
                                false);

        if($captureInvoice)  {
            $invoiceApi->capture($invoiceId);
        }
    }
  }

回答1:


mainly it depend on the payment method settings that your invoice will be set as paid or pending.

if the payment method has specified:

check the below code for setting invoice as pending.

protected $_canCapture                  = true;
protected $_canCapturePartial           = true;

$emailInvoice = true;
$captureInvoice = false;

$invoiceApi = Mage::getModel('sales/order_invoice_api');
$invoiceId = $invoiceApi->create(
    $order->getIncrementId(),
    array(),
    Mage::helper('sales')->__('Pending Invoice created!'),
    $emailInvoice,
    false
);

if ($captureInvoice) {
    $invoiceApi->capture($invoiceId);
}



回答2:


After urfusion's answer was not working for me (after I couldn't use the two protected $_canCapture(Partial) = true; lines) I tried to find out more about the two protected variables.

I found them in app/code/core/Mage/Payment/Model/Method/Abstract.php. Since this class is abtract, I couldn't rewrite it globally, which is maybe good, since I would interfere with other payment methods.

For now, I just need the "unpaid invoice creation" for the payment method banktransfer (app/code/core/Mage/Payment/Model/Method/Banktransfer.php) which is extending the abstract class.

What did I do now to solve my problem?

Creating my own module to rewrite the class Mage_Payment_Model_Method_Banktransfer.

In the following, {MY_COMPANY/NAMESPACE} is a placeholder and can be replaced by your company name or some other namespace name in which your module will be placed.

1) Create the folder app/code/local/{MY_COMPANY/NAMESPACE}/Payment/

2) Create subfolder etc/ with file config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <{MY_COMPANY/NAMESPACE}_Payment>
      <version>1.0</version>
    </{MY_COMPANY/NAMESPACE}_Payment>
  </modules>

  <global>
    <models>
      <payment>
        <rewrite>
          <method_banktransfer>{MY_COMPANY/NAMESPACE}_Payment_Model_Method_Banktransfer</method_banktransfer>
        </rewrite>
      </payment>
    </models>
  </global>
</config>

3) Create subfolder Model/Method/ with file Banktransfer.php to just override the two variables and inheritate all the rest of the original class:

class {MY_COMPANY/NAMESPACE}_Payment_Model_Method_Banktransfer extends Mage_Payment_Model_Method_Banktransfer
{
    protected $_canCapture                  = true;
    protected $_canCapturePartial           = true;
}

4) Activate the module by adding {MY_COMPANY/NAMESPACE}_Payment.xml to app/etc/modules/

<?xml version="1.0"?>
<config>
  <modules>
    <{MY_COMPANY/NAMESPACE}_Payment>
      <active>true</active>
      <codePool>local</codePool>
    </{MY_COMPANY/NAMESPACE}_Payment>
  </modules>
</config>


来源:https://stackoverflow.com/questions/33242058/magento-create-unpaid-invoice-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!