mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Remove 'destination' field if we are not moving stock
- Allow different stock adjustment actions
This commit is contained in:
		@@ -578,10 +578,7 @@ function loadStockTable(table, options) {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $('#multi-item-add').click(function() {
 | 
					    $('#multi-item-add').click(function() {
 | 
				
			||||||
        updateStockItems({
 | 
					        stockAdjustment('add');
 | 
				
			||||||
            action: 'add',
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
        return false;
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $("#multi-item-move").click(function() {
 | 
					    $("#multi-item-move").click(function() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,7 +21,10 @@
 | 
				
			|||||||
            {{ item.part.full_name }} <small><i>{{ item.part.description }}</i></small></td> 
 | 
					            {{ item.part.full_name }} <small><i>{{ item.part.description }}</i></small></td> 
 | 
				
			||||||
          <td>{{ item.location.pathstring }}</td> 
 | 
					          <td>{{ item.location.pathstring }}</td> 
 | 
				
			||||||
          <td>
 | 
					          <td>
 | 
				
			||||||
            <input class='numberinput' min='0' max='{{ item.quantity }}' value='{{ item.new_quantity }}' type='number' name='stock-id-{{ item.id }}' id='stock-id-{{ item.id }}'/>
 | 
					            <input class='numberinput'
 | 
				
			||||||
 | 
					              min='0'
 | 
				
			||||||
 | 
					              {% if  stock_action == 'move' %} max='{{ item.quantity }}' {% endif %}
 | 
				
			||||||
 | 
					              value='{{ item.new_quantity }}' type='number' name='stock-id-{{ item.id }}' id='stock-id-{{ item.id }}'/>
 | 
				
			||||||
            {% if item.error %}
 | 
					            {% if item.error %}
 | 
				
			||||||
            <br><span class='help-inline'>{{ item.error }}</span>
 | 
					            <br><span class='help-inline'>{{ item.error }}</span>
 | 
				
			||||||
            {% endif %}
 | 
					            {% endif %}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -174,7 +174,7 @@ class StockAdjust(AjaxView, FormMixin):
 | 
				
			|||||||
        for item in items:
 | 
					        for item in items:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Initialize quantity to zero for addition/removal
 | 
					            # Initialize quantity to zero for addition/removal
 | 
				
			||||||
            if self.stock_action in ['take', 'give']:
 | 
					            if self.stock_action in ['take', 'add']:
 | 
				
			||||||
                item.new_quantity = 0
 | 
					                item.new_quantity = 0
 | 
				
			||||||
            # Initialize quantity at full amount for counting or moving
 | 
					            # Initialize quantity at full amount for counting or moving
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
@@ -216,6 +216,15 @@ class StockAdjust(AjaxView, FormMixin):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return context
 | 
					        return context
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_form(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        form = super().get_form()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if not self.stock_action == 'move':
 | 
				
			||||||
 | 
					            form.fields.pop('destination')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return form
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get(self, request, *args, **kwargs):
 | 
					    def get(self, request, *args, **kwargs):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.request = request
 | 
					        self.request = request
 | 
				
			||||||
@@ -224,7 +233,7 @@ class StockAdjust(AjaxView, FormMixin):
 | 
				
			|||||||
        self.stock_action = request.GET.get('action', '').lower()
 | 
					        self.stock_action = request.GET.get('action', '').lower()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Pick a default action...
 | 
					        # Pick a default action...
 | 
				
			||||||
        if self.stock_action not in ['move', 'count', 'take', 'give']:
 | 
					        if self.stock_action not in ['move', 'count', 'take', 'add']:
 | 
				
			||||||
            self.stock_action = 'count'
 | 
					            self.stock_action = 'count'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Choose the form title based on the action
 | 
					        # Choose the form title based on the action
 | 
				
			||||||
@@ -232,7 +241,7 @@ class StockAdjust(AjaxView, FormMixin):
 | 
				
			|||||||
            'move': 'Move Stock',
 | 
					            'move': 'Move Stock',
 | 
				
			||||||
            'count': 'Count Stock',
 | 
					            'count': 'Count Stock',
 | 
				
			||||||
            'take': 'Remove Stock',
 | 
					            'take': 'Remove Stock',
 | 
				
			||||||
            'give': 'Add Stock'
 | 
					            'add': 'Add Stock'
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.ajax_form_title = titles[self.stock_action]
 | 
					        self.ajax_form_title = titles[self.stock_action]
 | 
				
			||||||
@@ -246,6 +255,8 @@ class StockAdjust(AjaxView, FormMixin):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        self.request = request
 | 
					        self.request = request
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.stock_action = request.POST.get('stock_action').lower()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Update list of stock items
 | 
					        # Update list of stock items
 | 
				
			||||||
        self.stock_items = self.get_POST_items()
 | 
					        self.stock_items = self.get_POST_items()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -266,6 +277,8 @@ class StockAdjust(AjaxView, FormMixin):
 | 
				
			|||||||
                valid = False
 | 
					                valid = False
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if self.stock_action in ['move']:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if item.new_quantity > item.quantity:
 | 
					                if item.new_quantity > item.quantity:
 | 
				
			||||||
                    item.error = _('Quantity must not exceed {x}'.format(x=item.quantity))
 | 
					                    item.error = _('Quantity must not exceed {x}'.format(x=item.quantity))
 | 
				
			||||||
                    valid = False
 | 
					                    valid = False
 | 
				
			||||||
@@ -281,25 +294,67 @@ class StockAdjust(AjaxView, FormMixin):
 | 
				
			|||||||
            'form_valid': valid,
 | 
					            'form_valid': valid,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.stock_action = request.POST.get('stock_action').lower()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if valid:
 | 
					        if valid:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if self.stock_action == 'move':
 | 
					            data['success'] = self.do_action()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return self.renderJsonResponse(request, form, data=data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def do_action(self):
 | 
				
			||||||
 | 
					        """ Perform stock adjustment action """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if self.stock_action == 'move':
 | 
				
			||||||
            destination = None
 | 
					            destination = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                    destination = StockLocation.objects.get(id=form['destination'].value())
 | 
					                destination = StockLocation.objects.get(id=self.request.POST.get('destination'))
 | 
				
			||||||
            except StockLocation.DoesNotExist:
 | 
					            except StockLocation.DoesNotExist:
 | 
				
			||||||
                pass
 | 
					                pass
 | 
				
			||||||
            except ValueError:
 | 
					            except ValueError:
 | 
				
			||||||
                pass
 | 
					                pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if destination:
 | 
					            return self.do_move(destination)
 | 
				
			||||||
                    data['success'] = self.do_move(destination)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self.renderJsonResponse(request, form, data=data)
 | 
					        elif self.stock_action == 'add':
 | 
				
			||||||
 | 
					            return self.do_add()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        elif self.stock_action == 'take':
 | 
				
			||||||
 | 
					            return self.do_take()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        elif self.stock_action == 'count':
 | 
				
			||||||
 | 
					            return self.do_count()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            return 'No action performed'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def do_add(self):
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        count = 0
 | 
				
			||||||
 | 
					        note = self.request.POST['note']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for item in self.stock_items:
 | 
				
			||||||
 | 
					            if item.new_quantity <= 0:
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            count += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return _("Added stock to {n} items".format(n=count))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def do_take(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        count = 0
 | 
				
			||||||
 | 
					        note = self.request.POST['note']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for item in self.stock_items:
 | 
				
			||||||
 | 
					            if item.new_quantity <= 0:
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            count += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return _("Removed stock from {n} items".format(n=count))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def do_count(self):
 | 
				
			||||||
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def do_move(self, destination):
 | 
					    def do_move(self, destination):
 | 
				
			||||||
        """ Perform actual stock movement """
 | 
					        """ Perform actual stock movement """
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user