[pos_order_line] new write method
[burette/pos_membership.git] / pos_membership.py
index 6e75dd7..fcfe106 100644 (file)
@@ -11,7 +11,7 @@
 #    the Free Software Foundation, either version 3 of the License, or
 #    (at your option) any later version.
 #
-#    ReMembership is distributed in the hope that it will be useful,
+#    POS Membership 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 General Public License for more details.
@@ -39,6 +39,30 @@ STATE = [
 ]
 
 
+class pos_session(osv.osv):
+    _inherit = 'pos.session'
+
+    def open_cb(self, cr, uid, ids, context=None):
+        """
+        Avoid call the Point Of Sale interface and set the pos.session to 'opened' (in progress)
+        """
+        if context is None:
+            context = dict()
+
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+
+        this_record = self.browse(cr, uid, ids[0], context=context)
+        this_record._workflow_signal('open')
+
+        context.update(active_id=this_record.id)
+
+        return {
+        }
+pos_session()
+
+
+
 class pos_order(osv.osv):
     _inherit = 'pos.order'
 
@@ -622,8 +646,8 @@ class pos_order(osv.osv):
                 inv_line['price_unit'] = line.price_unit
                 inv_line['discount'] = line.discount
                 inv_line['name'] = inv_name
-                inv_line['invoice_line_tax_id'] = ('invoice_line_tax_id' in inv_line)\
-                    and [(6, 0, inv_line['invoice_line_tax_id'])] or []
+                inv_line['invoice_line_tax_id'] = [(6, 0, [x.id for x in line.product_id.taxes_id] )]
+                inv_line_ref.create(cr, uid, inv_line, context=context)
                 #inv_line_ref.create(cr, uid, inv_line, context=context)
                 inv['invoice_line'].append((0, 0, inv_line))
             inv_id = inv_ref.create(cr, uid, inv, context=context)
@@ -678,72 +702,70 @@ class pos_order_line(osv.osv):
         print ("DEV: [pos_membership] [pos_order_line] [write] [super] : res=%s" % str(res))
         for line in self.browse(cr, uid, ids, context=context):
             ml_ids = member_line_obj.search(cr, uid, [('pos_order_line', '=', line.id)], context=context)
