linea_deuda.py
4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
from odoo import models, fields, api, exceptions
from odoo.exceptions import UserError, ValidationError
from datetime import datetime 
from calendar import monthrange
class vnt_linea_deuda(models.Model):
    _name = 'vnt.linea_deuda'
    ld_factura = fields.Many2one(
        string='Factura',
        comodel_name='asw.comprobante',
        ondelete='restrict',
    ) 
    ld_fecha_vto = fields.Date(
        string=u'Fecha de Vencimiento',
        related='ld_factura.fecha_vencimiento_fact'
    )
    ld_estado = fields.Selection(
        string=u'Estado Factura',
        related='ld_factura.comp_estado',
    )
    ld_total = fields.Float(
        string=u'Total Factura',
        compute="_compute_ld_total",
    )
    ld_select = fields.Boolean(
        string="Seleccionar",
    )
    ld_nd = fields.Many2one(
        string='Nota Débito',
        comodel_name='asw.comprobante',
        ondelete='restrict',
    ) 
    ld_cobros = fields.Many2one(
        string='Cobros',
        comodel_name='vnt.cobros',
        ondelete='set null',
    )
    ld_interes = fields.Float(
        string=u'Total Interés',
        compute='_compute_ld_interes',
        store= True,
    )
    ld_fecha_calculo = fields.Date(
        string=u'Fecha de cálculo',
        default = datetime.today(),
    )
    ld_cobro = fields.Selection(
        string=u'Cobro',
        selection=[('todo', 'Cobro Total'), ('negociado', 'Cobro Negociado'), ('perdido', 'Interés perdido')], 
        readonly= True, 
    )
    #calculo del total de la factura a traves de comp_total 
    @api.depends('ld_factura')
    def _compute_ld_total(self):
        for record in self:
            record.ld_total = record.ld_factura.comp_total 
    @api.depends('ld_factura')
    def _compute_ld_interes(self):
        for record in self: 
            dias_mes = record.number_of_days_in_month(datetime.now().year, datetime.now().month)
            dias_anio = record.number_of_days_in_year(datetime.now().year)
            fmt = '%Y-%m-%d'
            d1= datetime.strptime(record.ld_fecha_vto, fmt)
            d2= datetime.strptime(record.ld_fecha_calculo, fmt)
            if d2>d1:
                dias_pasados= (d2 - d1).days
                cliente_id = record.ld_factura.comp_cliente.id
                config_int = self.env['vnt.interes'].search([('int_cliente', '=', cliente_id)], limit=1)
                tasa_an = config_int.int_tasa_anual
                tasa_diaria = tasa_an/dias_anio 
                if config_int.int_ritmo == 'mensual':
                    tasa_mensual = tasa_diaria * dias_mes
                    cociente = dias_pasados // dias_mes 
                    cociente =+ 1; 
                    int_calculado = cociente * tasa_mensual
                if config_int.int_ritmo == 'quincenal':
                    tasa_quincenal = tasa_diaria * 15
                    cociente = dias_pasados // 15 
                    cociente =+ 1; 
                    int_calculado = cociente * tasa_quincenal
                if config_int.int_ritmo == 'semanal':
                    tasa_semanal = tasa_diaria * 7
                    cociente = dias_pasados // 7 
                    cociente =+ 1; 
                    int_calculado = cociente * tasa_semanal
                if config_int.int_ritmo == 'diaria':
                    int_calculado = dias_pasados * tasa_diaria
            else:
                int_calculado = 0
            record.ld_interes = int_calculado
    #fx que devuelve nro de dias del mes
    def number_of_days_in_month(self, anio, mes):
        return monthrange(anio, mes)[1]
    #fx que devuelve nro de dias del anio
    def number_of_days_in_year(self, anio):
        suma = 0
        meses = range(1,13)
        for m in meses:
            suma += self.number_of_days_in_month(anio, m) 
        return suma 
    def cobrar_todo(self):
        self.ld_cobro = 'todo'
    def cobrar_negociado(self):
        self.ld_cobro = 'negociado'
        #abrir wizard con in_ld self id 
        datos = {'in_ld': self.id}
        nvo_w = self.env['vnt.interes_negociado'].create(datos)
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'vnt.interes_negociado',
            'view_mode': 'form',
            'view_type': 'form',
            'res_id': nvo_w.id,
            'target': 'new',
        }
    
    def interes_perdido(self):
        self.ld_cobro = 'perdido'
        self.ld_interes = 0 
    #no pude hacer funcionar la cosa como monetary, tuve q hacer el campo total como float 
    # ld_total = fields.Monetary(
    #     string=u'Total Factura',
    #     related='ld_factura.comp_total',
    # )
    # ld_moneda = fields.Many2one(
    #     string=u'Moneda',
    #     related='ld_factura.comp_moneda',
    # )
    # ld_moneda = fields.Many2one(
    #     string=u'Moneda',
    #     comodel_name='res.currency',
    #     ondelete='set null',
    #     default=lambda self: self.env.user.company_id.currency_id.id,
    # )