[MERGE] from trunk
[odoo/odoo.git] / addons / point_of_sale / controllers / main.py
1 # -*- coding: utf-8 -*-
2 import logging
3 import simplejson
4 import os
5 import openerp
6
7 from openerp.addons.web.controllers.main import manifest_list, module_boot, html_template
8
9 class PointOfSaleController(openerp.addons.web.http.Controller):
10     _cp_path = '/pos'
11
12     @openerp.addons.web.http.httprequest
13     def app(self, req, s_action=None, **kw):
14         js = "\n        ".join('<script type="text/javascript" src="%s"></script>' % i for i in manifest_list(req, None, 'js'))
15         css = "\n        ".join('<link rel="stylesheet" href="%s">' % i for i in manifest_list(req, None, 'css'))
16
17         cookie = req.httprequest.cookies.get("instance0|session_id")
18         session_id = cookie.replace("%22","")
19         template = html_template.replace('<html','<html manifest="/pos/manifest?session_id=%s"'%session_id)
20         r = template % {
21             'js': js,
22             'css': css,
23             'modules': simplejson.dumps(module_boot(req)),
24             'init': 'var wc = new s.web.WebClient();wc.appendTo($(document.body));'
25         }
26         return r
27
28     @openerp.addons.web.http.httprequest
29     def manifest(self, req, **kwargs):
30         """ This generates a HTML5 cache manifest files that preloads the categories and products thumbnails 
31             and other ressources necessary for the point of sale to work offline """
32
33         ml = ["CACHE MANIFEST"]
34
35         # loading all the images in the static/src/img/* directories
36         def load_css_img(srcdir,dstdir):
37             for f in os.listdir(srcdir):
38                 path = os.path.join(srcdir,f)
39                 dstpath = os.path.join(dstdir,f)
40                 if os.path.isdir(path) :
41                     load_css_img(path,dstpath)
42                 elif f.endswith(('.png','.PNG','.jpg','.JPG','.jpeg','.JPEG','.gif','.GIF')):
43                     ml.append(dstpath)
44
45         imgdir = openerp.modules.get_module_resource('point_of_sale','static/src/img');
46         load_css_img(imgdir,'/point_of_sale/static/src/img')
47         
48         products = req.session.model('product.product')
49         for p in products.search_read([('pos_categ_id','!=',False)], ['name']):
50             product_id = p['id']
51             url = "/web/binary/image?session_id=%s&model=product.product&field=image&id=%s" % (req.session_id, product_id)
52             ml.append(url)
53         
54         categories = req.session.model('pos.category')
55         for c in categories.search_read([],['name']):
56             category_id = c['id']
57             url = "/web/binary/image?session_id=%s&model=pos.category&field=image&id=%s" % (req.session_id, category_id)
58             ml.append(url)
59
60         ml += ["NETWORK:","*"]
61         m = "\n".join(ml)
62
63         return m
64
65     @openerp.addons.web.http.jsonrequest
66     def dispatch(self, request, iface, **kwargs):
67         method = 'iface_%s' % iface
68         return getattr(self, method)(request, **kwargs)
69
70     @openerp.addons.web.http.jsonrequest
71     def scan_item_success(self, request, ean):
72         """
73         A product has been scanned with success
74         """
75         print 'scan_item_success: ' + str(ean)
76         return 
77
78     @openerp.addons.web.http.jsonrequest
79     def scan_item_error_unrecognized(self, request, ean):
80         """
81         A product has been scanned without success
82         """
83         print 'scan_item_error_unrecognized: ' + str(ean)
84         return 
85
86     @openerp.addons.web.http.jsonrequest
87     def help_needed(self, request):
88         """
89         The user wants an help (ex: light is on)
90         """
91         print "help_needed"
92         return 
93
94     @openerp.addons.web.http.jsonrequest
95     def help_canceled(self, request):
96         """
97         The user stops the help request
98         """
99         print "help_canceled"
100         return 
101
102     @openerp.addons.web.http.jsonrequest
103     def weighting_start(self, request):
104         print "weighting_start"
105         return 
106
107     @openerp.addons.web.http.jsonrequest
108     def weighting_read_kg(self, request):
109         print "weighting_read_kg"
110         return 0.0
111
112     @openerp.addons.web.http.jsonrequest
113     def weighting_end(self, request):
114         print "weighting_end"
115         return 
116
117     @openerp.addons.web.http.jsonrequest
118     def payment_request(self, request, price):
119         """
120         The PoS will activate the method payment 
121         """
122         print "payment_request: price:"+str(price)
123         return 'ok'
124
125     @openerp.addons.web.http.jsonrequest
126     def payment_status(self, request):
127         print "payment_status"
128         return { 'status':'waiting' } 
129
130     @openerp.addons.web.http.jsonrequest
131     def payment_cancel(self, request):
132         print "payment_cancel"
133         return 
134
135     @openerp.addons.web.http.jsonrequest
136     def transaction_start(self, request):
137         print 'transaction_start'
138         return 
139
140     @openerp.addons.web.http.jsonrequest
141     def transaction_end(self, request):
142         print 'transaction_end'
143         return 
144
145     @openerp.addons.web.http.jsonrequest
146     def cashier_mode_activated(self, request):
147         print 'cashier_mode_activated'
148         return 
149
150     @openerp.addons.web.http.jsonrequest
151     def cashier_mode_deactivated(self, request):
152         print 'cashier_mode_deactivated'
153         return 
154
155     @openerp.addons.web.http.jsonrequest
156     def open_cashbox(self, request):
157         print 'open_cashbox'
158         return
159
160     @openerp.addons.web.http.jsonrequest
161     def print_receipt(self, request, receipt):
162         print 'print_receipt' + str(receipt)
163         return
164
165     @openerp.addons.web.http.jsonrequest
166     def print_pdf_invoice(self, request, pdfinvoice):
167         print 'print_pdf_invoice' + str(pdfinvoice)
168         return
169
170