Skip to content

Commit

Permalink
[ADD] make invoice_vals inheritable
Browse files Browse the repository at this point in the history
  • Loading branch information
baimont committed Jun 14, 2021
1 parent 3827600 commit 0781fa3
Showing 1 changed file with 121 additions and 98 deletions.
219 changes: 121 additions & 98 deletions fieldservice_isp_account/models/fsm_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,7 @@ def create_bills(self):
fpos = self.person_id.partner_id.property_account_position_id
invoice_line_vals = []
for cost in self.contractor_cost_ids:
template = cost.product_id.product_tmpl_id
accounts = template.get_product_accounts()
account = accounts["expense"]
taxes = template.supplier_taxes_id
tax_ids = fpos.map_tax(taxes)
invoice_line_vals.append(
(
0,
0,
{
"analytic_account_id": self.location_id.analytic_account_id.id,
"product_id": cost.product_id.id,
"quantity": cost.quantity,
"name": cost.product_id.display_name,
"price_unit": cost.price_unit,
"account_id": account.id,
"fsm_order_ids": [(4, self.id)],
"tax_ids": [(6, 0, tax_ids.ids)],
},
)
)
invoice_line_vals.append((0, 0, self._get_bill_line_vals(cost, fpos)))
vals = {
"partner_id": self.person_id.partner_id.id,
"type": "in_invoice",
Expand All @@ -128,6 +108,23 @@ def create_bills(self):
bill = self.env["account.move"].sudo().create(vals)
bill._recompute_tax_lines()

def _get_bill_line_vals(self, cost, fpos):
template = cost.product_id.product_tmpl_id
accounts = template.get_product_accounts()
account = accounts["expense"]
taxes = template.supplier_taxes_id
tax_ids = fpos.map_tax(taxes)
return {
"analytic_account_id": self.location_id.analytic_account_id.id,
"product_id": cost.product_id.id,
"quantity": cost.quantity,
"name": cost.product_id.display_name,
"price_unit": cost.price_unit,
"account_id": account.id,
"fsm_order_ids": [(4, self.id)],
"tax_ids": [(6, 0, tax_ids.ids)],
}

def account_confirm(self):
for order in self:
contractor = order.person_id.partner_id.supplier_rank
Expand All @@ -143,96 +140,28 @@ def account_confirm(self):
order.account_stage = "confirmed"

def account_create_invoice(self):
jrnl = self.env["account.journal"].search(
[
("company_id", "=", self.env.company.id),
("type", "=", "sale"),
("active", "=", True),
],
limit=1,
)
if self.bill_to == "customer":
if not self.customer_id:
raise ValidationError(_("Customer empty"))
fpos = self.customer_id.property_account_position_id
invoice_vals = {
"partner_id": self.customer_id.id,
"type": "out_invoice",
"journal_id": jrnl.id or False,
"fiscal_position_id": fpos.id or False,
"fsm_order_ids": [(4, self.id)],
}
price_list = self.customer_id.property_product_pricelist

else:
fpos = self.location_id.customer_id.property_account_position_id
invoice_vals = {
"partner_id": self.location_id.customer_id.id,
"type": "out_invoice",
"journal_id": jrnl.id or False,
"fiscal_position_id": fpos.id or False,
"fsm_order_ids": [(4, self.id)],
"company_id": self.env.company.id,
}
price_list = self.location_id.customer_id.property_product_pricelist

fpos = self._get_fpos()
invoice_vals = self._get_invoice_vals(fpos)
price_list = self._get_pricelist()
invoice_line_vals = []
for cost in self.contractor_cost_ids:
price = price_list.get_product_price(
product=cost.product_id,
quantity=cost.quantity,
partner=invoice_vals.get("partner_id"),
date=False,
uom_id=False,
)
template = cost.product_id.product_tmpl_id
accounts = template.get_product_accounts()
account = accounts["income"]
taxes = template.taxes_id
tax_ids = fpos.map_tax(taxes)
invoice_line_vals.append(
(
0,
0,
{
"product_id": cost.product_id.id,
"analytic_account_id": self.location_id.analytic_account_id.id,
"quantity": cost.quantity,
"name": cost.product_id.display_name,
"price_unit": price,
"account_id": account.id,
"fsm_order_ids": [(4, self.id)],
"tax_ids": [(6, 0, tax_ids.ids)],
},
self._get_cost_invoice_line_vals(
cost, fpos, price_list, invoice_vals
),
)
)
for line in self.employee_timesheet_ids:
price = price_list.get_product_price(
product=line.product_id,
quantity=line.unit_amount,
partner=invoice_vals.get("partner_id"),
date=False,
uom_id=False,
)
template = line.product_id.product_tmpl_id
accounts = template.get_product_accounts()
account = accounts["income"]
taxes = template.taxes_id
tax_ids = fpos.map_tax(taxes)
invoice_line_vals.append(
(
0,
0,
{
"product_id": line.product_id.id,
"analytic_account_id": line.account_id.id,
"quantity": line.unit_amount,
"name": line.name,
"price_unit": price,
"account_id": account.id,
"fsm_order_ids": [(4, self.id)],
"tax_ids": [(6, 0, tax_ids.ids)],
},
self._get_timesheet_line_invoice_line_vals(
line, fpos, price_list, invoice_vals
),
)
)

Expand All @@ -243,5 +172,99 @@ def account_create_invoice(self):
self.account_stage = "invoiced"
return invoice

def _get_pricelist(self):
if self.bill_to == "customer":
if not self.customer_id:
raise ValidationError(_("Customer empty"))
return self.customer_id.property_product_pricelist
elif self.bill_to == "location":
return self.location_id.customer_id.property_product_pricelist

def _get_fpos(self):
if self.bill_to == "customer":
if not self.customer_id:
raise ValidationError(_("Customer empty"))
return self.customer_id.property_account_position_id
elif self.bill_to == "location":
return self.location_id.customer_id.property_account_position_id

def _get_invoice_vals(self, fpos):
jrnl = self.env["account.journal"].search(
[
("company_id", "=", self.env.company.id),
("type", "=", "sale"),
("active", "=", True),
],
limit=1,
)
invoice_vals = {
"journal_id": jrnl.id or False,
"type": "out_invoice",
"fiscal_position_id": fpos.id or False,
"fsm_order_ids": [(4, self.id)],
}
if self.bill_to == "customer":
if not self.customer_id:
raise ValidationError(_("Customer empty"))
invoice_vals.update({
"partner_id": self.customer_id.id,
})
elif self.bill_to == "location":
invoice_vals.update({
"partner_id": self.location_id.customer_id.id,
"company_id": self.env.company.id,
})
return invoice_vals

def _get_cost_invoice_line_vals(self, cost, fpos, price_list, invoice_vals):
price = price_list.get_product_price(
product=cost.product_id,
quantity=cost.quantity,
partner=invoice_vals.get("partner_id"),
date=False,
uom_id=False,
)
template = cost.product_id.product_tmpl_id
accounts = template.get_product_accounts()
account = accounts["income"]
taxes = template.taxes_id
tax_ids = fpos.map_tax(taxes)
return {
"product_id": cost.product_id.id,
"analytic_account_id": self.location_id.analytic_account_id.id,
"quantity": cost.quantity,
"name": cost.product_id.display_name,
"price_unit": price,
"account_id": account.id,
"fsm_order_ids": [(4, self.id)],
"tax_ids": [(6, 0, tax_ids.ids)],
}

def _get_timesheet_line_invoice_line_vals(
self, line, fpos, price_list, invoice_vals
):
price = price_list.get_product_price(
product=line.product_id,
quantity=line.unit_amount,
partner=invoice_vals.get("partner_id"),
date=False,
uom_id=False,
)
template = line.product_id.product_tmpl_id
accounts = template.get_product_accounts()
account = accounts["income"]
taxes = template.taxes_id
tax_ids = fpos.map_tax(taxes)
return {
"product_id": line.product_id.id,
"analytic_account_id": line.account_id.id,
"quantity": line.unit_amount,
"name": line.name,
"price_unit": price,
"account_id": account.id,
"fsm_order_ids": [(4, self.id)],
"tax_ids": [(6, 0, tax_ids.ids)],
}

def account_no_invoice(self):
self.account_stage = "no"

0 comments on commit 0781fa3

Please sign in to comment.