def test_move_lines(self):
        'Test move lines'
        pool = Pool()
        Account = pool.get('account.account')
        FiscalYear = pool.get('account.fiscalyear')
        Journal = pool.get('account.journal')
        Move = pool.get('account.move')
        MoveLine = pool.get('account.move.line')
        PaymentType = pool.get('account.payment.type')

        company = create_company()
        with set_company(company):
            create_chart(company)
            fiscalyear = get_fiscalyear(company)
            set_invoice_sequences(fiscalyear)
            fiscalyear.save()
            FiscalYear.create_period([fiscalyear])
            period = fiscalyear.periods[0]

            journal_revenue, = Journal.search([
                    ('code', '=', 'REV'),
                    ])
            revenue, = Account.search([
                    ('type.revenue', '=', True),
                    ])
            receivable, = Account.search([
                    ('type.receivable', '=', True),
                    ])
            payable, = Account.search([
                    ('type.payable', '=', True),
                    ])
            payment_payable, = PaymentType.create([{
                    'name': 'Payment Payable',
                    'kind': 'payable',
                    'company': company.id,
                    }])
            payment_receivable, = PaymentType.create([{
                    'name': 'Payment Receivable',
                    'kind': 'receivable',
                    'company': company.id,
                    }])
            move, = Move.create([{
                    'period': period.id,
                    'journal': journal_revenue.id,
                    'date': period.start_date,
                    }])
            MoveLine.create([{
                    'move': move.id,
                    'account': revenue.id,
                    'debit': Decimal(30),
                    }])
            self.assertRaises(Exception, MoveLine.create, [{
                    'move': move.id,
                    'account': revenue.id,
                    'debit': Decimal(30),
                    'payment_type': payment_receivable,
                    }])
