Passed
Push — developer ( 6b5868...bed0f9 )
by Radosław
22:42 queued 03:39
created

TableCorrectTaxSummary::process()   C

Complexity

Conditions 17
Paths 28

Size

Total Lines 63
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
eloc 41
c 0
b 0
f 0
dl 0
loc 63
ccs 0
cts 36
cp 0
rs 5.2166
cc 17
nc 28
nop 0
crap 306

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 6.5 (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
				$taxAmount = $relatedTaxAmount = 0;
72
				$html .= '
73
						<table class="table-correct-tax-summary" style="width:100%;vertical-align:top;border-collapse:collapse;border:1px solid #ddd;">
74
						<thead>
75
								<tr>
76
									<th colspan="2" style="font-weight:bold;padding:0px 4px;">' . \App\Language::translate('LBL_TAX_CORRECT_SUMMARY', $this->textParser->moduleName) . '</th>
77
								</tr>
78
								</thead><tbody>';
79
80
				foreach ($taxes as $tax) {
81
					$taxAmount += $tax;
82
				}
83
				foreach ($relatedTaxes as $tax) {
84
					$relatedTaxAmount += $tax;
85
				}
86
				$html .= '<tr>
87
									<td class="name" style="text-align:left;font-weight:bold;padding:0px 4px;">' . \App\Language::translate('LBL_AMOUNT', $this->textParser->moduleName) . '</td>
88
									<td class="value" style="text-align:right;font-weight:bold;padding:0px 4px;">' . \CurrencyField::convertToUserFormat($relatedTaxAmount - $taxAmount, 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

88
									<td class="value" style="text-align:right;font-weight:bold;padding:0px 4px;">' . /** @scrutinizer ignore-deprecated */ \CurrencyField::convertToUserFormat($relatedTaxAmount - $taxAmount, 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...
89
								</tr>
90
								</tbody>
91
						</table>';
92
			}
93
		}
94
		return $html;
95
	}
96
}
97