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.

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