Example #2
0
def create_fiscalyear(company):
    pool = Pool()
    FiscalYear = pool.get('account.fiscalyear')
    InvoiceSequence = pool.get(
        'account.fiscalyear.invoice_sequence')

    invoice_seq = create_sequence(
        'Invoice Sequence', 'account.invoice', company.id, True)

    seq = InvoiceSequence()
    seq.company = company
    seq.out_invoice_sequence = invoice_seq
    seq.out_credit_note_sequence = invoice_seq
    seq.in_invoice_sequence = invoice_seq
    seq.in_credit_note_sequence = invoice_seq

    fy = get_fiscalyear(company)
    fy.invoice_sequences = [seq]
    fy.save()
    FiscalYear.create_period([fy])
    def test_check_credit_limit(self):
        'Test check_credit_limit'
        pool = Pool()
        Account = pool.get('account.account')
        Move = pool.get('account.move')
        Journal = pool.get('account.journal')
        Party = pool.get('party.party')

        company = create_company()
        with set_company(company):
            create_chart(company)
            fiscalyear = get_fiscalyear(company)
            fiscalyear.save()
            fiscalyear.create_period([fiscalyear])
            period = fiscalyear.periods[0]

            receivable, = Account.search([
                    ('kind', '=', 'receivable'),
                    ])
            revenue, = Account.search([
                    ('kind', '=', 'revenue'),
                    ])
            journal, = Journal.search([], limit=1)
            party, = Party.create([{
                        'name': 'Party',
                        }])
            Move.create([{
                        'journal': journal.id,
                        'period': period.id,
                        'date': period.start_date,
                        'lines': [
                            ('create', [{
                                        'debit': Decimal(100),
                                        'account': receivable.id,
                                        'party': party.id,
                                        }, {
                                        'credit': Decimal(100),
                                        'account': revenue.id,
                                        }]),
                            ],
                        }])
            self.assertEqual(party.credit_amount, Decimal(100))
            self.assertEqual(party.credit_limit_amount, None)
            party.check_credit_limit(Decimal(0))
            party.check_credit_limit(Decimal(0), 'test')
            party.check_credit_limit(Decimal(100))
            party.check_credit_limit(Decimal(100), 'test')
            party.credit_limit_amount = Decimal(0)
            party.save()
            self.assertRaises(UserError, party.check_credit_limit,
                Decimal(0))
            self.assertRaises(UserWarning, party.check_credit_limit,
                Decimal(0), 'test')
            party.credit_limit_amount = Decimal(200)
            party.save()
            party.check_credit_limit(Decimal(0))
            party.check_credit_limit(Decimal(0), 'test')
            self.assertRaises(UserError, party.check_credit_limit,
                Decimal(150))
            self.assertRaises(UserWarning, party.check_credit_limit,
                Decimal(150), 'test')
    def test_invoice_generation(self):
        'Test invoice generation'

        pool = Pool()
        Account = pool.get('account.account')
        FiscalYear = pool.get('account.fiscalyear')
        Invoice = pool.get('account.invoice')
        InvoiceLine = pool.get('account.invoice.line')
        Party = pool.get('party.party')
        PaymentTerm = pool.get('account.invoice.payment_term')
        ProductUom = pool.get('product.uom')
        ProductCategory = pool.get('product.category')
        ProductTemplate = pool.get('product.template')
        Product = pool.get('product.product')
        Tax = pool.get('account.tax')
        Address = pool.get('party.address')
        PartyIdentifier = pool.get('party.identifier')
        Country = pool.get('country.country')
        Subdivision = pool.get('country.subdivision')
        PaymentType = pool.get('account.payment.type')

        country = Country(name='Country', code='ES', code3='ESP')
        country.save()
        subdivision = Subdivision(name='Subdivision',
                                  country=country,
                                  code='SUB',
                                  type='province')
        subdivision.save()

        company = create_company()
        currency = create_currency('EUR')
        add_currency_rate(currency, 1.0)

        tax_identifier = PartyIdentifier()
        tax_identifier.type = 'eu_vat'
        tax_identifier.code = 'BE0897290877'
        company.header = 'Report Header'
        company.party.name = 'Seller'
        company.party.identifiers = [tax_identifier]
        company.party.facturae_person_type = 'J'
        company.party.facturae_residence_type = 'R'
        company.party.save()
        company.save()

        # Save certificate into company
        with open(os.path.join(CURRENT_PATH, 'certificate.pfx'),
                  'rb') as cert_file:
            company.facturae_certificate = cert_file.read()

        payment_term, = PaymentTerm.create([{
            'name':
            '20 days, 40 days',
            'lines': [('create', [{
                'sequence':
                0,
                'type':
                'percent',
                'divisor':
                2,
                'ratio':
                Decimal('.5'),
                'relativedeltas': [
                    ('create', [
                        {
                            'days': 20,
                        },
                    ]),
                ],
            }, {
                'sequence':
                1,
                'type':
                'remainder',
                'relativedeltas': [
                    ('create', [
                        {
                            'days': 40,
                        },
                    ]),
                ],
            }])]
        }])

        with set_company(company):
            create_chart(company, tax=True)

            fiscalyear = set_invoice_sequences(get_fiscalyear(company))
            fiscalyear.save()
            FiscalYear.create_period([fiscalyear])

            payment_receivable, = PaymentType.create([{
                'name': 'Payment Receivable',
                'kind': 'receivable',
                'company': company.id,
                'facturae_type': '01',
            }])
            revenue, = Account.search([('type.revenue', '=', True)])
            expense, = Account.search([('type.expense', '=', True)])
            tax_account, = Account.search([
                ('name', '=', 'Main Tax'),
            ])
            with Transaction().set_user(0):
                vat21 = Tax()
                vat21.name = vat21.description = '21% VAT'
                vat21.type = 'percentage'
                vat21.rate = Decimal('0.21')
                vat21.invoice_account = tax_account
                vat21.report_type = '05'
                vat21.credit_note_account = tax_account

            vat21.save()

            company_address, = company.party.addresses
            company_address.street = 'street'
            company_address.zip = '08201'
            company_address.city = 'City'
            company_address.subdivision = subdivision
            company_address.country = country
            company_address.save()
            party = Party(name='Buyer')
            party.facturae_person_type = 'J'
            party.facturae_residence_type = 'R'
            tax_identifier = PartyIdentifier()
            tax_identifier.type = 'eu_vat'
            tax_identifier.code = 'BE0897290877'
            party.identifiers = [tax_identifier]
            party.save()

            address_dict = {
                'party': party.id,
                'street': 'St sample, 15',
                'city': 'City',
                'zip': '08201',
                'subdivision': subdivision.id,
                'country': country.id,
            }

            address, = Address.create([address_dict])

            term, = PaymentTerm.create([{
                'name':
                'Payment term',
                'lines': [('create', [{
                    'type':
                    'remainder',
                    'relativedeltas': [('create', [{
                        'sequence': 0,
                        'days': 0,
                        'months': 0,
                        'weeks': 0,
                    }])],
                }])],
            }])

            account_category = ProductCategory()
            account_category.name = 'Account Category'
            account_category.accounting = True
            account_category.account_expense = expense
            account_category.account_revenue = revenue
            account_category.customer_taxes = [vat21]
            account_category.save()

            unit, = ProductUom.search([('name', '=', 'Unit')])
            template = ProductTemplate()
            template.name = 'product'
            template.default_uom = unit
            template.type = 'service'
            template.list_price = Decimal('40')
            template.account_category = account_category
            template.save()
            product = Product()
            product.template = template
            product.save()

            currency = create_currency('Eur')
            add_currency_rate(currency, 1)

            with Transaction().set_user(0):
                invoice = Invoice()
                invoice.type = 'out'
                invoice.on_change_type()
                invoice.party = party
                invoice.on_change_party()
                invoice.payment_type = payment_receivable
                invoice.payment_term = term
                invoice.currency = currency
                invoice.company = company
                invoice.payment_term = invoice.on_change_with_payment_term()
                invoice.account = invoice.on_change_with_account()

                line1 = InvoiceLine()
                line1.product = product
                line1.on_change_product()
                line1.on_change_account()
                line1.quantity = 5
                line1.unit_price = Decimal('40')

                line2 = InvoiceLine()
                line2.account = revenue
                line2.on_change_account()
                line2.product = product
                line2.on_change_product()
                line2.description = 'Test'
                line2.quantity = 1
                line2.unit_price = Decimal(20)

                invoice.lines = [line1, line2]
                invoice.on_change_lines()

                invoice.save()
                Invoice.post([invoice])

            Invoice.generate_facturae_default([invoice], 'privatepassword')
    def test_check_credit_limit(self):
        'Test check_credit_limit'
        pool = Pool()
        Account = pool.get('account.account')
        Move = pool.get('account.move')
        Journal = pool.get('account.journal')
        Party = pool.get('party.party')
        Sale = pool.get('sale.sale')
        PaymentTerm = pool.get('account.invoice.payment_term')
        Property = pool.get('ir.property')
        ModelField = pool.get('ir.model.field')
        FiscalYear = pool.get('account.fiscalyear')

        company = create_company()
        with set_company(company):
            create_chart(company)
            fiscalyear = set_invoice_sequences(get_fiscalyear(company))
            fiscalyear.save()
            FiscalYear.create_period([fiscalyear])
            period = fiscalyear.periods[0]

            receivable, = Account.search([
                ('kind', '=', 'receivable'),
            ])
            revenue, = Account.search([
                ('kind', '=', 'revenue'),
            ])
            journal, = Journal.search([], limit=1)
            party, = Party.create([{
                'name': 'Party',
                'addresses': [
                    ('create', [{}]),
                ],
                'credit_limit_amount': Decimal('100'),
            }])
            Move.create([{
                'journal':
                journal.id,
                'period':
                period.id,
                'date':
                period.start_date,
                'lines': [
                    ('create', [{
                        'debit': Decimal('100'),
                        'account': receivable.id,
                        'party': party.id,
                    }, {
                        'credit': Decimal('100'),
                        'account': revenue.id,
                    }]),
                ],
            }])
            payment_term, = PaymentTerm.create([{
                'name':
                'Test',
                'lines': [('create', [{
                    'type': 'remainder',
                }])],
            }])
            field, = ModelField.search([
                ('model.model', '=', 'product.template'),
                ('name', '=', 'account_revenue'),
            ],
                                       limit=1)
            Property.create([{
                'field': field.id,
                'value': str(revenue),
                'company': company.id,
            }])
            sale, = Sale.create([{
                'party':
                party.id,
                'company':
                company.id,
                'payment_term':
                payment_term.id,
                'currency':
                company.currency.id,
                'invoice_address':
                party.addresses[0].id,
                'shipment_address':
                party.addresses[0].id,
                'lines': [
                    ('create', [{
                        'description': 'Test',
                        'quantity': 1,
                        'unit_price': Decimal('50'),
                    }]),
                ],
            }])
            self.assertEqual(party.credit_amount, Decimal('100'))
            Sale.quote([sale])
            Sale.confirm([sale])
            self.assertEqual(party.credit_amount, Decimal('100'))
            # Test limit reaches
            self.assertRaises(UserWarning, Sale.process, [sale])
            # Increase limit
            party.credit_limit_amount = Decimal('200')
            party.save()
            # process should work
            Sale.process([sale])
            self.assertEqual(sale.state, 'processing')
            self.assertEqual(party.credit_amount, Decimal('150'))

            # Re-process
            Sale.process([sale])
            # Decrease limit
            party.credit_limit_amount = Decimal('100')
            party.save()
            # process should still work as sale is already processing
            Sale.process([sale])
