Selling Price based on Markup and Buying Price

Hi all, I am trying to set Selling Price Based on Markup and Buying Price,
I made 3 Custom field Buying Price, Markup, Selling Rate under DocType Item Price
and client script to do calculation and made these fields to display when the price list is for Selling only.
Its doing the job but not exactly how I want.
When Markup change it update the selling Rate, but when Buying Price change it wont update Selling Rate if Markup dont get altered.
Also I want Buying price to be updated while capturing the Purchase Invoice if Buying Price Changed.

eval:doc.selling == 1

frappe.ui.form.on(‘Item Price’, {
refresh(frm) {

cur_frm.cscript.markup = function(doc, cdt, cdn) {

doc.selling_rate = (flt(doc.buying_price)*(flt(doc.markup)/100))+flt(doc.buying_price);



cur_frm.cscript.buying_price = cur_frm.cscript.buying_price;

cur_frm.cscript.price_list_rate = cur_frm.cscript.price_list_rate;