analitic_sales.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. # @authors: Alexander Ezquevo <alexander@acysos.com>
  3. # Copyright (C) 2015 Acysos S.L.
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import models, fields, api
  6. from openerp.exceptions import Warning
  7. class analitic_account_invoice(models.Model):
  8. _inherit = "account.invoice"
  9. @api.multi
  10. def invoice_validate(self):
  11. for res in self:
  12. if res.type == 'out_invoice' or res.type == 'out_refund':
  13. if res.type == 'out_invoice':
  14. ref_id = res.id
  15. line_name = 'sale'
  16. else:
  17. ref_id = res.origin_invoices_ids[0].id
  18. line_name = 'refund'
  19. self.create_analitic_move(ref_id, res, line_name)
  20. return super(analitic_account_invoice, self).invoice_validate()
  21. @api.multi
  22. @api.returns('self')
  23. def refund(self, date=None, period_id=None, description=None,
  24. journal_id=None):
  25. for res in self:
  26. if res.type == 'out_invoice':
  27. ref_id = res.id
  28. line_name = 'invoice-cancel'
  29. self.create_analitic_move(ref_id, res, line_name)
  30. return super(
  31. analitic_account_invoice, self).refund(date, period_id,
  32. description, journal_id)
  33. def create_analitic_move(self, ref_id, res, line_name):
  34. analytic_line_obj = self.env['account.analytic.line']
  35. sales = self.env['sale.order'].search([
  36. ('invoice_ids', 'in', [ref_id, ])])
  37. animal_total = 0
  38. sale_animal = []
  39. for sale in sales:
  40. for sline in sale.order_line:
  41. procurement = self.env['procurement.order'].search([
  42. ('sale_line_id', '=', sline.id)])
  43. moves = self.env['stock.move'].search([
  44. ('procurement_id', '=', procurement.id)])
  45. lots = []
  46. for move in moves:
  47. for lot in move.quant_ids:
  48. operations = self.env['stock.pack.operation'].search([
  49. ('picking_id', '=', move.picking_id.id),
  50. ('lot_id', '=', lot.lot_id.id)])
  51. for op in operations:
  52. lots.append([lot.lot_id.name, op.product_qty])
  53. animals_obj = self.env['farm.animal.group']
  54. partys = animals_obj.search([(True, '=', True)])
  55. for party in partys:
  56. for lot in lots:
  57. if party.number == lot[0]:
  58. sale_animal.append([party, lot[1]])
  59. animal_total = animal_total + lot[1]
  60. if len(sale_animal) > 0:
  61. total_price = 0
  62. for line in res.invoice_line:
  63. total_price = total_price \
  64. + line.price_subtotal
  65. if line_name == 'invoice-cancel':
  66. total_price = total_price * -1
  67. price_unit = total_price/animal_total
  68. for group in sale_animal:
  69. company = self.env['res.company'].search([
  70. (True, '=', True)])[0]
  71. journal = self.env[
  72. 'account.analytic.journal'].search([
  73. ('code', '=', 'FAR')])
  74. amount = price_unit * group[1]
  75. analytic_line_obj.create({
  76. 'name': line_name,
  77. 'date': res.date_invoice,
  78. 'amount': amount,
  79. 'unit_amount': group[1],
  80. 'account_id': group[0].account.id,
  81. 'general_account_id': company.feed_account.id,
  82. 'journal_id': journal.id,
  83. })
  84. else:
  85. species_obj = self.env['farm.specie']
  86. for specie in species_obj:
  87. if move.product_id.id == specie.group_product.id:
  88. raise Warning(_('group sold not found'))