# -*- 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' }