uc_shipping.admin.inc 44 KB

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