<?php
$content = json_decode('{"creditor_trans_id":"655354-1","invoices":[{"id":"111333","date_created":"2015-06-02T00:00:00","payment_due":"2015-06-15T00:00:00","currency":"PLN","total":"45000","left_to_pay":"40000"}],"attachments":[{"id":"586a84aa-1b41-11e5-91d6-0cc47a41af63","name":"test324794_256.bmp","size":10002,"thumbnailUrl":null,"type":"image\/x-ms-bmp","expires":"2015-06-26T01:51:57+02:00","_links":{"self":{"href":"http:\/\/api.transinkasso.eu\/api\/rest\/debt-attachment\/586a84aa-1b41-11e5-91d6-0cc47a41af63"}}},{"id":"586b2f8c-1b41-11e5-91d6-0cc47a41af63","name":"tif_test.tif","size":26144,"thumbnailUrl":null,"type":"image\/tiff","expires":"2015-06-26T01:51:57+02:00","_links":{"self":{"href":"http:\/\/api.transinkasso.eu\/api\/rest\/debt-attachment\/586b2f8c-1b41-11e5-91d6-0cc47a41af63"}}},{"id":"586b9033-1b41-11e5-91d6-0cc47a41af63","name":"test324794.bmp","size":26438,"thumbnailUrl":null,"type":"image\/x-ms-bmp","expires":"2015-06-26T01:51:57+02:00","_links":{"self":{"href":"http:\/\/api.transinkasso.eu\/api\/rest\/debt-attachment\/586b9033-1b41-11e5-91d6-0cc47a41af63"}}},{"id":"586d3a17-1b41-11e5-91d6-0cc47a41af63","name":"test324794.png","size":24989,"thumbnailUrl":null,"type":"image\/png","expires":"2015-06-26T01:51:57+02:00","_links":{"self":{"href":"http:\/\/api.transinkasso.eu\/api\/rest\/debt-attachment\/586d3a17-1b41-11e5-91d6-0cc47a41af63"}}},{"id":"5879e073-1b41-11e5-91d6-0cc47a41af63","name":"Rotating_earth_(large).gif","size":316029,"thumbnailUrl":null,"type":"image\/gif","expires":"2015-06-26T01:51:57+02:00","_links":{"self":{"href":"http:\/\/api.transinkasso.eu\/api\/rest\/debt-attachment\/5879e073-1b41-11e5-91d6-0cc47a41af63"}}}],"debtor_company":{"country":"PL","vat_id":"1112223334","name":"RST test12 15 51","email":"test1@truckx.eu","phone":"1234567","city":"Wroc\u0142aw","postal_code":"21-222","address":"chabrowa 4 kilka plikow 5 do 1mb"}} | completed | 2015-06-25 15:52:16 | 2015-06-25 15:52:16 || 41 | {"creditor_trans_id":"655354-1","invoices":[{"id":"1111444","date_created":"2015-06-01T00:00:00","payment_due":"2015-06-22T00:00:00","currency":"PLN","total":"55555","left_to_pay":"9000"},{"id":"2222555","date_created":"2015-06-02T00:00:00","payment_due":"2015-06-23T00:00:00","currency":"PLN","total":"600","left_to_pay":"60"}],"attachments":[],"debtor_company":{"country":"PL","vat_id":"1112223334","name":"RST test12 15 52","email":"test1@truckx.eu","phone":"1234567","city":"Wroclaw","postal_code":"21-222","address":"chabrowa 4 kilka faktur bez plikow 1 waluta"}}', true);
$data = [
'transId' => $content['creditor_trans_id'],
'debtorCompany' => [
'nip' => $content['debtor_company']['vat_id'],
'zipCode' => $content['debtor_company']['postal_code'],
'city' => $content['debtor_company']['city'],
'name' => $content['debtor_company']['name'],
'address' => $content['debtor_company']['address'],
'countryCode' => $content['debtor_company']['country'],
'email' => empty($content['debtor_company']['email']) ? null : $content['debtor_company']['email'],
'phone' => empty($content['debtor_company']['phone']) ? null : preg_replace('/[^0-9]/', '',
$content['debtor_company']['phone']),
],
'invoices' => $content['invoices'],
'attachments' => $content['attachments'],
];
$content = $data;
$content['debts'] = [];
foreach ($content['invoices'] as $invoice) {
$key = false;
if ($content['debts']) {
$key = array_search($invoice['currency'], array_column($content['debts'], 'currency'));
}
if ($key !== false) {
$content['debts'][$key]['total'] = bcadd($content['debts'][$key]['total'], $invoice['total'], 2);
$content['debts'][$key]['invoices'][] = $invoice;
} else {
$content['debts'][] = [
'id' => 0,
'debtReportId' => 0,
'currency' => $invoice['currency'],
'total' => $invoice['total'],
'dateCreated' => null,
'invoices' => [
[
'id' => 0,
'number' => $invoice['id'],
'issueDate' => $invoice['date_created'],
'paymentDate' => $invoice['payment_due'],
'total' => $invoice['total'],
'leftToPay' => $invoice['left_to_pay'],
]
],
];
}
}
unset($content['invoices']);
echo json_encode($content);
preferences:
32.44 ms | 402 KiB | 5 Q