-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoiceExporter.php
More file actions
81 lines (69 loc) · 1.94 KB
/
InvoiceExporter.php
File metadata and controls
81 lines (69 loc) · 1.94 KB
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
<?php
namespace Drupal\art_invoice\Export;
use Drupal\art_invoice\Entity\Invoice;
use Drupal\art_invoice\InvoiceStatus;
use Drupal\Core\Config\ConfigImporterException;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Config\StorageComparerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Defines a invoice exporter.
*/
class InvoiceExporter {
use DependencySerializationTrait;
/**
* Database storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $storage;
/**
* InvoiceExporter constructor.
*
* @param \Drupal\Core\Config\StorageInterface $storage
* Database storage.
*/
public function __construct(StorageInterface $storage) {
$this->storage = $storage;
}
/**
* Store invoice in database for export.
*
* @param \Drupal\art_invoice\Entity\Invoice $invoice
* The invoice entity.
*/
public function addInvoice(Invoice $invoice) {
$this->storage->write($invoice->label(), $this->mapInvoice($invoice));
}
/**
* Map fields.
*
* @param Invoice $invoice
* Invoice entity.
*
* @return array
* Mapped fields.
*/
private function mapInvoice(Invoice $invoice) {
$entity_data = [];
if (empty($invoice->label())) {
return [];
}
$mapping = [
'Sale ID' => 'field_invoice_sale_number',
'Invoice Reference' => 'name',
'Invoice number' => 'field_invoice_invoice_number',
'Amount' => 'amount',
'Bank transaction ID' => 'field_invoice_transaction_id',
];
foreach ($mapping as $name => $field_name) {
$entity_data[$name] = $invoice->get($field_name)->value ?: NULL;
}
return $entity_data;
}
}