Exemplo n.º 1
0
    def assign_number(cls, invoice, template):
        """
        This function should be run within an SQL transaction to enforce
        sequence index unicity.
        """
        if invoice.official_number:
            raise ValueError('This invoice already have an official number')
        cls.validate_template(template)

        db = DBSESSION()
        formatter = InvoiceNumberFormatter(invoice, cls.SEQUENCES_MAP)
        invoice_number = formatter.format(template)

        involved_sequences = cls.get_involved_sequences(invoice, template)
        # Create SequenceNumber objects (the index useages have not been
        # booked until now).
        for sequence, next_index in involved_sequences:
            sn = SequenceNumber(
                sequence=sequence.db_key,
                index=next_index,
                task_id=invoice.id,
            )
            db.add(sn)
        db.flush()
        invoice.official_number = invoice_number
        db.merge(invoice)
        return invoice_number
Exemplo n.º 2
0
def global_seq_1(dbsession, invoice):
    from autonomie.models.task.sequence_number import SequenceNumber
    s = SequenceNumber(
        sequence=SequenceNumber.SEQUENCE_INVOICE_GLOBAL,
        index=1,
        task_id=invoice.id,
    )
    dbsession.add(s)
    dbsession.flush()
    return s
Exemplo n.º 3
0
 def _set_seq_index(index, year, month, sequence, company=company):
     s = SequenceNumber(
         sequence=sequence,
         index=index,
         task_id=mk_invoice(
             date=datetime.date(year, month, 1),
             company=company,
         ).id,
     )
     dbsession.add(s)
     dbsession.flush()
     return s
Exemplo n.º 4
0
    def assign_number(cls, invoice, template):
        """
        This function should be run within an SQL transaction to enforce
        sequence index unicity.
        """
        if invoice.official_number:
            raise ValueError('This invoice already have an official number')

        db = DBSESSION()
        formatter = InvoiceNumberFormatter(invoice, cls.SEQUENCES_MAP)
        invoice_number = formatter.format(template)

        involved_sequences = cls.get_involved_sequences(invoice, template)

        with db.begin_nested():
            # Create SequenceNumber objects (the index useages have not been
            # booked until now).
            for sequence, next_index in involved_sequences:
                sn = SequenceNumber(
                    sequence=sequence.db_key,
                    index=next_index,
                    task_id=invoice.id,
                )
                db.add(sn)
            invoice.official_number = invoice_number
            db.merge(invoice)

            # Imported here to avoid circular dependencies
            from autonomie.models.task import Task, Invoice, CancelInvoice

            query = Task.query().with_polymorphic([Invoice, CancelInvoice])
            query = query.filter(
                Task.official_number == invoice_number,
                Task.id != invoice.id,
                Task.legacy_number == False,
            ).scalar()

            if query is not None:
                # This case is exceptionnal, we can afford a crash here
                # Context manager will take care of rolling back
                # subtransaction.
                raise ValueError(
                    'Invoice number collision, rolling back to avoid it'
                )

        return invoice_number