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.

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