1
|
|
|
<?php |
2
|
|
|
namespace Jh\DataImportMagento\Writer; |
3
|
|
|
|
4
|
|
|
use Ddeboer\DataImport\Exception\WriterException; |
5
|
|
|
use Ddeboer\DataImport\Writer\AbstractWriter; |
6
|
|
|
use Jh\DataImportMagento\Exception\MagentoSaveException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class InvoiceWriter |
10
|
|
|
* @package Jh\DataImportMagento\Writer |
11
|
|
|
* @author Adam Paterson <[email protected]> |
12
|
|
|
* @author Aydin Hassan <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class InvoiceWriter extends AbstractWriter |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Mage_Core_Model_Resource_Transaction |
18
|
|
|
*/ |
19
|
|
|
protected $transactionResourceModel; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Mage_Sales_Model_Order |
23
|
|
|
*/ |
24
|
|
|
protected $orderModel; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \Mage_Core_Model_Resource_Transaction $transactionResourceModel |
28
|
|
|
* @param \Mage_Sales_Model_Order $order |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
\Mage_Core_Model_Resource_Transaction $transactionResourceModel, |
32
|
|
|
\Mage_Sales_Model_Order $order |
33
|
|
|
) { |
34
|
|
|
$this->transactionResourceModel = $transactionResourceModel; |
35
|
|
|
$this->orderModel = $order; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $item |
40
|
|
|
* @return \Ddeboer\DataImport\Writer\WriterInterface|void |
41
|
|
|
* @throws \Ddeboer\DataImport\Exception\WriterException |
42
|
|
|
* @throws \Jh\DataImportMagento\Exception\MagentoSaveException |
43
|
|
|
*/ |
44
|
|
|
public function writeItem(array $item) |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
if (!isset($item['order_id'])) { |
48
|
|
|
throw new WriterException('order_id must be set'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$order = clone $this->orderModel; |
52
|
|
|
$order->loadByIncrementId($item['order_id']); |
53
|
|
|
|
54
|
|
|
if (!$order->getId()) { |
55
|
|
|
throw new WriterException(sprintf('Order with ID: "%s" cannot be found', $item['order_id'])); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/* @var $invoice Mage_Sales_Model_Order_Invoice */ |
59
|
|
|
$invoice = $order->prepareInvoice(); |
60
|
|
|
|
61
|
|
|
if (!$invoice->getData('total_qty')) { |
62
|
|
|
throw new WriterException( |
63
|
|
|
sprintf('Cannot create invoice without products. Order ID: "%s"', $order->getId()) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$invoice->register(); |
69
|
|
|
$invoice->getOrder()->setIsInProcess(true); |
70
|
|
|
|
71
|
|
|
$invoice->setData('request_capture_case', 'offline'); |
72
|
|
|
$transactionSave = clone $this->transactionResourceModel; |
73
|
|
|
$transactionSave |
74
|
|
|
->addObject($invoice) |
75
|
|
|
->addObject($invoice->getOrder()) |
76
|
|
|
->save(); |
77
|
|
|
|
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
throw new MagentoSaveException($e->getMessage()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|