Plugin Directory

Changeset 2395291


Ignore:
Timestamp:
10/07/2020 02:11:10 PM (5 years ago)
Author:
officegestpt
Message:

Fixed Bug for nif validation for portugal
Added Limit for Syncronizing Parts

Location:
officegest/trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • officegest/trunk/officegest.php

    r2387074 r2395291  
    55 *   Plugin URI:   https://www.officegest.com/woocommerce
    66 *   Description:  A forma mais fácil de ligar a sua loja online com a sua faturação.
    7  *   Version:      1.0.27
     7 *   Version:      1.0.28
    88 *   Author:       OfficeGest
    99 *   Author URI:   https://www.officegest.com
  • officegest/trunk/readme.txt

    r2387077 r2395291  
    55Tested up to: 5.5
    66Requires PHP: 5.6
    7 Stable tag: 1.0.27
     7Stable tag: 1.0.28
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    3333
    3434== Changelog ==
     35
     36= 1.0.28 =
     37Fixed Bug for nif validation for portugal
     38
    3539= 1.0.27 =
    3640Fixed Bug for observations for ecoauto Parts and Vehicles
     
    123127
    124128== Upgrade Notice ==
    125 Added Manual
     129Fixed Bug for nif validation for portugal
  • officegest/trunk/src/Controllers/SyncEcoAuto.php

    r2329411 r2395291  
    1919    public function run()
    2020    {
    21         $has_ecoauto = OfficeGestDBModel::getOption('officegest_ecoauto')==1;
     21        $has_ecoauto = OfficeGestDBModel::getOption('officegest_ecoauto')==1;
     22        $has_syncpecas = OfficeGestDBModel::getOption('ecoauto_sync_pecas')==1;
    2223        if ($has_ecoauto){
    23             $params = [
    24                 'tipos_pecas'=>OfficeGestDBModel::getOption('ecoauto_tipos_pecas'),
    25                 'imagens'=>OfficeGestDBModel::getOption('ecoauto_imagens'),
    26             ];
    27             $filtros= OfficeGestDBModel::getAllEcoautoPartsDB(true,$params);
    28             $contador = OfficeGestDBModel::cria_peca($filtros);
    29             OfficeGestDBModel::limpapecas();
    30             $this->found = $contador;
     24            if ($has_syncpecas){
     25                $params = [
     26                    'tipos_pecas'=>OfficeGestDBModel::getOption('ecoauto_tipos_pecas'),
     27                    'imagens'=>OfficeGestDBModel::getOption('ecoauto_imagens'),
     28                ];
     29                $filtros= OfficeGestDBModel::getAllEcoautoPartsDB(true,$params);
     30                $contador = OfficeGestDBModel::cria_peca($filtros);
     31                OfficeGestDBModel::limpapecas();
     32                $this->found = $contador;
     33            }
    3134        }
    3235        return $this;
  • officegest/trunk/src/Controllers/SyncImagens.php

    r2334140 r2395291  
    1717    {
    1818        global $wpdb;
    19         $has_ecoauto = OfficeGestDBModel::getOption('officegest_ecoauto')==1;
     19        $has_ecoauto = OfficeGestDBModel::getOption('officegest_ecoauto')==1;
     20        $has_sync_imagens = OfficeGestDBModel::getOption('ecoauto_sync_imagens')==1;
     21        $limit_imagens = OfficeGestDBModel::getOption('ecoauto_sync_imagens_limit');
    2022        if ($has_ecoauto){
    21             OfficeGestDBModel::getAllPhotosDB();
    22             $query = "Select id,woo_id from ".TABLE_OFFICEGEST_ECO_PARTS." where woo_id>0 and photos_imported=0 LIMIT 60";
    23             $filtros = $wpdb->get_results($query,ARRAY_A);
    24             $this->found = OfficegestProduct::update_images($filtros);
     23            if ($has_sync_imagens){
     24                OfficeGestDBModel::getAllPhotosDB();
     25                $query = "Select id,woo_id from ".TABLE_OFFICEGEST_ECO_PARTS." where woo_id>0 and photos_imported=0 LIMIT ".$limit_imagens;
     26                $filtros = $wpdb->get_results($query,ARRAY_A);
     27                $this->found = OfficegestProduct::update_images($filtros);
     28            }
    2529        }
    2630        return $this;
  • officegest/trunk/src/Crons.php

    r2363243 r2395291  
    7777                (new SyncArvore())->run();
    7878            } else {
    79                 Log::write("Sincronizacação da arvore desactiva no plugin");
     79                Log::write("Sincronizacação da arvore desactivada no plugin");
    8080            }
    8181        } catch (Exception $ex) {
     
    104104                (new SyncEcoAuto())->run();
    105105            } else {
    106                 Log::write("Sincronizacação de Peças desactiva no plugin");
     106                Log::write("Sincronizacação de Peças desactivada no plugin");
    107107            }
    108108        } catch (Exception $ex) {
     
    113113
    114114    public function syncImagens(){
     115        global $wpdb;
     116        $runningAt = time();
    115117        try {
    116118            self::requires();
     
    119121                return false;
    120122            }
    121             (new SyncImagens())->run();
     123            $stock_sync = OfficeGestDBModel::getOption('ecoauto_sync_imagens');
     124            if ($stock_sync==1) {
     125                $sync_time = OfficeGestDBModel::getOption('ecoauto_imagens_time');
     126                if ($sync_time==null) {
     127                    $sync_time =  (int)(time() - 600);
     128                    OfficeGestDBModel::setOption('ecoauto_imagens_time',$sync_time);
     129                }
     130                (new SyncImagens())->run();
     131            } else {
     132                Log::write("Sincronizacação de Imagens desactivada no plugin");
     133            }
    122134        } catch (Exception $ex) {
    123135            Log::write("Fatal Errror: " . $ex->getMessage());
    124136        }
     137        OfficeGestDBModel::setOption("ecoauto_imagens_time", (int)$runningAt);
    125138    }
    126139
  • officegest/trunk/src/Nif.php

    r2284138 r2395291  
    4343    //Add field to billing address fields - Globally
    4444    public function woocommerce_nif_billing_fields( $fields, $country ) {
    45             $fields['billing_nif'] = array(
    46                 'type'          =>  'text',
    47                 'label'         => __('NIF / NIPC'),
    48                 'placeholder'   => __('Número de Identificação Fiscal'),
    49                 'class'         => ['form-row-first'],
    50                 'required'      => false,
    51                 'clear'         => true, //Should be an option (?)
    52                 'autocomplete'  => 'billing_nif',
    53                 'priority'      => 120, //WooCommerce should order by this parameter but it doesn't seem to be doing so
    54                 'maxlength'     => 9,
    55                 'validate'      => [$country === 'PT']
    56             );
    57             return $fields;
    58         }
     45        $fields['billing_nif'] = array(
     46            'type'          =>  'text',
     47            'label'         => apply_filters( 'woocommerce_nif_field_label', __( 'NIF / NIPC', 'officegest' ) ),
     48            'placeholder'   => apply_filters( 'woocommerce_nif_field_placeholder', __( 'Portuguese VAT identification number', 'officegest' ) ),
     49            'class'         => apply_filters( 'woocommerce_nif_field_class', array( 'form-row-first' ) ), //Should be an option (?)
     50            'required'      => (
     51                                    $country == 'PT' || apply_filters( 'woocommerce_nif_show_all_countries', false )
     52                                    ?
     53                                    apply_filters( 'woocommerce_nif_field_required', false ) //Should be an option (?)
     54                                    :
     55                                    false
     56                                ),
     57            'clear'         => apply_filters( 'woocommerce_nif_field_clear', true ), //Should be an option (?)
     58            'autocomplete'  => apply_filters( 'woocommerce_nif_field_autocomplete', 'on' ),
     59            'priority'      => apply_filters( 'woocommerce_nif_field_priority', 120 ), //WooCommerce should order by this parameter but it doesn't seem to be doing so
     60            'maxlength'     => apply_filters( 'woocommerce_nif_field_maxlength', 9 ),
     61            'validate'      => (
     62                                    $country == 'PT'
     63                                    ?
     64                                    (
     65                                        apply_filters( 'woocommerce_nif_field_validate', false )
     66                                        ?
     67                                        array( 'nif_pt' ) //Does nothing, actually - Validation is down there on the 'woocommerce_checkout_process' action
     68                                        :
     69                                        array()
     70                                    )
     71                                    :
     72                                    false
     73                                ),
     74        );
     75        return $fields;
     76
     77    }
    5978
    6079    //Add field to order admin panel
     
    6281    public function woocommerce_nif_admin_billing_fields( $billing_fields ) {
    6382        global $post;
    64         if ( $post->post_type === 'shop_order' || $post->post_type === 'shop_subscription' ) {
     83        if ( $post->post_type == 'shop_order' || $post->post_type == 'shop_subscription' ) {
    6584            $order = new WC_Order( $post->ID );
    6685            $countries = new WC_Countries();
    67             $billing_country = version_compare( $this->wc_version, '3.0', '>=' ) ? $order->get_billing_country() : $order->billing_country;
     86            $billing_country = version_compare( WC_VERSION, '3.0', '>=' ) ? $order->get_billing_country() : $order->billing_country;
    6887            //Customer is portuguese or it's a new order ?
    69             if ( $billing_country === 'PT' || ( $billing_country == '' && $countries->get_base_country() === 'PT' ) ) {
     88            if ( $billing_country == 'PT' || ( $billing_country == '' && $countries->get_base_country() == 'PT' ) || apply_filters( 'woocommerce_nif_show_all_countries', false ) ) {
    7089                $billing_fields['nif'] = array(
    71                     'label' => __('NIF / NIPC') ,
    72                     'description' => __('Número de Identificação Fiscal')
     90                    'label' => apply_filters( 'woocommerce_nif_field_label', __( 'NIF / NIPC', 'officegest' ) ),
    7391                );
    7492            }
    7593        }
    7694        return $billing_fields;
     95
    7796    }
    7897
     
    88107    //3.0 and above - See https://github.com/woocommerce/woocommerce/issues/12654
    89108    public function woocommerce_nif_ajax_get_customer_details( $customer_data, $customer, $user_id ) {
    90         if ( ( isset( $customer_data['billing']['country']) && $customer_data['billing']['country'] == 'PT' ) ) {
     109        if ( ( isset( $customer_data['billing']['country']) && $customer_data['billing']['country'] == 'PT' ) || apply_filters( 'woocommerce_nif_show_all_countries', false ) ) {
    91110            $customer_data['billing']['nif'] = $customer->get_meta( 'billing_nif' );
    92111        }
     
    99118        if ( isset( $show_fields['billing'] ) && is_array( $show_fields['billing']['fields'] ) ) {
    100119            $show_fields['billing']['fields']['billing_nif'] = array(
    101                 'label' => __('NIF / NIPC'),
    102                 'description' => __('Número de Identificação Fiscal')
     120                'label' => apply_filters( 'woocommerce_nif_field_label', __( 'NIF / NIPC', 'officegest' ) ),
     121                'description' => apply_filters( 'woocommerce_nif_field_placeholder', __( 'Portuguese VAT identification number', 'officegest' ) ),
    103122            );
    104123        }
     
    109128
    110129    public function woocommerce_nif_order_details_after_customer_details( $order ) {
    111         $billing_country = version_compare( $this->wc_version, '3.0', '>=' ) ? $order->get_billing_country() : $order->billing_country;
    112         $billing_nif = version_compare( $this->wc_version, '3.0', '>=' ) ? $order->get_meta( '_billing_nif' ) : $order->billing_nif;
    113         if ( ( $billing_country === 'PT' ) && $billing_nif ) {
     130        $billing_country = version_compare( WC_VERSION, '3.0', '>=' ) ? $order->get_billing_country() : $order->billing_country;
     131        $billing_nif = version_compare( WC_VERSION, '3.0', '>=' ) ? $order->get_meta( '_billing_nif' ) : $order->billing_nif;
     132        if ( ( $billing_country == 'PT' || apply_filters( 'woocommerce_nif_show_all_countries', false ) ) && $billing_nif ) {
    114133            ?>
    115134            <tr>
    116                 <th><?php echo  __( 'NIF / NIPC'); ?>:</th>
     135                <th><?php echo apply_filters( 'woocommerce_nif_field_label', __( 'NIF / NIPC', 'officegest' ) ); ?>:</th>
    117136                <td><?php echo esc_html( $billing_nif ); ?></td>
    118137            </tr>
    119138            <?php
    120139        }
     140
    121141    }
    122142
     
    124144
    125145    public function woocommerce_nif_email_customer_details_fields( $fields, $sent_to_admin, $order ) {
    126         $billing_nif = version_compare( $this->wc_version, '3.0', '>=' ) ? $order->get_meta( '_billing_nif' ) : $order->billing_nif;
     146        $billing_nif = version_compare( WC_VERSION, '3.0', '>=' ) ? $order->get_meta( '_billing_nif' ) : $order->billing_nif;
    127147        if ( $billing_nif ) {
    128148            $fields['billing_nif'] = array(
    129                 'label' => __('NIF / NIPC'),
     149                'label' => apply_filters( 'woocommerce_nif_field_label', __( 'NIF / NIPC', 'officegest' ) ),
    130150                'value' => wptexturize( $billing_nif )
    131151            );
     
    139159        //Order
    140160        if ( isset( $order_data['billing_address'] ) ) {
    141             $billing_nif = version_compare( $this->wc_version, '3.0', '>=' ) ? $order->get_meta( '_billing_nif' ) : $order->billing_nif;
     161            $billing_nif = version_compare( WC_VERSION, '3.0', '>=' ) ? $order->get_meta( '_billing_nif' ) : $order->billing_nif;
    142162            $order_data['billing_address']['nif'] = $billing_nif;
    143163        }
    144164        return $order_data;
     165
    145166    }
    146167
     
    157178
    158179    public function woocommerce_nif_checkout_process() {
    159         $customer_country = version_compare( $this->wc_version, '3.0', '>=' ) ? WC()->customer->get_billing_country() : WC()->customer->get_country();
    160         $countries = new WC_Countries();
    161         if ( $customer_country == 'PT' || ( $customer_country == '' && $countries->get_base_country() == 'PT' ) ) {
    162             $billing_nif = wc_clean( isset( $_POST['billing_nif'] ) ? $_POST['billing_nif'] : '' );
    163             if ( Tools::validaNIFPortugal( $billing_nif, true ) || ( trim( $billing_nif ) == '' ) ) {
    164                 //OK
     180        if ( apply_filters( 'woocommerce_nif_field_validate', false ) ) {
     181            $customer_country = version_compare( WC_VERSION, '3.0', '>=' ) ? WC()->customer->get_billing_country() : WC()->customer->get_country();
     182            $countries = new WC_Countries();
     183            if ( $customer_country == 'PT' || ( $customer_country == '' && $countries->get_base_country() == 'PT' ) ) {
     184                $billing_nif = wc_clean( isset( $_POST['billing_nif'] ) ? $_POST['billing_nif'] : '' );
     185                if ( Tools::validaNIFPortugal( $billing_nif, true ) || ( trim( $billing_nif ) == '' &&  !apply_filters( 'woocommerce_nif_field_required', false ) ) ) { //If the field is NOT required and it's empty, we shouldn't validate it
     186                    //OK
     187                } else {
     188                    wc_add_notice(
     189                        sprintf( __( 'You have entered an invalid %s for Portugal.', 'officegest' ), '<strong>'.apply_filters( 'woocommerce_nif_field_label', __( 'NIF / NIPC', 'officegest' ) ).'</strong>' ),
     190                        'error'
     191                    );
     192                }
    165193            } else {
    166                 wc_add_notice(
    167                     sprintf( __( 'You have entered an invalid %s for Portugal.'), '<strong>'.'NIF / NIPC'.'</strong>' ), 'error'
    168                 );
    169             }
    170         }
     194                //Not Portugal
     195            }
     196        } else {
     197            //All good - No validation required
     198        }
     199
    171200    }
    172201
     
    174203    public function woocommerce_nif_after_save_address_validation( $user_id, $load_address, $address ) {
    175204        if ( $load_address == 'billing' ) {
    176             $country = wc_clean( isset( $_POST['billing_country'] ) ? $_POST['billing_country'] : '' );
    177             if ( $country == 'PT' ) {
    178                 $billing_nif = wc_clean( isset( $_POST['billing_nif'] ) ? $_POST['billing_nif'] : '' );
    179                 if ( Tools::validaNIFPortugal( $billing_nif, true ) || ( trim( $billing_nif ) == '' ) ) { //If the field is NOT required and it's empty, we shouldn't validate it
     205            if ( apply_filters( 'woocommerce_nif_field_validate', false ) ) {
     206                $country = wc_clean( isset( $_POST['billing_country'] ) ? $_POST['billing_country'] : '' );
     207                if ( $country == 'PT' ) {
     208                    $billing_nif = wc_clean( isset( $_POST['billing_nif'] ) ? $_POST['billing_nif'] : '' );
     209                    if ( Tools::validaNIFPortugal( $billing_nif, true ) || ( trim( $billing_nif ) == '' &&  !apply_filters( 'woocommerce_nif_field_required', false ) ) ) { //If the field is NOT required and it's empty, we shouldn't validate it
    180210                    //OK
    181                 } else {
    182                     wc_add_notice( sprintf( __( 'You have entered an invalid %s for Portugal.' ), '<strong>NIF / NIPC</strong>' ), 'error' );
     211                    } else {
     212                        wc_add_notice(
     213                            sprintf( __( 'You have entered an invalid %s for Portugal.', 'officegest' ), '<strong>'.apply_filters( 'woocommerce_nif_field_label', __( 'NIF / NIPC', 'officegest' ) ).'</strong>' ),
     214                            'error'
     215                        );
     216                    }
    183217                }
    184218            }
    185219        }
     220
    186221    }
    187222
  • officegest/trunk/src/Templates/Containers/Settings/EcoAuto.php

    r2376560 r2395291  
    1010$ecoauto_sync_pecas= OfficeGestDBModel::getOption('ecoauto_sync_pecas');
    1111$ecoauto_sync_arvore= OfficeGestDBModel::getOption('ecoauto_sync_arvore');
     12$ecoauto_sync_imagens =  OfficeGestDBModel::getOption('ecoauto_sync_imagens');
     13$ecoauto_sync_imagens_limit =  OfficeGestDBModel::getOption('ecoauto_sync_imagens_limit');
    1214$ecoauto_viaturas= OfficeGestDBModel::getOption('ecoauto_viaturas');
    1315$ecoauto_tipos_viaturas= OfficeGestDBModel::getOption('ecoauto_tipos_viaturas');
     
    118120            <p class='description'><?= __('Sincronizar Peças Automaticamente') ?></p>
    119121        </td>
     122    </tr>
     123    <tr>
     124        <th scope="row" class="titledesc">
     125            <label for="ecoauto_sync_imagens"><?= __("Sincronizar Imagens Automaticamente") ?></label>
     126        </th>
     127        <td>
     128            <select id="ecoauto_sync_imagens" name='opt[ecoauto_sync_imagens]' class='officegest_select2 inputOut'>
     129                <option value='1' <?= $ecoauto_sync_imagens == '1' ? 'selected' : '1' ?>><?= __("Sim") ?></option>
     130                <option value='2' <?= $ecoauto_sync_imagens == '2' ? 'selected' : '2' ?>><?= __("Não") ?></option>
     131            </select>
     132            <p class='description'><?= __('Sincronizar Imagens Automaticamente') ?></p>
     133        </td>
     134    </tr>
     135    <tr>
     136        <th scope="row" class="titledesc">
     137            <label for="ecoauto_sync_imagens_limit"><?= __("Limite de Imagens por sincronização") ?></label>
     138        </th>
     139        <td>
     140            <select id="ecoauto_sync_imagens_limit" name='opt[ecoauto_sync_imagens_limit]' class='officegest_select2 inputOut'>
     141                <option value='20' <?= $ecoauto_sync_imagens_limit == '20' ? 'selected' : '20' ?>>20</option>
     142                <option value='40' <?= $ecoauto_sync_imagens_limit == '40' ? 'selected' : '40' ?>>40</option>
     143                <option value='60' <?= $ecoauto_sync_imagens_limit == '40' ? 'selected' : '60' ?>>60</option>
     144                <option value='80' <?= $ecoauto_sync_imagens_limit == '40' ? 'selected' : '80' ?>>80</option>
     145                <option value='100' <?= $ecoauto_sync_imagens_limit == '100' ? 'selected' : '100' ?>>100</option>
     146                <option value='120' <?= $ecoauto_sync_imagens_limit == '120' ? 'selected' : '120' ?>>120</option>
     147                <option value='140' <?= $ecoauto_sync_imagens_limit == '140' ? 'selected' : '140' ?>>140</option>
     148                <option value='160' <?= $ecoauto_sync_imagens_limit == '160' ? 'selected' : '160' ?>>160</option>
     149                <option value='180' <?= $ecoauto_sync_imagens_limit == '180' ? 'selected' : '180' ?>>180</option>
     150                <option value='200' <?= $ecoauto_sync_imagens_limit == '200' ? 'selected' : '200' ?>>200</option>
     151            </select>
     152            <p class='description'><?= __('Limite de Imagens por sincronização') ?></p>
     153        </td>
    120154    </tr>
    121155    <tr>
  • officegest/trunk/src/Tools.php

    r2334140 r2395291  
    183183    public static function validaNIFPortugal($nif, $ignoreFirst=true) {
    184184        //Limpamos eventuais espaços a mais
    185         $nif=trim($nif);
     185        $nif = trim( $nif );
    186186        //Verificamos se é numérico e tem comprimento 9
    187         if (!is_numeric($nif) || strlen($nif)!=9) {
     187        if ( !is_numeric( $nif ) || strlen( $nif ) != 9 ) {
    188188            return false;
    189         }
    190 
    191         $nifSplit=str_split($nif);
    192         //O primeiro digíto tem de ser 1, 2, 3, 5, 6, 8 ou 9
    193         //Ou não, se optarmos por ignorar esta "regra"
    194         if ( in_array($nifSplit[0], array(1, 2, 3, 5, 6, 8, 9)) || $ignoreFirst
    195         ) {
    196             //Calculamos o dígito de controlo
    197             $checkDigit=0;
    198             for($i=0; $i<8; $i++) {
    199                 $checkDigit+=$nifSplit[$i]*(10-$i-1);
    200             }
    201             $checkDigit=11-($checkDigit % 11);
    202             //Se der 10 então o dígito de controlo tem de ser 0
    203             if($checkDigit>=10) $checkDigit=0;
    204             //Comparamos com o último dígito
    205             return $checkDigit == $nifSplit[8] ? true : false;
    206         }
    207 
    208         return false;
     189        } else {
     190            $nifSplit = str_split( $nif );
     191            //O primeiro digíto tem de ser 1, 2, 5, 6, 8 ou 9
     192            //Ou não, se optarmos por ignorar esta "regra"
     193            if (
     194                in_array( $nifSplit[0], array( 1, 2, 5, 6, 8, 9 ) )
     195                ||
     196                $ignoreFirst
     197            ) {
     198                //Calculamos o dígito de controlo
     199                $checkDigit=0;
     200                for( $i=0; $i<8; $i++ ) {
     201                    $checkDigit += $nifSplit[$i] * ( 10-$i-1 );
     202                }
     203                $checkDigit = 11 - ( $checkDigit % 11 );
     204                //Se der 10 então o dígito de controlo tem de ser 0
     205                if( $checkDigit >= 10 ) $checkDigit = 0;
     206                //Comparamos com o último dígito
     207                if ( $checkDigit == $nifSplit[8] ) {
     208                    return true;
     209                } else {
     210                    return false;
     211                }
     212            } else {
     213                return false;
     214            }
     215        }
    209216    }
    210217
Note: See TracChangeset for help on using the changeset viewer.