Example #6
0
    def test_account_debit_credit(self):
        'Test account debit/credit'
        pool = Pool()
        Party = pool.get('party.party')
        AnalyticAccount = pool.get('analytic_account.account')
        Journal = pool.get('account.journal')
        Account = pool.get('account.account')
        Move = pool.get('account.move')
        transaction = Transaction()

        party = Party(name='Party')
        party.save()
        company = create_company()
        with set_company(company):
            root, = AnalyticAccount.create([{
                        'type': 'root',
                        'name': 'Root',
                        }])
            analytic_account, = AnalyticAccount.create([{
                        'type': 'normal',
                        'name': 'Analytic Account',
                        'parent': root.id,
                        'root': root.id,
                        }])
            create_chart(company)
            fiscalyear = get_fiscalyear(company)
            fiscalyear.save()
            fiscalyear.create_period([fiscalyear])
            period = fiscalyear.periods[0]
            journal_revenue, = Journal.search([
                    ('code', '=', 'REV'),
                    ])
            journal_expense, = Journal.search([
                    ('code', '=', 'EXP'),
                    ])
            revenue, = Account.search([
                    ('kind', '=', 'revenue'),
                    ])
            receivable, = Account.search([
                    ('kind', '=', 'receivable'),
                    ])
            expense, = Account.search([
                    ('kind', '=', 'expense'),
                    ])
            payable, = Account.search([
                    ('kind', '=', 'payable'),
                    ])

            first_account_line = {
                'account': revenue.id,
                'credit': Decimal(100),
                'analytic_lines': [
                    ('create', [{
                                'account': analytic_account.id,
                                'name': 'Analytic Line',
                                'credit': Decimal(100),
                                'debit': Decimal(0),
                                'journal': journal_revenue.id,
                                'date': period.start_date,
                                }])
                    ]}
            second_account_line = {
                'account': expense.id,
                'debit': Decimal(30),
                'analytic_lines': [
                    ('create', [{
                                'account': analytic_account.id,
                                'name': 'Analytic Line',
                                'debit': Decimal(30),
                                'credit': Decimal(0),
                                'journal': journal_expense.id,
                                'date': period.start_date,
                                }])
                    ]}
            # Create some moves
            vlist = [{
                    'period': period.id,
                    'journal': journal_revenue.id,
                    'date': period.start_date,
                    'lines': [
                        ('create', [first_account_line, {
                                    'account': receivable.id,
                                    'debit': Decimal(100),
                                    'party': party.id,
                                    }]),
                        ],
                    }, {
                    'period': period.id,
                    'journal': journal_expense.id,
                    'date': period.start_date,
                    'lines': [
                        ('create', [second_account_line, {
                                    'account': payable.id,
                                    'credit': Decimal(30),
                                    'party': party.id,
                                    }]),
                        ],
                    },
                ]
            Move.create(vlist)

            self.assertEqual((analytic_account.debit, analytic_account.credit),
                (Decimal(30), Decimal(100)))
            self.assertEqual(analytic_account.balance, Decimal(70))

            with transaction.set_context(start_date=period.end_date):
                analytic_account = AnalyticAccount(analytic_account.id)
                self.assertEqual((analytic_account.debit,
                        analytic_account.credit),
                    (Decimal(0), Decimal(0)))
                self.assertEqual(analytic_account.balance, Decimal(0))

            with transaction.set_context(end_date=period.end_date):
                analytic_account = AnalyticAccount(analytic_account.id)
                self.assertEqual((analytic_account.debit,
                        analytic_account.credit),
                    (Decimal(30), Decimal(100)))
                self.assertEqual(analytic_account.balance, Decimal(70))
