ERP Tech Talk #5: Beauty Class

Indrajaya
IT Paragon
Published in
4 min readNov 29, 2020

--

Beauty class is one of the training programs to beautify yourself, such as daily makeup or flawless makeup, which aims to increase the insight of female consumers in using various types of cosmetics. In a company, beauty class can be applied to increase sales by collaborating between customer and company that have included the implementation period, the number of products, and the types of products to be sold.

Photo by freestocks on Unsplash

In this article, I would like to share how PT Paragon Technology and Innovation, as an FMCG Company, runs Beauty Class from a different perspective. Lets start it!

In the normal process, user (the company) create sale order then confirm it. As a result, delivery order was created. User will validate the delivery order after confirm the product and it’s quantity. Afterward user create the invoice based on delivered quantity. User will perform this typical activity repeatedly. This process will form a pattern: sale order ➟ delivery order (picking) ➟ invoice.

Table Relation

The relationship between sales orders, delivery orders and invoices can be explained below. Any unrelated fields in this article are not displayed.

Relation in a very simple form

Sale Order

In the common structure, the sale order should have information customer, order date, price list, payment term and the important one is sale order line. Okay, let’s deep into sale order line.

The relationship between sales orders (table sale_order) and sales order rows (table sale_order_line) is one by many. Tables sale_order_line record product, unit price, discount (if any), order quantity, delivery and invoice. When the user creates a sales order, all fields are filled, except qty_delivered and qty_invoiced which are still zero.

Delivery Order

When user confirm the sale, Odoo will create a record in stock_picking and stock_move. The sale_id in stock_picking will refer to sale_order.id and field sale_line_id in stock_move will link to sale_order_line.id. The relation between sale_order and stock_picking is one to many, as well as table sale_order_line and stock_move.

When user validate the delivery order, Odoo will call method _action_done for every line of the picking and update field qty_delivered in sale_order_line (see at addons/sale_stock/models/stock.py).

def _action_done(self):
result = super(StockMove, self)._action_done()
for line in result.mapped('sale_line_id').sudo():
line.qty_delivered = line._get_delivered_qty()
return result

Invoice

If the invoice policy is “Invoice what is delivered” at menu Sales | Configuration | Settings > Invoicing Policy, Odoo will create invoice based on delivered product through iterate the sale order line. During this iterate process, Odoo will make a link between sale order line and invoice line in method invoice_line_create of the model sale.order.line (see at addons/sale/models/sale.py). As a result, new record of table sale_order_line_invoice_rel created.

@api.multi
def invoice_line_create(self, invoice_id, qty):
invoice_lines = self.env['account.invoice.line']
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
for line in self:
if not float_is_zero(qty, precision_digits=precision):
vals = line._prepare_invoice_line(qty=qty)
vals.update({'invoice_id': invoice_id, 'sale_line_ids': [(6, 0, [line.id])]})
invoice_lines |= self.env['account.invoice.line'].create(vals)
return invoice_lines

Merge Invoice

Basically, Odoo has ability to create 1 invoice from several sales order. The picture below is just example.

All sales orders link to the same invoice. If a product is in multiple sales orders, it will be distributed across multiple lines as well.

Challenge

If the customer wants no duplicate products on the invoice, how can this request be fulfilled? Well, it’s not a difficult matter. What needs to be done is as follows:

  1. Inherit method action_invoice_create from model sale.advance.payment.inv (see addons/sale/wizard/sale_make_invoice_advance.py).
  2. Iterate invoice line and group it by same product. Add up all the quantity grouping by product.
  3. Remove link between model sale.order.line and account.invoice.line by assign [(5, False, False)] to field sale_line_ids in the account.invoice.line.
  4. Remove the duplicate product line.
  5. Update product quantity.

In point 3, if the link still exists, any change in quantity will update the qty_invoiced field in the sale.order.line model. For example, let’s see product “[CARD] Graphics Card” in SO020. The quantity is 20. Changing quantity from 20 to 45 in the invoice will propagate qty_invoiced in SO020 as well (see addons/sale/models/sale.py at method _get_invoice_qty).

Origin state
Updated state

Conclusion

Odoo has great features. Thanks to the automatic relationship between models. Developers only update a field without updating the many objects associated with it. Odoo will scatter and take the rest.

Reference

  1. IT Paragon’s ERP Tech Talk #5 by Rika Nuriwati

--

--