invoiceCopy()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
c 0
b 0
f 0
dl 0
loc 36
rs 9.2088
cc 5
nc 8
nop 2
1
#!/usr/bin/php -f
2
<?php
3
/**
4
 * FlexiPeeHP - Example how to copy Invoice
5
 *
6
 * @author     Vítězslav Dvořák <[email protected]>
7
 * @copyright  (G) 2017 Vitex Software
8
 */
9
10
namespace Example\FlexiPeeHP;
11
12
include_once './config.php';
13
include_once '../vendor/autoload.php';
14
15
/**
16
 * Copy an invoice to new one
17
 *
18
 * @param \FlexiPeeHP\FakturaVydana $invoice
19
 * @param array                     $extraValues Extra values for invoice Copy
20
 *
21
 * @return \FlexiPeeHP\FakturaVydana
22
 */
23
function invoiceCopy(\FlexiPeeHP\FakturaVydana $invoice, $extraValues = [])
24
{
25
    $invoice2 = new \FlexiPeeHP\FakturaVydana(array_merge(
26
            $extraValues, $invoice->getData()));
27
    $invoice2->unsetDataValue('id');
28
    $invoice2->unsetDataValue('kod');
29
    if ($invoice2->getDataValue('stavUhrK') != 'stavUhr.uhrazenoRucne') {
30
        $invoice2->unsetDataValue('stavUhrK');
31
    }
32
    $invoiceItems = $invoice2->getDataValue('polozkyFaktury');
33
    if (count($invoiceItems)) {
34
        foreach ($invoiceItems as $pid => $invoiceItem) {
35
            unset($invoiceItems[$pid]['id']);
36
            unset($invoiceItems[$pid]['doklFak']);
37
            unset($invoiceItems[$pid]['doklFak@showAs']);
38
            unset($invoiceItems[$pid]['doklFak@ref']);
39
        }
40
        $invoice2->setDataValue('polozkyFaktury', $invoiceItems);
41
42
//   $invoice2->addArrayToBranch($invoiceItems, 'polozkyFaktury');
43
    }
44
    $invoice2->setDataValue('typDokl', 'code:FAKTURA');
45
46
    $invoice2->unsetDataValue('external-ids');
47
48
    $today = date('Y-m-d');
49
50
    $invoice2->setDataValue('duzpPuv', $today);
51
    $invoice2->setDataValue('duzpUcto', $today);
52
    $invoice2->setDataValue('datUcto', $today);
53
    $invoice2->insertToFlexiBee();
0 ignored issues
show
Deprecated Code introduced by
The function FlexiPeeHP\RW::insertToFlexiBee() has been deprecated: since version 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

53
    /** @scrutinizer ignore-deprecated */ $invoice2->insertToFlexiBee();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
54
    if ($invoice2->lastResponseCode == 201) {
55
        $invoice->addStatusMessage(sprintf(_('Invoice copy from %s to id:%s done'),
56
                (string) $invoice, (string) $invoice2), 'success');
57
    }
58
    return $invoice2;
59
}
60
include_once './common.php';
61
62
$invoiceID = askForFlexiBeeID();
63
64
/*
65
 * FlexiPeeHP Classes accept this form of initial identifier:
66
 *
67
 * (int) 2588
68
 * (string) ext:ESHOP:oi1978
69
 * (array) ['varSym'=>'20080015']
70
 */
71
72
$originalInvoice = new \FlexiPeeHP\FakturaVydana($invoiceID);
73
if ($originalInvoice->getMyKey()) {
74
    $invoiceCopy = invoiceCopy($originalInvoice,
75
        ['poznam' => 'FlexiPeeHP php library example - this is copy of '.$invoiceID]);
76
77
    if ($invoiceCopy->lastResponseCode == 201) {
78
        echo $invoiceCopy->getEvidenceURL().'/'.(string) $invoiceCopy;
79
    }
80
} else {
81
    $originalInvoice->addStatusMessage('Source Invoice does not exists');
82
}
83