Hello,
I'm currently trying to add my attribute "coupon_nb" (the amount of reduction given to a customer) to the amount total in pos.order.
My code is here :
class loyalty_reward(osv.osv): _name = 'loyalty.reward' _columns = { 'name': fields.char('Name', size=32, select=1, required=True, help='An internal identification for this loyalty reward'), 'loyalty_program_id': fields.many2one('loyalty.program', 'Loyalty Program', help='The Loyalty Program this reward belongs to'), 'minimum_points': fields.float('Minimum Points', help='The minimum amount of points the customer must have to qualify for this reward'), 'type': fields.selection((('gift','Gift'),('discount','Discount'),('resale','Resale')), 'Type', required=True, help='The type of the reward'), 'gift_product_id': fields.many2one('product.product','Gift Product', help='The product given as a reward'), 'point_cost': fields.float('Point Cost', help='The cost of the reward'), 'discount_product_id': fields.many2one('product.product','Discount Product', help='The product used to apply discounts'), 'discount': fields.float('Discount',help='The discount percentage'), 'point_product_id': fields.many2one('product.product', 'Point Product', help='The product that represents a point that is sold by the customer'), } def _check_gift_product(self, cr, uid, ids, context=None): for reward in self.browse(cr, uid, ids, context=context): if reward.type == 'gift': return bool(reward.gift_product_id) else: return True def _check_discount_product(self, cr, uid, ids, context=None): for reward in self.browse(cr, uid, ids, context=context): if reward.type == 'discount': return bool(reward.discount_product_id) else: return True def _check_point_product(self, cr, uid, ids, context=None): for reward in self.browse(cr, uid, ids, context=context): if reward.type == 'resale': return bool(reward.point_product_id) else: return True _constraints = [ (_check_gift_product, "The gift product field is mandatory for gift rewards", ["type","gift_product_id"]), (_check_discount_product, "The discount product field is mandatory for discount rewards", ["type","discount_product_id"]), (_check_point_product, "The point product field is mandatory for point resale rewards", ["type","discount_product_id"]), ] |
<record model="ir.ui.view" id="view_pos_pos_form"> <field name="name">pos.order.inherit</field> <field name="model">pos.order</field> <field name="inherit_id" ref="point_of_sale.view_pos_pos_form"/> <field name="arch" type="xml"> <xpath expr="//page[@name='extra']" position="inside"> <group name="loyalty" string="Loyalty Programs"> <field name="loyalty_points" /> <field name="coupon_nb" /> </group> </xpath> </field> </record> |
But I want coupon_nb to be used to calculate the amount total.
Here you can see the result :
http://i.imgur.com/yGbwoSN.png
But as you can see here, the amount total doesn't change at all :
http://i.imgur.com/NMcc8TE.png
Please, help me.
I failed, the right python code is this : class pos_order(osv.osv): _inherit = 'pos.order' _columns = { 'loyalty_points': fields.float('Loyalty Points', help='The amount of Loyalty points the customer won or lost with this order'), 'coupon_nb': fields.float('Coupon Reduction', help='Coupon amount reduction'), } def _order_fields(self, cr, uid, ui_order, context=None): fields = super(pos_order,self)._order_fields(cr,uid,ui_order,context) fields['loyalty_points'] = ui_order.get('loyalty_points',0) fields['coupon_nb'] = ui_order.get('coupon_nb',0) return fields def _amount_all(self, cr, uid, ids, field_name, arg, context=None): res = super(sale_order, self)._amount_all(cr, uid, ids, field_name, arg, context) for order in self.browse(cr, uid, ids, context=context): res[order.id]['amount_total'] += order.amount_total + order.coupon_nb return res def create_from_ui(self, cr, uid, orders, context=None): ids = super(pos_order,self).create_from_ui(cr,uid,orders,context=context) for order in orders: if order['data']['coupon_nb'] != 0: orderT = self.pool.get('pos.order').browse(cr, uid, ids, context=context) orderT.write({'coupon_nb': order['data']['coupon_nb']}) if order['data']['loyalty_points'] != 0 and order['data']['partner_id']: partner = self.pool.get('res.partner').browse(cr,uid,order['data']['partner_id'], context=context) partner.write({'loyalty_points': partner['loyalty_points'] + order['data']['loyalty_points']}) return ids