Replies: 1 comment
-
I have written this code here to work with Python and export to PDF as an example: import requests
import random
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from datetime import datetime
# Base URL of your InvenTree instance
base_url = "http://0.0.0.0:8000/api/"
# Your API token for authentication
api_token = "inv-0000000000000000000000000000000000000000000"
# Set up headers for the API request
headers = {
"Authorization": f"Token {api_token}",
"Content-Type": "application/json",
}
def get_all_parts():
"""Retrieve a list of all parts."""
url = f"{base_url}part/"
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to retrieve parts. Status Code: {response.status_code}")
return []
def get_stock_for_part(part_id):
"""Retrieve stock levels for a specific part."""
url = f"{base_url}stock/?part={part_id}"
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to retrieve stock for part ID {part_id}. Status Code: {response.status_code}")
return []
def get_all_locations_stock():
"""Retrieve stock levels for all parts and their locations."""
all_parts = get_all_parts()
all_stock_data = []
for part in all_parts:
part_id = part.get('pk')
stock_items = get_stock_for_part(part_id)
if not stock_items:
# If no stock items, create an entry with zero stock
all_stock_data.append({
'Part ID': part_id,
'Part Name': part.get('name', 'Unknown'),
'Location': 'Unknown',
'Current Stock Level': 0,
'Stock On Order': 0
})
else:
# Append each stock item
for stock_item in stock_items:
location = stock_item.get('location', {}).get('name', 'Unknown') if stock_item.get('location') else 'Unknown'
current_stock = stock_item.get('quantity', 0)
stock_on_order = stock_item.get('quantity_on_order', 0)
all_stock_data.append({
'Part ID': part_id,
'Part Name': part.get('name', 'Unknown'),
'Location': location,
'Current Stock Level': current_stock,
'Stock On Order': stock_on_order
})
return all_stock_data
def select_random_stock_items(stock_data, count=10):
"""Select a specified number of random stock items."""
if len(stock_data) <= count:
return stock_data # If fewer stock items are available than requested, return them all.
return random.sample(stock_data, count)
def save_stock_to_pdf(stock_data, filename='random_stock_items.pdf'):
"""Save the selected stock data to a PDF file with a title and logo."""
c = canvas.Canvas(filename, pagesize=letter)
width, height = letter
current_time = datetime.now().strftime('%d-%m-%Y')
# Add Title
title = "Perpetual Stock Take - "+current_time
c.setFont("Helvetica-Bold", 18)
c.drawString(40, height - 40, title)
# Add Logo
logo_path = "logo.png" # Update with your logo file path
c.drawImage(logo_path, 400, height - 150, width=200, height=200) # Adjust size and position as needed
# Set font for table header
c.setFont("Helvetica", 10)
y = height - 140 # Starting y position for table
# Header
c.drawString(40, y, 'Part ID')
c.drawString(100, y, 'Part Name')
c.drawString(300, y, 'Location')
c.drawString(400, y, 'Current Stock Level')
c.drawString(550, y, 'Stock On Order')
y -= 20
for item in stock_data:
if y < 40: # Create a new page if needed
c.showPage()
c.setFont("Helvetica", 10)
y = height - 40
c.drawString(40, y, 'Part ID')
c.drawString(100, y, 'Part Name')
c.drawString(300, y, 'Location')
c.drawString(400, y, 'Current Stock Level')
c.drawString(550, y, 'Stock On Order')
y -= 20
# Ensure all data is converted to string
part_id = str(item.get('Part ID', ''))
part_name = str(item.get('Part Name', ''))
location = str(item.get('Location', ''))
current_stock_level = str(item.get('Current Stock Level', 0))
stock_on_order = str(item.get('Stock On Order', 0))
c.drawString(40, y, part_id)
c.drawString(100, y, part_name)
c.drawString(300, y, location)
c.drawString(400, y, current_stock_level)
c.drawString(550, y, stock_on_order)
y -= 20
c.save()
def print_stock_info(stock_data):
"""Print the stock data information to the shell."""
print(f"{'Part ID':<10} {'Part Name':<30} {'Location':<20} {'Current Stock Level':<20} {'Stock On Order':<20}")
print("="*100)
for item in stock_data:
print(f"{item['Part ID']:<10} {item['Part Name']:<30} {item['Location']:<20} {item['Current Stock Level']:<20} {item['Stock On Order']:<20}")
if __name__ == "__main__":
all_stock_data = get_all_locations_stock()
random_stock_items = select_random_stock_items(all_stock_data, 10)
# Generate filename with date and time
current_time = datetime.now().strftime('%d-%m-%Y_%H-%M-%S')
filename = f'Perpetual Stock Take - {current_time}.pdf'
save_stock_to_pdf(random_stock_items, filename)
print_stock_info(random_stock_items)
print(f"Random stock items have been saved to '{filename}'.") |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi guys,
Just a thought, would it be possible to consider adding an option for perpetual stock take?
So a perpetual stock take is where there are 10 (or so) items randomly selected from all stock, and this is to be checked by a someone. They are then able to enter the counted value, and adjust the stock accordingly from a single page relating to the perpetual. The perpetual stock take would be best to auto generate on a week-daily basis.
This would help keep on top of stock going missing.
I use to work in wholesale where we stocked 4000 lines of stock and we had to count 10 lines of stock a day to ensure the stock levels were correct, and it really helped to keep track of stock incase someone forgot to put it on an invoice, normally if the missing stock is found, you will have half a chance of recovering the losses from the customer.
I would love to be able to help code it, but unfortunately i've never work on a system like this. I can code in python & PHP & SQL, but i am unsure of where to start, so some guidance or help would be massively apricated!!
Thank you...
Winticee
Beta Was this translation helpful? Give feedback.
All reactions