summaryrefslogtreecommitdiff
path: root/fixco_custom/models/account_move.py
blob: 0a417cdb83d1e9169b237f5ed4274220fa11faf4 (plain)
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
from odoo import models, api, fields
from odoo.exceptions import AccessError, UserError, ValidationError
from datetime import timedelta, date, datetime
from pytz import timezone, utc
import logging
import base64
import PyPDF2
import os
import re

_logger = logging.getLogger(__name__)


class AccountMove(models.Model):
    _inherit = 'account.move'

    invoice_marketplace = fields.Char('Invoice Marketplace')
    address = fields.Char('Address')
    sale_id = fields.Many2one('sale.order', string='Sale Order')
    picking_id = fields.Many2one('stock.picking', string='Picking')
    transaction_type = fields.Selection(
        [('digunggung', 'Digunggung'),
         ('difaktur', 'Faktur Pajak')],
        string='Transaction Type'
    )
    purchase_vendor_bill_ids = fields.Many2many(
        'purchase.bill.union',
        string='Auto-complete',
        store=False,
        readonly=True,
        states={'draft': [('readonly', False)]},
        help="Auto-complete from multiple past bills / purchase orders.",
    )


    def action_post(self):
        res = super(AccountMove, self).action_post()
        for entry in self:
            entry.invoice_date = entry.picking_id.date_done

        return res
    
    @api.onchange('purchase_vendor_bill_ids', 'purchase_id')
    def _onchange_purchase_auto_complete(self):
        """ Load from either multiple old purchase orders or vendor bills. """

        vendor_bills = self.purchase_vendor_bill_ids.mapped('vendor_bill_id')
        purchase_orders = self.purchase_vendor_bill_ids.mapped('purchase_order_id')

        for bill in vendor_bills:
            self.invoice_vendor_bill_id = bill
            self._onchange_invoice_vendor_bill()

        for po in purchase_orders:
            self.purchase_id = po

            invoice_vals = po.with_company(po.company_id)._prepare_invoice()
            invoice_vals['currency_id'] = self.line_ids and self.currency_id or invoice_vals.get('currency_id')
            invoice_vals.pop('ref', None)
            self.update(invoice_vals)

            po_lines = po.order_line - self.line_ids.mapped('purchase_line_id')
            new_lines = self.env['account.move.line']
            sequence = max(self.line_ids.mapped('sequence')) + 1 if self.line_ids else 10

            for line in po_lines.filtered(lambda l: not l.display_type):
                line_vals = line._prepare_account_move_line(self)
                line_vals.update({'sequence': sequence})
                new_line = new_lines.new(line_vals)
                sequence += 1
                new_line.account_id = new_line._get_computed_account()
                new_line._onchange_price_subtotal()
                new_lines += new_line

            new_lines._onchange_mark_recompute_taxes()

        # Compute invoice_origin
        origins = set(self.line_ids.mapped('purchase_line_id.order_id.name'))
        self.invoice_origin = ', '.join(origins)

        # Compute ref
        refs = self._get_invoice_reference()
        self.ref = ', '.join(refs)

        # Compute payment_reference
        if len(refs) == 1:
            self.payment_reference = refs[0]

        self.purchase_id = False
        self.purchase_vendor_bill_ids = [(5, 0, 0)]  # clear after use
        self._onchange_currency()