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.

360 lines
18 KiB

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. import openerp.addons.decimal_precision as dp
  7. _REQUIRED = ['email','firstname','lastname','birthdate','address','share_product_id','ordered_parts','zip_code','city','iban','no_registre','gender'] # Could be improved including required from model
  8. @api.model
  9. def _lang_get(self):
  10. languages = self.env['res.lang'].search([])
  11. return [(language.code, language.name) for language in languages]
  12. class subscription_request(models.Model):
  13. _name = 'subscription.request'
  14. _description = 'Subscription Request'
  15. def get_required_field(self):
  16. return _REQUIRED
  17. @api.model
  18. def create(self,vals):
  19. if not vals.get('partner_id'):
  20. cooperator = False
  21. if vals.get('no_registre'):
  22. cooperator = self.env['res.partner'].get_cooperator_from_nin(vals.get('no_registre'))
  23. if cooperator:
  24. if cooperator.member:
  25. vals['type'] = 'increase'
  26. vals['already_cooperator'] = True
  27. else:
  28. vals['type'] = 'subscription'
  29. vals['partner_id'] = cooperator.id
  30. if not cooperator.cooperator:
  31. cooperator.cooperator = True
  32. subscr_request = super(subscription_request, self).create(vals)
  33. mail_template_obj = self.env['mail.template']
  34. confirmation_mail_template = mail_template_obj.search([('name', '=', 'Confirmation Email')])[0]
  35. confirmation_mail_template.send_mail(subscr_request.id, True)
  36. return subscr_request
  37. @api.model
  38. def create_comp_sub_req(self, vals):
  39. if not vals.get('partner_id'):
  40. cooperator = self.env['res.partner'].get_cooperator_from_crn(vals.get('company_register_number'))
  41. if cooperator:
  42. vals['partner_id'] = cooperator.id
  43. vals['type'] = 'increase'
  44. vals['already_cooperator'] = True
  45. subscr_request = super(subscription_request, self).create(vals)
  46. mail_template_obj = self.env['mail.template']
  47. confirmation_mail_template = mail_template_obj.search([('name', '=', 'Company Confirmation Email')])[0]
  48. confirmation_mail_template.send_mail(subscr_request.id, True)
  49. return subscr_request
  50. def check_belgian_identification_id(self, nat_register_num):
  51. if not self.check_empty_string(nat_register_num):
  52. return False
  53. if len(nat_register_num) != 11:
  54. return False
  55. if not nat_register_num.isdigit():
  56. return False
  57. birthday_number = nat_register_num[0:9]
  58. controle = nat_register_num[9:11]
  59. check_controle = 97 - (int(birthday_number) % 97)
  60. if int(check_controle) != int(controle):
  61. check_controle = 97 - ((2000000000 + int(birthday_number)) % 97)
  62. if int(check_controle) != int(controle):
  63. return False
  64. return True
  65. def check_empty_string(self, value):
  66. if value == None or value == False or value == '':
  67. return False
  68. return True
  69. @api.multi
  70. @api.depends('iban', 'no_registre','skip_control_ng')
  71. def _validated_lines(self):
  72. for sub_request in self:
  73. try:
  74. base_iban.validate_iban(sub_request.iban)
  75. sub_request.validated = True
  76. except ValidationError:
  77. sub_request.validated = False
  78. if not sub_request.is_company and (sub_request.skip_control_ng or self.check_belgian_identification_id(sub_request.no_registre)):
  79. sub_request.validated = True
  80. @api.multi
  81. @api.depends('share_product_id', 'share_product_id.list_price','ordered_parts')
  82. def _compute_subscription_amount(self):
  83. for sub_request in self:
  84. sub_request.subscription_amount = sub_request.share_product_id.list_price * sub_request.ordered_parts
  85. already_cooperator = fields.Boolean(string="I'm already cooperator")
  86. name = fields.Char(string='Name', required=True)
  87. firstname = fields.Char(string='Firstname')
  88. lastname = fields.Char(string='Lastname')
  89. birthdate = fields.Date(string="Birthdate")
  90. gender = fields.Selection([('male', _('Male')),
  91. ('female', _('Female')),
  92. ('other', _('Other'))], string='Gender')
  93. type = fields.Selection([('new','New Cooperator'),
  94. ('subscription','Subscription'),
  95. ('increase','Increase number of share')],
  96. string='Type', default="new")
  97. state = fields.Selection([('draft','Draft'),
  98. ('block','Blocked'),
  99. ('done','Done'),
  100. ('cancelled','Cancelled'),
  101. ('paid','paid')],
  102. string='State',required=True, default="draft")
  103. email = fields.Char(string='Email')
  104. iban = fields.Char(string='Account Number')
  105. partner_id = fields.Many2one('res.partner',string='Cooperator')
  106. share_product_id = fields.Many2one('product.product', string='Share type', domain=[('is_share','=',True)])
  107. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name')
  108. share_unit_price = fields.Float(related='share_product_id.list_price', string='Share price')
  109. subscription_amount = fields.Float(compute='_compute_subscription_amount', string='Subscription amount')
  110. ordered_parts = fields.Integer(string='Number of Share')
  111. address = fields.Char(string='Address')
  112. city = fields.Char(string='City')
  113. zip_code = fields.Char(string='Zip Code')
  114. country_id = fields.Many2one('res.country', string='Country', ondelete='restrict')
  115. phone = fields.Char(string='Phone')
  116. no_registre = fields.Char(string='National Register Number')
  117. user_id = fields.Many2one('res.users', string='Responsible', readonly=True)
  118. validated = fields.Boolean(compute='_validated_lines', string='Valid Line?', readonly=True)
  119. skip_control_ng = fields.Boolean(string="Skip control",
  120. help="if this field is checked then no control will be done on the national register number and on the iban bank account. To be done in case of the id card is from abroad or in case of a passport")
  121. lang = fields.Selection(_lang_get, 'Language', default='fr_BE',
  122. help="If the selected language is loaded in the system, all documents related to this contact will be printed in this language. If not, it will be English.")
  123. date = fields.Date(string='Subscription date request', default=lambda self: datetime.strftime(datetime.now(), '%Y-%m-%d'))
  124. company_id = fields.Many2one('res.company', string='Company', required=True,
  125. change_default=True, readonly=True,
  126. default=lambda self: self.env['res.company']._company_default_get())
  127. is_company = fields.Boolean(string='Is a company')
  128. is_operation = fields.Boolean(string='Is an operation')
  129. company_name = fields.Char(string="Company name")
  130. company_email = fields.Char(string="Company email")
  131. company_register_number = fields.Char(string='Company register number')
  132. company_type = fields.Selection([('scrl','SCRL'),
  133. ('asbl','ASBL'),
  134. ('sprl','SPRL'),
  135. ('sa','SA'),
  136. ('other','Other')])
  137. same_address = fields.Boolean(string='Same address')
  138. activities_address = fields.Char(string='Activities address')
  139. activities_city = fields.Char(string='Activities city')
  140. activities_zip_code = fields.Char(string='Activities zip Code')
  141. activities_country_id = fields.Many2one('res.country', string='Activities country', ondelete='restrict')
  142. contact_person_function = fields.Char(string='Function')
  143. operation_request_id = fields.Many2one('operation.request', string="Operation Request")
  144. is_operation = fields.Boolean(string="Is Operation request")
  145. capital_release_request = fields.One2many('account.invoice','subscription_request', string='Subscription request')
  146. source = fields.Selection([('website','Website'),
  147. ('crm','CRM'),
  148. ('manual','Manual')], string="Source", default="website")
  149. _order = "id desc"
  150. def _prepare_invoice_line(self, product, partner, qty):
  151. self.ensure_one()
  152. res = {}
  153. account = product.property_account_income_id or product.categ_id.property_account_income_categ_id
  154. if not account:
  155. raise UserError(_('Please define income account for this product: "%s" (id:%d) - or for its category: "%s".') % \
  156. (product.name, product.id, product.categ_id.name))
  157. fpos = partner.property_account_position_id
  158. if fpos:
  159. account = fpos.map_account(account)
  160. res = {
  161. 'name': product.name,
  162. 'account_id': account.id,
  163. 'price_unit': product.lst_price,
  164. 'quantity': qty,
  165. 'uom_id': product.uom_id.id,
  166. 'product_id': product.id or False,
  167. }
  168. return res
  169. def send_capital_release_request(self, invoice):
  170. invoice_email_template = self.env['mail.template'].search([('name', '=', 'Request to Release Capital - Send by Email')])[0]
  171. # we send the email with the capital release request in attachment
  172. invoice_email_template.send_mail(invoice.id, True)
  173. invoice.sent = True
  174. def create_invoice(self, partner):
  175. # get subscription journal
  176. journal = self.env['account.journal'].search([('code','=','SUBJ')])[0]
  177. # get the account for associate
  178. # TODO this should be defined in configuration
  179. if self.company_id.property_cooperator_account:
  180. account = self.company_id.property_cooperator_account
  181. else:
  182. account = self.env['account.account'].search([('code','=','416000')])[0]
  183. # creating invoice and invoice lines
  184. invoice = self.env['account.invoice'].create({'partner_id':partner.id,
  185. 'journal_id':journal.id,'account_id':account.id,
  186. 'type': 'out_invoice', 'release_capital_request':True,
  187. 'subscription_request':self.id})
  188. vals = self._prepare_invoice_line(self.share_product_id, partner, self.ordered_parts)
  189. vals['invoice_id'] = invoice.id
  190. line = self.env['account.invoice.line'].create(vals)
  191. # validate the capital release request
  192. invoice.signal_workflow('invoice_open')
  193. self.send_capital_release_request(invoice)
  194. return invoice
  195. def get_partner_company_vals(self):
  196. # this should go to the many2many tag field
  197. #'title':'company',
  198. #self.env['res.partner.title'].search([('shortcut','=',self.company_type)])
  199. partner_vals = {'name':self.company_name, 'is_company': self.is_company,
  200. 'company_register_number':self.company_register_number, 'customer':False,
  201. 'cooperator':True, 'street':self.address, 'zip':self.zip_code,
  202. 'city': self.city,'email':self.email, 'out_inv_comm_type':'bba',
  203. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id, 'lang':self.lang}
  204. return partner_vals
  205. def get_partner_vals(self):
  206. partner_vals = {'name':self.name, 'first_name':self.firstname, 'last_name': self.lastname,
  207. 'customer':False, 'gender':self.gender,'cooperator':True, 'street':self.address,'zip':self.zip_code,
  208. 'city': self.city, 'phone': self.phone, 'email':self.email,
  209. 'national_register_number':self.no_registre, 'out_inv_comm_type':'bba',
  210. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id,
  211. 'lang':self.lang, 'birthdate':self.birthdate}
  212. return partner_vals
  213. def create_coop_partner(self):
  214. partner_obj = self.env['res.partner']
  215. if self.is_company:
  216. partner_vals = self.get_partner_company_vals()
  217. else:
  218. partner_vals = self.get_partner_vals()
  219. partner = partner_obj.create(partner_vals)
  220. if self.iban :
  221. self.env['res.partner.bank'].create({'partner_id':partner.id,'acc_number':self.iban})
  222. return partner
  223. @api.one
  224. def validate_subscription_request(self):
  225. partner_obj = self.env['res.partner']
  226. if self.partner_id:
  227. if not self.partner_id.cooperator:
  228. self.partner_id.cooperator = True
  229. partner = self.partner_id
  230. else:
  231. if self.already_cooperator:
  232. raise UserError(_('The checkbox already cooperator is checked please select a cooperator.'))
  233. elif self.is_company:
  234. partner = partner_obj.search([('company_register_number','=',self.company_register_number)])
  235. elif self.no_registre:
  236. partner = partner_obj.search([('national_register_number','=',self.no_registre)])
  237. else:
  238. partner = None
  239. if not partner:
  240. partner = self.create_coop_partner()
  241. else:
  242. partner = partner[0]
  243. if self.is_company:
  244. contact = partner_obj.search([('national_register_number','=',self.no_registre)])
  245. if not contact:
  246. contact_vals = {'name':self.name, 'first_name':self.firstname, 'last_name': self.lastname,
  247. 'customer':False, 'is_company':False, 'street':self.address,'zip':self.zip_code,
  248. 'city': self.city, 'phone': self.phone, 'email':self.email,
  249. 'national_register_number':self.no_registre, 'out_inv_comm_type':'bba',
  250. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id,
  251. 'lang':self.lang, 'birthdate':self.birthdate, 'parent_id': partner.id,
  252. 'function':self.contact_person_function}
  253. contact = partner_obj.create(contact_vals)
  254. else:
  255. if contact.parent_id and contact.parent_id.id != partner.id:
  256. raise UserError(_('This contact person is already defined for another company. Please select another contact'))
  257. else:
  258. contact.parent_id = partner.id
  259. invoice = self.create_invoice(partner)
  260. self.write({'partner_id':partner.id, 'state':'done'})
  261. return invoice
  262. @api.one
  263. def block_subscription_request(self):
  264. self.write({'state':'block'})
  265. @api.one
  266. def unblock_subscription_request(self):
  267. self.write({'state':'draft'})
  268. @api.one
  269. def cancel_subscription_request(self):
  270. self.write({'state':'cancelled'})
  271. class share_line(models.Model):
  272. _name='share.line'
  273. @api.multi
  274. def _compute_total_line(self):
  275. res = {}
  276. for line in self:
  277. line.total_amount_line = line.share_unit_price * line.share_number
  278. return res
  279. share_product_id = fields.Many2one('product.product', string='Share type', required=True, readonly=True)
  280. share_number = fields.Integer(string='Number of Share', required=True, readonly=True)
  281. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name')
  282. share_unit_price = fields.Float(string='Share price', readonly=True)
  283. effective_date = fields.Date(string='Effective Date', readonly=True)
  284. partner_id = fields.Many2one('res.partner',string='Cooperator', required=True, ondelete='cascade', readonly=True)
  285. total_amount_line = fields.Float(compute='_compute_total_line', string='Total amount line')
  286. class subscription_register(models.Model):
  287. _name= 'subscription.register'
  288. @api.multi
  289. def _compute_total_line(self):
  290. res = {}
  291. for register_line in self:
  292. register_line.total_amount_line = register_line.share_unit_price * register_line.quantity
  293. name = fields.Char(string='Register Number Operation', required=True, readonly=True)
  294. register_number_operation = fields.Integer(string='Register Number Operation', required=True, readonly=True)
  295. partner_id = fields.Many2one('res.partner',string='Cooperator', required=True, readonly=True)
  296. partner_id_to = fields.Many2one('res.partner',string='Transfered to', readonly=True)
  297. date = fields.Date(string='Subscription Date', required= True, readonly=True)
  298. quantity = fields.Integer(string='Number of share', readonly=True)
  299. share_unit_price = fields.Float(string='Share price', readonly=True)
  300. total_amount_line = fields.Float(compute='_compute_total_line', string='Total amount line')
  301. share_product_id = fields.Many2one('product.product', string='Share type', required=True, readonly=True, domain=[('is_share','=',True)])
  302. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name', readonly=True)
  303. type = fields.Selection([('subscription','Subscription'),
  304. ('transfer','Transfer'),
  305. ('sell_back','Sell Back'),
  306. ('convert','Conversion')],
  307. string='Operation Type', readonly=True)
  308. company_id = fields.Many2one('res.company', string='Company', required=True,
  309. change_default=True, readonly=True,
  310. default=lambda self: self.env['res.company']._company_default_get())
  311. user_id = fields.Many2one('res.users', string='Responsible', readonly=True, default=lambda self: self.env.user)
  312. _order = "register_number_operation asc"