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.

351 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. source = fields.Selection([('website','Website'),
  145. ('crm','CRM'),
  146. ('manual','Manual')], string="Source", default="website")
  147. _order = "id desc"
  148. def _prepare_invoice_line(self, product, partner, qty):
  149. self.ensure_one()
  150. res = {}
  151. account = product.property_account_income_id or product.categ_id.property_account_income_categ_id
  152. if not account:
  153. raise UserError(_('Please define income account for this product: "%s" (id:%d) - or for its category: "%s".') % \
  154. (product.name, product.id, product.categ_id.name))
  155. fpos = partner.property_account_position_id
  156. if fpos:
  157. account = fpos.map_account(account)
  158. res = {
  159. 'name': product.name,
  160. 'account_id': account.id,
  161. 'price_unit': product.lst_price,
  162. 'quantity': qty,
  163. 'uom_id': product.uom_id.id,
  164. 'product_id': product.id or False,
  165. }
  166. return res
  167. def send_capital_release_request(self, invoice):
  168. invoice_email_template = self.env['mail.template'].search([('name', '=', 'Request to Release Capital - Send by Email')])[0]
  169. # we send the email with the capital release request in attachment
  170. invoice_email_template.send_mail(invoice.id, True)
  171. invoice.sent = True
  172. def create_invoice(self, partner):
  173. # get subscription journal
  174. journal = self.env['account.journal'].search([('code','=','SUBJ')])[0]
  175. # get the account for associate
  176. # TODO this should be defined in configuration
  177. if self.company_id.property_cooperator_account:
  178. account = self.company_id.property_cooperator_account
  179. else:
  180. account = self.env['account.account'].search([('code','=','416000')])[0]
  181. # creating invoice and invoice lines
  182. invoice = self.env['account.invoice'].create({'partner_id':partner.id,
  183. 'journal_id':journal.id,'account_id':account.id,
  184. 'type': 'out_invoice', 'release_capital_request':True,
  185. 'subscription_request':self.id})
  186. vals = self._prepare_invoice_line(self.share_product_id, partner, self.ordered_parts)
  187. vals['invoice_id'] = invoice.id
  188. line = self.env['account.invoice.line'].create(vals)
  189. # validate the capital release request
  190. invoice.signal_workflow('invoice_open')
  191. self.send_capital_release_request(invoice)
  192. return invoice
  193. def get_partner_company_vals(self):
  194. # this should go to the many2many tag field
  195. #'title':'company',
  196. #self.env['res.partner.title'].search([('shortcut','=',self.company_type)])
  197. partner_vals = {'name':self.company_name, 'is_company': self.is_company,
  198. 'company_register_number':self.company_register_number, 'customer':False,
  199. 'cooperator':True, 'street':self.address, 'zip':self.zip_code,
  200. 'city': self.city,'email':self.email, 'out_inv_comm_type':'bba',
  201. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id, 'lang':self.lang}
  202. return partner_vals
  203. def get_partner_vals(self):
  204. partner_vals = {'name':self.name, 'first_name':self.firstname, 'last_name': self.lastname,
  205. 'customer':False, 'gender':self.gender,'cooperator':True, 'street':self.address,'zip':self.zip_code,
  206. 'city': self.city, 'phone': self.phone, 'email':self.email,
  207. 'national_register_number':self.no_registre, 'out_inv_comm_type':'bba',
  208. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id,
  209. 'lang':self.lang, 'birthdate':self.birthdate}
  210. return partner_vals
  211. def create_coop_partner(self):
  212. partner_obj = self.env['res.partner']
  213. if self.is_company:
  214. partner_vals = self.get_partner_company_vals()
  215. else:
  216. partner_vals = self.get_partner_vals()
  217. partner = partner_obj.create(partner_vals)
  218. if self.iban :
  219. self.env['res.partner.bank'].create({'partner_id':partner.id,'acc_number':self.iban})
  220. return partner
  221. @api.one
  222. def validate_subscription_request(self):
  223. partner_obj = self.env['res.partner']
  224. if self.partner_id:
  225. if not self.partner_id.cooperator:
  226. self.partner_id.cooperator = True
  227. partner = self.partner_id
  228. else:
  229. if self.already_cooperator:
  230. raise UserError(_('The checkbox already cooperator is checked please select a cooperator.'))
  231. elif self.is_company:
  232. partner = partner_obj.search([('company_register_number','=',self.company_register_number)])
  233. elif self.no_registre:
  234. partner = partner_obj.search([('national_register_number','=',self.no_registre)])
  235. else:
  236. partner = None
  237. if not partner:
  238. partner = self.create_coop_partner()
  239. if self.is_company:
  240. contact = partner_obj.search([('national_register_number','=',self.no_registre)])
  241. if not contact:
  242. contact_vals = {'name':self.name, 'first_name':self.firstname, 'last_name': self.lastname,
  243. 'customer':False, 'street':self.address,'zip':self.zip_code,
  244. 'city': self.city, 'phone': self.phone, 'email':self.email,
  245. 'national_register_number':self.no_registre, 'out_inv_comm_type':'bba',
  246. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id,
  247. 'lang':self.lang, 'birthdate':self.birthdate, 'parent_id': partner.id,'function':self.contact_person_function}
  248. contact = partner_obj.create(contact_vals)
  249. else:
  250. partner = partner[0]
  251. invoice = self.create_invoice(partner)
  252. self.write({'partner_id':partner.id, 'state':'done'})
  253. return invoice
  254. @api.one
  255. def block_subscription_request(self):
  256. self.write({'state':'block'})
  257. @api.one
  258. def unblock_subscription_request(self):
  259. self.write({'state':'draft'})
  260. @api.one
  261. def cancel_subscription_request(self):
  262. self.write({'state':'cancelled'})
  263. class share_line(models.Model):
  264. _name='share.line'
  265. @api.multi
  266. def _compute_total_line(self):
  267. res = {}
  268. for line in self:
  269. line.total_amount_line = line.share_unit_price * line.share_number
  270. return res
  271. share_product_id = fields.Many2one('product.product', string='Share type', required=True, readonly=True)
  272. share_number = fields.Integer(string='Number of Share', required=True, readonly=True)
  273. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name')
  274. share_unit_price = fields.Float(string='Share price', readonly=True)
  275. effective_date = fields.Date(string='Effective Date', readonly=True)
  276. partner_id = fields.Many2one('res.partner',string='Cooperator', required=True, ondelete='cascade', readonly=True)
  277. total_amount_line = fields.Float(compute='_compute_total_line', string='Total amount line')
  278. class subscription_register(models.Model):
  279. _name= 'subscription.register'
  280. @api.multi
  281. def _compute_total_line(self):
  282. res = {}
  283. for register_line in self:
  284. register_line.total_amount_line = register_line.share_unit_price * register_line.quantity
  285. name = fields.Char(string='Register Number Operation', required=True, readonly=True)
  286. register_number_operation = fields.Integer(string='Register Number Operation', required=True, readonly=True)
  287. partner_id = fields.Many2one('res.partner',string='Cooperator', required=True, readonly=True)
  288. partner_id_to = fields.Many2one('res.partner',string='Transfered to', readonly=True)
  289. date = fields.Date(string='Subscription Date', required= True, readonly=True)
  290. quantity = fields.Integer(string='Number of share', readonly=True)
  291. share_unit_price = fields.Float(string='Share price', readonly=True)
  292. total_amount_line = fields.Float(compute='_compute_total_line', string='Total amount line')
  293. share_product_id = fields.Many2one('product.product', string='Share type', required=True, readonly=True, domain=[('is_share','=',True)])
  294. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name', readonly=True)
  295. type = fields.Selection([('subscription','Subscription'),
  296. ('transfer','Transfer'),
  297. ('sell_back','Sell Back'),
  298. ('convert','Conversion')],
  299. string='Operation Type', readonly=True)
  300. company_id = fields.Many2one('res.company', string='Company', required=True,
  301. change_default=True, readonly=True,
  302. default=lambda self: self.env['res.company']._company_default_get())
  303. user_id = fields.Many2one('res.users', string='Responsible', readonly=True, default=lambda self: self.env.user)
  304. _order = "register_number_operation asc"