function build_discounts()
 {
     $db = DB::get();
     $discount_table = DatabaseObject::tablename(Discount::$table);
     $product_table = DatabaseObject::tablename(Product::$table);
     $price_table = DatabaseObject::tablename(Price::$table);
     $catalog_table = DatabaseObject::tablename(Catalog::$table);
     $category_table = DatabaseObject::tablename(Category::$table);
     $where = "";
     // Go through each rule to construct an SQL query
     // that gets all applicable product & price ids
     if (!empty($this->rules) && is_array($this->rules)) {
         foreach ($this->rules as $rule) {
             if ($this->values[$rule['property']] == "price") {
                 $value = floatnum($rule['value']);
             } else {
                 $value = "'" . $rule['value'] . "'";
             }
             switch ($rule['logic']) {
                 case "Is equal to":
                     $match = "={$value}";
                     break;
                 case "Is not equal to":
                     $match = "!={$value}";
                     break;
                 case "Contains":
                     $match = " LIKE '%{$value}%'";
                     break;
                 case "Does not contain":
                     $match = " NOT LIKE '%{$value}%'";
                     break;
                 case "Begins with":
                     $match = " LIKE '{$value}%'";
                     break;
                 case "Ends with":
                     $match = " LIKE '%{$value}'";
                     break;
                 case "Is greater than":
                     $match = "> {$value}";
                     break;
                 case "Is greater than or equal to":
                     $match = ">= {$value}";
                     break;
                 case "Is less than":
                     $match = "< {$value}";
                     break;
                 case "Is less than or equal to":
                     $match = "<= {$value}";
                     break;
             }
             $where .= "AND ";
             switch ($rule['property']) {
                 case "Name":
                     $where .= "p.name{$match}";
                     break;
                 case "Category":
                     $where .= "cat.name{$match}";
                     break;
                 case "Variation":
                     $where .= "prc.label{$match}";
                     break;
                 case "Price":
                     $where .= "prc.price{$match}";
                     break;
                 case "Sale price":
                     $where .= "(prc.onsale='on' AND prc.saleprice{$match})";
                     break;
                 case "Type":
                     $where .= "prc.type{$match}";
                     break;
                 case "In stock":
                     $where .= "(prc.inventory='on' AND prc.stock{$match})";
                     break;
             }
         }
     }
     $type = $this->type == "Item" ? 'catalog' : 'cart';
     // Delete previous discount records
     $db->query("DELETE FROM {$discount_table} WHERE promo={$this->id}");
     $query = "INSERT INTO {$discount_table} (promo,product,price) \n\t\t\t\t\tSELECT '{$this->id}' as promo,p.id AS product,prc.id AS price\n\t\t\t\t\tFROM {$product_table} as p \n\t\t\t\t\tLEFT JOIN {$price_table} AS prc ON prc.product=p.id \n\t\t\t\t\tLEFT JOIN {$catalog_table} AS clog ON clog.product=p.id \n\t\t\t\t\tLEFT JOIN {$category_table} AS cat ON clog.category=cat.id \n\t\t\t\t\tWHERE TRUE {$where} \n\t\t\t\t\tGROUP BY prc.id";
     $db->query($query);
 }
 function add($qty)
 {
     if ($this->type == "Donation" && $this->donation['var'] == "on") {
         $qty = floatnum($qty);
         $this->quantity = $this->unitprice;
     }
     $this->quantity($this->quantity + $qty);
 }
 function settings_shipping()
 {
     global $Shopp;
     if (!current_user_can('manage_options')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     if (!empty($_POST['save'])) {
         check_admin_referer('shopp-settings-shipping');
         // Sterilize $values
         foreach ($_POST['settings']['shipping_rates'] as $i => &$method) {
             $method['name'] = stripslashes($method['name']);
             foreach ($method as $key => &$mr) {
                 if (!is_array($mr)) {
                     continue;
                 }
                 foreach ($mr as $id => &$v) {
                     if ($v == ">" || $v == "+" || $key == "services") {
                         continue;
                     }
                     $v = floatnum($v);
                 }
             }
         }
         $_POST['settings']['order_shipfee'] = floatnum($_POST['settings']['order_shipfee']);
         $this->settings_save();
         $updated = __('Shipping settings saved.', 'Shopp');
         $Shopp->ShipCalcs = new ShipCalcs($Shopp->path);
         $rates = $Shopp->Settings->get('shipping_rates');
         $Errors =& ShoppErrors();
         foreach ($rates as $rate) {
             $process = '';
             $ShipCalcClass = $rate['method'];
             if (strpos($rate['method'], '::') != false) {
                 list($ShipCalcClass, $process) = explode("::", $rate['method']);
             }
             if (isset($Shopp->ShipCalcs->modules[$ShipCalcClass]->requiresauth) && $Shopp->ShipCalcs->modules[$ShipCalcClass]->requiresauth) {
                 $Shopp->ShipCalcs->modules[$ShipCalcClass]->verifyauth();
                 if ($Errors->exist()) {
                     $autherrors = $Errors->get();
                 }
             }
         }
         if (!empty($autherrors)) {
             $updated = __('Shipping settings saved but there were errors: ', 'Shopp');
             foreach ((array) $autherrors as $error) {
                 $updated .= '<p>' . $error->message() . '</p>';
             }
             $Errors->reset();
         }
     }
     $methods = $Shopp->ShipCalcs->methods;
     $base = $Shopp->Settings->get('base_operations');
     $regions = $Shopp->Settings->get('regions');
     $region = $regions[$base['region']];
     $useRegions = $Shopp->Settings->get('shipping_regions');
     $areas = $Shopp->Settings->get('areas');
     if (is_array($areas[$base['country']]) && $useRegions == "on") {
         $areas = array_keys($areas[$base['country']]);
     } else {
         $areas = array($base['country'] => $base['name']);
     }
     unset($countries, $regions);
     $rates = $Shopp->Settings->get('shipping_rates');
     if (!empty($rates)) {
         ksort($rates);
     }
     $lowstock = $Shopp->Settings->get('lowstock_level');
     if (empty($lowstock)) {
         $lowstock = 0;
     }
     include SHOPP_ADMINPATH . "/settings/shipping.php";
 }
 /**
  * Prepare the data properties for entry into
  * the database */
 function prepare($object)
 {
     $data = array();
     // Go through each data property of the object
     foreach (get_object_vars($object) as $property => $value) {
         if (!isset($object->_datatypes[$property])) {
             continue;
         }
         // If the property is has a _datatype
         // it belongs in the database and needs
         // to be prepared
         // Process the data
         switch ($object->_datatypes[$property]) {
             case "string":
                 // Escape characters in strings as needed
                 if (is_array($value)) {
                     $value = serialize($value);
                 }
                 $data[$property] = "'" . $this->escape($value) . "'";
                 break;
             case "list":
                 // If value is empty, skip setting the field
                 // so it inherits the default value in the db
                 if (!empty($value)) {
                     $data[$property] = "'{$value}'";
                 }
                 break;
             case "date":
                 // If it's an empty date, set it to now()'s timestamp
                 if (is_null($value)) {
                     $data[$property] = "now()";
                     // If the date is an integer, convert it to an
                     // sql YYYY-MM-DD HH:MM:SS format
                 } elseif (!empty($value) && is_int(intval($value))) {
                     $data[$property] = "'" . mkdatetime(intval($value)) . "'";
                     // Otherwise it's already ready, so pass it through
                 } else {
                     $data[$property] = "'{$value}'";
                 }
                 break;
             case "int":
             case "float":
                 $value = floatnum($value);
                 $data[$property] = "'{$value}'";
                 if (empty($value)) {
                     $data[$property] = "'0'";
                 }
                 // Special exception for id fields
                 if ($property == "id" && empty($value)) {
                     $data[$property] = "NULL";
                 }
                 break;
             default:
                 // Anything not needing processing
                 // passes through into the object
                 $data[$property] = "'{$value}'";
         }
     }
     return $data;
 }