summaryrefslogtreecommitdiff
path: root/indoteknik_api/models/sale_order.py
blob: 1c0180ec2eec452096c7eb791b103c24445cbaec (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from odoo import models


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    def api_v1_single_response(self, sale_order, context=False):
        APPROVAL_STEP = {
            'company': 1,
            'cust_manager': 2,
            'cust_director': 3,
        }
        
        data = {
            'token': self.env['rest.api'].md5_salt(sale_order.id, 'sale.order'),
            'id': sale_order.id,
            'name': sale_order.name,
            'sales': sale_order.user_id.name,
            'amount_untaxed': sale_order.amount_untaxed,
            'amount_tax': sale_order.amount_tax,
            'amount_total': sale_order.grand_total,
            'purchase_order_name': sale_order.partner_purchase_order_name or sale_order.client_order_ref,
            'purchase_order_file': True if sale_order.partner_purchase_order_file else False,
            'invoice_count': sale_order.invoice_count,
            'status': 'draft',
            'approval_step': APPROVAL_STEP[sale_order.web_approval] if sale_order.web_approval else 0,
            'date_order': self.env['rest.api'].datetime_to_str(sale_order.date_order, '%d/%m/%Y %H:%M:%S'),
            'pickings': []
        }
        for picking in sale_order.picking_ids:
            data['pickings'].append({
                'id': picking.id,
                'name': picking.name,
                'tracking_number': picking.delivery_tracking_no or '',
                'delivered': picking.waybill_id.delivered or picking.driver_arrival_date != False,
            })
        if sale_order.state == 'cancel':
            data['status'] = 'cancel'
        if sale_order.state in ['draft', 'sent']:
            data['status'] = 'draft'
            if sale_order.approval_status in ['pengajuan1', 'pengajuan2']:
                data['status'] = 'waiting'
        if sale_order.state == 'sale': 
            data['status'] = 'sale'
            picking_count = {
                'assigned': 0,
                'done': 0,
            }
            for picking in sale_order.picking_ids:
                if picking.state in ['confirmed', 'assigned']:
                    picking_count['assigned'] += 1
                if picking.state == 'done':
                    picking_count['done'] += 1
            if picking_count['done'] > 0:
                data['status'] = 'shipping'
                if picking_count['assigned'] > 0:
                    data['status'] = 'partial_shipping'
        if sale_order.state == 'done': 
            data['status'] = 'done'
        
        res_users = self.env['res.users']
        if context:
            if context == 'with_detail':
                data_with_detail = {
                    'payment_term': sale_order.payment_term_id.name or '',
                    'products': [],
                    'delivery_amount': sale_order.delivery_amt or 0,
                    'address': {
                        'customer': res_users.api_address_response(sale_order.partner_id),
                        'invoice': res_users.api_address_response(sale_order.partner_invoice_id),
                        'shipping': res_users.api_address_response(sale_order.partner_shipping_id)
                    },
                    'invoices': []
                }
                for line in sale_order.order_line:
                    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_reduce_taxexcl,
                        'subtotal': line.price_subtotal
                    }
                    product['quantity'] = line.product_uom_qty
                    data_with_detail['products'].append(product)
                for invoice in sale_order.invoice_ids:
                    if invoice.state == 'posted':
                        data_with_detail['invoices'].append(self.env['account.move'].api_v1_single_response(invoice))
                data.update(data_with_detail)
        else:
            data_with_detail = {
                'address': {
                    'customer': res_users.api_address_response(sale_order.partner_id),
                }
            }
            data.update(data_with_detail)
        return data


class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    def api_single_response(self, sale_order_line, context=False):
        data = {
            'image': self.env['ir.attachment'].api_image('product.template', 'image_128', sale_order_line.product_id.product_tmpl_id.id),
            'item_code': sale_order_line.product_id.default_code,
            'product_name': sale_order_line.product_id.name,
            'price_before_discount': sale_order_line.price_unit * sale_order_line.product_uom_qty,
            'price_after_discount': sale_order_line.price_total,
            'tax': sale_order_line.price_tax
        }
        return data