How to combine same item in a single row?

as you can see theres a same item. how to combine this in a single row?

I’d guess these are different batches and that’s the reason why have to select the same item twice (or multiple times). If its not batch numbers that is forcing you to do this, can you tell us your business case why you have to have the same item appearing multiple times in your transactions?

Do you have to do this often (display same item appearing in multiple lines consolidated in your transactions)?
Is it very essential for you (or your clients) that the same item gets combined in the Delivery Note or Invoice?

I will touch upon options after I hear from you.

Thanks

Jay

Hi @JayRam thanks for your response! yes its different batches and thats the reason why enter mutiple item.

i dont have any idea how to do this in my print format.

Apologies, different branches or different batches?

Hi @Denmark_Del_Puso,

You can check out this following code for doing your print format. The following code groups the records using groupby, and then prints the item description and total qty. You can use something similar.

    {% set list = [] %}
    {% set dict = {} %}
    {% set tot_qty = [] %}
    {% set avg_rate = [] %}
    {% set tot_amt = 0 %}
  
    {% set groups = doc.items|groupby('item_code') %}
    {% set no_of_group = groups|length %}
    {% set total_count = 0 %}
{% set group_count = 0 %}
    {% set tot_inv_qty = doc.items|sum(attribute='qty') %}

    {% set item_details = {"total_qty":0.0} %}	

    {% for group in doc.items | groupby('item_code') %}
        {% set group_count = group.list|length %}
    {% set total_count = total_count + group_count %}
    {% set count = 1 %} 
         {% set item_desc = [] %} 
               {% for item in group.list %}
                   
		  {% if count == 1 -%}

		     {% set _ = item_desc.append(item.item_name) %}
                         
		  {%- endif %}
                      {% set count = count + 1 %}

		{%- endfor %}
           
           
           	 <tr>
           <div class="row" style="padding-top:0px;" !important;"> 
                   <td style="width: 35%; text-align: left;"><font size = "1">{{ group.grouper }} - {{ item_desc[0] }}<font></div>
                  
                   {% set tot_qty = group.list|sum(attribute='qty') %}
		{% set tot_amt = group.list|sum(attribute='net_amount') %}
	       {% set avg_rate = tot_amt / tot_qty %}
		
		   <td style="width: 15%; text-align: right;"><font size = "1">{{ (tot_qty | round(2)|string).rstrip('0').rstrip('.') }} Nos<font></div>
		

                   <td style="width: 25%; text-align: right;"><font size = "1">{{ "{:,.2f}".format(avg_rate) }}<font></td>			
	       <td style="width: 25%; text-align: right;"><font size = "1">{{ "{:,.2f}".format(tot_amt) }}<font></td>
        </tr>
2 Likes

im sorry i mean diff. batches.

hi! thankyou for your response when ill try your code i got this error.

Traceback (most recent call last):
File “/home/jun/frappe-bench/apps/frappe/frappe/app.py”, line 56, in application
response = frappe.handler.handle()
File “/home/jun/frappe-bench/apps/frappe/frappe/handler.py”, line 19, in handle
execute_cmd(cmd)
File “/home/jun/frappe-bench/apps/frappe/frappe/handler.py”, line 42, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/jun/frappe-bench/apps/frappe/frappe/init.py”, line 903, in call
return fn(*args, **newargs)
File “/home/jun/frappe-bench/apps/frappe/frappe/www/print.py”, line 181, in get_html_and_style
no_letterhead=no_letterhead, trigger_print=trigger_print),
File “/home/jun/frappe-bench/apps/frappe/frappe/www/print.py”, line 152, in get_html
html = template.render(args, filters={“len”: len})
File “/home/jun/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py”, line 989, in render
return self.environment.handle_exception(exc_info, True)
File “/home/jun/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py”, line 754, in handle_exception
reraise(exc_type, exc_value, tb)
File "

Hi,

This is just a part of the code, to give you an idea. You might have to tweak the code as per your requirements, and field names.

