Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pricing] Add option to convert received items currency #8970

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/qc_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ env:
INVENTREE_STATIC_ROOT: ../test_inventree_static
INVENTREE_BACKUP_DIR: ../test_inventree_backup
INVENTREE_SITE_URL: http://localhost:8000
INVENTREE_DEBUG: true

permissions:
contents: read
Expand Down Expand Up @@ -570,7 +571,7 @@ jobs:
install: true
update: true
- name: Set up test data
run: invoke dev.setup-test -i
run: invoke dev.setup-test -iv
- name: Rebuild thumbnails
run: invoke int.rebuild-thumbnails
- name: Install dependencies
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ api.yaml
src/backend/InvenTree/web/static
InvenTree/web/static

# Generated docs files
# Exported interim files
docs/schema.yml
docs/docs/api/*.yml
docs/docs/api/schema/*.yml
inventree_settings.json
inventree_filters.yml
inventree_tags.yml
65 changes: 65 additions & 0 deletions docs/docs/concepts/pricing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: Pricing Support
---

## Pricing

Pricing is an inherently complex topic, often subject to the particular requirements of the user. InvenTree attempts to provide a comprehensive pricing architecture which is useful without being proscriptive.

InvenTree provides support for multiple currencies, allowing pricing information to be stored with base currency rates.

!!! warning "Raw Data Only"
InvenTree stores raw pricing data, as provided by the user. Any calculations or decisions based on this data must take into consideration the context in which the data are entered.

InvenTree uses the [django-money](https://github.com/django-money/django-money) library, which in turn uses the [py-moneyed library](https://py-moneyed.readthedocs.io/en/latest/index.html). `py-moneyed` supports any currency which is defined in the [ISO 3166 standard](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes) standard.


### Terminology

Throughout this documentation (and within InvenTree) the concepts of *cost* and *price* are separated as follows:

| Term | Description |
| --- | --- |
| Price | The theoretical amount of money required to pay for something. |
| Cost | The actual amount of money paid. |


## Currency Support

InvenTree supports pricing data in multiple currencies, allowing integration with suppliers and customers using different currency systems.

### Default Currency

Many of the pricing operations are performed in reference to a *Default Currency* (which can be selected for the particular InvenTree installation).

The default currency is user configurable in the InvenTree settings.

!!! warning "Setting Default Currency"
Changing the default currency once the system in use may have unintended consequences. It is recommended to set the default currency during the initial setup of the InvenTree instance.

## Conversion Rates

To facilitate conversion between different currencies, exchange rate information is stored in the InvenTree database.

### Currency Codes

The list of support currency codes is user configurable in the InvenTree settings. It is recommended to select only the currencies which are relevant to the user.

While InvenTree can support any of the currencies defined in the ISO 3166 standard, the list of supported currencies can be limited to only those which are relevant to the user. The supported currencies are used to populate the currency selection dropdowns throughout the InvenTree interface.


### Exchange Rate Data

The exchange rate data is provided by a [currency plugin](../extend/plugins/currency.md) which fetches exchange rate data from an external source.

InvenTree includes a default currency plugin which fetches exchange rate data from the [frankfurter](https://frankfurter.dev/) API, which is an open source currency API made freely available.

However, the user can configure a custom currency plugin to fetch exchange rate data from a different source. If a different currency exchange backend is needed, or a custom implementation is desired, the currency exchange framework can be extended [via plugins](../extend/plugins/currency.md). Plugins which implement custom currency exchange frameworks can be easily integrated into the InvenTree framework.

### Exchange Rate Updates

Currency exchange rates are updated periodically, using the configured currency plugin. The update frequency can be configured in the InvenTree settings.

## Pricing Settings

Refer to the [global settings](../settings/global.md#pricing-and-currency) documentation for more information on available currency settings.
20 changes: 17 additions & 3 deletions docs/docs/order/purchase_order.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ Purchase Order Status supports [custom states](../concepts/custom_states.md).

### Purchase Order Currency

The currency code can be specified for an individual purchase order. If not specified, the default currency specified against the [supplier](./company.md#suppliers) will be used.
The currency code can be specified for an individual purchase order. If not specified, the default currency specified against the [supplier](./company.md#suppliers) will be used. Additionally, the currency can be specified separately for each line item.

So, when determining the cost of each line item in the purchase order, the following order of precedence is used:

1. Line item currency
2. Purchase order currency
3. Supplier currency
4. Default (base) currency

## Create Purchase Order

Expand Down Expand Up @@ -77,11 +84,11 @@ It is possible to upload an exported purchase order from the supplier instead of
!!! info "Supported Formats"
This process only supports tabular data and the following formats are supported: CSV, TSV, XLS, XLSX, JSON and YAML

### Issue Order
## Issue Order

Once all the line items were added, click on the <span class='fas fa-paper-plane'></span> button on the main purchase order detail panel and confirm the order has been submitted.

### Receive Line Items
## Receive Line Items

After receiving all the items from the order, the purchase order will convert the line items into stock items / inventory.

Expand All @@ -107,6 +114,12 @@ Each item marked as "received" is automatically converted into a stock item.

To see the list of stock items created from the purchase order, click on the <span class="badge inventree nav side"><span class='fas fa-sign-in-alt'></span> Received Items</span> tab.

### Item Value Currency

The unit cost of the purchase order line item is transferred across to the created stock item. By default, the same currency is used for the stock item as was used for the purchase order line item.

However, if the [Convert Currency](#purchase-order-settings) setting is enabled, the currency of the stock item will be converted to the [default currency](../concepts/pricing.md#default-currency) of the system. This may be useful when ordering stock in a different currency, to ensure that the unit cost of the stock item is converted to the base currency at the time of receipt.

## Complete Order

Once the quantity of all __received__ items is equal or above the quantity of all line items, the order will be automatically marked as __complete__.
Expand Down Expand Up @@ -174,5 +187,6 @@ The following [global settings](../settings/global.md) are available for purchas
| ---- | ----------- | ------- | ----- |
{{ globalsetting("PURCHASEORDER_REFERENCE_PATTERN") }}
{{ globalsetting("PURCHASEORDER_REQUIRE_RESPONSIBLE") }}
{{ globalsetting("PURCHASEORDER_CONVERT_CURRENCY") }}
{{ globalsetting("PURCHASEORDER_EDIT_COMPLETED_ORDERS") }}
{{ globalsetting("PURCHASEORDER_AUTO_COMPLETE") }}
43 changes: 5 additions & 38 deletions docs/docs/part/pricing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@
title: Pricing
---

## Pricing
## Part Pricing

Pricing is an inherently complex topic, often subject to the particular requirements of the user. InvenTree attempts to provide a comprehensive pricing architecture which is useful without being proscriptive.

!!! warning "Raw Data Only"
InvenTree stores raw pricing data, as provided by the user. Any calculations or decisions based on this data must take into consideration the context in which the data are entered.

### Terminology

Throughout this documentation (and within InvenTree) the concepts of *cost* and *price* are separated as follows:

| Term | Description |
| --- | --- |
| Price | The theoretical amount of money required to pay for something. |
| Cost | The actual amount of money paid. |

### Pricing Sources
!!! info "Pricing Support"
Refer to the [Pricing Support](../concepts/pricing.md) documentation for more information on pricing support in InvenTree.

Pricing information can be determined from multiple sources:

Expand All @@ -42,26 +29,6 @@ Additionally, the following information is stored for each part, in relation to
| Sale Price | How much a salable item is sold for (with price-breaks) | [Part](../part/part.md) |
| Sale Cost | How much an item was sold for | [Sales Order](../order/sales_order.md) |

### Currency Support

InvenTree supports pricing data in multiple currencies, allowing integration with suppliers and customers using different currency systems.

Supported currencies can be configured in the [InvenTree settings](../settings/currency.md).

!!! info "Currency Support"
InvenTree provides multi-currency pricing support via the [django-money](https://django-money.readthedocs.io/en/latest/) library.

#### Default Currency

Many of the pricing operations are performed in reference to a *Default Currency* (which can be selected for the particular InvenTree installation).

#### Conversion Rates

To facilitate conversion between different currencies, exchange rate data is provided via the [exchangerate.host](https://exchangerate.host/#/) API. Currency exchange rates are updated once per day.

!!! tip "Custom Exchange Rates"
Custom exchange rates or databases can be used if desired.

## Pricing Tab

The pricing tab for a given Part provides all available pricing information for that part. It shows all price ranges and provides tools to calculate them.
Expand All @@ -81,7 +48,7 @@ At the top of the pricing tab, an *Overview* section shows a synopsis of the ava

This overview tab provides information on the *range* of pricing data available within each category. If pricing data is not available for a given category, it is marked as *No data*.

Each price range is calculated in the [Default Currency](#default-currency), independent of the currency in which the original pricing information is stored. This is necessary for operations such as data sorting, price comparison, etc. Note that while the *overview* information is calculated in a single currency, the original pricing information is still available in the original currency.
Each price range is calculated in the [Default Currency](../concepts/pricing.md#default-currency), independent of the currency in which the original pricing information is stored. This is necessary for operations such as data sorting, price comparison, etc. Note that while the *overview* information is calculated in a single currency, the original pricing information is still available in the original currency.

Price range data is [cached in the database](#price-data-caching) when underlying pricing information changes.

Expand Down Expand Up @@ -167,7 +134,7 @@ Pricing calculations (and conversions) can be expensive to perform. This can mak

For this reason, all information displayed in the [pricing overview](#pricing-overview) section is pre-calculated and *cached* in the database. This ensures that when it needs to be retrieved (e.g. viewing pricing for an entire BOM) it can be accessed immediately.

Pricing data is cached in the [default currency](#default-currency), which ensures that pricing can be compared across multiple parts in a consistent format.
Pricing data is cached in the [default currency](../concepts/pricing.md/#default-currency), which ensures that pricing can be compared across multiple parts in a consistent format.

#### Data Updates

Expand Down
31 changes: 0 additions & 31 deletions docs/docs/settings/currency.md

This file was deleted.

11 changes: 9 additions & 2 deletions docs/docs/settings/global.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,18 @@ Configuration of pricing data and currency support:
| ---- | ----------- | ------- | ----- |
{{ globalsetting("INVENTREE_DEFAULT_CURRENCY") }}
{{ globalsetting("CURRENCY_CODES") }}
{{ globalsetting("PART_INTERNAL_PRICE") }}
{{ globalsetting("PART_BOM_USE_INTERNAL_PRICE") }}
{{ globalsetting("PRICING_DECIMAL_PLACES_MIN") }}
{{ globalsetting("PRICING_DECIMAL_PLACES") }}
{{ globalsetting("PRICING_UPDATE_DAYS") }}

#### Part Pricing

Configuration of part pricing:

| Name | Description | Default | Units |
| ---- | ----------- | ------- | ----- |
{{ globalsetting("PART_INTERNAL_PRICE") }}
{{ globalsetting("PART_BOM_USE_INTERNAL_PRICE") }}
{{ globalsetting("PRICING_USE_SUPPLIER_PRICING") }}
{{ globalsetting("PRICING_PURCHASE_HISTORY_OVERRIDES_SUPPLIER") }}
{{ globalsetting("PRICING_USE_STOCK_PRICING") }}
Expand Down
2 changes: 1 addition & 1 deletion docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ nav:
- Terminology: concepts/terminology.md
- Physical Units: concepts/units.md
- Custom States: concepts/custom_states.md
- Pricing: concepts/pricing.md
- Development:
- Contributing: develop/contributing.md
- Devcontainer: develop/devcontainer.md
Expand Down Expand Up @@ -157,7 +158,6 @@ nav:
- Single Sign on: settings/SSO.md
- Multi Factor Authentication: settings/MFA.md
- Email: settings/email.md
- Currency Support: settings/currency.md
- Export Data: settings/export.md
- Import Data: settings/import.md
- Operations:
Expand Down
15 changes: 0 additions & 15 deletions src/backend/InvenTree/common/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import math
from typing import Optional

from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

Expand All @@ -20,14 +19,6 @@ def currency_code_default():
"""Returns the default currency code (or USD if not specified)."""
from common.settings import get_global_setting

try:
cached_value = cache.get('currency_code_default', '')
except Exception:
cached_value = None

if cached_value:
return cached_value

try:
code = get_global_setting('INVENTREE_DEFAULT_CURRENCY', create=True, cache=True)
except Exception: # pragma: no cover
Expand All @@ -37,12 +28,6 @@ def currency_code_default():
if code not in CURRENCIES:
code = 'USD' # pragma: no cover

# Cache the value for a short amount of time
try:
cache.set('currency_code_default', code, 30)
except Exception:
pass

return code


Expand Down
6 changes: 6 additions & 0 deletions src/backend/InvenTree/common/setting/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,12 @@ def __call__(self, value):
'default': False,
'validator': bool,
},
'PURCHASEORDER_CONVERT_CURRENCY': {
'name': _('Convert Currency'),
'description': _('Convert item value to base currency when receiving stock'),
'default': False,
'validator': bool,
},
'PURCHASEORDER_AUTO_COMPLETE': {
'name': _('Auto Complete Purchase Orders'),
'description': _(
Expand Down
12 changes: 12 additions & 0 deletions src/backend/InvenTree/order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,19 @@ def receive_line_item(
# Calculate unit purchase price (in base units)
if line.purchase_price:
unit_purchase_price = line.purchase_price

# Convert purchase price to base units
unit_purchase_price /= line.part.base_quantity(1)

# Convert to base currency
if get_global_setting('PURCHASEORDER_CONVERT_CURRENCY'):
try:
unit_purchase_price = convert_money(
unit_purchase_price, currency_code_default()
)
except Exception:
log_error('PurchaseOrder.receive_line_item')

else:
unit_purchase_price = None

Expand Down
Loading
Loading