/
Invoice.php
188 lines (161 loc) · 5.76 KB
/
Invoice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Xigen\AutoInvoice\Console\Command;
use Magento\Sales\Model\Service\InvoiceService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Invoice console command.
*/
class Invoice extends Command
{
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* @var \Magento\Sales\Model\OrderRepository
*/
protected $orderRepository;
/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
/**
* @var \Magento\Framework\App\State
*/
protected $state;
/**
* @var \Magento\Sales\Model\Order\ShipmentFactory
*/
protected $shipmentFactory;
/**
* @var \Magento\Framework\DB\TransactionFactory
*/
protected $transactionFactory;
/**
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
protected $orderRepositoryInterface;
/**
* @var Symfony\Component\Console\Output\OutputInterface
*/
protected $output;
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Sales\Model\OrderRepository $orderRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Framework\App\State $state,
\Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory,
\Magento\Framework\DB\TransactionFactory $transactionFactory,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepositoryInterface
) {
$this->logger = $logger;
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->state = $state;
$this->shipmentFactory = $shipmentFactory;
$this->transactionFactory = $transactionFactory;
$this->orderRepositoryInterface = $orderRepositoryInterface;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('xigen:invoice:order')
->setDescription('Invoice order within magento')
->addOption(
'orderid',
'o',
InputOption::VALUE_REQUIRED,
'Order ID',
false
);
parent::configure();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
try {
$this->output = $output;
$orderid = $input->getOption('orderid');
$this->output->writeln((string) __('%1 Processing order <info>%2<info>', date('Y-m-d H:i:s'), $orderid));
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('increment_id', $orderid, 'eq')
->create();
$order = $this->orderRepository
->getList($searchCriteria)
->getFirstItem();
if ($order && $order->getId()) {
$this->output->writeln((string) __('[%1] Processing order <info>%2 : %3<info>', date('Y-m-d H:i:s'), $order->getId(), $order->getIncrementId()));
$this->createInvoice($order->getId());
}
} catch (\Exception $e) {
$this->logger->debug($e->getMessage());
$this->output->writeln((string) __('<error>Error:</error> Unable to load order %1', $e->getMessage()));
return;
}
}
/**
* Create invoice by entity_id.
*
* @param int $orderId
*
* @return void
*/
public function createInvoice($orderId)
{
try {
$order = $this->getById($orderId);
if (!$order || !$order->getId() || !$order->canInvoice() || !$order->getPayment()) {
$this->output->writeln((string) __('[%1] Cannot invoice <info>%2<info>', date('Y-m-d H:i:s'), $orderId));
return;
}
$paymentMethod = $order->getPayment()->getMethod();
if (!$paymentMethod) {
return;
}
$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->invoiceService = $this->_objectManager->create(InvoiceService::class);
$invoice = $this->invoiceService->prepareInvoice($order);
if (!$invoice || !$invoice->getTotalQty()) {
return;
}
$invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);
$order->addStatusHistoryComment('Console INVOICED', false);
$transactionSave = $this->transactionFactory
->create()
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
$this->output->writeln((string) __('[%1] Order Invoiced order <info>%2 : %3<info>', date('Y-m-d H:i:s'), $order->getId(), $order->getIncrementId()));
} catch (\Exception $e) {
$this->logger->debug($e->getMessage());
$this->output->writeln((string) __('[%1] <error>Error:</error> Unable to load order %2', date('Y-m-d H:i:s'), $e->getMessage()));
}
}
/**
* Get order by Id.
*
* @param int $orderId
*
* @return \Magento\Sales\Model\Data\Order
*/
public function getById($orderId)
{
try {
return $this->orderRepositoryInterface->get($orderId);
} catch (\Exception $e) {
return false;
}
}
}