*** pristine_uc_bulk_stock_updater/uc_bulk_stock_updater.inc	2010-08-29 20:12:29.000000000 +0200
--- uc_bulk_stock_updater/uc_bulk_stock_updater.inc	2010-11-30 13:16:02.006768001 +0100
***************
*** 14,19 ****
--- 14,35 ----
   * @return HTML.
   */
  function uc_bulk_stock_updater_stock_update() {
+ 
+   // Check if we need to do UberPos Multiple Location stuff
+   $has_up_multi = FALSE;
+   if(module_exists('up_multi')) {
+     $has_up_multi = TRUE;
+   };
+ 
+   // Fetch UberPos Multiple Location location data
+   $up_multi_locations = array();
+   if($has_up_multi) {
+     $up_multi_locations_result = db_query("SELECT * FROM up_stores");
+     while($store = db_fetch_object($up_multi_locations_result)){
+       $up_multi_locations[$store->store_id] = $store;
+     };
+   };
+ 
    drupal_add_css(drupal_get_path('module', 'uc_stock') . '/uc_stock.css');
    drupal_add_css(drupal_get_path('module', 'uc_bulk_stock_updater') . '/uc_bulk_stock_updater.css');
    
***************
*** 72,77 ****
--- 88,130 ----
        ),
        'class' => (intval($stock->threshold) >= intval($stock->stock)) ? 'uc-stock-below-threshold' : 'uc-stock-above-threshold',
      );
+ 
+     // Add UP_Multi data, if necessary
+     if($has_up_multi) {
+       $up_multi_stock_result = db_query("SELECT * FROM up_store_stock WHERE model='\%s'", $stock->sku);
+       $up_multi_stock_stock_by_location = array();
+       while($stock_by_location = db_fetch_object($up_multi_stock_result)) {
+         $up_multi_stock_stock_by_location[$stock_by_location->store_id] = $stock_by_location->stock_level;
+       };
+       foreach ($up_multi_locations as $location_id => $location) {
+         $stock_for_location = $up_multi_stock_stock_by_location[$location_id];
+         if(! $stock_for_location ) {
+           $stock_for_location = 0;
+         };
+         $stock_for_location_textfield = theme_textfield(array(
+           '#name' => 'location['.$location_id.']['.$stock->sku.']',
+           '#value' => $stock_for_location,
+           '#size' => 3,
+           '#maxlength' => 9,
+           '#attributes' => array('class' => 'uc_bulk_stock_updater_location_value uc_bulk_stock_updater_value'),
+           '#parents' => array(),
+         ));
+         $rows[] = array(
+           'data' => array(
+             array('data' => "<span>&nbsp;</span>"),
+             array(
+               'data' => $location->store_name,
+               'class' => 'up_multi_locationname',
+             ),
+             array('data' => $stock_for_location_textfield),
+             array('data' => '&nbsp;'),
+           ),
+           'class' => 'uc_bulk_stock_updater_location_row',
+         );
+       };
+ 
+     };
+ 
    }
    
    return theme('uc_bulk_stock_updater_table', $page_size, $viewing_all_records, $header, $rows);
***************
*** 122,131 ****
  	if (!isset($_POST['sku']) || !isset($_POST['stock']) || 0 > intval($_POST['stock'])) {
  		$err = t('Invalid parameters supplied');
  	} 
! 	else {
!   		if (!db_query("UPDATE {uc_product_stock} SET stock = %d WHERE sku = '%s'", $_POST['stock'], $_POST['sku']))
! 			$err = t('Error saving to db');
! 	}
  	
  	$ret = array();
  	
--- 175,228 ----
  	if (!isset($_POST['sku']) || !isset($_POST['stock']) || 0 > intval($_POST['stock'])) {
  		$err = t('Invalid parameters supplied');
  	} 
!   else {
!     $sku = $_POST['sku'];
!     $stock = $_POST['stock'];
! 
!     // presumably we're trying to update stock for an UberPos location
!     if(strstr($sku, 'location[')) {
! 
!       // Check if this is at all possible
!       $has_up_multi = FALSE;
!       if(module_exists('up_multi')) {
!         $has_up_multi = TRUE;
!       };
!       preg_match('/^location\[(\d+)\]\[(.*?)\]/', $sku, $matches);
!       $location_id = $matches[1];
!       $real_sku = $matches[2];
! 
!       if($has_up_multi  && $location_id && $real_sku ) { 
!         $multi_stock_exists_query = "SELECT * FROM {up_store_stock} WHERE store_id = %d AND model = '%s' ";
!         $multi_stock_record_exists = FALSE;
!         if(db_fetch_object(db_query($multi_stock_exists_query, $location_id, $real_sku))) {
!           $multi_stock_record_exists = TRUE;
!         };
!         if($multi_stock_record_exists) {
!           $update_multi_stock_query = "UPDATE {up_store_stock} SET stock_level = %d WHERE store_id = %d AND model = '%s'";
!           if(! db_query($update_multi_stock_query, $stock, $location_id, $real_sku)) {
!             $err = t('Error saving up_multi stock to db');
!           };
!         }
!         else {
!           $insert_multi_stock_query = "INSERT INTO {up_store_stock} (stock_level, store_id, model) VALUES (%d, %d, '%s')";
!           if(! db_query($insert_multi_stock_query, $stock, $location_id, $real_sku)) {
!             $err = t('Error inserting up_multi stock into db');
!           };
!         }
! 
! 
!       }
!       else {
!         $err = t('Invalid sku supplied for up_multi');
!       }
! 
!     }
!     // no up multi. update the default stock table
!     else {
!       if (!db_query("UPDATE {uc_product_stock} SET stock = %d WHERE sku = '%s'", $_POST['stock'], $_POST['sku']))
!         $err = t('Error saving to db');
!     }
!   };
  	
  	$ret = array();
  	
