ctucx.git: ctucx.things

simple inventory management web-app

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
<?php
require_once 'Parsedown.php';
require_once 'helpers.php';

class LibraryRenderer {
	public  array       $show;
	public  array       $lists;
	public  array       $categories;
	public  array       $units;

	public  string|bool $chartData;
	public  string      $currencySymbol;
	public  string      $totalUnit;
	public  string      $listName;
	public  string      $listDescription;

	public  int         $listTotalQty;
	public  int         $listTotalWeight;
	public  float       $listTotalWeightDisplay;

	public function __construct (array $library, int $listId) {
		$parsedown   = new Parsedown();

		$categoryIds = array_column($library['categories'], 'id');
		$itemIds     = array_column($library['items'], 'id');
		$listIds     = array_column($library['lists'], 'id');

		$listId      = array_search($listId, $listIds);

		$show        = array_merge($library['optionalFields'], [
			'weight' => ($library['lists'][$listId]['totalWeight'] !== 0),	
		]);

		$this->show                   = $show;
		$this->units                  = renderUnits($library['totalUnit']);

		$this->currencySymbol         = $library['currencySymbol'];
		$this->totalUnit              = $library['totalUnit'];
		$this->listName               = $library['lists'][$listId]['name'];
		$this->listDescription        = $parsedown->text($library['lists'][$listId]['description']);

		$this->listTotalQty           = $library['lists'][$listId]['totalQty'];
		$this->listTotalWeight        = $library['lists'][$listId]['totalWeight'];
		$this->listTotalWeightDisplay = mgToWeight($library['lists'][$listId]['totalWeight'], $library['totalUnit']);


		foreach ( $library['lists'] as $id => $list ) {
			$this->lists[] = [
				'id'     => $list['id'],
				'name'   => $list['name'],
				'active' => ($id == $listId)
			];
		}


		$list      = $library['lists'][$listId];
		$chartData = [
			'points' => [],
			'total'  => 0,
		];		

		foreach ( $list['categoryIds'] as $id ) {
			$categoryId  = array_search($id, $categoryIds);

			$points      = [];

			$category                          = $library['categories'][$categoryId];
			$category['items']                 = [];
			$category['subtotalWeightDisplay'] = mgToWeight($category['subtotalWeight'], $library['totalUnit']);

			foreach ( $category['categoryItems'] as $item ) {
				$itemId = array_search($item['itemId'], $itemIds);
				$item   = array_merge($item, $library['items'][$itemId]);

				$item['displayWeight'] = mgToWeight($item['weight'], $item['authorUnit']);
				$item['units']         = renderUnits($item['authorUnit']);

				$itemWeight = ($item['weight'] * $item['qty']);
				$itemName   = $item['name'].': '.mgToWeight($item['weight'], $item['authorUnit']).' '.$item['authorUnit'];

				$category['items'][] = $item;

				// calculate chartData
				if ( $itemWeight == 0 ) continue;
				if ( $item['qty'] > 1 ) $itemName .= ' x '.$item['qty'];

				$points[] = [
					'id'      => $item['id'],
					'value'   => $itemWeight,
					'name'    => $itemName,
					'percent' => ($itemWeight / $category['subtotalWeight']),
				];
			}

			$this->categories[]    = $category;

			//calculate chartData
			if ( $list['totalWeight'] == 0 ) continue;
			$chartData['total']   += $category['subtotalWeight'];
			$chartData['points'][] = [
				'id'            => $category['id'],
				'name'          => $category['name'].': '.mgToWeight($category['subtotalWeight'], $library['totalUnit']).' '.$library['totalUnit'],
				'color'         => $category['color'],
				'total'         => $category['subtotalWeight'],
				'points'        => $points,
				'visiblePoints' => false,
			];
		}

		//calculate chartData
		if ( $chartData['total'] !== 0 ) {
			foreach ( $chartData['points'] as $id => $point ) {
				$chartData['points'][$id]['percent'] = ($point['total'] / $chartData['total']);
			}

			$this->chartData = rawurlencode(json_encode($chartData));
		} else {
			$this->chartData = false;
		}
	}

}