-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzasilkovna.php
1358 lines (1153 loc) · 49.6 KB
/
zasilkovna.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Joomla\CMS\Toolbar\Button\LinkButton;
use Joomla\CMS\Toolbar\Button\SeparatorButton;
use VirtueMartModelZasilkovna\ShipmentMethod;
use VirtueMartModelZasilkovna\Carrier\VendorGroups;
defined('_JEXEC') or die('Restricted access');
defined('PACKETERY_MEDIA_DIR') || define('PACKETERY_MEDIA_DIR', __DIR__ . '/../../../media/com_zasilkovna/media');
spl_autoload_register(
function ($className) {
$className = ltrim($className, '\\');
$parts = explode('\\', $className);
$path = JPATH_ADMINISTRATOR . '/components/com_virtuemart/models/zasilkovna_src/' . implode('/', $parts) . '.php';
if (is_file($path)) {
require_once $path;
}
}
);
if(!class_exists('vmPSPlugin'))
require(JPATH_VM_PLUGINS . DS . 'vmpsplugin.php');
if(!class_exists('calculationHelper')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
}
if(!class_exists('CurrencyDisplay')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'currencydisplay.php');
}
if(!class_exists('VirtueMartModelVendor')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'vendor.php');
}
require_once VMPATH_ADMIN . '/fields/vmzasilkovnacountries.php';
require_once VMPATH_ADMIN . '/fields/vmzasilkovnahdcarriers.php';
class plgVmShipmentZasilkovna extends vmPSPlugin
{
const DEFAULT_WEIGHT_UNIT = 'KG';
const TEMPLATES_DIR = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'views' . DS . 'zasilkovna' . DS . 'tmpl';
const TRACKING_URL = 'https://tracking.app.packeta.com/%s';
const ZASILKOVNA_OLD_RECREATE_KEY_VALUE = 'zasilkovna_old_recreate_key_value';
public static $_this = false;
/** @var VirtueMartModelZasilkovna */
protected $model;
/** @var \Joomla\CMS\Session\Session */
protected $session;
/** @var \VirtueMartModelZasilkovna\CheckoutModuleDetector */
protected $checkoutModuleDetector;
/** @var \VirtueMartModelZasilkovna\ShipmentMethodStorage */
private $shipmentMethodStorage;
/** @var \VirtueMartModelZasilkovna\ShipmentMethodValidator */
protected $shipmentMethodValidator;
/** @var \VirtueMartModelZasilkovna\Order\Detail */
protected $orderDetail;
/** @var \VirtueMartModelZasilkovna\Order\Repository */
protected $orderRepository;
/** @var \VirtueMartModelZasilkovna\Box\Renderer */
protected $renderer;
/** @var \VirtueMartModelZasilkovna\Carrier\Repository */
protected $carrierRepository;
/**
* plgVmShipmentZasilkovna constructor.
*
* @param $subject
* @param $config
*/
public function __construct(&$subject, $config) {
parent::__construct($subject, $config);
$this->_loggable = true;
$this->tableFields = array_keys($this->getTableSQLFields());
$varsToPush = $this->getVarsToPush();
self::addVarsToPushCore($varsToPush,0);
$this->setConfigParameterable($this->_configTableFieldName, $varsToPush);
$this->setConvertable(
[
'min_amount',
'max_amount',
'shipment_cost' // see convertToVendorCurrency method
]
);
$this->model = VmModel::getModel('zasilkovna');
$this->session = JFactory::getSession();
$this->checkoutModuleDetector = new \VirtueMartModelZasilkovna\CheckoutModuleDetector();
$this->shipmentMethodStorage = new \VirtueMartModelZasilkovna\ShipmentMethodStorage($this->session);
$this->shipmentMethodValidator = new \VirtueMartModelZasilkovna\ShipmentMethodValidator();
$this->orderDetail = new \VirtueMartModelZasilkovna\Order\Detail();
$this->orderRepository = new \VirtueMartModelZasilkovna\Order\Repository();
$this->renderer = new \VirtueMartModelZasilkovna\Box\Renderer();
$this->carrierRepository = new \VirtueMartModelZasilkovna\Carrier\Repository();
}
/**
* @param $type
* @param $name
* @param $render
*/
public function plgVmOnSelfCallFE($type, $name, &$render) {
/** @var \Joomla\CMS\Application\CMSApplication $app */
$app = JFactory::getApplication();
// JInput object
$input = $app->input;
$task = $input->get('task', 'none', 'string');
$method = 'handle' . ucfirst($task);
if (method_exists($this, $method)) {
$render = call_user_func_array([$this, $method], []);
}
}
/**
* @return \JResponseJson
*/
public function handleSaveSelectedPoint() {
$app = JFactory::getApplication();
$branchId = $app->input->getInt('branch_id');
if ($branchId) {
$methodId = $app->input->getInt('shipment_id', 0);
$this->shipmentMethodStorage->set($methodId, 'branch_id', $branchId);
$this->shipmentMethodStorage->set($methodId, 'branch_currency', $app->input->getString('branch_currency', ''));
$this->shipmentMethodStorage->set($methodId, 'branch_name_street', $app->input->getString('branch_name_street', ''));
$this->shipmentMethodStorage->set($methodId, 'branch_country', $app->input->getString('branch_country', ''));
$this->shipmentMethodStorage->set($methodId, 'branch_carrier_id', $app->input->getString('branch_carrier_id', ''));
$this->shipmentMethodStorage->set($methodId, 'branch_carrier_pickup_point', $app->input->getString('branch_carrier_pickup_point', ''));
}
$response = (object)[
'status' => 'ok',
];
return new JResponseJson($response);
}
public function handleProvideCheckoutTailBlockJsFile() {
/** @var \Joomla\CMS\Application\CMSApplication $app */
$app = JFactory::getApplication();
$app->setHeader('Content-Type', 'application/javascript', true);
$app->sendHeaders();
$activeCheckout = $this->checkoutModuleDetector->getActiveCheckout();
$jsFile = $activeCheckout->getTailBlockJs();
if (is_file($jsFile)) {
echo file_get_contents($jsFile);
} else {
http_response_code(404);
}
jExit();
}
/**
* Updates carriers.
*/
public function handleUpdateCarriers() {
$app = JFactory::getApplication();
$token = $app->input->getString('token', '');
$expectedToken = $this->model->getConfig('cron_token');
if ($token !== $expectedToken) {
jExit();
}
/** @var VirtueMartModelZasilkovna $model */
$model = VmModel::getModel('zasilkovna');
$model->updateCarriers();
foreach ($model->errors as $error) {
echo $error;
echo '<br>';
}
if (empty($model->errors)) {
echo JText::_('PLG_VMSHIPMENT_PACKETERY_CARRIERS_UPDATED');
echo '<br>';
}
jExit();
}
/**
* Create the table for this plugin if it does not yet exist.
*
* @author Valérie Isaksen
*/
public function getVmPluginCreateTableSQL() {
return $this->createTableSQL('zasilkovna');
}
/**
* Get plugin table fields definition.
*
* @return array
*/
public function getTableSQLFields() {
$updater = new GenericTableUpdater();
$tableDefinitions = $updater->getTablesBySql(__DIR__ . '/install.sql');
$pluginTableDefinition = $tableDefinitions['#__virtuemart_shipment_plg_zasilkovna'];
return $pluginTableDefinition[0];
}
/**
* This method is fired when showing the order details in the frontend.
* It displays the shipment-specific data.
*
* @param integer $virtuemart_order_id The order Number
* @return mixed Null for shipments that aren't active, text (HTML) otherwise
* @author Valérie Isaksen
* @author Max Milbers
*/
public function plgVmOnShowOrderFEShipment($virtuemart_order_id, $virtuemart_shipmentmethod_id, &$shipment_name)
{
if (!($this->selectedThisByMethodId($virtuemart_shipmentmethod_id))) {
return null;
}
$order = $this->orderRepository->getOrderByVmOrderId($virtuemart_order_id);
if ($order->isHomeDelivery()) {
$this->onShowOrderFE($virtuemart_order_id, $virtuemart_shipmentmethod_id, $shipment_name);
} else {
$shipment_name .= $this->getOrderShipmentHtml($order);
}
}
/**
* @param string|int $methodId
*/
public function clearPickedDeliveryPoint($methodId) {
$this->shipmentMethodStorage->clear($methodId, 'branch_id');
$this->shipmentMethodStorage->clear($methodId, 'branch_currency');
$this->shipmentMethodStorage->clear($methodId, 'branch_name_street');
$this->shipmentMethodStorage->clear($methodId, 'branch_country');
$this->shipmentMethodStorage->clear($methodId, 'branch_carrier_id');
$this->shipmentMethodStorage->clear($methodId, 'branch_carrier_pickup_point');
}
/**
* This event is fired after the order has been stored; it gets the shipment method-
* specific data.
*
* @return mixed Null when this method was not selected, otherwise true
* @author Valerie Isaksen
*/
function plgVmConfirmedOrder(VirtueMartCart $cart, $order) {
if (!($method = $this->getVmPluginMethod($order['details']['BT']->virtuemart_shipmentmethod_id))) {
return null; // Another method was selected, do nothing
}
if (!$this->selectedThisElement($method->shipment_element)) {
return false;
}
if (!$this->OnSelectCheck($cart)) {
return false;
}
$zasMethod = ShipmentMethod::fromRandom($method);
$currency = $this->model->getCurrencyCode($order['details']['BT']->order_currency);
// GET PARAMETERS FROM SESSION AND CLEAR
$branch_id = $this->shipmentMethodStorage->get($cart->virtuemart_shipmentmethod_id, 'branch_id', 0);
$branch_name_street = $this->shipmentMethodStorage->get($cart->virtuemart_shipmentmethod_id, 'branch_name_street', '');
$branch_carrier_id = $this->shipmentMethodStorage->get($cart->virtuemart_shipmentmethod_id, 'branch_carrier_id');
$branch_carrier_pickup_point = $this->shipmentMethodStorage->get($cart->virtuemart_shipmentmethod_id, 'branch_carrier_pickup_point');
$this->clearPickedDeliveryPoint($cart->virtuemart_shipmentmethod_id);
$codSettings = $this->model->getConfig('zasilkovna_payment_method_'.$cart->virtuemart_paymentmethod_id, 0);
$billing=$order['details']['BT'];
$shipping=$order['details']['ST'];
// $shippingDetails = $order['details']['ST'];
// IF BILLING AND SHIPPING DETAILS ARE DIFFERENT USE SHIPPING DETAILS
if((int)$billing->STsameAsBT === 0){
// FALLBACK TO BILLING CONTACT IF NECESSARY, BECAUSE OF THE POSSIBILITY TO HAVE SHIPPING ADDRESS ENTIRELY WITHOUT CONTACT
$noShippingContact = (
empty($shipping->email) &&
empty($shipping->phone_1) &&
empty($shipping->phone_2)
);
if($noShippingContact){
$email = $billing->email;
$phone_1 = $billing->phone_1;
$phone_2 = $billing->phone_2;
}else{
$email = $shipping->email;
$phone_1 = $shipping->phone_1;
$phone_2 = $shipping->phone_2;
}
$details = $shipping;
}
else{
$email = $billing->email;
$phone_1 = $billing->phone_1;
$phone_2 = $billing->phone_2;
$details = $billing;
}
// external pickup point support
if (empty($branch_carrier_id)) {
$is_carrier = 0;
$branch_carrier_pickup_point = ''; // VirtueMart is unable to handle null values
} else {
$branch_id = $branch_carrier_id;
$is_carrier = 1;
}
if ($zasMethod->isHdCarrier()) {
$carrier = $this->carrierRepository->getCarrierById($zasMethod->getHdCarrierId()) ;
if (!$carrier) {
return false;
}
$is_carrier = 1;
$branch_id = $carrier->id;
$branch_name_street = $carrier->name ?: $carrier->id;
}
$values['virtuemart_order_id'] = $details->virtuemart_order_id;
$values['virtuemart_shipmentmethod_id'] = $details->virtuemart_shipmentmethod_id;
$values['order_number'] = $details->order_number;
$values['zasilkovna_packet_id'] = 0;
$values['zasilkovna_packet_price'] = $details->order_total;
$values['branch_id'] = $branch_id;
$values['branch_currency'] = $currency;
$values['branch_name_street'] = $branch_name_street;
$values['is_carrier'] = $is_carrier;
$values['carrier_pickup_point'] = $branch_carrier_pickup_point;
$values['email'] = $email;
$values['phone'] = $phone_1 ? $phone_1 : $phone_2;
$values['first_name'] = $details->first_name;
$values['last_name'] = $details->last_name;
$values['address'] = $details->address_1;
$values['city'] = $details->city;
$values['zip_code'] = $details->zip;
$values['adult_content'] = 0;
$values['packet_cod'] = ($codSettings ? $details->order_total : 0);
$values['is_cod'] = $codSettings; //depends on actual settings of COD payments until its set manually in administration
$values['exported'] = 0;
$values['shipment_name'] = $method->shipment_name;
$values['shipment_cost'] = $this->getCosts($cart, ShipmentMethod::fromRandom($method), "");
$values['weight'] = $this->getOrderWeight($cart, self::DEFAULT_WEIGHT_UNIT);
$values['tax_id'] = $method->tax_id;
$this->orderRepository->insertOrder($values);
return true;
}
/**
* @param $method
*/
function convertToVendorCurrency(&$method){
if(!isset($method->converted) && isset($method->currency_id)){
$currencyId = $method->currency_id;
$method->min_amount = $this->convertValueToVendorCurrency($method->min_amount, $currencyId);
$method->max_amount = $this->convertValueToVendorCurrency($method->max_amount, $currencyId);
$method->shipment_cost = $this->convertValueToVendorCurrency($method->shipment_cost, $currencyId);
$method->free_shipment = $this->convertValueToVendorCurrency($method->free_shipment, $currencyId);
$rulesFE = ($method->globalWeightRules ?: []);
foreach ($rulesFE as &$globalWeightRule) {
$globalWeightRule->price = $this->convertValueToVendorCurrency($globalWeightRule->price, $currencyId);
}
$rules2FE = ($method->pricingRules ?: []);
foreach ($rules2FE as &$pricingRule) {
$pricingRule->shipment_cost = $this->convertValueToVendorCurrency($pricingRule->shipment_cost, $currencyId);
$pricingRule->free_shipment = $this->convertValueToVendorCurrency($pricingRule->free_shipment, $currencyId);
$rules3 = ($pricingRule->weightRules ?: []);
foreach ($rules3 as &$weightRule) {
$weightRule->price = $this->convertValueToVendorCurrency($weightRule->price, $currencyId);
}
}
$method->converted = 1;
}
}
/**
* @param $value
* @param $currency_id
* @return mixed
*/
function convertValueToVendorCurrency($value, $currency_id)
{
$calculator = calculationHelper::getInstance ();
if(!empty($value)){
return $calculator->_currencyDisplay->convertCurrencyTo($currency_id, $value, true);
}
return $value;
}
/**
* return total cart weight
*
* @param $cart
* @return float|int
*/
public function getCartWeight($cart){
$conversionArray = array(
'KG' => 1,
'G' => 1000,
'MG' => 1000000,
'LB' => 2.20462262,
'OZ' => 35.2739619
);
$totalWeight = 0;
foreach ($cart->products as $product){
if( isset($conversionArray[$product->product_weight_uom]) ){
$totalWeight += (($product->product_weight/$conversionArray[$product->product_weight_uom])*$product->quantity);
}else{
$totalWeight += ($product->product_weight*$product->quantity);
}
}
return $totalWeight;
}
/**
* @return string versionString
*/
function getVersionString(){
$versionStrings = array(
'joomla' => "Joomla-".Joomla\CMS\Version::MAJOR_VERSION.".".Joomla\CMS\Version::MINOR_VERSION,
'virtuemart' => "VirtueMart-".vmVersion::$RELEASE,
'module' => "Packeta-".VirtueMartModelZasilkovna::VERSION,
);
return implode('-', $versionStrings);
}
/**
* Return delivery price for weight, NULL if none found.
* @param int $countryId
* @param ShipmentMethod $method
* @param float $weight
* @return float|null
*/
protected function resolveCountryPrice($countryId, ShipmentMethod $method, $weight)
{
$hasCountryConfig = $method->hasPricingRuleForCountry($countryId);
if ($hasCountryConfig) {
$weightRules = $method->getCountryWeightRules($countryId);
if ($weightRules) {
$weightRule = $method->resolveWeightRule($weightRules, $weight);
if ($weightRule) {
return (float) $weightRule->price;
}
}
return $method->getCountryDefaultPrice($countryId);
}
$weightRules = $method->getGlobalWeightRules();
if ($weightRules) {
$weightRule = $method->resolveWeightRule($weightRules, $weight);
if ($weightRule) {
return (float) $weightRule->price;
}
}
return null;
}
/**
* Return country code from order billing address.
* @param VirtueMartCart $cart
* @return string|null
*/
protected function getOrderBillingAddressCountryId(VirtueMartCart $cart)
{
// If user set Shipping address same as Billing we take the billing address
// (shipping address may contain other data but that is discarded)
/** @var array $address */
$address = $this->getAddressFromCart($cart);
// No country id is found in billing address.
if (!is_array($address) || !isset($address['virtuemart_country_id']))
{
return NULL;
}
// Get country code.
$cid = $address['virtuemart_country_id'];
// Return country code.
return ($cid ? $cid : NULL);
}
/**
* Check if order has available free shipping.
*
* @param VirtueMartCart $cart
* @param $cart_prices
* @param ShipmentMethod $method
* @return bool
*/
protected function isFreeShippingActive(VirtueMartCart $cart, $cart_prices, ShipmentMethod $method)
{
// Billing address country code is required to free shipping.
$countryId = $this->getOrderBillingAddressCountryId($cart);
if ($countryId === NULL) {
return FALSE;
}
// Load order price.
if (empty($cart_prices['salesPrice']) || !is_numeric($cart_prices['salesPrice'])) {
return FALSE;
}
$orderPrice = (float)$cart_prices['salesPrice'];
// 1) Check if country free shipping criteria is met.
$countryLimit = $method->getCountryFreeShipping($countryId);
// if country limit then override global free shipment
if ($countryLimit !== null) {
if (is_numeric($countryLimit) && $orderPrice >= (float)$countryLimit) {
return true;
}
return false;
}
// 3) Check if default free shipping criteria is met.
$defaultLimit = $method->getGlobalFreeShipping();
if (is_numeric($defaultLimit) && $orderPrice >= (float)$defaultLimit) {
return TRUE;
}
// 4) Free shipping is not available.
return FALSE;
}
/**
* Get Zasilkovna delivery price.
* @param VirtueMartCart $cart
* @param $method
* @param $cart_prices
* @return float delivery cost for the shipping method instance
*/
function getCosts(VirtueMartCart $cart, $method, $cart_prices)
{
$this->convertToVendorCurrency($method);
$method = ShipmentMethod::fromRandom($method);
$defaultPrice = $method->getGlobalDefaultPrice();
// Load default price from global config.
if (!$defaultPrice || !is_numeric($defaultPrice))
{
// Is safe set default price to 0, because if price = 0 and
// free shipping is not active this delivery method is disabled!
$defaultPrice = 0.0;
}
else
{
// Default price must be float.
$defaultPrice = (float) $defaultPrice;
}
// Load order billing address country code.
$code = $this->getOrderBillingAddressCountryId($cart);
// If no code (address) is set return global default price or 0.
if ($code === NULL)
{
return $defaultPrice;
}
// Calculate total weight of the order package.
$totalWeight = round($this->getOrderWeight($cart, self::DEFAULT_WEIGHT_UNIT),2);
// 1) Check if is free shipping criteria meet.
if ($this->isFreeShippingActive($cart, $cart_prices, $method))
{
return 0.0;
}
// 2) Try calculate country delivery price for weight.
$resolvedPrice = $this->resolveCountryPrice($code, $method, $totalWeight);
if ($resolvedPrice !== null)
{
return $resolvedPrice;
}
// 4) Return default delivery price.
return $defaultPrice;
}
/**
* Is delivery available?
* @param VirtueMartCart $cart
* @param TableShipmentmethods $method
* @param array $cart_prices
* @return bool
*/
protected function checkConditions($cart, $method, $cart_prices)
{
$this->convertToVendorCurrency($method);
$method = ShipmentMethod::fromRandom($method);
// Check order max weight (TODO: duplicate with plgVmDisplayListFEShipment).
$orderMaxWeight = ($method->getGlobalMaxWeight() ?: VirtueMartModelZasilkovna::MAX_WEIGHT_DEFAULT);
$orderActualWeight = $this->getOrderWeight($cart, self::DEFAULT_WEIGHT_UNIT);
if ($orderActualWeight > $orderMaxWeight) {
return false;
}
$deliveryCost = (int)$this->getCosts($cart, $method, $cart_prices);
$isFreeShippingActive = $this->isFreeShippingActive($cart, $cart_prices, $method);
if (($deliveryCost === 0 && !$isFreeShippingActive) === true) {
return false;
}
return parent::checkConditions($cart, $method->getParams(), $cart_prices);
}
/**
* Create the table for this plugin if it does not yet exist.
* This functions checks if the called plugin is active one.
* When yes it is calling the standard method to create the tables
*
* @author Valérie Isaksen
*
*/
function plgVmOnStoreInstallShipmentPluginTable($jplugin_id) {
$return = $this->onStoreInstallPluginTable($jplugin_id);
$oldRecreateIndexes = VmConfig::get(self::ZASILKOVNA_OLD_RECREATE_KEY_VALUE, -1);
if ($oldRecreateIndexes !== -1) {
VmConfig::set('reCreaKey', $oldRecreateIndexes);
}
VmConfig::set(self::ZASILKOVNA_OLD_RECREATE_KEY_VALUE, -1);
return $return;
}
/**
* This event is fired after the shipment method has been selected. It can be used to store
* additional payment info in the cart.
*
* @author Max Milbers
* @author Valérie isaksen
*
* @param VirtueMartCart $cart : the actual cart
* @return null if the payment was not selected, true if the data is valid, error message if the data is not vlaid
*
*/
public function plgVmOnSelectCheckShipment(VirtueMartCart &$cart) {
if(!$this->selectedThisByMethodId($cart->virtuemart_shipmentmethod_id)) {
return NULL; // Another method was selected, do nothing
}
if(!($method = $this->getVmPluginMethod($cart->virtuemart_shipmentmethod_id))) {
return NULL; // Another method was selected, do nothing
}
if($this->OnSelectCheck($cart)) {
return true;
}
return false;
}
/**
* @param VirtueMartCart $cart
* @return bool|null
*/
public function plgVmOnCheckoutCheckDataShipment(VirtueMartCart $cart) {
if(!($method = $this->getVmPluginMethod($cart->virtuemart_shipmentmethod_id))) {
return null;
}
if ($method->shipment_element === VirtueMartModelZasilkovna::PLG_NAME) {
$zasMethod = ShipmentMethod::fromRandom($method);
return $zasMethod->isHdCarrier() || $this->hasPointSelected($cart->virtuemart_shipmentmethod_id);
}
return null;
}
/** Has session branch id selected
* @param string|int $methodId
* @return bool
*/
public function hasPointSelected($methodId)
{
$branchId = $this->shipmentMethodStorage->get($methodId, 'branch_id');
return !empty($branchId);
}
/**
* plgVmDisplayListFE
* This event is fired to display the pluginmethods in the cart (edit shipment/payment) for exampel
*
* @param object $cart Cart object
* @param integer $selected ID of the method selected
* @return boolean True on succes, false on failures, null when this plugin was not selected.
* On errors, JError::raiseWarning (or JError::raiseError) must be used to set a message.
*
* @author Valerie Isaksen
* @author Max Milbers
*/
public function plgVmDisplayListFEShipment(VirtueMartCart $cart, $selected, &$htmlIn) {
$js_html = '';
if ($this->getPluginMethods($cart->vendorId) === 0) {
return FALSE;
}
// DO NOT DISPLAY OPTION IF CART WEIGHT OVER GLOBAL LIMIT
$weight = $this->getOrderWeight($cart, self::DEFAULT_WEIGHT_UNIT);
$document = JFactory::getDocument();
$document->addStyleSheet($this->model->_media_url . 'css/packetery.css?v=' . filemtime($this->model->_media_path . 'css/packetery.css'));
// If user set Shipping address same as Billing we take the billing address
// (shipping address may contain other data but that is discarded)
$address = $this->getAddressFromCart($cart);
// GET CODE OF SELECTED COUNTRY
$countryCode = '';
if( isset( $address['virtuemart_country_id'] ) )
{
$countryCode = strtolower(ShopFunctions::getCountryByID($address['virtuemart_country_id'], 'country_2_code'));
}
// If the country stored in session is different from the one in the address
// we clear session variables = the pickup point is deselected
$shipmentIds = $this->model->getShipmentMethodIds();
if ($shipmentIds) {
foreach ($shipmentIds as $shipmentId) {
$countrySession = $this->shipmentMethodStorage->get($shipmentId, 'branch_country', '');
if ($countrySession !== '' && $countrySession !== $countryCode) {
$this->clearPickedDeliveryPoint($shipmentId);
}
}
}
$lang = JFactory::getLanguage();
$langCode = substr($lang->getTag(), 0, strpos($lang->getTag(), '-'));
if (!empty($this->model->errors))
{
//api key or smth is wrong - more info shows in administration
return false;
}
$html = array();
$method_name = $this->_psType . '_name';
if(!isset($address['virtuemart_country_id'])) {
$address['virtuemart_country_id'] = 0;
}
$activeCheckout = $this->checkoutModuleDetector->getActiveCheckout();
$shouldIncludeTailBlock = false;
foreach($this->methods as $key => $method) {
$zasMethod = ShipmentMethod::fromRandom($method);
$maxWeight = ($zasMethod->getGlobalMaxWeight() ?: VirtueMartModelZasilkovna::MAX_WEIGHT_DEFAULT);
$isHdCarrier = $zasMethod->isHdCarrier();
if($weight > $maxWeight)
{
continue;
}
$countries = array();
if(!empty($method->countries)) {
if(!is_array($method->countries)) {
$countries[0] = $method->countries;
}
else {
$countries = $method->countries;
}
}
// intentional type unsafe comparison, handles both string (PHP < 8.1) and int (PHP >= 8.1) returned from db
if (count($countries) && !in_array($address['virtuemart_country_id'], $countries, false)) {
continue;
}
$carrierId = $isHdCarrier ? $zasMethod->getHdCarrierId() : $zasMethod->getPPCarrierId();
if ($carrierId) {
$carrier = $this->carrierRepository->getCarrierById($carrierId);
if (!$carrier || (int)$carrier->deleted === 1 || ($countryCode !== '' && $carrier->country !== $countryCode)) {
continue;
}
}
$html[$key] = '';
if($this->checkConditions($cart, $method, $cart->cartPrices)) {
$methodSalesPrice = $this->calculateSalesPrice($cart, $method, $cart->cartPrices);
$method->$method_name = $this->renderPluginName($method);
$baseHtml = $this->getPluginHtml($method, $selected, $methodSalesPrice);
$this->renderer->setTemplate($activeCheckout->getTemplate());
$widgetVendors = (!$isHdCarrier && $carrierId)
? [self::createWidgetVendor($countryCode, null, $carrierId)]
: self::createWidgetVendorsParam(
$zasMethod,
$countryCode,
$this->carrierRepository->getAllActiveCarrierIds(true, $countryCode)
);
$this->renderer->setVariables(
[
'selectPoint' => \JText::_('PLG_VMSHIPMENT_PACKETERY_WIDGET_SELECT_POINT'),
'selectedPoint' => \JText::_('PLG_VMSHIPMENT_PACKETERY_WIDGET_SELECTED_POINT'),
'enterAddress' => \JText::_('PLG_VMSHIPMENT_PACKETERY_WIDGET_ENTER_ADDRESS'),
'widgetVendors' => json_encode($widgetVendors),
'baseHtml' => $baseHtml,
'isCountrySelected' => !empty($address['virtuemart_country_id']),
'savedBranchNameStreet' =>
(string)$this->shipmentMethodStorage->get($method->virtuemart_shipmentmethod_id, 'branch_name_street', ''),
]
);
$html[$key] = $isHdCarrier ? $baseHtml : $this->renderer->renderToString();
if (!$isHdCarrier) {
$shouldIncludeTailBlock = true;
}
}
}
if(empty($html)) {
return FALSE;
}
if ($shouldIncludeTailBlock) {
$tailBlockHtml = $this->getTailBlockHtml($activeCheckout, $countryCode, $langCode, $carrierId);
$html[$key] .= $tailBlockHtml;
}
$htmlIn[] = $html;
return TRUE;
}
/**
* @param ShipmentMethod $shipmentMethod
* @param string $countryCode
* @param array<int, int|string> $carrierIds
* @return array
*/
private static function createWidgetVendorsParam(
ShipmentMethod $shipmentMethod,
$countryCode,
array $carrierIds = []
) {
$widgetVendors = [];
$vendorGroups = $shipmentMethod->getVendorGroups();
foreach ($vendorGroups as $vendorGroup) {
if (!VendorGroups::isGroupPresentInCountry($vendorGroup, $countryCode)) {
continue;
}
$widgetVendors[] = self::createWidgetVendor($countryCode, $vendorGroup, null);
}
if (empty($widgetVendors)) {
return $widgetVendors;
}
foreach ($carrierIds as $carrierId) {
$widgetVendors[] = self::createWidgetVendor($countryCode, null, $carrierId);
}
return $widgetVendors;
}
/**
* @param string $countryCode
* @param string|null $group
* @param string|null $carrierId
* @return array<string, bool|string>
*/
private static function createWidgetVendor($countryCode, $group, $carrierId)
{
$widgetVendor = [
'country' => $countryCode,
'selected' => true,
];
if ($group !== null && $group !== VendorGroups::ZPOINT) {
$widgetVendor['group'] = $group;
}
if ($carrierId !== null) {
$widgetVendor['carrierId'] = $carrierId;
}
return $widgetVendor;
}
/**
* @param string $task
* @param array $params
* @return string
*/
protected function createSignalUrl($task, array $params = []) {
$params['task'] = $task;
$params['option'] = 'com_virtuemart';
$params['view'] = 'plugin';
$params['type'] = 'vmshipment';
$params['name'] = VirtueMartModelZasilkovna::PLG_NAME;
return Juri::base(true) . '/index.php?' . http_build_query($params);
}
/**
* @param VirtueMartCart $cart
*/
public function plgVmOnUpdateCart(VirtueMartCart $cart) {
$virtuemartShipmentMethodId = $cart->virtuemart_shipmentmethod_id;
if (empty($virtuemartShipmentMethodId)) {
return null; // shipping method not selected by customer
}
$method = $this->getVmPluginMethod($virtuemartShipmentMethodId);
if (empty($method) || $method->shipment_element !== VirtueMartModelZasilkovna::PLG_NAME) {
$this->clearPickedDeliveryPoint($virtuemartShipmentMethodId);
return null; // not Packetery method
}
$address = $this->getAddressFromCart($cart);
if (empty($address) || empty($address['virtuemart_country_id'])) {
$this->clearPickedDeliveryPoint($virtuemartShipmentMethodId);
return null; // destination country not specified yet
}
$countryCode = strtolower(ShopFunctions::getCountryByID($address['virtuemart_country_id'], 'country_2_code'));
$sessionCountry = $this->shipmentMethodStorage->get($virtuemartShipmentMethodId, 'branch_country');
if ($sessionCountry && $countryCode !== $sessionCountry) {
$this->clearPickedDeliveryPoint($virtuemartShipmentMethodId);
$cart->virtuemart_shipmentmethod_id = null; // makes selected shipping method disappear
}
$zasMethod = ShipmentMethod::fromRandom($method);
if ($zasMethod->isHdCarrier()) {
$this->clearPickedDeliveryPoint($virtuemartShipmentMethodId);
$hdCarrier = $this->carrierRepository->getCarrierById($zasMethod->getHdCarrierId());
if (!$hdCarrier || (int)$hdCarrier->deleted === 1 || $hdCarrier->country !== $countryCode) {
$cart->virtuemart_shipmentmethod_id = null; // makes selected shipping method disappear
}
}
}
/**
* This method is fired when showing the order details in the backend.
* It displays the shipment-specific data.
* NOTE, this plugin should NOT be used to display form fields, since it's called outside
* a form! Use plgVmOnUpdateOrderBE() instead!
*
* @param integer $virtuemart_order_id The order ID
* @param integer $virtuemart_shipmentmethod_id The order shipment method ID
* @return mixed Null for shipments that aren't active, text (HTML) otherwise
* @author Valerie Isaksen
*/
public function plgVmOnShowOrderBEShipment($virtuemart_order_id, $virtuemart_shipmentmethod_id) {
if(!($this->selectedThisByMethodId($virtuemart_shipmentmethod_id))) {
return NULL;
}
$document = JFactory::getDocument();
$document->addStyleSheet(
sprintf('%smedia/com_zasilkovna/media/css/admin.css?v=%s',
JUri::root(),
filemtime(JPATH_ROOT . '/media/com_zasilkovna/media/css/admin.css')
));