You are here

function uc_shipping_package_save in Ubercart 5

Same name and namespace in other branches
  1. 6.2 shipping/uc_shipping/uc_shipping.module \uc_shipping_package_save()
  2. 7.3 shipping/uc_shipping/uc_shipping.module \uc_shipping_package_save()

Save a package.

3 calls to uc_shipping_package_save()
uc_shipping_new_package_submit in shipping/uc_shipping/uc_shipping.module
Submit handler for uc_shipping_new_package().
uc_shipping_package_edit_submit in shipping/uc_shipping/uc_shipping.module
Submit handler for uc_shipping_package_edit().
uc_shipping_shipment_save in shipping/uc_shipping/uc_shipping.module
Save a shipment.

File

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

Code

function uc_shipping_package_save($package) {
  $package = (object) $package;
  if (!isset($package->package_id)) {
    $package->package_id = db_next_id('{uc_packages}_package_id');
    db_query("INSERT INTO {uc_packages} (package_id, order_id) VALUES (%d, %d)", $package->package_id, $package->order_id);
  }
  if (count($package->products)) {
    $types = array();
    $values = array();
    foreach ($package->products as $id => $product) {
      $types[] = '(%d, %d, %d)';
      $values[] = $package->package_id;
      $values[] = $id;
      $values[] = $product->qty;
      $result = db_query("SELECT data FROM {uc_order_products} WHERE order_product_id = %d", $id);
      if ($order_product = db_fetch_object($result)) {
        $order_product->data = unserialize($order_product->data);
        $order_product->data['package_id'] = intval($package->package_id);
        db_query("UPDATE {uc_order_products} SET data = '%s' WHERE order_product_id = %d", serialize($order_product->data), $id);
      }
    }
    db_query("DELETE FROM {uc_packaged_products} WHERE package_id = %d", $package->package_id);
    db_query("INSERT INTO {uc_packaged_products} (package_id, order_product_id, qty) VALUES " . implode(',', $types), $values);
  }
  $types = array(
    "shipping_type = '%s'",
  );
  $values = array(
    $package->shipping_type,
  );
  if (isset($package->pkg_type)) {
    $types[] = "pkg_type = '%s'";
    $values[] = $package->pkg_type;
  }
  if (isset($package->length) && isset($package->width) && isset($package->height) && isset($package->length_units)) {
    array_push($types, 'length = %f', 'width = %f', 'height = %f', "length_units = '%s'");
    array_push($values, $package->length, $package->width, $package->height, $package->length_units);
  }
  if (isset($package->value)) {
    $types[] = 'value = %f';
    $values[] = $package->value;
  }
  if (isset($package->sid)) {
    $types[] = 'sid = %d';
    $values[] = $package->sid;
  }
  if (isset($package->tracking_number)) {
    $types[] = "tracking_number = '%s'";
    $values[] = $package->tracking_number;
  }
  if (isset($package->label_image)) {
    $types[] = "label_image = '%s'";
    $values[] = $package->label_image;
  }
  $values[] = $package->package_id;
  if (count($types)) {

    // Let it be known that I think it's ridiculous that Drupal doesn't put NULL into its database. --JLM
    db_query("UPDATE {uc_packages} SET " . implode(',', $types) . " WHERE package_id = %d", $values);
  }
}