Prestashop | Aggiungere 2 nuovi campi SDI e PEC per la fatturazione elettronica

15/02/2019

Guido

Prestashop | Aggiungere 2 nuovi campi SDI e PEC per la fatturazione elettronica

Dal 2019 è divenuta obbligatoria l'emissione della fattura elettronica (semprechè non si è in regime forfettario) verso tutti i clienti aziende e verso i privati  che ne facciano richiesta. A chi usa Prestashop per la fatturazione, sia direttamente che tramite un gestionale, si è presentato il problema nel richiedere al cliente il codice destinatario (SDI) e/o la pec (PEC) per poter inviare la fattura elettronica. Sappiamo che Prestashop ancora non è stato aggiornato con queste funzionalità e che non si conosce se il tutto sarà implementato in futuro.


Per poter richiedere questi dati ai propri clienti si devono aggiungere due nuovi campi e di default Prestashop 1.7 (a differenza di Opencart) non permette di aggiungere dall'admin questi nuovi campi. Così per sopperire a questa mancanza è necessario acquistare un modulo con simili funzioni o di intervenire manualmente sul codice. Io ho scelto questo seconda strada.


Per chi possiede un pò di conoscenze può facilmente farlo seguendo le mie sottostanti indicazioni. Innanzitutto queste modifiche le ho effettuate sulla versione di Prestashop 1.7.4.4 e non mi prendo nessuna colpa se non dovessero funzionare su altre versioni.


AGGIUNGERE DUE NUOVI CAMPI " SDI - PEC " NEL DATABASE TRAMITE phpMyAdmin


Entrare nella gestione del proprio database tramite phpMyAdmin ---> trovate il link all'interno di CPanel - Plesk od altro pannello hosting. Scegliere il proprio database e selezionare la tabella


ps_address


ed eseguire questa query


ALTER TABLE `ps_address` ADD COLUMN sdi VARCHAR(16) AFTER `dni`,

ADD COLUMN pec VARCHAR(128) AFTER `sdi`;


A query eseguita dovreste avere questa situazione :

Modifica dei files core di Prestashop


Adesso sarà necessario modificare 2 files core di Prestashop. Ma sappiamo bene che non è mai una cosa saggia andare a modificare direttamente i files di sistema perchè potrebbero andare in conflitto con altri moduli o che in seguito ad aggiornamenti di Prestashop vengano soprascritti, andando a perdere tutte le modifiche. Per ovviare a tutto ciò Prestashop permette di apportare queste modifiche tramite l' OVERRIDE.


Non sto qui a spiegare come funzionano gli overrides in Prestashop 1.7.x, in quanto a voi sarà solamente necessario creare questi 2 nuovi files con il codice da me indicato.


creare il file  Address.php in override/classes/   ---> override/classes/Address.php


Aprire il file con un editor ed inserire il seguente codice :

<?php

class AdminAddressesController extends AdminAddressesControllerCore

{

    public function renderForm()

    {

        $this->fields_form = array(

            'legend' => array(

                'title' => $this->trans('Addresses', array(), 'Admin.Orderscustomers.Feature'),

                'icon' => 'icon-envelope-alt'

            ),

            'input' => array(

                array(

                    'type' => 'text_customer',

                    'label' => $this->trans('Customer', array(), 'Admin.Global'),

                    'name' => 'id_customer',

                    'required' => false,

                ),

                array(

                    'type' => 'text',

                    'label' => $this->trans('Identification number', array(), 'Admin.Orderscustomers.Feature'),

                    'name' => 'dni',

                    'required' => false,

                    'col' => '4',

                    'hint' => $this->trans('The national ID card number of this person, or a unique tax identification number.', array(), 'Admin.Orderscustomers.Feature')

                ),

                array(

                    'type' => 'text',

                    'label' => $this->trans('Address alias', array(), 'Admin.Orderscustomers.Feature'),

                    'name' => 'alias',

                    'required' => true,

                    'col' => '4',

                    'hint' => $this->trans('Invalid characters:', array(), 'Admin.Notifications.Info').' &lt;&gt;;=#{}'

                ),

                array(

                    'type' => 'textarea',

                    'label' => $this->trans('Other', array(), 'Admin.Global'),

                    'name' => 'other',

                    'required' => false,

                    'cols' => 15,

                    'rows' => 3,

                    'hint' => $this->trans('Invalid characters:', array(), 'Admin.Notifications.Info').' &lt;&gt;;=#{}'

                ),

                array(

                    'type' => 'hidden',

                    'name' => 'id_order'

                ),

                array(

                    'type' => 'hidden',

                    'name' => 'address_type',

                ),

                array(

                    'type' => 'hidden',

                    'name' => 'back'

                )

            ),

            'submit' => array(

                'title' => $this->trans('Save', array(), 'Admin.Actions'),

            )

        );

 

 

         $this->fields_value['address_type'] = (int)Tools::getValue('address_type', 1);


        $id_customer = (int)Tools::getValue('id_customer');

        if (!$id_customer && Validate::isLoadedObject($this->object)) {

            $id_customer = $this->object->id_customer;

        }

        if ($id_customer) {

            $customer = new Customer((int)$id_customer);

            $token_customer = Tools::getAdminToken('AdminCustomers'.(int)(Tab::getIdFromClassName('AdminCustomers')).(int)$this->context->employee->id);

        }


        $this->tpl_form_vars = array(

            'customer' => isset($customer) ? $customer : null,

            'tokenCustomer' => isset($token_customer) ? $token_customer : null,

            'back_url' => urldecode(Tools::getValue('back'))

        );


        // Order address fields depending on country format

        $addresses_fields = $this->processAddressFormat();

        // we use  delivery address

        $addresses_fields = $addresses_fields['dlv_all_fields'];


        // get required field

        $required_fields = AddressFormat::getFieldsRequired();


        // Merge with field required

        $addresses_fields = array_unique(array_merge($addresses_fields, $required_fields));


        $temp_fields = array();


        foreach ($addresses_fields as $addr_field_item) {

            if ($addr_field_item == 'company') {

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Company', array(), 'Admin.Global'),

                    'name' => 'company',

                    'required' => in_array('company', $required_fields),

                    'col' => '4',

                    'hint' => $this->trans('Invalid characters:', array(), 'Admin.Notifications.Info').' &lt;&gt;;=#{}'

                );

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('VAT number', array(), 'Admin.Orderscustomers.Feature'),

                    'col' => '2',

                    'name' => 'vat_number',

                    'required' => in_array('vat_number', $required_fields)

                );                

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Sdi', array(), 'Admin.Orderscustomers.Feature'),

                    'col' => '2',

                    'name' => 'sdi',

                    'required' => false

                );

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Pec', array(), 'Admin.Orderscustomers.Feature'),

