You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
8.1 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. from datetime import datetime
  4. import openerp.addons.decimal_precision as dp
  5. from openerp import fields, models, api
  6. class dividend_year(models.Model):
  7. _name = 'dividend.year'
  8. @api.multi
  9. def _compute_dividend_info(self):
  10. res = {}
  11. for dividend in self:
  12. res[dividend.id] = {'grand_total_dividend': 0.0,
  13. 'grand_total_taxes': 0.0}
  14. for line in dividend.dividend_ids:
  15. res[dividend.id]['grand_total_dividend'] += line.dividend_amount
  16. res[dividend.id]['grand_total_taxes'] += line.dividend_taxes
  17. return res
  18. name = fields.Char(string='Code')
  19. date_from = fields.Date(string='Date from')
  20. date_to = fields.Date(string='Date to')
  21. # fiscal_year_id = fields.Many2one('account.fiscalyear',
  22. # string='Fiscal year')
  23. percentage = fields.Float(string='Percentage')
  24. withholding_tax = fields.Float(string='Withholding tax')
  25. detailed_dividend_ids = fields.One2many('detailed.dividend.line',
  26. 'dividend_year_id',
  27. string='Dividend lines')
  28. dividend_ids = fields.One2many('dividend.line',
  29. 'dividend_year_id',
  30. string='Dividend lines')
  31. grand_total_dividend = fields.Float(
  32. compute=_compute_dividend_info,
  33. string='Grand total dividend',
  34. digits_compute=dp.get_precision('Account'))
  35. grand_total_taxes = fields.Float(
  36. compute=_compute_dividend_info,
  37. string='Grand total taxes',
  38. digits_compute=dp.get_precision('Account'))
  39. @api.multi
  40. def compute_dividend(self):
  41. self.ensure_one()
  42. det_div_line_obj = self.env['detailed.dividend.line']
  43. div_line_obj = self.env['dividend.line']
  44. res_partner_obj = self.env['res.partner']
  45. # delete lines if any
  46. detailed_dividend_ids = det_div_line_obj.search([('dividend_year_id', '=', self.id)])
  47. detailed_dividend_ids.unlink()
  48. dividend_ids = div_line_obj.search([('dividend_year_id', '=', self.id)])
  49. dividend_ids.unlink()
  50. partner_ids = res_partner_obj.search([
  51. ('cooperator', '=', True),
  52. ('member', '=', True)],
  53. order='cooperator_register_number')
  54. number_of_days = (datetime.strptime(self.date_to, '%Y-%m-%d')
  55. - datetime.strptime(self.date_from, '%Y-%m-%d')
  56. ).days + 1
  57. print number_of_days
  58. for partner in partner_ids:
  59. total_amount_dividend = 0.0
  60. for line in partner.share_ids:
  61. vals = {}
  62. vals2 = {}
  63. line_id = False
  64. if line.effective_date >= self.date_from \
  65. and line.effective_date <= self.date_to:
  66. date_res = (datetime.strptime(self.date_to, '%Y-%m-%d')
  67. - datetime.strptime(line.effective_date, '%Y-%m-%d')).days
  68. coeff = (date_res / number_of_days) * self.percentage
  69. dividend_amount = line.total_amount_line * coeff
  70. vals['days'] = date_res
  71. vals['dividend_year_id'] = self.id
  72. vals['coop_number'] = line.partner_id.cooperator_register_number
  73. vals['partner_id'] = partner.id
  74. vals['share_line_id'] = line.id
  75. vals['coeff'] = coeff
  76. vals['dividend_amount'] = dividend_amount
  77. total_amount_dividend += dividend_amount
  78. line_id = det_div_line_obj.create(vals)
  79. elif line.effective_date < self.date_from:
  80. dividend_amount = line.total_amount_line * self.percentage
  81. vals['days'] = number_of_days
  82. vals['dividend_year_id'] = self.id
  83. vals['coop_number'] = line.partner_id.cooperator_register_number
  84. vals['partner_id'] = partner.id
  85. vals['share_line_id'] = line.id
  86. vals['coeff'] = self.percentage
  87. vals['dividend_amount'] = dividend_amount
  88. total_amount_dividend += dividend_amount
  89. line_id = det_div_line_obj.create(vals)
  90. if line_id:
  91. vals2['coop_number'] = line.partner_id.cooperator_register_number
  92. vals2['dividend_year_id'] = self.id
  93. vals2['partner_id'] = line.partner_id.id
  94. vals2['dividend_amount_net'] = total_amount_dividend
  95. vals2['dividend_amount'] = total_amount_dividend
  96. # TODO set as a parameter on dividend year object
  97. if total_amount_dividend <= 190.00:
  98. vals2['dividend_taxes'] = 0.0
  99. else:
  100. div_tax = (total_amount_dividend - 190) * self.withholding_tax
  101. vals2['dividend_taxes'] = div_tax
  102. vals2['dividend_amount_net'] = total_amount_dividend - div_tax
  103. div_line_obj.create(vals2)
  104. return True
  105. class DetailedDividendLine(models.Model):
  106. _name = 'detailed.dividend.line'
  107. @api.multi
  108. def _compute_total_line(self):
  109. res = {}
  110. for line in self:
  111. res[line.id] = line.share_unit_price * line.share_number
  112. return res
  113. dividend_year_id = fields.Many2one('dividend.year',
  114. string='Dividend year')
  115. coop_number = fields.Integer(string='Cooperator Number')
  116. days = fields.Integer(string='Days')
  117. partner_id = fields.Many2one('res.partner',
  118. string='Cooperator',
  119. readonly=True)
  120. share_line_id = fields.Many2one('share.line',
  121. string='Share line',
  122. readonly=True)
  123. share_number = fields.Integer(related='share_line_id.share_number',
  124. string='Number of Share')
  125. share_unit_price = fields.Float(related='share_line_id.share_unit_price',
  126. string='Share unit price')
  127. effective_date = fields.Date(related='share_line_id.effective_date',
  128. string='Effective date')
  129. total_amount_line = fields.Float(compute=_compute_total_line,
  130. string="Total value of share",
  131. readonly=True)
  132. coeff = fields.Float(string='Coefficient to apply',
  133. digits=(2, 4))
  134. dividend_amount = fields.Float(string='Gross Dividend')
  135. dividend_amount_net = fields.Float(string='Dividend net')
  136. dividend_taxes = fields.Float(string='Taxes')
  137. class dividend_line(models.Model):
  138. _name = 'dividend.line'
  139. @api.multi
  140. def _get_account_number(self):
  141. res = {}
  142. for line in self:
  143. bank_accounts = self.env['res.partner.bank'].search(
  144. [('partner_id', '=', line.partner_id.id)])
  145. res[line.id] = bank_accounts[0].acc_number
  146. return res
  147. coop_number = fields.Integer(string='Coop Number')
  148. dividend_year_id = fields.Many2one('dividend.year',
  149. string='Dividend year')
  150. partner_id = fields.Many2one('res.partner',
  151. string='Cooperator',
  152. readonly=True)
  153. account_number = fields.Char(compute=_get_account_number,
  154. string='Account Number')
  155. dividend_amount = fields.Float(string='Gross Dividend')
  156. dividend_amount_net = fields.Float(string='Dividend net')
  157. dividend_taxes = fields.Float(string='Taxes')