20
20
* You should have received a copy of the GNU Affero General Public License
21
21
* along with this program. If not, see <https://www.gnu.org/licenses/>.
22
22
*/
23
+
23
24
namespace App \DataTables \Helpers ;
24
25
26
+ use App \Entity \Parts \StorageLocation ;
25
27
use App \Entity \ProjectSystem \Project ;
26
28
use App \Entity \Attachments \Attachment ;
27
29
use App \Entity \Parts \Part ;
28
30
use App \Services \Attachments \AttachmentURLGenerator ;
29
31
use App \Services \Attachments \PartPreviewGenerator ;
30
32
use App \Services \EntityURLGenerator ;
33
+ use App \Services \Formatters \AmountFormatter ;
31
34
use Symfony \Contracts \Translation \TranslatorInterface ;
32
35
33
36
/**
34
37
* A helper service which contains common code to render columns for part related tables
35
38
*/
36
39
class PartDataTableHelper
37
40
{
38
- public function __construct (private readonly PartPreviewGenerator $ previewGenerator , private readonly AttachmentURLGenerator $ attachmentURLGenerator , private readonly EntityURLGenerator $ entityURLGenerator , private readonly TranslatorInterface $ translator )
39
- {
41
+ public function __construct (
42
+ private readonly PartPreviewGenerator $ previewGenerator ,
43
+ private readonly AttachmentURLGenerator $ attachmentURLGenerator ,
44
+ private readonly EntityURLGenerator $ entityURLGenerator ,
45
+ private readonly TranslatorInterface $ translator ,
46
+ private readonly AmountFormatter $ amountFormatter ,
47
+ ) {
40
48
}
41
49
42
50
public function renderName (Part $ context ): string
@@ -45,14 +53,16 @@ public function renderName(Part $context): string
45
53
46
54
//Depending on the part status we show a different icon (the later conditions have higher priority)
47
55
if ($ context ->isFavorite ()) {
48
- $ icon = sprintf ('<i class="fa-solid fa-star fa-fw me-1" title="%s"></i> ' , $ this ->translator ->trans ('part.favorite.badge ' ));
56
+ $ icon = sprintf ('<i class="fa-solid fa-star fa-fw me-1" title="%s"></i> ' ,
57
+ $ this ->translator ->trans ('part.favorite.badge ' ));
49
58
}
50
59
if ($ context ->isNeedsReview ()) {
51
- $ icon = sprintf ('<i class="fa-solid fa-ambulance fa-fw me-1" title="%s"></i> ' , $ this ->translator ->trans ('part.needs_review.badge ' ));
60
+ $ icon = sprintf ('<i class="fa-solid fa-ambulance fa-fw me-1" title="%s"></i> ' ,
61
+ $ this ->translator ->trans ('part.needs_review.badge ' ));
52
62
}
53
63
if ($ context ->getBuiltProject () instanceof Project) {
54
64
$ icon = sprintf ('<i class="fa-solid fa-box-archive fa-fw me-1" title="%s"></i> ' ,
55
- $ this ->translator ->trans ('part.info.projectBuildPart.hint ' ) . ': ' . $ context ->getBuiltProject ()->getName ());
65
+ $ this ->translator ->trans ('part.info.projectBuildPart.hint ' ). ': ' . $ context ->getBuiltProject ()->getName ());
56
66
}
57
67
58
68
@@ -85,4 +95,62 @@ public function renderPicture(Part $context): string
85
95
$ title
86
96
);
87
97
}
98
+
99
+ public function renderStorageLocations (Part $ context ): string
100
+ {
101
+ $ tmp = [];
102
+ foreach ($ context ->getPartLots () as $ lot ) {
103
+ //Ignore lots without storelocation
104
+ if (!$ lot ->getStorageLocation () instanceof StorageLocation) {
105
+ continue ;
106
+ }
107
+ $ tmp [] = sprintf (
108
+ '<a href="%s" title="%s">%s</a> ' ,
109
+ $ this ->entityURLGenerator ->listPartsURL ($ lot ->getStorageLocation ()),
110
+ htmlspecialchars ($ lot ->getStorageLocation ()->getFullPath ()),
111
+ htmlspecialchars ($ lot ->getStorageLocation ()->getName ())
112
+ );
113
+ }
114
+
115
+ return implode ('<br> ' , $ tmp );
116
+ }
117
+
118
+ public function renderAmount (Part $ context ): string
119
+ {
120
+ $ amount = $ context ->getAmountSum ();
121
+ $ expiredAmount = $ context ->getExpiredAmountSum ();
122
+
123
+ $ ret = '' ;
124
+
125
+ if ($ context ->isAmountUnknown ()) {
126
+ //When all amounts are unknown, we show a question mark
127
+ if ($ amount === 0.0 ) {
128
+ $ ret .= sprintf ('<b class="text-primary" title="%s">?</b> ' ,
129
+ $ this ->translator ->trans ('part_lots.instock_unknown ' ));
130
+ } else { //Otherwise mark it with greater equal and the (known) amount
131
+ $ ret .= sprintf ('<b class="text-primary" title="%s">≥</b> ' ,
132
+ $ this ->translator ->trans ('part_lots.instock_unknown ' )
133
+ );
134
+ $ ret .= htmlspecialchars ($ this ->amountFormatter ->format ($ amount , $ context ->getPartUnit ()));
135
+ }
136
+ } else {
137
+ $ ret .= htmlspecialchars ($ this ->amountFormatter ->format ($ amount , $ context ->getPartUnit ()));
138
+ }
139
+
140
+ //If we have expired lots, we show them in parentheses behind
141
+ if ($ expiredAmount > 0 ) {
142
+ $ ret .= sprintf (' <span title="%s" class="text-muted">(+%s)</span> ' ,
143
+ $ this ->translator ->trans ('part_lots.is_expired ' ),
144
+ htmlspecialchars ($ this ->amountFormatter ->format ($ expiredAmount , $ context ->getPartUnit ())));
145
+ }
146
+
147
+ //When the amount is below the minimum amount, we highlight the number red
148
+ if ($ context ->isNotEnoughInstock ()) {
149
+ $ ret = sprintf ('<b class="text-danger" title="%s">%s</b> ' ,
150
+ $ this ->translator ->trans ('part.info.amount.less_than_desired ' ),
151
+ $ ret );
152
+ }
153
+
154
+ return $ ret ;
155
+ }
88
156
}
0 commit comments