                    'col' => '2',

                    'name' => 'pec',

                    'required' => false

                );


            } elseif ($addr_field_item == 'lastname') {

                if (isset($customer) &&

                    !Tools::isSubmit('submit'.strtoupper($this->table)) &&

                    Validate::isLoadedObject($customer) &&

                    !Validate::isLoadedObject($this->object)) {

                    $default_value = $customer->lastname;

                } else {

                    $default_value = '';

                }


                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Last Name', array(), 'Admin.Global'),

                    'name' => 'lastname',

                    'required' => true,

                    'col' => '4',

                    'hint' => $this->trans('Invalid characters:', array(), 'Admin.Notifications.Info').' 0-9!&amp;lt;&amp;gt;,;?=+()@#"�{}_$%:',

                    'default_value' => $default_value,

                );

            } elseif ($addr_field_item == 'firstname') {

                if (isset($customer) &&

                    !Tools::isSubmit('submit'.strtoupper($this->table)) &&

                    Validate::isLoadedObject($customer) &&

                    !Validate::isLoadedObject($this->object)) {

                    $default_value = $customer->firstname;

                } else {

                    $default_value = '';

                }


                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('First Name', array(), 'Admin.Global'),

                    'name' => 'firstname',

                    'required' => true,

                    'col' => '4',

                    'hint' => $this->trans('Invalid characters:', array(), 'Admin.Notifications.Info').' 0-9!&amp;lt;&amp;gt;,;?=+()@#"�{}_$%:',

                    'default_value' => $default_value,

                );

            } elseif ($addr_field_item == 'address1') {

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Address', array(), 'Admin.Global'),

                    'name' => 'address1',

                    'col' => '6',

                    'required' => true,

                );

            } elseif ($addr_field_item == 'address2') {

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Address', array(), 'Admin.Global').' (2)',

                    'name' => 'address2',

                    'col' => '6',

                    'required' => in_array('address2', $required_fields),

                );

            } elseif ($addr_field_item == 'postcode') {

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Zip/postal code', array(), 'Admin.Global'),

                    'name' => 'postcode',

                    'col' => '2',

                    'required' => true,

                );

            } elseif ($addr_field_item == 'city') {

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('City', array(), 'Admin.Global'),

                    'name' => 'city',

                    'col' => '4',

                    'required' => true,

                );

            } elseif ($addr_field_item == 'country' || $addr_field_item == 'Country:name') {

                $temp_fields[] = array(

                    'type' => 'select',

                    'label' => $this->trans('Country', array(), 'Admin.Global'),

                    'name' => 'id_country',

                    'required' => in_array('Country:name', $required_fields) || in_array('country', $required_fields),

                    'col' => '4',

                    'default_value' => (int)$this->context->country->id,

                    'options' => array(

                        'query' => Country::getCountries($this->context->language->id),

                        'id' => 'id_country',

                        'name' => 'name'

                    )

                );

                $temp_fields[] = array(

                    'type' => 'select',

                    'label' => $this->trans('State', array(), 'Admin.Global'),

                    'name' => 'id_state',

                    'required' => false,

                    'col' => '4',

                    'options' => array(

                        'query' => array(),

                        'id' => 'id_state',

                        'name' => 'name'

                    )

                );

            } elseif ($addr_field_item == 'phone') {

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Home phone', array(), 'Admin.Global'),

                    'name' => 'phone',

                    'required' => in_array('phone', $required_fields),

                    'col' => '4',

                );

            } elseif ($addr_field_item == 'phone_mobile') {

                $temp_fields[] = array(

                    'type' => 'text',

                    'label' => $this->trans('Mobile phone', array(), 'Admin.Global'),

                    'name' => 'phone_mobile',

                    'required' =>  in_array('phone_mobile', $required_fields),

                    'col' => '4',

                );

            }

        }


        // merge address format with the rest of the form

        array_splice($this->fields_form['input'], 3, 0, $temp_fields);


        return AdminController::renderForm();

        //return parent::renderForm();


    }

  

}

