コード例 #1
0
ファイル: seur.php プロジェクト: tomideru/PrestaShop-modules
    public function hookAdminOrder($params)
    {
        $versionSpecialClass = '';
        if (!file_exists(_PS_MODULE_DIR_ . 'seur/img/logonew_32.png') && file_exists(_PS_MODULE_DIR_ . 'seur/img/logonew.png')) {
            ImageManager::resize(_PS_MODULE_DIR_ . 'seur/img/logonew.png', _PS_MODULE_DIR_ . 'seur/img/logonew_32.png', 32, 32, 'png');
        }
        if (version_compare(_PS_VERSION_, '1.5', '<')) {
            $versionSpecialClass = 'ver14';
        }
        $this->displayWarning();
        if (Configuration::get('SEUR_Configured') == 1) {
            $cookie = $this->context->cookie;
            $token = Tools::getValue('token');
            $back = Tools::safeOutput($_SERVER['REQUEST_URI']);
            $seur_carriers = SeurLib::getSeurCarriers(false);
            $ids_seur_carriers = array();
            foreach ($seur_carriers as $value) {
                $ids_seur_carriers[] = (int) $value['id'];
            }
            $order = new Order((int) $params['id_order']);
            $address_saved = DB::getInstance()->getValue('
				SELECT `id_address_delivery`
				FROM `' . _DB_PREFIX_ . 'seur_order`
				WHERE `id_order` = "' . (int) $order->id . '"
			');
            if ($address_saved === '0') {
                $this->context->smarty->assign('pickup_point_warning', true);
            }
            if (!Validate::isLoadedObject($order)) {
                return false;
            }
            $delivery_price = $order_weigth = 0;
            $products = $order->getProductsDetail();
            foreach ($products as $product) {
                $order_weigth += (double) $product['product_weight'] * (double) $product['product_quantity'];
            }
            $order_weigth = $order_weigth < 1.0 ? 1.0 : (double) $order_weigth;
            $customer = new Customer((int) $order->id_customer);
            $address_delivery = new Address((int) $order->id_address_delivery, (int) $cookie->id_lang);
            if (!Validate::isLoadedObject($address_delivery)) {
                return false;
            }
            $iso_country = Country::getIsoById((int) $address_delivery->id_country);
            if ($iso_country == 'PT') {
                $post_code = explode(' ', $address_delivery->postcode);
                $post_code = $post_code[0];
            } else {
                $post_code = $address_delivery->postcode;
            }
            $international_orders = SeurLib::getConfigurationField('international_orders');
            $date_calculate = strtotime('-14 day', strtotime(date('Y-m-d')));
            $date_display = date('Y-m-d H:m:i', $date_calculate);
            if (strtotime($order->date_add) > strtotime($date_display) && in_array((int) $order->id_carrier, $ids_seur_carriers)) {
                if (!$international_orders && ($iso_country == 'ES' || $iso_country == 'PT' || $iso_country == 'AD') || $international_orders) {
                    if (!SeurLib::getSeurOrder((int) $order->id)) {
                        SeurLib::setSeurOrder((int) $order->id, 1, $order_weigth, null);
                    } elseif (Tools::getValue('numBultos') && Tools::getValue('pesoBultos')) {
                        SeurLib::setSeurOrder((int) $order->id, (int) Tools::getValue('numBultos'), str_replace(',', '.', Tools::getValue('pesoBultos')), null);
                    }
                    $order_data = SeurLib::getSeurOrder((int) $order->id);
                    $response_post_code = Town::getTowns($post_code);
                    $order_weigth = (double) $order_weigth != $order_data['peso_bultos'] ? (double) $order_data['peso_bultos'] : (double) $order_weigth;
                    if (is_object($response_post_code)) {
                        $towns = array();
                        $num = (int) $response_post_code->attributes()->NUM[0];
                        for ($i = 1; $i <= $num; $i++) {
                            $name = 'REG' . $i;
                            $towns[] = utf8_decode((string) $response_post_code->{$name}->NOM_POBLACION);
                        }
                    }
                    $name = $address_delivery->firstname . ' ' . $address_delivery->lastname;
                    $direccion = $address_delivery->address1 . ' ' . $address_delivery->address2;
                    $newcountry = new Country((int) $address_delivery->id_country, (int) $cookie->id_lang);
                    $iso_merchant = SeurLib::getMerchantField('country');
                    $rate_data = array('town' => $address_delivery->city, 'peso' => (double) $order_weigth, 'post_code' => $post_code, 'bultos' => $order_data['numero_bultos'], 'ccc' => SeurLib::getMerchantField('ccc'), 'franchise' => SeurLib::getMerchantField('franchise'), 'iso' => $newcountry->iso_code, 'iso_merchant' => $iso_merchant, 'id_employee' => $cookie->id_employee, 'token' => Tools::getAdminTokenLite('AdminOrders'), 'back' => $back);
                    $order_messages_str = '';
                    $info_adicional_str = $address_delivery->other;
                    $order_messages = Message::getMessagesByOrderId((int) $params['id_order']);
                    if (is_array($order_messages)) {
                        foreach ($order_messages as $order_messag_tmp) {
                            $order_messages_str .= "\n" . $order_messag_tmp['message'];
                        }
                        if (substr_count($order_messages_str, "\n") > 5) {
                            $order_messages_str = str_replace(array("\r", "\n"), ' | ', $order_messages_str);
                        }
                        if (Tools::strlen($order_messages_str) > 250) {
                            $order_messages_str = Tools::substr($order_messages_str, 0, 247) . '...';
                        }
                        $order_messages_str = trim($order_messages_str);
                    }
                    if (!empty($order_messages_str)) {
                        $info_adicional_str = $order_messages_str;
                    }
                    $label_data = array('pedido' => sprintf('%06d', (int) $order->id), 'total_bultos' => $order_data['numero_bultos'], 'total_kilos' => (double) $order_weigth, 'direccion_consignatario' => $direccion, 'consignee_town' => $address_delivery->city, 'codPostal_consignatario' => $post_code, 'telefono_consignatario' => !empty($address_delivery->phone_mobile) ? $address_delivery->phone_mobile : $address_delivery->phone, 'movil' => $address_delivery->phone_mobile, 'name' => $name, 'companyia' => !empty($address_delivery->company) ? $address_delivery->company : '', 'email_consignatario' => Validate::isLoadedObject($customer) ? $customer->email : '', 'dni' => $address_delivery->dni, 'info_adicional' => $info_adicional_str, 'country' => $newcountry->name, 'iso' => $newcountry->iso_code, 'iso_merchant' => $iso_merchant, 'admin_dir' => utf8_encode(_PS_ADMIN_DIR_), 'id_employee' => $cookie->id_employee, 'token' => Tools::getAdminTokenLite('AdminOrders'), 'back' => $back);
                    if (strcmp($order->module, 'seurcashondelivery') == 0) {
                        $rate_data['reembolso'] = (double) $order->total_paid;
                        $label_data['reembolso'] = (double) $order->total_paid;
                    }
                    $carrier_pos = SeurLib::getSeurCarrier('SEP');
                    $datospos = '';
                    if ((int) $order->id_carrier == $carrier_pos['id']) {
                        $datospos = SeurLib::getOrderPos((int) $order->id_cart);
                        if (!empty($datospos)) {
                            $label_data = array('pedido' => sprintf('%06d', (int) $order->id), 'total_bultos' => $order_data['numero_bultos'], 'total_kilos' => (double) $order_weigth, 'direccion_consignatario' => $direccion, 'consignee_town' => $datospos['city'], 'codPostal_consignatario' => $datospos['postal_code'], 'telefono_consignatario' => !empty($address_delivery->phone_mobile) ? $address_delivery->phone_mobile : $address_delivery->phone, 'movil' => $address_delivery->phone_mobile, 'name' => $name, 'companyia' => $datospos['company'], 'email_consignatario' => Validate::isLoadedObject($customer) ? $customer->email : '', 'dni' => $address_delivery->dni, 'info_adicional' => $info_adicional_str, 'country' => $newcountry->name, 'iso' => $newcountry->iso_code, 'cod_centro' => $datospos['id_seur_pos'], 'iso_merchant' => $iso_merchant);
                            $rate_data['cod_centro'] = $datospos['id_seur_pos'];
                        }
                    }
                    if ($iso_country == 'ES' || $iso_country == 'PT' || $iso_country == 'AD') {
                        $xml = Rate::getPrivateRate($rate_data);
                        if (is_object($xml)) {
                            foreach ($xml as $tarifa) {
                                $delivery_price += (double) $tarifa->VALOR;
                            }
                        }
                    }
                    if (Tools::getValue('submitLabel')) {
                        if ($this->isPrinted((int) $order->id)) {
                            $success = true;
                        } else {
                            $success = Label::createLabels($label_data, 'pdf');
                        }
                        if ($success === true) {
                            if (!$this->setAsPrinted((int) $order->id)) {
                                $this->context->smarty->assign('error', $this->l('Could not set printed value for this order'));
                            } else {
                                $this->printLabel((int) $order->id, 'pdf');
                            }
                        } else {
                            $this->context->smarty->assign('error', $success);
                        }
                    }
                    if (Tools::getValue('submitPrint')) {
                        if ($this->isPrinted((int) $order->id, true)) {
                            $success = true;
                        } else {
                            $success = Label::createLabels($label_data, 'zebra');
                        }
                        if ($success === true) {
                            if (!$this->setAsPrinted((int) $order->id, true)) {
                                $this->context->smarty->assign('error', $this->l('Could not set printed value for this order'));
                            } else {
                                $this->printLabel((int) $order->id, 'txt');
                            }
                        } else {
                            $this->context->smarty->assign('error', $success);
                        }
                    }
                    $seur_carriers = SeurLib::getSeurCarriers(false);
                    $pickup = Pickup::getLastPickup();
                    if (!empty($pickup)) {
                        $pickup_date = explode(' ', $pickup['date']);
                        $pickup_date = $pickup_date[0];
                    }
                    $address_error = 0;
                    if (!empty($towns) && !in_array(mb_strtoupper($this->replaceAccentedChars($address_delivery->city), 'UTF-8'), $towns)) {
                        $address_error = 1;
                    }
                    $pickup_s = 0;
                    if ($pickup && strtotime(date('Y-m-d')) == strtotime($pickup_date)) {
                        $pickup_s = 1;
                    }
                    $state = Expedition::getExpeditions(array('reference_number' => sprintf('%06d', (int) $order->id)));
                    $is_empty_state = false;
                    $xml_s = false;
                    if (!empty($state->out)) {
                        $is_empty_state = true;
                    } else {
                        $string_xml = htmlspecialchars_decode($state->out);
                        $string_xml = str_replace('&', '&amp; ', $string_xml);
                        $xml_s = simplexml_load_string($string_xml);
                    }
                    $rate_data_ajax = Tools::jsonEncode($rate_data);
                    $path = '../modules/seur/js/';
                    $file = (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . __PS_BASE_URI__ . 'modules/seur/files/deliveries_labels/' . sprintf('%06d', (int) $order->id) . '.txt';
                    $filePath = _PS_MODULE_DIR_ . 'seur/files/deliveries_labels/' . sprintf('%06d', (int) $order->id) . '.txt';
                    $label_data['file'] = $file;
                    $this->context->smarty->assign(array('path' => $this->_path, 'request_uri' => $_SERVER['REQUEST_URI'], 'module_instance' => $this, 'address_error' => $address_error, 'address_error_message' => $this->l('Addressess error, please check the customer address.'), 'pickup_s' => $pickup_s, 'pickup' => $pickup, 'isEmptyState' => $is_empty_state, 'xml' => $xml_s, 'order_data' => $order_data, 'iso_country' => $iso_country, 'order_weigth' => $order_weigth, 'delivery_price' => $delivery_price, 'rate_data_ajax' => $rate_data_ajax, 'js_path' => $path, 'token' => $token, 'order' => $order, 'label_data' => $label_data, 'fileExists' => file_exists($filePath), 'file' => $file, 'datospos' => $datospos, 'versionSpecialClass' => $versionSpecialClass, 'configured' => (int) Configuration::get('SEUR_Configured'), 'printed' => (bool) ($this->isPrinted((int) $order->id) || $this->isPrinted((int) $order->id, true))));
                    return $this->display(__FILE__, 'views/templates/admin/orders.tpl');
                }
            }
        } else {
            $this->context->smarty->assign(array('configured' => Configuration::get('SEUR_Configured'), 'path' => $this->_path, 'configuration_warning_message' => $this->l('Please, first configure your SEUR module as a merchant.')));
            return $this->display(__FILE__, 'views/templates/admin/orders.tpl');
        }
    }
コード例 #2
0
ファイル: Label.php プロジェクト: tomideru/PrestaShop-modules
    public static function createLabels($label_data, $tipo)
    {
        try {
            if (Validate::isFileName($label_data['pedido'])) {
                $label_name = $label_data['pedido'];
            } else {
                $module_instance = Module::getInstanceByName('seur');
                return SeurLib::displayErrors($label_data['pedido'] . ' ' . $module_instance->l('could not be used as file name', 'Label'));
            }
            $sc_options = array('connection_timeout' => 30);
            $soap_client = new SoapClient((string) Configuration::get('SEUR_URLWS_ET'), $sc_options);
            $merchant_data = SeurLib::getMerchantData();
            $notification = SeurLib::getConfigurationField('notification_advice_radio');
            $advice_checkbox = SeurLib::getConfigurationField('advice_checkbox');
            $distribution_checkbox = SeurLib::getConfigurationField('distribution_checkbox');
            $servicio = 31;
            $producto = 2;
            $mercancia = false;
            $claveReembolso = '';
            $valorReembolso = '';
            if (SeurLib::getConfigurationField('international_orders') == 1 && ($label_data['iso'] != 'ES' && $label_data['iso'] != 'PT' && $label_data['iso'] != 'AD')) {
                $servicio = 77;
                $producto = 70;
                $mercancia = true;
                $label_data['total_bultos'] = 1;
            }
            if (isset($label_data['reembolso']) && ($label_data['iso'] == 'ES' || $label_data['iso'] == 'PT' || $label_data['iso'] == 'AD')) {
                $claveReembolso = 'f';
                $valorReembolso = (double) $label_data['reembolso'];
            }
            if (isset($label_data['cod_centro']) && ($label_data['iso'] == 'ES' || $label_data['iso'] == 'PT' || $label_data['iso'] == 'AD')) {
                $servicio = 1;
                $producto = 48;
            }
            $total_weight = $label_data['total_kilos'];
            $total_packages = $label_data['total_bultos'];
            $pesoBulto = $total_weight / $total_packages;
            if ($pesoBulto < 1) {
                $pesoBulto = 1;
                $total_weight = $total_packages;
            }
            $cont = 0;
            $xml = '<?xml version="1.0" encoding="ISO-8859-1"?><root><exp>';
            for ($i = 0; $i <= (double) $total_packages - 1; $i++) {
                $cont++;
                $xml .= '<bulto>
							<ci>' . (int) $merchant_data['cit'] . '</ci>
							<nif>' . pSQL($merchant_data['nif_dni']) . '</nif>
							<ccc>' . (int) $merchant_data['ccc'] . '</ccc>
							<servicio>' . pSQL($servicio) . '</servicio>
							<producto>' . pSQL($producto) . '</producto>';
                if ($mercancia) {
                    $xml .= '<id_mercancia>382</id_mercancia>';
                }
                $xml .= '<cod_centro></cod_centro>
							<total_bultos>' . pSQL($total_packages) . '</total_bultos>
							<total_kilos>' . pSQL($total_weight) . '</total_kilos>
							<pesoBulto>' . pSQL($pesoBulto) . '</pesoBulto>
							<observaciones>' . pSQL($label_data['info_adicional']) . '</observaciones>
							<referencia_expedicion>' . pSQL($label_data['pedido']) . '</referencia_expedicion>
							<ref_bulto>' . pSQL($label_data['pedido'] . sprintf('%03d', (int) $i + 1)) . '</ref_bulto>
							<clavePortes>F</clavePortes>
							<clavePod></clavePod>
							<claveReembolso>' . pSQL($claveReembolso) . '</claveReembolso>
							<valorReembolso>' . pSQL($valorReembolso) . '</valorReembolso>
							<libroControl></libroControl>
							<nombre_consignatario>' . pSQL($label_data['name']) . '</nombre_consignatario>
							<direccion_consignatario>' . pSQL($label_data['direccion_consignatario']) . '</direccion_consignatario>
							<tipoVia_consignatario>CL</tipoVia_consignatario>
							<tNumVia_consignatario>N</tNumVia_consignatario>
							<numVia_consignatario>.</numVia_consignatario>
							<escalera_consignatario>.</escalera_consignatario>
							<piso_consignatario>.</piso_consignatario>
							<puerta_consignatario>.</puerta_consignatario>
							<poblacion_consignatario>' . pSQL($label_data['consignee_town']) . '</poblacion_consignatario>';
                if (!empty($label_data['codPostal_consignatario'])) {
                    $xml .= '<codPostal_consignatario>' . pSQL($label_data['codPostal_consignatario']) . '</codPostal_consignatario>';
                }
                $xml .= '   <pais_consignatario>' . pSQL($label_data['iso']) . '</pais_consignatario>
							<codigo_pais_origen>' . pSQL($label_data['iso_merchant']) . '</codigo_pais_origen>
							<email_consignatario>' . pSQL($label_data['email_consignatario']) . '</email_consignatario>
							<sms_consignatario>' . ((int) $notification ? pSQL($label_data['movil']) : '') . '</sms_consignatario>
							<test_sms>' . ((int) $notification ? 'S' : 'N') . '</test_sms>
							<test_preaviso>' . ((int) $advice_checkbox ? 'S' : 'N') . '</test_preaviso>
							<test_reparto>' . ((int) $distribution_checkbox ? 'S' : 'N') . '</test_reparto>
							<test_email>' . ((int) $notification ? 'N' : 'S') . '</test_email>
							<eci>N</eci>
							<et>N</et>
							<telefono_consignatario>' . pSQL($label_data['telefono_consignatario']) . '</telefono_consignatario>
							<atencion_de>' . pSQL($label_data['companyia']) . '</atencion_de>
						 </bulto>
						 ';
            }
            $xml .= '</exp></root>';
            $xml_name = (int) $merchant_data['franchise'] . '_' . (int) $merchant_data['cit'] . '_' . date('dmYHi') . '.xml';
            $make_pickup = false;
            $auto = false;
            $pickup_data = Pickup::getLastPickup();
            if (!empty($pickup_data)) {
                $datepickup = explode(' ', $pickup_data['date']);
                $datepickup = $datepickup[0];
                if (strtotime(date('Y-m-d')) != strtotime($datepickup)) {
                    $make_pickup = true;
                }
                if (SeurLib::getConfigurationField('pickup') == 0) {
                    $auto = true;
                }
            }
            if ($tipo == 'pdf') {
                $data = array('in0' => $merchant_data['user'], 'in1' => $merchant_data['pass'], 'in2' => $xml, 'in3' => $xml_name, 'in4' => $merchant_data['nif_dni'], 'in5' => $merchant_data['franchise'], 'in6' => '-1', 'in7' => 'prestashop');
                $response = $soap_client->impresionIntegracionPDFConECBWS($data);
                if ($response->out == 'ERROR') {
                    return SeurLib::displayErrors((string) $response->out);
                }
                if ($response->out->mensaje != 'OK') {
                    return SeurLib::displayErrors((string) $response->out->mensaje);
                } else {
                    $pdf = base64_decode($response->out->PDF);
                    if (is_writable(_PS_MODULE_DIR_ . 'seur/files/deliveries_labels/')) {
                        file_put_contents(_PS_MODULE_DIR_ . 'seur/files/deliveries_labels/' . $label_name . '.pdf', $pdf);
                    }
                    SeurLib::setSeurOrder($label_data['pedido'], $total_packages, $total_weight, 'PDF');
                    if ($make_pickup && $auto) {
                        Pickup::createPickup();
                    }
                }
            } elseif ($tipo == 'zebra') {
                $data = array('in0' => pSQL($merchant_data['user']), 'in1' => pSQL($merchant_data['pass']), 'in2' => 'ZEBRA', 'in3' => 'LP2844-Z', 'in4' => '2C', 'in5' => $xml, 'in6' => $xml_name, 'in7' => pSQL($merchant_data['nif_dni']), 'in8' => pSQL($merchant_data['franchise']), 'in9' => '-1', 'in10' => 'prestashop');
                $response = $soap_client->impresionIntegracionConECBWS($data);
                if ($response->out == 'ERROR' || $response->out->mensaje != 'OK') {
                    return SeurLib::displayErrors('Error al crear el envio y la etiqueta: ' . $response->out->mensaje);
                } else {
                    if (is_writable(_PS_MODULE_DIR_ . 'seur/files/deliveries_labels/')) {
                        file_put_contents(_PS_MODULE_DIR_ . 'seur/files/deliveries_labels/' . pSQL($label_name) . '.txt', (string) $response->out->traza);
                    }
                    SeurLib::setSeurOrder(pSQL($label_data['pedido']), (double) $total_packages, (double) $total_weight, 'zebra');
                    if ($make_pickup && $auto) {
                        Pickup::createPickup();
                    }
                }
            }
        } catch (PrestaShopException $e) {
            $e->displayMessage();
        }
        return true;
    }