This question has been flagged
3143 Views

How to create the invoicelines for the rest of the downpayments in odoo12 community Version

Here in my module I have created the invoice lines for the downpayment initially . After that I cant create the invoice_lines for the rest of the amount...

Here is my code...

Model:-

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class ResPartner(models.Model):
    _inherit = 'res.partner'
   
    default_downpay_mode = fields.Boolean(string = 'Default Downpayment')
    default_downpay_amount = fields.Monetary(string = 'Amount')
   
   
class SaleOrder(models.Model):
    _inherit = 'sale.order'
   
    @api.multi
    def action_confirm(self):
        res = super(SaleOrder, self).action_confirm()
        self.env['account.invoice'].create({
                            'type': 'out_invoice',
                            'partner_id': self.partner_id.id,
                            'user_id': self.env.user.id,
                            'origin': self.name,
                            'invoice_line_ids': [(0, 0, {
                                'name': 'DownPayment Amount',
                                'quantity': 1,
                                'price_unit': self.partner_id.default_downpay_amount,
                                'price_subtotal' : self.partner_id.default_downpay_amount,
                                'account_id': 21
                            })],
                        })
       
        temp = self.env['ir.config_parameter'].browse([14])
        sale_line_obj = self.env['sale.order.line']
        so_line = sale_line_obj.create({
                    'name': self.name,
                    'price_unit': self.partner_id.default_downpay_amount,
                    'product_uom_qty': 0,
                    'order_id': self.id,
                    'discount': 0.0,
                    'product_uom': self.env['uom.uom'].browse([1]).id,
                    'product_id': 43,
                    'is_downpayment': True,
                })
       
        self._amount_all()
   
   
    def _amount_all(self):
        super(SaleOrder,self)._amount_all()
        self.amount_total = self.amount_total - self.partner_id.default_downpay_amount
        self.amount_untaxed = self.amount_untaxed - self.partner_id.default_downpay_amount
               
       
      
       
    @api.multi
    def action_view_downpay_invoice(self):
        invoices = self.mapped('invoice_ids')
        action = self.env.ref('account.action_invoice_tree1').read()[0]
        if invoices:
            action['views'] = [(self.env.ref('account.invoice_form').id, 'form')]
            action['res_id'] = invoices.ids[-1]
        else:
            action = {'type': 'ir.actions.act_window_close'}
        return action
       
       
        """if not self.order_line.product_id:
            vals = self._prepare_deposit_product()
            print(vals)
            self.product_id = self.env['product.product'].create(vals)
            self.env['ir.config_parameter'].sudo().set_param('sale.default_deposit_product_id', self.product_id.id)
        sale_line_obj = self.env['sale.order.line']
        for order in self:
            sale_line_obj = self.env['sale.order.line']
            so_line = sale_line_obj.create({
                        'name': self.name,
                        'price_unit': self.partner_id.default_downpay_amount,
                        'product_uom_qty': 0.0,
                        'order_id': self.id,
                        'discount': 0.0,
                        'product_uom': self.product_id.uom_id.id,
                        'product_id': self.product_id.id,
                        'analytic_tag_ids': self.analytic_tag_ids,
                        'tax_id': [(6, 0, tax_ids)],
                        'is_downpayment': True,
                    })
                #self._create_invoice(order, so_line, amount)
    def _prepare_deposit_product(self):
        return {
            'name': 'Down payment',
            'type': 'service',
            'invoice_policy': 'order',
            'property_account_income_id': self.deposit_account_id.id,
            'taxes_id': [(6, 0, self.deposit_taxes_id.ids)],
            'company_id': False,
        }"""
       
       
       
       

class SaleAdvancePaymentInv(models.TransientModel):
    _inherit = "sale.advance.payment.inv"
   
   
    def create_invoices(self):
        sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))
        res= self.env['account.invoice']
        if self.advance_payment_method == 'delivered':
            super(SaleAdvancePaymentInv, self).create_invoices()
            self.env['account.invoice.line'].create({
                            'name': 'test',
                            'account_id': 21,
                            'price_unit': 0,
                            'quantity':0,
                            })
            """sale_orders = self.env['sale.order.line']
            for order in sale_orders:
                res.create({'type': 'out_invoice',
                            'partner_id': res.partner_id.id,
                            'user_id': res.env.user.id,
                            'origin': res.name,
                            'invoice_line_ids': [(0, 0, {
                                'name': 'DownPayment Amount',
                                'quantity': 1,
                                'price_unit': res.partner_id.default_downpay_amount,
                                'price_subtotal' : res.partner_id.default_downpay_amount,
                                'account_id': 21
                            })],
                        })"""
               
        else:
            return
           
My View is:-<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        
                
        <record id="partner_form_inherit_default_downpay" model="ir.ui.view">
            <field name="name">default.downpayment</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form"/>
            <field name="arch" type="xml">
            <xpath expr="//field[@name='property_payment_term_id']" position="after">
                    <field name="default_downpay_mode"/>
                    <field name="default_downpay_amount"/>
            </xpath>
            </field>
        </record>
        
        
        <record id="sale_view_order_inherit_default_downpay" model="ir.ui.view">
            <field name="name">default.downpayment</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml">
            <div class="oe_button_box" name="button_box">
                <button name="action_view_downpay_invoice"
                    type="object"
                    class="oe_stat_button"
                    icon="fa-pencil-square-o"
                    string = "Sample Invoices"
                    attrs="{'invisible': [('state', 'in', ['draft', 'sent'])]}">
                </button>
            </div>
            </field>
        </record>
        
        
    </data>
</odoo>


Please Help...

Thanks in Advance...


Avatar
Discard