123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- # -*- coding: utf-8 -*-
- from openerp import api, fields, models
- from openerp.exceptions import except_orm
- from datetime import datetime, timedelta
- from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
- from dateutil.relativedelta import relativedelta
- class PurchaseOrder (models.Model):
- _inherit = 'purchase.order'
- date_quota = fields.Date("Fecha de la primera cuota")
- def sale_convert_str_to_datetime(self, date):
- return datetime.strptime(date, DEFAULT_SERVER_DATE_FORMAT)
- ''' Get Purchase '''
- @api.model
- def getPurchaseOrder(self,idOrder):
- return[{
- 'id': order.id,
- 'amountTotal': order.amount_total,
- 'dateOrder': order.date_order.split(" ")[0],
- 'dateQuota': order.date_quota or order.date_order.split(" ")[0],
- 'currency': {
- 'id': order.pricelist_id.currency_id.id,
- 'symbol': order.pricelist_id.currency_id.symbol,
- 'decimalSeparator': order.pricelist_id.currency_id.decimal_separator,
- 'decimalPlaces': order.pricelist_id.currency_id.decimal_places,
- 'thousandsSeparator': order.pricelist_id.currency_id.thousands_separator,
- }
- } for order in self.env['purchase.order'].browse(idOrder)]
- ''' CALCULATOR '''
- @api.model
- def calculatePaymentTermPurchaseOrder(self, values):
- order = self.env['purchase.order'].browse(values['orderId'])
- if (not order):
- return False
- dateSale = self.sale_convert_str_to_datetime(order.date_order.split(" ")[0])
- datefirst = self.sale_convert_str_to_datetime(order.date_quota or order.date_order.split(" ")[0])
- amountTotal = order.amount_total
- amountPayments = values['amountPayments']
- amountResidual = amountTotal - amountPayments
- cuotaCant = (amountResidual /values['amountCuota'])
- if (amountPayments > 0):
- cuotaCant +=1
- termCalculator = []
- cuotaTotal= int(cuotaCant) if ((cuotaCant - int(cuotaCant)) <= 0) else int(cuotaCant)+1
- termType = self.env['account.payment.term.type'].browse(values['typeTerm'])
- if (not termType):
- return False
- numberCuota = 0
- for cuota in xrange(int(cuotaCant)):
- numberCuota = cuota+1
- amount = values['amountCuota']
- adddaysMaturity = datefirst - dateSale
- date = datefirst
- if (numberCuota == 1 and (amountPayments > 0)):
- amount = amountPayments
- if (adddaysMaturity.days > 0):
- adddaysMaturity = dateSale - dateSale
- date = dateSale
- amountTotal -= amount
- termCalculator.append({
- 'number' : numberCuota,
- 'cuotaNumber': str(numberCuota)+"/"+str(cuotaTotal),
- 'date': date.strftime(DEFAULT_SERVER_DATE_FORMAT),
- 'amount': amount,
- 'days': adddaysMaturity.days,
- 'value': 'fixed' if (numberCuota < cuotaTotal) else 'balance'
- })
- days = datefirst - dateSale
- if(numberCuota == 1 and (amountPayments > 0) and days.days > 0 ):
- continue
- if (termType.type_calculation == 'month' ):
- datefirst += relativedelta(months=termType.qty_add)
- else :
- datefirst += timedelta(days=termType.qty_add)
- if (amountTotal > 0 ):
- numberCuota +=1
- adddaysMaturity = datefirst - dateSale
- termCalculator.append({
- 'number' : numberCuota,
- 'cuotaNumber': str(numberCuota)+"/"+str(cuotaTotal),
- 'date': datefirst.strftime(DEFAULT_SERVER_DATE_FORMAT),
- 'amount': amountTotal,
- 'days': adddaysMaturity.days,
- 'value': 'fixed' if (numberCuota < cuotaTotal) else 'balance'
- })
- return termCalculator
- ''' Config Term '''
- @api.model
- def accountPaymentTermConfigurator(self, values, orderId):
- resUser = self.env.user
- if (not resUser):
- return {
- 'state': False,
- 'message': 'No fue posible localizar el usuario.'
- }
- term = self.env['account.payment.term'].search([('config', '=', True),('user_term', '=', resUser.id),('type_operation', '=', 'purchase')])
- if (term):
- term.unlink()
- line = []
- for configTerm in values:
- line.append([0,False,{
- 'payment_id': term.id,
- 'value': configTerm['value'],
- 'days': configTerm['days'],
- 'value_amount': - configTerm['amount'] if (configTerm['value'] == 'fixed') else 0
- }])
- term = self.env['account.payment.term'].create({
- 'name': 'Plazos Configurables %s - %s'%('Compras', resUser.name),
- 'type_operation': 'purchase',
- 'user_term': resUser.id,
- 'config': True,
- 'line_ids': line,
- })
- ''' Actualizar condiciones de pago en la purchase '''
- order = self.env['purchase.order'].browse(orderId)
- if (order):
- order.write({'payment_term_id': term.id})
- return {
- 'state': True,
- 'message': 'Condiciones de pago configurado con éxito'
- }
|