At the moment there are a number of module_exists('uc_stock') calls in uc_order.admin.inc, which should be replaced with form alters and hook implementations in uc_stock.

It would also be useful to add a setting/checkbox when editing orders to indicate if stock should be adjusted when order quantities are changed. At the moment this is handled with hardcoded order status checks, but there are some scenarios where order statuses are not enough. Sample use case: an order is completed and has been shipped, but the customer returns part of the order for a refund. Depending on the goods, the store owner may want to return the items to stock, or discard them; they can send a refund and edit the order to match (so any reports are correct), but at present the items will always be returned to stock.

I will be working on a patch for this soon but have posted this issue so others can comment if needed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

longwave’s picture

Title: Decouple stock control from order editing » Add checkboxes to enable/disable stock level changes when editing orders
Status: Active » Needs review
FileSize
7.95 KB

Patch attached. A new checkbox is added to the order edit page that allows you to enable/disable stock level changes when removing products or altering quantities (also handles changes of SKU), and another is added to the "add product" subform to decrease stock levels when new products are added to an order.

This is implemented by removing the uc_stock code from uc_order.admin.inc and adding a new hook_order op 'edit-products', which is caught by uc_stock to make the relevant changes.

longwave’s picture

Bumping to note that this has been successfully tested and is in use by my client that requested this feature, but perhaps could do with a code review before committing.

TR’s picture

Status: Needs review » Needs work
TR’s picture

Status: Needs work » Needs review

Let's kick the bot and see if it will test #1.

Status: Needs review » Needs work

The last submitted patch, order-edit-stock-control.patch, failed testing.

SilviuChingaru’s picture

The simplest way is to provide an order_save event for rules with original order updated order vars and user could check for status etc and change rules to decrease stock only in some states / statuses or any other rules condition. I already use this on my site...

Alan D.’s picture

Status: Needs work » Needs review
FileSize
7.48 KB

Re-roll.

Basic tests by myself all passed, have past to client to confirm.

longwave’s picture

@Alan: If your client confirms this works, I think it's worth committing to 6.x still, and then we can consider whether the same approach should apply in 7.x.

Alan D.’s picture

Early feedback suggests no problems.

Background from the clients prospective
Their issue was that when they added the orders via the admin interface, the stock was adjusted here, then again after doing the payment. This differs from the from end where stock is removed during the order process and is not adjusted when payments are processed.

This issue indirectly helps by giving them control here, but they are a bit perplexed for the need and are worried about getting this right.

longwave’s picture

Version: 6.x-2.x-dev » 7.x-3.x-dev
Status: Needs review » Patch (to be ported)

Committed to 6.x-2.x, let's decide whether this is the correct approach for 7.x-3.x now.

rjlang’s picture

In 7.x, all other stock incrementing/decrementing happens via Rules (which can be enabled/disabled/modified through the Rules interface). If the stock increment/decrement for admin-edited orders also took place via Rules, then store administrators could decide for themselves whether to apply such changes, and with far more flexibility than a simple checkbox, and the decisions for when and where stock gets incremented/decremented would all be in the same place, in the Rules interface.

rjlang’s picture

Here's a proposed implementation for D7, patched against 7.x-3.x-dev. All stock changes now happen via Rules, so an administrator can decide whether/when admin edits change stock levels by editing the relevant Rules conditions (as was the case with the existing Rules-based stock processing).

Summary of changes:

--Added a new event, 'uc_order_product_updated', to uc_order, which fires on product changes in admin edited orders;
--Added a new action, 'uc_stock_action_adjust_product_stock', to uc_stock, which updates stock levels based on changes in individual products;
--Added a new default rule, 'uc_stock_adjust_on_product_change', to uc_stock, which connects the two.
--Throughout uc_order and uc_stock, removed the direct stock manipulations that happened for edited orders and replaced them with firings of the 'uc_order_product_updated' event.
--Gave all uc_stock-related rules the tag 'uc_stock' so that tag filtering in Rules is useful.

