mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 21:25:42 +00:00 
			
		
		
		
	Refactor add_tracking_entry
This commit is contained in:
		| @@ -370,19 +370,17 @@ class PurchaseOrder(Order): | |||||||
|  |  | ||||||
|             tracking_info = { |             tracking_info = { | ||||||
|                 'status': status, |                 'status': status, | ||||||
|                 'purchaseorder': self.pk, |  | ||||||
|                 'quantity': quantity, |  | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             if location: |  | ||||||
|                 tracking_info['location'] = location.pk |  | ||||||
|  |  | ||||||
|             stock.add_tracking_entry( |             stock.add_tracking_entry( | ||||||
|                 StockHistoryCode.RECEIVED_AGAINST_PURCHASE_ORDER, |                 StockHistoryCode.RECEIVED_AGAINST_PURCHASE_ORDER, | ||||||
|                 user, |                 user, | ||||||
|                 notes=notes, |                 notes=notes, | ||||||
|                 url=self.get_absolute_url(), |                 url=self.get_absolute_url(), | ||||||
|                 deltas=tracking_info |                 deltas=tracking_info, | ||||||
|  |                 location=location, | ||||||
|  |                 purchaseorder=self, | ||||||
|  |                 quantity=quantity | ||||||
|             ) |             ) | ||||||
|  |  | ||||||
|         # Update the number of parts received against the particular line item |         # Update the number of parts received against the particular line item | ||||||
|   | |||||||
| @@ -199,17 +199,15 @@ class StockItem(MPTTModel): | |||||||
|         if add_note: |         if add_note: | ||||||
|  |  | ||||||
|             tracking_info = { |             tracking_info = { | ||||||
|                 'quantity': self.quantity, |  | ||||||
|                 'status': self.status, |                 'status': self.status, | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             if self.location: |  | ||||||
|                 tracking_info['location'] = self.location.pk |  | ||||||
|  |  | ||||||
|             self.add_tracking_entry( |             self.add_tracking_entry( | ||||||
|                 StockHistoryCode.CREATED, |                 StockHistoryCode.CREATED, | ||||||
|                 user, |                 user, | ||||||
|                 deltas=tracking_info |                 deltas=tracking_info, | ||||||
|  |                 location=self.location, | ||||||
|  |                 quantity=self.quantity, | ||||||
|             ) |             ) | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
| @@ -636,10 +634,6 @@ class StockItem(MPTTModel): | |||||||
|  |  | ||||||
|         tracking_info = {} |         tracking_info = {} | ||||||
|  |  | ||||||
|         if location: |  | ||||||
|             tracking_info['location'] = location.id |  | ||||||
|             tracking_info['location_name'] = location.name |  | ||||||
|  |  | ||||||
|         if self.customer: |         if self.customer: | ||||||
|             tracking_info['customer'] = self.customer.id |             tracking_info['customer'] = self.customer.id | ||||||
|             tracking_info['customer_name'] = self.customer.name |             tracking_info['customer_name'] = self.customer.name | ||||||
| @@ -648,7 +642,8 @@ class StockItem(MPTTModel): | |||||||
|             StockHistoryCode.RETURNED_FROM_CUSTOMER, |             StockHistoryCode.RETURNED_FROM_CUSTOMER, | ||||||
|             user, |             user, | ||||||
|             notes=notes, |             notes=notes, | ||||||
|             deltas=tracking_info |             deltas=tracking_info, | ||||||
|  |             location=location | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         self.customer = None |         self.customer = None | ||||||
| @@ -856,22 +851,15 @@ class StockItem(MPTTModel): | |||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         tracking_info = { |         tracking_info = { | ||||||
|             'assembly': self.belongs_to.pk |             'stockitem': self.belongs_to.pk | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if location: |  | ||||||
|             tracking_info['location'] = location.pk |  | ||||||
|             tracking_info['location_name'] = location.name |  | ||||||
|             url = location.get_absolute_url() |  | ||||||
|         else: |  | ||||||
|             url = '' |  | ||||||
|  |  | ||||||
|         self.add_tracking_entry( |         self.add_tracking_entry( | ||||||
|             StockHistoryCode.REMOVED_FROM_ASSEMBLY, |             StockHistoryCode.REMOVED_FROM_ASSEMBLY, | ||||||
|             user, |             user, | ||||||
|             notes=notes, |             notes=notes, | ||||||
|             url=url, |             deltas=tracking_info, | ||||||
|             deltas=tracking_info |             location=location, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         # Mark this stock item as *not* belonging to anyone |         # Mark this stock item as *not* belonging to anyone | ||||||
| @@ -935,7 +923,7 @@ class StockItem(MPTTModel): | |||||||
|     def has_tracking_info(self): |     def has_tracking_info(self): | ||||||
|         return self.tracking_info_count > 0 |         return self.tracking_info_count > 0 | ||||||
|  |  | ||||||
|     def add_tracking_entry(self, entry_type, user, deltas={}, notes='', url=''): |     def add_tracking_entry(self, entry_type, user, deltas={}, notes='', url='', **kwargs): | ||||||
|         """ |         """ | ||||||
|         Add a history tracking entry for this StockItem |         Add a history tracking entry for this StockItem | ||||||
|  |  | ||||||
| @@ -947,6 +935,25 @@ class StockItem(MPTTModel): | |||||||
|             url - Optional URL associated with this tracking entry |             url - Optional URL associated with this tracking entry | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|  |         # Has a location been specified? | ||||||
|  |         location = kwargs.get('location', None) | ||||||
|  |  | ||||||
|  |         if location: | ||||||
|  |             deltas['location'] = location.id | ||||||
|  |             deltas['location_path'] = location.pathstring | ||||||
|  |  | ||||||
|  |         # Has a PurchaseOrder been specified? | ||||||
|  |         po = kwargs.get('purchaseorder', None) | ||||||
|  |  | ||||||
|  |         if po: | ||||||
|  |             deltas['purchaseorder'] = po.id | ||||||
|  |  | ||||||
|  |         # Quantity specified? | ||||||
|  |         quantity = kwargs.get('quantity', None) | ||||||
|  |  | ||||||
|  |         if quantity: | ||||||
|  |             deltas['quantity'] = float(quantity) | ||||||
|  |  | ||||||
|         entry = StockItemTracking.objects.create( |         entry = StockItemTracking.objects.create( | ||||||
|             item=self, |             item=self, | ||||||
|             tracking_type=entry_type, |             tracking_type=entry_type, | ||||||
| @@ -1037,7 +1044,8 @@ class StockItem(MPTTModel): | |||||||
|                 notes=notes, |                 notes=notes, | ||||||
|                 deltas={ |                 deltas={ | ||||||
|                     'serial': serial, |                     'serial': serial, | ||||||
|                 } |                 }, | ||||||
|  |                 location=location | ||||||
|             ) |             ) | ||||||
|  |  | ||||||
|         # Remove the equivalent number of items |         # Remove the equivalent number of items | ||||||
| @@ -1126,7 +1134,8 @@ class StockItem(MPTTModel): | |||||||
|             notes=notes, |             notes=notes, | ||||||
|             deltas={ |             deltas={ | ||||||
|                 'stockitem': self.pk, |                 'stockitem': self.pk, | ||||||
|             } |             }, | ||||||
|  |             location=location, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         # Remove the specified quantity from THIS stock item |         # Remove the specified quantity from THIS stock item | ||||||
| @@ -1187,18 +1196,12 @@ class StockItem(MPTTModel): | |||||||
|  |  | ||||||
|         tracking_info = {} |         tracking_info = {} | ||||||
|  |  | ||||||
|         if location: |  | ||||||
|             tracking_info['location'] = location.pk |  | ||||||
|             url = location.get_absolute_url() |  | ||||||
|         else: |  | ||||||
|             url = '' |  | ||||||
|  |  | ||||||
|         self.add_tracking_entry( |         self.add_tracking_entry( | ||||||
|             StockHistoryCode.STOCK_MOVE, |             StockHistoryCode.STOCK_MOVE, | ||||||
|             user, |             user, | ||||||
|             notes=notes, |             notes=notes, | ||||||
|             deltas=tracking_info, |             deltas=tracking_info, | ||||||
|             url=url, |             location=location, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         self.save() |         self.save() | ||||||
| @@ -1306,7 +1309,8 @@ class StockItem(MPTTModel): | |||||||
|  |  | ||||||
|     @transaction.atomic |     @transaction.atomic | ||||||
|     def take_stock(self, quantity, user, notes=''): |     def take_stock(self, quantity, user, notes=''): | ||||||
|         """ Remove items from stock |         """ | ||||||
|  |         Remove items from stock | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|         # Cannot remove items from a serialized part |         # Cannot remove items from a serialized part | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user