20
20
use Application \Controller \CoreEntityController ;
21
21
use Application \Model \CoreEntityModel ;
22
22
use OnePlace \Basket \Model \Basket ;
23
+ use OnePlace \Article \Model \ArticleTable ;
24
+ use OnePlace \Article \Variant \Model \VariantTable ;
23
25
use OnePlace \Basket \Model \BasketTable ;
24
26
use Laminas \View \Model \ViewModel ;
25
27
use Laminas \Db \Adapter \AdapterInterface ;
28
+ use OnePlace \Basket \Position \Model \PositionTable ;
29
+ use OnePlace \Contact \Address \Model \AddressTable ;
30
+ use OnePlace \Contact \Model \ContactTable ;
26
31
27
32
class BasketController extends CoreEntityController {
28
33
/**
@@ -31,6 +36,7 @@ class BasketController extends CoreEntityController {
31
36
* @since 1.0.0
32
37
*/
33
38
protected $ oTableGateway ;
39
+ protected $ aPluginTables ;
34
40
35
41
/**
36
42
* BasketController constructor.
@@ -39,10 +45,11 @@ class BasketController extends CoreEntityController {
39
45
* @param BasketTable $oTableGateway
40
46
* @since 1.0.0
41
47
*/
42
- public function __construct (AdapterInterface $ oDbAdapter ,BasketTable $ oTableGateway ,$ oServiceManager ) {
48
+ public function __construct (AdapterInterface $ oDbAdapter ,BasketTable $ oTableGateway ,$ oServiceManager, $ aPluginTables = [] ) {
43
49
$ this ->oTableGateway = $ oTableGateway ;
44
50
$ this ->sSingleForm = 'basket-single ' ;
45
51
parent ::__construct ($ oDbAdapter ,$ oTableGateway ,$ oServiceManager );
52
+ $ this ->aPluginTables = $ aPluginTables ;
46
53
47
54
if ($ oTableGateway ) {
48
55
# Attach TableGateway to Entity Models
@@ -121,4 +128,300 @@ public function viewAction() {
121
128
*/
122
129
return $ this ->generateViewView ('basket ' );
123
130
}
131
+
132
+ /**
133
+ * Close Basket
134
+ *
135
+ * @since 1.0.4
136
+ */
137
+ public function closeAction () {
138
+ $ this ->layout ('layout/json ' );
139
+
140
+ $ iJobID = $ this ->params ()->fromRoute ('id ' , 0 );
141
+ $ oJob = $ this ->oTableGateway ->getSingle ($ iJobID );
142
+
143
+ # Get State Tag
144
+ $ oStateTag = CoreEntityController::$ aCoreTables ['core-tag ' ]->select (['tag_key ' => 'state ' ]);
145
+ if (count ($ oStateTag ) > 0 ) {
146
+ $ oStateTag = $ oStateTag ->current ();
147
+
148
+ # Get Basket "done" Entity State Tag
149
+ $ oDoneState = CoreEntityController::$ aCoreTables ['core-entity-tag ' ]->select ([
150
+ 'entity_form_idfs ' => 'basket-single ' ,
151
+ 'tag_idfs ' => $ oStateTag ->Tag_ID ,
152
+ 'tag_key ' => 'done ' ,
153
+ ]);
154
+
155
+ if (count ($ oDoneState ) > 0 ) {
156
+ $ oDoneState = $ oDoneState ->current ();
157
+ $ this ->oTableGateway ->updateAttribute ('state_idfs ' ,$ oDoneState ->Entitytag_ID ,'Basket_ID ' ,$ iJobID );
158
+ $ this ->oTableGateway ->updateAttribute ('is_archived_idfs ' ,1 ,'Basket_ID ' ,$ iJobID );
159
+ $ this ->flashMessenger ()->addSuccessMessage ('Basket successfully closed ' );
160
+ }
161
+ }
162
+
163
+ $ this ->flashMessenger ()->addErrorMessage ('Could not close basket ' );
164
+ $ this ->redirect ()->toRoute ('basket ' ,['action ' =>'view ' ,'id ' =>$ iJobID ]);
165
+ }
166
+
167
+ /**
168
+ * Close Basket
169
+ *
170
+ * @since 1.0.4
171
+ */
172
+ public function orderAction () {
173
+ $ this ->layout ('layout/json ' );
174
+
175
+ $ iBasketID = $ this ->params ()->fromRoute ('id ' , 0 );
176
+
177
+ $ oBasket = $ this ->oTableGateway ->getSingle ($ iBasketID );
178
+ if ($ oBasket ) {
179
+ $ this ->closeBasketAndCreateOrder ($ oBasket ,$ oBasket ->payment_received );
180
+ $ this ->flashMessenger ()->addSuccessMessage ('Order successfully created ' );
181
+ $ this ->redirect ()->toRoute ('basket ' ,['action ' =>'view ' ,'id ' =>$ iBasketID ]);
182
+ } else {
183
+ $ this ->flashMessenger ()->addErrorMessage ('Could not close basket ' );
184
+ $ this ->redirect ()->toRoute ('basket ' ,['action ' =>'view ' ,'id ' =>$ iBasketID ]);
185
+ }
186
+ }
187
+
188
+ private function closeBasketAndCreateOrder ($ oBasket ,$ sPaymentReceived = '0000-00-00 00:00:00 ' ) {
189
+ # Get State Tag
190
+ $ oStateTag = CoreEntityController::$ aCoreTables ['core-tag ' ]->select (['tag_key ' => 'state ' ]);
191
+ if (count ($ oStateTag ) > 0 ) {
192
+ $ oStateTag = $ oStateTag ->current ();
193
+
194
+ # Get Basket "done" Entity State Tag
195
+ $ oDoneState = CoreEntityController::$ aCoreTables ['core-entity-tag ' ]->select ([
196
+ 'entity_form_idfs ' => 'basket-single ' ,
197
+ 'tag_idfs ' => $ oStateTag ->Tag_ID ,
198
+ 'tag_key ' => 'done ' ,
199
+ ]);
200
+
201
+ # only proceed of we have state tag present
202
+ if (count ($ oDoneState ) > 0 ) {
203
+ $ oDoneState = $ oDoneState ->current ();
204
+ $ this ->oTableGateway ->updateAttribute ('state_idfs ' , $ oDoneState ->Entitytag_ID , 'Basket_ID ' , $ oBasket ->getID ());
205
+ }
206
+ }
207
+ # archive basket
208
+ $ this ->oTableGateway ->updateAttribute ('is_archived_idfs ' , 1 , 'Basket_ID ' , $ oBasket ->getID ());
209
+
210
+ $ oNewJob = $ this ->aPluginTables ['job ' ]->generateNew ();
211
+
212
+ # Get Job "new" Entity State Tag
213
+ $ oNewState = CoreEntityController::$ aCoreTables ['core-entity-tag ' ]->select ([
214
+ 'entity_form_idfs ' => 'job-single ' ,
215
+ 'tag_idfs ' => $ oStateTag ->Tag_ID ,
216
+ 'tag_key ' => 'new ' ,
217
+ ]);
218
+ if (count ($ oNewState )) {
219
+ $ oNewState = $ oNewState ->current ();
220
+ $ aDelivery = false ;
221
+ $ oDeliveryMethod = CoreEntityController::$ aCoreTables ['core-entity-tag ' ]->select ([
222
+ 'Entitytag_ID ' => $ oBasket ->deliverymethod_idfs ,
223
+ ]);
224
+ if (count ($ oDeliveryMethod ) > 0 ) {
225
+ $ oDeliveryMethod = $ oDeliveryMethod ->current ();
226
+ $ aDelivery = [
227
+ 'id ' => $ oDeliveryMethod ->Entitytag_ID ,
228
+ 'label ' => $ oDeliveryMethod ->tag_value ,
229
+ 'gateway ' => $ oDeliveryMethod ->tag_key ,
230
+ 'icon ' => $ oDeliveryMethod ->tag_icon
231
+ ];
232
+ }
233
+
234
+ $ aPayment = false ;
235
+ $ oPaymentMethod = CoreEntityController::$ aCoreTables ['core-entity-tag ' ]->select ([
236
+ 'Entitytag_ID ' => $ oBasket ->paymentmethod_idfs ,
237
+ ]);
238
+ if (count ($ oPaymentMethod ) > 0 ) {
239
+ $ oPaymentMethod = $ oPaymentMethod ->current ();
240
+ $ aPayment = [
241
+ 'id ' => $ oPaymentMethod ->Entitytag_ID ,
242
+ 'label ' => $ oPaymentMethod ->tag_value ,
243
+ 'gateway ' => $ oPaymentMethod ->tag_key ,
244
+ 'icon ' => $ oPaymentMethod ->tag_icon
245
+ ];
246
+ }
247
+
248
+ $ aJobData = [
249
+ 'contact_idfs ' => $ oBasket ->contact_idfs ,
250
+ 'state_idfs ' => $ oNewState ->Entitytag_ID ,
251
+ 'paymentmethod_idfs ' => $ oBasket ->paymentmethod_idfs ,
252
+ 'payment_session_id ' => $ oBasket ->payment_session_id ,
253
+ 'payment_started ' => $ oBasket ->payment_started ,
254
+ 'payment_received ' => $ sPaymentReceived ,
255
+ 'payment_id ' => $ oBasket ->payment_id ,
256
+ 'deliverymethod_idfs ' => $ oBasket ->deliverymethod_idfs ,
257
+ 'label ' => 'Shop Bestellung vom ' .date ('d.m.Y H:i ' ,time ()),
258
+ 'date ' => date ('Y-m-d H:i:s ' ,time ()),
259
+ 'discount ' => 0 ,
260
+ 'description ' => 'Bestellung aus dem Shop. Kommentar des Kunden: ' .$ oBasket ->comment ,
261
+ 'created_by ' => 1 ,
262
+ 'created_date ' => date ('Y-m-d H:i:s ' ,time ()),
263
+ 'modified_by ' => 1 ,
264
+ 'modified_date ' => date ('Y-m-d H:i:s ' ,time ())
265
+ ];
266
+ $ oNewJob ->exchangeArray ($ aJobData );
267
+ $ iNewJobID = $ this ->aPluginTables ['job ' ]->saveSingle ($ oNewJob );
268
+ $ this ->oTableGateway ->updateAttribute ('job_idfs ' ,$ iNewJobID ,'Basket_ID ' ,$ oBasket ->getID ());
269
+
270
+ $ aPositions = $ this ->getBasketPositions ($ oBasket );
271
+ $ sPosHtml = '' ;
272
+ $ fTotal = 0 ;
273
+ if (count ($ aPositions ) > 0 ) {
274
+ $ iSortID = 0 ;
275
+ foreach ($ aPositions as $ oPos ) {
276
+ $ sHtmlLabel = '- ' ;
277
+ switch ($ oPos ->article_type ) {
278
+ case 'article ' :
279
+ $ sHtmlLabel = $ oPos ->oArticle ->label ;
280
+ break ;
281
+ case 'variant ' :
282
+ $ sHtmlLabel = $ oPos ->oArticle ->label .': ' .$ oPos ->oVariant ->label ;
283
+ break ;
284
+ case 'event ' :
285
+ $ sHtmlLabel = $ oPos ->oEvent ->label .': ' .$ oPos ->oVariant ->label ;
286
+ break ;
287
+ default :
288
+ break ;
289
+ }
290
+ $ sPosHtml .= '<tr> ' ;
291
+ $ sPosHtml .= '<td> ' .$ sHtmlLabel .'</td> ' ;
292
+ $ sPosHtml .= '<td> ' .$ oPos ->amount .'</td> ' ;
293
+ $ sPosHtml .= '<td> ' .number_format ((float )$ oPos ->price ,2 ,', ' ,'. ' ).' €</td> ' ;
294
+ $ sPosHtml .= '<td> ' .$ oPos ->comment .'</td></tr> ' ;
295
+
296
+ $ this ->aPluginTables ['job-position ' ]->insert ([
297
+ 'job_idfs ' => $ iNewJobID ,
298
+ 'article_idfs ' => $ oPos ->article_idfs ,
299
+ 'ref_idfs ' => $ oPos ->ref_idfs ,
300
+ 'ref_type ' => $ oPos ->ref_type ,
301
+ 'type ' => $ oPos ->article_type ,
302
+ 'sort_id ' => $ iSortID ,
303
+ 'amount ' => $ oPos ->amount ,
304
+ 'price ' => $ oPos ->price ,
305
+ 'discount ' => 0 ,
306
+ 'discount_type ' => 'percent ' ,
307
+ 'description ' => $ oPos ->comment
308
+ ]);
309
+ $ fTotal +=($ oPos ->amount *$ oPos ->price );
310
+ $ iSortID ++;
311
+ }
312
+ }
313
+ if ($ fTotal <= 100 && $ aDelivery ['gateway ' ] == 'mail ' ) {
314
+ $ this ->aPluginTables ['job-position ' ]->insert ([
315
+ 'job_idfs ' => $ iNewJobID ,
316
+ 'article_idfs ' => 0 ,
317
+ 'ref_idfs ' => 0 ,
318
+ 'ref_type ' => 'none ' ,
319
+ 'type ' => 'custom ' ,
320
+ 'sort_id ' => $ iSortID ,
321
+ 'amount ' => 1 ,
322
+ 'price ' => 2.5 ,
323
+ 'discount ' => 0 ,
324
+ 'discount_type ' => 'percent ' ,
325
+ 'description ' => 'Lieferkosten Postversand unter 100 € '
326
+ ]);
327
+ }
328
+
329
+ $ oContactTbl = CoreEntityController::$ oServiceManager ->get (ContactTable::class);
330
+ $ oAddressTbl = CoreEntityController::$ oServiceManager ->get (AddressTable::class);
331
+ try {
332
+ $ oContact = $ oContactTbl ->getSingle ($ oBasket ->contact_idfs );
333
+ } catch (\RuntimeException $ e ) {
334
+
335
+ }
336
+
337
+ if (isset ($ oContact )) {
338
+ $ oAddress = $ oAddressTbl ->getSingle ($ oContact ->getID (),'contact_idfs ' );
339
+ $ oContact ->address = $ oAddress ;
340
+
341
+ $ sEmail = $ aPayment ['gateway ' ];
342
+ if ($ aDelivery ['gateway ' ] == 'pickup ' ) {
343
+ $ sEmail = 'instore ' ;
344
+ }
345
+
346
+ $ this ->sendEmail ('email/shop/ ' .$ sEmail ,[
347
+ 'sInstallInfo ' => CoreEntityController::$ aGlobalSettings ['shop-email-subject-receipt ' ],
348
+ 'sContactName ' => $ oContact ->getLabel (),
349
+ 'sContactEmail ' => $ oContact ->getTextField ('email_private ' ),
350
+ 'sContactPhone ' => $ oContact ->getTextField ('phone_private ' ),
351
+ 'sContactAddr ' => $ oContact ->address ->street ,
352
+ 'sOrderComment ' => $ oBasket ->comment ,
353
+ 'sPaymentMethod ' => $ aPayment ['label ' ],
354
+ 'sDeliveryMethod ' => $ aDelivery ['label ' ],
355
+ 'oOrderPositions ' => $ sPosHtml ,
356
+ 'fOrderTotal ' => $ fTotal ,
357
+ 'sLogoPath ' => 'https://schwitzers.onep.lc/img/logo.png ' ,
358
+ ],$ oContact ->email_private ,$ oContact ->getLabel (),CoreEntityController::$ aGlobalSettings ['shop-email-subject-receipt ' ]);
359
+ }
360
+
361
+
362
+ } else {
363
+ // could not find state "new" tag for job
364
+ }
365
+ }
366
+
367
+ private function getBasketPositions ($ oBasketExists ) {
368
+ # we have a basket - lets check for positions
369
+ $ aPositions = [];
370
+ $ oPosTbl = CoreEntityController::$ oServiceManager ->get (PositionTable::class);
371
+ $ oArticleTbl = CoreEntityController::$ oServiceManager ->get (ArticleTable::class);
372
+ $ oVariantTbl = CoreEntityController::$ oServiceManager ->get (VariantTable::class);
373
+
374
+ try {
375
+ $ oEventTbl = CoreEntityController::$ oServiceManager ->get (\OnePlace \Event \Model \EventTable::class);
376
+ } catch (\RuntimeException $ e ) {
377
+ # event plugin not present
378
+ }
379
+
380
+ # attach positions
381
+ $ oBasketPositions = $ oPosTbl ->fetchAll (false ,['basket_idfs ' => $ oBasketExists ->getID ()]);
382
+ if (count ($ oBasketPositions ) > 0 ) {
383
+ foreach ($ oBasketPositions as $ oPos ) {
384
+ switch ($ oPos ->article_type ) {
385
+ case 'variant ' :
386
+ $ oPos ->oVariant = $ oVariantTbl ->getSingle ($ oPos ->article_idfs );
387
+ $ oPos ->oArticle = $ oArticleTbl ->getSingle ($ oPos ->oVariant ->article_idfs );
388
+ $ oPos ->oArticle ->featured_image = '/data/article/ ' .$ oPos ->oArticle ->getID ().'/ ' .$ oPos ->oArticle ->featured_image ;
389
+ # check for custom price (used for free amount coupons)
390
+ if ($ oPos ->price != 0 ) {
391
+ $ oPos ->oVariant ->price = $ oPos ->price ;
392
+ }
393
+ # event plugin
394
+ if ($ oPos ->ref_idfs != 0 ) {
395
+ switch ($ oPos ->ref_type ) {
396
+ case 'event ' :
397
+ if (isset ($ oEventTbl )) {
398
+ $ oPos ->article_type = 'event ' ;
399
+ $ oPos ->oEvent = $ oEventTbl ->getSingle ($ oPos ->ref_idfs );
400
+ # Event Rerun Plugin Start
401
+ if ($ oPos ->oEvent ->root_event_idfs != 0 ) {
402
+ $ oRoot = $ oEventTbl ->getSingle ($ oPos ->oEvent ->root_event_idfs );
403
+ $ oPos ->oEvent ->label = $ oRoot ->label ;
404
+ $ oPos ->oEvent ->excerpt = $ oRoot ->excerpt ;
405
+ $ oPos ->oEvent ->featured_image = $ oRoot ->featured_image ;
406
+ $ oPos ->oEvent ->description = $ oRoot ->description ;
407
+ $ oPos ->oEvent ->featured_image = '/data/event/ ' .$ oRoot ->getID ().'/ ' .$ oPos ->oEvent ->featured_image ;
408
+ } else {
409
+ $ oPos ->oEvent ->featured_image = '/data/event/ ' .$ oPos ->oEvent ->getID ().'/ ' .$ oPos ->oEvent ->featured_image ;
410
+ }
411
+ }
412
+ break ;
413
+ default :
414
+ break ;
415
+ }
416
+ }
417
+ break ;
418
+ default :
419
+ break ;
420
+ }
421
+ $ aPositions [] = $ oPos ;
422
+ }
423
+ }
424
+
425
+ return $ aPositions ;
426
+ }
124
427
}
0 commit comments