The new rules are enabled by default and reproduce prior behavior so that existing installations don't break.

I've tested this on my local installation, would appreciate any further testing and/or feedback on the implementation. (Would love to see it adopted, of course.)

Robert

SilviuChingaru’s picture

Status: Patch (to be ported) » Needs review

Status: Needs review » Needs work

The last submitted patch, ubercart-1034044-12-stock-control-rules.patch, failed testing.

TR’s picture

Patch isn't in p1 format, so testbot can't apply it ...

rjlang’s picture

Sorry about that. Just switched to git from GUI+SVN and still learning the finer points. I think this is right.

longwave’s picture

Status: Needs work » Needs review
cwrinn’s picture

Tried this patch. Made no further modifications to the site and it still decrements stock/qoh twice when using Create Order (as if taking an order over the phone), once when the item is added to the invoice and once when payment is applied. I checked the rules list and there are none. Perhaps I am doing something wrong?

longwave’s picture

Did you clear cache after applying the patch? You should see a new rule for "Adjust stock upon change of product in order" which, when disabled, should mean that stock is no longer modified when you initially create the order. You could also add conditions to the rule so it only applies in specific situations, depending on how you want to run your store.

cwrinn’s picture

Done, still no rules. Odd there are no rules, at all. I did do an "Upgrade rules" after upgrading from D6 to D7, and seem to recall seeing rules then.

longwave’s picture

Where are you looking for these rules? You need to enable Rules UI module and go to /admin/config/workflow/rules to see all rules configured on a site.

cwrinn’s picture

That's where I've been. :\

fotisp’s picture

Issue summary: View changes

I've tested the patch for uc3 and it works fine
Now store admins can add / remove products and stock gets updated accordingly!
It's also works fine when dealing with alternate skus of the product :)
This is a must have patch, please commit it!
thanks

fotisp’s picture

hello there!
any news with this patch;
will it be included in the next release;
Do we need any extra tests;
thanks

fotisp’s picture

hello again,
is there anything i can do to "force" this patch to be commited? (test etc?)
thanks

rjlang’s picture

I'll just add that we've now been using the patch for the past two years with no issues.

henkpotman’s picture

Hello,

Is there any update regarding this matter? I have tried patch #16 on uc_order.admin.inc using Netbeams IDE 8.2, however I get a message 'patch partly applied' (Ubercart 7.3.10). I installed the patched module, run update.php, cleared cache, run cron but could not find any rule to change stock decrement settings for manual created admin orders.
Also I tried patch #7 but got a message 'patch cannot be applied in the selected context'. I tried the same with Ubercart 7.3.6 with the same result.
In my website customers pay mainly by bank transfer (so not all order will actually be confirmed), therefore in Rules I have set stock decrement for web created orders when a shipment is saved (instead of order creating), this is working fine.
However when creating a manual order by the admin, the stock is inmediately decremented when adding a product. This is a big problem because many of these orders won't be confirmed (when no payment is received).

Is there any way how i can avoid that stock is decremented inmediately when manually creating an order?

All suggestions are welcome. Thanks a lot!

TR’s picture

The patch still applies, with minor offsets. The patch affects five separate files - you can't just alter one file and expect the change to work. Read https://www.drupal.org/patch for more information about how to work with patches.

I am re-uploading the patch from #16 so that the "new" testbot can run it - #16 was uploaded before the testbot change, and the new testbot is unable to access any patches prior to that change. I have not altered the patch in any way - it's still the same as in #16.

henkpotman’s picture

Thanks a lot TR!

Indeed, you're right, it affects 5 different files. It's working fine now, I could dissable the automatic stock deduction for manually created orders in the Rules. It now works just as I needed!

Learning new things avery day...

Thanks a lot for your help!!

rjlang’s picture

For those who are interested, this patch has been merged into the Backdrop version of Ubercart.

https://github.com/backdrop-contrib/ubercart/pull/237