summaryrefslogtreecommitdiff
path: root/indoteknik_api/models/account_move.py
blob: 645c157a22641bf059236395e39b3e27c5a25e81 (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
import datetime
from odoo import models


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

    def api_v1_single_response(self, account_move, context=False):
        sale_order = self.env['sale.order'].search([('name', '=', account_move.invoice_origin), ('state', '=', 'done')], limit=1)
        sale_order_v2 = self.env['sale.order'].search([('name', '=', account_move.invoice_origin)],limit=1)
        amount_residual = account_move.amount_residual
        if sale_order.payment_status == 'settlement' or sale_order.payment_status == 'capture':
            amount_residual = 0
        data = {
            'token': self.env['rest.api'].md5_salt(account_move.id, 'account.move'),
            'id': account_move.id,
            'name': account_move.name,
            'purchase_order_name': account_move.ref or '',
            'payment_term': account_move.invoice_payment_term_id.name or '',
            'sales': account_move.invoice_user_id.name,
            'amount_total': account_move.amount_total,
            'amount_residual': amount_residual,
            'invoice_date': account_move.invoice_date.strftime('%d/%m/%Y') or '',
            'efaktur': True if account_move.efaktur_document else False,
            'invoice_date_due': account_move.invoice_date_due.strftime('%d/%m/%Y') or '-',
            'sales_order': account_move.invoice_origin,
            'sales_order_id': sale_order_v2.id,
        }
        if isinstance(object, datetime.date):
            data['invoice_date'] = account_move.invoice_date.strftime('%d/%m/%Y')
        if context:
            if context == 'with_detail':
                res_users = self.env['res.users']
                data_with_detail = {
                    'id': account_move.id,
                    'name': account_move.name,
                    'purchase_order_name': account_move.ref or '',
                    'payment_term': account_move.invoice_payment_term_id.name or '',
                    'sales': account_move.invoice_user_id.name,
                    'amount_total': account_move.amount_total,
                    'amount_residual': amount_residual,
                    'customer': res_users.api_address_response(account_move.partner_id),
                    'products': [],
                }
                for line in account_move.invoice_line_ids:
                    product = self.env['product.product'].api_single_response(line.product_id)
                    product['price'] = {
                        'price': line.price_unit,
                        'discount_percentage': line.discount,
                        'price_discount': line.price_unit - (line.price_unit * (line.discount/100))
                    }
                    product['quantity'] = line.quantity
                    data_with_detail['products'].append(product)
                data.update(data_with_detail)
        return data