# Not return last currency purchase

**URL:** <https://discuss.tryton.org/t/not-return-last-currency-purchase/5552>\
**Category:** Developer\
**Tags:** purchasing\
**Created:** [September 21, 2022, 11:17am UTC](https://discuss.tryton.org/t/not-return-last-currency-purchase/5552 "2022-09-21T11:17:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://discuss.tryton.org/u/raimon)\
**Post date:** [September 21, 2022, 11:17am UTC](https://discuss.tryton.org/t/not-return-last-currency-purchase/5552/1 "2022-09-21T11:17:53Z")

</div>

Last issue9855 that return invoice method and payment term from last purchase, I see that not return last currency

1. Purchases and currencies in our database:

```auto
select id, create_date, currency from purchase_purchase where party = 411 order by id desc;
 id | create_date | currency 
----+----------------------------+----------
 14 | 2022-09-21 10:51:57.055779 | 143
 13 | 2022-09-21 10:43:48.525552 | 143
 12 | 2022-09-21 10:40:17.945997 | 143
  8 | 2022-09-19 09:39:58.972665 | 44
  7 | 2022-09-19 09:28:06.622479 | 44
  6 | 2022-09-19 08:43:21.719179 | 44
  3 | 2022-09-19 08:15:50.225536 | 44

```

When on\_change\_party() and get last purchase [1], I get currency ID 44. Not ID 143 that is last currency in our purchases.

Debug:

```auto
(Pdb) cursor.execute(*subquery.select(subquery.currency,subquery.payment_term,subquery.invoice_method,group_by=[subquery.currency,subquery.payment_term,subquery.invoice_method,],order_by=Count(Literal(1)).desc))
(Pdb) row = cursor.fetchone()
(Pdb) row
(44, 8, 'shipment')

```

Why return ID 44 when last purchase, the currency is ID 143?

**note:**

All results from query:

```auto
SELECT a.currency, a.payment_term, a.invoice_method FROM (SELECT b.currency, b.payment_term, b.invoice_method FROM purchase_purchase AS b WHERE ((b.party = 411) AND (b.invoice_party IS NULL)) ORDER BY b.id LIMIT 10) AS a GROUP BY a.currency, a.payment_term, a.invoice_method ORDER BY COUNT(1) DESC;
 currency | payment_term | invoice_method 
----------+--------------+----------------
       44 | 8 | shipment
      143 | 8 | shipment

```

[1] [purchase/purchase.py at 06149a1cd43f301940af3b795bc52610a056eb0b · tryton/purchase · GitHub](https://github.com/tryton/purchase/blob/06149a1cd43f301940af3b795bc52610a056eb0b/purchase.py#L445)

---

<div class="post-metadata">

**Author:** ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)\
**Post date:** [September 21, 2022, 11:42am UTC](https://discuss.tryton.org/t/not-return-last-currency-purchase/5552/2 "2022-09-21T11:42:43Z")

</div>

Currency is taken from the most used currency in the last 10 purchases: [https://hg.tryton.org/modules/purchase/file/tip/purchase.py#l427](https://hg.tryton.org/modules/purchase/file/tip/purchase.py#l427)

So in your case, currency 44 is most used than currency 143.

---

<div class="post-metadata">

**Author:** ![raimon](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://discuss.tryton.org/u/raimon)\
**Post date:** [September 21, 2022, 12:12pm UTC](https://discuss.tryton.org/t/not-return-last-currency-purchase/5552/3 "2022-09-21T12:12:07Z")

</div>

Thanks maxx.

Last purchases != last purchase 🙂

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [October 21, 2022, 12:12pm UTC](https://discuss.tryton.org/t/not-return-last-currency-purchase/5552/4 "2022-10-21T12:12:56Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