Example #7
0
    def test_check_credit_limit(self):
        'Test check_credit_limit'
        pool = Pool()
        Account = pool.get('account.account')
        Move = pool.get('account.move')
        Journal = pool.get('account.journal')
        Party = pool.get('party.party')

        company = create_company()
        with set_company(company):
            create_chart(company)
            fiscalyear = get_fiscalyear(company)
            fiscalyear.save()
            fiscalyear.create_period([fiscalyear])
            period = fiscalyear.periods[0]

            receivable, = Account.search([
                ('type.receivable', '=', True),
            ])
            revenue, = Account.search([
                ('type.revenue', '=', True),
            ])
            journal, = Journal.search([], limit=1)
            party, = Party.create([{
                'name': 'Party',
            }])
            Move.create([{
                'journal':
                journal.id,
                'period':
                period.id,
                'date':
                period.start_date,
                'lines': [
                    ('create', [{
                        'debit': Decimal(100),
                        'account': receivable.id,
                        'party': party.id,
                    }, {
                        'credit': Decimal(100),
                        'account': revenue.id,
                    }]),
                ],
            }])
            self.assertEqual(party.credit_amount, Decimal(100))
            self.assertEqual(party.credit_limit_amount, None)
            party.check_credit_limit(Decimal(0))
            party.check_credit_limit(Decimal(0), 'test')
            party.check_credit_limit(Decimal(100))
            party.check_credit_limit(Decimal(100), 'test')
            party.credit_limit_amount = Decimal(0)
            party.save()
            self.assertRaises(UserError, party.check_credit_limit, Decimal(0))
            self.assertRaises(UserWarning, party.check_credit_limit,
                              Decimal(0), 'test')
            party.credit_limit_amount = Decimal(200)
            party.save()
            party.check_credit_limit(Decimal(0))
            party.check_credit_limit(Decimal(0), 'test')
            self.assertRaises(UserError, party.check_credit_limit,
                              Decimal(150))
            self.assertRaises(UserWarning, party.check_credit_limit,
                              Decimal(150), 'test')
    def test_account_debit_credit(self):
        'Test account debit/credit'
        pool = Pool()
        Party = pool.get('party.party')
        AnalyticAccount = pool.get('analytic_account.account')
        Journal = pool.get('account.journal')
        Account = pool.get('account.account')
        Move = pool.get('account.move')
        transaction = Transaction()

        party = Party(name='Party')
        party.save()
        company = create_company()
        with set_company(company):
            root, = AnalyticAccount.create([{
                        'type': 'root',
                        'name': 'Root',
                        }])
            analytic_account, = AnalyticAccount.create([{
                        'type': 'normal',
                        'name': 'Analytic Account',
                        'parent': root.id,
                        'root': root.id,
                        }])
            create_chart(company)
            fiscalyear = get_fiscalyear(company)
            fiscalyear.save()
            fiscalyear.create_period([fiscalyear])
            period = fiscalyear.periods[0]
            journal_revenue, = Journal.search([
                    ('code', '=', 'REV'),
                    ])
            journal_expense, = Journal.search([
                    ('code', '=', 'EXP'),
                    ])
            revenue, = Account.search([
                    ('kind', '=', 'revenue'),
                    ])
            receivable, = Account.search([
                    ('kind', '=', 'receivable'),
                    ])
            expense, = Account.search([
                    ('kind', '=', 'expense'),
                    ])
            payable, = Account.search([
                    ('kind', '=', 'payable'),
                    ])

            first_account_line = {
                'account': revenue.id,
                'credit': Decimal(100),
                'analytic_lines': [
                    ('create', [{
                                'account': analytic_account.id,
                                'name': 'Analytic Line',
                                'credit': Decimal(100),
                                'debit': Decimal(0),
                                'journal': journal_revenue.id,
                                'date': period.start_date,
                                }])
                    ]}
            second_account_line = {
                'account': expense.id,
                'debit': Decimal(30),
                'analytic_lines': [
                    ('create', [{
                                'account': analytic_account.id,
                                'name': 'Analytic Line',
                                'debit': Decimal(30),
                                'credit': Decimal(0),
                                'journal': journal_expense.id,
                                'date': period.start_date,
                                }])
                    ]}
            # Create some moves
            vlist = [{
                    'period': period.id,
                    'journal': journal_revenue.id,
                    'date': period.start_date,
                    'lines': [
                        ('create', [first_account_line, {
                                    'account': receivable.id,
                                    'debit': Decimal(100),
                                    'party': party.id,
                                    }]),
                        ],
                    }, {
                    'period': period.id,
                    'journal': journal_expense.id,
                    'date': period.start_date,
                    'lines': [
                        ('create', [second_account_line, {
                                    'account': payable.id,
                                    'credit': Decimal(30),
                                    'party': party.id,
                                    }]),
                        ],
                    },
                ]
            Move.create(vlist)

            self.assertEqual((analytic_account.debit, analytic_account.credit),
                (Decimal(30), Decimal(100)))
            self.assertEqual(analytic_account.balance, Decimal(70))

            with transaction.set_context(start_date=period.end_date):
                analytic_account = AnalyticAccount(analytic_account.id)
                self.assertEqual((analytic_account.debit,
                        analytic_account.credit),
                    (Decimal(0), Decimal(0)))
                self.assertEqual(analytic_account.balance, Decimal(0))

            with transaction.set_context(end_date=period.end_date):
                analytic_account = AnalyticAccount(analytic_account.id)
                self.assertEqual((analytic_account.debit,
                        analytic_account.credit),
                    (Decimal(30), Decimal(100)))
                self.assertEqual(analytic_account.balance, Decimal(70))
