[IMP] Invoice_state based on delivery wizard recheck + returns and invoice_state
authorJosse Colpaert <jco@odoo.com>
Fri, 13 Jun 2014 15:44:03 +0000 (17:44 +0200)
committerJosse Colpaert <jco@odoo.com>
Fri, 13 Jun 2014 15:44:03 +0000 (17:44 +0200)
addons/purchase/purchase.py
addons/stock_account/__openerp__.py
addons/stock_account/stock.py
addons/stock_account/stock_account_view.xml
addons/stock_account/wizard/__init__.py
addons/stock_account/wizard/stock_invoice_onshipping.py
addons/stock_account/wizard/stock_invoice_onshipping_view.xml
addons/stock_account/wizard/stock_return_picking.py [new file with mode: 0644]
addons/stock_account/wizard/stock_return_picking_view.xml [new file with mode: 0644]

index f12e702..bdd8af1 100644 (file)
@@ -700,6 +700,7 @@ class purchase_order(osv.osv):
             'origin': order.name,
             'route_ids': order.picking_type_id.warehouse_id and [(6, 0, [x.id for x in order.picking_type_id.warehouse_id.route_ids])] or [],
             'warehouse_id':order.picking_type_id.warehouse_id.id,
+            'invoice_state': order.invoice_method == 'picking' and '2binvoiced' or 'none'
         }
 
         diff_quantity = order_line.product_qty
@@ -709,9 +710,10 @@ class purchase_order(osv.osv):
             tmp.update({
                 'product_uom_qty': min(procurement_qty, diff_quantity),
                 'product_uos_qty': min(procurement_qty, diff_quantity),
-                'move_dest_id': procurement.move_dest_id.id,  # blabla
-                'group_id': procurement.group_id.id or group_id,  # blabla to check ca devrait etre bon et groupĂ© dans le meme picking qd meme
+                'move_dest_id': procurement.move_dest_id.id,  #move destination is same as procurement destination
+                'group_id': procurement.group_id.id or group_id,  #move group is same as group of procurements if it exists, otherwise take another group
                 'procurement_id': procurement.id,
+                'invoice_state': procurement.rule_id.invoice_state or (procurement.location_id and procurement.location_id.usage == 'customer' and procurement.invoice_state) or (order.invoice_method == 'picking' and '2binvoiced') or 'none', #dropship case takes from sale
             })
             diff_quantity -= min(procurement_qty, diff_quantity)
             res.append(tmp)
index a2dab12..5c67002 100644 (file)
@@ -53,6 +53,7 @@ Dashboard / Reports for Warehouse Management includes:
         'wizard/stock_change_standard_price_view.xml',
         'wizard/stock_invoice_onshipping_view.xml',
         'wizard/stock_valuation_history_view.xml',
+        'wizard/stock_return_picking_view.xml',
         'product_data.xml',
         'product_view.xml',
         'stock_account_view.xml',
index db08ba3..4459035 100644 (file)
@@ -27,11 +27,10 @@ class stock_location_path(osv.osv):
         'invoice_state': fields.selection([
             ("invoiced", "Invoiced"),
             ("2binvoiced", "To Be Invoiced"),
-            ("none", "Not Applicable")], "Invoice Status",
-            required=True,),
+            ("none", "Not Applicable")], "Invoice Status",),
     }
     _defaults = {
-        'invoice_state': 'none',
+        'invoice_state': '',
     }
 
 #----------------------------------------------------------
@@ -43,11 +42,10 @@ class procurement_rule(osv.osv):
         'invoice_state': fields.selection([
             ("invoiced", "Invoiced"),
             ("2binvoiced", "To Be Invoiced"),
-            ("none", "Not Applicable")], "Invoice Status",
-            required=True),
+            ("none", "Not Applicable")], "Invoice Status",),
         }
     _defaults = {
-        'invoice_state': 'none',
+        'invoice_state': '',
     }
 
 #----------------------------------------------------------
