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.

190 lines
8.2 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. for partner in partner_ids:
  58. total_amount_dividend = 0.0
  59. for line in partner.share_ids:
  60. vals = {}
  61. vals2 = {}
  62. line_id = False
  63. if line.effective_date >= self.date_from \
  64. and line.effective_date <= self.date_to:
  65. date_res = (datetime.strptime(self.date_to, '%Y-%m-%d')
  66. - datetime.strptime(line.effective_date, '%Y-%m-%d')).days
  67. coeff = (date_res / number_of_days) * self.percentage
  68. dividend_amount = line.total_amount_line * coeff
  69. vals['days'] = date_res
  70. vals['dividend_year_id'] = self.id
  71. vals['coop_number'] = line.partner_id.cooperator_register_number
  72. vals['partner_id'] = partner.id
  73. vals['share_line_id'] = line.id
  74. vals['coeff'] = coeff
  75. vals['dividend_amount'] = dividend_amount
  76. total_amount_dividend += dividend_amount
  77. line_id = det_div_line_obj.create(vals)
  78. elif line.effective_date < self.date_from:
  79. dividend_amount = line.total_amount_line * self.percentage
  80. vals['days'] = number_of_days
  81. vals['dividend_year_id'] = self.id
  82. vals['coop_number'] = line.partner_id.cooperator_register_number
  83. vals['partner_id'] = partner.id
  84. vals['share_line_id'] = line.id
  85. vals['coeff'] = self.percentage
  86. vals['dividend_amount'] = dividend_amount
  87. total_amount_dividend += dividend_amount
  88. line_id = det_div_line_obj.create(vals)
  89. if line_id:
  90. vals2['coop_number'] = line.partner_id.cooperator_register_number
  91. vals2['dividend_year_id'] = self.id
  92. vals2['partner_id'] = line.partner_id.id
  93. vals2['dividend_amount_net'] = total_amount_dividend
  94. vals2['dividend_amount'] = total_amount_dividend
  95. # TODO set as a parameter on dividend year object
  96. if total_amount_dividend <= 190.00:
  97. vals2['dividend_taxes'] = 0.0
  98. else:
  99. div_tax = (total_amount_dividend - 190) * self.withholding_tax
  100. vals2['dividend_taxes'] = div_tax
  101. vals2['dividend_amount_net'] = total_amount_dividend - div_tax
  102. div_line_obj.create(vals2)
  103. return True
  104. class DetailedDividendLine(models.Model):
  105. _name = 'detailed.dividend.line'
  106. @api.multi
  107. def _compute_total_line(self):
  108. res = {}
  109. for line in self:
  110. res[line.id] = line.share_unit_price * line.share_number
  111. return res
  112. dividend_year_id = fields.Many2one('dividend.year',
  113. string='Dividend year')
  114. coop_number = fields.Integer(string='Cooperator Number')
  115. days = fields.Integer(string='Days')
  116. partner_id = fields.Many2one('res.partner',
  117. string='Cooperator',
  118. readonly=True)
  119. share_line_id = fields.Many2one('share.line',
  120. string='Share line',
  121. readonly=True)
  122. share_number = fields.Integer(related='share_line_id.share_number',
  123. string='Number of Share')
  124. share_unit_price = fields.Monetary(
  125. string='Share unit price',
  126. related='share_line_id.share_unit_price',
  127. )
  128. effective_date = fields.Date(related='share_line_id.effective_date',
  129. string='Effective date')
  130. total_amount_line = fields.Monetary(
  131. string="Total value of share",
  132. currency_field="company_currency_id",
  133. compute=_compute_total_line,
  134. readonly=True,
  135. )
  136. coeff = fields.Float(string='Coefficient to apply',
  137. digits=(2, 4))
  138. dividend_amount = fields.Float(string='Gross Dividend')
  139. dividend_amount_net = fields.Float(string='Dividend net')
  140. dividend_taxes = fields.Float(string='Taxes')
  141. company_currency_id = fields.Many2one(
  142. related="share_line_id.company_currency_id",
  143. readonly=True,
  144. )
  145. class dividend_line(models.Model):
  146. _name = 'dividend.line'
  147. @api.multi
  148. def _get_account_number(self):
  149. res = {}
  150. for line in self:
  151. bank_accounts = self.env['res.partner.bank'].search(
  152. [('partner_id', '=', line.partner_id.id)])
  153. res[line.id] = bank_accounts[0].acc_number
  154. return res
  155. coop_number = fields.Integer(string='Coop Number')
  156. dividend_year_id = fields.Many2one('dividend.year',
  157. string='Dividend year')
  158. partner_id = fields.Many2one('res.partner',
  159. string='Cooperator',
  160. readonly=True)
  161. account_number = fields.Char(compute=_get_account_number,
  162. string='Account Number')
  163. dividend_amount = fields.Float(string='Gross Dividend')
  164. dividend_amount_net = fields.Float(string='Dividend net')
  165. dividend_taxes = fields.Float(string='Taxes')