8
8
namespace Magento \Bundle \Model \Sales \Order \Pdf \Items ;
9
9
10
10
use Magento \Framework \Data \Collection \AbstractDb ;
11
+ use Magento \Framework \DataObject ;
11
12
use Magento \Framework \Filesystem ;
12
13
use Magento \Framework \Filter \FilterManager ;
13
14
use Magento \Framework \Model \Context ;
@@ -69,50 +70,53 @@ public function __construct(
69
70
}
70
71
71
72
/**
72
- * Draw item line
73
+ * Draw bundle product item line
73
74
*
74
75
* @return void
75
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
76
- * @SuppressWarnings(PHPMD.NPathComplexity)
77
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
78
76
*/
79
77
public function draw ()
80
78
{
81
- $ order = $ this ->getOrder ();
82
- $ item = $ this ->getItem ();
83
- $ pdf = $ this ->getPdf ();
84
- $ page = $ this ->getPage ();
79
+ $ draw = $ this ->drawChildrenItems ();
80
+ $ draw = $ this ->drawCustomOptions ($ draw );
85
81
82
+ $ page = $ this ->getPdf ()->drawLineBlocks ($ this ->getPage (), $ draw , ['table_header ' => true ]);
83
+
84
+ $ this ->setPage ($ page );
85
+ }
86
+
87
+ /**
88
+ * Draw bundle product children items
89
+ *
90
+ * @return array
91
+ */
92
+ private function drawChildrenItems (): array
93
+ {
86
94
$ this ->_setFontRegular ();
87
- $ items = $ this ->getChildren ($ item );
88
95
89
96
$ prevOptionId = '' ;
90
97
$ drawItems = [];
91
-
92
- foreach ($ items as $ childItem ) {
93
- $ line = [];
94
-
98
+ $ optionId = 0 ;
99
+ foreach ($ this -> getChildren ( $ this -> getItem ()) as $ childItem ) {
100
+ $ lines = [];
101
+ $ index = array_key_last ( $ lines ) !== null ? array_key_last ( $ lines ) + 1 : 0 ;
95
102
$ attributes = $ this ->getSelectionAttributes ($ childItem );
96
103
if (is_array ($ attributes )) {
97
104
$ optionId = $ attributes ['option_id ' ];
98
- } else {
99
- $ optionId = 0 ;
100
105
}
101
106
102
107
if (!isset ($ drawItems [$ optionId ])) {
103
108
$ drawItems [$ optionId ] = ['lines ' => [], 'height ' => 15 ];
104
109
}
105
110
106
111
if ($ childItem ->getOrderItem ()->getParentItem () && $ prevOptionId != $ attributes ['option_id ' ]) {
107
- $ line [ 0 ] = [
112
+ $ lines [ $ index ][ ] = [
108
113
'font ' => 'italic ' ,
109
114
'text ' => $ this ->string ->split ($ attributes ['option_label ' ], 45 , true , true ),
110
115
'feed ' => 35 ,
111
116
];
112
117
113
- $ drawItems [$ optionId ] = ['lines ' => [$ line ], 'height ' => 15 ];
114
-
115
- $ line = [];
118
+ $ drawItems [$ optionId ] = ['height ' => 15 ];
119
+ $ index ++;
116
120
$ prevOptionId = $ attributes ['option_id ' ];
117
121
}
118
122
@@ -124,35 +128,97 @@ public function draw()
124
128
$ feed = 35 ;
125
129
$ name = $ childItem ->getName ();
126
130
}
127
- $ line [] = ['text ' => $ this ->string ->split ($ name , 35 , true , true ), 'feed ' => $ feed ];
131
+ $ lines [ $ index ] [] = ['text ' => $ this ->string ->split ($ name , 35 , true , true ), 'feed ' => $ feed ];
128
132
129
- // draw SKUs
130
- if (!$ childItem ->getOrderItem ()->getParentItem ()) {
131
- $ text = [];
132
- foreach ($ this ->string ->split ($ item ->getSku (), 17 ) as $ part ) {
133
- $ text [] = $ part ;
134
- }
135
- $ line [] = ['text ' => $ text , 'feed ' => 255 ];
136
- }
133
+ $ lines = $ this ->drawSkus ($ childItem , $ lines );
137
134
138
- // draw prices
139
- if ($ this ->canShowPriceInfo ($ childItem )) {
140
- $ price = $ order ->formatPriceTxt ($ childItem ->getPrice ());
141
- $ line [] = ['text ' => $ price , 'feed ' => 395 , 'font ' => 'bold ' , 'align ' => 'right ' ];
142
- $ line [] = ['text ' => $ childItem ->getQty () * 1 , 'feed ' => 435 , 'font ' => 'bold ' ];
135
+ $ lines = $ this ->drawPrices ($ childItem , $ lines );
136
+ $ drawItems [$ optionId ]['lines ' ] = $ lines ;
137
+ }
143
138
144
- $ tax = $ order -> formatPriceTxt ( $ childItem -> getTaxAmount ()) ;
145
- $ line [] = [ ' text ' => $ tax , ' feed ' => 495 , ' font ' => ' bold ' , ' align ' => ' right ' ];
139
+ return $ drawItems ;
140
+ }
146
141
147
- $ row_total = $ order ->formatPriceTxt ($ childItem ->getRowTotal ());
148
- $ line [] = ['text ' => $ row_total , 'feed ' => 565 , 'font ' => 'bold ' , 'align ' => 'right ' ];
142
+ /**
143
+ * Draw sku parts
144
+ *
145
+ * @param DataObject $childItem
146
+ * @param array $lines
147
+ * @return array
148
+ */
149
+ private function drawSkus (DataObject $ childItem , array $ lines ): array
150
+ {
151
+ $ index = array_key_last ($ lines );
152
+ if (!$ childItem ->getOrderItem ()->getParentItem ()) {
153
+ $ text = [];
154
+ foreach ($ this ->string ->split ($ this ->getItem ()->getSku (), 17 ) as $ part ) {
155
+ $ text [] = $ part ;
149
156
}
157
+ $ lines [$ index ][] = ['text ' => $ text , 'feed ' => 255 ];
158
+ }
159
+
160
+ return $ lines ;
161
+ }
150
162
151
- $ drawItems [$ optionId ]['lines ' ][] = $ line ;
163
+ /**
164
+ * Draw prices for bundle product children items
165
+ *
166
+ * @param DataObject $childItem
167
+ * @param array $lines
168
+ * @return array
169
+ */
170
+ private function drawPrices (DataObject $ childItem , array $ lines ): array
171
+ {
172
+ $ index = array_key_last ($ lines );
173
+ if ($ this ->canShowPriceInfo ($ childItem )) {
174
+ $ lines [$ index ][] = ['text ' => $ childItem ->getQty () * 1 , 'feed ' => 435 , 'align ' => 'right ' ];
175
+
176
+ $ tax = $ this ->getOrder ()->formatPriceTxt ($ childItem ->getTaxAmount ());
177
+ $ lines [$ index ][] = ['text ' => $ tax , 'feed ' => 495 , 'font ' => 'bold ' , 'align ' => 'right ' ];
178
+
179
+ $ item = $ this ->getItem ();
180
+ $ this ->_item = $ childItem ;
181
+ $ feedPrice = 380 ;
182
+ $ feedSubtotal = $ feedPrice + 185 ;
183
+ foreach ($ this ->getItemPricesForDisplay () as $ priceData ) {
184
+ if (isset ($ priceData ['label ' ])) {
185
+ // draw Price label
186
+ $ lines [$ index ][] = ['text ' => $ priceData ['label ' ], 'feed ' => $ feedPrice , 'align ' => 'right ' ];
187
+ // draw Subtotal label
188
+ $ lines [$ index ][] = ['text ' => $ priceData ['label ' ], 'feed ' => $ feedSubtotal , 'align ' => 'right ' ];
189
+ $ index ++;
190
+ }
191
+ // draw Price
192
+ $ lines [$ index ][] = [
193
+ 'text ' => $ priceData ['price ' ],
194
+ 'feed ' => $ feedPrice ,
195
+ 'font ' => 'bold ' ,
196
+ 'align ' => 'right ' ,
197
+ ];
198
+ // draw Subtotal
199
+ $ lines [$ index ][] = [
200
+ 'text ' => $ priceData ['subtotal ' ],
201
+ 'feed ' => $ feedSubtotal ,
202
+ 'font ' => 'bold ' ,
203
+ 'align ' => 'right ' ,
204
+ ];
205
+ $ index ++;
206
+ }
207
+ $ this ->_item = $ item ;
152
208
}
153
209
154
- // custom options
155
- $ options = $ item ->getOrderItem ()->getProductOptions ();
210
+ return $ lines ;
211
+ }
212
+
213
+ /**
214
+ * Draw bundle product custom options
215
+ *
216
+ * @param array $draw
217
+ * @return array
218
+ */
219
+ private function drawCustomOptions (array $ draw ): array
220
+ {
221
+ $ options = $ this ->getItem ()->getOrderItem ()->getProductOptions ();
156
222
if ($ options && isset ($ options ['options ' ])) {
157
223
foreach ($ options ['options ' ] as $ option ) {
158
224
$ lines = [];
@@ -180,12 +246,10 @@ public function draw()
180
246
$ lines [][] = ['text ' => $ text , 'feed ' => 40 ];
181
247
}
182
248
183
- $ drawItems [] = ['lines ' => $ lines , 'height ' => 15 ];
249
+ $ draw [] = ['lines ' => $ lines , 'height ' => 15 ];
184
250
}
185
251
}
186
252
187
- $ page = $ pdf ->drawLineBlocks ($ page , $ drawItems , ['table_header ' => true ]);
188
-
189
- $ this ->setPage ($ page );
253
+ return $ draw ;
190
254
}
191
255
}
0 commit comments