@@ -61,16 +59,16 @@ class procurement_order(osv.osv):
         'invoice_state': fields.selection([("invoiced", "Invoiced"),
             ("2binvoiced", "To Be Invoiced"),
             ("none", "Not Applicable")
-         ], "Invoice Control", required=True),
+         ], "Invoice Control"),
         }
 
     def _run_move_create(self, cr, uid, procurement, context=None):
         res = super(procurement_order, self)._run_move_create(cr, uid, procurement, context=context)
-        res.update({'invoice_state': (procurement.rule_id.invoice_state in ('none', False) and procurement.invoice_state or procurement.rule_id.invoice_state) or 'none'})
+        res.update({'invoice_state': procurement.rule_id.invoice_state or procurement.invoice_state or 'none'})
         return res
 
     _defaults = {
-        'invoice_state': 'none'
+        'invoice_state': ''
         }
 
 
@@ -92,7 +90,7 @@ class stock_move(osv.osv):
     }
 
     def _get_master_data(self, cr, uid, move, company, context=None):
-        ''' returns a tupple (browse_record(res.partner), ID(res.users), ID(res.currency)'''
+        ''' returns a tuple (browse_record(res.partner), ID(res.users), ID(res.currency)'''
         return move.picking_id.partner_id, uid, company.currency_id.id
 
     def _create_invoice_line_from_vals(self, cr, uid, move, invoice_line_vals, context=None):
index 3b3ab37..867f2ea 100644 (file)
@@ -32,8 +32,8 @@
             <field name="inherit_id" ref="stock.view_picking_form"/>
             <field name="arch" type="xml">
                 <xpath expr="//button[@name='do_partial_open_barcode']" position="after">
-                    <button name="%(action_stock_invoice_onshipping)d" string="Create Invoice" attrs="{'invisible': ['|',('state','&lt;&gt;','done'),('invoice_state','&lt;&gt;','2binvoiced')]}" type="action" class="oe_highlight" groups="base.group_user"/>
-                    <button name="%(action_stock_invoice_onshipping)d" string="Refund Invoice" attrs="{'invisible': ['|',('state','&lt;&gt;','done'),('invoice_state','&lt;&gt;','invoiced')]}" type="action" class="oe_highlight" groups="base.group_user" context="{'inv_type': 'out_refund'}"/>
+                    <button name="%(action_stock_invoice_onshipping)d" string="Create/Refund Invoice" attrs="{'invisible': ['|',('state','&lt;&gt;','done'),('invoice_state','&lt;&gt;','2binvoiced')]}" type="action" class="oe_highlight" groups="base.group_user"/>
+                    <!--<button name="%(action_stock_invoice_onshipping)d" string="Refund Invoice" attrs="{'invisible': ['|',('state','&lt;&gt;','done'),('invoice_state','&lt;&gt;','invoiced')]}" type="action" class="oe_highlight" groups="base.group_user" context="{'inv_type': 'out_refund'}"/>-->
                 </xpath>
                 <xpath expr="//field[@name='move_type']" position="after">
                     <field name="invoice_state" groups="account.group_account_invoice"/>
index 87f0841..09d08bb 100644 (file)
@@ -22,3 +22,4 @@
 import stock_change_standard_price
 import stock_invoice_onshipping
 import stock_valuation_history
+import stock_return_picking
index f600b28..b0a98c7 100644 (file)
@@ -33,12 +33,32 @@ class stock_invoice_onshipping(osv.osv_memory):
         if context is None:
             context = {}
         journal_obj = self.pool.get('account.journal')
-        value = journal_obj.search(cr, uid, [('type', 'in',('sale','sale_Refund'))])
+        res_ids = context and context.get('active_ids', [])
+        pick_obj = self.pool.get('stock.picking')
+        pickings = pick_obj.browse(cr, uid, res_ids, context=context)
         vals = []
