Passed
Push — developer ( 2408dd...d29d97 )
by Radosław
15:11
created

TableCorrectTaxSummary::process()   D

Complexity

Conditions 18
Paths 40

Size

Total Lines 65
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 65
ccs 0
cts 36
cp 0
rs 4.8666
c 0
b 0
f 0
cc 18
nc 40
nop 0
crap 342

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\TextParser;
4
5
/**
6
 * Table tax summary class fo correct.
7
 *
8
 * @package TextParser
9
 *
10
 * @copyright YetiForce S.A.
11
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
12
 * @author    Tomasz Kur <[email protected]>
13
 */
14
class TableCorrectTaxSummary extends Base
15
{
16
	/** @var string Class name */
17
	public $name = 'LBL_TABLE_TAX_SUMMARY_FOR_CORRECT';
18
19
	/** @var mixed Parser type */
20
	public $type = 'pdf';
21
22
	/** @var array Allowed modules */
23
	public $allowedModules = ['FCorectingInvoice'];
24
	/** @var array Related modules fields */
25
	protected $relatedModulesFields = ['FCorectingInvoice' => 'finvoiceid'];
26
27
	/**
28
	 * Process.
29
	 *
30
	 * @return string
31
	 */
32
	public function process()
33
	{
34
		if (!$this->textParser->recordModel || !$this->textParser->recordModel->getModule()->isInventory()) {
35
			return '';
36
		}
37
		$relatedRecordModel = \Vtiger_Record_Model::getInstanceById($this->textParser->recordModel->get($this->relatedModulesFields[$this->textParser->recordModel->getModuleName()]));
38
		$relatedInventoryRows = $relatedRecordModel->getInventoryData();
39
		$relatedInventory = \Vtiger_Inventory_Model::getInstance($relatedRecordModel->getModuleName());
40
		$html = '';
41
		$inventory = \Vtiger_Inventory_Model::getInstance($this->textParser->moduleName);
42
		$fields = $inventory->getFieldsByBlocks();
43
		$baseCurrency = \Vtiger_Util_Helper::getBaseCurrency();
44
		$inventoryRows = $this->textParser->recordModel->getInventoryData();
45
		$firstRow = current($inventoryRows);
46
		if ($inventory->isField('currency')) {
47
			if (!empty($firstRow) && null !== $firstRow['currency']) {
48
				$currency = $firstRow['currency'];
49
			} else {
50
				$currency = $baseCurrency['id'];
51
			}
52
			$currencySymbol = \App\Fields\Currency::getById($currency)['currency_symbol'];
53
		} else {
54
			$currencySymbol = \App\Fields\Currency::getDefault()['currency_symbol'];
55
		}
56
		if (!empty($fields[0])) {
57
			$taxes = $relatedTaxes = [];
58
			if ($inventory->isField('tax') && $inventory->isField('net')) {
59
				$taxField = $inventory->getField('tax');
60
				foreach ($inventoryRows as $key => $inventoryRow) {
61
					$taxes = $taxField->getTaxParam($inventoryRow['taxparam'], $inventoryRow['net'], $taxes);
0 ignored issues
show
Bug introduced by
The method getTaxParam() does not exist on Vtiger_Basic_InventoryField. It seems like you code against a sub-type of Vtiger_Basic_InventoryField such as Vtiger_Tax_InventoryField. ( Ignorable by Annotation )

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

61
					/** @scrutinizer ignore-call */ 
62
     $taxes = $taxField->getTaxParam($inventoryRow['taxparam'], $inventoryRow['net'], $taxes);
Loading history...
62
				}
63
			}
64
			if ($relatedInventory->isField('tax') && $relatedInventory->isField('net')) {
65
				$taxField = $relatedInventory->getField('tax');
66
				foreach ($relatedInventoryRows as $key => $inventoryRow) {
67
					$relatedTaxes = $taxField->getTaxParam($inventoryRow['taxparam'], $inventoryRow['net'], $relatedTaxes);
68
				}
69
			}
70
			if ($inventory->isField('tax') && $inventory->isField('taxmode')) {
71
				$correctionTaxAmount = $invoiceTaxAmount = $taxValue = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $taxValue is dead and can be removed.
Loading history...
72
				foreach ($taxes as $tax) {
73
					$correctionTaxAmount += $tax;
74
				}
75
				foreach ($relatedTaxes as $tax) {
76
					$invoiceTaxAmount += $tax;
77
				}
78
				$taxValue = $correctionTaxAmount - $invoiceTaxAmount;
79
				$taxLabel = $taxValue > 0 ? 'LBL_VAT_INCREASE_AMOUNT' : 'LBL_VAT_REDUCTION_AMOUNT';
80
				$html .= '
81
						<table class="table-correct-tax-summary" style="width:100%;vertical-align:top;border-collapse:collapse;border:1px solid #ddd;">
82
						<thead>
83
								<tr>
84
									<th colspan="2" style="font-weight:bold;padding:0px 4px;">' . \App\Language::translate($taxLabel, $this->textParser->moduleName) . '</th>
85
								</tr>
86
								</thead><tbody>';
87
88
				$html .= '<tr>
89
									<td class="name" style="text-align:left;font-weight:bold;padding:0px 4px;">' . \App\Language::translate('LBL_AMOUNT', $this->textParser->moduleName) . '</td>
90
									<td class="value" style="text-align:right;font-weight:bold;padding:0px 4px;">' . \CurrencyField::convertToUserFormat($taxValue, null, true) . ' ' . $currencySymbol . '</td>
0 ignored issues
show
Deprecated Code introduced by
The function CurrencyField::convertToUserFormat() has been deprecated: Recommend using function \App\Fields\Currency::formatToDisplay ( Ignorable by Annotation )

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

90
									<td class="value" style="text-align:right;font-weight:bold;padding:0px 4px;">' . /** @scrutinizer ignore-deprecated */ \CurrencyField::convertToUserFormat($taxValue, null, true) . ' ' . $currencySymbol . '</td>

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...
91
								</tr>
92
								</tbody>
93
						</table>';
94
			}
95
		}
96
		return $html;
97
	}
98
}
99