uc_shipping.rules.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @file
  4. * Rules hooks for uc_shipping.module.
  5. */
  6. /**
  7. * Implements hook_rules_data_info().
  8. */
  9. function uc_shipping_rules_data_info() {
  10. $address_info = uc_address_property_info();
  11. $entities['uc_shipment'] = array(
  12. 'label' => t('Ubercart shipment object'),
  13. 'group' => t('Ubercart'),
  14. 'wrap' => TRUE,
  15. 'property info' => array(
  16. 'sid' => array(
  17. 'type' => 'integer',
  18. 'label' => t('Shipment ID'),
  19. ),
  20. 'order_id' => array(
  21. 'type' => 'integer',
  22. 'label' => t('Order ID'),
  23. ),
  24. 'origin' => array(
  25. 'type' => 'struct',
  26. 'label' => t('Origin address'),
  27. 'description' => t('The origin location for the shipment.'),
  28. 'getter callback' => 'uc_shipping_address_property_get',
  29. 'setter callback' => 'uc_shipping_address_property_set',
  30. 'setter permission' => 'fulfill orders',
  31. 'property info' => $address_info,
  32. ),
  33. 'destination' => array(
  34. 'type' => 'struct',
  35. 'label' => t('Destination address'),
  36. 'description' => t('The destination location for the shipment.'),
  37. 'getter callback' => 'uc_shipping_address_property_get',
  38. 'setter callback' => 'uc_shipping_address_property_set',
  39. 'setter permission' => 'fulfill orders',
  40. 'property info' => $address_info,
  41. ),
  42. 'shipping_method' => array(
  43. 'type' => 'text',
  44. 'label' => t('Shipping method'),
  45. 'description' => t('The transportation method used to ship.'),
  46. ),
  47. 'accessorials' => array(
  48. 'type' => 'text',
  49. 'label' => t('Accessorials'),
  50. 'description' => t('Shipping options and special instructions.'),
  51. ),
  52. 'carrier' => array(
  53. 'type' => 'text',
  54. 'label' => t('Carrier'),
  55. 'description' => t('The company making the delivery.'),
  56. ),
  57. 'transaction_id' => array(
  58. 'type' => 'text',
  59. 'label' => t('Transaction ID'),
  60. 'description' => t("The carrier's shipment identifier."),
  61. ),
  62. 'tracking_number' => array(
  63. 'type' => 'text',
  64. 'label' => t('Tracking number'),
  65. 'description' => t('The number used by the carrier to locate the shipment while it is in transit.'),
  66. ),
  67. 'ship_date' => array(
  68. 'type' => 'date',
  69. 'label' => t('Ship date'),
  70. 'description' => t('The time the shipment was sent out.'),
  71. ),
  72. 'expected_delivery' => array(
  73. 'type' => 'date',
  74. 'label' => t('Expected delivery'),
  75. 'description' => t('The time the shipment is expected to be delivered'),
  76. ),
  77. 'cost' => array(
  78. 'type' => 'decimal',
  79. 'label' => t('Cost'),
  80. 'description' => t('The cost of the shipment.'),
  81. ),
  82. 'packages' => array(
  83. 'type' => 'list<struct>',
  84. 'label' => t('Packages'),
  85. 'description' => t('The physical items being shipped.'),
  86. 'property info' => array(
  87. 'package_id' => array(
  88. 'type' => 'integer',
  89. 'label' => t('Package ID'),
  90. ),
  91. 'shipping_type' => array(
  92. 'type' => 'text',
  93. 'label' => t('Shipping type'),
  94. 'description' => t('The basic type of shipment, e.g.: small package, freight, etc.'),
  95. ),
  96. 'pkg_type' => array(
  97. 'type' => 'text',
  98. 'label' => t('Package type'),
  99. 'description' => t('The type of packaging.'),
  100. ),
  101. 'length' => array(
  102. 'type' => 'decimal',
  103. 'label' => t('Length'),
  104. 'description' => t('The package length.'),
  105. ),
  106. 'width' => array(
  107. 'type' => 'decimal',
  108. 'label' => t('Width'),
  109. 'description' => t('The package width.'),
  110. ),
  111. 'height' => array(
  112. 'type' => 'decimal',
  113. 'label' => t('Height'),
  114. 'description' => t('The package height.'),
  115. ),
  116. 'value' => array(
  117. 'type' => 'decimal',
  118. 'label' => t('Value'),
  119. 'description' => t('The monetary value of the package contents.'),
  120. ),
  121. 'tracking_number' => array(
  122. 'type' => 'text',
  123. 'label' => t('Tracking number'),
  124. 'description' => t('The number used by the carrier to locate the shipment while it is in transit.'),
  125. ),
  126. 'description' => array(
  127. 'type' => 'text',
  128. 'label' => t('Description'),
  129. 'description' => t('The package description'),
  130. ),
  131. 'weight' => array(
  132. 'type' => 'decimal',
  133. 'label' => t('Weight'),
  134. 'description' => t('The physical weight of the package.'),
  135. ),
  136. 'products' => array(
  137. 'type' => 'list<uc_order_product>',
  138. 'label' => t('Products'),
  139. 'description' => t('The package contents.'),
  140. ),
  141. 'label_image' => array(
  142. 'type' => 'file',
  143. 'label' => t('Label image'),
  144. 'description' => t('An image of the shipping label.'),
  145. ),
  146. ),
  147. ),
  148. ),
  149. );
  150. return $entities;
  151. }
  152. /**
  153. * Entity metadata callback to get origin or destination address of an shipment.
  154. */
  155. function uc_shipping_address_property_get($shipment, array $options, $name, $entity_type) {
  156. switch ($name) {
  157. case 'origin':
  158. $type = 'o_';
  159. break;
  160. case 'destination':
  161. $type = 'd_';
  162. break;
  163. default:
  164. return NULL;
  165. }
  166. $address = new UcAddress();
  167. foreach ($address as $field => $value) {
  168. $address->{$field} = $shipment->{$type . $field};
  169. }
  170. return $address;
  171. }
  172. /**
  173. * Entity metadata callback to set origin or destination address of an order.
  174. */
  175. function uc_shipping_address_property_set($shipment, $name, $address) {
  176. switch ($name) {
  177. case 'origin':
  178. $type = 'o_';
  179. break;
  180. case 'destination':
  181. $type = 'd_';
  182. break;
  183. default:
  184. return;
  185. }
  186. foreach ($address as $field => $value) {
  187. $shipment->{$type . $field} = $value;
  188. }
  189. }
  190. /**
  191. * Implements hook_rules_event_info().
  192. */
  193. function uc_shipping_rules_event_info() {
  194. $events['uc_shipment_save'] = array(
  195. 'label' => t('A shipment is saved'),
  196. 'group' => t('Fulfillment'),
  197. 'variables' => array(
  198. 'order' => array(
  199. 'type' => 'uc_order',
  200. 'label' => t('Order'),
  201. ),
  202. 'shipment' => array(
  203. 'type' => 'uc_shipment',
  204. 'label' => t('Shipment'),
  205. ),
  206. ),
  207. );
  208. return $events;
  209. }