-        for jr_type in journal_obj.browse(cr, uid, value, context=context):
-            t1 = jr_type.id,jr_type.name
-            if t1 not in vals:
-                vals.append(t1)
+        for pick in pickings:
+            if not pick.move_lines:
+                continue
+            src_usage = pick.move_lines[0].location_id.usage
+            dest_usage = pick.move_lines[0].location_dest_id.usage
+            type = pick.picking_type_id.code
+            if type == 'outgoing' and dest_usage == 'supplier':
+                journal_type = 'purchase_refund'
+            elif type == 'outgoing' and dest_usage == 'customer':
+                journal_type = 'sale'
+            elif type == 'incoming' and src_usage == 'supplier':
+                journal_type = 'purchase'
+            elif type == 'incoming' and src_usage == 'customer':
+                journal_type = 'sale_refund'
+            else:
+                journal_type = 'sale'
+            journals = journal_obj.search(cr, uid, [('type', '=', journal_type)])
+            for jr_type in journal_obj.browse(cr, uid, journals, context=context):
+                t1 = str(jr_type.id),jr_type.name
+                print t1
+                if t1 not in vals:
+                    vals.append(t1)
         return vals
 
     _name = "stock.invoice.onshipping"
@@ -48,6 +68,7 @@ class stock_invoice_onshipping(osv.osv_memory):
         'group': fields.boolean("Group by partner"),
         'inv_type': fields.selection([('out_invoice','Create Invoice'),('out_refund','Refund Invoice')], "Invoice Type"),
         'invoice_date': fields.date('Invoice Date'),
