# -*- 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 AccountInvoice(models.Model): _inherit = 'account.invoice' date_emision = fields.Date("Fecha de Emisión") # def sale_convert_str_to_datetime(self, date): # return datetime.strptime(date, DEFAULT_SERVER_DATE_FORMAT) # # @api.model # def getSaleOrder(self,idOrder): # return[{ # 'id': order.id, # 'dateOrder': order.date_order.split(" ")[0], # 'dateQuota': order.date_emision or order.date_order.split(" ")[0], # } for order in self.env['sale.order'].browse(idOrder)] # @api.model # def getSaleOrderInterest(self,idOrder): # termConfig = self.env['accont.payments.term.config'].search([('compute', '=', True)]) # if (not termConfig): # return [] # # saleOrder = self.env['sale.order'].browse(idOrder) # if (not saleOrder): # return [] # dateSale = self.sale_convert_str_to_datetime(saleOrder.date_order.split(" ")[0]) # datefirst = self.sale_convert_str_to_datetime(saleOrder.date_quota or saleOrder.date_order.split(" ")[0]) # daysDiference = datefirst - dateSale # # orderLine = self.env['sale.order.line'].search([('order_id', '=', idOrder),('is_interest','=',True)]) # amountInteres = orderLine.price_subtotal or 0 # # return [{ # 'id': order.id, # 'amountTotal': (order.amount_total - amountInteres), # 'dateOrder': order.date_order.split(" ")[0], # 'dateQuota': order.date_quota or order.date_order.split(" ")[0], # 'amountInteres': round(daysDiference.days * ((order.amount_total - amountInteres) * (termConfig.interest_rate / 100)),order.pricelist_id.currency_id.decimal_places), # 'differenceDay': daysDiference.days, # 'messageDefault': termConfig.message_default, # '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 saleOrder] # ''' Discount ''' # @api.model # def account_Sale_line_add_discount(self,idOrder, newAmount): # saleOrder = self.env['sale.order'].browse(idOrder) # if (not saleOrder): # return {'state': False } # # discount = abs(saleOrder.amount_total - newAmount) # # discountLine = { # 'order_id': saleOrder.id, # 'name':"DESCUENTO APLICADO", # 'price_unit': - discount, # 'price_subtotal': - discount, # } # # line = self.env['sale.order.line'].create(discountLine) # if (not line): # return { 'state': False } # # return { 'state': True } # ''' Interest ''' # @api.model # def account_sale_line_interest(self, values): # interestLine = { # 'order_id': values['saleId'], # 'name':"Cambio de fecha de vencimiento, Diferencia("+str(values['differenceDay'])+") días.", # 'price_unit':values['amountInteres'], # 'price_subtotal':values['amountInteres'], # 'is_interest': True # } # # saleLine = self.env['sale.order.line'].search([('order_id', '=', values['saleId']),('is_interest','=',True)]) # # if (not saleLine): # if (values['amountInteres'] <= 0): # return { 'state': True } # # saleLine.create(interestLine) # return { 'state': True } # # if (values['amountInteres'] <= 0): # saleLine.unlink() # return { 'state': True } # # saleLine.write(interestLine) # return { 'state': True } # # @api.model # def calculatePaymentTermSaleOrder(self, values): # order = self.env['sale.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 # # ''' Payments Term ''' # @api.model # def accountPaymentTermConfiguratorSale(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', '=', 'sale')]) # 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'%('Ventas', resUser.name), # 'type_operation': 'sale', # 'user_term': resUser.id, # 'config': True, # 'line_ids': line, # }) # # ''' Actualizar condiciones de pago en la venta ''' # # order = self.env['sale.order'].browse(orderId) # if (order): # order.write({'payment_term': term.id}) # # return { # 'state': True, # 'message': 'Condiciones de pago configurado con éxito' # }