magento create unpaid invoice programmatically

2019-08-07 11:06发布

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);
        }
    }
  }

2条回答
劫难
2楼-- · 2019-08-07 11:51

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>
查看更多
乱世女痞
3楼-- · 2019-08-07 11:58

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);
}
查看更多
登录 后发表回答