Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _create_service_invoice(self):
- pool = Pool()
- HealthService = pool.get('pathocytology.health_service')
- Invoice = pool.get('account.invoice')
- Party = pool.get('party.party')
- Journal = pool.get('account.journal')
- currency_id = Transaction().context.get('currency')
- service = self.record
- services = self.records
- # Invoice Lines
- invoice_lines = []
- for service in services:
- # Invoice Header
- if service.state == 'invoiced':
- raise ServiceAlreadyInvoiced(
- gettext('pathocytology_service.msg_service_already_invoiced'))
- party = service.invoice_to or service.patient.name
- sale_price_list = party.sale_price_list \
- if hasattr(party, 'sale_price_list') else None
- ctx = {
- 'price_list': sale_price_list.id,
- 'sale_date': date.today(),
- 'currency': currency_id,
- 'customer': party.id
- } if sale_price_list else {}
- journals = Journal.search([('type', '=', 'revenue')], limit=1)
- journal, = journals if journals else None
- party_address = Party.address_get(party, type='invoice')
- if not party_address:
- raise NoInvoiceAddress(
- gettext('pathocytology_service.msg_no_invoice_address'))
- if not party.customer_payment_term:
- raise NoPaymentTerm(
- gettext('pathocytology_service.msg_no_payment_term'))
- for seq, line in enumerate(service.service_line):
- if sale_price_list:
- with Transaction().set_context(ctx):
- unit_price = sale_price_list.compute(
- party, line.product, line.product.list_price,
- line.qty, line.product.default_uom)
- else:
- unit_price = line.product.list_price
- invoice_lines.append({
- 'origin': f"{line}",
- 'product': line.product.id,
- 'description': line.desc,
- 'quantity': line.qty,
- 'account': line.product.template.account_revenue_used.id,
- 'unit': line.product.default_uom.id,
- 'unit_price': unit_price,
- 'sequence': seq + 1,
- 'taxes': [product_tax_line.id
- for product_tax_line in line.product.customer_taxes_used
- if line.to_invoice],
- })
- invoice = Invoice(
- description=service.desc,
- party=party.id,
- type='out',
- invoice_date=date.today(),
- account=party.account_receivable.id,
- company=service.company.id,
- journal=journal.id,
- invoice_address=party_address.id,
- reference=service.name,
- payment_term=party.customer_payment_term.id,
- lines=invoice_lines
- )
- invoice.save()
- invoices = [invoice]
- Invoice.update_taxes(invoices)
- # Change to invoiced the status on the service document.
- HealthService.write(services, {'state': 'invoiced', 'invoice': invoice.id})
- return invoice
- def do_open_(self, action):
- invoice = self._create_service_invoice()
- action['res_id'] = [invoice.id]
- return action, {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement