|
10 | 10 |
|
11 | 11 | from polar.auth.models import AuthSubject |
12 | 12 | from polar.config import Environment, settings |
13 | | -from polar.enums import AccountType |
| 13 | +from polar.enums import AccountType, InvoiceNumbering |
14 | 14 | from polar.exceptions import PolarRequestValidationError |
15 | | -from polar.models import Organization, Product, User |
| 15 | +from polar.models import Customer, Organization, Product, User |
16 | 16 | from polar.models.account import Account |
17 | 17 | from polar.models.organization import OrganizationNotificationSettings |
18 | 18 | from polar.models.organization_review import OrganizationReview |
@@ -163,19 +163,111 @@ async def test_valid_with_none_subscription_settings( |
163 | 163 |
|
164 | 164 |
|
165 | 165 | @pytest.mark.asyncio |
166 | | -async def test_get_next_invoice_number( |
| 166 | +async def test_get_next_invoice_number_organization( |
167 | 167 | session: AsyncSession, |
168 | 168 | organization: Organization, |
| 169 | + customer: Customer, |
169 | 170 | ) -> None: |
| 171 | + organization.order_settings = { |
| 172 | + **organization.order_settings, |
| 173 | + "invoice_numbering": InvoiceNumbering.organization, |
| 174 | + } |
170 | 175 | assert organization.customer_invoice_next_number == 1 |
171 | 176 |
|
172 | 177 | next_invoice_number = await organization_service.get_next_invoice_number( |
173 | | - session, organization |
| 178 | + session, organization, customer |
174 | 179 | ) |
175 | 180 |
|
176 | 181 | assert next_invoice_number == f"{organization.customer_invoice_prefix}-0001" |
177 | 182 | assert organization.customer_invoice_next_number == 2 |
178 | 183 |
|
| 184 | + await session.refresh(customer) |
| 185 | + assert customer.invoice_next_number == 1 |
| 186 | + |
| 187 | + |
| 188 | +@pytest.mark.asyncio |
| 189 | +async def test_get_next_invoice_number_customer( |
| 190 | + session: AsyncSession, |
| 191 | + save_fixture: SaveFixture, |
| 192 | + organization: Organization, |
| 193 | + customer: Customer, |
| 194 | +) -> None: |
| 195 | + await save_fixture(organization) |
| 196 | + |
| 197 | + initial_org_counter = organization.customer_invoice_next_number |
| 198 | + assert customer.invoice_next_number == 1 |
| 199 | + |
| 200 | + next_invoice_number = await organization_service.get_next_invoice_number( |
| 201 | + session, organization, customer |
| 202 | + ) |
| 203 | + |
| 204 | + customer_suffix = str(customer.id).split("-")[0].upper() |
| 205 | + assert ( |
| 206 | + next_invoice_number |
| 207 | + == f"{organization.customer_invoice_prefix}-{customer_suffix}-0001" |
| 208 | + ) |
| 209 | + await session.commit() |
| 210 | + await session.refresh(customer) |
| 211 | + assert customer.invoice_next_number == 2 |
| 212 | + |
| 213 | + await session.refresh(organization) |
| 214 | + assert organization.customer_invoice_next_number == initial_org_counter |
| 215 | + |
| 216 | + await session.refresh(customer) |
| 217 | + |
| 218 | + next_invoice_number = await organization_service.get_next_invoice_number( |
| 219 | + session, organization, customer |
| 220 | + ) |
| 221 | + |
| 222 | + assert ( |
| 223 | + next_invoice_number |
| 224 | + == f"{organization.customer_invoice_prefix}-{customer_suffix}-0002" |
| 225 | + ) |
| 226 | + await session.commit() |
| 227 | + await session.refresh(customer) |
| 228 | + assert customer.invoice_next_number == 3 |
| 229 | + |
| 230 | + |
| 231 | +@pytest.mark.asyncio |
| 232 | +async def test_get_next_invoice_number_multiple_customers( |
| 233 | + session: AsyncSession, |
| 234 | + save_fixture: SaveFixture, |
| 235 | + organization: Organization, |
| 236 | + customer: Customer, |
| 237 | +) -> None: |
| 238 | + await save_fixture(organization) |
| 239 | + |
| 240 | + customer2 = Customer( |
| 241 | + |
| 242 | + organization=organization, |
| 243 | + ) |
| 244 | + session.add(customer2) |
| 245 | + |
| 246 | + invoice1 = await organization_service.get_next_invoice_number( |
| 247 | + session, organization, customer |
| 248 | + ) |
| 249 | + customer_suffix = str(customer.id).split("-")[0].upper() |
| 250 | + await session.commit() |
| 251 | + assert invoice1 == f"{organization.customer_invoice_prefix}-{customer_suffix}-0001" |
| 252 | + |
| 253 | + invoice2 = await organization_service.get_next_invoice_number( |
| 254 | + session, organization, customer2 |
| 255 | + ) |
| 256 | + customer2_suffix = str(customer2.id).split("-")[0].upper() |
| 257 | + await session.commit() |
| 258 | + assert invoice2 == f"{organization.customer_invoice_prefix}-{customer2_suffix}-0001" |
| 259 | + |
| 260 | + invoice3 = await organization_service.get_next_invoice_number( |
| 261 | + session, organization, customer |
| 262 | + ) |
| 263 | + await session.commit() |
| 264 | + assert invoice3 == f"{organization.customer_invoice_prefix}-{customer_suffix}-0002" |
| 265 | + |
| 266 | + await session.refresh(customer) |
| 267 | + await session.refresh(customer2) |
| 268 | + assert customer.invoice_next_number == 3 |
| 269 | + assert customer2.invoice_next_number == 2 |
| 270 | + |
179 | 271 |
|
180 | 272 | @pytest.mark.asyncio |
181 | 273 | class TestCheckReviewThreshold: |
|
0 commit comments