uc_shipping.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/ubercart/shipping/uc_shipping/uc_shipping.module
    2. 6 contributions/ubercart/shipping/uc_shipping/uc_shipping.module
    3. 7 contributions/ubercart/shipping/uc_shipping/uc_shipping.module

Organizes ordered products into packages and sets them up for shipment. Shipping method modules may add functionality to generate shipping labels and tracking numbers.

Coded by Lyle Mantooth.

Functions & methods

NameDescription
theme_uc_shipping_addressCompact the address into a table.
theme_uc_shipping_edit_package_fieldsetDisplay a formatted shipping type fieldset.
theme_uc_shipping_new_package_fieldsetFormat and display the products in a shipping type fieldset.
theme_uc_shipping_new_shipmentFormat and display the new shipment form.
theme_uc_shipping_package_dimensionsDisplay length, width, and height fields on one line.
uc_shipping_address_form
uc_shipping_make_shipmentDefault method to send packages on a shipment.
uc_shipping_menuImplementation of hook_shipping_menu().
uc_shipping_new_packagePut ordered products into a package.
uc_shipping_new_package_submitSubmit handler for uc_shipping_new_package().
uc_shipping_new_package_validateValidation handler for uc_shipping_new_package().
uc_shipping_new_shipmentSet up a new shipment with the chosen packages.
uc_shipping_new_shipment_submitSubmit handler for uc_shipping_new_shipment().
uc_shipping_order_actions
uc_shipping_order_packagesDisplay a list of an order's packaged products.
uc_shipping_order_pane
uc_shipping_order_pane_packages
uc_shipping_order_shipmentsDisplay a list of shipments for an order.
uc_shipping_package_cancel_confirm
uc_shipping_package_cancel_confirm_submit
uc_shipping_package_deleteDelete a package.
uc_shipping_package_delete_confirmDecide to unpackage products.
uc_shipping_package_delete_confirm_submitSubmit handler for uc_shipping_package_delete_confirm().
uc_shipping_package_editRearrange the products in or out of a package.
uc_shipping_package_edit_submitSubmit handler for uc_shipping_package_edit().
uc_shipping_package_loadLoad a package and its products.
uc_shipping_package_saveSave a package.
uc_shipping_package_viewDisplay the details of a package.
uc_shipping_permImplementation of hook_perm().
uc_shipping_select_addressChoose an address to fill out a form.
uc_shipping_shipment_deleteDelete a shipment.
uc_shipping_shipment_delete_confirmDecide to release packages to be put on another shipment.
uc_shipping_shipment_delete_confirm_submitSubmit handler for uc_shipping_shipment_delete_confirm().
uc_shipping_shipment_editCreate or edit a shipment.
uc_shipping_shipment_edit_submitSubmit handler for uc_shipping_shipment_edit().
uc_shipping_shipment_loadLoad a shipment and it's packages.
uc_shipping_shipment_saveSave a shipment.
uc_shipping_shipment_viewDisplay shipment details.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Organizes ordered products into packages and sets them up for shipment. Shipping
  5. * method modules may add functionality to generate shipping labels and tracking
  6. * numbers.
  7. *
  8. * Coded by Lyle Mantooth.
  9. */
  10. /******************************************************************************
  11. * Drupal hooks *
  12. ******************************************************************************/
  13. /**
  14. * Implementation of hook_shipping_menu().
  15. */
  16. function uc_shipping_menu($may_cache) {
  17. $items = array();
  18. if (!$may_cache) {
  19. if (is_numeric(arg(3))) {
  20. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/packages',
  21. 'access' => user_access('fulfill orders'),
  22. 'title' => t('Packages'),
  23. 'callback' => 'uc_shipping_order_packages',
  24. 'callback arguments' => array(arg(3)),
  25. 'weight' => 6,
  26. 'type' => MENU_LOCAL_TASK,
  27. );
  28. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/packages/new',
  29. 'access' => user_access('fulfill orders'),
  30. 'title' => t('New packages'),
  31. 'callback' => 'drupal_get_form',
  32. 'callback arguments' => array('uc_shipping_new_package', arg(3)),
  33. 'type' => MENU_CALLBACK,
  34. );
  35. if (is_numeric(arg(5))) {
  36. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/packages/'. arg(5) .'/edit',
  37. 'access' => user_access('fulfill orders'),
  38. 'title' => t('Edit package'),
  39. 'callback' => 'drupal_get_form',
  40. 'callback arguments' => array('uc_shipping_package_edit', arg(3), arg(5)),
  41. 'type' => MENU_CALLBACK,
  42. );
  43. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/packages/'. arg(5) .'/cancel',
  44. 'access' => user_access('fulfill orders'),
  45. 'title' => t('Cancel package shipment'),
  46. 'callback' => 'drupal_get_form',
  47. 'callback arguments' => array('uc_shipping_package_cancel_confirm', arg(3), arg(5)),
  48. 'type' => MENU_CALLBACK,
  49. );
  50. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/packages/'. arg(5) .'/delete',
  51. 'access' => user_access('fulfill orders'),
  52. 'title' => t('Delete package'),
  53. 'callback' => 'drupal_get_form',
  54. 'callback arguments' => array('uc_shipping_package_delete_confirm', arg(3), arg(5)),
  55. 'type' => MENU_CALLBACK,
  56. );
  57. }
  58. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/shipments',
  59. 'access' => user_access('fulfill orders'),
  60. 'title' => t('Shipments'),
  61. 'callback' => 'uc_shipping_order_shipments',
  62. 'callback arguments' => array(arg(3)),
  63. 'weight' => 7,
  64. 'type' => MENU_LOCAL_TASK,
  65. );
  66. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/shipments/new',
  67. 'access' => user_access('fulfill orders'),
  68. 'title' => t('New shipment'),
  69. 'callback' => 'drupal_get_form',
  70. 'callback arguments' => array('uc_shipping_new_shipment', arg(3)),
  71. 'type' => MENU_CALLBACK,
  72. );
  73. if (is_numeric(arg(5))) {
  74. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/shipments/'. arg(5),
  75. 'title' => t('Shipment !id', array('!id' => arg(5))),
  76. 'callback' => 'uc_shipping_shipment_view',
  77. 'callback arguments' => array(arg(3), arg(5)),
  78. 'type' => MENU_CALLBACK,
  79. );
  80. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/shipments/'. arg(5) .'/view',
  81. 'title' => t('View'),
  82. 'weight' => -5,
  83. 'type' => MENU_DEFAULT_LOCAL_TASK,
  84. );
  85. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/shipments/'. arg(5) .'/edit',
  86. 'access' => user_access('fulfill orders'),
  87. 'title' => t('Edit'),
  88. 'callback' => 'drupal_get_form',
  89. 'callback arguments' => array('uc_shipping_shipment_edit', arg(3), arg(5)),
  90. 'type' => MENU_LOCAL_TASK,
  91. );
  92. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/shipments/'. arg(5) .'/cancel',
  93. 'access' => user_access('fulfill orders'),
  94. 'title' => t('Cancel shipment'),
  95. 'callback' => 'drupal_get_form',
  96. 'callback arguments' => array('uc_shipping_shipment_cancel_confirm', arg(3), arg(5)),
  97. 'type' => MENU_CALLBACK,
  98. );
  99. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/shipments/'. arg(5) .'/delete',
  100. 'access' => user_access('fulfill orders'),
  101. 'title' => t('Delete shipment'),
  102. 'callback' => 'drupal_get_form',
  103. 'callback arguments' => array('uc_shipping_shipment_delete_confirm', arg(3), arg(5)),
  104. 'type' => MENU_CALLBACK,
  105. );
  106. }
  107. $items[] = array('path' => 'admin/store/orders/'. arg(3) .'/ship',
  108. 'access' => user_access('fulfill orders'),
  109. 'title' => t('Ship packages'),
  110. 'callback' => 'uc_shipping_make_shipment',
  111. 'callback arguments' => array(arg(3)),
  112. 'type' => MENU_CALLBACK,
  113. );
  114. }
  115. }
  116. return $items;
  117. }
  118. /**
  119. * Implementation of hook_perm().
  120. */
  121. function uc_shipping_perm() {
  122. return array('fulfill orders');
  123. }
  124. /******************************************************************************
  125. * Übercart hooks *
  126. ******************************************************************************/
  127. function uc_shipping_order_pane() {
  128. $panes[] = array(
  129. 'id' => 'packages',
  130. 'callback' => 'uc_shipping_order_pane_packages',
  131. 'title' => t('Tracking numbers'),
  132. 'desc' => t('Display tracking numbers of shipped packages.'),
  133. 'class' => 'pos-left',
  134. 'weight' => 7,
  135. 'show' => array('view', 'invoice', 'customer'),
  136. );
  137. return $panes;
  138. }
  139. function uc_shipping_order_actions($order) {
  140. $actions = array();
  141. $module_path = base_path() . drupal_get_path('module', 'uc_shipping');
  142. if (user_access('fulfill orders')) {
  143. $result = db_query("SELECT nid FROM {uc_order_products} WHERE order_id = %d AND data LIKE '%%s:9:\"shippable\";s:1:\"1\";%%'", $order->order_id);
  144. if (db_num_rows($result)) {
  145. $title = t('Package order !order_id products.', array('!order_id' => $order->order_id));
  146. $actions[] = array(
  147. 'name' => t('Package'),
  148. 'url' => 'admin/store/orders/'. $order->order_id .'/packages',
  149. 'icon' => '<img src="'. $module_path .'/images/package.gif" alt="'. $title .'" />',
  150. 'title' => $title,
  151. );
  152. $result = db_query("SELECT package_id FROM {uc_packages} WHERE order_id = %d", $order->order_id);
  153. if (db_num_rows($result)) {
  154. $title = t('Ship order !order_id packages.', array('!order_id' => $order->order_id));
  155. $actions[] = array(
  156. 'name' => t('Ship'),
  157. 'url' => 'admin/store/orders/'. $order->order_id .'/shipments',
  158. 'icon' => '<img src="'. $module_path .'/images/ship.gif" alt="'. $title .'" />',
  159. 'title' => $title,
  160. );
  161. }
  162. }
  163. }
  164. return $actions;
  165. }
  166. /******************************************************************************
  167. * Menu callbacks *
  168. ******************************************************************************/
  169. /**
  170. * Display a list of an order's packaged products.
  171. */
  172. function uc_shipping_order_packages($order_id) {
  173. $header = array(t('Package ID'), t('Products'), t('Shipping type'), t('Package type'), t('Shipment ID'), t('Tracking number'), t('Labels'), array('data' => t('Actions'), 'colspan' => 3));
  174. $rows = array();
  175. $result = db_query("SELECT * FROM {uc_packages} WHERE order_id = %d", $order_id);
  176. while ($package = db_fetch_object($result)) {
  177. $row = array();
  178. $row[] = $package->package_id;
  179. $product_list = array();
  180. $result2 = db_query("SELECT op.order_product_id, pp.qty, op.title, op.model FROM {uc_packaged_products} AS pp LEFT JOIN {uc_order_products} AS op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = %d", $package->package_id);
  181. while ($product = db_fetch_object($result2)) {
  182. $product_list[] = $product->qty .' x '. check_plain($product->model);
  183. }
  184. $row[] = '<ul><li>'. implode('</li><li>', $product_list) .'</li></ul>';
  185. $row[] = strtr($package->shipping_type, '_', ' ');
  186. $row[] = check_plain($package->pkg_type);
  187. $row[] = isset($package->sid) ? l($package->sid, 'admin/store/orders/'. $order_id .'/shipments/'. $package->sid .'/view') : '';
  188. $row[] = isset($package->tracking_number) ? check_plain($package->tracking_number) : '';
  189. if ($package->sid && $package->label_image) {
  190. $method = db_result(db_query("SELECT shipping_method FROM {uc_shipments} WHERE sid = %d", $package->sid));
  191. }
  192. $row[] = isset($package->label_image) ? l(theme('imagecache', 'uc_thumbnail', $package->label_image, t('Shipping Label'), t('Shipping Label')), 'admin/store/orders/'. $order_id .'/shipments/labels/'. $method .'/'. $package->label_image, array(), null, null, false, true) : '';
  193. $row[] = l(t('edit'), 'admin/store/orders/'. $order_id .'/packages/'. $package->package_id .'/edit');
  194. $row[] = l(t('delete'), 'admin/store/orders/'. $order_id .'/packages/'. $package->package_id .'/delete');
  195. if ($package->sid) {
  196. $row[] = l(t('cancel shipment'), 'admin/store/orders/'. $order_id .'/packages/'. $package->package_id .'/cancel');
  197. }
  198. else {
  199. $row[] = '';
  200. }
  201. $rows[] = $row;
  202. }
  203. if (empty($rows)) {
  204. $rows[][] = array('data' => t("This order's products have not been organized into packages."), 'colspan' => 10);
  205. }
  206. $output = theme('table', $header, $rows);
  207. $result = db_query("SELECT op.order_product_id, CAST(SUM(op.qty) / COUNT(pp.qty) AS UNSIGNED) AS total, SUM(pp.qty) AS packaged FROM {uc_order_products} AS op LEFT JOIN {uc_packaged_products} AS pp ON op.order_product_id = pp.order_product_id WHERE op.order_id = %d AND op.data LIKE '%%%s%%' GROUP BY op.order_product_id HAVING SUM(pp.qty) IS NULL OR CAST(SUM(op.qty) / COUNT(pp.qty) AS UNSIGNED) > SUM(pp.qty)", $order_id, 's:9:"shippable";s:1:"1";');
  208. if (db_num_rows($result)) {
  209. $output .= l(t('Create packages.'), 'admin/store/orders/'. $order_id .'/packages/new');
  210. }
  211. return $output;
  212. }
  213. /**
  214. * Put ordered products into a package.
  215. *
  216. * @ingroup forms
  217. * @see theme_uc_shipping_new_package_fieldset
  218. * @see uc_shipping_new_package_validate
  219. * @see uc_shipping_new_package_submit
  220. */
  221. function uc_shipping_new_package($order_id) {
  222. $breadcrumb = drupal_get_breadcrumb();
  223. $breadcrumb[] = l(t('Packages'), 'admin/store/orders/'. $order_id .'/packages');
  224. drupal_set_breadcrumb($breadcrumb);
  225. $form = array('#tree' => true);
  226. $form['instructions'] = array('#value' => t('Organize products into packages.
  227. Package numbers in multiple shipping types are of the first shipping type they appear in. All
  228. packages are given a unique ID when they are saved. Choose the default package "Sep." to
  229. automatically create a package for each of the selected quantity of products in that row.'));
  230. $order = uc_order_load($order_id);
  231. $shipping_types_products = array();
  232. foreach ($order->products as $product) {
  233. if ($product->data['shippable']) {
  234. $product->shipping_type = uc_product_get_shipping_type($product);
  235. $shipping_types_products[$product->shipping_type][] = $product;
  236. }
  237. }
  238. $shipping_type_weights = variable_get('uc_quote_type_weight', array());
  239. $packaged_products = array();
  240. $result = db_query("SELECT op.order_product_id, SUM(pp.qty) AS quantity FROM {uc_packaged_products} AS pp LEFT JOIN {uc_packages} AS p ON pp.package_id = p.package_id LEFT JOIN {uc_order_products} AS op ON op.order_product_id = pp.order_product_id WHERE p.order_id = %d GROUP BY op.order_product_id", $order_id);
  241. while ($boxed_product = db_fetch_object($result)) {
  242. $packaged_products[$boxed_product->order_product_id] = $boxed_product->quantity;
  243. }
  244. $form['shipping_types'] = array();
  245. foreach ($shipping_types_products as $shipping_type => $products) {
  246. $form['shipping_types'][$shipping_type] = array('#type' => 'fieldset',
  247. '#title' => ucwords(str_replace('_', ' ', $shipping_type)),
  248. '#collapsible' => true,
  249. '#collapsed' => false,
  250. '#weight' => $shipping_type_weights[$shipping_type],
  251. );
  252. foreach ($products as $product) {
  253. $unboxed_qty = $product->qty - $packaged_products[$product->order_product_id];
  254. if ($unboxed_qty > 0) {
  255. $product_row = array();
  256. $product_row['checked'] = array('#type' => 'checkbox', '#default_value' => 0);
  257. $product_row['model'] = array('#type' => 'markup', '#value' => check_plain($product->model));
  258. $product_row['name'] = array('#type' => 'markup', '#value' => filter_xss_admin($product->title));
  259. $product_row['qty'] = array('#type' => 'select',
  260. '#options' => drupal_map_assoc(uc_range(1, $unboxed_qty)),
  261. '#default_value' => $unboxed_qty,
  262. );
  263. $options = drupal_map_assoc(uc_range(0, count($order->products)));
  264. $options[0] = t('Sep.');
  265. $product_row['package'] = array('#type' => 'select',
  266. '#options' => $options,
  267. '#default_value' => 0,
  268. );
  269. $form['shipping_types'][$shipping_type][$product->order_product_id] = $product_row;
  270. }
  271. }
  272. $form['shipping_types'][$shipping_type]['#theme'] = 'uc_shipping_new_package_fieldset';
  273. }
  274. $form['order_id'] = array('#type' => 'hidden', '#value' => $order_id);
  275. $form['create'] = array('#type' => 'submit', '#value' => t('Make packages'));
  276. $form['combine'] = array('#type' => 'submit', '#value' => t('Create one package'));
  277. $form['cancel'] = array('#type' => 'submit', '#value' => t('Cancel'));
  278. return $form;
  279. }
  280. /**
  281. * Format and display the products in a shipping type fieldset.
  282. *
  283. * @ingroup themeable
  284. */
  285. function theme_uc_shipping_new_package_fieldset($fieldset) {
  286. $output = '';
  287. $header = array(theme('table_select_header_cell'), t('Model'), t('Title'), t('Qty'), t('Package'));
  288. $rows = array();
  289. foreach (element_children($fieldset) as $op_id) {
  290. $row = array();
  291. $row[] = drupal_render($fieldset[$op_id]['checked']);
  292. $row[] = drupal_render($fieldset[$op_id]['model']);
  293. $row[] = drupal_render($fieldset[$op_id]['name']);
  294. $row[] = drupal_render($fieldset[$op_id]['qty']);
  295. $row[] = drupal_render($fieldset[$op_id]['package']);
  296. $rows[] = $row;
  297. }
  298. $output .= theme('table', $header, $rows);
  299. $output .= drupal_render($fieldset);
  300. return $output;
  301. }
  302. /**
  303. * Validation handler for uc_shipping_new_package().
  304. *
  305. * Do not allow empty packages.
  306. */
  307. function uc_shipping_new_package_validate($form_id, $form_values) {
  308. if ($form_values['op'] != t('Cancel')) {
  309. $empty = true;
  310. foreach ($form_values['shipping_types'] as $shipping_type => $products) {
  311. foreach ($products as $product) {
  312. if ($product['checked'] != 0) {
  313. $empty = false;
  314. break 2;
  315. }
  316. }
  317. }
  318. if ($empty) {
  319. form_set_error($shipping_type, t('Packages should have at least one product in them.'));
  320. }
  321. }
  322. }
  323. /**
  324. * Submit handler for uc_shipping_new_package().
  325. */
  326. function uc_shipping_new_package_submit($form_id, $form_values) {
  327. if ($form_values['op'] != t('Cancel')) {
  328. $packages = array(0 => array());
  329. foreach ($form_values['shipping_types'] as $shipping_type => $products) {
  330. foreach ($products as $id => $product) {
  331. if ($product['checked']) {
  332. if ($form_values['op'] == t('Create one package')) {
  333. $product['package'] = 1;
  334. }
  335. if ($product['package'] != 0) {
  336. $packages[$product['package']]['products'][$id] = (object)$product;
  337. if (!isset($packages[$product['package']]['shipping_type'])) {
  338. $packages[$product['package']]['shipping_type'] = $shipping_type;
  339. }
  340. }
  341. else {
  342. $packages[0][$shipping_type][$id] = (object)$product;
  343. }
  344. }
  345. }
  346. if (is_array($packages[0][$shipping_type])) {
  347. foreach ($packages[0][$shipping_type] as $id => $product) {
  348. $qty = $product->qty;
  349. $product->qty = 1;
  350. for ($i = 0; $i < $qty; $i++) {
  351. $packages[] = array('products' => array($id => $product), 'shipping_type' => $shipping_type);
  352. }
  353. }
  354. }
  355. unset($packages[0][$shipping_type]);
  356. }
  357. if (empty($packages[0])) {
  358. unset($packages[0]);
  359. }
  360. foreach ($packages as $package) {
  361. $package['order_id'] = $form_values['order_id'];
  362. uc_shipping_package_save($package);
  363. }
  364. }
  365. return 'admin/store/orders/'. $form_values['order_id'] .'/packages';
  366. }
  367. /**
  368. * Display the details of a package.
  369. */
  370. function uc_shipping_package_view($package_id) {
  371. $package = uc_shipping_package_load($package_id);
  372. $shipment = uc_shipping_shipment_load($package->sid);
  373. $output = '';
  374. $rows = array();
  375. $output .= '<div class="order-pane pos-left"><div class="order-pane-title">'. t('Package %id:', array('%id' => $package_id)) .'</div>';
  376. $rows[] = array(t('Contents:'), filter_xss_admin($package->description));
  377. if ($shipment) {
  378. $methods = module_invoke_all('shipping_method');
  379. $method = $methods[$shipment->shipping_method];
  380. $pkg_type = $method['ship']['pkg_types'][$package->pkg_type];
  381. }
  382. $rows[] = array(t('Package type:'), strlen($pkg_type) ? $pkg_type : check_plain($package->pkg_type));
  383. if ($package->length && $package->width && $package->height) {
  384. $rows[] = array(t('Dimensions:'), t('!l x !w x !h', array('!l' => uc_length_format($package->length), '!w' => uc_length_format($package->width), '!h' => uc_length_format($package->height))));
  385. }
  386. $rows[] = array(t('Insured value:'), uc_currency_format($package->value));
  387. if ($package->tracking_number) {
  388. $rows[] = array(t('Tracking number:'), check_plain($package->tracking_number));
  389. }
  390. if ($shipment && $package->label_image && file_exists($package->label_image)) {
  391. $rows[] = array(t('Label:'), l(t('Click to view.'), 'admin/store/orders/'. $package->order_id .'/shipments/labels/'. $shipment->shipping_method .'/'. $package->label_image));
  392. }
  393. else {
  394. $rows[] = array(t('Label:'), t('n/a'));
  395. }
  396. $output .= theme('table', array(), $rows, array('style' => 'width:auto;'));
  397. $output .= '</div>';
  398. return $output;
  399. }
  400. /**
  401. * Rearrange the products in or out of a package.
  402. *
  403. * @ingroup forms
  404. * @see theme_uc_shipping_edit_package_fieldset
  405. * @see uc_shipping_package_edit_submit
  406. */
  407. function uc_shipping_package_edit($order_id, $package_id) {
  408. $package = uc_shipping_package_load($package_id);
  409. $products = array();
  410. $order = uc_order_load($order_id);
  411. $shipping_types_products = array();
  412. foreach ($order->products as $product) {
  413. if ($product->data['shippable']) {
  414. $product->shipping_type = uc_product_get_shipping_type($product);
  415. $shipping_types_products[$product->shipping_type][$product->order_product_id] = $product;
  416. $products[$product->order_product_id] = $product;
  417. }
  418. }
  419. $result = db_query("SELECT order_product_id, SUM(qty) AS quantity FROM {uc_packaged_products} AS pp LEFT JOIN {uc_packages} AS p ON pp.package_id = p.package_id WHERE p.order_id = %d GROUP BY order_product_id", $order_id);
  420. while ($packaged_product = db_fetch_object($result)) {
  421. //Make already packaged products unavailable, except those in this package.
  422. $products[$packaged_product->order_product_id]->qty = $products[$packaged_product->order_product_id]->qty - $packaged_product->quantity + $package->products[$packaged_product->order_product_id]->qty;
  423. }
  424. $form = array();
  425. $form['#tree'] = true;
  426. $form['package_id'] = array('#type' => 'hidden', '#value' => $package_id);
  427. $form['products'] = array();
  428. foreach ($products as $product) {
  429. if ($product->qty > 0) {
  430. $product_row = array();
  431. $product_row['checked'] = array('#type' => 'checkbox', '#default_value' => isset($package->products[$product->order_product_id]));
  432. $product_row['model'] = array('#type' => 'markup', '#value' => check_plain($product->model));
  433. $product_row['name'] = array('#type' => 'markup', '#value' => filter_xss_admin($product->title));
  434. $product_row['qty'] = array('#type' => 'select',
  435. '#options' => drupal_map_assoc(uc_range(1, $product->qty)),
  436. '#default_value' => $package->products[$product->order_product_id]->qty,
  437. );
  438. $form['products'][$product->order_product_id] = $product_row;
  439. }
  440. }
  441. $form['products']['#theme'] = 'uc_shipping_edit_package_fieldset';
  442. $options = array();
  443. foreach (array_keys($shipping_types_products) as $type) {
  444. $options[$type] = ucwords(str_replace('_', ' ', $type));
  445. }
  446. $form['shipping_type'] = array('#type' => 'select',
  447. '#title' => t('Shipping type'),
  448. '#options' => $options,
  449. '#default_value' => $package->shipping_type,
  450. );
  451. $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
  452. $form['cancel'] = array('#type' => 'submit', '#value' => t('Cancel'));
  453. return $form;
  454. }
  455. /**
  456. * Display a formatted shipping type fieldset.
  457. *
  458. * @ingroup themeable
  459. */
  460. function theme_uc_shipping_edit_package_fieldset($fieldset) {
  461. $output = '';
  462. $header = array(theme('table_select_header_cell'), t('Model'), t('Title'), t('Qty'));
  463. $rows = array();
  464. foreach (element_children($fieldset) as $op_id) {
  465. $row = array();
  466. $row[] = drupal_render($fieldset[$op_id]['checked']);
  467. $row[] = drupal_render($fieldset[$op_id]['model']);
  468. $row[] = drupal_render($fieldset[$op_id]['name']);
  469. $row[] = drupal_render($fieldset[$op_id]['qty']);
  470. $rows[] = $row;
  471. }
  472. $output .= theme('table', $header, $rows);
  473. $output .= drupal_render($fieldset);
  474. return $output;
  475. }
  476. /**
  477. * Submit handler for uc_shipping_package_edit().
  478. */
  479. function uc_shipping_package_edit_submit($form_id, $form_values) {
  480. $package = uc_shipping_package_load($form_values['package_id']);
  481. if ($form_values['op'] != t('Cancel')) {
  482. foreach ($form_values['products'] as $id => $product) {
  483. if ($product['checked']) {
  484. $package->products[$id] = (object)$product;
  485. }
  486. else {
  487. unset($package->products[$id]);
  488. }
  489. }
  490. $package->shipping_type = $form_values['shipping_type'];
  491. uc_shipping_package_save($package);
  492. }
  493. return 'admin/store/orders/'. $package->order_id .'/packages';
  494. }
  495. function uc_shipping_package_cancel_confirm($order_id, $package_id) {
  496. $form = array();
  497. $form['order_id'] = array('#type' => 'value', '#value' => $order_id);
  498. $form['package_id'] = array('#type' => 'value', '#value' => $package_id);
  499. $output = confirm_form($form, t('Are you sure you want to cancel the shipment of this package?'), 'admin/store/orders/'. $order_id .'/packages',
  500. t('It will be available for reshipment.'), t('Cancel shipment'), t('Nevermind'));
  501. return $output;
  502. }
  503. function uc_shipping_package_cancel_confirm_submit($form_id, $form_values) {
  504. $package = uc_shipping_package_load($form_values['package_id']);
  505. $shipment = uc_shipping_shipment_load($package->sid);
  506. $methods = module_invoke_all('shipping_method');
  507. if (function_exists($methods[$shipment->shipping_method]['cancel'])) {
  508. $result = call_user_func($methods[$shipment->shipping_method]['cancel'], $shipment->tracking_number, array($package->tracking_number));
  509. if ($result) {
  510. db_query("UPDATE {uc_packages} SET sid = NULL, label_image = NULL, tracking_number = NULL WHERE package_id = %d", $package->package_id);
  511. unset($shipment->packages[$package->package_id]);
  512. if (!count($shipment->packages)) {
  513. uc_shipping_shipment_delete($shipment->sid);
  514. }
  515. }
  516. }
  517. return 'admin/store/orders/'. $form_values['order_id'] .'/packages';
  518. }
  519. /**
  520. * Decide to unpackage products.
  521. *
  522. * @ingroup forms
  523. * @see uc_shipping_package_delete_confirm_submit
  524. */
  525. function uc_shipping_package_delete_confirm($order_id, $package_id) {
  526. $form = array();
  527. $form['order_id'] = array('#type' => 'value', '#value' => $order_id);
  528. $form['package_id'] = array('#type' => 'value', '#value' => $package_id);
  529. $output = confirm_form($form, t('Are you sure you want to delete this package?'), 'admin/store/orders/'. $order_id .'/packages',
  530. t('The products it contains will be available for repackaging.'), t('Delete'), t('Cancel'));
  531. return $output;
  532. }
  533. /**
  534. * Submit handler for uc_shipping_package_delete_confirm().
  535. */
  536. function uc_shipping_package_delete_confirm_submit($form_id, $form_values) {
  537. uc_shipping_package_delete($form_values['package_id']);
  538. return 'admin/store/orders/'. $form_values['order_id'] .'/packages';
  539. }
  540. /**
  541. * Display a list of shipments for an order.
  542. *
  543. * @param $order_id
  544. * The order's id.
  545. */
  546. function uc_shipping_order_shipments($order_id) {
  547. $result = db_query("SELECT * FROM {uc_shipments} WHERE order_id = %d", $order_id);
  548. $header = array(t('Shipment ID'), t('Name'), t('Company'), t('Destination'), t('Ship date'), t('Estimated delivery'), t('Tracking number'),array('data' => t('Actions'), 'colspan' => 3));
  549. $rows = array();
  550. while ($shipment = db_fetch_object($result)) {
  551. $row = array();
  552. $row[] = $shipment->sid;
  553. $row[] = check_plain($shipment->d_first_name) .' '. check_plain($shipment->d_last_name);
  554. $row[] = check_plain($shipment->d_company);
  555. $row[] = check_plain($shipment->d_city) .', '. uc_get_zone_code($shipment->d_zone) .' '. check_plain($shipment->d_postal_code);
  556. $row[] = format_date($shipment->ship_date, 'custom', variable_get('uc_date_format_default', 'm/d/Y'));
  557. $row[] = format_date($shipment->expected_delivery, 'custom', variable_get('uc_date_format_default', 'm/d/Y'));
  558. $row[] = is_null($shipment->tracking_number) ? t('n/a') : check_plain($shipment->tracking_number);
  559. $row[] = l(t('view'), 'admin/store/orders/'. $order_id .'/shipments/'. $shipment->sid .'/view');
  560. $row[] = l(t('edit'), 'admin/store/orders/'. $order_id .'/shipments/'. $shipment->sid .'/edit');
  561. $row[] = l(t('delete'), 'admin/store/orders/'. $order_id .'/shipments/'. $shipment->sid .'/delete');
  562. $rows[] = $row;
  563. }
  564. if (empty($rows)) {
  565. $rows[] = array(array('data' => t('No shipments have been made for this order.'), 'colspan' => 10));
  566. }
  567. $output = theme('table', $header, $rows);
  568. $packages = db_num_rows(db_query("SELECT * FROM {uc_packages} WHERE order_id = %d AND sid IS NULL", $order_id));
  569. if ($packages) {
  570. $output .= l(t('Make a new shipment'), 'admin/store/orders/'. $order_id .'/shipments/new');
  571. }
  572. else {
  573. $result = db_query("SELECT op.order_product_id, CAST(SUM(op.qty) / COUNT(pp.qty) AS UNSIGNED) AS total, SUM(pp.qty) AS packaged FROM {uc_order_products} AS op LEFT JOIN {uc_packaged_products} AS pp ON op.order_product_id = pp.order_product_id WHERE op.order_id = %d AND op.data LIKE '%%%s%%' GROUP BY op.order_product_id HAVING SUM(pp.qty) IS NULL OR CAST(SUM(op.qty) / COUNT(pp.qty) AS UNSIGNED) > SUM(pp.qty)", $order_id, 's:9:"shippable";s:1:"1";');
  574. if (db_num_rows($result)) {
  575. $output .= l(t('Put products into packages to make shipments.'), 'admin/store/orders/'. $order_id .'/packages/new');
  576. }
  577. }
  578. return $output;
  579. }
  580. /**
  581. * Set up a new shipment with the chosen packages.
  582. *
  583. * @ingroup forms
  584. * @see theme_uc_shipping_new_shipment
  585. * @see uc_shipping_new_shipment_submit
  586. */
  587. function uc_shipping_new_shipment($order_id) {
  588. $breadcrumb = drupal_get_breadcrumb();
  589. $breadcrumb[] = l(t('Shipments'), 'admin/store/orders/'. $order_id .'/shipments');
  590. drupal_set_breadcrumb($breadcrumb);
  591. $form = array('#tree' => true);
  592. $form['order_id'] = array('#type' => 'hidden', '#value' => $order_id);
  593. $packages_by_type = array();
  594. $result = db_query("SELECT * FROM {uc_packages} WHERE order_id = %d AND sid IS NULL", $order_id);
  595. while ($package = db_fetch_object($result)) {
  596. $products = array();
  597. $weight = 0;
  598. $result2 = db_query("SELECT pp.order_product_id, pp.qty, pp.qty * op.weight AS weight, op.title, op.model FROM {uc_packaged_products} AS pp LEFT JOIN {uc_order_products} AS op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = %d", $package->package_id);
  599. while ($product = db_fetch_object($result2)) {
  600. $weight += $product->weight;
  601. $products[$product->order_product_id] = $product;
  602. }
  603. $package->weight = $weight;
  604. $package->products = $products;
  605. $packages_by_type[$package->shipping_type][$package->package_id] = $package;
  606. }
  607. $option_methods = array();
  608. $shipping_types = module_invoke_all('shipping_type');
  609. $shipping_methods = module_invoke_all('shipping_method');
  610. $shipping_methods_by_type = array();
  611. foreach ($shipping_methods as $method) {
  612. if (isset($method['ship'])) {
  613. $shipping_methods_by_type[$method['ship']['type']][] = $method;
  614. }
  615. }
  616. foreach ($packages_by_type as $shipping_type => $packages) {
  617. $form[$shipping_type] = array('#type' => 'fieldset',
  618. '#title' => $shipping_types[$shipping_type]['title'],
  619. );
  620. $form[$shipping_type]['packages'] = array();
  621. foreach ($packages as $package) {
  622. $pkgs_exist = true;
  623. $package_row = array();
  624. $package_row['checked'] = array('#type' => 'checkbox', '#default_value' => 0);
  625. $package_row['package_id'] = array('#value' => $package->package_id);
  626. $product_list = array();
  627. foreach ($package->products as $product) {
  628. $product_list[] = $product->qty .' x '. check_plain($product->model);
  629. }
  630. $package_row['products'] = array('#value' => '<ul><li>'. implode('</li><li>', $product_list) .'</li></ul>');
  631. $package_row['weight'] = array('#value' => $package->weight);
  632. $form[$shipping_type]['packages'][$package->package_id] = $package_row;
  633. }
  634. if (is_array($shipping_methods_by_type[$shipping_type])) {
  635. foreach ($shipping_methods_by_type[$shipping_type] as $method) {
  636. $option_methods += array($method['id'] => $method['title']);
  637. }
  638. }
  639. }
  640. if ($pkgs_exist) {
  641. $option_methods = array('all' => t('Ship Manually')) + $option_methods;
  642. $form['method'] = array('#type' => 'select',
  643. '#title' => t('Shipping method'),
  644. '#options' => $option_methods,
  645. '#default_value' => 'all',
  646. );
  647. $form['ship'] = array('#type' => 'submit',
  648. '#value' => t('Ship packages'),
  649. );
  650. }
  651. //drupal_set_message('<pre>'. print_r($form, true) .'</pre>');
  652. return $form;
  653. }
  654. /**
  655. * Format and display the new shipment form.
  656. *
  657. * @ingroup themeable
  658. */
  659. function theme_uc_shipping_new_shipment($form) {
  660. $output = '';
  661. $header = array(theme('table_select_header_cell'), t('Package'), t('Products'), t('Weight'));
  662. foreach (element_children($form) as $shipping_type) {
  663. $rows = array();
  664. foreach (element_children($form[$shipping_type]['packages']) as $package_id) {
  665. $row = array();
  666. $row[] = drupal_render($form[$shipping_type]['packages'][$package_id]['checked']);
  667. $row[] = drupal_render($form[$shipping_type]['packages'][$package_id]['package_id']);
  668. $row[] = drupal_render($form[$shipping_type]['packages'][$package_id]['products']);
  669. $row[] = drupal_render($form[$shipping_type]['packages'][$package_id]['weight']);
  670. $rows[] = $row;
  671. }
  672. if (count($rows)) {
  673. $form[$shipping_type]['packages']['table'] = array('#value' => theme('table', $header, $rows));
  674. }
  675. //$output .= drupal_render($form[$shipping_type]['methods']);
  676. }
  677. $output .= drupal_render($form);
  678. return $output;
  679. }
  680. /**
  681. * Submit handler for uc_shipping_new_shipment().
  682. *
  683. * Send package information to the chosen method.
  684. *
  685. * @see uc_shipping_make_shipment
  686. */
  687. function uc_shipping_new_shipment_submit($form_id, $form_values) {
  688. $packages = array();
  689. foreach ($form_values as $shipping_type) {
  690. if (is_array($shipping_type['packages'])) {
  691. foreach ($shipping_type['packages'] as $id => $input) {
  692. if ($input['checked']) {
  693. $packages[] = $id;
  694. }
  695. }
  696. }
  697. }
  698. return 'admin/store/orders/'. $form_values['order_id'] .'/ship/'. $form_values['method'] .'/'. implode('/', $packages);
  699. }
  700. /**
  701. * Default method to send packages on a shipment.
  702. */
  703. function uc_shipping_make_shipment() {
  704. $args = func_get_args();
  705. //print_r($args, true));
  706. if (count($args) > 2) {
  707. $order_id = array_shift($args);
  708. $method_id = array_shift($args);
  709. $package_ids = $args;
  710. $methods = module_invoke_all('shipping_method');
  711. $method = $methods[$method_id];
  712. if (isset($method)) {
  713. return drupal_get_form($method['ship']['callback'], $order_id, $package_ids);
  714. }
  715. else {
  716. $shipment = new stdClass();
  717. $shipment->order_id = $order_id;
  718. $shipment->packages = array();
  719. foreach ($package_ids as $id) {
  720. $package = uc_shipping_package_load($id);
  721. $shipment->packages[$id] = $package;
  722. }
  723. return drupal_get_form('uc_shipping_shipment_edit', $order_id, $shipment);
  724. }
  725. }
  726. else {
  727. drupal_set_message(t('There is no sense in making a shipment with no packages on it, right?'));
  728. drupal_goto('admin/store/orders/'. $args[0]);
  729. }
  730. }
  731. /**
  732. * Display shipment details.
  733. */
  734. function uc_shipping_shipment_view($order_id, $sid) {
  735. $breadcrumb = drupal_get_breadcrumb();
  736. $breadcrumb[] = l(t('Shipments'), 'admin/store/orders/'. $order_id .'/shipments');
  737. drupal_set_breadcrumb($breadcrumb);
  738. $shipment = uc_shipping_shipment_load($sid);
  739. $output = '';
  740. $origin = uc_order_address($shipment, 'o');
  741. $destination = uc_order_address($shipment, 'd');
  742. $output .= '<div class="order-pane pos-left"><div class="order-pane-title">'. t('Pickup Address:') .'</div>'. $origin .'</div>';
  743. $output .= '<div class="order-pane pos-left"><div class="order-pane-title">'. t('Delivery Address:') .'</div>'. $destination .'</div>';
  744. $output .= '<div class="order-pane abs-left"><div class="order-pane-title">'. t('Schedule:') .'</div>';
  745. $rows = array();
  746. $rows[] = array(t('Ship date:'), format_date($shipment->ship_date, 'custom', 'D, '. variable_get('uc_date_format_default', 'm/d/Y')));
  747. $rows[] = array(t('Expected delivery:'), format_date($shipment->expected_delivery, 'custom', 'D, '. variable_get('uc_date_format_default', 'm/d/Y')));
  748. $output .= theme('table', array(), $rows, array('style' => 'width:auto'));
  749. $output .= '</div>';
  750. $output .= '<div class="order-pane abs-left"><div class="order-pane-title">'. t('Shipment Details:') .'</div>';
  751. $rows = array();
  752. $rows[] = array(t('Carrier:'), check_plain($shipment->carrier));
  753. if ($shipment->transaction_id) {
  754. $rows[] = array(t('Transaction ID:'), check_plain($shipment->transaction_id));
  755. }
  756. if ($shipment->tracking_number) {
  757. $rows[] = array(t('Tracking Number:'), check_plain($shipment->tracking_number));
  758. }
  759. $methods = module_invoke_all('shipping_method');
  760. $method = $methods[$shipment->shipping_method];
  761. if (isset($method['quote']['accessorials'][$shipment->accessorials])) {
  762. $rows[] = array(t('Services:'), $method['quote']['accessorials'][$shipment->accessorials]);
  763. }
  764. else {
  765. $rows[] = array(t('Services:'), $shipment->accessorials);
  766. }
  767. $rows[] = array(t('Cost:'), uc_currency_format($shipment->cost));
  768. $output .= theme('table', array(), $rows, array('style' => 'width:auto'));
  769. $output .= '</div>';
  770. foreach ($shipment->packages as $package) {
  771. $output .= uc_shipping_package_view($package->package_id);
  772. }
  773. return $output;
  774. }
  775. /**
  776. * Create or edit a shipment.
  777. *
  778. * @ingroup forms
  779. * @see theme_uc_shipping_package_dimensions
  780. * @see theme_uc_shipping_address
  781. * @see uc_shipping_shipment_edit_submit
  782. */
  783. function uc_shipping_shipment_edit($order_id, $shipment) {
  784. drupal_add_css(drupal_get_path('module', 'uc_shipping') .'/uc_shipping.css');
  785. $order = uc_order_load($order_id);
  786. if (is_numeric($shipment)) {
  787. $shipment = uc_shipping_shipment_load($shipment);
  788. }
  789. $form = array();
  790. $form['order_id'] = array('#type' => 'value', '#value' => $order_id);
  791. if (isset($shipment->sid)) {
  792. $form['sid'] = array('#type' => 'value', '#value' => $shipment->sid);
  793. $methods = module_invoke_all('shipping_method');
  794. $method = $methods[$shipment->shipping_method];
  795. }
  796. $addresses = array();
  797. $form['packages'] = array(
  798. '#tree' => true,
  799. '#weight' => 1,
  800. );
  801. if ($shipment->o_street1) {
  802. $o_address = new stdClass();
  803. foreach ($shipment as $field => $value) {
  804. if (substr($field, 0, 2) == 'o_') {
  805. $o_address->{substr($field, 2)} = $value;
  806. }
  807. }
  808. $addresses[] = $o_address;
  809. }
  810. foreach ($shipment->packages as $id => $package) {
  811. foreach ($package->addresses as $address) {
  812. if (!in_array($address, $addresses)) {
  813. $addresses[] = $address;
  814. }
  815. }
  816. $declared_value = 0;
  817. $product_list = array();
  818. foreach ($package->products as $product) {
  819. $product_list[] = $product->qty .' x '. check_plain($product->model);
  820. $declared_value += $product->qty * $product->price;
  821. }
  822. $form['packages'][$id] = array('#type' => 'fieldset',
  823. '#title' => t('Package @id', array('@id' => $id)),
  824. '#collapsible' => true,
  825. );
  826. $form['packages'][$id]['products'] = array('#value' => theme('item_list', $product_list));
  827. $form['packages'][$id]['pkg_type'] = array('#type' => 'textfield',
  828. '#title' => t('Package type'),
  829. '#default_value' => $package->pkg_type,
  830. '#description' => t('E.g.: Box, pallet, tube, treasure chest, cocoon, etc.'),
  831. );
  832. if (isset($method) && is_array($method['ship']['pkg_types'])) {
  833. $form['packages'][$id]['pkg_type']['#type'] = 'select';
  834. $form['packages'][$id]['pkg_type']['#options'] = $method['ship']['pkg_types'];
  835. $form['packages'][$id]['pkg_type']['#description'] = '';
  836. }
  837. $form['packages'][$id]['dimensions'] = array('#type' => 'fieldset',
  838. '#title' => t('Dimensions'),
  839. '#description' => t('Physical dimensions of the packaged product.'),
  840. '#theme' => 'uc_shipping_package_dimensions',
  841. );
  842. $form['packages'][$id]['dimensions']['units'] = array('#type' => 'select',
  843. '#title' => t('Units of measurement'),
  844. '#options' => array(
  845. 'in' => t('Inches'),
  846. 'ft' => t('Feet'),
  847. 'cm' => t('Centimeters'),
  848. 'mm' => t('Millimeters'),
  849. ),
  850. '#default_value' => $package->length_units ? $package->length_units : variable_get('uc_length_unit', 'in'),
  851. );
  852. $form['packages'][$id]['dimensions']['length'] = array('#type' => 'textfield',
  853. '#title' => t('Length'),
  854. '#default_value' => $package->length,
  855. '#size' => 8,
  856. );
  857. $form['packages'][$id]['dimensions']['width'] = array('#type' => 'textfield',
  858. '#title' => t('Width'),
  859. '#default_value' => $package->width,
  860. '#size' => 8,
  861. );
  862. $form['packages'][$id]['dimensions']['height'] = array('#type' => 'textfield',
  863. '#title' => t('Height'),
  864. '#default_value' => $package->height,
  865. '#size' => 8,
  866. );
  867. $form['packages'][$id]['declared_value'] = array('#type' => 'textfield',
  868. '#title' => t('Declared value'),
  869. '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
  870. '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
  871. '#default_value' => isset($package->value) ? $package->value : $declared_value,
  872. );
  873. $form['packages'][$id]['tracking_number'] = array('#type' => 'textfield',
  874. '#title' => t('Tracking number'),
  875. '#default_value' => $package->tracking_number,
  876. );
  877. }
  878. $form = array_merge($form, uc_shipping_address_form($addresses, $order));
  879. $form['shipment'] = array('#type' => 'fieldset',
  880. '#title' => t('Shipment data'),
  881. '#collapsible' => true,
  882. '#weight' => 0,
  883. );
  884. $form['shipment']['shipping_method'] = array('#type' => 'hidden',
  885. '#value' => isset($shipment->shipping_method) ? $shipment->shipping_method : 'manual',
  886. );
  887. $form['shipment']['carrier'] = array('#type' => 'textfield',
  888. '#title' => t('Carrier'),
  889. '#default_value' => $shipment->carrier,
  890. );
  891. $form['shipment']['accessorials'] = array('#type' => 'textfield',
  892. '#title' => t('Shipment options'),
  893. '#default_value' => $shipment->accessorials,
  894. '#description' => t('Short notes about the shipment, e.g. residential, overnight, etc.'),
  895. );
  896. $form['shipment']['transaction_id'] = array('#type' => 'textfield',
  897. '#title' => t('Transaction ID'),
  898. '#default_value' => $shipment->transaction_id,
  899. );
  900. $form['shipment']['tracking_number'] = array('#type' => 'textfield',
  901. '#title' => t('Tracking number'),
  902. '#default_value' => $shipment->tracking_number,
  903. );
  904. if (isset($shipment->ship_date)) {
  905. $ship_date = getdate($shipment->ship_date);
  906. }
  907. else {
  908. $ship_date = getdate();
  909. }
  910. if (isset($shipment->expected_delivery)) {
  911. $exp_delivery = getdate($shipment->expected_delivery);
  912. }
  913. else {
  914. $exp_delivery = getdate();
  915. }
  916. $form['shipment']['ship_date'] = array('#type' => 'date',
  917. '#title' => t('Ship date'),
  918. '#default_value' => array('year' => $ship_date['year'], 'month' => $ship_date['mon'], 'day' => $ship_date['mday']),
  919. );
  920. $form['shipment']['expected_delivery'] = array('#type' => 'date',
  921. '#title' => t('Expected delivery'),
  922. '#default_value' => array('year' => $exp_delivery['year'], 'month' => $exp_delivery['mon'], 'day' => $exp_delivery['mday']),
  923. );
  924. $form['shipment']['cost'] = array('#type' => 'textfield',
  925. '#title' => t('Shipping cost'),
  926. '#default_value' => $shipment->cost,
  927. '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
  928. '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
  929. );
  930. $form['submit'] = array('#type' => 'submit', '#value' => t('Save shipment'), '#weight' => 10);
  931. return $form;
  932. }
  933. /**
  934. * Display length, width, and height fields on one line.
  935. *
  936. * @ingroup themeable
  937. */
  938. function theme_uc_shipping_package_dimensions($form) {
  939. $output = '';
  940. $row = array();
  941. foreach (element_children($form) as $dimension) {
  942. $row[] = drupal_render($form[$dimension]);
  943. }
  944. $output .= theme('table', array(), array($row));
  945. return $output;
  946. }
  947. /**
  948. * Compact the address into a table.
  949. *
  950. * @ingroup themeable
  951. */
  952. function theme_uc_shipping_address($address) {
  953. drupal_add_css(drupal_get_path('module', 'uc_cart') .'/uc_cart.css');
  954. if ($address['#collapsed']) {
  955. $collapsed = ' collapsed';
  956. }
  957. $output = '<table class="pane-table" cellpadding="2">';
  958. $req = '<span class="form-required">*</span>';
  959. foreach (element_children($address) as $field) {
  960. list($type, $name) = explode('_', $field, 2);
  961. if ($address !== NULL) {
  962. $title = $address[$field]['#title'] .':';
  963. unset($address[$field]['#title']);
  964. if ($name == 'street1') {
  965. $title = uc_get_field_name('street') .':';
  966. }
  967. elseif ($name == 'street2') {
  968. $title = ' ';
  969. }
  970. $output .= '<tr><td class="field-label">';
  971. if ($address[$field]['#required']) {
  972. $output .= $req;
  973. }
  974. $output .= $title .'</td><td>'
  975. . drupal_render($address[$field])
  976. .'</td></tr>';
  977. }
  978. }
  979. $output .= '</table>';
  980. foreach (element_children($address) as $element) {
  981. $output .= drupal_render($address[$element]);
  982. }
  983. return $output;
  984. }
  985. /**
  986. * Submit handler for uc_shipping_shipment_edit().
  987. */
  988. function uc_shipping_shipment_edit_submit($form_id, $form_values) {
  989. if ($form_values['op'] != t('Cancel')) {
  990. $shipment = new stdClass();
  991. $shipment->order_id = $form_values['order_id'];
  992. if (isset($form_values['sid'])) {
  993. $shipment->sid = $form_values['sid'];
  994. }
  995. $shipment->origin = new stdClass();
  996. $shipment->destination = new stdClass();
  997. foreach ($form_values as $key => $value) {
  998. if (substr($key, 0, 7) == 'pickup_') {
  999. $field = substr($key, 7);
  1000. $shipment->origin->$field = $value;
  1001. }
  1002. else if (substr($key, 0, 9) == 'delivery_') {
  1003. $field = substr($key, 9);
  1004. $shipment->destination->$field = $value;
  1005. }
  1006. }
  1007. $shipment->packages = array();
  1008. foreach ($form_values['packages'] as $id => $pkg_form) {
  1009. $package = uc_shipping_package_load($id);
  1010. $package->pkg_type = $pkg_form['pkg_type'];
  1011. $package->value = $pkg_form['declared_value'];
  1012. $package->length = $pkg_form['dimensions']['length'];
  1013. $package->width = $pkg_form['dimensions']['width'];
  1014. $package->height = $pkg_form['dimensions']['height'];
  1015. $package->length_units = $pkg_form['dimensions']['units'];
  1016. $package->tracking_number = $pkg_form['tracking_number'];
  1017. $package->qty = 1;
  1018. $shipment->packages[$id] = $package;
  1019. }
  1020. $shipment->shipping_method = $form_values['shipping_method'];
  1021. $shipment->accessorials = $form_values['accessorials'];
  1022. $shipment->carrier = $form_values['carrier'];
  1023. $shipment->transaction_id = $form_values['transaction_id'];
  1024. $shipment->tracking_number = $form_values['tracking_number'];
  1025. $shipment->ship_date = gmmktime(12, 0, 0, $form_values['ship_date']['month'], $form_values['ship_date']['day'], $form_values['ship_date']['year']);
  1026. $shipment->expected_delivery = gmmktime(12, 0, 0, $form_values['expected_delivery']['month'], $form_values['expected_delivery']['day'], $form_values['expected_delivery']['year']);
  1027. $shipment->cost = $form_values['cost'];
  1028. uc_shipping_shipment_save($shipment);
  1029. }
  1030. return 'admin/store/orders/'. $form_values['order_id'] .'/shipments';
  1031. }
  1032. /**
  1033. * Decide to release packages to be put on another shipment.
  1034. *
  1035. * @ingroup forms
  1036. * @see uc_shipping_shipment_delete_confirm_submit
  1037. */
  1038. function uc_shipping_shipment_delete_confirm($order_id, $sid) {
  1039. $form = array();
  1040. $form['order_id'] = array('#type' => 'value', '#value' => $order_id);
  1041. $form['sid'] = array('#type' => 'value', '#value' => $sid);
  1042. $output = confirm_form($form, t('Are you sure you want to delete this shipment?'), 'admin/store/orders/'. $order_id .'/shipments',
  1043. t('The shipment will be canceled and the packages it contains will be available for reshipment.'), t('Delete'), t('Cancel'));
  1044. return $output;
  1045. }
  1046. /**
  1047. * Submit handler for uc_shipping_shipment_delete_confirm().
  1048. */
  1049. function uc_shipping_shipment_delete_confirm_submit($form_id, $form_values) {
  1050. $shipment = uc_shipping_shipment_load($form_values['sid']);
  1051. $methods = module_invoke_all('shipping_method');
  1052. if ($shipment->tracking_number && function_exists($methods[$shipment->shipping_method]['cancel'])) {
  1053. $result = call_user_func($methods[$shipment->shipping_method]['cancel'], $shipment->tracking_number);
  1054. if ($result) {
  1055. uc_shipping_shipment_delete($form_values['sid']);
  1056. }
  1057. else {
  1058. drupal_set_message(t('The shipment %tracking could not be canceled with %carrier. To delete it anyway, remove the tracking number and try again.', array('%tracking' => $shipment->tracking_number, '%carrier' => $shipment->carrier)));
  1059. }
  1060. }
  1061. else {
  1062. uc_shipping_shipment_delete($form_values['sid']);
  1063. }
  1064. return 'admin/store/orders/'. $form_values['order_id'] .'/shipments';
  1065. }
  1066. /******************************************************************************
  1067. * Module and helper functions *
  1068. ******************************************************************************/
  1069. /**
  1070. * Load a package and its products.
  1071. */
  1072. function uc_shipping_package_load($package_id) {
  1073. static $packages = array();
  1074. if (!isset($packages[$package_id])) {
  1075. $products = array();
  1076. $descripion = '';
  1077. $weight = 0;
  1078. $units = variable_get('uc_weight_unit', 'lb');
  1079. $addresses = array();
  1080. $result = db_query("SELECT op.order_product_id, pp.qty, pp.qty * op.weight AS weight, p.weight_units, op.nid, op.title, op.model, op.price FROM {uc_packaged_products} AS pp LEFT JOIN {uc_order_products} AS op ON op.order_product_id = pp.order_product_id LEFT JOIN {uc_products} AS p ON op.nid = p.nid WHERE pp.package_id = %d", $package_id);
  1081. while ($product = db_fetch_object($result)) {
  1082. $address = uc_quote_get_default_shipping_address($product->nid);
  1083. // TODO: Lodge complaint that array_unique() compares as strings.
  1084. if (!in_array($address, $addresses)) {
  1085. $addresses[] = $address;
  1086. }
  1087. $description .= ', '. $product->qty .' x '. $product->model;
  1088. // Normalize all weights to default units.
  1089. $weight += $product->weight * uc_weight_conversion($product->weight_units, $units);
  1090. $products[$product->order_product_id] = $product;
  1091. }
  1092. $result = db_query("SELECT * FROM {uc_packages} WHERE package_id = %d", $package_id);
  1093. $packages[$package_id] = db_fetch_object($result);
  1094. $packages[$package_id]->addresses = $addresses;
  1095. $packages[$package_id]->description = substr($description, 2);
  1096. $packages[$package_id]->weight = $weight;
  1097. $packages[$package_id]->weight_units = $units;
  1098. $packages[$package_id]->products = $products;
  1099. }
  1100. return $packages[$package_id];
  1101. }
  1102. /**
  1103. * Save a package.
  1104. */
  1105. function uc_shipping_package_save($package) {
  1106. $package = (object)$package;
  1107. if (!isset($package->package_id)) {
  1108. $package->package_id = db_next_id('{uc_packages}_package_id');
  1109. db_query("INSERT INTO {uc_packages} (package_id, order_id) VALUES (%d, %d)", $package->package_id, $package->order_id);
  1110. }
  1111. if (count($package->products)) {
  1112. $types = array();
  1113. $values = array();
  1114. foreach ($package->products as $id => $product) {
  1115. $types[] = '(%d, %d, %d)';
  1116. $values[] = $package->package_id;
  1117. $values[] = $id;
  1118. $values[] = $product->qty;
  1119. $result = db_query("SELECT data FROM {uc_order_products} WHERE order_product_id = %d", $id);
  1120. if ($order_product = db_fetch_object($result)) {
  1121. $order_product->data = unserialize($order_product->data);
  1122. $order_product->data['package_id'] = intval($package->package_id);
  1123. db_query("UPDATE {uc_order_products} SET data = '%s' WHERE order_product_id = %d", serialize($order_product->data), $id);
  1124. }
  1125. }
  1126. db_query("DELETE FROM {uc_packaged_products} WHERE package_id = %d", $package->package_id);
  1127. db_query("INSERT INTO {uc_packaged_products} (package_id, order_product_id, qty) VALUES ". implode(',', $types), $values);
  1128. }
  1129. $types = array("shipping_type = '%s'");
  1130. $values = array($package->shipping_type);
  1131. if (isset($package->pkg_type)) {
  1132. $types[] = "pkg_type = '%s'";
  1133. $values[] = $package->pkg_type;
  1134. }
  1135. if (isset($package->length) && isset($package->width) && isset($package->height) && isset($package->length_units)) {
  1136. array_push($types, 'length = %f', 'width = %f', 'height = %f', "length_units = '%s'");
  1137. array_push($values, $package->length, $package->width, $package->height, $package->length_units);
  1138. }
  1139. if (isset($package->value)) {
  1140. $types[] = 'value = %f';
  1141. $values[] = $package->value;
  1142. }
  1143. if (isset($package->sid)) {
  1144. $types[] = 'sid = %d';
  1145. $values[] = $package->sid;
  1146. }
  1147. if (isset($package->tracking_number)) {
  1148. $types[] = "tracking_number = '%s'";
  1149. $values[] = $package->tracking_number;
  1150. }
  1151. if (isset($package->label_image)) {
  1152. $types[] = "label_image = '%s'";
  1153. $values[] = $package->label_image;
  1154. }
  1155. $values[] = $package->package_id;
  1156. if (count($types)) {
  1157. // Let it be known that I think it's ridiculous that Drupal doesn't put NULL into its database. --JLM
  1158. db_query("UPDATE {uc_packages} SET ". implode(',', $types) ." WHERE package_id = %d", $values);
  1159. }
  1160. }
  1161. /**
  1162. * Delete a package.
  1163. */
  1164. function uc_shipping_package_delete($package_id) {
  1165. db_query("DELETE FROM {uc_packages} WHERE package_id = %d", $package_id);
  1166. db_query("DELETE FROM {uc_packaged_products} WHERE package_id = %d", $package_id);
  1167. drupal_set_message(t('Package @id has been deleted.', array('@id' => $package_id)));
  1168. }
  1169. /**
  1170. * Load a shipment and it's packages.
  1171. */
  1172. function uc_shipping_shipment_load($shipment_id) {
  1173. $shipment = db_fetch_object(db_query("SELECT * FROM {uc_shipments} WHERE sid = %d", $shipment_id));
  1174. if ($shipment) {
  1175. $result = db_query("SELECT package_id FROM {uc_packages} WHERE sid = %d", $shipment_id);
  1176. $packages = array();
  1177. while ($package = db_fetch_object($result)) {
  1178. $packages[$package->package_id] = uc_shipping_package_load($package->package_id);
  1179. }
  1180. $shipment->packages = $packages;
  1181. $extra = module_invoke_all('shipment', 'load', $shipment);
  1182. if (is_array($extra)) {
  1183. foreach ($extra as $key => $value) {
  1184. $shipment->$key = $value;
  1185. }
  1186. }
  1187. }
  1188. return $shipment;
  1189. }
  1190. /**
  1191. * Save a shipment.
  1192. */
  1193. function uc_shipping_shipment_save($shipment) {
  1194. if (!$shipment->sid) {
  1195. $shipment->sid = db_next_id('{uc_shipments}_sid');
  1196. db_query("INSERT INTO {uc_shipments} (sid, order_id) VALUES (%d, %d)", $shipment->sid, $shipment->order_id);
  1197. $shipment->is_new = TRUE;
  1198. }
  1199. else {
  1200. $shipment->is_new = FALSE;
  1201. }
  1202. if (is_array($shipment->packages)) {
  1203. foreach ($shipment->packages as $package) {
  1204. $package->sid = $shipment->sid;
  1205. // Since the products haven't changed, we take them out of the object so that they are not deleted and re-inserted.
  1206. $products = $package->products;
  1207. unset($package->products);
  1208. uc_shipping_package_save($package);
  1209. // But they're still necessary for hook_shipment(), so they're added back in.
  1210. $package->products = $products;
  1211. }
  1212. }
  1213. if (isset($shipment->origin)) {
  1214. foreach ($shipment->origin as $field => $value) {
  1215. $field = 'o_'. $field;
  1216. $shipment->$field = $value;
  1217. }
  1218. }
  1219. if (isset($shipment->destination)) {
  1220. foreach ($shipment->destination as $field => $value) {
  1221. $field = 'd_'. $field;
  1222. $shipment->$field = $value;
  1223. }
  1224. }
  1225. db_query("UPDATE {uc_shipments} SET order_id = %d, o_first_name = '%s', o_last_name = '%s', o_company = '%s', o_street1 = '%s', o_street2 = '%s', o_city = '%s', o_zone = %d, o_postal_code = '%s', o_country = %d, d_first_name = '%s', d_last_name = '%s', d_company = '%s', d_street1 = '%s', d_street2 = '%s', d_city = '%s', d_zone = %d, d_postal_code = '%s', d_country = %d, shipping_method = '%s', accessorials = '%s', carrier = '%s', transaction_id = '%s', tracking_number = '%s', ship_date = %d, expected_delivery = %d, cost = %f WHERE sid = %d",
  1226. $shipment->order_id, $shipment->o_first_name, $shipment->o_last_name, $shipment->o_company, $shipment->o_street1, $shipment->o_street2, $shipment->o_city, $shipment->o_zone, $shipment->o_postal_code, $shipment->o_country, $shipment->d_first_name, $shipment->d_last_name, $shipment->d_company, $shipment->d_street1, $shipment->d_street2, $shipment->d_city, $shipment->d_zone, $shipment->d_postal_code, $shipment->d_country, $shipment->shipping_method, $shipment->accessorials, $shipment->carrier, $shipment->transaction_id, $shipment->tracking_number, $shipment->ship_date, $shipment->expected_delivery, $shipment->cost, $shipment->sid
  1227. );
  1228. module_invoke_all('shipment', 'save', $shipment);
  1229. }
  1230. /**
  1231. * Delete a shipment.
  1232. */
  1233. function uc_shipping_shipment_delete($shipment_id) {
  1234. $shipment = uc_shipping_shipment_load($shipment_id);
  1235. foreach ($shipment->packages as $package) {
  1236. if (file_exists($package->label_image)) {
  1237. file_delete($package->label_image);
  1238. }
  1239. }
  1240. db_query("UPDATE {uc_packages} SET sid = NULL, tracking_number = NULL, label_image = NULL WHERE sid = %d", $shipment_id);
  1241. db_query("DELETE FROM {uc_shipments} WHERE sid = %d", $shipment_id);
  1242. module_invoke_all('shipment', 'delete', $shipment);
  1243. }
  1244. function uc_shipping_order_pane_packages($op, $arg1) {
  1245. switch ($op) {
  1246. case 'view':
  1247. case 'customer':
  1248. $tracking = array();
  1249. $result = db_query("SELECT sid FROM {uc_shipments} WHERE order_id = %d", $arg1->order_id);
  1250. while ($shipment = db_fetch_object($result)) {
  1251. $shipment = uc_shipping_shipment_load($shipment->sid);
  1252. if ($shipment->tracking_number) {
  1253. $tracking[$shipment->carrier]['children'][] = check_plain($shipment->tracking_number);
  1254. }
  1255. else {
  1256. foreach ($shipment->packages as $package) {
  1257. if ($package->tracking_number) {
  1258. $tracking[$shipment->carrier]['children'][] = check_plain($package->tracking_number);
  1259. }
  1260. }
  1261. }
  1262. }
  1263. $output = '';
  1264. foreach ($tracking as $carrier => $item) {
  1265. $output .= '<strong>'. $carrier .':</strong>'. theme('item_list', $item);
  1266. }
  1267. return $output;
  1268. break;
  1269. }
  1270. }
  1271. /**
  1272. * Choose an address to fill out a form.
  1273. */
  1274. function uc_shipping_select_address($addresses, $onchange = '', $title = NULL, $icon_suffix = FALSE) {
  1275. if (!is_array($addresses) || count($addresses) == 0) {
  1276. $addresses = array();
  1277. }
  1278. $store_address = variable_get('uc_quote_store_default_address', new stdClass());
  1279. if (!in_array($store_address, $addresses)) {
  1280. $addresses[] = $store_address;
  1281. }
  1282. $blank = array('first_name' => '',
  1283. 'last_name' => '',
  1284. 'phone' => '',
  1285. 'company' => '',
  1286. 'street1' => '',
  1287. 'street2' => '',
  1288. 'city' => '',
  1289. 'postal_code' => '',
  1290. 'country' => 0,
  1291. 'zone' => 0,
  1292. );
  1293. $options = array(drupal_to_js($blank) => t('<Reset fields>'));
  1294. foreach ($addresses as $address) {
  1295. $options[drupal_to_js($address)] = $address->company .' '. $address->street1 .' '. $address->city;
  1296. }
  1297. $select = array(
  1298. '#type' => 'select',
  1299. '#title' => is_null($title) ? t('Address book') : $title,
  1300. '#options' => $options,
  1301. '#default_value' => drupal_to_js($addresses[0]),
  1302. '#attributes' => array('onchange' => $onchange),
  1303. '#suffix' => $icon_suffix ? uc_store_get_icon('file:address_book', FALSE, 'address-book-icon') : NULL,
  1304. );
  1305. return $select;
  1306. }
  1307. function uc_shipping_address_form($addresses, $order) {
  1308. uc_add_js(drupal_get_path('module', 'uc_shipping') .'/uc_shipping.js');
  1309. $form = array();
  1310. $form['origin'] = array('#type' => 'fieldset',
  1311. '#title' => t('Origin address'),
  1312. '#collapsible' => true,
  1313. '#collapsed' => false,
  1314. '#weight' => -2,
  1315. '#theme' => 'uc_shipping_address',
  1316. );
  1317. $address = reset($addresses);
  1318. $form['origin']['pickup_address_select'] = uc_shipping_select_address($addresses, 'apply_address(\'pickup\', this.value);', t('Saved Addresses'), TRUE);
  1319. $form['origin']['pickup_address_select']['#weight'] = -2;
  1320. $form['origin']['pickup_email'] = uc_textfield(uc_get_field_name('email'), variable_get('uc_store_email', null), FALSE);
  1321. $form['origin']['pickup_email']['#weight'] = -1;
  1322. $form['origin']['pickup_first_name'] = uc_textfield(uc_get_field_name('first_name'), $address->first_name, FALSE);
  1323. $form['origin']['pickup_last_name'] = uc_textfield(uc_get_field_name('last_name'), $address->last_name, FALSE);
  1324. $form['origin']['pickup_phone'] = uc_textfield(uc_get_field_name('phone'), variable_get('uc_store_phone', null), FALSE, NULL, 32, 16);
  1325. $form['origin']['pickup_company'] = uc_textfield(uc_get_field_name('company'), $address->company, FALSE);
  1326. $form['origin']['pickup_street1'] = uc_textfield(uc_get_field_name('street1'), $address->street1, FALSE, NULL, 64);
  1327. $form['origin']['pickup_street2'] = uc_textfield(uc_get_field_name('street2'), $address->street2, FALSE, NULL, 64);
  1328. $form['origin']['pickup_city'] = uc_textfield(uc_get_field_name('city'), $address->city, FALSE);
  1329. $form['origin']['pickup_country'] = uc_country_select(uc_get_field_name('country'), $address->country);
  1330. if (isset($_POST['pickup_country'])) {
  1331. $country = $_POST['pickup_country'];
  1332. }
  1333. else {
  1334. $country = $address->country;
  1335. }
  1336. $form['origin']['pickup_zone'] = uc_zone_select(uc_get_field_name('zone'), $address->zone, null, $country);
  1337. $form['origin']['pickup_postal_code'] = uc_textfield(uc_get_field_name('postal_code'), $address->postal_code, FALSE, NULL, 10, 10);
  1338. $order_form = uc_order_pane_ship_to('edit-form', $order);
  1339. $form['destination'] = $order_form['ship_to'];
  1340. $form['destination']['delivery_email'] = uc_textfield(uc_get_field_name('email'), $order->primary_email, FALSE);
  1341. $form['destination']['delivery_email']['#weight'] = -1;
  1342. $form['destination']['#title'] = t('Destination Address');
  1343. $form['destination']['#collapsible'] = true;
  1344. $form['destination']['#weight'] = -1;
  1345. $form['destination']['#theme'] = 'uc_shipping_address';
  1346. return $form;
  1347. }