Corrections pep8
[burette/remembership.git] / remembership.py
index 2072258..b6a6857 100644 (file)
@@ -24,6 +24,7 @@
 from osv import osv
 from osv import fields
 
+
 class Partner(osv.osv):
     _inherit = 'res.partner'
 
@@ -31,97 +32,38 @@ class Partner(osv.osv):
         'member_ident': fields.char('Member identifier', size=64, readonly=True),
     }
 
-    def create_membership_invoice(self, cr, uid, ids, product_id=None, datas=None, context=None):
-        """ Create Customer Invoice of Membership for partners.
-        @param datas: datas has dictionary value which consist Id of Membership product and Cost Amount of Membership.
-                      datas = {'membership_product_id': None, 'amount': None}
-        """
-        invoice_obj = self.pool.get('account.invoice')
-        invoice_line_obj = self.pool.get('account.invoice.line')
-        invoice_tax_obj = self.pool.get('account.invoice.tax')
-        product_id = product_id or datas.get('membership_product_id', False)
-        amount = datas.get('amount', 0.0)
-        invoice_list = []
-        if type(ids) in (int, long,):
-            ids = [ids]
-        for partner in self.browse(cr, uid, ids, context=context):
-            account_id = partner.property_account_receivable and partner.property_account_receivable.id or False
-            fpos_id = partner.property_account_position and partner.property_account_position.id or False
-            addr = self.address_get(cr, uid, [partner.id], ['invoice'])
-            if partner.free_member:
-                raise osv.except_osv(_('Error !'),
-                        _("Partner is a free Member."))
-            if not addr.get('invoice', False):
-                raise osv.except_osv(_('Error !'),
-                        _("Partner doesn't have an address to make the invoice."))
-            quantity = 1
-            line_value =  {
-                'product_id': product_id,
-            }
-
-            line_dict = invoice_line_obj.product_id_change(cr, uid, {},
-                            product_id, False, quantity, '', 'out_invoice', partner.id, fpos_id, price_unit=amount, context=context)
-            line_value.update(line_dict['value'])
-            line_value['price_unit'] = amount
-            if line_value.get('invoice_line_tax_id', False):
-                tax_tab = [(6, 0, line_value['invoice_line_tax_id'])]
-                line_value['invoice_line_tax_id'] = tax_tab
-
-            invoice_id = invoice_obj.create(cr, uid, {
-                'partner_id': partner.id,
-                'address_invoice_id': addr.get('invoice', False),
-                'account_id': account_id,
-                'fiscal_position': fpos_id or False
-                }, context=context)
-            line_value['invoice_id'] = invoice_id
-            invoice_line_id = invoice_line_obj.create(cr, uid, line_value, context=context)
-            invoice_obj.write(cr, uid, invoice_id, {'invoice_line': [(6, 0, [invoice_line_id])]}, context=context)
-            invoice_list.append(invoice_id)
-            if line_value['invoice_line_tax_id']:
-                tax_value = invoice_tax_obj.compute(cr, uid, invoice_id).values()
-                for tax in tax_value:
-                       invoice_tax_obj.create(cr, uid, tax, context=context)
-            if partner.member_ident:
-                mbr_id = partner.member_ident
-            else:
-                mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
-        #recompute the membership_state of those partners
-        self.pool.get('res.partner').write(cr, uid, ids, {'member_ident': mbr_id})
-        return invoice_list
-
 Partner()
 
 
 class account_invoice_line(osv.osv):
-    _inherit='account.invoice.line'
+    _inherit = 'account.invoice.line'
 
-    def write(self, cr, uid, ids, vals, context=None):
-        """Overrides orm write method
-        """
-        member_line_obj = self.pool.get('membership.membership_line')
-        res = super(account_invoice_line, self).write(cr, uid, ids, vals, context=context)
-        for line in self.browse(cr, uid, ids, context=context):
-            if line.invoice_id.type == 'out_invoice':
-                ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id)], context=context)
-                if line.product_id and line.product_id.membership and not ml_ids:
-                    # Product line has changed to a membership product
-                    date_from = line.product_id.membership_date_from
-                    date_to = line.product_id.membership_date_to
-                    if line.invoice_id.date_invoice > date_from and line.invoice_id.date_invoice < date_to:
-                        date_from = line.invoice_id.date_invoice
-                    member_line_obj.create(cr, uid, {
-                                    'partner': line.invoice_id.partner_id.id,
-                                    'membership_id': line.product_id.id,
-                                    'member_price': line.price_unit,
-                                    'date': time.strftime('%Y-%m-%d'),
-                                    'date_from': date_from,
-                                    'date_to': date_to,
-                                    'account_invoice_line': line.id,
-                                    }, context=context)
-                if line.product_id and not line.product_id.membership and ml_ids:
-                    # Product line has changed to a non membership product
-                    member_line_obj.unlink(cr, uid, ml_ids, context=context)
+    def create(self, cr, uid, vals, context=None):
+        res = super(account_invoice_line, self).create(cr, uid, vals, context=context)
+        line = self.browse(cr, uid, res, context=context)
+        if line.invoice_id.type == 'out_invoice':
+            if line.product_id and line.product_id.membership and not line.invoice_id.partner_id.member_ident:
+                mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
+                self.pool.get('res.partner').write(cr, uid, line.invoice_id.partner_id.id, {'member_ident': mbr_id})
         return res
 
 account_invoice_line()
+
+
+class pos_make_payment(osv.osv_memory):
+    _inherit = 'pos.make.payment'
+
+    def check(self, cr, uid, ids, context=None):
+        """
+        Auto invoice orders. Temporarly solution before make partner members just with orders, without invoice.
+        """
+        res = super(pos_make_payment, self).check(cr, uid, ids, context=context)
+
+        order_obj = self.pool.get('pos.order')
+        order_obj.action_invoice(cr, uid, ids, context=context)
+
+        return res
+
+pos_make_payment()
+
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: