CommercialDocument::parseData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 41
nc 2
nop 1
dl 0
loc 46
rs 9.264
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * MÓDULO DE EMISIÓN ELECTRÓNICA F72X
5
 * UBL 2.1
6
 * Version 1.0
7
 * 
8
 * Copyright 2019, Jaime Cruz
9
 */
10
11
namespace F72X\Sunat;
12
13
use DateTime;
14
use F72X\Sunat\Operations;
15
use F72X\Company;
16
17
class CommercialDocument {
18
19
    private $currencyCode;
20
    private $documentType;
21
    private $documentNumber;
22
    private $documentId;
23
    private $documentFileName;
24
    private $issueDate;
25
    private $referenceDate;
26
    private $companyDocType;
27
    private $companyRUC;
28
    private $companyName;
29
    private $items;
30
    private $rawData;
31
    private $parsedData;
32
33
    public function __construct($type, $rawData) {
34
        $parsedData = $this->parseData($rawData);
35
        $this->currencyCode = $parsedData['currencyCode'];
36
        $this->documentType = $type;
37
        $this->documentNumber = str_pad($rawData['documentNumber'], 5, '0', STR_PAD_LEFT);
38
        $this->issueDate = new DateTime($parsedData['issueDate']);
39
        $this->referenceDate = new DateTime($parsedData['referenceDate']);
40
        $this->documentId = $this->documentType.'-' . $this->issueDate->format('Ymd') . '-' . $this->documentNumber;
41
        $this->documentFileName = Company::getRUC() . '-' . $this->documentId;
42
        $this->rawData = $rawData;
43
        $this->parsedData = $parsedData;
44
        $this->companyDocType = '6'; // RUC
45
        $this->companyRUC = Company::getRUC();
46
        $this->companyName = Company::getCompanyName();
47
        $this->items = $parsedData['items'];
48
    }
49
    public function getCurrencyCode() {
50
        return $this->currencyCode;
51
    }
52
53
    public function getDocumentType() {
54
        return $this->documentType;
55
    }
56
57
    public function getDocumentNumber() {
58
        return $this->documentNumber;
59
    }
60
61
    public function getDocumentId() {
62
        return $this->documentId;
63
    }
64
65
    public function getDocumentFileName() {
66
        return $this->documentFileName;
67
    }
68
69
    public function getIssueDate() {
70
        return $this->issueDate;
71
    }
72
73
    public function getReferenceDate() {
74
        return $this->referenceDate;
75
    }
76
77
    public function getCompanyRUC() {
78
        return $this->companyRUC;
79
    }
80
81
    public function getCompanyName() {
82
        return $this->companyName;
83
    }
84
85
    public function getItems() {
86
        return $this->items;
87
    }
88
89
    public function getRawData() {
90
        return $this->rawData;
91
    }
92
93
    public function getParsedData() {
94
        return $this->parsedData;
95
    }
96
97
    public function setCurrencyCode($currencyCode) {
98
        $this->currencyCode = $currencyCode;
99
        return $this;
100
    }
101
102
    public function setDocumentType($documentType) {
103
        $this->documentType = $documentType;
104
        return $this;
105
    }
106
107
    public function setDocumentNumber($documentNumber) {
108
        $this->documentNumber = $documentNumber;
109
        return $this;
110
    }
111
112
    public function setDocumentId($documentId) {
113
        $this->documentId = $documentId;
114
        return $this;
115
    }
116
117
    public function setIssueDate($issueDate) {
118
        $this->issueDate = $issueDate;
119
        return $this;
120
    }
121
122
    public function setReferenceDate($referenceDate) {
123
        $this->referenceDate = $referenceDate;
124
        return $this;
125
    }
126
127
    public function setCompanyRUC($companyRUC) {
128
        $this->companyRUC = $companyRUC;
129
        return $this;
130
    }
131
132
    public function setCompanyName($companyName) {
133
        $this->companyName = $companyName;
134
        return $this;
135
    }
136
    
137
    public function getDataForXml() {
138
        return [
139
            'documentId'    => $this->documentId,
140
            'currencyCode'  => $this->currencyCode,
141
            'issueDate'     => $this->issueDate->format('Y-m-d'),
142
            'referenceDate' => $this->referenceDate->format('Y-m-d'),
143
            'companyRUC'    => $this->companyRUC,
144
            'companyName'   => $this->companyName,
145
            'items'         => $this->items
146
        ];
147
    }
148
149
    private function parseData($rawData) {
150
        $headData = [
151
            'companyDocType' => 6, // RUC
152
            'companyRUC'     => Company::getRUC(),
153
            'companyName'    => Company::getCompanyName()
154
        ];
155
        $parsedData = array_merge($rawData, $headData);
156
        $itemDefaults = [
157
            'taxableOperations' => 0,
158
            'exemptedOperations' => 0,
159
            'unaffectedOperations' => 0,
160
            'freeOperations' => 0,
161
            'totalCharges' => 0,
162
            'totalIsc' => 0,
163
            'totalIgv' => 0,
164
            'totalOtherTaxes' => 0,
165
            'affectedDocType' => null,
166
            'affectedDocId' => null,
167
            'perceptionRegimeType' => null,
168
            'perceptionPercentage' => null,
169
            'perceptionBaseAmount' => null,
170
            'perceptionAmount' => null,
171
            'perceptionIncludedAmount' => null
172
        ];
173
        $parsedItems = [];
174
        foreach ($rawData['items'] as $item) {
175
            $item = array_merge($itemDefaults, $item);
176
            $item2 = [
177
                'payableAmount'        => Operations::formatAmount($item['payableAmount']),
178
                'taxableOperations'    => Operations::formatAmount($item['taxableOperations']),
179
                'exemptedOperations'   => Operations::formatAmount($item['exemptedOperations']),
180
                'unaffectedOperations' => Operations::formatAmount($item['unaffectedOperations']),
181
                'freeOperations'       => Operations::formatAmount($item['freeOperations']),
182
                'totalCharges'         => Operations::formatAmount($item['totalCharges']),
183
                'totalIsc'             => Operations::formatAmount($item['totalIsc']),
184
                'totalIgv'             => Operations::formatAmount($item['totalIgv']),
185
                'totalOtherTaxes'      => Operations::formatAmount($item['totalOtherTaxes']),
186
                'perceptionPercentage' => Operations::formatAmount($item['perceptionPercentage']),
187
                'perceptionBaseAmount' => Operations::formatAmount($item['perceptionBaseAmount']),
188
                'perceptionAmount'     => Operations::formatAmount($item['perceptionAmount']),
189
                'perceptionIncludedAmount'  => Operations::formatAmount($item['perceptionIncludedAmount']),
190
            ];
191
            $parsedItems[] = array_merge($item, $item2);
192
        }
193
        $parsedData['items'] = $parsedItems;
194
        return $parsedData;
195
    }
196
197
}
198