mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Start of API forms for stock item
This commit is contained in:
		@@ -134,7 +134,7 @@ class StockItemSerializer(InvenTreeModelSerializer):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    tracking_items = serializers.IntegerField(source='tracking_info_count', read_only=True, required=False)
 | 
					    tracking_items = serializers.IntegerField(source='tracking_info_count', read_only=True, required=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    quantity = serializers.FloatField()
 | 
					    # quantity = serializers.FloatField()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    allocated = serializers.FloatField(source='allocation_count', required=False)
 | 
					    allocated = serializers.FloatField(source='allocation_count', required=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -142,20 +142,22 @@ class StockItemSerializer(InvenTreeModelSerializer):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    stale = serializers.BooleanField(required=False, read_only=True)
 | 
					    stale = serializers.BooleanField(required=False, read_only=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    serial = serializers.CharField(required=False)
 | 
					    # serial = serializers.CharField(required=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    required_tests = serializers.IntegerField(source='required_test_count', read_only=True, required=False)
 | 
					    required_tests = serializers.IntegerField(source='required_test_count', read_only=True, required=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    purchase_price = InvenTreeMoneySerializer(
 | 
					    purchase_price = InvenTreeMoneySerializer(
 | 
				
			||||||
        label=_('Purchase Price'),
 | 
					        label=_('Purchase Price'),
 | 
				
			||||||
        max_digits=19, decimal_places=4,
 | 
					        max_digits=19, decimal_places=4,
 | 
				
			||||||
        allow_null=True
 | 
					        allow_null=True,
 | 
				
			||||||
 | 
					        help_text=_('Purchase price of this stock item'),
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    purchase_price_currency = serializers.ChoiceField(
 | 
					    purchase_price_currency = serializers.ChoiceField(
 | 
				
			||||||
        choices=currency_code_mappings(),
 | 
					        choices=currency_code_mappings(),
 | 
				
			||||||
        default=currency_code_default,
 | 
					        default=currency_code_default,
 | 
				
			||||||
        label=_('Currency'),
 | 
					        label=_('Currency'),
 | 
				
			||||||
 | 
					        help_text=_('Purchase currency of this stock item'),
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    purchase_price_string = serializers.SerializerMethodField()
 | 
					    purchase_price_string = serializers.SerializerMethodField()
 | 
				
			||||||
@@ -197,6 +199,7 @@ class StockItemSerializer(InvenTreeModelSerializer):
 | 
				
			|||||||
            'belongs_to',
 | 
					            'belongs_to',
 | 
				
			||||||
            'build',
 | 
					            'build',
 | 
				
			||||||
            'customer',
 | 
					            'customer',
 | 
				
			||||||
 | 
					            'delete_on_deplete',
 | 
				
			||||||
            'expired',
 | 
					            'expired',
 | 
				
			||||||
            'expiry_date',
 | 
					            'expiry_date',
 | 
				
			||||||
            'in_stock',
 | 
					            'in_stock',
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -150,6 +150,7 @@
 | 
				
			|||||||
                <li><a href='#' id='stock-duplicate' title='{% trans "Duplicate stock item" %}'><span class='fas fa-copy'></span> {% trans "Duplicate stock item" %}</a></li>
 | 
					                <li><a href='#' id='stock-duplicate' title='{% trans "Duplicate stock item" %}'><span class='fas fa-copy'></span> {% trans "Duplicate stock item" %}</a></li>
 | 
				
			||||||
                {% endif %}
 | 
					                {% endif %}
 | 
				
			||||||
                <li><a href='#' id='stock-edit' title='{% trans "Edit stock item" %}'><span class='fas fa-edit icon-blue'></span> {% trans "Edit stock item" %}</a></li>
 | 
					                <li><a href='#' id='stock-edit' title='{% trans "Edit stock item" %}'><span class='fas fa-edit icon-blue'></span> {% trans "Edit stock item" %}</a></li>
 | 
				
			||||||
 | 
					                <li><a href='#' id='stock-edit-2' title='{% trans "Edit stock item" %}'><span class='fas fa-edit icon-blue'></span> {% trans "Edit stock item" %}</a></li>
 | 
				
			||||||
                {% if user.is_staff or roles.stock.delete %}
 | 
					                {% if user.is_staff or roles.stock.delete %}
 | 
				
			||||||
                    {% if item.can_delete %}
 | 
					                    {% if item.can_delete %}
 | 
				
			||||||
                    <li><a href='#' id='stock-delete' title='{% trans "Delete stock item" %}'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete stock item" %}</a></li>
 | 
					                    <li><a href='#' id='stock-delete' title='{% trans "Delete stock item" %}'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete stock item" %}</a></li>
 | 
				
			||||||
@@ -520,6 +521,10 @@ $("#stock-edit").click(function () {
 | 
				
			|||||||
    );
 | 
					    );
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$('#stock-edit-2').click(function() {
 | 
				
			||||||
 | 
					    editStockItem({{ item.pk }});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$('#stock-edit-status').click(function () {
 | 
					$('#stock-edit-status').click(function () {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    constructForm('{% url "api-stock-detail" item.pk %}', {
 | 
					    constructForm('{% url "api-stock-detail" item.pk %}', {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -179,6 +179,7 @@ function constructChangeForm(fields, options) {
 | 
				
			|||||||
    // Request existing data from the API endpoint
 | 
					    // Request existing data from the API endpoint
 | 
				
			||||||
    $.ajax({
 | 
					    $.ajax({
 | 
				
			||||||
        url: options.url,
 | 
					        url: options.url,
 | 
				
			||||||
 | 
					        data: options.params || {},
 | 
				
			||||||
        type: 'GET',
 | 
					        type: 'GET',
 | 
				
			||||||
        contentType: 'application/json',
 | 
					        contentType: 'application/json',
 | 
				
			||||||
        dataType: 'json',
 | 
					        dataType: 'json',
 | 
				
			||||||
@@ -195,6 +196,17 @@ function constructChangeForm(fields, options) {
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
 | 
					            // An optional function can be provided to process the returned results,
 | 
				
			||||||
 | 
					            // before they are rendered to the form
 | 
				
			||||||
 | 
					            if (options.processResults) {
 | 
				
			||||||
 | 
					                var processed = options.processResults(data, fields, options);
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                // If the processResults function returns data, it will be stored
 | 
				
			||||||
 | 
					                if (processed) {
 | 
				
			||||||
 | 
					                    data = processed;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Store the entire data object
 | 
					            // Store the entire data object
 | 
				
			||||||
            options.instance = data;
 | 
					            options.instance = data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -68,6 +68,95 @@ function locationFields() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function stockItemFields(options={}) {
 | 
				
			||||||
 | 
					    var fields = {
 | 
				
			||||||
 | 
					        part: {},
 | 
				
			||||||
 | 
					        supplier_part: {
 | 
				
			||||||
 | 
					            filters: {
 | 
				
			||||||
 | 
					                part_detail: true,
 | 
				
			||||||
 | 
					                supplier_detail: true,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            adjustFilters: function(query, opts) {
 | 
				
			||||||
 | 
					                var part = getFormFieldValue('part', {}, opts);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (part) {
 | 
				
			||||||
 | 
					                    query.part = part;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                return query;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        serial: {},
 | 
				
			||||||
 | 
					        status: {},
 | 
				
			||||||
 | 
					        expiry_date: {},
 | 
				
			||||||
 | 
					        batch: {},
 | 
				
			||||||
 | 
					        purchase_price: {},
 | 
				
			||||||
 | 
					        purchase_price_currency: {},
 | 
				
			||||||
 | 
					        packaging: {},
 | 
				
			||||||
 | 
					        link: {},
 | 
				
			||||||
 | 
					        delete_on_deplete: {},
 | 
				
			||||||
 | 
					        // owner: {},
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Remove stock expiry fields if feature is not enabled
 | 
				
			||||||
 | 
					    if (!global_settings.STOCK_ENABLE_EXPIRY) {
 | 
				
			||||||
 | 
					        delete fields['expiry_date'];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return fields;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function stockItemGroups(options={}) {
 | 
				
			||||||
 | 
					    return {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Launch a modal form to edit a given StockItem
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function editStockItem(pk, options={}) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var url = `/api/stock/${pk}/`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var fields = stockItemFields(options);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Prevent editing of the "part"
 | 
				
			||||||
 | 
					    fields.part.hidden = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var groups = stockItemGroups(options);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    constructForm(url, {
 | 
				
			||||||
 | 
					        fields: fields,
 | 
				
			||||||
 | 
					        groups: groups,
 | 
				
			||||||
 | 
					        title: '{% trans "Edit Stock Item" %}',
 | 
				
			||||||
 | 
					        params: {
 | 
				
			||||||
 | 
					            part_detail: true,
 | 
				
			||||||
 | 
					            supplier_part_detail: true,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        processResults: function(data, fields, options) {
 | 
				
			||||||
 | 
					            // Callback when StockItem data is received from server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (data.part_detail.trackable) {
 | 
				
			||||||
 | 
					                delete options.fields.delete_on_deplete;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                // Remove serial number field if part is not trackable
 | 
				
			||||||
 | 
					                delete options.fields.serial;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Remove pricing fields if part is not purchaseable
 | 
				
			||||||
 | 
					            if (!data.part_detail.purchaseable) {
 | 
				
			||||||
 | 
					                delete options.fields.supplier_part;
 | 
				
			||||||
 | 
					                delete options.fields.purchase_price;
 | 
				
			||||||
 | 
					                delete options.fields.purchase_price_currency;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Stock API functions
 | 
					/* Stock API functions
 | 
				
			||||||
 * Requires api.js to be loaded first
 | 
					 * Requires api.js to be loaded first
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user