Example #9
0
    def _test_analytic_line_state(self):
        pool = Pool()
        Party = pool.get('party.party')
        AnalyticAccount = pool.get('analytic_account.account')
        AnalyticLine = pool.get('analytic_account.line')
        Journal = pool.get('account.journal')
        Account = pool.get('account.account')
        Move = pool.get('account.move')
        MoveLine = pool.get('account.move.line')

        party = Party(name='Party')
        party.save()
        company = create_company()
        with set_company(company):
            root, = AnalyticAccount.create([{
                'type': 'root',
                'name': 'Root',
            }])
            analytic_account1, analytic_account2 = AnalyticAccount.create([{
                'type':
                'normal',
                'name':
                'Analytic Account 1',
                'parent':
                root.id,
                'root':
                root.id,
            }, {
                'type':
                'normal',
                'name':
                'Analytic Account 2',
                'parent':
                root.id,
                'root':
                root.id,
            }])
            create_chart(company)
            fiscalyear = get_fiscalyear(company)
            fiscalyear.save()
            fiscalyear.create_period([fiscalyear])
            period = fiscalyear.periods[0]
            journal_expense, = Journal.search([
                ('code', '=', 'EXP'),
            ])
            expense, = Account.search([
                ('type.expense', '=', True),
            ])
            payable, = Account.search([
                ('type.payable', '=', True),
            ])

            move = Move()
            move.period = period
            move.journal = journal_expense
            move.date = period.start_date
            move.lines = [
                MoveLine(account=expense, debit=Decimal(100)),
                MoveLine(account=payable, credit=Decimal(100), party=party),
            ]
            move.save()
            Move.post([move])

            expense_line, = [l for l in move.lines if l.account == expense]
            payable_line, = [l for l in move.lines if l.account == payable]

            self.assertEqual(expense_line.analytic_state, 'draft')
            self.assertEqual(payable_line.analytic_state, 'valid')

            expense_line.analytic_lines = [
                AnalyticLine(account=analytic_account1,
                             debit=Decimal(50),
                             date=period.start_date),
                AnalyticLine(account=analytic_account2,
                             debit=Decimal(50),
                             date=period.start_date),
            ]
            expense_line.save()

            self.assertEqual(expense_line.analytic_state, 'valid')
