from odoo import api, fields, models, _ from odoo.exceptions import UserError import logging _logger = logging.getLogger(__name__) class SaleOrder(models.Model): _inherit = "sale.order" carrier = fields.Char(string='Shipping Method', required=True, copy=False) invoice_mp = fields.Char(string='Invoice Marketplace', required=True, copy=False) order_reference = fields.Char(string='Order Reference', required=True, copy=False) address = fields.Char('Address', copy=False) channel = fields.Char('Channel', copy=False) note_by_buyer = fields.Char('Note By Buyer', copy=False) source = fields.Selection([('manual', 'Manual'), ('ginee', 'Ginee')], string='Source', default='manual', copy=False) count_payment = fields.Integer('Count Payment', compute='_compute_count_payment') count_journals = fields.Integer('Count Payment', compute='_compute_count_journals') remaining_down_payment = fields.Float('Remaining Down Payment', compute='_compute_remaining_down_payment', store=True) deadline_date = fields.Datetime('Deadline', copy=False) total_discount = fields.Monetary( string='Total Discount', compute='_compute_total_discount', currency_field='currency_id', ) uangmuka_exist = fields.Boolean(string='Uang Muka Exist', copy=False) delivery_amount = fields.Monetary("Delivery Amount", currency_field='currency_id') marketplace_discount = fields.Monetary( string="Marketplace Discount", currency_field='currency_id' ) marketplace_tax = fields.Monetary( string="Marketplace Tax", currency_field='currency_id' ) @api.depends('order_line.price_total', 'delivery_amount') def _amount_all(self): super()._amount_all() for order in self: if order.partner_id.id == 281: subtotal = sum(line.price_subtotal for line in order.order_line) tax = order.marketplace_tax # delivery = order.delivery_amount or 0 # discount = order.marketplace_discount or 0 order.amount_total = subtotal + tax @api.depends( 'order_line.price_unit', 'order_line.product_uom_qty', 'order_line.discount', ) def _compute_total_discount(self): for order in self: if order.partner_id.id == 281: # Biar gk error order.total_discount = order.total_discount continue total = 0.0 for line in order.order_line: if line.discount: line_discount = ( line.price_unit * line.product_uom_qty * line.discount / 100 ) total += line_discount order.total_discount = total def create_invoices(self): created_invoices = self.env['account.move'] for order in self: # Create invoice for this SO only invoice = order.with_context(default_invoice_origin=order.name, force_picking_date=True)._create_invoices(final=True) invoice.action_post() invoice.invoice_date = invoice.picking_id.date_done created_invoices += invoice # Link the invoice to the SO order.invoice_ids += invoice def _compute_remaining_down_payment(self): for order in self: down_payments = self.env['account.move'].search([ ('sale_id', '=', order.id), ('move_type', '=', 'entry'), ('state', '=', 'posted'), ('uangmuka', '=', True) ]) reklas = self.env['account.move'].search([ ('sale_id', '=', order.id), ('move_type', '=', 'entry'), ('state', '=', 'posted'), ('reklas', '=', True) ]) def check_reklas_used(reklas): for line in reklas.line_ids: if line.reconciled and line.matching_number: return True return False reklas = reklas.filtered(check_reklas_used) nominal_down_payment = sum(down_payments.mapped('amount_total_signed')) if down_payments else 0 nominal_reklas = sum(reklas.mapped('amount_total_signed')) if reklas else 0 order.remaining_down_payment = nominal_down_payment - nominal_reklas def _compute_count_journals(self): for order in self: journals = self.env['account.move'].search([ ('sale_id', '=', order.id), ('move_type', '!=', 'out_invoice') ]) order.count_journals = len(journals) def action_view_related_journals(self): self.ensure_one() journals = self.env['account.move'].search([ ('sale_id', '=', self.id), ('move_type', '!=', 'out_invoice') ]) return { 'name': 'Journals', 'type': 'ir.actions.act_window', 'res_model': 'account.move', 'view_mode': 'tree,form', 'target': 'current', 'domain': [('id', 'in', journals.ids)], } def _compute_count_payment(self): for rec in self: if rec.invoice_ids: invoice_ids_set = set(rec.invoice_ids.ids) payments = self.env['account.payment'].search([]).filtered( lambda p: any(inv.id in invoice_ids_set for inv in p.reconciled_invoice_ids) ) rec.count_payment = len(payments) else: rec.count_payment = 0 def action_view_related_payment(self): self.ensure_one() for rec in self: if rec.invoice_ids: invoice_ids_set = set(rec.invoice_ids.ids) payments = self.env['account.payment'].search([]).filtered( lambda p: any(inv.id in invoice_ids_set for inv in p.reconciled_invoice_ids) ) return { 'name': 'Payments', 'type': 'ir.actions.act_window', 'res_model': 'account.payment', 'view_mode': 'tree,form', 'target': 'current', 'domain': [('id', 'in', payments.ids)], } def _check_duplicate_order_id(self): for rec in self: if rec.order_reference and rec.invoice_mp: 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: if order.source == 'manual' and self.env.user.id not in [9, 10, 15]: raise UserError("Anda tidak memiliki akses untuk approve SO manual") order._check_duplicate_order_id() res = super(SaleOrder, self).action_confirm() return res def _clean_sale_order(self): orders = self.env['sale.order'].search([ # ('invoice_status', '=', 'no'), ('state', 'in', ['draft', 'sale']) ], limit=2000) for order in orders: print("cancel sales or draft "+order.name) order.action_cancel()