-            if line.product_id and line.product_id.membership and not ml_ids:
-                date_from = line.product_id.membership_date_from
-                date_to = line.product_id.membership_date_to
-                if line.order_id.date_order > date_from and \
-                   line.order_id.date_order < date_to:
-                    date_from = line.order_id.date_order
-                if line.product_id.membership_date2date:
-                    date_from = ('date_from' in context
-                      and context['date_from']
-                      and datetime.strptime(context['date_from'], "%Y-%m-%d")
-                      or date.today())
-                    date_to   = date_from + relativedelta(months = +12) # TODO: parameterize this delta?
-                    date_from = date_from.strftime("%Y-%m-%d")
-                    date_to   = date_to  .strftime("%Y-%m-%d")
-                print ("DEV: [pos_membership] [write] date_from: %s" % str(date_from))
-                print ("DEV: [pos_membership] [write] date_to  : %s" % str(date_to))
-                member_line_obj.create(cr, uid
-                 , { 'partner': line.order_id.partner_id and line.order_id.partner_id.id or False
-                   , '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
-                   , 'pos_order_line': line.id
-                   }
-                 , context=context)
+            if line.product_id and line.product_id.membership:
+                member_line_id = member_line_obj.search(cr, uid
+                  , [('partner', '=', line.invoice_id.partner_id.id)
+                    ,('pos_order_line', '=', line.id)]
+                  , limit=1
+                  , context=context)
+                for member_line in member_line_obj.browse(cr, uid, member_line_id, context=context):
+                  # NOTE: get member_line created in pos_membership.pos_order_line.create
+                    date_from = member_line.date_from
+                    date_to   = member_line.date_to
+                    if line.product_id.membership_date2date:
+                        date_from = ('date_from' in context
+                          and context.get('date_from')
+                          and datetime.strptime(context('date_from'), "%Y-%m-%d")
+                          or date.today())
+                        date_to   = date_from + relativedelta(months = +12) # TODO: parameterize this delta?
+                        date_from = date_from.strftime("%Y-%m-%d")
+                        date_to   = date_to  .strftime("%Y-%m-%d")
+                    print ("DEV: [pos_membership] [pos_order_line] [write] date_from: %s" % str(date_from))
+                    print ("DEV: [pos_membership] [pos_order_line] [write] date_to  : %s" % str(date_to))
+                    member_line_obj.write(cr, uid, member_line.id
+                      , {'date_from': date_from
+                        ,'date_to'  : date_to
+                        }
+                      , context=context)
                 if line.product_id.membership_grouped:
                     if line.order_id.partner_id.associate_members:
                         associate_member_line_ids = member_line_obj.search(cr, uid
-                          , [ ('pos_order_line', '=', line.id)
-                            , ('partner', '!=', line.order_id.partner_id.id)
-                            ]
-                          , context=context)
-                        for associate_member_line in member_line_obj.browse(cr, uid, associate_member_line_ids, context=context):
-                            print ("DEV: [pos_membership] [write] [associate] date_from: %s" % str(date_from))
-                            print ("DEV: [pos_membership] [write] [associate] date_to  : %s" % str(date_to))
-                            member_line_obj.write(cr, uid, associate_member_line.id
-                              , {'date_from': date_from
-                                ,'date_to'  : date_to
-                                }
-                              , context=context)
-                    else:
-                        print("DEV: mettre une contrainte pour l'objet\
-                              membership.membership_line interdisant les\
-                              adhésions groupées reliées à des partenaires\
-                              sans membres associés")
-                else:
-                    associate_member_line_ids = member_line_obj.search(cr, uid
                       , [ ('pos_order_line', '=', line.id)
-                        , ('partner', '!=', line.order_id.partner_id.id)
+                        , ('partner', '!=', line.invoice_id.partner_id.id)
                         ]
                       , context=context)
-                    member_line_obj.unlink(cr, uid, associate_member_line_ids, context=context)
-
-                #Define member ident if it's necessary
-                partners = [line.order_id.partner_id]
-                if line.order_id.partner_id.associate_members:
-                    partners.extend(line.order_id.partner_id.associate_members)
-                for i in partners:
-                    if not i.member_ident:
-                        mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
-                        self.pool.get('res.partner').write(cr, uid, i.id, {'member_ident': mbr_id})
-            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)
-        print("DEV: [pos_membership] [pos_order_line] [write]: res=%s" % str(res))
+                    for associate_member_line in member_line_obj.browse(cr, uid, associate_member_line_ids, context=context):
+                        print ("DEV: [pos_membership] [pos_order_line] [write] [associate] date_from: %s" % str(date_from))
+                        print ("DEV: [pos_membership] [pos_order_line] [write] [associate] date_to  : %s" % str(date_to))
+                        member_line_obj.write(cr, uid, associate_member_line.id
+                          , {'date_from': date_from
+                            ,'date_to'  : date_to
+                            }
+                          , context=context)
+                else:
+                    print("DEV: mettre une contrainte pour l'objet\
+                          membership.membership_line interdisant les\
+                          adhésions groupées reliées à des partenaires\
+                          sans membres associés")
+            else:
+                associate_member_line_ids = member_line_obj.search(cr, uid
+                  , [ ('pos_order_line', '=', line.id)
+                    , ('partner', '!=', line.order_id.partner_id.id)
+                    ]
+                  , context=context)
+                member_line_obj.unlink(cr, uid, associate_member_line_ids, context=context)
+            #Define member ident if it's necessary
+            partners = [line.order_id.partner_id]
+            if line.order_id.partner_id.associate_members:
+                partners.extend(line.order_id.partner_id.associate_members)
+            for i in partners:
+                if not i.member_ident:
+                    mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
+                    self.pool.get('res.partner').write(cr, uid, i.id, {'member_ident': mbr_id})
+        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)
+        print ("DEV: [pos_membership] [pos_order_line] [write] : return=%s" % str(res))
         return res
 
     def unlink(self, cr, uid, ids, context=None):