Example #10
0
    def test_check_credit_limit(self):
        'Test check_credit_limit'
        pool = Pool()
        Account = pool.get('account.account')
        Move = pool.get('account.move')
        Journal = pool.get('account.journal')
        Party = pool.get('party.party')
        Sale = pool.get('sale.sale')
        PaymentTerm = pool.get('account.invoice.payment_term')
        Configuration = pool.get('account.configuration')
        FiscalYear = pool.get('account.fiscalyear')
        Invoice = pool.get('account.invoice')

        company = create_company()
        with set_company(company):
            create_chart(company)
            fiscalyear = set_invoice_sequences(get_fiscalyear(company))
            fiscalyear.save()
            FiscalYear.create_period([fiscalyear])
            period = fiscalyear.periods[0]

            receivable, = Account.search([
                ('type.receivable', '=', True),
            ])
            revenue, = Account.search([
                ('type.revenue', '=', True),
            ])
            journal, = Journal.search([], limit=1)
            party, = Party.create([{
                'name': 'Party',
                'addresses': [
                    ('create', [{}]),
                ],
                'credit_limit_amount': Decimal('100'),
            }])
            Move.create([{
                'journal':
                journal.id,
                'period':
                period.id,
                'date':
                period.start_date,
                'lines': [
                    ('create', [{
                        'debit': Decimal('100'),
                        'account': receivable.id,
                        'party': party.id,
                    }, {
                        'credit': Decimal('100'),
                        'account': revenue.id,
                    }]),
                ],
            }])
            payment_term, = PaymentTerm.create([{
                'name':
                'Test',
                'lines': [('create', [{
                    'type': 'remainder',
                }])],
            }])
            config = Configuration(1)
            config.default_category_account_revenue = revenue
            config.save()
            sale, = Sale.create([{
                'party':
                party.id,
                'company':
                company.id,
                'payment_term':
                payment_term.id,
                'currency':
                company.currency.id,
                'invoice_address':
                party.addresses[0].id,
                'shipment_address':
                party.addresses[0].id,
                'lines': [
                    ('create', [{
                        'description': 'Test',
                        'quantity': 1,
                        'unit_price': Decimal('50'),
                    }]),
                ],
            }])
            self.assertEqual(party.credit_amount, Decimal('100'))
            Sale.quote([sale])
            # Test limit reaches
            self.assertRaises(UserWarning, Sale.confirm, [sale])
            self.assertEqual(party.credit_amount, Decimal('100'))
            # Increase limit
            party.credit_limit_amount = Decimal('200')
            party.save()
            # process should work
            Sale.confirm([sale])
            self.assertEqual(sale.state, 'confirmed')
            self.assertEqual(party.credit_amount, Decimal('150'))

            # Process
            Sale.process([sale])
            # Decrease limit
            party.credit_limit_amount = Decimal('100')
            party.save()
            # process should still work as sale is already processing
            Sale.process([sale])

            # Increase quantity invoiced does not change the credit amount
            invoice, = sale.invoices
            invoice_line, = invoice.lines
            invoice_line.quantity += 1
            invoice_line.save()
            Invoice.post([invoice])
            self.assertEqual(party.credit_amount, Decimal('150'))
    def test_check_credit_limit(self):
        'Test check_credit_limit'
        pool = Pool()
        Account = pool.get('account.account')
        Move = pool.get('account.move')
        Journal = pool.get('account.journal')
        Party = pool.get('party.party')
        Sale = pool.get('sale.sale')
        PaymentTerm = pool.get('account.invoice.payment_term')
        Property = pool.get('ir.property')
        ModelField = pool.get('ir.model.field')
        FiscalYear = pool.get('account.fiscalyear')

        company = create_company()
        with set_company(company):
            create_chart(company)
            fiscalyear = set_invoice_sequences(get_fiscalyear(company))
            fiscalyear.save()
            FiscalYear.create_period([fiscalyear])
            period = fiscalyear.periods[0]

            receivable, = Account.search([
                    ('kind', '=', 'receivable'),
                    ])
            revenue, = Account.search([
                    ('kind', '=', 'revenue'),
                    ])
            journal, = Journal.search([], limit=1)
            party, = Party.create([{
                        'name': 'Party',
                        'addresses': [
                            ('create', [{}]),
                            ],
                        'credit_limit_amount': Decimal('100'),
                        }])
            Move.create([{
                        'journal': journal.id,
                        'period': period.id,
                        'date': period.start_date,
                        'lines': [
                            ('create', [{
                                        'debit': Decimal('100'),
                                        'account': receivable.id,
                                        'party': party.id,
                                        }, {
                                        'credit': Decimal('100'),
                                        'account': revenue.id,
                                        }]),
                            ],
                        }])
            payment_term, = PaymentTerm.create([{
                        'name': 'Test',
                        'lines': [
                            ('create', [{
                                        'type': 'remainder',
                                        }])
                            ],
                        }])
            field, = ModelField.search([
                    ('model.model', '=', 'product.template'),
                    ('name', '=', 'account_revenue'),
                    ], limit=1)
            Property.create([{
                    'field': field.id,
                    'value': str(revenue),
                    'company': company.id,
                    }])
            sale, = Sale.create([{
                        'party': party.id,
                        'company': company.id,
                        'payment_term': payment_term.id,
                        'currency': company.currency.id,
                        'invoice_address': party.addresses[0].id,
                        'shipment_address': party.addresses[0].id,
                        'lines': [
                            ('create', [{
                                        'description': 'Test',
                                        'quantity': 1,
                                        'unit_price': Decimal('50'),
                                        }]),
                            ],
                        }])
            self.assertEqual(party.credit_amount, Decimal('100'))
            Sale.quote([sale])
            Sale.confirm([sale])
            self.assertEqual(party.credit_amount, Decimal('100'))
            # Test limit reaches
            self.assertRaises(UserWarning, Sale.process, [sale])
            # Increase limit
            party.credit_limit_amount = Decimal('200')
            party.save()
            # process should work
            Sale.process([sale])
            self.assertEqual(sale.state, 'processing')
            self.assertEqual(party.credit_amount, Decimal('150'))

            # Re-process
            Sale.process([sale])
            # Decrease limit
            party.credit_limit_amount = Decimal('100')
            party.save()
            # process should still work as sale is already processing
            Sale.process([sale])