Advertisement
NeyderCorrea

Untitled

Feb 13th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. def _create_service_invoice(self):
  2. pool = Pool()
  3. HealthService = pool.get('pathocytology.health_service')
  4. Invoice = pool.get('account.invoice')
  5. Party = pool.get('party.party')
  6. Journal = pool.get('account.journal')
  7.  
  8. currency_id = Transaction().context.get('currency')
  9.  
  10. service = self.record
  11.  
  12. services = self.records
  13.  
  14. # Invoice Lines
  15. invoice_lines = []
  16.  
  17. for service in services:
  18. # Invoice Header
  19. if service.state == 'invoiced':
  20. raise ServiceAlreadyInvoiced(
  21. gettext('pathocytology_service.msg_service_already_invoiced'))
  22.  
  23. party = service.invoice_to or service.patient.name
  24.  
  25. sale_price_list = party.sale_price_list \
  26. if hasattr(party, 'sale_price_list') else None
  27.  
  28. ctx = {
  29. 'price_list': sale_price_list.id,
  30. 'sale_date': date.today(),
  31. 'currency': currency_id,
  32. 'customer': party.id
  33. } if sale_price_list else {}
  34.  
  35. journals = Journal.search([('type', '=', 'revenue')], limit=1)
  36. journal, = journals if journals else None
  37.  
  38. party_address = Party.address_get(party, type='invoice')
  39. if not party_address:
  40. raise NoInvoiceAddress(
  41. gettext('pathocytology_service.msg_no_invoice_address'))
  42.  
  43. if not party.customer_payment_term:
  44. raise NoPaymentTerm(
  45. gettext('pathocytology_service.msg_no_payment_term'))
  46.  
  47. for seq, line in enumerate(service.service_line):
  48. if sale_price_list:
  49. with Transaction().set_context(ctx):
  50. unit_price = sale_price_list.compute(
  51. party, line.product, line.product.list_price,
  52. line.qty, line.product.default_uom)
  53. else:
  54. unit_price = line.product.list_price
  55.  
  56. invoice_lines.append({
  57. 'origin': f"{line}",
  58. 'product': line.product.id,
  59. 'description': line.desc,
  60. 'quantity': line.qty,
  61. 'account': line.product.template.account_revenue_used.id,
  62. 'unit': line.product.default_uom.id,
  63. 'unit_price': unit_price,
  64. 'sequence': seq + 1,
  65. 'taxes': [product_tax_line.id
  66. for product_tax_line in line.product.customer_taxes_used
  67. if line.to_invoice],
  68.  
  69. })
  70.  
  71. invoice = Invoice(
  72. description=service.desc,
  73. party=party.id,
  74. type='out',
  75. invoice_date=date.today(),
  76. account=party.account_receivable.id,
  77. company=service.company.id,
  78. journal=journal.id,
  79. invoice_address=party_address.id,
  80. reference=service.name,
  81. payment_term=party.customer_payment_term.id,
  82. lines=invoice_lines
  83. )
  84. invoice.save()
  85. invoices = [invoice]
  86. Invoice.update_taxes(invoices)
  87.  
  88. # Change to invoiced the status on the service document.
  89. HealthService.write(services, {'state': 'invoiced', 'invoice': invoice.id})
  90.  
  91. return invoice
  92.  
  93. def do_open_(self, action):
  94. invoice = self._create_service_invoice()
  95. action['res_id'] = [invoice.id]
  96. return action, {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement