uc_shipping.admin.inc 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. <?php
  2. /**
  3. * @file
  4. * Shipping administration menu items.
  5. */
  6. /**
  7. * Displays a list of an order's packaged products.
  8. */
  9. function uc_shipping_order_packages($order) {
  10. $shipping_type_options = uc_quote_shipping_type_options();
  11. $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' => 4));
  12. $rows = array();
  13. $result = db_query("SELECT * FROM {uc_packages} WHERE order_id = :id", array(':id' => $order->order_id));
  14. foreach ($result as $package) {
  15. $row = array();
  16. $row[] = $package->package_id;
  17. $product_list = array();
  18. $result2 = db_query("SELECT op.order_product_id, pp.qty, op.title, op.model FROM {uc_packaged_products} pp LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = :id", array(':id' => $package->package_id));
  19. foreach ($result2 as $product) {
  20. $product_list[] = $product->qty . ' x ' . check_plain($product->model);
  21. }
  22. $row[] = '<ul><li>' . implode('</li><li>', $product_list) . '</li></ul>';
  23. $row[] = isset($shipping_type_options[$package->shipping_type]) ? $shipping_type_options[$package->shipping_type] : strtr($package->shipping_type, '_', ' ');
  24. $row[] = check_plain($package->pkg_type);
  25. $row[] = isset($package->sid) ? l($package->sid, 'admin/store/orders/' . $order->order_id . '/shipments/' . $package->sid . '/view') : '';
  26. $row[] = isset($package->tracking_number) ? check_plain($package->tracking_number) : '';
  27. if ($package->label_image && $image = file_load($package->label_image)) {
  28. $package->label_image = $image;
  29. }
  30. else {
  31. unset($package->label_image);
  32. }
  33. if (isset($package->sid) && isset($package->label_image)) {
  34. $method = db_query("SELECT shipping_method FROM {uc_shipments} WHERE sid = :sid", array(':sid' => $package->sid))->fetchField();
  35. $row[] = l(
  36. theme('image_style', array(
  37. 'style_name' => 'uc_thumbnail',
  38. 'path' => $package->label_image->uri,
  39. 'alt' => t('Shipping label'),
  40. 'title' => t('Shipping label'),
  41. )),
  42. 'admin/store/orders/' . $order->order_id . '/shipments/labels/' . $method . '/' . $package->label_image->uri,
  43. array('html' => TRUE)
  44. );
  45. }
  46. else {
  47. $row[] = '';
  48. }
  49. $row[] = l(t('edit'), 'admin/store/orders/' . $order->order_id . '/packages/' . $package->package_id . '/edit');
  50. $row[] = l(t('ship'), 'admin/store/orders/' . $order->order_id . '/shipments/new', array('query' => array('pkgs' => array($package->package_id))));
  51. $row[] = l(t('delete'), 'admin/store/orders/' . $order->order_id . '/packages/' . $package->package_id . '/delete');
  52. if ($package->sid) {
  53. $row[] = l(t('cancel shipment'), 'admin/store/orders/' . $order->order_id . '/packages/' . $package->package_id . '/cancel');
  54. }
  55. else {
  56. $row[] = '';
  57. }
  58. $rows[] = $row;
  59. }
  60. if (empty($rows)) {
  61. drupal_set_message(t("This order's products have not been organized into packages."));
  62. drupal_goto('admin/store/orders/' . $order->order_id . '/packages/new');
  63. }
  64. $build['packages'] = array(
  65. '#theme' => 'table',
  66. '#header' => $header,
  67. '#rows' => $rows,
  68. );
  69. return $build;
  70. }
  71. /**
  72. * Puts ordered products into a package.
  73. *
  74. * @see uc_shipping_new_package_validate()
  75. * @see uc_shipping_new_package_submit()
  76. * @see theme_uc_shipping_new_package_fieldset()
  77. * @ingroup forms
  78. */
  79. function uc_shipping_new_package($form, &$form_state, $order) {
  80. $form['#tree'] = TRUE;
  81. $shipping_types_products = array();
  82. foreach ($order->products as $product) {
  83. if ($product->data['shippable']) {
  84. $product->shipping_type = uc_product_get_shipping_type($product);
  85. $shipping_types_products[$product->shipping_type][] = $product;
  86. }
  87. }
  88. $shipping_type_weights = variable_get('uc_quote_type_weight', array());
  89. $result = db_query("SELECT op.order_product_id, SUM(pp.qty) AS quantity FROM {uc_packaged_products} pp LEFT JOIN {uc_packages} p ON pp.package_id = p.package_id LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE p.order_id = :id GROUP BY op.order_product_id", array(':id' => $order->order_id));
  90. $packaged_products = $result->fetchAllKeyed();
  91. $form['shipping_types'] = array();
  92. $shipping_type_options = uc_quote_shipping_type_options();
  93. foreach ($shipping_types_products as $shipping_type => $products) {
  94. $form['shipping_types'][$shipping_type] = array(
  95. '#type' => 'fieldset',
  96. '#title' => isset($shipping_type_options[$shipping_type]) ?
  97. $shipping_type_options[$shipping_type] :
  98. ucwords(str_replace('_', ' ', $shipping_type)),
  99. '#collapsible' => TRUE,
  100. '#collapsed' => FALSE,
  101. '#weight' => isset($shipping_type_weights[$shipping_type]) ? $shipping_type_weights[$shipping_type] : 0,
  102. );
  103. foreach ($products as $product) {
  104. $unboxed_qty = $product->qty;
  105. if (isset($packaged_products[$product->order_product_id])) {
  106. $unboxed_qty -= $packaged_products[$product->order_product_id];
  107. }
  108. if ($unboxed_qty > 0) {
  109. $product_row = array();
  110. $product_row['checked'] = array(
  111. '#type' => 'checkbox',
  112. '#default_value' => 0,
  113. );
  114. $product_row['model'] = array(
  115. '#markup' => check_plain($product->model)
  116. );
  117. $product_row['name'] = array(
  118. '#markup' => filter_xss_admin($product->title)
  119. );
  120. $product_row['qty'] = array(
  121. '#type' => 'select',
  122. '#title' => theme('uc_qty_label'),
  123. '#title_display' => 'invisible',
  124. '#options' => drupal_map_assoc(range(1, $unboxed_qty)),
  125. '#default_value' => $unboxed_qty,
  126. );
  127. $options = drupal_map_assoc(range(0, count($order->products)));
  128. $options[0] = t('Sep.');
  129. $product_row['package'] = array(
  130. '#type' => 'select',
  131. '#title' => t('Package'),
  132. '#title_display' => 'invisible',
  133. '#options' => $options,
  134. '#default_value' => 0,
  135. );
  136. $form['shipping_types'][$shipping_type][$product->order_product_id] = $product_row;
  137. }
  138. }
  139. $form['shipping_types'][$shipping_type]['#theme'] = 'uc_shipping_new_package_fieldset';
  140. }
  141. $form['order_id'] = array(
  142. '#type' => 'hidden',
  143. '#value' => $order->order_id,
  144. );
  145. $form['actions'] = array(
  146. '#type' => 'actions',
  147. );
  148. $form['actions']['create'] = array(
  149. '#type' => 'submit',
  150. '#value' => t('Make packages'),
  151. );
  152. $form['actions']['combine'] = array(
  153. '#type' => 'submit',
  154. '#value' => t('Create one package'),
  155. );
  156. $form['actions']['cancel'] = array(
  157. '#type' => 'submit',
  158. '#value' => t('Cancel'),
  159. );
  160. return $form;
  161. }
  162. /**
  163. * Formats and displays the products in a shipping type fieldset.
  164. *
  165. * @see uc_shipping_new_package()
  166. * @ingroup themeable
  167. */
  168. function theme_uc_shipping_new_package_fieldset($variables) {
  169. $fieldset = $variables['fieldset'];
  170. drupal_add_js('misc/tableselect.js');
  171. $output = '';
  172. $header = array(array('class' => array('select-all')), t('SKU'), t('Title'), theme('uc_qty_label'), t('Package'));
  173. $rows = array();
  174. foreach (element_children($fieldset) as $op_id) {
  175. $row = array();
  176. $row[] = drupal_render($fieldset[$op_id]['checked']);
  177. $row[] = drupal_render($fieldset[$op_id]['model']);
  178. $row[] = drupal_render($fieldset[$op_id]['name']);
  179. $row[] = drupal_render($fieldset[$op_id]['qty']);
  180. $row[] = drupal_render($fieldset[$op_id]['package']);
  181. $rows[] = $row;
  182. }
  183. $output .= theme('table', array(
  184. 'header' => $header,
  185. 'rows' => $rows,
  186. 'empty' => t('There are no products available for this type of package.'),
  187. ));
  188. $output .= drupal_render_children($fieldset);
  189. return $output;
  190. }
  191. /**
  192. * Validation handler for uc_shipping_new_package().
  193. *
  194. * Do not allow empty packages.
  195. *
  196. * @see uc_shipping_new_package()
  197. * @see uc_shipping_new_package_submit()
  198. */
  199. function uc_shipping_new_package_validate($form, &$form_state) {
  200. if ($form_state['values']['op'] != t('Cancel')) {
  201. $empty = TRUE;
  202. foreach ($form_state['values']['shipping_types'] as $shipping_type => $products) {
  203. foreach ($products as $product) {
  204. if ($product['checked'] != 0) {
  205. $empty = FALSE;
  206. break 2;
  207. }
  208. }
  209. }
  210. if ($empty) {
  211. form_set_error($shipping_type, t('Packages should have at least one product in them.'));
  212. }
  213. }
  214. }
  215. /**
  216. * Submit handler for uc_shipping_new_package().
  217. *
  218. * @see uc_shipping_new_package()
  219. * @see uc_shipping_new_package_validate()
  220. */
  221. function uc_shipping_new_package_submit($form, &$form_state) {
  222. if ($form_state['values']['op'] != t('Cancel')) {
  223. $packages = array(0 => array());
  224. foreach ($form_state['values']['shipping_types'] as $shipping_type => $products) {
  225. foreach ($products as $id => $product) {
  226. if ($product['checked']) {
  227. if ($form_state['values']['op'] == t('Create one package')) {
  228. $product['package'] = 1;
  229. }
  230. if ($product['package'] != 0) {
  231. $packages[$product['package']]['products'][$id] = (object)$product;
  232. if (!isset($packages[$product['package']]['shipping_type'])) {
  233. $packages[$product['package']]['shipping_type'] = $shipping_type;
  234. }
  235. }
  236. else {
  237. $packages[0][$shipping_type][$id] = (object)$product;
  238. }
  239. }
  240. }
  241. if (isset($packages[0][$shipping_type])) {
  242. foreach ($packages[0][$shipping_type] as $id => $product) {
  243. $qty = $product->qty;
  244. $product->qty = 1;
  245. for ($i = 0; $i < $qty; $i++) {
  246. $packages[] = array('products' => array($id => $product), 'shipping_type' => $shipping_type);
  247. }
  248. }
  249. }
  250. unset($packages[0][$shipping_type]);
  251. }
  252. if (empty($packages[0])) {
  253. unset($packages[0]);
  254. }
  255. foreach ($packages as $package) {
  256. $package['order_id'] = $form_state['values']['order_id'];
  257. uc_shipping_package_save($package);
  258. }
  259. }
  260. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/packages';
  261. }
  262. /**
  263. * Rearranges the products in or out of a package.
  264. *
  265. * @see uc_shipping_package_edit_submit()
  266. * @see theme_uc_shipping_edit_package_fieldset()
  267. * @ingroup forms
  268. */
  269. function uc_shipping_package_edit($form, &$form_state, $order, $package) {
  270. $products = array();
  271. $shipping_types_products = array();
  272. foreach ($order->products as $product) {
  273. if ($product->data['shippable']) {
  274. $product->shipping_type = uc_product_get_shipping_type($product);
  275. $shipping_types_products[$product->shipping_type][$product->order_product_id] = $product;
  276. $products[$product->order_product_id] = $product;
  277. }
  278. }
  279. $result = db_query("SELECT order_product_id, SUM(qty) AS quantity FROM {uc_packaged_products} pp LEFT JOIN {uc_packages} p ON pp.package_id = p.package_id WHERE p.order_id = :id GROUP BY order_product_id", array(':id' => $order->order_id));
  280. foreach ($result as $packaged_product) {
  281. // Make already packaged products unavailable, except those in this package.
  282. $products[$packaged_product->order_product_id]->qty -= $packaged_product->quantity;
  283. if (isset($package->products[$packaged_product->order_product_id])) {
  284. $products[$packaged_product->order_product_id]->qty += $package->products[$packaged_product->order_product_id]->qty;
  285. }
  286. }
  287. $form['#tree'] = TRUE;
  288. $form['package_id'] = array('#type' => 'hidden', '#value' => $package->package_id);
  289. $form['products'] = array();
  290. foreach ($products as $product) {
  291. if ($product->qty > 0) {
  292. $product_row = array();
  293. $product_row['checked'] = array('#type' => 'checkbox', '#default_value' => isset($package->products[$product->order_product_id]));
  294. $product_row['model'] = array('#markup' => check_plain($product->model));
  295. $product_row['name'] = array('#markup' => filter_xss_admin($product->title));
  296. $product_row['qty'] = array(
  297. '#type' => 'select',
  298. '#options' => drupal_map_assoc(range(1, $product->qty)),
  299. '#default_value' => isset($package->products[$product->order_product_id]) ? $package->products[$product->order_product_id]->qty : 1,
  300. );
  301. $form['products'][$product->order_product_id] = $product_row;
  302. }
  303. }
  304. $form['products']['#theme'] = 'uc_shipping_edit_package_fieldset';
  305. $options = array();
  306. $shipping_type_options = uc_quote_shipping_type_options();
  307. foreach (array_keys($shipping_types_products) as $type) {
  308. $options[$type] = isset($shipping_type_options[$type]) ? $shipping_type_options[$type] : ucwords(str_replace('_', ' ', $type));
  309. }
  310. $form['shipping_type'] = array(
  311. '#type' => 'select',
  312. '#title' => t('Shipping type'),
  313. '#options' => $options,
  314. '#default_value' => $package->shipping_type,
  315. );
  316. $form['actions'] = array('#type' => 'actions');
  317. $form['actions']['submit'] = array(
  318. '#type' => 'submit',
  319. '#value' => t('Save'),
  320. );
  321. return $form;
  322. }
  323. /**
  324. * Displays a formatted shipping type fieldset.
  325. *
  326. * @ingroup themeable
  327. */
  328. function theme_uc_shipping_edit_package_fieldset($variables) {
  329. $fieldset = $variables['fieldset'];
  330. drupal_add_js('misc/tableselect.js');
  331. $output = '';
  332. $header = array(array('class' => array('select-all')), t('SKU'), t('Title'), theme('uc_qty_label'));
  333. $rows = array();
  334. foreach (element_children($fieldset) as $op_id) {
  335. $row = array();
  336. $row[] = drupal_render($fieldset[$op_id]['checked']);
  337. $row[] = drupal_render($fieldset[$op_id]['model']);
  338. $row[] = drupal_render($fieldset[$op_id]['name']);
  339. $row[] = drupal_render($fieldset[$op_id]['qty']);
  340. $rows[] = $row;
  341. }
  342. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  343. $output .= drupal_render_children($fieldset);
  344. return $output;
  345. }
  346. /**
  347. * Submit handler for uc_shipping_package_edit().
  348. *
  349. * @see uc_shipping_package_edit()
  350. */
  351. function uc_shipping_package_edit_submit($form, &$form_state) {
  352. $package = uc_shipping_package_load($form_state['values']['package_id']);
  353. foreach ($form_state['values']['products'] as $id => $product) {
  354. if ($product['checked']) {
  355. $package->products[$id] = (object)$product;
  356. }
  357. else {
  358. unset($package->products[$id]);
  359. }
  360. }
  361. $package->shipping_type = $form_state['values']['shipping_type'];
  362. uc_shipping_package_save($package);
  363. $form_state['redirect'] = 'admin/store/orders/' . $package->order_id . '/packages';
  364. }
  365. /**
  366. * Confirms cancellation of a package's shipment.
  367. *
  368. * @see uc_shipping_package_cancel_confirm_submit()
  369. * @ingroup forms
  370. */
  371. function uc_shipping_package_cancel_confirm($form, &$form_state, $order, $package) {
  372. $form['order_id'] = array('#type' => 'value', '#value' => $order->order_id);
  373. $form['package_id'] = array('#type' => 'value', '#value' => $package->package_id);
  374. $output = confirm_form($form, t('Are you sure you want to cancel the shipment of this package?'), 'admin/store/orders/' . $order->order_id . '/packages',
  375. t('It will be available for reshipment.'), t('Cancel shipment'), t('Nevermind'));
  376. return $output;
  377. }
  378. /**
  379. * Form submission handler for uc_shipping_package_cancel_confirm().
  380. *
  381. * @see uc_shipping_package_cancel_confirm()
  382. */
  383. function uc_shipping_package_cancel_confirm_submit($form, &$form_state) {
  384. $package = uc_shipping_package_load($form_state['values']['package_id']);
  385. $shipment = uc_shipping_shipment_load($package->sid);
  386. $methods = module_invoke_all('uc_shipping_method');
  387. if (isset($methods[$shipment->shipping_method]['cancel']) &&
  388. function_exists($methods[$shipment->shipping_method]['cancel'])) {
  389. $result = call_user_func($methods[$shipment->shipping_method]['cancel'], $shipment->tracking_number, array($package->tracking_number));
  390. if ($result) {
  391. db_update('uc_packages')
  392. ->fields(array(
  393. 'sid' => NULL,
  394. 'label_image' => NULL,
  395. 'tracking_number' => NULL,
  396. ))
  397. ->condition('package_id', $package->package_id)
  398. ->execute();
  399. if (isset($package->label_image)) {
  400. file_usage_delete($package->label_image, 'uc_shipping', 'package', $package->package_id);
  401. file_delete($package->label_image);
  402. unset($package->label_image);
  403. }
  404. unset($shipment->packages[$package->package_id]);
  405. if (!count($shipment->packages)) {
  406. uc_shipping_shipment_delete($shipment->sid);
  407. }
  408. }
  409. }
  410. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/packages';
  411. }
  412. /**
  413. * Decides to unpackage products.
  414. *
  415. * @see uc_shipping_package_delete_confirm_submit()
  416. * @ingroup forms
  417. */
  418. function uc_shipping_package_delete_confirm($form, &$form_state, $order, $package) {
  419. $form['order_id'] = array('#type' => 'value', '#value' => $order->order_id);
  420. $form['package_id'] = array('#type' => 'value', '#value' => $package->package_id);
  421. $output = confirm_form($form, t('Are you sure you want to delete this package?'), 'admin/store/orders/' . $order->order_id . '/packages',
  422. t('The products it contains will be available for repackaging.'), t('Delete'), t('Cancel'));
  423. return $output;
  424. }
  425. /**
  426. * Submit handler for uc_shipping_package_delete_confirm().
  427. *
  428. * @see uc_shipping_package_delete_confirm()
  429. */
  430. function uc_shipping_package_delete_confirm_submit($form, &$form_state) {
  431. uc_shipping_package_delete($form_state['values']['package_id']);
  432. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/packages';
  433. }
  434. /**
  435. * Displays a list of shipments for an order.
  436. *
  437. * @param $order
  438. * The order object.
  439. */
  440. function uc_shipping_order_shipments($order) {
  441. $result = db_query("SELECT * FROM {uc_shipments} WHERE order_id = :id", array(':id' => $order->order_id));
  442. $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' => 5));
  443. $rows = array();
  444. foreach ($result as $shipment) {
  445. $row = array();
  446. $row[] = $shipment->sid;
  447. $row[] = check_plain($shipment->d_first_name) . ' ' . check_plain($shipment->d_last_name);
  448. $row[] = check_plain($shipment->d_company);
  449. $row[] = check_plain($shipment->d_city) . ', ' . uc_get_zone_code($shipment->d_zone) . ' ' . check_plain($shipment->d_postal_code);
  450. $row[] = format_date($shipment->ship_date, 'uc_store');
  451. $row[] = format_date($shipment->expected_delivery, 'uc_store');
  452. $row[] = is_null($shipment->tracking_number) ? t('n/a') : check_plain($shipment->tracking_number);
  453. $row[] = l(t('view'), 'admin/store/orders/' . $order->order_id . '/shipments/' . $shipment->sid . '/view');
  454. $row[] = l(t('edit'), 'admin/store/orders/' . $order->order_id . '/shipments/' . $shipment->sid . '/edit');
  455. $row[] = l(t('print'), 'admin/store/orders/' . $order->order_id . '/shipments/' . $shipment->sid . '/print');
  456. $row[] = l(t('packing slip'), 'admin/store/orders/' . $order->order_id . '/shipments/' . $shipment->sid . '/packing_slip');
  457. $row[] = l(t('delete'), 'admin/store/orders/' . $order->order_id . '/shipments/' . $shipment->sid . '/delete');
  458. $rows[] = $row;
  459. }
  460. if (empty($rows)) {
  461. if (!db_query("SELECT COUNT(*) FROM {uc_packages} WHERE order_id = :id", array(':id' => $order->order_id))->fetchField()) {
  462. drupal_set_message(t("This order's products have not been organized into packages."));
  463. drupal_goto('admin/store/orders/' . $order->order_id . '/packages/new');
  464. }
  465. else {
  466. drupal_set_message(t('No shipments have been made for this order.'));
  467. drupal_goto('admin/store/orders/' . $order->order_id . '/shipments/new');
  468. }
  469. }
  470. $build['shipments'] = array(
  471. '#theme' => 'table',
  472. '#header' => $header,
  473. '#rows' => $rows,
  474. );
  475. return $build;
  476. }
  477. /**
  478. * Sets up a new shipment with the chosen packages.
  479. *
  480. * @see uc_shipping_new_shipment_submit()
  481. * @see theme_uc_shipping_new_shipment()
  482. * @ingroup forms
  483. */
  484. function uc_shipping_new_shipment($form, &$form_state, $order) {
  485. $checked_pkgs = isset($_GET['pkgs']) ? $_GET['pkgs'] : array();
  486. $form['#tree'] = TRUE;
  487. $form['order_id'] = array('#type' => 'hidden', '#value' => $order->order_id);
  488. $packages_by_type = array();
  489. $units = variable_get('uc_weight_unit', 'lb');
  490. $result = db_query("SELECT * FROM {uc_packages} WHERE order_id = :id AND sid IS NULL", array(':id' => $order->order_id));
  491. foreach ($result as $package) {
  492. $products = array();
  493. $weight = 0;
  494. $result2 = db_query("SELECT pp.order_product_id, pp.qty, pp.qty * op.weight AS weight, op.weight_units, op.title, op.model FROM {uc_packaged_products} pp LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = :id", array(':id' => $package->package_id));
  495. foreach ($result2 as $product) {
  496. $units_conversion = uc_weight_conversion($product->weight_units, $units);
  497. $weight += $product->weight * $units_conversion;
  498. $products[$product->order_product_id] = $product;
  499. }
  500. $package->weight = $weight;
  501. $package->products = $products;
  502. $packages_by_type[$package->shipping_type][$package->package_id] = $package;
  503. }
  504. $option_methods = array();
  505. $shipping_types = uc_quote_get_shipping_types();
  506. $shipping_methods = module_invoke_all('uc_shipping_method');
  507. $shipping_methods_by_type = array();
  508. foreach ($shipping_methods as $method) {
  509. if (isset($method['ship'])) {
  510. $shipping_methods_by_type[$method['ship']['type']][] = $method;
  511. }
  512. }
  513. $pkgs_exist = FALSE;
  514. foreach ($packages_by_type as $shipping_type => $packages) {
  515. $form['shipping_types'][$shipping_type] = array(
  516. '#type' => 'fieldset',
  517. '#title' => $shipping_types[$shipping_type]['title'],
  518. );
  519. $form['shipping_types'][$shipping_type]['packages'] = array();
  520. foreach ($packages as $package) {
  521. $pkgs_exist = TRUE;
  522. $package_row = array();
  523. $package_row['checked'] = array('#type' => 'checkbox', '#default_value' => (in_array($package->package_id, $checked_pkgs) ? 1 : 0));
  524. $package_row['package_id'] = array('#markup' => $package->package_id);
  525. $product_list = array();
  526. foreach ($package->products as $product) {
  527. $product_list[] = $product->qty . ' x ' . check_plain($product->model);
  528. }
  529. $package_row['products'] = array('#markup' => '<ul><li>' . implode('</li><li>', $product_list) . '</li></ul>');
  530. $package_row['weight'] = array('#markup' => uc_weight_format($package->weight, $units));
  531. $form['shipping_types'][$shipping_type]['packages'][$package->package_id] = $package_row;
  532. }
  533. if (isset($shipping_methods_by_type[$shipping_type])) {
  534. foreach ($shipping_methods_by_type[$shipping_type] as $method) {
  535. $option_methods += array($method['id'] => $method['title']);
  536. }
  537. }
  538. }
  539. if ($pkgs_exist) {
  540. $option_methods = array('all' => t('Ship Manually')) + $option_methods;
  541. $form['method'] = array(
  542. '#type' => 'select',
  543. '#title' => t('Shipping method'),
  544. '#options' => $option_methods,
  545. '#default_value' => 'all',
  546. );
  547. $form['actions'] = array('#type' => 'actions');
  548. $form['actions']['ship'] = array(
  549. '#type' => 'submit',
  550. '#value' => t('Ship packages'),
  551. );
  552. }
  553. return $form;
  554. }
  555. /**
  556. * Formats and displays the new shipment form.
  557. *
  558. * @see uc_shipping_new_shipment()
  559. * @ingroup themeable
  560. */
  561. function theme_uc_shipping_new_shipment($variables) {
  562. $form = $variables['form'];
  563. drupal_add_js('misc/tableselect.js');
  564. $output = '';
  565. $header = array(array('class' => array('select-all')), t('Package'), t('Products'), t('Weight'));
  566. foreach (element_children($form['shipping_types']) as $shipping_type) {
  567. $rows = array();
  568. foreach (element_children($form['shipping_types'][$shipping_type]['packages']) as $package_id) {
  569. $row = array();
  570. $row[] = drupal_render($form['shipping_types'][$shipping_type]['packages'][$package_id]['checked']);
  571. $row[] = drupal_render($form['shipping_types'][$shipping_type]['packages'][$package_id]['package_id']);
  572. $row[] = drupal_render($form['shipping_types'][$shipping_type]['packages'][$package_id]['products']);
  573. $row[] = drupal_render($form['shipping_types'][$shipping_type]['packages'][$package_id]['weight']);
  574. $rows[] = $row;
  575. }
  576. if (count($rows)) {
  577. $form['shipping_types'][$shipping_type]['packages']['table'] = array(
  578. '#theme' => 'table',
  579. '#header' => $header,
  580. '#rows' => $rows,
  581. );
  582. }
  583. }
  584. $output .= drupal_render_children($form);
  585. return $output;
  586. }
  587. /**
  588. * Submit handler for uc_shipping_new_shipment().
  589. *
  590. * Sends package information to the chosen method.
  591. *
  592. * @see uc_shipping_new_shipment()
  593. */
  594. function uc_shipping_new_shipment_submit($form, &$form_state) {
  595. $packages = array();
  596. foreach ($form_state['values']['shipping_types'] as $shipping_type) {
  597. if (is_array($shipping_type['packages'])) {
  598. foreach ($shipping_type['packages'] as $id => $input) {
  599. if ($input['checked']) {
  600. $packages[] = $id;
  601. }
  602. }
  603. }
  604. }
  605. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/ship/' . $form_state['values']['method'] . '/' . implode('/', $packages);
  606. }
  607. /**
  608. * Displays shipment details.
  609. */
  610. function uc_shipping_shipment_view($order, $shipment) {
  611. $build = array();
  612. $origin = uc_order_address($shipment, 'o');
  613. $destination = uc_order_address($shipment, 'd');
  614. $build['pickup_address'] = array('#markup' => '<div class="order-pane pos-left"><div class="order-pane-title">' . t('Pickup Address:') . '</div>' . $origin . '</div>');
  615. $build['delivery_address'] = array('#markup' => '<div class="order-pane pos-left"><div class="order-pane-title">' . t('Delivery Address:') . '</div>' . $destination . '</div>');
  616. $rows = array();
  617. $rows[] = array(t('Ship date:'), format_date($shipment->ship_date, 'uc_store'));
  618. $rows[] = array(t('Expected delivery:'), format_date($shipment->expected_delivery, 'uc_store'));
  619. $build['schedule'] = array(
  620. '#theme' => 'table',
  621. '#rows' => $rows,
  622. '#attributes' => array('style' => 'width: auto'),
  623. '#prefix' => '<div class="order-pane abs-left"><div class="order-pane-title">' . t('Schedule:') . '</div>',
  624. '#suffix' => '</div>',
  625. );
  626. $rows = array();
  627. $rows[] = array(t('Carrier:'), check_plain($shipment->carrier));
  628. if ($shipment->transaction_id) {
  629. $rows[] = array(t('Transaction ID:'), check_plain($shipment->transaction_id));
  630. }
  631. if ($shipment->tracking_number) {
  632. $rows[] = array(t('Tracking number:'), check_plain($shipment->tracking_number));
  633. }
  634. $methods = module_invoke_all('uc_shipping_method');
  635. if (isset($methods[$shipment->shipping_method]['quote']['accessorials'][$shipment->accessorials])) {
  636. $rows[] = array(t('Services:'), $methods[$shipment->shipping_method]['quote']['accessorials'][$shipment->accessorials]);
  637. }
  638. else {
  639. $rows[] = array(t('Services:'), $shipment->accessorials);
  640. }
  641. $rows[] = array(t('Cost:'), array('data' => array('#theme' => 'uc_price', '#price' => $shipment->cost)));
  642. $build['details'] = array(
  643. '#theme' => 'table',
  644. '#rows' => $rows,
  645. '#attributes' => array('style' => 'width:auto'),
  646. '#prefix' => '<div class="order-pane abs-left"><div class="order-pane-title">' . t('Shipment Details:') . '</div>',
  647. '#suffix' => '</div>',
  648. );
  649. foreach ($shipment->packages as $package) {
  650. $build['packages'][] = uc_shipping_package_view($package);
  651. }
  652. return $build;
  653. }
  654. /**
  655. * Creates or edits a shipment.
  656. *
  657. * @see uc_shipping_shipment_edit_validate()
  658. * @see uc_shipping_shipment_edit_submit()
  659. * @ingroup forms
  660. */
  661. function uc_shipping_shipment_edit($form, &$form_state, $order, $shipment) {
  662. $form['order_id'] = array('#type' => 'value', '#value' => $order->order_id);
  663. if (isset($shipment->sid)) {
  664. $form['sid'] = array('#type' => 'value', '#value' => $shipment->sid);
  665. $methods = module_invoke_all('uc_shipping_method');
  666. if (isset($methods[$shipment->shipping_method])) {
  667. $method = $methods[$shipment->shipping_method];
  668. }
  669. }
  670. $addresses = array();
  671. $form['packages'] = array(
  672. '#type' => 'fieldset',
  673. '#title' => t('Packages'),
  674. '#collapsible' => TRUE,
  675. '#tree' => TRUE,
  676. );
  677. if (isset($shipment->o_street1)) {
  678. $o_address = new stdClass();
  679. foreach ($shipment as $field => $value) {
  680. if (substr($field, 0, 2) == 'o_') {
  681. $o_address->{substr($field, 2)} = $value;
  682. }
  683. }
  684. $addresses[] = $o_address;
  685. }
  686. foreach ($shipment->packages as $id => $package) {
  687. foreach ($package->addresses as $address) {
  688. if (!in_array($address, $addresses)) {
  689. $addresses[] = $address;
  690. }
  691. }
  692. // Create list of products and get a representative product (last one in
  693. // the loop) to use for some default values
  694. $product_list = array();
  695. $declared_value = 0;
  696. foreach ($package->products as $product) {
  697. $product_list[] = $product->qty . ' x ' . check_plain($product->model);
  698. $declared_value += $product->qty * $product->price;
  699. }
  700. $pkg_form = array(
  701. '#type' => 'fieldset',
  702. '#title' => t('Package @id', array('@id' => $id)),
  703. );
  704. $pkg_form['products'] = array(
  705. '#theme' => 'item_list',
  706. '#items' => $product_list,
  707. );
  708. $pkg_form['package_id'] = array(
  709. '#type' => 'hidden',
  710. '#value' => $id,
  711. );
  712. $pkg_form['pkg_type'] = array(
  713. '#type' => 'textfield',
  714. '#title' => t('Package type'),
  715. '#default_value' => $package->pkg_type,
  716. '#description' => t('For example: Box, pallet, tube, envelope, etc.'),
  717. );
  718. if (isset($method) && is_array($method['ship']['pkg_types'])) {
  719. $pkg_form['pkg_type']['#type'] = 'select';
  720. $pkg_form['pkg_type']['#options'] = $method['ship']['pkg_types'];
  721. $pkg_form['pkg_type']['#description'] = '';
  722. }
  723. $pkg_form['declared_value'] = array(
  724. '#type' => 'uc_price',
  725. '#title' => t('Declared value'),
  726. '#default_value' => isset($package->value) ? $package->value : $declared_value,
  727. );
  728. $pkg_form['weight'] = array(
  729. '#type' => 'container',
  730. '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
  731. '#description' => t('Weight of the package. Default value is sum of product weights in the package.'),
  732. '#weight' => 15,
  733. );
  734. $pkg_form['weight']['weight'] = array(
  735. '#type' => 'textfield',
  736. '#title' => t('Weight'),
  737. '#default_value' => isset($package->weight) ? $package->weight : 0,
  738. '#size' => 10,
  739. );
  740. $pkg_form['weight']['units'] = array(
  741. '#type' => 'select',
  742. '#title' => t('Units'),
  743. '#options' => array(
  744. 'lb' => t('Pounds'),
  745. 'kg' => t('Kilograms'),
  746. 'oz' => t('Ounces'),
  747. 'g' => t('Grams'),
  748. ),
  749. '#default_value' => isset($package->weight_units) ?
  750. $package->weight_units :
  751. variable_get('uc_weight_unit', 'lb'),
  752. );
  753. $pkg_form['dimensions'] = array(
  754. '#type' => 'container',
  755. '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
  756. '#title' => t('Dimensions'),
  757. '#description' => t('Physical dimensions of the packaged product.'),
  758. '#weight' => 20,
  759. );
  760. $pkg_form['dimensions']['length'] = array(
  761. '#type' => 'textfield',
  762. '#title' => t('Length'),
  763. '#default_value' => isset($package->length) ? $package->length : 1,
  764. '#size' => 8,
  765. );
  766. $pkg_form['dimensions']['width'] = array(
  767. '#type' => 'textfield',
  768. '#title' => t('Width'),
  769. '#default_value' => isset($package->width) ? $package->width : 1,
  770. '#size' => 8,
  771. );
  772. $pkg_form['dimensions']['height'] = array(
  773. '#type' => 'textfield',
  774. '#title' => t('Height'),
  775. '#default_value' => isset($package->height) ? $package->height : 1,
  776. '#size' => 8,
  777. );
  778. $pkg_form['dimensions']['units'] = array(
  779. '#type' => 'select',
  780. '#title' => t('Units of measurement'),
  781. '#options' => array(
  782. 'in' => t('Inches'),
  783. 'ft' => t('Feet'),
  784. 'cm' => t('Centimeters'),
  785. 'mm' => t('Millimeters'),
  786. ),
  787. '#default_value' => isset($package->length_units) ?
  788. $package->length_units :
  789. variable_get('uc_length_unit', 'in'),
  790. );
  791. $pkg_form['tracking_number'] = array(
  792. '#type' => 'textfield',
  793. '#title' => t('Tracking number'),
  794. '#default_value' => isset($package->tracking_number) ? $package->tracking_number : '',
  795. );
  796. $form['packages'][$id] = $pkg_form;
  797. }
  798. if (!empty($shipment->d_street1)) {
  799. foreach ($shipment as $field => $value) {
  800. if (substr($field, 0, 2) == 'd_') {
  801. $order->{'delivery_' . substr($field, 2)} = $value;
  802. }
  803. }
  804. }
  805. $form = uc_shipping_address_form($form, $form_state, $addresses, $order);
  806. $form['shipment'] = array(
  807. '#type' => 'fieldset',
  808. '#title' => t('Shipment data'),
  809. '#collapsible' => TRUE,
  810. );
  811. // Determine shipping option chosen by the customer.
  812. $message = '';
  813. if (isset($order->quote['method'])) {
  814. // Order has a quote attached.
  815. $method = $order->quote['method'];
  816. $methods = module_invoke_all('uc_shipping_method');
  817. if (isset($methods[$method])) {
  818. // Quote is from a currently-active shipping method.
  819. $services = $methods[$method]['quote']['accessorials'];
  820. $method = $services[$order->quote['accessorials']];
  821. }
  822. $message = t('Customer selected "@method" as the shipping method and paid @rate', array('@method' => $method, '@rate' => uc_currency_format($order->quote['rate'])));
  823. }
  824. else {
  825. // No quotes for this order.
  826. $message = t('There are no shipping quotes attached to this order. Customer was not charged for shipping.');
  827. }
  828. // Inform administrator of customer's shipping choice.
  829. $form['shipment']['shipping_choice'] = array(
  830. '#type' => 'markup',
  831. '#prefix' => '<div>',
  832. '#markup' => $message,
  833. '#suffix' => '</div>',
  834. );
  835. $form['shipment']['shipping_method'] = array(
  836. '#type' => 'hidden',
  837. '#value' => isset($shipment->shipping_method) ? $shipment->shipping_method : 'manual',
  838. );
  839. $form['shipment']['carrier'] = array(
  840. '#type' => 'textfield',
  841. '#title' => t('Carrier'),
  842. '#default_value' => isset($shipment->carrier) ? $shipment->carrier : '',
  843. );
  844. $form['shipment']['accessorials'] = array(
  845. '#type' => 'textfield',
  846. '#title' => t('Shipment options'),
  847. '#default_value' => isset($shipment->accessorials) ? $shipment->accessorials : '',
  848. '#description' => t('Short notes about the shipment, e.g. residential, overnight, etc.'),
  849. );
  850. $form['shipment']['transaction_id'] = array(
  851. '#type' => 'textfield',
  852. '#title' => t('Transaction ID'),
  853. '#default_value' => isset($shipment->transaction_id) ? $shipment->transaction_id : '',
  854. );
  855. $form['shipment']['tracking_number'] = array(
  856. '#type' => 'textfield',
  857. '#title' => t('Tracking number'),
  858. '#default_value' => isset($shipment->tracking_number) ? $shipment->tracking_number : '',
  859. );
  860. if (isset($shipment->ship_date)) {
  861. $ship_date = getdate($shipment->ship_date);
  862. }
  863. else {
  864. $ship_date = getdate();
  865. }
  866. if (isset($shipment->expected_delivery)) {
  867. $exp_delivery = getdate($shipment->expected_delivery);
  868. }
  869. else {
  870. $exp_delivery = getdate();
  871. }
  872. $form['shipment']['ship_date'] = array(
  873. '#type' => 'date',
  874. '#title' => t('Ship date'),
  875. '#default_value' => array(
  876. 'year' => $ship_date['year'],
  877. 'month' => $ship_date['mon'],
  878. 'day' => $ship_date['mday']
  879. ),
  880. );
  881. $form['shipment']['expected_delivery'] = array(
  882. '#type' => 'date',
  883. '#title' => t('Expected delivery'),
  884. '#default_value' => array(
  885. 'year' => $exp_delivery['year'],
  886. 'month' => $exp_delivery['mon'],
  887. 'day' => $exp_delivery['mday']
  888. ),
  889. );
  890. $form['shipment']['cost'] = array(
  891. '#type' => 'uc_price',
  892. '#title' => t('Shipping cost'),
  893. '#default_value' => isset($shipment->cost) ? $shipment->cost : 0,
  894. );
  895. $form['actions'] = array(
  896. '#type' => 'actions'
  897. );
  898. $form['actions']['submit'] = array(
  899. '#type' => 'submit',
  900. '#value' => t('Save shipment'),
  901. '#weight' => 10
  902. );
  903. return $form;
  904. }
  905. /**
  906. * Ensures the package dimensions are positive numbers.
  907. *
  908. * @see uc_shipping_shipment_edit()
  909. * @see uc_shipping_shipment_submit()
  910. */
  911. function uc_shipping_shipment_edit_validate($form, &$form_state) {
  912. foreach ($form_state['values']['packages'] as $key => $package) {
  913. foreach (array('length', 'width', 'height') as $property) {
  914. if (!empty($package['dimensions'][$property]) && (!is_numeric($package['dimensions'][$property]) || $package['dimensions'][$property] < 0)) {
  915. form_set_error('packages][' . $key . '][dimensions][' . $property, t('@property must be a positive number. No commas and only one decimal point.', array('@property' => drupal_ucfirst($property))));
  916. }
  917. }
  918. }
  919. }
  920. /**
  921. * Submit handler for uc_shipping_shipment_edit().
  922. *
  923. * @see uc_shipping_shipment_edit()
  924. * @see uc_shipping_shipment_validate()
  925. */
  926. function uc_shipping_shipment_edit_submit($form, &$form_state) {
  927. $shipment = new stdClass();
  928. $shipment->order_id = $form_state['values']['order_id'];
  929. if (isset($form_state['values']['sid'])) {
  930. $shipment->sid = $form_state['values']['sid'];
  931. }
  932. $shipment->origin = (object) $form_state['values']['pickup_address'];
  933. $shipment->destination = new stdClass();
  934. foreach ($form_state['values'] as $key => $value) {
  935. if (substr($key, 0, 9) == 'delivery_') {
  936. $field = substr($key, 9);
  937. $shipment->destination->$field = $value;
  938. }
  939. }
  940. $shipment->packages = array();
  941. foreach ($form_state['values']['packages'] as $id => $pkg_form) {
  942. $package = uc_shipping_package_load($id);
  943. $package->pkg_type = $pkg_form['pkg_type'];
  944. $package->value = $pkg_form['declared_value'];
  945. $package->length = $pkg_form['dimensions']['length'];
  946. $package->width = $pkg_form['dimensions']['width'];
  947. $package->height = $pkg_form['dimensions']['height'];
  948. $package->length_units = $pkg_form['dimensions']['units'];
  949. $package->tracking_number = $pkg_form['tracking_number'];
  950. $package->qty = 1;
  951. $shipment->packages[$id] = $package;
  952. }
  953. $shipment->shipping_method = $form_state['values']['shipping_method'];
  954. $shipment->accessorials = $form_state['values']['accessorials'];
  955. $shipment->carrier = $form_state['values']['carrier'];
  956. $shipment->transaction_id = $form_state['values']['transaction_id'];
  957. $shipment->tracking_number = $form_state['values']['tracking_number'];
  958. $shipment->ship_date = gmmktime(12, 0, 0, $form_state['values']['ship_date']['month'], $form_state['values']['ship_date']['day'], $form_state['values']['ship_date']['year']);
  959. $shipment->expected_delivery = gmmktime(12, 0, 0, $form_state['values']['expected_delivery']['month'], $form_state['values']['expected_delivery']['day'], $form_state['values']['expected_delivery']['year']);
  960. $shipment->cost = $form_state['values']['cost'];
  961. uc_shipping_shipment_save($shipment);
  962. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/shipments';
  963. }
  964. /**
  965. * Shows a printer-friendly version of a shipment.
  966. */
  967. function uc_shipping_shipment_print($order, $shipment, $labels = TRUE) {
  968. $build = array(
  969. '#theme' => 'uc_shipping_shipment_print',
  970. '#order' => $order,
  971. '#shipment' => $shipment,
  972. '#labels' => $labels,
  973. );
  974. drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
  975. print theme('uc_packing_slip_page', array('content' => drupal_render($build)));
  976. exit();
  977. }
  978. /**
  979. * Displays the packing slip and shipping labels for printing.
  980. *
  981. * @ingroup themeable
  982. */
  983. function theme_uc_shipping_shipment_print($variables) {
  984. $order = $variables['order'];
  985. $shipment = $variables['shipment'];
  986. $labels = $variables['labels'];
  987. $output = theme('uc_packing_slip', array('order' => $order, 'shipment' => $shipment));
  988. if ($labels) {
  989. foreach ($shipment->packages as $id => $package) {
  990. if (isset($package->label_image) &&
  991. file_exists($package->label_image->uri)) {
  992. // TODO: Find a way to store these magic numbers specifically for UPS.
  993. list($width, $height) = array(672, 392);
  994. $output .= '<br class="page-break" />' . "\n";
  995. $output .= theme('image', array(
  996. 'path' => $package->label_image->uri,
  997. 'attributes' => array('width' => $width, 'height' => $height),
  998. 'getsize' => FALSE,
  999. )) . "\n";
  1000. }
  1001. }
  1002. }
  1003. return $output;
  1004. }
  1005. /**
  1006. * Decides to release packages to be put on another shipment.
  1007. *
  1008. * @see uc_shipping_shipment_delete_confirm_submit()
  1009. * @ingroup forms
  1010. */
  1011. function uc_shipping_shipment_delete_confirm($form, &$form_state, $order, $shipment) {
  1012. $form['order_id'] = array('#type' => 'value', '#value' => $order->order_id);
  1013. $form['sid'] = array('#type' => 'value', '#value' => $shipment->sid);
  1014. $output = confirm_form($form, t('Are you sure you want to delete this shipment?'), 'admin/store/orders/' . $order->order_id . '/shipments',
  1015. t('The shipment will be canceled and the packages it contains will be available for reshipment.'), t('Delete'), t('Cancel'));
  1016. return $output;
  1017. }
  1018. /**
  1019. * Submit handler for uc_shipping_shipment_delete_confirm().
  1020. *
  1021. * @see uc_shipping_shipment_delete_confirm()
  1022. */
  1023. function uc_shipping_shipment_delete_confirm_submit($form, &$form_state) {
  1024. $shipment = uc_shipping_shipment_load($form_state['values']['sid']);
  1025. $methods = module_invoke_all('uc_shipping_method');
  1026. if ($shipment->tracking_number &&
  1027. isset($methods[$shipment->shipping_method]['cancel']) &&
  1028. function_exists($methods[$shipment->shipping_method]['cancel'])) {
  1029. $result = call_user_func($methods[$shipment->shipping_method]['cancel'], $shipment->tracking_number);
  1030. if ($result) {
  1031. uc_shipping_shipment_delete($form_state['values']['sid']);
  1032. }
  1033. else {
  1034. 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)));
  1035. }
  1036. }
  1037. else {
  1038. uc_shipping_shipment_delete($form_state['values']['sid']);
  1039. }
  1040. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/shipments';
  1041. }
  1042. /**
  1043. * Default method to send packages on a shipment.
  1044. */
  1045. function uc_shipping_make_shipment($order) {
  1046. $args = func_get_args();
  1047. if (count($args) > 2) {
  1048. $breadcrumb = drupal_get_breadcrumb();
  1049. $breadcrumb[] = l(t('Shipments'), 'admin/store/orders/' . $order->order_id . '/shipments');
  1050. drupal_set_breadcrumb($breadcrumb);
  1051. $order = array_shift($args);
  1052. $method_id = array_shift($args);
  1053. $package_ids = $args;
  1054. $methods = module_invoke_all('uc_shipping_method');
  1055. if (isset($methods[$method_id])) {
  1056. $method = $methods[$method_id];
  1057. if (isset($method['ship']['file'])) {
  1058. $inc_file = drupal_get_path('module', $method['module']) . '/' . $method['ship']['file'];
  1059. if (is_file($inc_file)) {
  1060. require_once($inc_file);
  1061. }
  1062. }
  1063. return drupal_get_form($method['ship']['callback'], $order, $package_ids);
  1064. }
  1065. else {
  1066. $shipment = new stdClass();
  1067. $shipment->order_id = $order->order_id;
  1068. $shipment->packages = array();
  1069. foreach ($package_ids as $id) {
  1070. $package = uc_shipping_package_load($id);
  1071. $shipment->packages[$id] = $package;
  1072. }
  1073. return drupal_get_form('uc_shipping_shipment_edit', $order, $shipment);
  1074. }
  1075. }
  1076. else {
  1077. drupal_set_message(t('There is no sense in making a shipment with no packages on it, right?'));
  1078. drupal_goto('admin/store/orders/' . $args[0]->order_id . '/shipments/new');
  1079. }
  1080. }