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.

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