Skip to content

Commit

Permalink
Merge 3b38825 into 7c1fde4
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggowing committed Oct 27, 2014
2 parents 7c1fde4 + 3b38825 commit e62c8ee
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
23 changes: 23 additions & 0 deletions sale_automatic_workflow/automatic_workflow_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,33 @@ def _validate_pickings(self, cr, uid, context=None):
picking_ids,
context=context)

def _invoice_pickings(self, cr, uid, context=None):
picking_obj = self.pool.get('stock.picking')
# We search on stock.picking (using the type) rather than
# stock.picking.out because the ORM seems bugged and can't
# search on stock_picking_out.workflow_process_id.
# Later, we'll call `validate_picking` on stock.picking.out
# because anyway they have the same ID and the call will be at
# the correct object level.
picking_ids = picking_obj.search(
cr, uid,
[('state', 'in', ['done']),
('workflow_process_id.create_invoice_on', '=', 'on_picking_done'),
('invoice_state', '=', '2binvoiced'),
('type', '=', 'out')],
context=context)
_logger.debug('Pickings to invoice: %s', picking_ids)
if picking_ids:
with commit(cr):
picking_obj.action_invoice_create(cr, uid,
picking_ids,
context=context)

def run(self, cr, uid, ids=None, context=None):
""" Must be called from ir.cron """

self._validate_sale_orders(cr, uid, context=context)
self._invoice_pickings(cr, uid, context=context)
self._validate_invoices(cr, uid, context=context)
self._reconcile_invoices(cr, uid, context=context)
self._validate_pickings(cr, uid, context=context)
Expand Down
54 changes: 51 additions & 3 deletions sale_automatic_workflow/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,61 @@ class stock_picking(orm.Model):

def _prepare_invoice(self, cr, uid, picking, partner, inv_type, journal_id,
context=None):
# Read the correct journal for the shop company
if not journal_id:
journal_ids = self.pool.get('account.journal').search(
cr, uid,
[('type', '=', 'sale'),
('company_id', '=', picking.sale_id.company_id.id)],
limit=1)
journal_id = journal_ids and journal_ids[0] or journal_id
# Re-read partner with correct context to get the
# correct accounts from properties
if picking.sale_id:
context = dict(context,
force_company=picking.sale_id.company_id.id,
company_id=picking.sale_id.company_id.id)
picking = self.browse(cr, uid, picking.id, context=context)
partner = self.pool.get('res.partner').browse(cr, uid,
partner.id,
context=context)
invoice_vals = super(stock_picking, self)._prepare_invoice(
cr, uid, picking, partner, inv_type, journal_id, context=context)
invoice_vals['workflow_process_id'] = picking.workflow_process_id.id
if picking.workflow_process_id.invoice_date_is_order_date:
invoice_vals['date_invoice'] = picking.sale_id.date_order
base_picking = self.pool.get('stock.picking').browse(cr, uid,
picking.id,
context=context)
if picking.sale_id:
invoice_vals['currency_id'] =\
picking.sale_id.pricelist_id.currency_id.id
if base_picking.workflow_process_id.invoice_date_is_order_date:
invoice_vals['date_invoice'] = picking.sale_id.date_order
# Force period to avoid multi-company issues
if not invoice_vals.get('period_id'):
period_ids = self.pool.get('account.period').find(
cr, uid, invoice_vals.get('date_invoice', False),
context=context)
invoice_vals['period_id'] =\
period_ids and period_ids[0] or False
invoice_vals['workflow_process_id'] =\
base_picking.workflow_process_id.id

return invoice_vals

def _prepare_invoice_line(self, cr, uid, group, picking, move_line,
invoice_id, invoice_vals, context=None):
if picking.sale_id:
context = dict(
context,
force_company=picking.sale_id.company_id.id,
company_id=picking.sale_id.company_id.id)
picking = self.browse(cr, uid, picking.id, context=context)
move_line = self.pool.get('stock.move').browse(cr, uid,
move_line.id,
context=context)
return super(stock_picking, self)._prepare_invoice_line(
cr, uid, group, picking, move_line, invoice_id,
invoice_vals, context=context)


class stock_picking_out(orm.Model):
_inherit = "stock.picking.out"
Expand Down

0 comments on commit e62c8ee

Please sign in to comment.