@@ -1662,7 +1662,7 @@ class Critic::OrderAmendmentTranslation < Critic::OrderAmendmentFunctionalTest
1662
1662
# create the initial sf order
1663
1663
sf_order = create_subscription_order (
1664
1664
sf_product_id : sf_product_id ,
1665
- contact_email : "syncs_three_stacked_diffruns " ,
1665
+ contact_email : "syncs_three_stacked_diffruns_3 " ,
1666
1666
additional_fields : {
1667
1667
CPQ_QUOTE_SUBSCRIPTION_START_DATE => format_date_for_salesforce ( initial_order_start_date ) ,
1668
1668
CPQ_QUOTE_BILLING_FREQUENCY => CPQBillingFrequencyOptions ::ANNUAL . serialize ,
@@ -1767,6 +1767,103 @@ class Critic::OrderAmendmentTranslation < Critic::OrderAmendmentFunctionalTest
1767
1767
assert_equal ( -1 * ( BigDecimal ( TEST_DEFAULT_PRICE ) * BigDecimal ( amendment_3_term ) / BigDecimal ( contract_term ) ) . round ( MAX_STRIPE_PRICE_PRECISION ) , BigDecimal ( credit_stripe_price . unit_amount_decimal ) )
1768
1768
end
1769
1769
1770
+ it 'syncs stacked backdated amendments and ensure no duplicate invoice items' do
1771
+ @user . disable_feature ( FeatureFlags ::SF_CACHING )
1772
+ # initial order: 1yr contract, billed monthly, started 3 months ago
1773
+ # amendment 1: started 2 months ago
1774
+ # amendment 2: started 1 month ago
1775
+ contract_term = TEST_DEFAULT_CONTRACT_TERM
1776
+ initial_order_start_date = now_time - 3 . months - 3 . days
1777
+ initial_order_end_date = initial_order_start_date + contract_term . months
1778
+
1779
+ amendment_1_term = 11
1780
+ amendment_1_start_date = initial_order_start_date + ( contract_term - amendment_1_term ) . months
1781
+
1782
+ amendment_2_term = 10
1783
+ amendment_2_start_date = initial_order_start_date + ( contract_term - amendment_2_term ) . months
1784
+
1785
+
1786
+ sf_product_id , _sf_pricebook_id = salesforce_recurring_product_with_price (
1787
+ additional_product_fields : {
1788
+ CPQ_QUOTE_BILLING_FREQUENCY => CPQBillingFrequencyOptions ::MONTHLY . serialize ,
1789
+ }
1790
+ )
1791
+
1792
+ # create the initial sf order
1793
+ sf_order = create_subscription_order (
1794
+ sf_product_id : sf_product_id ,
1795
+ contact_email : "syncs_three_stacked_diffruns" ,
1796
+ additional_fields : {
1797
+ CPQ_QUOTE_SUBSCRIPTION_START_DATE => format_date_for_salesforce ( initial_order_start_date ) ,
1798
+ CPQ_QUOTE_BILLING_FREQUENCY => CPQBillingFrequencyOptions ::MONTHLY . serialize ,
1799
+ CPQ_QUOTE_SUBSCRIPTION_TERM => contract_term ,
1800
+ }
1801
+ )
1802
+
1803
+ # create the first amendment to increase quantity (+2)
1804
+ sf_contract_1 = create_contract_from_order ( sf_order )
1805
+ amendment_quote = create_quote_data_from_contract_amendment ( sf_contract_1 )
1806
+ amendment_quote [ "lineItems" ] . first [ "record" ] [ CPQ_QUOTE_QUANTITY ] = 3
1807
+ amendment_quote [ "record" ] [ CPQ_QUOTE_SUBSCRIPTION_START_DATE ] = format_date_for_salesforce ( amendment_1_start_date )
1808
+ amendment_quote [ "record" ] [ CPQ_QUOTE_SUBSCRIPTION_TERM ] = amendment_1_term
1809
+ sf_order_amendment_1 = create_order_from_quote_data ( amendment_quote )
1810
+
1811
+ # translate the orders (initial order and first amendment)
1812
+ StripeForce ::Translate . perform_inline ( @user , sf_order . Id )
1813
+ sf_order . refresh
1814
+ stripe_id = sf_order [ prefixed_stripe_field ( GENERIC_STRIPE_ID ) ]
1815
+
1816
+ subscription_schedule = Stripe ::SubscriptionSchedule . retrieve ( stripe_id , @user . stripe_credentials )
1817
+ first_phase = T . must ( subscription_schedule . phases . first )
1818
+ second_phase = T . must ( subscription_schedule . phases . second )
1819
+
1820
+ # first phase should start at the backdated date
1821
+ assert_equal ( 0 , first_phase . start_date - initial_order_start_date . to_i )
1822
+ assert_equal ( 0 , first_phase . end_date - second_phase . start_date )
1823
+ # first phase should have an item with a quantity of 1 and no invoice items
1824
+ assert_equal ( 1 , first_phase . items . count )
1825
+ first_phase_item = T . must ( first_phase . items . first )
1826
+ assert_equal ( 1 , first_phase_item . quantity )
1827
+ assert_empty ( first_phase . add_invoice_items )
1828
+
1829
+ # second phase should start 'now' (since it was a backdated amendment)
1830
+ # and have two products with total quantity of 2
1831
+ assert ( second_phase . start_date . to_i - now_time . to_i < SECONDS_IN_DAY )
1832
+ assert_equal ( initial_order_end_date . to_i , second_phase . end_date . to_i )
1833
+ # second phase should have an item with a quantity of 3
1834
+ assert_equal ( 2 , second_phase . items . count )
1835
+ second_phase_item_1 = T . must ( second_phase . items . first )
1836
+ second_phase_item_2 = T . must ( second_phase . items . second )
1837
+ assert_equal ( 1 , second_phase_item_1 . quantity )
1838
+ assert_equal ( 2 , second_phase_item_2 . quantity )
1839
+
1840
+ # prorate the added items added since the amendment was backdated and missed a billing cycle
1841
+ assert_equal ( 1 , second_phase . add_invoice_items . count )
1842
+ prorated_item = T . unsafe ( second_phase . add_invoice_items . first )
1843
+ assert_equal ( 2 , prorated_item . quantity )
1844
+
1845
+ invoice_items_list = Stripe ::InvoiceItem . list ( { customer : subscription_schedule . customer , created : { gt : subscription_schedule . created } } , @user . stripe_credentials )
1846
+ assert_equal ( 1 , invoice_items_list . count )
1847
+
1848
+ # create the second amendment to terminate the order
1849
+ sf_contract_2 = create_contract_from_order ( sf_order_amendment_1 )
1850
+ amendment_quote = create_quote_data_from_contract_amendment ( sf_contract_2 )
1851
+ amendment_quote [ "lineItems" ] . first [ "record" ] [ CPQ_QUOTE_QUANTITY ] = 0
1852
+ amendment_quote [ "record" ] [ CPQ_QUOTE_SUBSCRIPTION_START_DATE ] = format_date_for_salesforce ( amendment_2_start_date )
1853
+ amendment_quote [ "record" ] [ CPQ_QUOTE_SUBSCRIPTION_TERM ] = amendment_2_term
1854
+ sf_order_amendment_2 = create_order_from_quote_data ( amendment_quote )
1855
+
1856
+ StripeForce ::Translate . perform_inline ( @user , sf_order_amendment_2 . Id )
1857
+ sf_order . refresh
1858
+ stripe_id = sf_order [ prefixed_stripe_field ( GENERIC_STRIPE_ID ) ]
1859
+
1860
+ # fetch the latest subscription schedule
1861
+ subscription_schedule = Stripe ::SubscriptionSchedule . retrieve ( stripe_id , @user . stripe_credentials )
1862
+ assert_equal ( 2 , subscription_schedule . phases . count )
1863
+
1864
+ invoice_items_list = Stripe ::InvoiceItem . list ( { customer : subscription_schedule . customer , created : { gt : subscription_schedule . created } } , @user . stripe_credentials )
1865
+ assert_equal ( 1 , invoice_items_list . count )
1866
+ end
1770
1867
end
1771
1868
1772
1869
# describe 'metadata' do
0 commit comments