Wrong rate for item on Quotation, Sales invoice, Sales Order

When you change the item in the sales doctype, the system considers the old sales amounts from the previous item, and puts the difference in the amount in the discount.

WARRING
If u inser a item with $8 of sales price and trade for a item with $10, the item of $10 will get $2 for sales price…

Example

Item A = $ 10

Item B = $2

How to reproduce
gif_wrong_rate

gif_wrong_rate

a Gif how to reproduce:

https://gifyu.com/image/eD8O

I solved it as follows:

Informing zero value for discount:

Can anyone confirm if this code is correct?