Regards
Uma

hi Sir @UmaG can you give very clear example code about that…
tnx sir :smile:

Hi Carlo,

{% set groups = doc.items|groupby('item_code') %}
{% set no_of_group = groups|length %}

The above set of code will group the set of data on the item_code, and will add the number of groups, to give you the number of groups that will be there. So, you can also use item_group in the groupby clause, or anything else.

Blockquote
{% for group in doc.items | groupby(‘item_code’) %}
{% set group_count = group.list|length %}
{% for item in group.list %}
{% if count == 1 -%}
{% set _ = item_desc.append(item.item_name) %}
{%- endif %}
{% set count = count + 1 %}
{%- endfor %}

             <tr>
       <div class="row" style="padding-top:0px;" !important;"> 
               <td style="width: 35%; text-align: left;"><font size = "1">{{ group.grouper }} - {{ item_desc[0] }}<font></div>
              
               {% set tot_qty = group.list|sum(attribute='qty') %}
            {% set tot_amt = group.list|sum(attribute='net_amount') %}
           {% set avg_rate = tot_amt / tot_qty %}
            
               <td style="width: 15%; text-align: right;"><font size = "1">{{ (tot_qty | round(2)|string).rstrip('0').rstrip('.') }} Nos<font></div>
            

               <td style="width: 25%; text-align: right;"><font size = "1">{{ "{:,.2f}".format(avg_rate) }}<font></td>                      
           <td style="width: 25%; text-align: right;"><font size = "1">{{ "{:,.2f}".format(tot_amt) }}<font></td>
    </tr>

The above code will run a for loop on the item code group, and will print the information. Group.grouper will print the item_code or the field that is grouped on. I hope this is clearer now.

Regards
Uma

1 Like

tnx sir @UmaG :relaxed::smile:

Can anyone give an example,
Suppose I have a table and I need to group row.item_name

    <table class="table table-bordered">
    <tbody>
        <tr>
            <th>Sr</th>
            <th>Item Name</th>
            <th class="text-right">Qty</th>
            <th class="text-right">Rate</th>
            <th class="text-right">Amount</th>
        </tr>
        {%- for row in doc.items -%}
        <tr>
            <td style="width: 3%;">{{ row.idx }}</td>
            <td style="width: 57%;">{{ row.item_name }}</td>
             <td style="width: 10%; text-align: right;">{{ row.qty }} {{ row.uom or row.stock_uom }}</td>
            <td style="width: 15%; text-align: right;">{{
                row.get_formatted("rate", doc) }}</td>
            <td style="width: 15%; text-align: right;">{{
                row.get_formatted("amount", doc) }}</td>
        </tr>
        {%- endfor -%}
    </tbody>
</table>

Skedia

Hi @skedia,

If you wish to group your code on Item Name, you can add these few lines in between:

<table class="table table-bordered">
<tbody>

    {% for group in doc.items | groupby('item_name') %}
             {% for item in group.list %}
		  <tr> 
			
			<div class="row" style="padding-top:0px;" !important;"> 
            	        <td style="width: 20%; text-align: left;"><font size = "1">{{item.item_code }}</font></div>
			</td>
		{%- set item_rec = frappe.get_doc("Item", item.item_code) -%}
		<td style="width: 45%; text-align: left;"><font size = "1"> {{ group.grouper }}</font></div>
			</td>
		<td style="width: 15%; text-align: left;"><font size = "1"> {{ item.stock_uom }}</font></div>
			</td>
		  <td style="width: 20%; text-align: left;"><font size = "1">{{ item.qty }}</font></div>
               	                      
		   </td>
	        </tr>
	{%- endfor %}

    {%- endfor -%}
</tbody>

sir @UmaG there some possible combine the same item but the arrangement of the customer’s P.O can’t be disorderly?

hi can you please share the code to combine same delivery note in row while printing invoice

In the Print setting of doctype tick the group the same item = True.

1 Like