Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
8313 Lượt xem

I hope I'll be clear enough to explain my problem. In account module we have account.payment.term and account.payment.term.line model that are related with one2many relation:

class AccountPaymentTerm(models.Model):
    _name = "account.payment.term"
    _description = "Payment Term"

    line_ids = fields.One2many('account.payment.term.line', 'payment_id', string='Terms', copy=True, default=_default_line_ids)
    period = fields.Selection([('month', '1 Month'),], string='Period', required=True, default='month', help="Select here the period between payments")
    how_much = fields.Float()
    fixed_amount = fields.Float()

class AccountPaymentTermLine(models.Model):
    _name = "account.payment.term.line"
    _description = "Payment Term Line"

    payment_id = fields.Many2one('account.payment.term', string='Payment Terms', required=True, index=True, ondelete='cascade')

I want to create a method in account.payment.term that creates automatically the payment term lines. This method should determine the number of slices number_of_slices = (self.how_much/self.fixed_amount) which will be the number of payment term lines. I tried this code for now:

def create(self):
    number_of_slices = (self.how_much/self.fixed_amount)
    if self.period == 'month':
        today = datetime.today()
        current_month = fields.Datetime(today.year, today.month, 1)
        i = 1
        while i <= number_of_slices:
            joker = {'value':'fixed', 
                     'value_amount':self.fixed_amount,
                     'days':30,
                     'option':'day_after_invoice_date',
                     'payment_id': self._origin.id}
            self.env['account.payment.term.line'].search([('payment_id','=',self.id)]).create(joker)
            i = i+1

This method doesn't seem to work. I got this error for now:

raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: account.payment.term(u'name', u'company_id', u'period', u'note', u'how_much', u'active', u'line_ids', u'fixed_amount')
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

I hope you are writing this code in account.payment.code. So you will get account.payment.code record in self. 

You don't need to search the record of account.payment.term.line, if you want to create new line for the current account.payment.term record.

Try this

def create(self):
    number_of_slices = (self.how_much/self.fixed_amount)
    if self.period == 'month':
        today = datetime.today()
        current_month = fields.Datetime(today.year, today.month, 1)
        i = 1
        while i <= number_of_slices:
            joker = {'value':'fixed', 
                     'value_amount':self.fixed_amount,
                     'days':30,
                     'option':'day_after_invoice_date',
                     'payment_id': self._origin.id}
            self.env['account.payment.term.line'].create(joker)
            i = i+1


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

Try to rewrite this line and see whether it will give you the solution or not,

 self.env['account.payment.term.line'].search([('payment_id','=',self.id)]).create(joker)

Change the above line to

 self.env['account.payment.term.line'].create(joker)


Also please see refer this : https://hilarlive.wordpress.com/2017/04/22/one2many-or-many2many-flags/

Thanks

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hello,

Try limit=1 in search method and try again. 

self.env['account.payment.term.line'].search([('payment_id','=',self.id)],limit=1).create(joker)


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 6 18
8622
1
thg 10 17
7653
1
thg 7 17
8073
0
thg 10 22
2326
2
thg 1 18
2894