I don't know perl very well, but as I understand, the cdr_period_costs insert statement is running in the case of outbound direction.