uc_shipping.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the uc_shipping module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function uc_shipping_schema() {
  10. $schema = array();
  11. $schema['uc_shipments'] = array(
  12. 'description' => 'Stores shipment information.',
  13. 'fields' => array(
  14. 'sid' => array(
  15. 'description' => 'Primary key: the shipment ID.',
  16. 'type' => 'serial',
  17. 'unsigned' => TRUE,
  18. 'not null' => TRUE,
  19. ),
  20. 'order_id' => array(
  21. 'description' => 'The {uc_orders}.order_id of the order associated with the shipment.',
  22. 'type' => 'int',
  23. 'unsigned' => TRUE,
  24. 'not null' => TRUE,
  25. 'default' => 0,
  26. ),
  27. 'o_first_name' => array(
  28. 'description' => 'Origin address: First name.',
  29. 'type' => 'varchar',
  30. 'length' => 255,
  31. 'not null' => TRUE,
  32. 'default' => '',
  33. ),
  34. 'o_last_name' => array(
  35. 'description' => 'Origin address: Last name.',
  36. 'type' => 'varchar',
  37. 'length' => 255,
  38. 'not null' => TRUE,
  39. 'default' => '',
  40. ),
  41. 'o_company' => array(
  42. 'description' => 'Origin address: Company name.',
  43. 'type' => 'varchar',
  44. 'length' => 255,
  45. 'not null' => TRUE,
  46. 'default' => '',
  47. ),
  48. 'o_street1' => array(
  49. 'description' => 'Origin address: Street line 1.',
  50. 'type' => 'varchar',
  51. 'length' => 255,
  52. 'not null' => TRUE,
  53. 'default' => '',
  54. ),
  55. 'o_street2' => array(
  56. 'description' => 'Origin address: Street line 2.',
  57. 'type' => 'varchar',
  58. 'length' => 255,
  59. 'not null' => TRUE,
  60. 'default' => '',
  61. ),
  62. 'o_city' => array(
  63. 'description' => 'Origin address: City.',
  64. 'type' => 'varchar',
  65. 'length' => 255,
  66. 'not null' => TRUE,
  67. 'default' => '',
  68. ),
  69. 'o_zone' => array(
  70. 'description' => 'Origin address: State/province, from {uc_zones}.zone_id.',
  71. 'type' => 'int',
  72. 'size' => 'medium',
  73. 'unsigned' => TRUE,
  74. 'not null' => TRUE,
  75. 'default' => 0,
  76. ),
  77. 'o_postal_code' => array(
  78. 'description' => 'Origin address: Postal code.',
  79. 'type' => 'varchar',
  80. 'length' => 255,
  81. 'not null' => TRUE,
  82. 'default' => '',
  83. ),
  84. 'o_country' => array(
  85. 'description' => 'Origin address: Country, from {uc_countries}.country_id.',
  86. 'type' => 'int',
  87. 'size' => 'medium',
  88. 'unsigned' => TRUE,
  89. 'not null' => TRUE,
  90. 'default' => 0,
  91. ),
  92. 'd_first_name' => array(
  93. 'description' => 'Destination address: First name.',
  94. 'type' => 'varchar',
  95. 'length' => 255,
  96. 'not null' => TRUE,
  97. 'default' => '',
  98. ),
  99. 'd_last_name' => array(
  100. 'description' => 'Destination address: Last name.',
  101. 'type' => 'varchar',
  102. 'length' => 255,
  103. 'not null' => TRUE,
  104. 'default' => '',
  105. ),
  106. 'd_company' => array(
  107. 'description' => 'Destination address: Company name.',
  108. 'type' => 'varchar',
  109. 'length' => 255,
  110. 'not null' => TRUE,
  111. 'default' => '',
  112. ),
  113. 'd_street1' => array(
  114. 'description' => 'Destination address: Street line 1.',
  115. 'type' => 'varchar',
  116. 'length' => 255,
  117. 'not null' => TRUE,
  118. 'default' => '',
  119. ),
  120. 'd_street2' => array(
  121. 'description' => 'Destination address: Street line 2.',
  122. 'type' => 'varchar',
  123. 'length' => 255,
  124. 'not null' => TRUE,
  125. 'default' => '',
  126. ),
  127. 'd_city' => array(
  128. 'description' => 'Destination address: City.',
  129. 'type' => 'varchar',
  130. 'length' => 255,
  131. 'not null' => TRUE,
  132. 'default' => '',
  133. ),
  134. 'd_zone' => array(
  135. 'description' => 'Destination address: State/province, from {uc_zones}.zone_id.',
  136. 'type' => 'int',
  137. 'size' => 'medium',
  138. 'unsigned' => TRUE,
  139. 'not null' => TRUE,
  140. 'default' => 0,
  141. ),
  142. 'd_postal_code' => array(
  143. 'description' => 'Destination address: Postal code.',
  144. 'type' => 'varchar',
  145. 'length' => 255,
  146. 'not null' => TRUE,
  147. 'default' => '',
  148. ),
  149. 'd_country' => array(
  150. 'description' => 'Destination address: Country, from {uc_countries}.country_id.',
  151. 'type' => 'int',
  152. 'size' => 'medium',
  153. 'unsigned' => TRUE,
  154. 'not null' => TRUE,
  155. 'default' => 0,
  156. ),
  157. 'shipping_method' => array(
  158. 'description' => 'The shipping method.',
  159. 'type' => 'varchar',
  160. 'length' => 255,
  161. 'not null' => TRUE,
  162. 'default' => '',
  163. ),
  164. 'accessorials' => array(
  165. 'description' => 'Shipping options and special instructions.',
  166. 'type' => 'varchar',
  167. 'length' => 255,
  168. 'not null' => TRUE,
  169. 'default' => '',
  170. ),
  171. 'carrier' => array(
  172. 'description' => 'The company making the delivery.',
  173. 'type' => 'varchar',
  174. 'length' => 255,
  175. 'not null' => TRUE,
  176. 'default' => '',
  177. ),
  178. 'transaction_id' => array(
  179. 'description' => "The carrier's shipment identifier.",
  180. 'type' => 'varchar',
  181. 'length' => 255,
  182. 'not null' => TRUE,
  183. 'default' => '',
  184. ),
  185. 'tracking_number' => array(
  186. 'description' => 'The number used by the carrier to locate the shipment while it is in transit.',
  187. 'type' => 'varchar',
  188. 'length' => 255,
  189. 'not null' => TRUE,
  190. 'default' => '',
  191. ),
  192. 'ship_date' => array(
  193. 'description' => 'The Unix timestamp indicating when the shipment left the origin address.',
  194. 'type' => 'int',
  195. 'not null' => TRUE,
  196. 'default' => 0,
  197. ),
  198. 'expected_delivery' => array(
  199. 'description' => 'The Unix timestamp indicating the expected date of delivery.',
  200. 'type' => 'int',
  201. 'not null' => TRUE,
  202. 'default' => 0,
  203. ),
  204. 'cost' => array(
  205. 'description' => 'The cost of the shipment.',
  206. 'type' => 'numeric',
  207. 'precision' => 16,
  208. 'scale' => 5,
  209. 'not null' => TRUE,
  210. 'default' => 0.0,
  211. ),
  212. 'changed' => array(
  213. 'description' => 'The Unix timestamp indicating the last time the shipment was modified.',
  214. 'type' => 'int',
  215. 'not null' => TRUE,
  216. 'default' => 0,
  217. ),
  218. ),
  219. 'primary key' => array('sid'),
  220. 'foreign keys' => array(
  221. 'uc_orders' => array(
  222. 'table' => 'uc_orders',
  223. 'columns' => array('order_id' => 'order_id'),
  224. ),
  225. ),
  226. 'indexes' => array(
  227. 'order_id' => array('order_id'),
  228. ),
  229. );
  230. $schema['uc_packages'] = array(
  231. 'description' => 'Stores shipment package information.',
  232. 'fields' => array(
  233. 'package_id' => array(
  234. 'description' => 'Primary key: the package ID.',
  235. 'type' => 'serial',
  236. 'unsigned' => TRUE,
  237. 'not null' => TRUE,
  238. ),
  239. 'order_id' => array(
  240. 'description' => 'The {uc_orders}.order_id.',
  241. 'type' => 'int',
  242. 'unsigned' => TRUE,
  243. 'not null' => TRUE,
  244. 'default' => 0,
  245. ),
  246. 'shipping_type' => array(
  247. 'description' => 'The basic type of shipment, e.g.: small package, freight, etc.',
  248. 'type' => 'varchar',
  249. 'length' => 255,
  250. 'not null' => TRUE,
  251. 'default' => '',
  252. ),
  253. 'pkg_type' => array(
  254. 'description' => 'The type of packaging.',
  255. 'type' => 'varchar',
  256. 'length' => 255,
  257. 'not null' => TRUE,
  258. 'default' => '',
  259. ),
  260. 'length' => array(
  261. 'description' => 'The package length.',
  262. 'type' => 'float',
  263. 'not null' => FALSE,
  264. ),
  265. 'width' => array(
  266. 'description' => 'The package width.',
  267. 'type' => 'float',
  268. 'not null' => FALSE,
  269. ),
  270. 'height' => array(
  271. 'description' => 'The package height.',
  272. 'type' => 'float',
  273. 'not null' => FALSE,
  274. ),
  275. 'length_units' => array(
  276. 'description' => 'The physical units of the length, width, and height.',
  277. 'type' => 'varchar',
  278. 'length' => 255,
  279. 'not null' => FALSE,
  280. ),
  281. 'value' => array(
  282. 'description' => 'The monetary value of the package contents.',
  283. 'type' => 'numeric',
  284. 'precision' => 16,
  285. 'scale' => 5,
  286. 'not null' => FALSE,
  287. 'default' => 0.0,
  288. ),
  289. 'sid' => array(
  290. 'description' => 'The {uc_shipments}.sid, if the package has been shipped.',
  291. 'type' => 'int',
  292. 'unsigned' => TRUE,
  293. 'not null' => FALSE,
  294. ),
  295. 'tracking_number' => array(
  296. 'description' => 'The package-specific tracking number, if available.',
  297. 'type' => 'varchar',
  298. 'length' => 255,
  299. 'not null' => FALSE,
  300. ),
  301. 'label_image' => array(
  302. 'description' => 'The {file}.fid that refers to an image of the shipping label of the package.',
  303. 'type' => 'int',
  304. 'unsigned' => TRUE,
  305. 'not null' => FALSE,
  306. ),
  307. ),
  308. 'primary key' => array('package_id'),
  309. 'foreign keys' => array(
  310. 'uc_orders' => array(
  311. 'table' => 'uc_orders',
  312. 'columns' => array('order_id' => 'order_id'),
  313. ),
  314. 'uc_quote_shipping_types' => array(
  315. 'table' => 'uc_quote_shipping_types',
  316. 'columns' => array('shipping_type' => 'shipping_type'),
  317. ),
  318. 'uc_shipments' => array(
  319. 'table' => 'uc_shipments',
  320. 'columns' => array('sid' => 'sid'),
  321. ),
  322. 'file' => array(
  323. 'table' => 'file',
  324. 'columns' => array('label_image' => 'fid'),
  325. ),
  326. ),
  327. 'indexes' => array(
  328. 'order_id' => array('order_id'),
  329. 'sid' => array('sid'),
  330. ),
  331. );
  332. $schema['uc_packaged_products'] = array(
  333. 'description' => 'Stores packaged product information.',
  334. 'fields' => array(
  335. 'package_id' => array(
  336. 'description' => 'The {uc_packages}.package_id in which the product is shipped.',
  337. 'type' => 'int',
  338. 'unsigned' => TRUE,
  339. 'not null' => TRUE,
  340. 'default' => 0,
  341. ),
  342. 'order_product_id' => array(
  343. 'description' => 'The {uc_order_products}.order_product_id of the ordered product.',
  344. 'type' => 'int',
  345. 'unsigned' => TRUE,
  346. 'not null' => TRUE,
  347. 'default' => 0,
  348. ),
  349. 'qty' => array(
  350. 'description' => 'The number of this product in this package.',
  351. 'type' => 'int',
  352. 'unsigned' => TRUE,
  353. 'not null' => TRUE,
  354. 'default' => 0,
  355. ),
  356. ),
  357. 'primary key' => array('package_id', 'order_product_id'),
  358. 'foreign keys' => array(
  359. 'package_id' => array('uc_packages' => 'package_id'),
  360. 'order_product_id' => array('uc_order_products' => 'order_product_id'),
  361. ),
  362. 'indexes' => array(
  363. 'order_product_id' => array('order_product_id'),
  364. ),
  365. );
  366. return $schema;
  367. }
  368. /**
  369. * Implements hook_update_last_removed().
  370. */
  371. function uc_shipping_update_last_removed() {
  372. return 6005;
  373. }
  374. /**
  375. * Changes {uc_packages}.label_image to an integer file id.
  376. */
  377. function uc_shipping_update_7000(&$sandbox) {
  378. $sandbox['#finished'] = 0;
  379. $schema = array(
  380. 'type' => 'int',
  381. 'unsigned' => TRUE,
  382. 'not null' => FALSE,
  383. );
  384. if (!isset($sandbox['total'])) {
  385. db_add_field('uc_packages', 'label_image_id', $schema);
  386. $sandbox['last'] = 0;
  387. $sandbox['count'] = 0;
  388. $sandbox['total'] = db_query("SELECT COUNT(*) FROM {uc_packages} WHERE label_image IS NOT NULL")->fetchField();
  389. }
  390. else {
  391. $found = FALSE;
  392. $scheme = variable_get('file_default_scheme', 'public');
  393. if ($sandbox['total']) {
  394. $limit = 200;
  395. $images = array();
  396. $packages = db_query_range("SELECT package_id, label_image FROM {uc_packages} WHERE package_id > :package_id AND label_image IS NOT NULL", 0, $limit, array(':package_id' => $sandbox['last']));
  397. foreach ($packages as $package) {
  398. $found = TRUE;
  399. $uri = file_stream_wrapper_uri_normalize($scheme . '://' . $package->label_image);
  400. $fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :uri", array(':uri' => $uri))->fetchField();
  401. if (!$fid) {
  402. $stat = stat($package->label_image);
  403. $info = getimagesize($package->label_image);
  404. $file = array(
  405. 'uid' => 1,
  406. 'filename' => basename($package->label_image),
  407. 'uri' => $uri,
  408. 'filemime' => $info['mime'],
  409. 'filesize' => $stat['size'],
  410. 'status' => FILE_STATUS_PERMANENT,
  411. 'timestamp' => $stat['ctime'],
  412. );
  413. $fid = db_insert('file_managed')->fields($file)->execute();
  414. }
  415. db_update('uc_packages')
  416. ->fields(array('label_image_id' => $fid))
  417. ->condition('package_id', $package->package_id)
  418. ->execute();
  419. $sandbox['last'] = $package->package_id;
  420. $sandbox['count']++;
  421. $sandbox['message'] = check_plain($package->label_image);
  422. }
  423. $sandbox['#finished'] = min(0.99, $sandbox['count'] / $sandbox['total']);
  424. }
  425. if (!$found) {
  426. db_drop_field('uc_packages', 'label_image');
  427. db_change_field('uc_packages', 'label_image_id', 'label_image', $schema, array(
  428. 'foreign keys' => array(
  429. 'label_image' => array('file_managed' => 'fid'),
  430. ),
  431. ));
  432. $sandbox['#finished'] = 1;
  433. return t('!number label images moved to the file table.', array('!number' => $sandbox['total']));
  434. }
  435. }
  436. }
  437. /**
  438. * Add 'changed' column to shipments.
  439. */
  440. function uc_shipping_update_7001() {
  441. if (!db_field_exists('uc_shipments', 'changed')) {
  442. db_add_field('uc_shipments', 'changed', array(
  443. 'description' => 'The Unix timestamp indicating the last time the shipment was modified.',
  444. 'type' => 'int',
  445. 'not null' => TRUE,
  446. 'default' => 0,
  447. ));
  448. }
  449. }
  450. /**
  451. * Add indexes to package and shipment tables.
  452. */
  453. function uc_shipping_update_7300() {
  454. // Alter {uc_shipments} table.
  455. if (!db_index_exists('uc_shipments', 'order_id')) {
  456. db_add_index('uc_shipments', 'order_id', array('order_id'));
  457. }
  458. // Alter {uc_packages} table.
  459. if (!db_index_exists('uc_packages', 'order_id')) {
  460. db_add_index('uc_packages', 'order_id', array('order_id'));
  461. }
  462. if (!db_index_exists('uc_packages', 'sid')) {
  463. db_add_index('uc_packages', 'sid', array('sid'));
  464. }
  465. // Alter {uc_packaged_products} table.
  466. if (!db_index_exists('uc_packaged_products', 'order_product_id')) {
  467. db_add_index('uc_packaged_products', 'order_product_id', array('order_product_id'));
  468. }
  469. }
  470. /**
  471. * Remove orphaned package and shipment records.
  472. */
  473. function uc_shipping_update_7301() {
  474. $subquery = db_select('uc_orders', 'o')
  475. ->fields('o', array('order_id'));
  476. db_delete('uc_shipments')
  477. ->condition('order_id', $subquery, 'NOT IN')
  478. ->execute();
  479. db_delete('uc_packages')
  480. ->condition('order_id', $subquery, 'NOT IN')
  481. ->execute();
  482. $subquery = db_select('uc_packages', 'p')
  483. ->fields('p', array('package_id'));
  484. db_delete('uc_packaged_products')
  485. ->condition('package_id', $subquery, 'NOT IN')
  486. ->execute();
  487. }