Conditions | 11 |
Paths | 385 |
Total Lines | 80 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
78 | public function populate($items, $currencyCode) { |
||
79 | foreach ($items as $idx => $item) { |
||
80 | |||
81 | $cat7Item = Catalogo::getCatItem(7, $item['igvAffectationCode']); // Catálogo 7 Tipo de afectación del IGV |
||
82 | $priceType = $item['priceTypeCode']; // Tipo de precio |
||
83 | |||
84 | $unitValue = $item['unitValue']; // Valor unitario |
||
85 | $igvIncluded = $item['igvIncluded']; |
||
86 | $unitPayableAmount = $unitValue; |
||
87 | if(isset($cat7Item['igv'])){ |
||
88 | // Aplica IGV |
||
89 | if ($igvIncluded) { |
||
90 | $unitPayableAmount = $unitValue; |
||
91 | $unitValue = $unitPayableAmount / (1 + SunatVars::IGV); |
||
92 | } else{ |
||
93 | $unitPayableAmount = $unitValue * (1 + SunatVars::IGV); |
||
94 | } |
||
95 | } |
||
96 | $unitBillableValue = ($priceType === SunatVars::CAT16_REF_VALUE) ? 0 : $unitValue; // Valor unitario pagable |
||
97 | $quantity = $item['quantity']; // Cantidad |
||
98 | |||
99 | $itemValue = $unitValue * $quantity; // Valor de item |
||
100 | $itemBillableValue = $unitBillableValue * $quantity; // Valor de item |
||
101 | // Descuentos de item |
||
102 | $itemAllowancesAmount = 0; |
||
103 | $allowancesArray = isset($item['allowances']) ? $item['allowances'] : []; |
||
104 | foreach ($allowancesArray as $allItem) { |
||
105 | $multFactor = $allItem['multiplierFactor']; |
||
106 | $amount = $itemBillableValue * $multFactor; |
||
107 | $itemAllowancesAmount += $amount; |
||
108 | } |
||
109 | // Cargos de item |
||
110 | $itemChargesAmount = 0; |
||
111 | $chargesArray = isset($item['charges']) ? $item['charges'] : []; |
||
112 | foreach ($chargesArray as $charItem) { |
||
113 | $multFactor = $charItem['multiplierFactor']; |
||
114 | $amount = $itemValue * $multFactor; |
||
115 | $itemChargesAmount += $amount; |
||
116 | } |
||
117 | |||
118 | // Valor de venta del ítem = (Valor del item - Descuentos + Cargos) |
||
119 | if ($priceType === SunatVars::CAT16_UNITARY_PRICE) { |
||
120 | $itemTaxableAmount = $itemValue - $itemAllowancesAmount + $itemChargesAmount; |
||
121 | } else { |
||
122 | // 0 si el valor del item es referencial! |
||
123 | $itemTaxableAmount = 0; |
||
124 | } |
||
125 | |||
126 | // Afectación al IGV por item |
||
127 | $igvAmount = 0; |
||
128 | // Aplica IGV |
||
129 | if (isset($cat7Item['igv'])) { |
||
130 | $igvAmount = $itemTaxableAmount * SunatVars::IGV; |
||
131 | } |
||
132 | |||
133 | $itemIgvTaxed = $itemBillableValue + $igvAmount; |
||
134 | |||
135 | $this->set(self::COL_PRODUCT_CODE, $idx, $item['productCode']); |
||
136 | $this->set(self::COL_UNPSC, $idx, $item['sunatProductCode']); |
||
137 | $this->set(self::COL_UNIT_CODE, $idx, $item['unitCode']); |
||
138 | $this->set(self::COL_QUANTITY, $idx, $quantity); |
||
139 | $this->set(self::COL_DESCRIPTION, $idx, $item['description']); |
||
140 | $this->set(self::COL_CURRENCY_CODE, $idx, $currencyCode); |
||
141 | // Códigos de catálogos predefinidos |
||
142 | $this->set(self::COL_PRICE_TYPE, $idx, $priceType); |
||
143 | $this->set(self::COL_TAX_TYPE, $idx, $item['taxTypeCode']); |
||
144 | $this->set(self::COL_IGV_AFFECTATION, $idx, $item['igvAffectationCode']); |
||
145 | |||
146 | $this->set(self::COL_UNIT_VALUE, $idx, $unitValue); |
||
147 | $this->set(self::COL_UNIT_BILLABLE_VALUE, $idx, $unitBillableValue); |
||
148 | $this->set(self::COL_UNIT_PAYABLE_AMOUNT, $idx, $unitPayableAmount); |
||
149 | $this->set(self::COL_ITEM_VALUE, $idx, $itemValue); |
||
150 | $this->set(self::COL_ITEM_BILLABLE_VALUE, $idx, $itemBillableValue); |
||
151 | $this->set(self::COL_ALLOWANCES, $idx, $allowancesArray); |
||
152 | $this->set(self::COL_ALLOWANCES_AMOUNT, $idx, $itemAllowancesAmount); |
||
153 | $this->set(self::COL_CHARGES, $idx, $chargesArray); |
||
154 | $this->set(self::COL_CHARGES_AMOUNT, $idx, $itemChargesAmount); |
||
155 | $this->set(self::COL_ITEM_TAXABLE_AMOUNT, $idx, $itemTaxableAmount); |
||
156 | $this->set(self::COL_IGV, $idx, $igvAmount); |
||
157 | $this->set(self::COL_ITEM_PAYABLE_AMOUNT, $idx, $itemIgvTaxed); |
||
158 | } |
||
344 |