2010-01-18 12:52:11 +00:00
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
##############################################################################
|
2010-03-16 14:03:02 +00:00
|
|
|
|
#
|
2010-01-18 12:52:11 +00:00
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2010-03-16 14:03:02 +00:00
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-18 12:52:11 +00:00
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
2012-10-26 11:39:01 +00:00
|
|
|
|
|
2012-10-31 14:24:48 +00:00
|
|
|
|
from xml.sax.saxutils import escape
|
2012-10-24 13:39:18 +00:00
|
|
|
|
import time
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp.osv import fields, osv
|
2012-11-05 18:18:14 +00:00
|
|
|
|
from datetime import datetime
|
2012-10-24 13:39:18 +00:00
|
|
|
|
from lxml import etree
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp import tools
|
|
|
|
|
from openerp.tools.translate import _
|
2012-10-25 14:44:57 +00:00
|
|
|
|
|
2012-10-24 13:39:18 +00:00
|
|
|
|
class lunch_order(osv.Model):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
lunch order (contains one or more lunch order line(s))
|
|
|
|
|
"""
|
2010-03-17 10:43:25 +00:00
|
|
|
|
_name = 'lunch.order'
|
2012-10-24 13:39:18 +00:00
|
|
|
|
_description = 'Lunch Order'
|
2012-12-18 13:03:17 +00:00
|
|
|
|
_order = 'date desc'
|
2010-01-18 12:52:11 +00:00
|
|
|
|
|
2013-01-03 10:39:35 +00:00
|
|
|
|
def name_get(self, cr, uid, ids, context=None):
|
|
|
|
|
if not ids:
|
|
|
|
|
return []
|
|
|
|
|
res = []
|
|
|
|
|
for elmt in self.browse(cr, uid, ids, context=context):
|
|
|
|
|
name = _("Lunch Order")
|
|
|
|
|
name = name + ' ' + str(elmt.id)
|
|
|
|
|
res.append((elmt.id, name))
|
|
|
|
|
return res
|
|
|
|
|
|
2012-10-25 14:44:57 +00:00
|
|
|
|
def _price_get(self, cr, uid, ids, name, arg, context=None):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
get and sum the order lines' price
|
|
|
|
|
"""
|
|
|
|
|
result = dict.fromkeys(ids, 0)
|
2012-10-26 11:39:01 +00:00
|
|
|
|
for order in self.browse(cr, uid, ids, context=context):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
result[order.id] = sum(order_line.product_id.price
|
|
|
|
|
for order_line in order.order_line_ids)
|
2012-10-24 13:39:18 +00:00
|
|
|
|
return result
|
|
|
|
|
|
2012-11-06 08:19:04 +00:00
|
|
|
|
def _fetch_orders_from_lines(self, cr, uid, ids, name, context=None):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-11-06 08:19:04 +00:00
|
|
|
|
return the list of lunch orders to which belong the order lines `ids´
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
result = set()
|
2012-10-26 12:58:08 +00:00
|
|
|
|
for order_line in self.browse(cr, uid, ids, context=context):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
if order_line.order_id:
|
|
|
|
|
result.add(order_line.order_id.id)
|
|
|
|
|
return list(result)
|
2012-10-26 11:39:01 +00:00
|
|
|
|
|
|
|
|
|
def add_preference(self, cr, uid, ids, pref_id, context=None):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
create a new order line based on the preference selected (pref_id)
|
|
|
|
|
"""
|
2012-11-05 13:47:01 +00:00
|
|
|
|
assert len(ids) == 1
|
2012-10-24 13:39:18 +00:00
|
|
|
|
orderline_ref = self.pool.get('lunch.order.line')
|
2012-10-26 11:39:01 +00:00
|
|
|
|
prod_ref = self.pool.get('lunch.product')
|
2012-11-05 13:47:01 +00:00
|
|
|
|
order = self.browse(cr, uid, ids[0], context=context)
|
|
|
|
|
pref = orderline_ref.browse(cr, uid, pref_id, context=context)
|
2012-11-02 08:15:48 +00:00
|
|
|
|
new_order_line = {
|
|
|
|
|
'date': order.date,
|
|
|
|
|
'user_id': uid,
|
|
|
|
|
'product_id': pref.product_id.id,
|
|
|
|
|
'note': pref.note,
|
|
|
|
|
'order_id': order.id,
|
|
|
|
|
'price': pref.product_id.price,
|
|
|
|
|
'supplier': pref.product_id.supplier.id
|
|
|
|
|
}
|
2012-11-06 08:19:04 +00:00
|
|
|
|
return orderline_ref.create(cr, uid, new_order_line, context=context)
|
2012-10-24 13:39:18 +00:00
|
|
|
|
|
2012-10-26 11:39:01 +00:00
|
|
|
|
def _alerts_get(self, cr, uid, ids, name, arg, context=None):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
get the alerts to display on the order form
|
|
|
|
|
"""
|
2012-11-05 18:18:14 +00:00
|
|
|
|
result = {}
|
2012-11-06 09:03:00 +00:00
|
|
|
|
alert_msg = self._default_alerts_get(cr, uid, context=context)
|
2012-11-05 18:18:14 +00:00
|
|
|
|
for order in self.browse(cr, uid, ids, context=context):
|
|
|
|
|
if order.state == 'new':
|
|
|
|
|
result[order.id] = alert_msg
|
2012-10-24 13:39:18 +00:00
|
|
|
|
return result
|
|
|
|
|
|
2012-10-26 11:39:01 +00:00
|
|
|
|
def check_day(self, alert):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
This method is used by can_display_alert
|
|
|
|
|
to check if the alert day corresponds
|
|
|
|
|
to the current day
|
2012-10-26 11:39:01 +00:00
|
|
|
|
"""
|
2012-10-25 08:10:59 +00:00
|
|
|
|
today = datetime.now().isoweekday()
|
2012-11-05 11:01:47 +00:00
|
|
|
|
assert 1 <= today <= 7, "Should be between 1 and 7"
|
2012-12-26 10:55:40 +00:00
|
|
|
|
mapping = dict((idx, name) for idx, name in enumerate('days monday tuesday wednesday thursday friday saturday sunday'.split()))
|
|
|
|
|
return alert[mapping[today]]
|
2012-10-25 08:10:59 +00:00
|
|
|
|
|
2012-10-26 11:39:01 +00:00
|
|
|
|
def can_display_alert(self, alert):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-11-06 08:19:04 +00:00
|
|
|
|
This method check if the alert can be displayed today
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-11-05 18:18:14 +00:00
|
|
|
|
if alert.alter_type == 'specific':
|
|
|
|
|
#the alert is only activated on a specific day
|
2012-11-06 08:19:04 +00:00
|
|
|
|
return alert.specific_day == time.strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
|
2012-11-05 18:18:14 +00:00
|
|
|
|
elif alert.alter_type == 'week':
|
2012-10-26 11:39:01 +00:00
|
|
|
|
#the alert is activated during some days of the week
|
|
|
|
|
return self.check_day(alert)
|
2013-01-11 18:45:24 +00:00
|
|
|
|
return True # alter_type == 'days' (every day)
|
2012-10-26 11:39:01 +00:00
|
|
|
|
|
2012-11-06 08:52:41 +00:00
|
|
|
|
def _default_alerts_get(self, cr, uid, context=None):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-11-06 08:19:04 +00:00
|
|
|
|
get the alerts to display on the order form
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-10-24 13:39:18 +00:00
|
|
|
|
alert_ref = self.pool.get('lunch.alert')
|
2012-11-06 08:19:04 +00:00
|
|
|
|
alert_ids = alert_ref.search(cr, uid, [], context=context)
|
2012-10-31 10:23:07 +00:00
|
|
|
|
alert_msg = []
|
2012-11-05 13:47:01 +00:00
|
|
|
|
for alert in alert_ref.browse(cr, uid, alert_ids, context=context):
|
2012-11-06 08:19:04 +00:00
|
|
|
|
#check if the address must be displayed today
|
2012-10-26 11:39:01 +00:00
|
|
|
|
if self.can_display_alert(alert):
|
2012-11-06 08:19:04 +00:00
|
|
|
|
#display the address only during its active time
|
2012-11-06 08:20:47 +00:00
|
|
|
|
mynow = fields.datetime.context_timestamp(cr, uid, datetime.now(), context=context)
|
2012-11-02 08:15:48 +00:00
|
|
|
|
hour_to = int(alert.active_to)
|
2012-11-06 08:19:04 +00:00
|
|
|
|
min_to = int((alert.active_to - hour_to) * 60)
|
|
|
|
|
to_alert = datetime.strptime(str(hour_to) + ":" + str(min_to), "%H:%M")
|
2012-11-02 08:15:48 +00:00
|
|
|
|
hour_from = int(alert.active_from)
|
2012-11-06 08:19:04 +00:00
|
|
|
|
min_from = int((alert.active_from - hour_from) * 60)
|
|
|
|
|
from_alert = datetime.strptime(str(hour_from) + ":" + str(min_from), "%H:%M")
|
|
|
|
|
if mynow.time() >= from_alert.time() and mynow.time() <= to_alert.time():
|
2012-10-31 10:23:07 +00:00
|
|
|
|
alert_msg.append(alert.message)
|
|
|
|
|
return '\n'.join(alert_msg)
|
2012-10-24 13:39:18 +00:00
|
|
|
|
|
2012-11-05 13:47:01 +00:00
|
|
|
|
def onchange_price(self, cr, uid, ids, order_line_ids, context=None):
|
2012-11-05 18:18:14 +00:00
|
|
|
|
"""
|
2012-11-05 11:01:47 +00:00
|
|
|
|
Onchange methode that refresh the total price of order
|
|
|
|
|
"""
|
2012-11-05 18:18:14 +00:00
|
|
|
|
res = {'value': {'total': 0.0}}
|
|
|
|
|
order_line_ids = self.resolve_o2m_commands_to_record_dicts(cr, uid, "order_line_ids", order_line_ids, ["price"], context=context)
|
2012-10-26 12:58:08 +00:00
|
|
|
|
if order_line_ids:
|
2012-10-24 13:39:18 +00:00
|
|
|
|
tot = 0.0
|
2012-10-31 14:24:48 +00:00
|
|
|
|
product_ref = self.pool.get("lunch.product")
|
2012-10-26 12:58:08 +00:00
|
|
|
|
for prod in order_line_ids:
|
2012-10-31 14:24:48 +00:00
|
|
|
|
if 'product_id' in prod:
|
2012-11-05 13:47:01 +00:00
|
|
|
|
tot += product_ref.browse(cr, uid, prod['product_id'], context=context).price
|
2012-10-31 14:24:48 +00:00
|
|
|
|
else:
|
|
|
|
|
tot += prod['price']
|
2012-11-05 18:18:14 +00:00
|
|
|
|
res = {'value': {'total': tot}}
|
2012-10-24 13:39:18 +00:00
|
|
|
|
return res
|
2010-03-17 10:43:25 +00:00
|
|
|
|
|
2012-10-24 13:39:18 +00:00
|
|
|
|
def __getattr__(self, attr):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-11-06 08:19:04 +00:00
|
|
|
|
this method catch unexisting method call and if it starts with
|
2012-11-05 11:01:47 +00:00
|
|
|
|
add_preference_'n' we execute the add_preference method with
|
|
|
|
|
'n' as parameter
|
|
|
|
|
"""
|
2012-10-24 13:39:18 +00:00
|
|
|
|
if attr.startswith('add_preference_'):
|
|
|
|
|
pref_id = int(attr[15:])
|
|
|
|
|
def specific_function(cr, uid, ids, context=None):
|
|
|
|
|
return self.add_preference(cr, uid, ids, pref_id, context=context)
|
|
|
|
|
return specific_function
|
2013-01-25 21:17:51 +00:00
|
|
|
|
return super(lunch_order, self).__getattr__(attr)
|
2012-10-24 13:39:18 +00:00
|
|
|
|
|
|
|
|
|
def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
Add preferences in the form view of order.line
|
|
|
|
|
"""
|
2012-10-24 13:39:18 +00:00
|
|
|
|
res = super(lunch_order,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
|
2012-11-02 08:15:48 +00:00
|
|
|
|
line_ref = self.pool.get("lunch.order.line")
|
2012-10-24 13:39:18 +00:00
|
|
|
|
if view_type == 'form':
|
2012-11-05 13:47:01 +00:00
|
|
|
|
doc = etree.XML(res['arch'])
|
2013-01-09 11:08:20 +00:00
|
|
|
|
pref_ids = line_ref.search(cr, uid, [('user_id', '=', uid)], order='id desc', context=context)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
xml_start = etree.Element("div")
|
|
|
|
|
#If there are no preference (it's the first time for the user)
|
2012-11-02 08:15:48 +00:00
|
|
|
|
if len(pref_ids)==0:
|
2012-11-05 13:47:01 +00:00
|
|
|
|
#create Elements
|
|
|
|
|
xml_no_pref_1 = etree.Element("div")
|
|
|
|
|
xml_no_pref_1.set('class','oe_inline oe_lunch_intro')
|
|
|
|
|
xml_no_pref_2 = etree.Element("h3")
|
|
|
|
|
xml_no_pref_2.text = _("This is the first time you order a meal")
|
|
|
|
|
xml_no_pref_3 = etree.Element("p")
|
|
|
|
|
xml_no_pref_3.set('class','oe_grey')
|
|
|
|
|
xml_no_pref_3.text = _("Select a product and put your order comments on the note.")
|
|
|
|
|
xml_no_pref_4 = etree.Element("p")
|
|
|
|
|
xml_no_pref_4.set('class','oe_grey')
|
|
|
|
|
xml_no_pref_4.text = _("Your favorite meals will be created based on your last orders.")
|
|
|
|
|
xml_no_pref_5 = etree.Element("p")
|
|
|
|
|
xml_no_pref_5.set('class','oe_grey')
|
|
|
|
|
xml_no_pref_5.text = _("Don't forget the alerts displayed in the reddish area")
|
|
|
|
|
#structure Elements
|
|
|
|
|
xml_start.append(xml_no_pref_1)
|
|
|
|
|
xml_no_pref_1.append(xml_no_pref_2)
|
|
|
|
|
xml_no_pref_1.append(xml_no_pref_3)
|
|
|
|
|
xml_no_pref_1.append(xml_no_pref_4)
|
|
|
|
|
xml_no_pref_1.append(xml_no_pref_5)
|
2012-11-05 18:18:14 +00:00
|
|
|
|
#Else: the user already have preferences so we display them
|
2012-10-31 14:24:48 +00:00
|
|
|
|
else:
|
2012-11-06 08:19:04 +00:00
|
|
|
|
preferences = line_ref.browse(cr, uid, pref_ids, context=context)
|
2012-10-26 11:39:01 +00:00
|
|
|
|
categories = {} #store the different categories of products in preference
|
2012-11-05 13:47:01 +00:00
|
|
|
|
count = 0
|
2012-10-26 11:39:01 +00:00
|
|
|
|
for pref in preferences:
|
2012-11-05 13:47:01 +00:00
|
|
|
|
#For each preference
|
2012-11-02 08:15:48 +00:00
|
|
|
|
categories.setdefault(pref.product_id.category_id.name, {})
|
2012-11-05 13:47:01 +00:00
|
|
|
|
#if this product has already been added to the categories dictionnary
|
|
|
|
|
if pref.product_id.id in categories[pref.product_id.category_id.name]:
|
|
|
|
|
#we check if for the same product the note has already been added
|
|
|
|
|
if pref.note not in categories[pref.product_id.category_id.name][pref.product_id.id]:
|
|
|
|
|
#if it's not the case then we add this to preferences
|
|
|
|
|
categories[pref.product_id.category_id.name][pref.product_id.id][pref.note] = pref
|
|
|
|
|
#if this product is not in the dictionnay, we add it
|
|
|
|
|
else:
|
|
|
|
|
categories[pref.product_id.category_id.name][pref.product_id.id] = {}
|
|
|
|
|
categories[pref.product_id.category_id.name][pref.product_id.id][pref.note] = pref
|
2012-11-02 08:15:48 +00:00
|
|
|
|
|
|
|
|
|
currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id
|
|
|
|
|
|
2012-11-05 13:47:01 +00:00
|
|
|
|
#For each preferences that we get, we will create the XML structure
|
2013-03-21 11:22:12 +00:00
|
|
|
|
for key, value in categories.items():
|
2012-11-05 13:47:01 +00:00
|
|
|
|
xml_pref_1 = etree.Element("div")
|
2013-03-21 11:22:12 +00:00
|
|
|
|
xml_pref_1.set('class', 'oe_lunch_30pc')
|
2012-11-05 13:47:01 +00:00
|
|
|
|
xml_pref_2 = etree.Element("h2")
|
|
|
|
|
xml_pref_2.text = key
|
|
|
|
|
xml_pref_1.append(xml_pref_2)
|
2012-10-30 14:02:44 +00:00
|
|
|
|
i = 0
|
2012-11-05 13:47:01 +00:00
|
|
|
|
value = value.values()
|
2013-03-21 11:22:12 +00:00
|
|
|
|
#TODO: sorted_values is used for a quick and dirty hack in order to display the 5 last orders of each categories.
|
|
|
|
|
#It would be better to fetch only the 5 items to display instead of fetching everything then sorting them in order to keep only the 5 last.
|
|
|
|
|
#NB: The note could also be ignored + we could fetch the preferences on the most ordered products instead of the last ones...
|
2013-01-09 11:08:20 +00:00
|
|
|
|
sorted_values = {}
|
2012-10-26 11:39:01 +00:00
|
|
|
|
for val in value:
|
2013-01-09 11:08:20 +00:00
|
|
|
|
for elmt in val.values():
|
2013-03-21 11:22:12 +00:00
|
|
|
|
sorted_values[elmt.id] = elmt
|
|
|
|
|
for key, pref in sorted(sorted_values.iteritems(), key=lambda (k, v): (k, v), reverse=True):
|
2013-01-09 11:08:20 +00:00
|
|
|
|
#We only show 5 preferences per category (or it will be too long)
|
2013-03-21 11:22:12 +00:00
|
|
|
|
if i == 5:
|
|
|
|
|
break
|
|
|
|
|
i += 1
|
2013-01-09 11:08:20 +00:00
|
|
|
|
xml_pref_3 = etree.Element("div")
|
|
|
|
|
xml_pref_3.set('class','oe_lunch_vignette')
|
|
|
|
|
xml_pref_1.append(xml_pref_3)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
2013-01-09 11:08:20 +00:00
|
|
|
|
xml_pref_4 = etree.Element("span")
|
|
|
|
|
xml_pref_4.set('class','oe_lunch_button')
|
|
|
|
|
xml_pref_3.append(xml_pref_4)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
2013-01-09 11:08:20 +00:00
|
|
|
|
xml_pref_5 = etree.Element("button")
|
|
|
|
|
xml_pref_5.set('name',"add_preference_"+str(pref.id))
|
|
|
|
|
xml_pref_5.set('class','oe_link oe_i oe_button_plus')
|
|
|
|
|
xml_pref_5.set('type','object')
|
|
|
|
|
xml_pref_5.set('string','+')
|
|
|
|
|
xml_pref_4.append(xml_pref_5)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
2013-01-09 11:08:20 +00:00
|
|
|
|
xml_pref_6 = etree.Element("button")
|
|
|
|
|
xml_pref_6.set('name',"add_preference_"+str(pref.id))
|
|
|
|
|
xml_pref_6.set('class','oe_link oe_button_add')
|
|
|
|
|
xml_pref_6.set('type','object')
|
|
|
|
|
xml_pref_6.set('string',_("Add"))
|
|
|
|
|
xml_pref_4.append(xml_pref_6)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
2013-01-09 11:08:20 +00:00
|
|
|
|
xml_pref_7 = etree.Element("div")
|
|
|
|
|
xml_pref_7.set('class','oe_group_text_button')
|
|
|
|
|
xml_pref_3.append(xml_pref_7)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
2013-01-09 11:08:20 +00:00
|
|
|
|
xml_pref_8 = etree.Element("div")
|
|
|
|
|
xml_pref_8.set('class','oe_lunch_text')
|
|
|
|
|
xml_pref_8.text = escape(pref.product_id.name)+str(" ")
|
|
|
|
|
xml_pref_7.append(xml_pref_8)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
2013-01-09 11:08:20 +00:00
|
|
|
|
price = pref.product_id.price or 0.0
|
|
|
|
|
cur = currency.name or ''
|
|
|
|
|
xml_pref_9 = etree.Element("span")
|
|
|
|
|
xml_pref_9.set('class','oe_tag')
|
|
|
|
|
xml_pref_9.text = str(price)+str(" ")+cur
|
|
|
|
|
xml_pref_8.append(xml_pref_9)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
2013-01-09 11:08:20 +00:00
|
|
|
|
xml_pref_10 = etree.Element("div")
|
|
|
|
|
xml_pref_10.set('class','oe_grey')
|
|
|
|
|
xml_pref_10.text = escape(pref.note or '')
|
|
|
|
|
xml_pref_3.append(xml_pref_10)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
2013-01-09 11:08:20 +00:00
|
|
|
|
xml_start.append(xml_pref_1)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
|
|
|
|
|
first_node = doc.xpath("//div[@name='preferences']")
|
|
|
|
|
if first_node and len(first_node)>0:
|
|
|
|
|
first_node[0].append(xml_start)
|
2012-10-31 14:24:48 +00:00
|
|
|
|
res['arch'] = etree.tostring(doc)
|
2010-01-18 12:52:11 +00:00
|
|
|
|
return res
|
|
|
|
|
|
2010-03-17 10:43:25 +00:00
|
|
|
|
_columns = {
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'user_id': fields.many2one('res.users', 'User Name', required=True, readonly=True, states={'new':[('readonly', False)]}),
|
|
|
|
|
'date': fields.date('Date', required=True, readonly=True, states={'new':[('readonly', False)]}),
|
2014-07-06 14:44:26 +00:00
|
|
|
|
'order_line_ids': fields.one2many('lunch.order.line', 'order_id', 'Products',
|
|
|
|
|
ondelete="cascade", readonly=True, states={'new':[('readonly', False)]},
|
|
|
|
|
copy=True),
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'total': fields.function(_price_get, string="Total", store={
|
2012-11-06 08:19:04 +00:00
|
|
|
|
'lunch.order.line': (_fetch_orders_from_lines, ['product_id','order_id'], 20),
|
2012-10-26 12:58:08 +00:00
|
|
|
|
}),
|
2012-11-05 13:47:01 +00:00
|
|
|
|
'state': fields.selection([('new', 'New'), \
|
|
|
|
|
('confirmed','Confirmed'), \
|
|
|
|
|
('cancelled','Cancelled'), \
|
|
|
|
|
('partially','Partially Confirmed')] \
|
2014-07-06 14:44:26 +00:00
|
|
|
|
,'Status', readonly=True, select=True, copy=False),
|
2012-10-24 13:39:18 +00:00
|
|
|
|
'alerts': fields.function(_alerts_get, string="Alerts", type='text'),
|
2010-01-18 12:52:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-17 10:43:25 +00:00
|
|
|
|
_defaults = {
|
2010-06-16 11:51:39 +00:00
|
|
|
|
'user_id': lambda self, cr, uid, context: uid,
|
2012-02-13 18:07:41 +00:00
|
|
|
|
'date': fields.date.context_today,
|
2012-10-26 11:39:01 +00:00
|
|
|
|
'state': 'new',
|
2012-10-24 13:39:18 +00:00
|
|
|
|
'alerts': _default_alerts_get,
|
2010-01-18 12:52:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-26 11:39:01 +00:00
|
|
|
|
|
|
|
|
|
class lunch_order_line(osv.Model):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-11-05 18:18:14 +00:00
|
|
|
|
lunch order line: one lunch order can have many order lines
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-10-24 14:20:25 +00:00
|
|
|
|
_name = 'lunch.order.line'
|
|
|
|
|
_description = 'lunch order line'
|
|
|
|
|
|
2012-11-05 13:47:01 +00:00
|
|
|
|
def onchange_price(self, cr, uid, ids, product_id, context=None):
|
2012-10-30 08:47:01 +00:00
|
|
|
|
if product_id:
|
2012-11-05 13:47:01 +00:00
|
|
|
|
price = self.pool.get('lunch.product').browse(cr, uid, product_id, context=context).price
|
2012-10-24 14:20:25 +00:00
|
|
|
|
return {'value': {'price': price}}
|
2012-10-30 08:47:01 +00:00
|
|
|
|
return {'value': {'price': 0.0}}
|
2012-10-24 14:20:25 +00:00
|
|
|
|
|
2012-11-05 13:47:01 +00:00
|
|
|
|
def order(self, cr, uid, ids, context=None):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
The order_line is ordered to the supplier but isn't received yet
|
|
|
|
|
"""
|
2014-07-06 14:44:26 +00:00
|
|
|
|
self.write(cr, uid, ids, {'state': 'ordered'}, context=context)
|
2012-11-05 18:18:14 +00:00
|
|
|
|
return self._update_order_lines(cr, uid, ids, context=context)
|
2012-10-24 14:20:25 +00:00
|
|
|
|
|
2012-11-05 13:47:01 +00:00
|
|
|
|
def confirm(self, cr, uid, ids, context=None):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
confirm one or more order line, update order status and create new cashmove
|
|
|
|
|
"""
|
2012-10-24 14:20:25 +00:00
|
|
|
|
cashmove_ref = self.pool.get('lunch.cashmove')
|
2012-11-05 13:47:01 +00:00
|
|
|
|
for order_line in self.browse(cr, uid, ids, context=context):
|
2012-11-06 08:19:04 +00:00
|
|
|
|
if order_line.state != 'confirmed':
|
2012-11-05 11:01:47 +00:00
|
|
|
|
values = {
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'user_id': order_line.user_id.id,
|
|
|
|
|
'amount': -order_line.price,
|
2012-11-05 11:01:47 +00:00
|
|
|
|
'description': order_line.product_id.name,
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'order_id': order_line.id,
|
|
|
|
|
'state': 'order',
|
2012-11-05 11:01:47 +00:00
|
|
|
|
'date': order_line.date,
|
|
|
|
|
}
|
|
|
|
|
cashmove_ref.create(cr, uid, values, context=context)
|
2014-07-06 14:44:26 +00:00
|
|
|
|
order_line.write({'state': 'confirmed'})
|
2012-11-05 13:47:01 +00:00
|
|
|
|
return self._update_order_lines(cr, uid, ids, context=context)
|
2012-11-02 08:15:48 +00:00
|
|
|
|
|
|
|
|
|
def _update_order_lines(self, cr, uid, ids, context=None):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
Update the state of lunch.order based on its orderlines
|
|
|
|
|
"""
|
2012-11-02 12:35:08 +00:00
|
|
|
|
orders_ref = self.pool.get('lunch.order')
|
|
|
|
|
orders = []
|
2012-11-05 13:47:01 +00:00
|
|
|
|
for order_line in self.browse(cr, uid, ids, context=context):
|
2012-11-02 12:35:08 +00:00
|
|
|
|
orders.append(order_line.order_id)
|
|
|
|
|
for order in set(orders):
|
2012-10-24 14:20:25 +00:00
|
|
|
|
isconfirmed = True
|
2012-11-02 08:15:48 +00:00
|
|
|
|
for orderline in order.order_line_ids:
|
2012-10-30 08:47:01 +00:00
|
|
|
|
if orderline.state == 'new':
|
2012-10-24 14:20:25 +00:00
|
|
|
|
isconfirmed = False
|
2012-10-30 08:47:01 +00:00
|
|
|
|
if orderline.state == 'cancelled':
|
2012-10-24 14:20:25 +00:00
|
|
|
|
isconfirmed = False
|
2012-11-06 08:26:33 +00:00
|
|
|
|
orders_ref.write(cr, uid, [order.id], {'state': 'partially'}, context=context)
|
2012-11-02 08:15:48 +00:00
|
|
|
|
if isconfirmed:
|
2012-11-06 08:26:33 +00:00
|
|
|
|
orders_ref.write(cr, uid, [order.id], {'state': 'confirmed'}, context=context)
|
2012-10-24 14:20:25 +00:00
|
|
|
|
return {}
|
|
|
|
|
|
2012-11-05 13:47:01 +00:00
|
|
|
|
def cancel(self, cr, uid, ids, context=None):
|
2012-11-06 08:19:04 +00:00
|
|
|
|
"""
|
|
|
|
|
cancel one or more order.line, update order status and unlink existing cashmoves
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
2012-10-24 14:20:25 +00:00
|
|
|
|
cashmove_ref = self.pool.get('lunch.cashmove')
|
2014-07-06 14:44:26 +00:00
|
|
|
|
self.write(cr, uid, ids, {'state':'cancelled'}, context=context)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
for order_line in self.browse(cr, uid, ids, context=context):
|
2012-11-06 08:19:04 +00:00
|
|
|
|
cash_ids = [cash.id for cash in order_line.cashmove]
|
|
|
|
|
cashmove_ref.unlink(cr, uid, cash_ids, context=context)
|
2012-11-05 13:47:01 +00:00
|
|
|
|
return self._update_order_lines(cr, uid, ids, context=context)
|
2013-02-19 09:09:23 +00:00
|
|
|
|
|
|
|
|
|
def _get_line_order_ids(self, cr, uid, ids, context=None):
|
|
|
|
|
"""
|
|
|
|
|
return the list of lunch.order.lines ids to which belong the lunch.order 'ids'
|
|
|
|
|
"""
|
|
|
|
|
result = set()
|
|
|
|
|
for lunch_order in self.browse(cr, uid, ids, context=context):
|
|
|
|
|
for lines in lunch_order.order_line_ids:
|
|
|
|
|
result.add(lines.id)
|
|
|
|
|
return list(result)
|
2012-10-24 14:20:25 +00:00
|
|
|
|
|
|
|
|
|
_columns = {
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'name': fields.related('product_id', 'name', readonly=True),
|
|
|
|
|
'order_id': fields.many2one('lunch.order', 'Order', ondelete='cascade'),
|
|
|
|
|
'product_id': fields.many2one('lunch.product', 'Product', required=True),
|
2013-02-19 09:09:23 +00:00
|
|
|
|
'date': fields.related('order_id', 'date', type='date', string="Date", readonly=True, store={
|
|
|
|
|
'lunch.order': (_get_line_order_ids, ['date'], 10),
|
2013-02-19 09:40:01 +00:00
|
|
|
|
'lunch.order.line': (lambda self, cr, uid, ids, ctx: ids, [], 10),
|
2013-02-19 09:09:23 +00:00
|
|
|
|
}),
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'supplier': fields.related('product_id', 'supplier', type='many2one', relation='res.partner', string="Supplier", readonly=True, store=True),
|
|
|
|
|
'user_id': fields.related('order_id', 'user_id', type='many2one', relation='res.users', string='User', readonly=True, store=True),
|
|
|
|
|
'note': fields.text('Note'),
|
|
|
|
|
'price': fields.float("Price"),
|
2012-11-05 13:47:01 +00:00
|
|
|
|
'state': fields.selection([('new', 'New'), \
|
2012-11-05 18:18:14 +00:00
|
|
|
|
('confirmed', 'Received'), \
|
|
|
|
|
('ordered', 'Ordered'), \
|
|
|
|
|
('cancelled', 'Cancelled')], \
|
2012-11-05 13:47:01 +00:00
|
|
|
|
'Status', readonly=True, select=True),
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'cashmove': fields.one2many('lunch.cashmove', 'order_id', 'Cash Move', ondelete='cascade'),
|
|
|
|
|
|
2012-10-24 14:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
_defaults = {
|
2012-10-30 08:47:01 +00:00
|
|
|
|
'state': 'new',
|
2012-10-24 14:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class lunch_product(osv.Model):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
lunch product
|
|
|
|
|
"""
|
2012-10-24 14:20:25 +00:00
|
|
|
|
_name = 'lunch.product'
|
|
|
|
|
_description = 'lunch product'
|
|
|
|
|
_columns = {
|
2014-05-21 09:52:05 +00:00
|
|
|
|
'name': fields.char('Product', required=True),
|
2012-11-02 08:15:48 +00:00
|
|
|
|
'category_id': fields.many2one('lunch.product.category', 'Category', required=True),
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'description': fields.text('Description', size=256),
|
|
|
|
|
'price': fields.float('Price', digits=(16,2)), #TODO: use decimal precision of 'Account', move it from product to decimal_precision
|
|
|
|
|
'supplier': fields.many2one('res.partner', 'Supplier'),
|
2012-10-24 14:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class lunch_product_category(osv.Model):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
lunch product category
|
|
|
|
|
"""
|
2012-10-24 14:20:25 +00:00
|
|
|
|
_name = 'lunch.product.category'
|
|
|
|
|
_description = 'lunch product category'
|
|
|
|
|
_columns = {
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'name': fields.char('Category', required=True), #such as PIZZA, SANDWICH, PASTA, CHINESE, BURGER, ...
|
2012-10-24 14:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class lunch_cashmove(osv.Model):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
lunch cashmove => order or payment
|
|
|
|
|
"""
|
2012-10-24 14:20:25 +00:00
|
|
|
|
_name = 'lunch.cashmove'
|
|
|
|
|
_description = 'lunch cashmove'
|
|
|
|
|
_columns = {
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'user_id': fields.many2one('res.users', 'User Name', required=True),
|
|
|
|
|
'date': fields.date('Date', required=True),
|
|
|
|
|
'amount': fields.float('Amount', required=True), #depending on the kind of cashmove, the amount will be positive or negative
|
|
|
|
|
'description': fields.text('Description'), #the description can be an order or a payment
|
|
|
|
|
'order_id': fields.many2one('lunch.order.line', 'Order', ondelete='cascade'),
|
|
|
|
|
'state': fields.selection([('order','Order'), ('payment','Payment')], 'Is an order or a Payment'),
|
2012-10-24 14:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
_defaults = {
|
|
|
|
|
'user_id': lambda self, cr, uid, context: uid,
|
|
|
|
|
'date': fields.date.context_today,
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'state': 'payment',
|
2012-10-24 14:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class lunch_alert(osv.Model):
|
2012-11-05 11:01:47 +00:00
|
|
|
|
"""
|
|
|
|
|
lunch alert
|
|
|
|
|
"""
|
2012-10-24 14:20:25 +00:00
|
|
|
|
_name = 'lunch.alert'
|
2012-11-05 18:18:14 +00:00
|
|
|
|
_description = 'Lunch Alert'
|
2012-10-24 14:20:25 +00:00
|
|
|
|
_columns = {
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'message': fields.text('Message', size=256, required=True),
|
|
|
|
|
'alter_type': fields.selection([('specific', 'Specific Day'), \
|
|
|
|
|
('week', 'Every Week'), \
|
|
|
|
|
('days', 'Every Day')], \
|
|
|
|
|
string='Recurrency', required=True, select=True),
|
2012-11-06 08:19:04 +00:00
|
|
|
|
'specific_day': fields.date('Day'),
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'monday': fields.boolean('Monday'),
|
|
|
|
|
'tuesday': fields.boolean('Tuesday'),
|
|
|
|
|
'wednesday': fields.boolean('Wednesday'),
|
|
|
|
|
'thursday': fields.boolean('Thursday'),
|
|
|
|
|
'friday': fields.boolean('Friday'),
|
|
|
|
|
'saturday': fields.boolean('Saturday'),
|
|
|
|
|
'sunday': fields.boolean('Sunday'),
|
|
|
|
|
'active_from': fields.float('Between', required=True),
|
|
|
|
|
'active_to': fields.float('And', required=True),
|
2012-10-24 14:20:25 +00:00
|
|
|
|
}
|
2012-11-02 08:15:48 +00:00
|
|
|
|
_defaults = {
|
2012-11-05 18:18:14 +00:00
|
|
|
|
'alter_type': 'specific',
|
2012-11-06 08:19:04 +00:00
|
|
|
|
'specific_day': fields.date.context_today,
|
2012-11-02 08:15:48 +00:00
|
|
|
|
'active_from': 7,
|
|
|
|
|
'active_to': 23,
|
2012-11-05 18:18:14 +00:00
|
|
|
|
}
|