Completed
Push — master ( 596467...bf19e6 )
by Vítězslav
11:06
created

FakturaVydanaTest::testodpocetZalohy()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 8.8571
cc 1
eloc 29
nc 1
nop 0
1
<?php
2
3
namespace Test\FlexiPeeHP;
4
5
use FlexiPeeHP\FakturaVydana;
6
7
/**
8
 * Generated by PHPUnit_SkeletonGenerator on 2016-04-27 at 17:32:11.
9
 */
10
class FakturaVydanaTest extends FlexiBeeRWTest
11
{
12
    /**
13
     * @var FakturaVydana
14
     */
15
    protected $object;
16
17
    /**
18
     * Sets up the fixture, for example, opens a network connection.
19
     * This method is called before a test is executed.
20
     */
21
    protected function setUp()
22
    {
23
        $this->object = new FakturaVydana();
24
    }
25
26
    /**
27
     * Tears down the fixture, for example, closes a network connection.
28
     * This method is called after a test is executed.
29
     */
30
    protected function tearDown()
31
    {
32
33
    }
34
35
    /**
36
     * @covers FlexiPeeHP\FakturaVydana::hotovostniUhrada
37
     */
38
    public function testhotovostniUhrada()
39
    {
40
        $this->makeInvoice();
41
        $this->object->unsetDataValue('kod');
42
        $this->object->hotovostniUhrada($this->object->getDataValue('sumCelkZakl'));
43
        $this->assertEquals(201, $this->object->lastResponseCode,
44
            _('Invoice settle error'));
45
    }
46
47
    /**
48
     * @covers FlexiPeeHP\FakturaVydana::sparujPlatbu
49
     */
50
    public function testsparujPlatbu()
51
    {
52
        $this->makeInvoice();
53
        $doklad     = new \FlexiPeeHP\PokladniPohyb();
54
        $value      = $this->object->getDataValue('sumCelkZakl');
55
        $dataPohybu = [
56
            'kod' => 'FP'.time(),
57
            'typDokl' => 'code:STANDARD',
58
            'typPohybuK' => 'typPohybu.prijem',
59
            'datVyst' => date("Y-m-d", time() - 60 * 60 * 24),
60
            'jakUhrK' => 'jakUhrazeno.rucne1',
61
            'pokladna' => 'code:POKLADNA KČ',
62
            'generovatSkl' => false,
63
            'zdrojProSkl' => false,
64
            'firma' => $this->object->getDataValue('firma'),
65
            'bezPolozek' => true,
66
            'poznam' => $this->poznam,
67
            'primUcet' => 'code:013001',
68
            'sumZklCelkem' => $value
69
        ];
70
        $doklad->takeData($dataPohybu);
71
        $doklad->insertToFlexiBee();
72
        $doklad->unsetDataValue('kod');
73
        $this->object->sparujPlatbu($doklad);
74
        $this->assertEquals(201, $doklad->lastResponseCode,
75
            _('Invoice match error'));
76
    }
77
78
    /**
79
     * Crerate testing invoice
80
     * 
81
     * @param array $invoiceData
82
     */
83
    public function makeInvoice($invoiceData = [])
84
    {
85
        if (!isset($invoiceData['kod'])) {
86
            $invoiceData['kod'] = 'PeeHP'.time();
87
        }
88
        if (!isset($invoiceData['varSym'])) {
89
            $invoiceData['varSym'] = \Ease\Sand::randomNumber(1000, 99999);
90
        }
91
        if (!isset($invoiceData['datVyst'])) {
92
            $invoiceData['datVyst'] = date("Y-m-d", time() - 60 * 60 * 24);
93
        }
94
        if (!isset($invoiceData['typDokl'])) {
95
            $invoiceData['typDokl'] = 'code:FAKTURA';
96
        }
97
        if (!isset($invoiceData['zdrojProSkl'])) {
98
            $invoiceData['zdrojProSkl'] = false;
99
        }
100
        if (!isset($invoiceData['dobropisovano'])) {
101
            $invoiceData['dobropisovano'] = false;
102
        }
103
        if (!isset($invoiceData['bezPolozek'])) {
104
            $invoiceData['bezPolozek'] = false;
105
        }
106
107
        if (!isset($invoiceData['polozky']) && !$invoiceData['bezPolozek']) {
108
            $invoiceData['bezPolozek'] = true;
109
            if (!array_key_exists('sumCelkZakl', $invoiceData)) {
110
                $scale                      = pow(1000, 2);
111
                $invoiceData['sumCelkZakl'] = round(mt_rand(10 * $scale,
112
                        9000 * $scale) / $scale, 2);
113
                $invoiceData['castkaMen']   = 0;
114
                $invoiceData['sumCelkem']   = $invoiceData['sumCelkZakl'];
115
            }
116
        } else {
117
            $invoiceData['bezPolozek'] = false;
118
        }
119
120
        if (!isset($invoiceData['firma'])) {
121
            $adresar = new \FlexiPeeHP\Adresar();
122
123
            $adresy = $adresar->getFlexiData(null,
124
                ['typVztahuK' => 'typVztahu.odberatel']);
125
            if (count($adresy)) {
126
                $dodavatel = $adresy[array_rand($adresy)];
127
128
                $invoiceData['firma'] = 'code:'.$dodavatel['kod'];
129
            } else {
130
                /**
131
                 * Make Some Address First ...
132
                 */
133
                $address              = new \FlexiPeeHP\Adresar();
134
                $address->setDataValue('nazev', \Ease\Sand::randomString());
135
                $address->setDataValue('poznam', 'Generated Unit Test Customer');
136
                $address->setDataValue('typVztahuK', 'typVztahu.odberatel');
137
                $address->insertToFlexiBee();
138
                $invoiceData['firma'] = $address;
139
            }
140
        }
141
142
        if (!isset($invoiceData['poznam'])) {
143
            $invoiceData['poznam'] = $this->poznam;
144
        }
145
146
        $this->object->takeData($invoiceData);
147
        $this->object->insertToFlexiBee();
148
149
        $id = $this->object->getLastInsertedId();
150
        $this->object->loadFromFlexiBee((int) $id);
151
        $this->object->setDataValue('id', $id);
152
        return $id;
153
    }
154
155
    /**
156
     * Provizorní zkopírování faktury
157
     *
158
     * @link https://www.flexibee.eu/podpora/Tickets/Ticket/View/28848 Chyba při Provádění akcí přes REST API JSON
159
     * @param \FlexiPeeHP\FakturaVydana $invoice
160
     * @param array $overide Hodnoty přepisující výchozí v kopii faktury
0 ignored issues
show
Documentation introduced by
There is no parameter named $overide. Did you maybe mean $override?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
161
     * @return \FlexiPeeHP\FakturaVydana
162
     */
163
    function invoiceCopy($invoice, $override = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
164
    {
165
        $invoice2        = new \FlexiPeeHP\FakturaVydana($invoice->getData());
166
        $invoice2->debug = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $debug was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
167
        $invoice2->unsetDataValue('id');
168
        $invoice2->unsetDataValue('kod');
169
        $polozky         = $invoice2->getDataValue('polozkyFaktury');
170
        if (!is_null($polozky)) {
171
            foreach ($polozky as $pid => $polozka) {
0 ignored issues
show
Bug introduced by
The expression $polozky of type object|integer|double|string|array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
172
                unset($polozky[$pid]['id']);
173
                unset($polozky[$pid]['doklFak']);
174
                unset($polozky[$pid]['doklFak@showAs']);
175
                unset($polozky[$pid]['doklFak@ref']);
176
            }
177
            $invoice2->setDataValue('polozkyFaktury', $polozky);
178
        }
179
        if (is_null($invoice2->getDataValue('typDokl'))) {
180
            $invoice2->setDataValue('typDokl', 'code:FAKTURA');
181
        }
182
        $invoice2->unsetDataValue('external-ids');
183
184
        $today = date('Y-m-d');
185
186
        $invoice2->setDataValue('duzpPuv', $today);
187
        $invoice2->setDataValue('duzpUcto', $today);
188
        $invoice2->setDataValue('datUcto', $today);
189
        $invoice2->takeData($override);
190
        $invoice2->insertToFlexiBee();
191
192
        return $invoice2;
193
    }
194
195
    /**
196
     * @covers FlexiPeeHP\FakturaVydana::odpocetZDD
197
     */
198
    public function testodpocetZDD()
199
    {
200
        $this->markTestIncomplete('TODO: Write Test');
201
    }
202
203
    /**
204
     * @covers FlexiPeeHP\FakturaVydana::odpocetZalohy
205
     */
206
    public function testodpocetZalohy()
207
    {
208
        $itemName = \Ease\Sand::randomString();
209
210
        $polozka = [
211
            "typCenyDphK" => "typCeny.bezDph",
212
            "typSzbDphK" => "typSzbDph.dphZakl",
213
            "kopClenKonVykDph" => "true",
214
            "typPolozkyK" => "typPolozky.obecny",
215
            'zdrojProSkl' => false,
216
            'zaloha' => true,
217
            'nazev' => $itemName,
218
            'szbDph' => 19.0,
219
            'cenaMj' => 123,
220
            "mnozMj" => "1.0",
221
            'poznam' => $this->poznam,
222
        ];
223
224
        $this->makeInvoice(
225
            [
226
                'typDokl' => 'code:ZÁLOHA',
227
                'polozky' => $polozka,
228
                'bezPolozek' => false
229
            ]
230
        );
231
232
233
        $this->object->hotovostniUhrada($this->object->getDataValue('sumCelkem'));
234
235
        $invoice2 = $this->invoiceCopy($this->object,
236
            ['typDokl' => 'code:FAKTURA']);
237
        $id       = (int) $invoice2->getLastInsertedId();
238
        $invoice2->loadFromFlexiBee($id);
239
        $kod      = $invoice2->getDataValue('kod');
240
        $invoice2->dataReset();
241
        $invoice2->setDataValue('id', 'code:'.$kod);
242
243
        $result = $invoice2->odpocetZalohy($this->object);
244
245
        $this->assertArrayHasKey('success', $result);
246
        $this->assertEquals('true', $result['success'], 'Matching Error');
247
    }
248
249
}
250