Skip to content

Commit

Permalink
[IMP] fieldservice_stock_account - refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
baimont committed Sep 6, 2021
1 parent 01c9c3b commit b5f9f24
Showing 1 changed file with 53 additions and 15 deletions.
68 changes: 53 additions & 15 deletions fieldservice_stock_account/models/fsm_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,49 @@
class FSMOrder(models.Model):
_inherit = "fsm.order"

def _prepare_inv_line_for_stock_request(self, stock_request, invoice=False):
accounts = stock_request.product_id.product_tmpl_id.get_product_accounts()
def _get_stock_request_invoice_line_vals(
self, stock_request, fpos, price_list, invoice_vals
):
price = price_list.get_product_price(
product=stock_request.product_id,
quantity=stock_request.product_uom_qty,
partner=invoice_vals.get("partner_id"),
date=False,
uom_id=stock_request.product_uom_id.id,
)
template = stock_request.product_id.product_tmpl_id
accounts = template.get_product_accounts()
account = accounts["income"]
taxes = template.taxes_id
tax_ids = fpos.map_tax(taxes)
if stock_request.direction == "inbound":
quantity = -stock_request.qty_done
else:
quantity = stock_request.qty_done
vals = {
return {
"product_id": stock_request.product_id.id,
"quantity": quantity,
"name": stock_request.product_id.name,
"price_unit": 0,
"price_unit": price,
"show_in_report": False,
"account_id": account.id,
"move_id": invoice.id,
"tax_ids": [(6, 0, tax_ids.ids)],
}
return vals

def _create_inv_line_for_stock_requests(self, invoice=False):
for stock_request in self.stock_request_ids:
vals = self._prepare_inv_line_for_stock_request(stock_request, invoice)
self.env["account.move.line"].create(vals)

def account_create_invoice(self):
invoice = super(FSMOrder, self).account_create_invoice()
def _get_invoice_line_vals(self, fpos, price_list, invoice_vals):
res = super()._get_invoice_line_vals(fpos, price_list, invoice_vals)
if self.location_id.inventory_location_id.usage == "customer":
self._create_inv_line_for_stock_requests(invoice)
return invoice
for stock_request in self.stock_request_ids:
res.append(
(
0,
0,
self._get_stock_request_invoice_line_vals(
stock_request, fpos, price_list, invoice_vals
),
)
)
return res

def account_no_invoice(self):
res = super(FSMOrder, self).account_no_invoice()
Expand Down Expand Up @@ -75,3 +90,26 @@ def account_no_invoice(self):
invoice = self.env["account.move"].sudo().create(vals)
self._create_inv_line_for_stock_requests(invoice)
return res

def _create_inv_line_for_stock_requests(self, invoice=False):
for stock_request in self.stock_request_ids:
vals = self._prepare_inv_line_for_stock_request(stock_request, invoice)
self.env["account.move.line"].create(vals)

def _prepare_inv_line_for_stock_request(self, stock_request, invoice=False):
accounts = stock_request.product_id.product_tmpl_id.get_product_accounts()
account = accounts["income"]
if stock_request.direction == "inbound":
quantity = -stock_request.qty_done
else:
quantity = stock_request.qty_done
vals = {
"product_id": stock_request.product_id.id,
"quantity": quantity,
"name": stock_request.product_id.name,
"price_unit": 0,
"show_in_report": False,
"account_id": account.id,
"move_id": invoice.id,
}
return vals

0 comments on commit b5f9f24

Please sign in to comment.