from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import time
-from openerp import pooler
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP, float_compare
'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'sale.shop', context=c),
}
-sale_shop()
class sale_order(osv.osv):
_name = "sale.order"
_description = "Sales Order"
_track = {
'state': {
- 'sale.mt_order_confirmed': lambda self, cr, uid, obj, ctx=None: obj['state'] in ['manual', 'progress'],
+ 'sale.mt_order_confirmed': lambda self, cr, uid, obj, ctx=None: obj['state'] in ['manual'],
'sale.mt_order_sent': lambda self, cr, uid, obj, ctx=None: obj['state'] in ['sent']
},
}
result[line.order_id.id] = True
return result.keys()
+ def _get_default_shop(self, cr, uid, context=None):
+ company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
+ shop_ids = self.pool.get('sale.shop').search(cr, uid, [('company_id','=',company_id)], context=context)
+ if not shop_ids:
+ raise osv.except_osv(_('Error!'), _('There is no default shop for the current user\'s company!'))
+ return shop_ids[0]
+
_columns = {
'name': fields.char('Order Reference', size=64, required=True,
readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, select=True),
'order_policy': fields.selection([
('manual', 'On Demand'),
], 'Create Invoice', required=True, readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
- help="""This field controls how invoice and delivery operations are synchronized.
- - With 'Before Delivery', a draft invoice is created, and it must be paid before delivery."""),
+ help="""This field controls how invoice and delivery operations are synchronized."""),
'pricelist_id': fields.many2one('product.pricelist', 'Pricelist', required=True, readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, help="Pricelist for current sales order."),
'currency_id': fields.related('pricelist_id', 'currency_id', type="many2one", relation="res.currency", string="Currency", readonly=True, required=True),
'project_id': fields.many2one('account.analytic.account', 'Contract / Analytic', readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, help="The analytic account related to a sales order."),
'user_id': lambda obj, cr, uid, context: uid,
'name': lambda obj, cr, uid, context: '/',
'invoice_quantity': 'order',
+ 'shop_id': _get_default_shop,
'partner_invoice_id': lambda self, cr, uid, context: context.get('partner_id', False) and self.pool.get('res.partner').address_get(cr, uid, [context['partner_id']], ['invoice'])['invoice'],
'partner_shipping_id': lambda self, cr, uid, context: context.get('partner_id', False) and self.pool.get('res.partner').address_get(cr, uid, [context['partner_id']], ['delivery'])['delivery'],
+ 'note': lambda self, cr, uid, context: self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.sale_note
}
_sql_constraints = [
('name_uniq', 'unique(name, company_id)', 'Order Reference must be unique per Company!'),
if s['state'] in ['draft', 'cancel']:
unlink_ids.append(s['id'])
else:
- raise osv.except_osv(_('Invalid Action!'), _('In order to delete a confirmed sales order, you must cancel it before !'))
+ raise osv.except_osv(_('Invalid Action!'), _('In order to delete a confirmed sales order, you must cancel it before!'))
return osv.osv.unlink(self, cr, uid, unlink_ids, context=context)
}
return {'warning': warning, 'value': value}
+ def get_salenote(self, cr, uid, ids, partner_id, context=None):
+ context_lang = context.copy()
+ if partner_id:
+ partner_lang = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context).lang
+ context_lang.update({'lang': partner_lang})
+ return self.pool.get('res.users').browse(cr, uid, uid, context=context_lang).company_id.sale_note
+
def onchange_partner_id(self, cr, uid, ids, part, context=None):
if not part:
return {'value': {'partner_invoice_id': False, 'partner_shipping_id': False, 'payment_term': False, 'fiscal_position': False}}
part = self.pool.get('res.partner').browse(cr, uid, part, context=context)
- #if the chosen partner is not a company and has a parent company, use the parent to choose the delivery, the
- #invoicing addresses and all the fields related to the partner.
- if part.parent_id and not part.is_company:
- part = part.parent_id
addr = self.pool.get('res.partner').address_get(cr, uid, [part.id], ['delivery', 'invoice', 'contact'])
pricelist = part.property_product_pricelist and part.property_product_pricelist.id or False
payment_term = part.property_payment_term and part.property_payment_term.id or False
}
if pricelist:
val['pricelist_id'] = pricelist
+ sale_note = self.get_salenote(cr, uid, ids, part.id, context=context)
+ if sale_note: val.update({'note': sale_note})
return {'value': val}
def create(self, cr, uid, vals, context=None):
- if vals.get('name','/')=='/':
+ if context is None:
+ context = {}
+ if vals.get('name', '/') == '/':
vals['name'] = self.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '/'
- return super(sale_order, self).create(cr, uid, vals, context=context)
+ context.update({'mail_create_nolog': True})
+ new_id = super(sale_order, self).create(cr, uid, vals, context=context)
+ self.message_post(cr, uid, [new_id], body=_("Quotation created"), context=context)
+ return new_id
def button_dummy(self, cr, uid, ids, context=None):
return True
This function prints the sales order and mark it as sent, so that we can see more easily the next step of the workflow
'''
assert len(ids) == 1, 'This option should only be used for a single id at a time'
- wf_service = netsvc.LocalService("workflow")
- wf_service.trg_validate(uid, 'sale.order', ids[0], 'quotation_sent', cr)
+ self.signal_quotation_sent(cr, uid, ids)
datas = {
'model': 'sale.order',
'ids': ids,
view of one of the newly created invoices
"""
mod_obj = self.pool.get('ir.model.data')
- wf_service = netsvc.LocalService("workflow")
-
+
# create invoices through the sales orders' workflow
inv_ids0 = set(inv.id for sale in self.browse(cr, uid, ids, context) for inv in sale.invoice_ids)
- for id in ids:
- wf_service.trg_validate(uid, 'sale.order', id, 'manual_invoice', cr)
+ self.signal_manual_invoice(cr, uid, ids)
inv_ids1 = set(inv.id for sale in self.browse(cr, uid, ids, context) for inv in sale.invoice_ids)
# determine newly created invoices
new_inv_ids = list(inv_ids1 - inv_ids0)
invoice_ref += o.name + '|'
self.write(cr, uid, [o.id], {'state': 'progress'})
cr.execute('insert into sale_order_invoice_rel (order_id,invoice_id) values (%s,%s)', (o.id, res))
+ #remove last '|' in invoice_ref
+ if len(invoice_ref) >= 1:
+ invoice_ref = invoice_ref[:-1]
invoice.write(cr, uid, [res], {'origin': invoice_ref, 'name': invoice_ref})
else:
for order, il in val:
return True
def action_cancel(self, cr, uid, ids, context=None):
- wf_service = netsvc.LocalService("workflow")
if context is None:
context = {}
sale_order_line_obj = self.pool.get('sale.order.line')
+ account_invoice_obj = self.pool.get('account.invoice')
for sale in self.browse(cr, uid, ids, context=context):
for inv in sale.invoice_ids:
if inv.state not in ('draft', 'cancel'):
_('Cannot cancel this sales order!'),
_('First cancel all invoices attached to this sales order.'))
for r in self.read(cr, uid, ids, ['invoice_ids']):
- for inv in r['invoice_ids']:
- wf_service.trg_validate(uid, 'account.invoice', inv, 'invoice_cancel', cr)
+ account_invoice_obj.signal_invoice_cancel(cr, uid, r['invoice_ids'])
sale_order_line_obj.write(cr, uid, [l.id for l in sale.order_line],
{'state': 'cancel'})
self.write(cr, uid, ids, {'state': 'cancel'})
def action_button_confirm(self, cr, uid, ids, context=None):
assert len(ids) == 1, 'This option should only be used for a single id at a time.'
- wf_service = netsvc.LocalService('workflow')
- wf_service.trg_validate(uid, 'sale.order', ids[0], 'order_confirm', cr)
+ self.signal_order_confirm(cr, uid, ids)
# redisplay the record as a sales order
view_ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'sale', 'view_order_form')
_description = 'Sales Order Line'
_columns = {
'order_id': fields.many2one('sale.order', 'Order Reference', required=True, ondelete='cascade', select=True, readonly=True, states={'draft':[('readonly',False)]}),
- 'name': fields.text('Description', required=True, select=True, readonly=True, states={'draft': [('readonly', False)]}),
+ 'name': fields.text('Description', required=True, readonly=True, states={'draft': [('readonly', False)]}),
'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of sales order lines."),
- 'product_id': fields.many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_default=True),
+ 'product_id': fields.many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_default=True, readonly=True, states={'draft': [('readonly', False)]}),
'invoice_lines': fields.many2many('account.invoice.line', 'sale_order_line_invoice_rel', 'order_line_id', 'invoice_id', 'Invoice Lines', readonly=True),
'invoiced': fields.function(_fnct_line_invoiced, string='Invoiced', type='boolean',
- store={'account.invoice': (_order_lines_from_invoice, ['state'], 10)}),
+ store={
+ 'account.invoice': (_order_lines_from_invoice, ['state'], 10),
+ 'sale.order.line': (lambda self,cr,uid,ids,ctx=None: ids, ['invoice_lines'], 10)}),
'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]}),
'type': fields.selection([('make_to_stock', 'from stock'), ('make_to_order', 'on order')], 'Procurement Method', required=True, readonly=True, states={'draft': [('readonly', False)]},
help="From stock: When needed, the product is taken from the stock or we wait for replenishment.\nOn order: When needed, the product is purchased or produced."),
'salesman_id':fields.related('order_id', 'user_id', type='many2one', relation='res.users', store=True, string='Salesperson'),
'company_id': fields.related('order_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
}
- _order = 'order_id desc, sequence'
+ _order = 'order_id desc, sequence, id'
_defaults = {
'product_uom' : _get_uom_id,
'discount': 0.0,
vals = self._prepare_order_line_invoice_line(cr, uid, line, False, context)
if vals:
inv_id = self.pool.get('account.invoice.line').create(cr, uid, vals, context=context)
- cr.execute('insert into sale_order_line_invoice_rel (order_line_id,invoice_id) values (%s,%s)', (line.id, inv_id))
+ self.write(cr, uid, [line.id], {'invoice_lines': [(4, inv_id)]}, context=context)
sales.add(line.order_id.id)
create_ids.append(inv_id)
# Trigger workflow events
context = context or {}
lang = lang or context.get('lang',False)
if not partner_id:
- raise osv.except_osv(_('No Customer Defined !'), _('Before choosing a product,\n select a customer in the sales form.'))
+ raise osv.except_osv(_('No Customer Defined!'), _('Before choosing a product,\n select a customer in the sales form.'))
warning = {}
product_uom_obj = self.pool.get('product.uom')
partner_obj = self.pool.get('res.partner')
date_order = time.strftime(DEFAULT_SERVER_DATE_FORMAT)
result = {}
- warning_msgs = {}
+ warning_msgs = ''
product_obj = product_obj.browse(cr, uid, product, context=context_partner)
uom2 = False
elif uom: # whether uos is set or not
default_uom = product_obj.uom_id and product_obj.uom_id.id
q = product_uom_obj._compute_qty(cr, uid, uom, qty, default_uom)
- result['product_uom'] = default_uom
if product_obj.uos_id:
result['product_uos'] = product_obj.uos_id.id
result['product_uos_qty'] = qty * product_obj.uos_coeff
raise osv.except_osv(_('Invalid Action!'), _('Cannot delete a sales order line which is in state \'%s\'.') %(rec.state,))
return super(sale_order_line, self).unlink(cr, uid, ids, context=context)
+class res_company(osv.Model):
+ _inherit = "res.company"
+ _columns = {
+ 'sale_note': fields.text('Default Terms and Conditions', translate=True, help="Default terms and conditions for quotations."),
+ }
+
class mail_compose_message(osv.Model):
_inherit = 'mail.compose.message'
context = context or {}
if context.get('default_model') == 'sale.order' and context.get('default_res_id') and context.get('mark_so_as_sent'):
context = dict(context, mail_post_autofollow=True)
- wf_service = netsvc.LocalService("workflow")
- wf_service.trg_validate(uid, 'sale.order', context['default_res_id'], 'quotation_sent', cr)
+ self.pool.get('sale.order').signal_quotation_sent(cr, uid, [context['default_res_id']])
return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context)
+
+class account_invoice(osv.Model):
+ _inherit = 'account.invoice'
+
+ def confirm_paid(self, cr, uid, ids, context=None):
+ sale_order_obj = self.pool.get('sale.order')
+ res = super(account_invoice, self).confirm_paid(cr, uid, ids, context=context)
+ so_ids = sale_order_obj.search(cr, uid, [('invoice_ids', 'in', ids)], context=context)
+ if so_ids:
+ sale_order_obj.message_post(cr, uid, so_ids, body=_("Invoice paid"), context=context)
+ return res
+
+ def unlink(self, cr, uid, ids, context=None):
+ """ Overwrite unlink method of account invoice to send a trigger to the sale workflow upon invoice deletion """
+ invoice_ids = self.search(cr, uid, [('id', 'in', ids), ('state', 'in', ['draft', 'cancel'])], context=context)
+ #if we can't cancel all invoices, do nothing
+ if len(invoice_ids) == len(ids):
+ #Cancel invoice(s) first before deleting them so that if any sale order is associated with them
+ #it will trigger the workflow to put the sale order in an 'invoice exception' state
+ wf_service = netsvc.LocalService("workflow")
+ for id in ids:
+ wf_service.trg_validate(uid, 'account.invoice', id, 'invoice_cancel', cr)
+ return super(account_invoice, self).unlink(cr, uid, ids, context=context)
+
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: