billing_type TEXT,
payment_ou INT,
billing_ou INT,
--- Debug billing_leftover NUMERIC(8,2),
--- Debug payment_leftover NUMERIC(8,2),
--- Debug used_billings BIGINT[],
--- Debug billing_l INT,
CONSTRAINT x_p_b_once UNIQUE (xact,payment,billing)
);
SELECT circ_lib INTO billing_ou FROM action.circulation WHERE id = p_xact;
END IF;
- -- figure out how many billing rows there are
- FOR current_billing IN
- SELECT *
- FROM money.billing
- WHERE xact = p_xact
- -- AND NOT voided
- ORDER BY billing_ts
- LOOP
- billing_row_count = billing_row_count + 1;
- END LOOP;
+ SELECT count(id) INTO billing_row_count FROM money.billing WHERE xact = p_xact;
+
-- Loop through the positive payments
FOR current_payment IN
SELECT *
JOIN actor.workstation ws ON (p.cash_drawer = ws.id)
WHERE p.id = current_payment.id;
ELSE
- payment_ou = billing_ou;
+ SELECT home_ou INTO payment_ou FROM actor.usr WHERE id = (SELECT accepting_usr FROM money.bnm_payment WHERE id = current_payment.id);
END IF;
-- Were we looking at a billing from a previous step in the loop?
current_result.billing_type = current_billing.billing_type;
current_result.payment_ou = payment_ou;
current_result.billing_ou = billing_ou;
--- Debug current_result.billing_leftover = billing_remainder;
--- Debug current_result.payment_leftover = payment_remainder;
--- Debug current_result.used_billings = billing_id_used;
--- Debug current_result.billing_l = billing_loop;
IF billing_remainder >= payment_remainder THEN
current_result.amount = payment_remainder;
current_result.billing_type = current_billing.billing_type;
current_result.payment_ou = payment_ou;
current_result.billing_ou = billing_ou;
--- Debug current_result.billing_leftover = billing_remainder;
--- Debug current_result.payment_leftover = payment_remainder;
--- Debug current_result.used_billings = billing_id_used;
--- Debug current_result.billing_l = billing_loop;
IF billing_remainder >= payment_remainder THEN
current_result.amount = payment_remainder;
current_result.billing_type = current_billing.billing_type;
current_result.payment_ou = payment_ou;
current_result.billing_ou = billing_ou;
--- Debug current_result.billing_leftover = billing_remainder;
--- Debug current_result.payment_leftover = payment_remainder;
--- Debug current_result.used_billings = billing_id_used;
--- Debug current_result.billing_l = billing_loop;
IF billing_remainder >= payment_remainder THEN
current_result.amount = payment_remainder;
billing_row_count := 0;
billing_loop := -1;
--- figure out how many voided billing rows there are
- FOR current_billing IN
- SELECT *
- FROM money.billing
- WHERE xact = p_xact
- AND voided
- ORDER BY billing_ts
- LOOP
- billing_row_count = billing_row_count + 1;
- END LOOP;
+ -- figure out how many voided billing rows there are
+ SELECT count(id) INTO billing_row_count FROM money.billing WHERE xact = p_xact AND voided;
-- Loop through the negative payments, these are refunds on voided billings
FOR current_payment IN
JOIN actor.workstation ws ON (p.cash_drawer = ws.id)
WHERE p.id = current_payment.id;
ELSE
- payment_ou = billing_ou;
+ SELECT home_ou INTO payment_ou FROM actor.usr WHERE id = (SELECT accepting_usr FROM money.bnm_payment WHERE id = current_payment.id);
END IF;
payment_remainder = -current_payment.amount; -- invert
current_result.billing_type = current_billing.billing_type;
current_result.payment_ou = payment_ou;
current_result.billing_ou = billing_ou;
--- Debug current_result.billing_leftover = billing_remainder;
--- Debug current_result.payment_leftover = payment_remainder;
--- Debug current_result.billing_l = billing_loop;
IF billing_remainder >= payment_remainder THEN
current_result.amount = payment_remainder;
current_result.billing_type = current_billing.billing_type;
current_result.payment_ou = payment_ou;
current_result.billing_ou = billing_ou;
--- Debug current_result.billing_leftover = billing_remainder;
--- Debug current_result.payment_leftover = payment_remainder;
--- Debug current_result.billing_l = billing_loop;
IF billing_remainder >= payment_remainder THEN
current_result.amount = payment_remainder;