<s> from __future__ import absolute_import , unicode_literals <EOL> from collections import defaultdict <EOL> from django . db . models import Sum <EOL> from django . utils . text import capfirst <EOL> from django . utils . translation import ugettext as _ <EOL> import plata <EOL> from plata . reporting . utils import XLSDocument <EOL> def product_xls ( ) : <EOL> """<STR_LIT>""" <EOL> from plata . product . stock . models import Period <EOL> StockTransaction = plata . stock_model ( ) <EOL> xls = XLSDocument ( ) <EOL> xls . add_sheet ( capfirst ( _ ( '<STR_LIT>' ) ) ) <EOL> _transactions = StockTransaction . objects . filter ( <EOL> period = Period . objects . current ( ) , <EOL> ) . order_by ( ) . values ( '<STR_LIT>' , '<STR_LIT:type>' ) . annotate ( Sum ( '<STR_LIT>' ) ) <EOL> transactions = defaultdict ( dict ) <EOL> for t in _transactions : <EOL> transactions [ t [ '<STR_LIT>' ] ] [ t [ '<STR_LIT:type>' ] ] = t [ '<STR_LIT>' ] <EOL> titles = [ <EOL> capfirst ( _ ( '<STR_LIT>' ) ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> capfirst ( _ ( '<STR_LIT>' ) ) , <EOL> ] <EOL> titles . extend ( <EOL> '<STR_LIT:%s>' % row [ <NUM_LIT:1> ] for row in StockTransaction . TYPE_CHOICES ) <EOL> data = [ ] <EOL> for product in plata . product_model ( ) . objects . all ( ) . select_related ( ) : <EOL> row = [ <EOL> product , <EOL> getattr ( product , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> getattr ( product , '<STR_LIT>' , - <NUM_LIT:1> ) , <EOL> ] <EOL> row . extend ( <EOL> transactions [ product . id ] . get ( key , '<STR_LIT>' ) <EOL> for key , name in StockTransaction . TYPE_CHOICES ) <EOL> data . append ( row ) <EOL> xls . table ( titles , data ) <EOL> return xls </s>
