This line is flawed. The matched line items are for only for the items that have this tax_category, but then you are using the *total* order promo_total and ship_total which could include items that are not a part of this tax_category.