summaryrefslogtreecommitdiff
path: root/fixco_custom/models/sale.py
blob: a36be0f87326fe93eade8cb00406d9b249fde42a (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
from odoo import api, fields, models, _
from odoo.exceptions import UserError


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

    carrier = fields.Char(string='Shipping Method', required=True)
    invoice_mp = fields.Char(string='Invoice Marketplace', required=True)
    order_reference = fields.Char(string='Order Reference', required=True)
    address = fields.Char('Address') 
    note_by_buyer = fields.Char('Note By Buyer')

    def _check_duplicate_order_id(self):
        for rec in self:
            so_duplicate = self.search([
                '|',
                ('order_reference', '=', rec.order_reference),
                ('invoice_mp', '=', rec.invoice_mp),
                ('id', '!=', rec.id),
            ], limit=1)

            if so_duplicate:
                raise UserError(f'Order Id tersebut sudah digunakan di so {so_duplicate.name}')

    # def open_form_multi_create_invoices(self):
    #     action = self.env['ir.actions.act_window']._for_xml_id('fixco_custom.action_sale_order_multi_invoices')
    #     action['context'] = {
    #         'so_ids': [x.id for x in self]
    #     }
    #     return action

    def _prepare_invoice(self):
        """
        Prepare the dict of values to create the new invoice for a sales order. This method may be
        overridden to implement custom invoice generation (making sure to call super() to establish
        a clean extension chain).
        """
        self.ensure_one()
        journal = self.env['account.move'].with_context(default_move_type='out_invoice')._get_default_journal()
        if not journal:
            raise UserError(_('Please define an accounting sales journal for the company %s (%s).') % (self.company_id.name, self.company_id.id))

        done_pickings = self.picking_ids.filtered(lambda p: p.state == 'done').sorted(key='create_date')
        invoice_vals = {
            'ref': self.client_order_ref or '',
            'move_type': 'out_invoice',
            'narration': self.note,
            'transaction_type': self.partner_id.transaction_type,
            'invoice_marketplace': self.invoice_mp,
            'address': self.address,
            'sale_id': self.id,
            'picking_id': done_pickings[0] if done_pickings else False,
            'currency_id': self.pricelist_id.currency_id.id,
            'campaign_id': self.campaign_id.id,
            'medium_id': self.medium_id.id,
            'source_id': self.source_id.id,
            'user_id': self.user_id.id,
            'invoice_user_id': self.user_id.id,
            'team_id': self.team_id.id,
            'partner_id': self.partner_invoice_id.id,
            'partner_shipping_id': self.partner_shipping_id.id,
            'fiscal_position_id': (self.fiscal_position_id or self.fiscal_position_id.get_fiscal_position(self.partner_invoice_id.id)).id,
            'partner_bank_id': self.company_id.partner_id.bank_ids.filtered(lambda bank: bank.company_id.id in (self.company_id.id, False))[:1].id,
            'journal_id': journal.id,  # company comes from the journal
            'invoice_origin': self.name,
            'invoice_payment_term_id': self.payment_term_id.id,
            'payment_reference': self.reference,
            'transaction_ids': [(6, 0, self.transaction_ids.ids)],
            'invoice_line_ids': [],
            'company_id': self.company_id.id,
        }
        return invoice_vals

    def open_form_multi_create_invoices(self):
        return {
            'name': _('Create Invoices'),
            'type': 'ir.actions.act_window',
            'res_model': 'sale.order.multi_invoices',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'so_ids': self.ids,
            }
        }
    
    def open_form_uangmuka_penjualan(self):
        return {
            'name': _('Create uang muka penjualan'),
            'type': 'ir.actions.act_window',
            'res_model': 'uangmuka.penjualan',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'so_ids': self.ids,
                'default_partner_name': self.partner_id.name
            }
        }

    def action_confirm(self):
        for order in self:
            order._check_duplicate_order_id()
        res = super(SaleOrder, self).action_confirm()
        return res