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.

660 lines
31 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  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. _REQUIRED = ['email',
  7. 'firstname',
  8. 'lastname',
  9. 'birthdate',
  10. 'address',
  11. 'share_product_id',
  12. 'ordered_parts',
  13. 'zip_code',
  14. 'city',
  15. 'iban',
  16. 'no_registre',
  17. 'gender'] # Could be improved including required from model
  18. @api.model
  19. def _lang_get(self):
  20. languages = self.env['res.lang'].search([])
  21. return [(language.code, language.name) for language in languages]
  22. class subscription_request(models.Model):
  23. _name = 'subscription.request'
  24. _description = 'Subscription Request'
  25. def get_required_field(self):
  26. return _REQUIRED
  27. @api.model
  28. def create(self, vals):
  29. partner_obj = self.env['res.partner']
  30. if not vals.get('partner_id'):
  31. cooperator = False
  32. if vals.get('no_registre'):
  33. cooperator = partner_obj.get_cooperator_from_nin(
  34. vals.get('no_registre'))
  35. if cooperator:
  36. # TODO remove the following line of code once it has
  37. # been founded a way to avoid dubble entry
  38. cooperator = cooperator[0]
  39. if cooperator.member:
  40. vals['type'] = 'increase'
  41. vals['already_cooperator'] = True
  42. else:
  43. vals['type'] = 'subscription'
  44. vals['partner_id'] = cooperator.id
  45. if not cooperator.cooperator:
  46. cooperator.write({'cooperator': True})
  47. else:
  48. cooperator_id = vals.get('partner_id')
  49. cooperator = partner_obj.browse(cooperator_id)
  50. if cooperator.member:
  51. vals['type'] = 'increase'
  52. vals['already_cooperator'] = True
  53. subscr_request = super(subscription_request, self).create(vals)
  54. confirmation_mail_template = self.env.ref('easy_my_coop.email_template_confirmation', False)
  55. confirmation_mail_template.send_mail(subscr_request.id)
  56. return subscr_request
  57. @api.model
  58. def create_comp_sub_req(self, vals):
  59. if not vals.get('partner_id'):
  60. cooperator = self.env['res.partner'].get_cooperator_from_crn(vals.get('company_register_number'))
  61. if cooperator:
  62. vals['partner_id'] = cooperator.id
  63. vals['type'] = 'increase'
  64. vals['already_cooperator'] = True
  65. subscr_request = super(subscription_request, self).create(vals)
  66. confirmation_mail_template = self.env.ref('easy_my_coop.email_template_confirmation_company', False)
  67. confirmation_mail_template.send_mail(subscr_request.id, True)
  68. return subscr_request
  69. def check_belgian_identification_id(self, nat_register_num):
  70. if not self.check_empty_string(nat_register_num):
  71. return False
  72. if len(nat_register_num) != 11:
  73. return False
  74. if not nat_register_num.isdigit():
  75. return False
  76. birthday_number = nat_register_num[0:9]
  77. controle = nat_register_num[9:11]
  78. check_controle = 97 - (int(birthday_number) % 97)
  79. if int(check_controle) != int(controle):
  80. check_controle = 97 - ((2000000000 + int(birthday_number)) % 97)
  81. if int(check_controle) != int(controle):
  82. return False
  83. return True
  84. def check_empty_string(self, value):
  85. if value is None or value is False or value == '':
  86. return False
  87. return True
  88. @api.multi
  89. @api.depends('iban', 'no_registre', 'skip_control_ng', 'is_company')
  90. def _validated_lines(self):
  91. for sub_request in self:
  92. validated = False
  93. try:
  94. base_iban.validate_iban(sub_request.iban)
  95. validated = True
  96. except ValidationError:
  97. validated = False
  98. # if not sub_request.is_company:
  99. if sub_request.skip_control_ng \
  100. or self.check_belgian_identification_id(
  101. sub_request.no_registre):
  102. validated = True
  103. else:
  104. validated = False
  105. sub_request.validated = validated
  106. @api.multi
  107. @api.depends('share_product_id',
  108. 'share_product_id.list_price',
  109. 'ordered_parts')
  110. def _compute_subscription_amount(self):
  111. for sub_request in self:
  112. sub_request.subscription_amount = sub_request.share_product_id.list_price * sub_request.ordered_parts
  113. already_cooperator = fields.Boolean(string="I'm already cooperator",
  114. readonly=True,
  115. states={'draft': [('readonly', False)]})
  116. name = fields.Char(string='Name',
  117. required=True,
  118. readonly=True,
  119. states={'draft': [('readonly', False)]})
  120. firstname = fields.Char(string='Firstname',
  121. readonly=True,
  122. states={'draft': [('readonly', False)]})
  123. lastname = fields.Char(string='Lastname',
  124. readonly=True,
  125. states={'draft': [('readonly', False)]})
  126. birthdate = fields.Date(string="Birthdate",
  127. readonly=True,
  128. states={'draft': [('readonly', False)]})
  129. gender = fields.Selection([('male', _('Male')),
  130. ('female', _('Female')),
  131. ('other', _('Other'))],
  132. string='Gender',
  133. readonly=True,
  134. states={'draft': [('readonly', False)]})
  135. type = fields.Selection([('new', 'New Cooperator'),
  136. ('subscription', 'Subscription'),
  137. ('increase', 'Increase number of share')],
  138. string='Type', default="new",
  139. readonly=True,
  140. states={'draft': [('readonly', False)]})
  141. state = fields.Selection([('draft', 'Draft'),
  142. ('block', 'Blocked'),
  143. ('done', 'Done'),
  144. ('waiting', 'Waiting'),
  145. ('transfer', 'Transfer'),
  146. ('cancelled', 'Cancelled'),
  147. ('paid', 'paid')],
  148. string='State', required=True, default="draft")
  149. email = fields.Char(string='Email',
  150. required=True,
  151. readonly=True,
  152. states={'draft': [('readonly', False)]})
  153. iban = fields.Char(string='Account Number',
  154. readonly=True,
  155. states={'draft': [('readonly', False)]})
  156. partner_id = fields.Many2one('res.partner',
  157. string='Cooperator',
  158. readonly=True,
  159. states={'draft': [('readonly', False)]})
  160. share_product_id = fields.Many2one('product.product',
  161. string='Share type',
  162. domain=[('is_share', '=', True)],
  163. required=True,
  164. readonly=True,
  165. states={'draft': [('readonly', False)]})
  166. share_short_name = fields.Char(related='share_product_id.short_name',
  167. string='Share type name',
  168. readonly=True,
  169. states={'draft': [('readonly', False)]})
  170. share_unit_price = fields.Float(related='share_product_id.list_price',
  171. string='Share price',
  172. readonly=True,
  173. states={'draft': [('readonly', False)]})
  174. subscription_amount = fields.Float(compute='_compute_subscription_amount',
  175. string='Subscription amount',
  176. readonly=True,
  177. states={'draft': [('readonly', False)]})
  178. ordered_parts = fields.Integer(string='Number of Share',
  179. required=True,
  180. readonly=True,
  181. states={'draft': [('readonly', False)]})
  182. address = fields.Char(string='Address',
  183. required=True,
  184. readonly=True,
  185. states={'draft': [('readonly', False)]})
  186. city = fields.Char(string='City',
  187. required=True,
  188. readonly=True,
  189. states={'draft': [('readonly', False)]})
  190. zip_code = fields.Char(string='Zip Code',
  191. required=True,
  192. readonly=True,
  193. states={'draft': [('readonly', False)]})
  194. country_id = fields.Many2one('res.country',
  195. string='Country',
  196. ondelete='restrict',
  197. required=True,
  198. readonly=True,
  199. states={'draft': [('readonly', False)]})
  200. phone = fields.Char(string='Phone',
  201. readonly=True,
  202. states={'draft': [('readonly', False)]})
  203. no_registre = fields.Char(string='National Register Number',
  204. readonly=True,
  205. states={'draft': [('readonly', False)]})
  206. user_id = fields.Many2one('res.users',
  207. string='Responsible',
  208. readonly=True)
  209. validated = fields.Boolean(compute='_validated_lines',
  210. string='Valid Line?',
  211. readonly=True)
  212. skip_control_ng = fields.Boolean(string="Skip control",
  213. help="if this field is checked then no"
  214. " control will be done on the national"
  215. " register number and on the iban bank"
  216. " account. To be done in case of the id"
  217. " card is from abroad or in case of"
  218. " a passport",
  219. readonly=True,
  220. states={'draft': [('readonly', False)]})
  221. lang = fields.Selection(_lang_get,
  222. string='Language',
  223. required=True,
  224. readonly=True,
  225. states={'draft': [('readonly', False)]},
  226. default=lambda self: self.env['res.company']._company_default_get().default_lang_id.code)
  227. date = fields.Date(string='Subscription date request',
  228. required=True,
  229. readonly=True,
  230. states={'draft': [('readonly', False)]},
  231. default=lambda self: datetime.strftime(datetime.now(), '%Y-%m-%d'))
  232. company_id = fields.Many2one('res.company', string='Company', required=True,
  233. change_default=True,
  234. readonly=True,
  235. default=lambda self: self.env['res.company']._company_default_get())
  236. is_company = fields.Boolean(string='Is a company',
  237. readonly=True,
  238. states={'draft': [('readonly', False)]})
  239. is_operation = fields.Boolean(string='Is an operation',
  240. readonly=True,
  241. states={'draft': [('readonly', False)]})
  242. company_name = fields.Char(string="Company name",
  243. readonly=True,
  244. states={'draft': [('readonly', False)]})
  245. company_email = fields.Char(string="Company email",
  246. readonly=True,
  247. states={'draft': [('readonly', False)]})
  248. company_register_number = fields.Char(string='Company register number',
  249. readonly=True,
  250. states={'draft': [('readonly', False)]})
  251. company_type = fields.Selection([('scrl', 'SCRL'),
  252. ('asbl', 'ASBL'),
  253. ('sprl', 'SPRL'),
  254. ('sa', 'SA'),
  255. ('other', 'Other')],
  256. string="Company type",
  257. readonly=True,
  258. states={'draft': [('readonly', False)]})
  259. same_address = fields.Boolean(string='Same address',
  260. readonly=True,
  261. states={'draft': [('readonly', False)]})
  262. activities_address = fields.Char(string='Activities address',
  263. readonly=True,
  264. states={'draft': [('readonly', False)]})
  265. activities_city = fields.Char(string='Activities city',
  266. readonly=True,
  267. states={'draft': [('readonly', False)]})
  268. activities_zip_code = fields.Char(string='Activities zip Code',
  269. readonly=True,
  270. states={'draft': [('readonly', False)]})
  271. activities_country_id = fields.Many2one('res.country',
  272. string='Activities country',
  273. ondelete='restrict',
  274. readonly=True,
  275. states={'draft': [('readonly', False)]})
  276. contact_person_function = fields.Char(string='Function',
  277. readonly=True,
  278. states={'draft': [('readonly', False)]})
  279. operation_request_id = fields.Many2one('operation.request',
  280. string="Operation Request",
  281. readonly=True,
  282. states={'draft': [('readonly', False)]})
  283. capital_release_request = fields.One2many('account.invoice',
  284. 'subscription_request',
  285. string='Capital release request',
  286. readonly=True,
  287. states={'draft': [('readonly', False)]})
  288. capital_release_request_date = fields.Date(string="Force the capital release request date",
  289. help="Keep empty to use the current date",
  290. copy=False,
  291. readonly=True,
  292. states={'draft': [('readonly', False)]})
  293. source = fields.Selection([('website', 'Website'),
  294. ('crm', 'CRM'),
  295. ('manual', 'Manual'),
  296. ('operation', 'Operation')],
  297. string="Source",
  298. default="website",
  299. readonly=True,
  300. states={'draft': [('readonly', False)]})
  301. _order = "id desc"
  302. # declare this function in order to be overriden
  303. def get_eater_vals(self, partner, share_product_id):
  304. return {}
  305. def _prepare_invoice_line(self, product, partner, qty):
  306. self.ensure_one()
  307. account = product.property_account_income_id \
  308. or product.categ_id.property_account_income_categ_id
  309. if not account:
  310. raise UserError(_('Please define income account for this product:'
  311. ' "%s" (id:%d) - or for its category: "%s".') %
  312. (product.name, product.id, product.categ_id.name))
  313. fpos = partner.property_account_position_id
  314. if fpos:
  315. account = fpos.map_account(account)
  316. res = {
  317. 'name': product.name,
  318. 'account_id': account.id,
  319. 'price_unit': product.lst_price,
  320. 'quantity': qty,
  321. 'uom_id': product.uom_id.id,
  322. 'product_id': product.id or False,
  323. }
  324. return res
  325. def send_capital_release_request(self, invoice):
  326. invoice_email_template = self.env['mail.template'].search([('name', '=', 'Request to Release Capital - Send by Email')])[0]
  327. # we send the email with the capital release request in attachment
  328. invoice_email_template.send_mail(invoice.id, True)
  329. invoice.sent = True
  330. def create_invoice(self, partner):
  331. # get subscription journal
  332. journal = self.env['account.journal'].search([('code', '=', 'SUBJ')])[0]
  333. # get the account for associate
  334. # TODO this should be defined in configuration
  335. if self.company_id.property_cooperator_account:
  336. account = self.company_id.property_cooperator_account
  337. else:
  338. account = self.env['account.account'].search([('code', '=', '416000')])[0]
  339. # creating invoice and invoice lines
  340. invoice_vals = {'partner_id': partner.id,
  341. 'journal_id': journal.id,
  342. 'account_id': account.id,
  343. 'type': 'out_invoice',
  344. 'release_capital_request': True,
  345. 'subscription_request': self.id}
  346. if self.capital_release_request_date:
  347. invoice_vals['date_invoice'] = self.capital_release_request_date
  348. invoice = self.env['account.invoice'].create(invoice_vals)
  349. vals = self._prepare_invoice_line(self.share_product_id, partner,
  350. self.ordered_parts)
  351. vals['invoice_id'] = invoice.id
  352. self.env['account.invoice.line'].create(vals)
  353. # validate the capital release request
  354. invoice.signal_workflow('invoice_open')
  355. self.send_capital_release_request(invoice)
  356. return invoice
  357. def get_partner_company_vals(self):
  358. partner_vals = {'name': self.company_name,
  359. 'last_name': self.company_name,
  360. 'is_company': self.is_company,
  361. 'company_register_number': self.company_register_number, #noqa
  362. 'customer': False, 'cooperator': True,
  363. 'street': self.address, 'zip': self.zip_code,
  364. 'city': self.city, 'email': self.company_email,
  365. 'out_inv_comm_type': 'bba',
  366. 'customer': self.share_product_id.customer,
  367. 'out_inv_comm_algorithm': 'random',
  368. 'country_id': self.country_id.id,
  369. 'lang': self.lang}
  370. return partner_vals
  371. def get_partner_vals(self):
  372. partner_vals = {'name': self.name, 'first_name': self.firstname,
  373. 'last_name': self.lastname, 'street': self.address,
  374. 'zip': self.zip_code, 'email': self.email,
  375. 'gender': self.gender, 'cooperator': True,
  376. 'city': self.city, 'phone': self.phone,
  377. 'national_register_number': self.no_registre,
  378. 'out_inv_comm_type': 'bba',
  379. 'out_inv_comm_algorithm': 'random',
  380. 'country_id': self.country_id.id, 'lang': self.lang,
  381. 'birthdate_date': self.birthdate,
  382. 'customer': self.share_product_id.customer}
  383. return partner_vals
  384. def create_coop_partner(self):
  385. partner_obj = self.env['res.partner']
  386. if self.is_company:
  387. partner_vals = self.get_partner_company_vals()
  388. else:
  389. partner_vals = self.get_partner_vals()
  390. partner = partner_obj.create(partner_vals)
  391. if self.iban:
  392. self.env['res.partner.bank'].create({
  393. 'partner_id': partner.id,
  394. 'acc_number': self.iban
  395. })
  396. return partner
  397. def create_user(self, partner):
  398. user_obj = self.env['res.users']
  399. # TODO replace self by partner
  400. email = self.email
  401. if self.is_company:
  402. email = self.company_email
  403. user = user_obj.search([('login', '=', email)])
  404. if not user:
  405. user_values = {'partner_id': partner.id, 'login': email}
  406. user_id = user_obj.sudo()._signup_create_user(user_values)
  407. user = user_obj.browse(user_id)
  408. user.sudo().with_context({'create_user': True}).action_reset_password()
  409. return True
  410. @api.one
  411. def validate_subscription_request(self):
  412. partner_obj = self.env['res.partner']
  413. if self.ordered_parts <= 0:
  414. raise UserError(_('Number of share must be greater than 0.'))
  415. if self.partner_id:
  416. if not self.partner_id.cooperator:
  417. self.partner_id.cooperator = True
  418. partner = self.partner_id
  419. else:
  420. if self.already_cooperator:
  421. raise UserError(_('The checkbox already cooperator is'
  422. ' checked please select a cooperator.'))
  423. elif self.is_company and self.company_register_number:
  424. partner = partner_obj.search([('company_register_number', '=', self.company_register_number)])
  425. elif self.no_registre:
  426. partner = partner_obj.search([('national_register_number', '=', self.no_registre)])
  427. else:
  428. partner = None
  429. if not partner:
  430. partner = self.create_coop_partner()
  431. else:
  432. partner = partner[0]
  433. if self.is_company and not partner.has_representative():
  434. contact = False
  435. if self.no_registre:
  436. contact = partner_obj.search([('national_register_number', '=', self.no_registre)])
  437. if contact:
  438. contact.type = 'representative'
  439. if not contact:
  440. contact_vals = {'name': self.name,
  441. 'first_name': self.firstname,
  442. 'last_name': self.lastname, 'customer': False,
  443. 'is_company': False, 'cooperator': True,
  444. 'street': self.address, 'gender': self.gender,
  445. 'zip': self.zip_code, 'city': self.city,
  446. 'phone': self.phone, 'email': self.email,
  447. 'national_register_number': self.no_registre,
  448. 'country_id': self.country_id.id,
  449. 'out_inv_comm_type': 'bba',
  450. 'out_inv_comm_algorithm': 'random',
  451. 'lang': self.lang,
  452. 'birthdate_date': self.birthdate,
  453. 'parent_id': partner.id,
  454. 'representative': True,
  455. 'function': self.contact_person_function,
  456. 'type': 'representative'}
  457. contact = partner_obj.create(contact_vals)
  458. else:
  459. if len(contact) > 1:
  460. raise UserError(_('There is two different persons with the'
  461. ' same national register number. Please'
  462. ' proceed to a merge before to continue')
  463. )
  464. if contact.parent_id and contact.parent_id.id != partner.id:
  465. raise UserError(_('This contact person is already defined'
  466. ' for another company. Please select'
  467. ' another contact'))
  468. else:
  469. contact.write({'parent_id': partner.id,
  470. 'representative': True})
  471. invoice = self.create_invoice(partner)
  472. self.write({'partner_id': partner.id, 'state': 'done'})
  473. self.create_user(partner)
  474. return invoice
  475. @api.one
  476. def block_subscription_request(self):
  477. self.write({'state': 'block'})
  478. @api.one
  479. def unblock_subscription_request(self):
  480. self.write({'state': 'draft'})
  481. @api.one
  482. def cancel_subscription_request(self):
  483. self.write({'state': 'cancelled'})
  484. @api.one
  485. def put_on_waiting_list(self):
  486. self.write({'state': 'waiting'})
  487. class share_line(models.Model):
  488. _name = 'share.line'
  489. @api.multi
  490. def _compute_total_line(self):
  491. res = {}
  492. for line in self:
  493. line.total_amount_line = line.share_unit_price * line.share_number
  494. return res
  495. share_product_id = fields.Many2one('product.product',
  496. string='Share type',
  497. required=True,
  498. readonly=True)
  499. share_number = fields.Integer(string='Number of Share',
  500. required=True,
  501. readonly=True)
  502. share_short_name = fields.Char(related='share_product_id.short_name',
  503. string='Share type name',
  504. readonly=True)
  505. share_unit_price = fields.Float(string='Share price',
  506. readonly=True)
  507. effective_date = fields.Date(string='Effective Date',
  508. readonly=True)
  509. partner_id = fields.Many2one('res.partner',
  510. string='Cooperator',
  511. required=True,
  512. ondelete='cascade',
  513. readonly=True)
  514. total_amount_line = fields.Float(compute='_compute_total_line',
  515. string='Total amount line')
  516. class subscription_register(models.Model):
  517. _name = 'subscription.register'
  518. @api.multi
  519. def _compute_total_line(self):
  520. for register_line in self:
  521. register_line.total_amount_line = register_line.share_unit_price * register_line.quantity
  522. name = fields.Char(string='Register Number Operation',
  523. required=True,
  524. readonly=True)
  525. register_number_operation = fields.Integer(string='Register Number Operation',
  526. required=True,
  527. readonly=True)
  528. partner_id = fields.Many2one('res.partner',
  529. string='Cooperator',
  530. required=True,
  531. readonly=True)
  532. partner_id_to = fields.Many2one('res.partner',
  533. string='Transfered to',
  534. readonly=True)
  535. date = fields.Date(string='Subscription Date',
  536. required=True,
  537. readonly=True)
  538. quantity = fields.Integer(string='Number of share',
  539. readonly=True)
  540. share_unit_price = fields.Float(string='Share price',
  541. readonly=True)
  542. total_amount_line = fields.Float(compute='_compute_total_line',
  543. string='Total amount line')
  544. share_product_id = fields.Many2one('product.product',
  545. string='Share type',
  546. required=True,
  547. readonly=True,
  548. domain=[('is_share', '=', True)])
  549. share_short_name = fields.Char(related='share_product_id.short_name',
  550. string='Share type name',
  551. readonly=True)
  552. share_to_product_id = fields.Many2one('product.product',
  553. string='Share to type',
  554. readonly=True,
  555. domain=[('is_share', '=', True)])
  556. share_to_short_name = fields.Char(related='share_to_product_id.short_name',
  557. string='Share to type name',
  558. readonly=True)
  559. quantity_to = fields.Integer(string='Number of share to',
  560. readonly=True)
  561. share_to_unit_price = fields.Float(string='Share to price',
  562. readonly=True)
  563. type = fields.Selection([('subscription', 'Subscription'),
  564. ('transfer', 'Transfer'),
  565. ('sell_back', 'Sell Back'),
  566. ('convert', 'Conversion')],
  567. string='Operation Type', readonly=True)
  568. company_id = fields.Many2one('res.company', string='Company',
  569. required=True,
  570. change_default=True, readonly=True,
  571. default=lambda self: self.env['res.company']._company_default_get())
  572. user_id = fields.Many2one('res.users',
  573. string='Responsible',
  574. readonly=True,
  575. default=lambda self: self.env.user)
  576. _order = "register_number_operation asc"
  577. @api.model
  578. def read_group(self, domain, fields, groupby, offset=0, limit=None,
  579. orderby=False,
  580. lazy=True):
  581. if 'share_unit_price' in fields:
  582. fields.remove('share_unit_price')
  583. if 'register_number_operation' in fields:
  584. fields.remove('register_number_operation')
  585. res = super(subscription_register, self).read_group(domain, fields,
  586. groupby,
  587. offset=offset,
  588. limit=limit,
  589. orderby=orderby,
  590. lazy=lazy)
  591. if 'total_amount_line' in fields:
  592. for line in res:
  593. if '__domain' in line:
  594. lines = self.search(line['__domain'])
  595. inv_value = 0.0
  596. for line2 in lines:
  597. inv_value += line2.total_amount_line
  598. line['total_amount_line'] = inv_value
  599. return res