/[drupal]/contributions/modules/uc_vat_number/uc_vat_number.module
ViewVC logotype

Contents of /contributions/modules/uc_vat_number/uc_vat_number.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Wed Nov 19 07:10:37 2008 UTC (12 months, 1 week ago) by zmove
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +7 -9 lines
File MIME type: text/x-php
Correct small token mistake
1 <?php
2 // $Id: uc_vat_number.module,v 1.1 2008/11/18 16:57:18 zmove Exp $
3
4 /**
5 * @file
6 * Defines a checkout pane that lets customers specify their VAT number
7 * This is required for store that sell to professionnal in European Union
8 *
9 * You can choose for which role you want to show this field.
10 *
11 */
12
13
14 /*******************************************************************************
15 * Hook Functions
16 ******************************************************************************/
17
18 /**
19 * Implementation of hook_perm()
20 */
21 function uc_vat_number_perm() {
22 return array('Have to fill VAT number');
23 }
24
25 /**
26 * Implementation of hook_form_alter()
27 */
28 function uc_vat_number_form_alter(&$form, $form_state, $form_id) {
29 // Alter the global shop setting form to ask for the store VAT Number
30 if ($form_id == 'uc_store_store_settings_form') {
31 $form['uc_store_vat_number'] = array(
32 '#type' => 'textfield',
33 '#title' => t('Store VAT number'),
34 '#description' => t('Your VAT number, this number have to be displayed in your invoices.'),
35 '#default_value' => variable_get('uc_store_vat_number', ''),
36 '#size' => 32,
37 '#maxlength' => 64,
38 '#required' => false,
39 '#weight' => 1,
40 );
41 $form['uc_store_vat_number_soap'] = array(
42 '#type' => 'checkbox',
43 '#title' => t('Check the validity on the customer VAT Number via <a href="http://ec.europa.eu/taxation_customs/vies/lang.do?fromWhichPage=vieshome">Europa VAT Number validation webservice</a>'),
44 '#weight' => 2,
45 '#default_value' => variable_get('uc_store_vat_number_soap', true),
46 );
47 $form['uc_notify_store_help_page']['#weight'] = 9;
48 $form['buttons']['#weight'] = 10;
49 }
50 // Alter the checkout form to ask for the Customer VAT number
51 if($form_id == 'uc_cart_checkout_form') {
52 global $user;
53 if(!user_access('Have to fill VAT number')) {
54 return;
55 }
56 if(is_numeric($_SESSION['cart_order'])) {
57 $order_data = db_result(db_query('SELECT data FROM {uc_orders} WHERE order_id = %d', $_SESSION['cart_order']));
58 $order_data = unserialize($order_data);
59 }
60
61 drupal_add_js(drupal_get_path('module', 'uc_vat_number') .'/uc_vat_number.js');
62 // Trigger a fake change action to force VAT Number refresh
63 drupal_add_js('$(document).ready(function() { $("#edit-panes-billing-billing-country").change(); });', 'inline');
64
65 $form['panes']['billing']['billing_company']['#required'] = true;
66 $form['#validate'][] = 'uc_vat_number_checkout_validate';
67
68 $form['panes']['billing']['billing_vat_number'] = array(
69 '#type' => 'textfield',
70 '#title' => t('VAT Number'),
71 '#description' => t('Required for professionnal customers in EU (eg: FR0123456789).'),
72 '#size' => 32,
73 '#maxlength' => 32,
74 '#weight' => 1,
75 '#required' => false,
76 '#default_value' => $order_data['vat_number'],
77 );
78 }
79 }
80
81 /**
82 * Implementation of hook_checkout_pane
83 */
84 function uc_vat_number_checkout_pane() {
85 $panes[] = array(
86 'id' => 'vat_number',
87 'title' => t('VAT Number'),
88 'desc' => t('VAT Number'),
89 'callback' => 'uc_checkout_pane_vat_number',
90 'weight' => 3,
91 );
92 return $panes;
93 }
94
95 /**
96 * Implementation of hook_order().
97 */
98 function uc_vat_number_order($op, &$arg1) {
99 switch ($op) {
100 case 'save':
101 // Load up the existing data array.
102 if(empty($_POST['panes']['billing']['billing_vat_number'])) {
103 break;
104 }
105
106 $data = db_result(db_query("SELECT data FROM {uc_orders} WHERE order_id = %d", $arg1->order_id));
107 $data = unserialize($data);
108
109 // Add the custom data into the data array
110 $data['vat_number'] = $_POST['panes']['billing']['billing_vat_number'];
111
112 // Save it again.
113 db_query("UPDATE {uc_orders} SET data = '%s' WHERE order_id = %d", serialize($data), $arg1->order_id);
114 break;
115 }
116 }
117
118 /**
119 * Implementation of hook_token_values(). (token.module)
120 */
121 function uc_vat_number_token_values($type, $object = NULL) {
122 switch ($type) {
123 case 'order':
124 $values['store-vat-number'] = variable_get('uc_store_vat_number', t('Your store VAT number'));
125
126 if (user_access('Have to fill VAT number')) {
127 $order = $object;
128 $values['order-vat-number'] = $order->data['vat_number'];
129 }
130 break;
131
132 }
133 return $values;
134 }
135
136 /**
137 * Implementation of hook_token_list(). (token.module)
138 */
139 function uc_vat_number_token_list($type = 'all') {
140 if ($type == 'ubercart') {
141 $tokens['order']['store-vat-number'] = t('Your store VAT number.');
142 $tokens['order']['order-vat-number'] = t('The VAT number of the customer.');
143 }
144 return $tokens;
145 }
146
147
148 /*******************************************************************************
149 * Callback Functions, Forms, and Tables
150 ******************************************************************************/
151
152 /**
153 * Checkout pane callback.
154 */
155 function uc_checkout_pane_vat_number($op, &$arg1, $arg2) {
156 switch($op) {
157 case 'review':
158 $review[] = array('title' => t('VAT Number'), 'data' => $arg1->data['vat_number']);
159 return $review;
160 }
161 }
162
163 function uc_vat_number_checkout_validate($form, $form_state) {
164 /*List of European countries (in order of the array)
165 Allemagne
166 Autriche
167 Belgique
168 Danemark
169 Espagne
170 Finlande
171 France
172 Grce
173 Irlande
174 Italie
175 Luxembourg
176 Pays-Bas
177 Portugal
178 Royaume-Uni
179 Sude
180 Chypre
181 Estonie
182 Hongrie
183 Lettonie
184 Lituanie
185 Malte
186 Pologne
187 Rpublique tchque
188 Slovaquie
189 Slovnie*/
190 // Skip validation if the customer is not in EU country.
191 $european_countries = array(276, 040, 056, 208, 724, 246, 250, 300, 372, 380, 442, 528, 620, 826, 752, 196, 233, 348, 428, 440, 470, 616, 203, 703, 705);
192 if(!in_array($form_state['values']['panes']['billing']['billing_country'], $european_countries)) {
193 return;
194 }
195 if(empty($form_state['values']['panes']['billing']['billing_vat_number'])) {
196 form_set_error('panes][billing][billing_vat_number', t('VAT Number field is required.'));
197 }
198
199 if(variable_get('uc_store_vat_number_soap', true) == true) {
200 $vat_number = str_replace(' ', '', $form_state['values']['panes']['billing']['billing_vat_number']);
201 $countryCode = substr($vat_number, 0, 2);
202 $vatNumber = substr($vat_number, 2);
203
204 if(strlen($countryCode) != 2 or is_numeric(substr($countryCode, 0, 1)) or is_numeric(substr($countryCode, 1, 2)) or !is_numeric($vatNumber)) {
205 form_set_error('panes][billing][billing_vat_number', t('Your VAT Number syntax is not correct. You should have something like that : FR0123456789'));
206 return;
207 }
208 $client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/api/checkVatPort?wsdl");
209 $params = array('countryCode' => $countryCode, 'vatNumber' => $vatNumber);
210 $result = $client->checkVat($params);
211 if(!$result->valid) {
212 form_set_error('panes][billing][billing_vat_number', t('VAT Number verification failed, check it\'s validity on <a href="http://ec.europa.eu/taxation_customs/vies/vieshome.do">this website</a>'));
213 }
214 }
215 }

  ViewVC Help
Powered by ViewVC 1.1.2