@@ -442,6 +442,98 @@ def expect_to_bill_together(subscriptions, date)
442442 end
443443 end
444444
445+ context "when grouping subscriptions by purchase order number" do
446+ let ( :interval ) { :monthly }
447+ let ( :billing_time ) { :anniversary }
448+ let ( :current_date ) { subscription_at . next_month }
449+
450+ before { subscription . destroy }
451+
452+ context "when subscriptions have different purchase order numbers" do
453+ let ( :po1_subscription ) do
454+ create (
455+ :subscription ,
456+ customer :,
457+ plan :,
458+ subscription_at :,
459+ started_at : current_date - 10 . days ,
460+ billing_time :,
461+ created_at :,
462+ purchase_order_number : "PO-1"
463+ )
464+ end
465+ let ( :po2_subscription ) do
466+ create (
467+ :subscription ,
468+ customer :,
469+ plan :,
470+ subscription_at :,
471+ started_at : current_date - 10 . days ,
472+ billing_time :,
473+ created_at :,
474+ purchase_order_number : "PO-2"
475+ )
476+ end
477+
478+ before do
479+ po1_subscription
480+ po2_subscription
481+ end
482+
483+ it "produces separate billing jobs per purchase order number" do
484+ billing_service . call
485+
486+ expect ( BillSubscriptionJob ) . to have_been_enqueued
487+ . with ( [ po1_subscription ] , current_date . to_i , invoicing_reason : :subscription_periodic )
488+ expect ( BillSubscriptionJob ) . to have_been_enqueued
489+ . with ( [ po2_subscription ] , current_date . to_i , invoicing_reason : :subscription_periodic )
490+ end
491+ end
492+
493+ context "when subscriptions share the same purchase order number" do
494+ let ( :subscription1 ) do
495+ create (
496+ :subscription ,
497+ customer :,
498+ plan :,
499+ subscription_at :,
500+ started_at : current_date - 10 . days ,
501+ billing_time :,
502+ created_at :,
503+ purchase_order_number : "PO-1"
504+ )
505+ end
506+ let ( :subscription2 ) do
507+ create (
508+ :subscription ,
509+ customer :,
510+ plan :,
511+ subscription_at :,
512+ started_at : current_date - 10 . days ,
513+ billing_time :,
514+ created_at :,
515+ purchase_order_number : "PO-1"
516+ )
517+ end
518+
519+ before do
520+ subscription1
521+ subscription2
522+ end
523+
524+ it "groups them into a single billing job" do
525+ billing_service . call
526+
527+ expect ( BillSubscriptionJob ) . to have_been_enqueued
528+ . with (
529+ contain_exactly ( subscription1 , subscription2 ) ,
530+ current_date . to_i ,
531+ invoicing_reason : :subscription_periodic
532+ )
533+ end
534+ end
535+ end
536+
445537 context "when grouping subscriptions by currency" do
446538 let ( :organization ) { create ( :organization , feature_flags : [ "multi_currency" ] ) }
447539 let ( :interval ) { :monthly }
0 commit comments