from odoo import fields, models, _, api from odoo.exceptions import UserError from datetime import datetime from odoo.http import request import logging, math _logger = logging.getLogger(__name__) class UangmukaPembelian(models.TransientModel): _name = 'uangmuka.pembelian' _description = 'digunakan untuk membuat Uang Muka Pembelian' pay_amt = fields.Float(string='Uang Muka', help='berapa nilai yang terbentuk untuk COA Uang Muka Pembelian') account_id = fields.Many2one('account.account', string='Bank Intransit', default=389, help='pilih COA intransit bank') ongkir_amt = fields.Float(string='Ongkir', help='masukan nilai yang akan menjadi Pendapatan Ongkos Kirim') selisih_amt = fields.Float(string='Selisih', help='masukan nilai yang akan menjadi Selisih Pembayaran') total_amt = fields.Float(string='Total', help='Total yang akan masuk di journal entries') @api.onchange('pay_amt', 'ongkir_amt', 'selisih_amt') def _compute_total_amt(self): for o in self: o.total_amt = o.pay_amt + o.ongkir_amt + o.selisih_amt def create_uangmukapembelian(self): if not self.account_id: raise UserError('Bank Intransit harus diisi') if not self.env.user.is_accounting: raise UserError('Hanya Finance yang dapat membuat Uang Muka Pembelian') orders = self.env['purchase.order'].browse(self._context.get('active_ids',[])) current_time = datetime.now() is_have_ongkir = is_have_selisih = False if self.ongkir_amt > 0: is_have_ongkir = True if not math.isclose(self.selisih_amt, 0): is_have_selisih = True for order in orders: if order.is_create_uangmuka == True: action = self.env['ir.actions.act_window']._for_xml_id('indoteknik_custom.action_purchase_order_multi_uangmuka2') action['context'] = { 'order_ids': [x.id for x in order] } return action partner_name = order.partner_id.name if order.partner_id.parent_id: partner_name = order.partner_id.parent_id.name ref_label = 'UANG MUKA PEMBELIAN '+order.name+' '+partner_name param_header = { 'ref': ref_label, 'date': current_time, 'journal_id': 11 } account_move = request.env['account.move'].create([param_header]) _logger.info('Success Create Uang Muka Pembelian %s' % account_move.name) account_move.purchase_order_id = order.id # isi field purchase_order_id if order.partner_id.parent_id: partner_id = order.partner_id.parent_id.id else: partner_id = order.partner_id.id param_debit = { 'move_id': account_move.id, 'account_id': 669, # uang muka persediaan barang dagang 'partner_id': partner_id, 'currency_id': 12, 'debit': self.pay_amt, 'credit': 0, 'name': ref_label, } param_debit_ongkir = { 'move_id': account_move.id, 'account_id': 536, # biaya ongkos kirim 'partner_id': partner_id, 'currency_id': 12, 'debit': self.ongkir_amt, 'credit': 0, 'name': ref_label, } param_debit_selisih = { 'move_id': account_move.id, 'account_id': 561, # selisih pembayaran 'partner_id': partner_id, 'currency_id': 12, 'debit': self.selisih_amt, 'credit': 0, 'name': ref_label, } param_credit = { 'move_id': account_move.id, 'account_id': self.account_id.id, # bank in transit 'partner_id': partner_id, 'currency_id': 12, 'debit': 0, 'credit': self.pay_amt + self.ongkir_amt + self.selisih_amt, 'name': ref_label, } if is_have_ongkir and is_have_selisih: request.env['account.move.line'].create([param_debit, param_debit_ongkir, param_debit_selisih, param_credit]) elif is_have_ongkir: request.env['account.move.line'].create([param_debit, param_debit_ongkir, param_credit]) elif is_have_selisih: request.env['account.move.line'].create([param_debit, param_debit_selisih, param_credit]) else: request.env['account.move.line'].create([param_debit, param_credit]) order.is_create_uangmuka = True order.move_id = account_move.id return { 'name': _('Journal Entries'), 'view_mode': 'form', 'res_model': 'account.move', 'target': 'current', 'view_id': False, 'type': 'ir.actions.act_window', 'res_id': account_move.id }