calc remaing
This commit is contained in:
parent
9b9283fa64
commit
af7156e850
|
|
@ -20,7 +20,7 @@ This course provides a comprehensive, hands-on guide to managing employee custod
|
|||
'version': '0.1',
|
||||
|
||||
# any module necessary for this one to work correctly
|
||||
'depends': ['base','hr','account','exp_payroll_custom','hr_expense','odex25_account_reports','system_dashboard_classic','odex_takaful'],
|
||||
'depends': ['base','hr','account','exp_payroll_custom','hr_expense','odex25_account_reports','system_dashboard_classic','odex_takaful','exp_budget_check'],
|
||||
|
||||
# always loaded
|
||||
'data': [
|
||||
|
|
|
|||
|
|
@ -69,12 +69,7 @@ class HrRequestPledge(models.Model):
|
|||
)
|
||||
journal_id = fields.Many2one(related='custody_type_id.journal_id', readonly=True)
|
||||
|
||||
spent_amount = fields.Monetary(
|
||||
string="Amount Spent",
|
||||
compute="_compute_spent_amount",
|
||||
currency_field='currency_id',
|
||||
store=False
|
||||
)
|
||||
|
||||
employee_id = fields.Many2one('hr.employee', 'Employee',
|
||||
default=lambda item: item.get_user_id(), index=True)
|
||||
|
||||
|
|
@ -87,62 +82,75 @@ class HrRequestPledge(models.Model):
|
|||
string='Currency',
|
||||
default=lambda self: self.env.company.currency_id
|
||||
)
|
||||
remaining_amount = fields.Monetary(
|
||||
string="Amount Remaining",
|
||||
compute="_compute_remaining_amount",
|
||||
store=True,
|
||||
currency_field='currency_id'
|
||||
)
|
||||
|
||||
spent_amount = fields.Float(string="Amount Spent", default=0.0)
|
||||
remaining_amount = fields.Float(string="Amount Remaining", compute="_compute_remaining_amount", store=True)
|
||||
custody_status = fields.Selection([
|
||||
('new', 'New'),
|
||||
('partial', 'Partial'),
|
||||
('paid', 'Paid'),
|
||||
('exceeded', 'Exceeded')
|
||||
], string='Custody Status', default='new', tracking=True)
|
||||
|
||||
@api.depends('spent_amount', 'emp_expect_amount')
|
||||
def _compute_remaining_amount(self):
|
||||
for rec in self:
|
||||
rec.remaining_amount = rec.emp_expect_amount - rec.spent_amount
|
||||
rec.remaining_amount = (rec.emp_expect_amount or 0.0) - (rec.spent_amount or 0.0)
|
||||
|
||||
@api.depends('employee_id', 'journal_id')
|
||||
def _compute_spent_amount(self):
|
||||
for rec in self:
|
||||
rec.spent_amount = 0.0
|
||||
|
||||
if not rec.employee_id or not rec.employee_id.user_id or not rec.journal_id:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@api.model
|
||||
def allocate_payment_to_pledges(self, employee_id, journal_id, amount):
|
||||
"""
|
||||
Allocate a payment amount to employee's custody requests ordered by oldest.
|
||||
Handles partial, full, and exceeded states.
|
||||
"""
|
||||
remaining_amount = amount
|
||||
|
||||
pledges = self.search([
|
||||
('employee_id', '=', employee_id),
|
||||
('journal_id', '=', journal_id),
|
||||
('custody_status', 'in', ['partial','new' ]),
|
||||
], order="id asc")
|
||||
|
||||
for pledge in pledges:
|
||||
if remaining_amount <= 0:
|
||||
break
|
||||
|
||||
spent = pledge.spent_amount or 0.0
|
||||
total = pledge.emp_expect_amount or 0.0
|
||||
pledge_remaining = total - spent
|
||||
|
||||
if pledge_remaining <= 0:
|
||||
continue
|
||||
|
||||
partner = rec.employee_id.user_id.partner_id
|
||||
journal = rec.journal_id
|
||||
allocated = min(pledge_remaining, remaining_amount)
|
||||
pledge.spent_amount = spent + allocated
|
||||
|
||||
if pledge.spent_amount < total:
|
||||
pledge.custody_status = 'partial'
|
||||
elif pledge.spent_amount == total:
|
||||
pledge.custody_status = 'paid'
|
||||
|
||||
remaining_amount -= allocated
|
||||
|
||||
if remaining_amount > 0:
|
||||
target_pledge = (pledges.filtered(lambda p: p.custody_status == 'partial') or pledges)[-1:]
|
||||
for pledge in target_pledge:
|
||||
pledge.spent_amount += remaining_amount
|
||||
pledge.custody_status = 'exceeded'
|
||||
break
|
||||
|
||||
|
||||
|
||||
|
||||
move_data = self.env['account.move.line'].read_group(
|
||||
domain=[
|
||||
('partner_id', '=', partner.id),
|
||||
('move_id.state', '=', 'posted'),
|
||||
('move_id.journal_id', '=', journal.id),
|
||||
('account_id.user_type_id.type', '=', 'receivable'),
|
||||
('debit', '>', 0),
|
||||
],
|
||||
fields=['debit:sum'],
|
||||
groupby=[]
|
||||
)
|
||||
# total_paid = move_data[0]['debit'] if move_data else 0.0
|
||||
total_paid = (move_data[0]['debit'] if move_data and move_data[0].get('debit') else 0.0) or 0.0
|
||||
all_requests = self.search([
|
||||
('employee_id', '=', rec.employee_id.id),
|
||||
('journal_id', '=', journal.id),
|
||||
], order='date asc, id asc')
|
||||
|
||||
remaining_amount = total_paid
|
||||
last_index = len(all_requests) - 1
|
||||
|
||||
for idx, pledge in enumerate(all_requests):
|
||||
if remaining_amount <= 0:
|
||||
pledge.spent_amount = 0.0
|
||||
elif idx == last_index:
|
||||
pledge.spent_amount = remaining_amount
|
||||
remaining_amount = 0
|
||||
elif remaining_amount >= pledge.emp_expect_amount:
|
||||
pledge.spent_amount = pledge.emp_expect_amount
|
||||
remaining_amount -= pledge.emp_expect_amount
|
||||
else:
|
||||
pledge.spent_amount = remaining_amount
|
||||
remaining_amount = 0
|
||||
|
||||
@api.constrains('custody_type_id', 'emp_expect_amount')
|
||||
def _check_custody_amount_limit(self):
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@
|
|||
<field name="date"/>
|
||||
</group>
|
||||
<group>
|
||||
<field name="emp_expect_amount"/>
|
||||
<field name="emp_expect_amount" attrs="{'readonly':[('state','!=','draft')]}"/>
|
||||
<field name="custody_type_id"/>
|
||||
<field name="journal_id" attrs="{'invisible': [('id', '!=', 0)]}"/>
|
||||
|
||||
|
|
@ -130,6 +130,8 @@
|
|||
<field name="job_id"/>
|
||||
<field name="description"/>
|
||||
<field name="spent_amount"/>
|
||||
<field name="custody_status" invisible="1"/>
|
||||
|
||||
<field name="remaining_amount"/>
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -63,19 +63,6 @@ class AccountPaymentRegister(models.TransientModel):
|
|||
}
|
||||
}
|
||||
|
||||
# def _create_payments(self):
|
||||
# payments = super()._create_payments()
|
||||
# print("منؤبء")
|
||||
# if self.is_custody_journal and self.custody_partner_id:
|
||||
# for payment in payments:
|
||||
# for line in payment.move_id.line_ids:
|
||||
# _logger.info("Line account: %s, destination account: %s", line.account_id.id,
|
||||
# payment.destination_account_id.id)
|
||||
# if line.account_id.id == payment.destination_account_id.id:
|
||||
# _logger.info("Updating partner_id of line %s to %s", line.id, self.custody_partner_id.id)
|
||||
# line.write({'partner_id': self.custody_partner_id.id})
|
||||
# return payments
|
||||
|
||||
def _create_payments(self):
|
||||
self.ensure_one()
|
||||
active_ids = self.env.context.get('active_ids')
|
||||
|
|
@ -144,15 +131,24 @@ class AccountPaymentRegister(models.TransientModel):
|
|||
)
|
||||
if line.account_id.id in [payment.destination_account_id.id]:
|
||||
line.name = liquidity_line_name or default_line_name
|
||||
if hasattr(self, 'is_custody_journal') and self.is_custody_journal \
|
||||
and hasattr(self, 'custody_partner_id') and self.custody_partner_id:
|
||||
|
||||
|
||||
if hasattr(self, 'is_custody_journal') and self.is_custody_journal and hasattr(self,
|
||||
'custody_partner_id') and self.custody_partner_id:
|
||||
print(f"تطبيق منطق العهد للدفع {payment.name}")
|
||||
for line in payment.move_id.line_ids:
|
||||
if (line.account_id.user_type_id.type == 'receivable' and line.debit > 0):
|
||||
print(f"تحديث الشريك للخط {line.id} إلى {self.custody_partner_id.name}")
|
||||
if line.debit > 0:
|
||||
line.write({'partner_id': self.custody_partner_id.id})
|
||||
|
||||
employee = self.env['hr.employee'].search([
|
||||
('user_id.partner_id', '=', self.custody_partner_id.id)
|
||||
], limit=1)
|
||||
|
||||
if employee:
|
||||
self.env['hr.request.pledge'].allocate_payment_to_pledges(
|
||||
employee_id=employee.id,
|
||||
journal_id=self.journal_id.id,
|
||||
amount=payment.amount
|
||||
)
|
||||
return payments
|
||||
|
||||
def action_create_payments(self):
|
||||
|
|
|
|||
Loading…
Reference in New Issue