+        #'inv_t': fields.selection([('in_invoice', ''), ]),
     }
     _defaults = {
         'journal_id' : _get_journal,
@@ -71,21 +92,28 @@ class stock_invoice_onshipping(osv.osv_memory):
     def open_invoice(self, cr, uid, ids, context=None):
         if context is None:
             context = {}
+        
         invoice_ids = self.create_invoice(cr, uid, ids, context=context)
         if not invoice_ids:
             raise osv.except_osv(_('Error!'), _('No invoice created!'))
 
-        onshipdata_obj = self.read(cr, uid, ids, ['journal_id', 'group', 'invoice_date', 'inv_type'])
-        inv_type = onshipdata_obj[0]['inv_type']
+        data = self.browse(cr, uid, ids[0], context=context)
 
         action_model = False
         action = {}
-
+        acc_journal = self.pool.get("account.journal")
+        journal = acc_journal.browse(cr, uid, int(data.journal_id), context=context)
+        journal2type = {'sale':'out_invoice', 'purchase':'in_invoice' , 'sale_refund':'out_refund', 'purchase_refund':'in_refund'}
+        inv_type = journal2type.get(journal.type) or 'out_invoice'
         data_pool = self.pool.get('ir.model.data')
-        if inv_type == "out_refund":
-            action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree3")
-        elif inv_type == "out_invoice":
+        if inv_type == "out_invoice":
             action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree1")
+        elif inv_type == "in_invoice":
+            action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree2")
+        elif inv_type == "out_refund":
+            action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree3")
+        elif inv_type == "in_refund":
+            action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree4")
 
         if action_model:
             action_pool = self.pool[action_model]
@@ -97,18 +125,18 @@ class stock_invoice_onshipping(osv.osv_memory):
     def create_invoice(self, cr, uid, ids, context=None):
         context = context or {}
         picking_pool = self.pool.get('stock.picking')
-        onshipdata_obj = self.read(cr, uid, ids, ['journal_id', 'group', 'invoice_date', 'inv_type'])
-
-        context['date_inv'] = onshipdata_obj[0]['invoice_date']
-        inv_type = onshipdata_obj[0]['inv_type']
+        data = self.browse(cr, uid, ids[0])
+        journal2type = {'sale':'out_invoice', 'purchase':'in_invoice', 'sale_refund':'out_refund', 'purchase_refund':'in_refund'}
+        context['date_inv'] = data.invoice_date
+        acc_journal = self.pool.get("account.journal")
+        journal = acc_journal.browse(cr, uid, int(data.journal_id), context=context)
+        inv_type = journal2type.get(journal.type) or 'out_invoice'
         context['inv_type'] = inv_type
 
         active_ids = context.get('active_ids', [])
-        if isinstance(onshipdata_obj[0]['journal_id'], tuple):
-            onshipdata_obj[0]['journal_id'] = onshipdata_obj[0]['journal_id'][0]
         res = picking_pool.action_invoice_create(cr, uid, active_ids,
-              journal_id = onshipdata_obj[0]['journal_id'],
-              group = onshipdata_obj[0]['group'],
+              journal_id = data.journal_id,
+              group = data.group,
               type = inv_type,
               context=context)
         return res
index a37af92..7589269 100644 (file)
             </field>
         </record>
 
-
-        <act_window name="Create Invoice"
-            res_model="stock.invoice.onshipping"
-            src_model="stock.picking"
-            key2="client_action_multi"
-            multi="True"
-            view_mode="form"
-            view_type="form"
-            target="new"
-            id="action_stock_invoice_onshipping"/>
-
         <act_window name="Create Draft Invoices"
             res_model="stock.invoice.onshipping"
             src_model="stock.picking"
diff --git a/addons/stock_account/wizard/stock_return_picking.py b/addons/stock_account/wizard/stock_return_picking.py
new file mode 100644 (file)
index 0000000..4ceaf35
--- /dev/null
@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 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
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+from openerp.osv import osv, fields
+from openerp.tools.translate import _
+import openerp.addons.decimal_precision as dp
+
+
+class stock_return_picking(osv.osv_memory):
+    _inherit = 'stock.return.picking'
+    _columns = {
+        'invoice_state': fields.selection([('2binvoiced', 'To be refunded/invoiced'), ('none', 'No invoicing')], 'Invoicing',required=True),
+    }
+
+    def default_get(self, cr, uid, fields, context=None):
+        res = super(stock_return_picking, self).default_get(cr, uid, fields, context=context)
+        record_id = context and context.get('active_id', False) or False
+        pick_obj = self.pool.get('stock.picking')
+        pick = pick_obj.browse(cr, uid, record_id, context=context)
+        if pick:
+            if 'invoice_state' in fields:
+                if pick.invoice_state=='invoiced':
+                    res.update({'invoice_state': '2binvoiced'})
+                else:
+                    res.update({'invoice_state': 'none'})
+        return res
+
+        
+
+    def _create_returns(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        data = self.browse(cr, uid, ids[0], context=context)
+        new_picking, picking_type_id = super(stock_return_picking, self)._create_returns(cr, uid, ids, context=context)
+        if data.invoice_state == '2binvoiced':
+            pick_obj = self.pool.get("stock.picking")
+            move_obj = self.pool.get("stock.move")
+            move_ids = [x.id for x in pick_obj.browse(cr, uid, new_picking, context=context).move_lines]
+            move_obj.write(cr, uid, move_ids, {'invoice_state': '2binvoiced'})
+        return new_picking, picking_type_id
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
diff --git a/addons/stock_account/wizard/stock_return_picking_view.xml b/addons/stock_account/wizard/stock_return_picking_view.xml
new file mode 100644 (file)
index 0000000..8365c42
--- /dev/null
@@ -0,0 +1,14 @@
+<openerp>
+    <data>
+        <record id="view_stock_return_picking_form_inherit" model="ir.ui.view">
+            <field name="name">Return lines</field>
+            <field name="model">stock.return.picking</field>
+            <field name="inherit_id" ref="stock.view_stock_return_picking_form"/>
+            <field name="arch" type="xml">
+                <field name="product_return_moves" position="after">
+                    <field name="invoice_state"/>
+                </field>
+            </field>
+        </record>
+    </data>
+</openerp>