Poi salvate il file.


Ora andiamo a creare il secondo file.


creare il file  AdminAddressesController.php in override/controllers/admin  ---> override/controllers/admin/AdminAddressesController.php


Anche per questo file, come il precedente, andiamo ad aprirlo con editor ed inseriamo il seguente codice



<?php 

class Address extends AddressCore

{

    public $sdi;

    public $pec;

     

    public static $definition = array(

    

        'table' => 'address',

        'primary' => 'id_address',

        'fields' => array(

     

            'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),

            'id_manufacturer' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),

            'id_supplier' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),

            'id_warehouse' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),

            'id_country' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),

            'id_state' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId'),

            'alias' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32),

            'company' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),

            'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),

            'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),

            'vat_number' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),

               'sdi' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 16),

            'pec' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 128),

            'address1' => array('type' => self::TYPE_STRING, 'validate' => 'isAddress', 'required' => true, 'size' => 128),

            'address2' => array('type' => self::TYPE_STRING, 'validate' => 'isAddress', 'size' => 128),

            'postcode' => array('type' => self::TYPE_STRING, 'validate' => 'isPostCode', 'size' => 12),

            'city' => array('type' => self::TYPE_STRING, 'validate' => 'isCityName', 'required' => true, 'size' => 64),

            'other' => array('type' => self::TYPE_STRING, 'validate' => 'isMessage', 'size' => 300),

            'phone' => array('type' => self::TYPE_STRING, 'validate' => 'isPhoneNumber', 'size' => 32),

            'phone_mobile' => array('type' => self::TYPE_STRING, 'validate' => 'isPhoneNumber', 'size' => 32),

            'dni' => array('type' => self::TYPE_STRING, 'validate' => 'isDniLite', 'size' => 16),

            'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),

            'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),

            'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),

        ),

    );


    protected $webserviceParameters = array(

        'objectsNodeName' => 'addresses',

        'fields' => array(

            'id_customer' => array('xlink_resource'=> 'customers'),

            'id_manufacturer' => array('xlink_resource'=> 'manufacturers'),

            'id_supplier' => array('xlink_resource'=> 'suppliers'),

            'id_warehouse' => array('xlink_resource'=> 'warehouse'),

            'id_country' => array('xlink_resource'=> 'countries'),

            'id_state' => array('xlink_resource'=> 'states'),

        ),

    );    

}

Poi salvate il file.


Ora dobbiamo eliminare un file che poi sarà rigenerato automaticamente dal sistema. Se qualcuno a paura di eliminare il file può anche solo rinominarlo:


eliminare o rinominare  class_index.php  in var/cache/prod/  ---->  var/cache/prod/class_index.php


A questo punto dobbiamo entrare in admin del nostro ecommerce Prestashop ed andare in Internazionale ---> Località e nel tab Nazione scegliere Italia ed andare in modifica


noi avremo questo :




e dopo il campo vat_number aggiungere i due nuovi campi "SDI" e "PEC" come in questa immagine :

Salviamo il tutto.


Prima di verificare il funzionamento cancelliamo la cache da Parametri Avanzati ---> Prestazioni.


 Ora dovrebbe funzionare il tutto come a me :



Spero che avete trovato molto utili queste modifiche.


Se siete interessati ad un sito ecommerce Prestashop potete contattare la web agency di Viterbo Studiowebcart 

LINKS

E-commerce Prestashop  -  E-commerce Opencart

Siti web aziendali  -  Siti web per professionisti

Assistenza e consulenza a 360°

CHI SONO

Con tantissimi anni di esperienza informatica e di conoscenza web, mi propongo per la consulenza e creazione di siti ecommerce Prestashop ed Opencart. Prestashop ed Opencart permettono di creare siti web natidamente specifici per l’ecommerce.

Altresì, sono specializzato nella consulenza e realizzazione di siti web per micro e piccole aziende locali nonché siti web per professionisti che operano in locale, nella propria zona geografica. 

Il marketing seo, digital seo, seo locale, sono gli altri miei punti di forza.

OLTRE

25

ESPERIENZA

NEL SETTORE

ANNI

 
 
 

Si eseguono servizi di realizzazioni siti e-commerce siti web su Viterbo,  Terni, Roma,  Rieti e tutta Italia

© 2022  Guido Perugi  ||  Consulente Web  ||   P.Iva 02412690568

 
 

 

Questo sito web fa uso di cookies. Si prega di consultare la nostra informativa sulla privacy per i dettagli.