shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

econt_delivery.php (9621B)


      1 <?php
      2 
      3 /** @noinspection PhpUndefinedClassInspection */
      4 
      5 /**
      6  * @property Loader $load
      7  * @property DB $db
      8  * @property Config $config
      9  * @property Language $language
     10  * @property Session $session
     11  * @property ModelSettingSetting $model_setting_setting
     12  * @property \Cart\Cart $cart
     13  * @property ModelAccountOrder model_account_order
     14  */
     15 class ModelExtensionShippingEcontDelivery extends Model {
     16 
     17     public function getQuote($address) {
     18         $geoZoneId = intval($this->config->get('shipping_econt_delivery_geo_zone_id'));
     19         if ($geoZoneId !== 0) {
     20             $result = $this->db->query(sprintf("
     21                 SELECT
     22                     COUNT(z.zone_to_geo_zone_id) AS zoneIdsCount
     23                 FROM `%s`.`%szone_to_geo_zone` AS z
     24                 WHERE TRUE
     25                     AND z.geo_zone_id = {$geoZoneId}
     26                     AND z.country_id = %d
     27                     AND z.zone_id IN (0, %d)
     28                 LIMIT 1
     29             ",
     30                 DB_DATABASE,
     31                 DB_PREFIX,
     32                 $address['country_id'],
     33                 $address['zone_id']
     34             ));
     35             if (intval($result->row['zoneIdsCount']) <= 0) return array();
     36         }
     37         $this->load->language('extension/shipping/econt_delivery');
     38         if($this->request->request['route'] == 'checkout/shipping_method') {
     39             if($this->cart->customer && $this->cart->customer->getEmail()) {
     40                 $email = $this->cart->customer->getEmail();
     41                 $phone = $this->cart->customer->getTelephone();
     42             } else {
     43                 $email = $this->session->data['guest']['email'];
     44                 $phone = $this->session->data['guest']['telephone'];
     45             }
     46 
     47             @$frameParams = array(
     48                 'id_shop' => intval(@reset(explode('@',$this->config->get('shipping_econt_delivery_private_key')))),
     49                 'order_weight' => $this->cart->getWeight(),
     50                 'order_total' => $this->getOrderTotal(),
     51                 'order_currency' => $this->session->data['currency'],
     52                 'customer_company' => $this->session->data['shipping_address']['company'],
     53                 'customer_name' => "{$this->session->data['shipping_address']['firstname']} {$this->session->data['shipping_address']['lastname']}",
     54                 'customer_phone' =>  $phone,
     55                 'customer_email' => $email,
     56                 'customer_country' => $this->session->data['shipping_address']['iso_code_3'],
     57                 'customer_city_name' => $this->session->data['shipping_address']['city'],
     58                 'customer_post_code' => $this->session->data['shipping_address']['postcode'],
     59                 'customer_address' => $this->session->data['shipping_address']['address_1'].' '.$this->session->data['shipping_address']['address_2'],
     60             );
     61             $officeCode = trim(@$this->session->data['econt_delivery']['customer_info']['office_code']);
     62             if (!empty($officeCode)) $frameParams['customer_office_code'] = $officeCode;
     63             $zip = trim(@$this->session->data['econt_delivery']['customer_info']['zip']);
     64             if (!empty($zip)) $frameParams['customer_zip'] = $zip;
     65 
     66             $this->load->model('setting/setting');
     67             $settings = $this->model_setting_setting->getSetting('shipping_econt_delivery');
     68             $deliveryBaseURL = $settings['shipping_econt_delivery_system_url'];
     69             $frameURL = $deliveryBaseURL.'/customer_info.php?'.http_build_query($frameParams,null,'&');
     70             $deliveryMethodTxt = $this->language->get('text_delivery_method_description');
     71             $deliveryMethodPriceCD = $this->language->get('text_delivery_method_description_cd');
     72 
     73             ?>
     74             <script>
     75                 (function($){
     76                     var $econtRadio = $('input:radio[value=\'econt_delivery.econt_delivery\']');
     77                     var $hiddenTextArea = $('<textarea style="display:none" name="econt_delivery_shipping_info"></textarea>').appendTo($econtRadio.parent().parent());
     78                     $econtRadio.parent().contents().each(function(i,el){if(el.nodeType == 3) el.nodeValue = '';});//zabursvane na originalniq text
     79                     var $econtLabelText = $('<span></span>').text(<?php echo json_encode($deliveryMethodTxt)?>);
     80                     $econtRadio.after($econtLabelText);
     81                     var shippingInfo = null;
     82                     var $frame = null;
     83                     $econtRadio.click(function(){
     84                         if(!$frame) {
     85                             $frame = $('<iframe style="width:100%;height:612px;border:none;margin-top: 15px;" src="<?php echo $frameURL?>"></iframe>');
     86                             $econtRadio.parent().parent().append($frame);
     87                         }
     88                     });
     89                     $('input:radio[name=shipping_method]').change(function(){
     90                         if(!$econtRadio.is(':checked')) {
     91                             $frame.remove();
     92                             $frame = null;
     93                         }
     94                     });
     95                     if($econtRadio.is(':checked')) $econtRadio.trigger('click');
     96                     $(window).unbind('message.econtShipping');
     97                     $(window).bind('message.econtShipping',function(e){
     98                         if(e.originalEvent.origin.indexOf('<?php echo $deliveryBaseURL?>') == 0) {
     99                             if(e.originalEvent.data.shipment_error) {
    100                                 alert(e.originalEvent.data.shipment_error)
    101                             } else {
    102                                 shippingInfo = e.originalEvent.data;
    103                                 $frame.remove();
    104                                 $frame = null;
    105                                 console.log(shippingInfo);
    106                                 var labelTxt = <?php echo json_encode($deliveryMethodTxt)?> + ' - ' + shippingInfo.shipping_price + shippingInfo.shipping_price_currency_sign;
    107                                 if(shippingInfo.shipping_price != shippingInfo.shipping_price_cod) {
    108                                     labelTxt += ' (+ ' + (shippingInfo.shipping_price_cod - shippingInfo.shipping_price).toFixed(2) + shippingInfo.shipping_price_currency_sign + ' ' + <?php echo json_encode($deliveryMethodPriceCD)?> + ')'
    109                                 }
    110                                 $econtLabelText.text(labelTxt);
    111                                 $hiddenTextArea.val(JSON.stringify(e.originalEvent.data));
    112                             }
    113                         }
    114                     });
    115                 })(jQuery);
    116             </script>
    117             <?php
    118         }
    119 
    120         return array(
    121             'code' => 'econt_delivery',
    122             'title' => $this->language->get('text_delivery_method_title'),
    123             'quote' => array(
    124                 'econt_delivery' => array(
    125                     'code' => 'econt_delivery.econt_delivery',
    126                     'title' => $this->language->get('text_delivery_method_description'),
    127                     'cost' => @floatval(($this->session->data['payment_method']['code'] === 'cod' ? $this->session->data['econt_delivery']['customer_info']['shipping_price_cod'] : $this->session->data['econt_delivery']['customer_info']['shipping_price'])),
    128                     'tax_class_id' => 0,
    129                     'text' => ''
    130                 )
    131             ),
    132             'sort_order' => intval($this->config->get('shipping_econt_delivery_sort_order')),
    133             'error' => false
    134         );
    135     }
    136 
    137     public function getOrderTotal() {
    138         $products = $this->cart->getProducts();
    139         foreach ($products as $product) {
    140             $product_total = 0;
    141             foreach ($products as $product_2) if ($product_2['product_id'] == $product['product_id']) $product_total += $product_2['quantity'];
    142             $option_data = array();
    143             foreach ($product['option'] as $option) {
    144                 $option_data[] = array(
    145                     'product_option_id'       => $option['product_option_id'],
    146                     'product_option_value_id' => $option['product_option_value_id'],
    147                     'name'                    => $option['name'],
    148                     'value'                   => $option['value'],
    149                     'type'                    => $option['type']
    150                 );
    151             }
    152         }
    153         $this->load->model('setting/extension');
    154         $totals = array();
    155         $taxes = $this->cart->getTaxes();
    156         $total = 0;
    157         $total_data = array(
    158             'totals' => &$totals,
    159             'taxes'  => &$taxes,
    160             'total'  => &$total
    161         );
    162         $sort_order = array();
    163         $results = $this->model_setting_extension->getExtensions('total');
    164         foreach ($results as $key => $value) $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
    165         array_multisort($sort_order, SORT_ASC, $results);
    166         foreach ($results as $result) {
    167             if ($this->config->get('total_' . $result['code'] . '_status')) {
    168                 $this->load->model('extension/total/' . $result['code']);
    169                 $this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
    170             }
    171         }
    172         $sort_order = array();
    173         foreach ($totals as $key => $value) $sort_order[$key] = $value['sort_order'];
    174         array_multisort($sort_order, SORT_ASC, $totals);
    175 
    176         return array_reduce($totals, function($total, $currentRow) {
    177             if (!in_array($currentRow['code'], array('shipping', 'total'))) $total += $currentRow['value'];
    178             return $total;
    179         }, 0);
    180     }
    181 
    182 }