Index: civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm.module |
— | — | @@ -262,20 +262,6 @@ |
263 | 263 | $contact['id'] = $contact_result['contact_id']; |
264 | 264 | |
265 | 265 | |
266 | | - /** |
267 | | - * When passing CiviCRM a state abbreviation, odd things can happen - like getting the right abbreviation, but the wrong state |
268 | | - * So we'll pull back the correct state/province name based off of a user's country/state abbreviation |
269 | | - */ |
270 | | - if ( strlen( $msg['country'] ) == 2 ) { |
271 | | - $query = "SELECT s.name AS state_name FROM civicrm_country c, civicrm_state_province s WHERE s.country_id=c.id AND c.iso_code='" . addslashes( $msg['country'] ) . "' AND s.abbreviation='" . addslashes( $msg['state_province'] ) . "'"; |
272 | | - } else { |
273 | | - $query = "SELECT s.name AS state_name FROM civicrm_country c, civicrm_state_province s WHERE s.country_id=c.id AND c.name='" . addslashes( $msg['country'] ) . "' AND s.abbreviation='" . addslashes( $msg['state_province'] ) . "'"; |
274 | | - } |
275 | | - $dao = CRM_Core_DAO::executeQuery( $query ); |
276 | | - while ( $dao->fetch() ) { |
277 | | - $state = $dao->state_name; |
278 | | - } |
279 | | - |
280 | 266 | // Insert the location record |
281 | 267 | $address = array( |
282 | 268 | 'contact_id' => $contact['id'], |
— | — | @@ -283,7 +269,7 @@ |
284 | 270 | 'street_address' => $msg['street_address'], |
285 | 271 | 'supplemental_address_1' => $msg['supplemental_address_1'], |
286 | 272 | 'city' => $msg['city'], |
287 | | - 'state_province' => ( !is_null( $state )) ? $state : $msg['state_province'], |
| 273 | + 'state_province' => queue2civicrm_get_state( $msg[ 'country' ], $msg['state_province'] ), |
288 | 274 | 'postal_code' => $msg['postal_code'], |
289 | 275 | 'country' => $msg['country'], |
290 | 276 | 'is_primary' => 1, |
— | — | @@ -365,6 +351,27 @@ |
366 | 352 | } |
367 | 353 | |
368 | 354 | /** |
| 355 | + * Find correct state for insertion |
| 356 | + * |
| 357 | + * When passing CiviCRM a state abbreviation, odd things can happen - like getting the right abbreviation, but the wrong state |
| 358 | + * So we'll pull back the correct state/province name based off of a user's country/state abbreviation |
| 359 | + */ |
| 360 | +function queue2civicrm_get_state( $country, $state ) { |
| 361 | + |
| 362 | + if ( strlen( $country ) == 2 ) { |
| 363 | + $query = "SELECT s.name AS state_name FROM civicrm_country c, civicrm_state_province s WHERE s.country_id=c.id AND c.iso_code='" . addslashes( $country ) . "' AND s.abbreviation='" . addslashes( $state ) . "'"; |
| 364 | + } else { |
| 365 | + $query = "SELECT s.name AS state_name FROM civicrm_country c, civicrm_state_province s WHERE s.country_id=c.id AND c.name='" . addslashes( $country ) . "' AND s.abbreviation='" . addslashes( $state ) . "'"; |
| 366 | + } |
| 367 | + $dao = CRM_Core_DAO::executeQuery( $query ); |
| 368 | + while ( $dao->fetch() ) { |
| 369 | + $state = ( !is_null( $dao->state_name ) ) ? $dao->state_name : $state; |
| 370 | + } |
| 371 | + |
| 372 | + return $state; |
| 373 | +} |
| 374 | + |
| 375 | +/** |
369 | 376 | * Make the form to insert a test message into the queue |
370 | 377 | */ |
371 | 378 | function queue2civicrm_insertmq_form() { |
Index: civicrm/trunk/sites/all/modules/wmf_premiums/wmf_premiums.module |
— | — | @@ -36,20 +36,43 @@ |
37 | 37 | // make sure that we have premiums to deal with |
38 | 38 | if ( !$contribution_info[ 'msg' ][ 'size' ] ) { |
39 | 39 | watchdog( 'wmf_premiums', 'No premium selection for contribution id: %contrib_id', array('%contrib_id' => $contribution_info['contribution_id'] ), WATCHDOG_DEBUG); |
40 | | - return; |
| 40 | + return 0; |
41 | 41 | } |
42 | 42 | |
43 | | - $query = "INSERT INTO civicrm.premiums (tracking_id, contact_id, contribution_id, shirt, shirt_size, shirt_language ) |
44 | | - VALUES ( %d, %d, %d, %d, '%s', '%s' )"; |
| 43 | + // prepare the shipping address |
| 44 | + $shipping = wmf_premiums_get_shipping_address( $contribution_info[ 'msg' ] ); |
45 | 45 | |
| 46 | + $query = "INSERT INTO civicrm.premiums ( |
| 47 | + tracking_id, |
| 48 | + contact_id, |
| 49 | + contribution_id, |
| 50 | + shirt, |
| 51 | + size, |
| 52 | + premium_language, |
| 53 | + name, |
| 54 | + address, |
| 55 | + supplemental_address |
| 56 | + city, |
| 57 | + state, |
| 58 | + postal_code, |
| 59 | + country |
| 60 | + ) VALUES ( %d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )"; |
| 61 | + |
46 | 62 | watchdog( 'wmf_premiums', 'Preparing to insert premium data with the following contribution info: %contrib_info', array( '%contrib_info' => print_r( $contribution_info, true )), WATCHDOG_DEBUG ); |
47 | | - $result = db_query( $query, |
| 63 | + $result = db_query( $query, |
48 | 64 | $contribution_info['msg']['contribution_tracking_id'], |
49 | 65 | $contribution_info['contact_id'], |
50 | 66 | $contribution_info['contribution_id'], |
51 | 67 | 1, |
52 | 68 | $contribution_info['msg']['size'], |
53 | | - $contribution_info['msg']['premium_language']); |
| 69 | + $contribution_info['msg']['premium_language'], |
| 70 | + $shipping[ 'name' ], |
| 71 | + $shipping[ 'address' ], |
| 72 | + $shipping[ 'supplemental_address' ], |
| 73 | + $shipping[ 'city' ], |
| 74 | + $shipping[ 'state' ], |
| 75 | + $shipping[ 'postal_code' ], |
| 76 | + $shipping[ 'country' ] ); |
54 | 77 | if ( !$result ) { |
55 | 78 | watchdog( 'wmf_premiums', 'There was an error inserting with the following contrib info: %contribution_info', array( '%contribution_info' => print_r( $contribution_info, true)), WATCHDOG_ERROR ); |
56 | 79 | return false; |
— | — | @@ -57,15 +80,49 @@ |
58 | 81 | $insert_id = db_last_insert_id( 'civicrm.premiums', 'id' ); |
59 | 82 | watchdog( 'wmf_premiums', 'Created new premiums record with id: %insert_id', array( '%insert_id' => $insert_id ), WATCHDOG_DEBUG ); |
60 | 83 | } |
| 84 | + |
61 | 85 | return $insert_id; |
62 | 86 | } |
63 | 87 | |
| 88 | +/** |
| 89 | + * Determine the shipping address fields |
| 90 | + * |
| 91 | + * @param $trxn_info The full trxn message taken from the queue |
| 92 | + * @return array of shipping information |
| 93 | + */ |
| 94 | +function wmf_premiums_get_shipping_address( $trxn_info ) { |
| 95 | + $shipping_info = array(); |
| 96 | + |
| 97 | + // if we have a secondary address, use that for shipping |
| 98 | + if ( strlen( trim( $trxn_info[ 'first_name_2' ] )) && strlen( trim( $trxn_info[ 'last_name_2' ]))) { |
| 99 | + $shipping[ 'name' ] = $trxn_info[ 'first_name_2' ] . " " . $trxn_info[ 'last_name_2' ]; |
| 100 | + $shipping[ 'address' ] = $trxn_info[ 'street_address_2' ]; |
| 101 | + $shipping[ 'supplemental_address' ] = $trxn_info[ 'supplemental_address_2' ]; |
| 102 | + $shipping[ 'city' ] = $trxn_info[ 'city_2' ]; |
| 103 | + // account for civi-related state oddness |
| 104 | + $shipping[ 'state' ] = queue2civicrm_get_state( $trxn_info[ 'country_2' ], $trxn_info[ 'state_province_2' ] ); |
| 105 | + $shipping[ 'postal_code' ] = $trxn_info[ 'postal_code_2' ]; |
| 106 | + $shipping[ 'country' ] = $trxn_info[ 'country_2' ]; |
| 107 | + } else { |
| 108 | + $shipping[ 'name' ] = $trxn_info[ 'first_name' ] . " " . $trxn_info[ 'last_name' ]; |
| 109 | + $shipping[ 'address' ] = $trxn_info[ 'street_address' ]; |
| 110 | + $shipping[ 'supplemental_address' ] = $trxn_info[ 'supplemental_address_1' ]; |
| 111 | + $shipping[ 'city' ] = $trxn_info[ 'city' ]; |
| 112 | + // civi-related state oddness already handled by queue2civicrm |
| 113 | + $shipping[ 'state' ] = $trxn_info[ 'state_province' ]; |
| 114 | + $shipping[ 'postal_code' ] = $trxn_info[ 'postal_code' ]; |
| 115 | + $shipping[ 'country' ] = $trxn_info[ 'country' ]; |
| 116 | + } |
| 117 | + |
| 118 | + return $shipping_info; |
| 119 | +} |
| 120 | + |
64 | 121 | function wmf_premiums_test_insert() { |
65 | 122 | $contribution_info = array( |
66 | 123 | 'msg' => array( |
67 | | - 'size' => 'large', |
68 | | - 'contribution_tracking_id' => 31337, |
69 | | - 'premium_language' => 'en' ), |
| 124 | + 'size' => 'large', |
| 125 | + 'contribution_tracking_id' => 31337, |
| 126 | + 'premium_language' => 'en' ), |
70 | 127 | 'contact_id' => 1234, |
71 | 128 | 'contribution_id' => 4321, |
72 | 129 | ); |