diff --git a/uc_stock/tests/uc_stock.test b/uc_stock/tests/uc_stock.test index f563a1a..0cd7e1e 100644 --- a/uc_stock/tests/uc_stock.test +++ b/uc_stock/tests/uc_stock.test @@ -92,4 +92,39 @@ class UbercartStockTestCase extends UbercartTestHelper { $this->assertTrue(strpos($mail['body'], $this->product->model) !== FALSE, 'Mail body contains SKU.'); $this->assertTrue(strpos($mail['body'], 'has reached ' . $qty) !== FALSE, 'Mail body contains quantity.'); } + + public function testStockConsistency() { + $this->drupalGet('node/' . $this->product->nid . '/edit/stock'); + $this->assertText($this->product->title); + $this->assertText($this->product->model, 'Product SKU found.'); + + $this->assertNoFieldChecked('edit-stock-0-active', 'Stock tracking is not active.'); + $this->assertFieldByName('stock[0][stock]', '0', 'Default stock level found.'); + $this->assertFieldByName('stock[0][threshold]', '0', 'Default stock threshold found.'); + + $stock = 10; + $edit = array( + 'stock[0][active]' => 1, + 'stock[0][stock]' => $stock, + 'stock[0][threshold]' => 3, + ); + $this->drupalPost(NULL, $edit, t('Save changes')); + + // now change sku + $this->drupalGet('node/' . $this->product->nid.'/edit'); + $edit = array('model'=>$this->product->model .'t'); + $this->drupalPost(NULL, $edit, t('Save')); + + // check stock for old and new sku + $this->assertNotEqual($stock, uc_stock_level($this->product->model)); + $this->assertEqual($stock, uc_stock_level($this->product->model.'t')); + // also check in form + $this->drupalGet('node/' . $this->product->nid . '/edit/stock'); + $this->assertFieldChecked('edit-stock-0-active', 'Stock tracking is active.'); + $this->assertFieldByName('stock[0][stock]', $stock, 'Stock level maintained'); + $this->assertFieldByName('stock[0][threshold]', 3, 'Setted stock threshold found.'); + } + + // missing:test this t('SKU @newmodel in use, not updated',array('@newmodel'=>$newmodel)) + } diff --git a/uc_stock/uc_stock.module b/uc_stock/uc_stock.module index df4ce7c..b76d4b6 100644 --- a/uc_stock/uc_stock.module +++ b/uc_stock/uc_stock.module @@ -295,3 +295,84 @@ function uc_stock_increment_product_stock($product, $key, $order) { $product->qty = -$product->qty; return uc_stock_adjust_product_stock($product, $key, $order); } + +/** + * Implements hook_node_delete(). + * Clean stock info not needed anymore + * and remove conflict for new added product with same sku + */ +function uc_stock_node_delete($node) { + db_delete('uc_product_stock') + ->condition('nid',$node->nid) + ->execute(); +} + +/** + * Implements hook_node_presave(). + * http://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_update/7#comment-21434 + * and in code of node.module node_save() use original + */ +function uc_stock_node_presave($node) { + if(uc_product_is_product($node)) { + $r = db_select('uc_product_stock','stock') + ->fields('stock') + ->condition('sku',$node->model) + ->execute() + ->fetchAssoc(); + if($r) { // wayback: do not update + $newmodel = $node->model; + $node->model = $node->original->model; + drupal_set_message(t('SKU @newmodel in use, not updated',array('@newmodel'=>$newmodel))); + } + } +} + +/** + * Implements hook_node_update(). + */ +function uc_stock_node_update($node) { + if($node->original->model != $node->model) { + drupal_set_message('stock table updated'); + db_update('uc_product_stock') + ->fields(array('sku' => $node->model)) + ->condition('sku', $node->original->model) + ->execute(); + } +} + +/** + * implements hook_form_FORM_ID_alter() for uc_product_adjustments_form(). + * + * Save a copy of the original form values and insert our own submission + * function for product adjustments so we can update stock when model is changed + * for an attribute. + */ +function uc_stock_form_uc_product_adjustments_form_alter(&$form, &$form_state, $form_id) { + $form['original_body'] = array( + '#type' => 'value', + '#value' => $form['table']['body'], + ); + $form['#submit'][] = 'uc_stock_form_uc_product_adjustments_form_submit'; +} + +/** + * Submission function for uc_product_adjustments_form(). + * + * Update the stock table if the model number has changed for the variant. + * + * @see uc_product_adjustments_form_submit() + */ +function uc_stock_form_uc_product_adjustments_form_submit($form, &$form_state) { + foreach ($form_state['values']['body'] as $key => $value) { + $default_model = $form_state['values']['default']; + $old_model = !empty($form_state['values']['original_body'][$key]['model']) ? $form_state['values']['original_body'][$key]['model']['#default_value'] : ''; + $new_model = !empty($value['model']) ? $value['model'] : ''; + if ($old_model != $new_model) { + db_update('uc_product_stock') + ->fields(array('sku' => $new_model)) + ->condition('sku', $old_model) + ->execute(); + drupal_set_message(t('Updated stock table: changed sku @old_model to @new_model', array('@old_model' => $old_model, '@new_model' => $new_model))); + } + } +}