Skip to content

Instantly share code, notes, and snippets.

@cesswairimu
Last active December 3, 2020 20:07
Show Gist options
  • Save cesswairimu/dd4774a910e0ca09c1f1fd6b64f8a004 to your computer and use it in GitHub Desktop.
Save cesswairimu/dd4774a910e0ca09c1f1fd6b64f8a004 to your computer and use it in GitHub Desktop.
with new_table as (
select crt.id,
crt.gross_amount/((100+tax.tax_default_percentage)/100) as net_amount,
crt.gross_amount - (crt.gross_amount/((100+tax.tax_default_percentage)/100)) as tax_amount,
tax.tax_default_percentage as tax_rate
from customer_return_items crt
inner join customer_returns cr on cr.id = crt.customer_return_id
inner join warehouses w on w.id = cr.warehouse_id
inner join organization_regions org on org.id = w.organization_region_id
inner join organization_cities oc on oc.id = org.organization_city_id
inner join organization_countries oct on oct.id = oc.organization_country_id
inner join products p on p.id = crt.product_id
inner join tax_maps tm on tm.id = p.tax_map_id
inner join tax_map_mappings tmm on tmm.tax_map_id = tm.id
inner join taxes tax on tax.id = tmm.tax_id
where tmm.organization_country_id = oct.id
and crt.created_at > '2020-11-21'::DATE
)
update customer_return_items pp
set net_amount = new_table.net_amount,
tax_amount = new_table.tax_amount,
tax_rate = new_table.tax_rate
from new_table
where pp.id = new_table.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment