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.

347 lines
18 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. from openerp import api, fields, models, _
  4. from openerp.addons.base_iban import base_iban
  5. from openerp.exceptions import UserError, ValidationError
  6. import openerp.addons.decimal_precision as dp
  7. _REQUIRED = ['email','firstname','lastname','birthdate','address','share_product_id','ordered_parts','zip_code','city','iban','no_registre','gender'] # Could be improved including required from model
  8. @api.model
  9. def _lang_get(self):
  10. languages = self.env['res.lang'].search([])
  11. return [(language.code, language.name) for language in languages]
  12. class subscription_request(models.Model):
  13. _name = 'subscription.request'
  14. _description = 'Subscription Request'
  15. def get_required_field(self):
  16. return _REQUIRED
  17. @api.model
  18. def create(self,vals):
  19. if not vals.get('partner_id'):
  20. cooperator = False
  21. if vals.get('no_registre'):
  22. cooperator = self.env['res.partner'].get_cooperator_from_nin(vals.get('no_registre'))
  23. if cooperator:
  24. if cooperator.member:
  25. vals['type'] = 'increase'
  26. vals['already_cooperator'] = True
  27. else:
  28. vals['type'] = 'subscription'
  29. vals['partner_id'] = cooperator.id
  30. subscr_request = super(subscription_request, self).create(vals)
  31. mail_template_obj = self.env['mail.template']
  32. confirmation_mail_template = mail_template_obj.search([('name', '=', 'Confirmation Email')])[0]
  33. confirmation_mail_template.send_mail(subscr_request.id, True)
  34. return subscr_request
  35. @api.model
  36. def create_comp_sub_req(self, vals):
  37. if not vals.get('partner_id'):
  38. cooperator = self.env['res.partner'].get_cooperator_from_crn(vals.get('company_register_number'))
  39. if cooperator:
  40. vals['partner_id'] = cooperator.id
  41. vals['type'] = 'increase'
  42. vals['already_cooperator'] = True
  43. subscr_request = super(subscription_request, self).create(vals)
  44. mail_template_obj = self.env['mail.template']
  45. confirmation_mail_template = mail_template_obj.search([('name', '=', 'Company Confirmation Email')])[0]
  46. confirmation_mail_template.send_mail(subscr_request.id, True)
  47. return subscr_request
  48. def check_belgian_identification_id(self, nat_register_num):
  49. if not self.check_empty_string(nat_register_num):
  50. return False
  51. if len(nat_register_num) != 11:
  52. return False
  53. if not nat_register_num.isdigit():
  54. return False
  55. birthday_number = nat_register_num[0:9]
  56. controle = nat_register_num[9:11]
  57. check_controle = 97 - (int(birthday_number) % 97)
  58. if int(check_controle) != int(controle):
  59. check_controle = 97 - ((2000000000 + int(birthday_number)) % 97)
  60. if int(check_controle) != int(controle):
  61. return False
  62. return True
  63. def check_empty_string(self, value):
  64. if value == None or value == False or value == '':
  65. return False
  66. return True
  67. @api.multi
  68. @api.depends('iban', 'no_registre','skip_control_ng')
  69. def _validated_lines(self):
  70. for sub_request in self:
  71. try:
  72. base_iban.validate_iban(sub_request.iban)
  73. sub_request.validated = True
  74. except ValidationError:
  75. sub_request.validated = False
  76. if not sub_request.is_company and (sub_request.skip_control_ng or self.check_belgian_identification_id(sub_request.no_registre)):
  77. sub_request.validated = True
  78. @api.multi
  79. @api.depends('share_product_id', 'share_product_id.list_price','ordered_parts')
  80. def _compute_subscription_amount(self):
  81. for sub_request in self:
  82. sub_request.subscription_amount = sub_request.share_product_id.list_price * sub_request.ordered_parts
  83. already_cooperator = fields.Boolean(string="I'm already cooperator")
  84. name = fields.Char(string='Name', required=True)
  85. firstname = fields.Char(string='Firstname')
  86. lastname = fields.Char(string='Lastname')
  87. birthdate = fields.Date(string="Birthdate")
  88. gender = fields.Selection([('male', 'Male'),
  89. ('female', 'Female'),
  90. ('other', 'Other')], string='Gender')
  91. type = fields.Selection([('new','New Cooperator'),
  92. ('subscription','Subscription'),
  93. ('increase','Increase number of share')]
  94. ,string='Type', default="new")
  95. state = fields.Selection([('draft','Draft'),
  96. ('block','Blocked'),
  97. ('done','Done'),
  98. ('cancelled','Cancelled'),
  99. ('paid','paid')],
  100. string='State',required=True, default="draft")
  101. email = fields.Char(string='Email')
  102. iban = fields.Char(string='Account Number')
  103. partner_id = fields.Many2one('res.partner',string='Cooperator')
  104. share_product_id = fields.Many2one('product.product', string='Share type', domain=[('is_share','=',True)])
  105. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name')
  106. share_unit_price = fields.Float(related='share_product_id.list_price', string='Share price')
  107. subscription_amount = fields.Float(compute='_compute_subscription_amount', string='Subscription amount')
  108. ordered_parts = fields.Integer(string='Number of Share')
  109. address = fields.Char(string='Address')
  110. city = fields.Char(string='City')
  111. zip_code = fields.Char(string='Zip Code')
  112. country_id = fields.Many2one('res.country', string='Country', ondelete='restrict')
  113. phone = fields.Char(string='Phone')
  114. no_registre = fields.Char(string='National Register Number')
  115. user_id = fields.Many2one('res.users', string='Responsible', readonly=True)
  116. validated = fields.Boolean(compute='_validated_lines', string='Valid Line?', readonly=True)
  117. skip_control_ng = fields.Boolean(string="Skip control",
  118. help="if this field is checked then no control will be done on the national register number and on the iban bank account. To be done in case of the id card is from abroad or in case of a passport")
  119. lang = fields.Selection(_lang_get, 'Language', default='fr_BE',
  120. help="If the selected language is loaded in the system, all documents related to this contact will be printed in this language. If not, it will be English.")
  121. date = fields.Date(string='Subscription date request', default=lambda self: datetime.strftime(datetime.now(), '%Y-%m-%d'))
  122. company_id = fields.Many2one('res.company', string='Company', required=True,
  123. change_default=True, readonly=True,
  124. default=lambda self: self.env['res.company']._company_default_get())
  125. is_company = fields.Boolean(string='Is a company')
  126. is_operation = fields.Boolean(string='Is an operation')
  127. company_name = fields.Char(string="Company name")
  128. company_email = fields.Char(string="Company email")
  129. company_register_number = fields.Char(string='Company register number')
  130. company_type = fields.Selection([('scrl','SCRL'),
  131. ('asbl','ASBL'),
  132. ('sprl','SPRL'),
  133. ('sa','SA'),
  134. ('other','Other')])
  135. same_address = fields.Boolean(string='Same address')
  136. activities_address = fields.Char(string='Activities address')
  137. activities_city = fields.Char(string='Activities city')
  138. activities_zip_code = fields.Char(string='Activities zip Code')
  139. activities_country_id = fields.Many2one('res.country', string='Activities country', ondelete='restrict')
  140. contact_person_function = fields.Char(string='Function')
  141. operation_request_id = fields.Many2one('operation.request', string="Operation Request")
  142. is_operation = fields.Boolean(string="Is Operation request")
  143. capital_release_request = fields.One2many('account.invoice','subscription_request', string='Subscription request')
  144. _order = "id desc"
  145. def _prepare_invoice_line(self, product, partner, qty):
  146. self.ensure_one()
  147. res = {}
  148. account = product.property_account_income_id or product.categ_id.property_account_income_categ_id
  149. if not account:
  150. raise UserError(_('Please define income account for this product: "%s" (id:%d) - or for its category: "%s".') % \
  151. (product.name, product.id, product.categ_id.name))
  152. fpos = partner.property_account_position_id
  153. if fpos:
  154. account = fpos.map_account(account)
  155. res = {
  156. 'name': product.name,
  157. 'account_id': account.id,
  158. 'price_unit': product.lst_price,
  159. 'quantity': qty,
  160. 'uom_id': product.uom_id.id,
  161. 'product_id': product.id or False,
  162. }
  163. return res
  164. def send_capital_release_request(self, invoice):
  165. invoice_email_template = self.env['mail.template'].search([('name', '=', 'Request to Release Capital - Send by Email')])[0]
  166. # we send the email with the capital release request in attachment
  167. invoice_email_template.send_mail(invoice.id, True)
  168. invoice.sent = True
  169. def create_invoice(self, partner):
  170. # get subscription journal
  171. journal = self.env['account.journal'].search([('code','=','SUBJ')])[0]
  172. # get the account for associate
  173. # TODO this should be defined in configuration
  174. if self.company_id.property_cooperator_account:
  175. account = self.company_id.property_cooperator_account
  176. else:
  177. account = self.env['account.account'].search([('code','=','416000')])[0]
  178. # creating invoice and invoice lines
  179. invoice = self.env['account.invoice'].create({'partner_id':partner.id,
  180. 'journal_id':journal.id,'account_id':account.id,
  181. 'type': 'out_invoice', 'release_capital_request':True,
  182. 'subscription_request':self.id})
  183. vals = self._prepare_invoice_line(self.share_product_id, partner, self.ordered_parts)
  184. vals['invoice_id'] = invoice.id
  185. line = self.env['account.invoice.line'].create(vals)
  186. # validate the capital release request
  187. invoice.signal_workflow('invoice_open')
  188. self.send_capital_release_request(invoice)
  189. return invoice
  190. def get_partner_company_vals(self):
  191. # this should go to the many2many tag field
  192. #'title':'company',
  193. #self.env['res.partner.title'].search([('shortcut','=',self.company_type)])
  194. partner_vals = {'name':self.company_name, 'is_company': self.is_company,
  195. 'company_register_number':self.company_register_number, 'customer':False,
  196. 'cooperator':True, 'street':self.address, 'zip':self.zip_code,
  197. 'city': self.city,'email':self.email, 'out_inv_comm_type':'bba',
  198. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id, 'lang':self.lang}
  199. return partner_vals
  200. def get_partner_vals(self):
  201. partner_vals = {'name':self.name, 'first_name':self.firstname, 'last_name': self.lastname,
  202. 'customer':False, 'gender':self.gender,'cooperator':True, 'street':self.address,'zip':self.zip_code,
  203. 'city': self.city, 'phone': self.phone, 'email':self.email,
  204. 'national_register_number':self.no_registre, 'out_inv_comm_type':'bba',
  205. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id,
  206. 'lang':self.lang, 'birthdate':self.birthdate}
  207. return partner_vals
  208. def create_coop_partner(self):
  209. partner_obj = self.env['res.partner']
  210. if self.is_company:
  211. partner_vals = self.get_partner_company_vals()
  212. else:
  213. partner_vals = self.get_partner_vals()
  214. partner = partner_obj.create(partner_vals)
  215. if self.iban :
  216. self.env['res.partner.bank'].create({'partner_id':partner.id,'acc_number':self.iban})
  217. return partner
  218. @api.one
  219. def validate_subscription_request(self):
  220. partner_obj = self.env['res.partner']
  221. if self.partner_id:
  222. partner = self.partner_id
  223. else:
  224. if self.already_cooperator:
  225. raise UserError(_('The checkbox already cooperator is checked please select a cooperator.'))
  226. elif self.is_company:
  227. partner = partner_obj.search([('company_register_number','=',self.company_register_number)])
  228. elif self.no_registre:
  229. partner = partner_obj.search([('national_register_number','=',self.no_registre)])
  230. else:
  231. partner = None
  232. if not partner:
  233. partner = self.create_coop_partner()
  234. if self.is_company:
  235. contact = partner_obj.search([('national_register_number','=',self.no_registre)])
  236. if not contact:
  237. contact_vals = {'name':self.name, 'first_name':self.firstname, 'last_name': self.lastname,
  238. 'customer':False, 'street':self.address,'zip':self.zip_code,
  239. 'city': self.city, 'phone': self.phone, 'email':self.email,
  240. 'national_register_number':self.no_registre, 'out_inv_comm_type':'bba',
  241. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id,
  242. 'lang':self.lang, 'birthdate':self.birthdate, 'parent_id': partner.id,'function':self.contact_person_function}
  243. contact = partner_obj.create(contact_vals)
  244. else:
  245. partner = partner[0]
  246. invoice = self.create_invoice(partner)
  247. self.write({'partner_id':partner.id, 'state':'done'})
  248. return invoice
  249. @api.one
  250. def block_subscription_request(self):
  251. self.write({'state':'block'})
  252. @api.one
  253. def unblock_subscription_request(self):
  254. self.write({'state':'draft'})
  255. @api.one
  256. def cancel_subscription_request(self):
  257. self.write({'state':'cancelled'})
  258. class share_line(models.Model):
  259. _name='share.line'
  260. @api.multi
  261. def _compute_total_line(self):
  262. res = {}
  263. for line in self:
  264. line.total_amount_line = line.share_unit_price * line.share_number
  265. return res
  266. share_product_id = fields.Many2one('product.product', string='Share type', required=True, readonly=True)
  267. share_number = fields.Integer(string='Number of Share', required=True, readonly=True)
  268. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name')
  269. share_unit_price = fields.Float(string='Share price', readonly=True)
  270. effective_date = fields.Date(string='Effective Date', readonly=True)
  271. partner_id = fields.Many2one('res.partner',string='Cooperator', required=True, ondelete='cascade', readonly=True)
  272. total_amount_line = fields.Float(compute='_compute_total_line', string='Total amount line')
  273. class subscription_register(models.Model):
  274. _name= 'subscription.register'
  275. @api.multi
  276. def _compute_total_line(self):
  277. res = {}
  278. for register_line in self:
  279. register_line.total_amount_line = register_line.share_unit_price * register_line.quantity
  280. name = fields.Char(string='Register Number Operation', required=True, readonly=True)
  281. register_number_operation = fields.Integer(string='Register Number Operation', required=True, readonly=True)
  282. partner_id = fields.Many2one('res.partner',string='Cooperator', required=True, readonly=True)
  283. partner_id_to = fields.Many2one('res.partner',string='Transfered to', readonly=True)
  284. date = fields.Date(string='Subscription Date', required= True, readonly=True)
  285. quantity = fields.Integer(string='Number of share', readonly=True)
  286. share_unit_price = fields.Float(string='Share price', readonly=True)
  287. total_amount_line = fields.Float(compute='_compute_total_line', string='Total amount line')
  288. share_product_id = fields.Many2one('product.product', string='Share type', required=True, readonly=True, domain=[('is_share','=',True)])
  289. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name', readonly=True)
  290. type = fields.Selection([('subscription','Subscription'),
  291. ('transfer','Transfer'),
  292. ('sell_back','Sell Back'),
  293. ('convert','Conversion')],
  294. string='Operation Type', readonly=True)
  295. company_id = fields.Many2one('res.company', string='Company', required=True,
  296. change_default=True, readonly=True,
  297. default=lambda self: self.env['res.company']._company_default_get())
  298. user_id = fields.Many2one('res.users', string='Responsible', readonly=True, default=lambda self: self.env.user)
  299. _order = "register_number_operation asc"