Пример #1
0
 def action_force_close(self, cr, uid, ids, context=None):
     self.write(cr, uid, ids, {'state':'closed'})
     line = self.browse(cr, uid, ids)[0]
     order = line.order_id
     states = {'done':1,'paid':2,'closed':2}
     state = compose(
         partial(map,lambda line:(states[line.state] if line.state in states else 0,line.state)),
         sorted,
         min)(order.order_line)
     # write of sale.order takes care of closed/paid as they are equal in weight 
     res = self.pool.get('sale.order').write(cr, uid, [order.id], {'state':state[1]}) if state[0] else True
     return {'type': 'ir.actions.client', 'tag': 'reload',}
def match_with_existing_partner(obj,cr,uid,vals):
    """
    when we could not find a partner by its unique identifier = email
    we do an extra check if we can find it based on address and name
    """
    def concat_names(p):
        try:
            return p.first_name + '_' + p.last_name
        except:
            return ''

    def match_on_fullname(target_ids):
        match_target_list = []
        for partner in obj.browse(cr,uid,target_ids):
            match_target_list.append((partner.id, concat_names(partner)))
        return difflib_cmp(concat_names(ref_vals), match_target_list)[0] if match_target_list else False

    def match_names_seperatly(cmp_res):
        """
        return tuple partner object,boolean full match
        """
        _logger.info("partner fullname match diff:{}".format(cmp_res))
        if cmp_res:
            partner = obj.browse(cr,uid,cmp_res[0])
            if cmp_res[1] == 1.0:
                return (partner,True)
            if cmp_res[1] > 0.5:
                first_name = partner.first_name if partner.first_name else ''
                cmp_res_first_name = difflib_cmp(ref_vals.first_name, [(partner.id, first_name)])[0]
                last_name =  partner.last_name if partner.last_name else ''
                cmp_res_last_name = difflib_cmp(ref_vals.last_name, [(partner.id, last_name)])[0]
                _logger.info("partner firstname match diff:{}".format(cmp_res_first_name))
                _logger.info("partner lastname match diff:{}".format(cmp_res_last_name))
                # rules, priority full match to less match
                rules = [lambda f,l : f == 0 and l == 1.0,    # no firstname, 100% lastname
                         lambda f,l : f >= 0.7 and l >= 0.85] # seperate firstname/lastname
                res = [func(cmp_res_first_name[1],cmp_res_last_name[1]) for func in rules]
                # return partner,full match or partial match
                return (partner,res[0]) if any(res) else (False,False)
            else:
                return (False,False)
        else:
            return (False,False)

    ref_vals = get_match_vals(vals)
    if 'street_id' in vals and vals['street_id']:
       target_domain = [
            ('street_id','=',vals['street_id']),
            ('zip_id','=',vals['zip_id']),
            ('street_nbr','=',vals['street_nbr']),
       ]
    else:
       target_domain = [
            ('street','=',vals['street']),
            ('zip','=',vals['zip']),
            ('street_nbr','=',vals['street_nbr']),
       ]
    partner = compose(
                match_on_fullname,
                match_names_seperatly,
                lambda (p,full_match): p if p and (not(p.donation_line_ids) or full_match) else False
              )(obj.search(cr,uid,target_domain))
    log = {'alert':['Lidmaatschap aanvraag naam match'] if partner else [],'renewal':False}
    return (partner if partner else False, vals, log)
    def create_web_membership(self,cr,uid,ids,vals,datas,context=None):
        context = context or {}
        if ids == None:
            ids = []

        # membership_origin_id                    
        membership_origin_id = datas.get('membership_origin_id', 0)
        if self._verify_membership_origin(cr,uid,[membership_origin_id],context=context):
            vals['membership_origin_id'] = membership_origin_id

        # recruiting_organisation_id
        recruiting_organisation_id = datas.get('recruiting_organisation_id', 0)
        if not(self._verify_recruiting_organisation(cr,uid,[recruiting_organisation_id],context=context)):
            datas.pop('recruiting_organisation_id', None)

        # convert website membership + subscriptions to product
        product_id = self._web_membership_product(cr,uid,datas['subscriptions'],context=context)

        # renewal product...? , update contact  
        if datas.get('membership_renewal', False):
            vals['membership_renewal_product_id'] = product_id

        # override default from website            
        vals['customer'] = False

        # membership partner update or create
        _logger.info(vals)
        if not ids:
            _logger.info("partner niet aangemeld")
            ids,vals,log = compose(
                    partial(match_with_existing_partner,self,cr,uid),
                    partial(verify_partner_membership_state,self,cr,uid),
                    partial(verify_if_customer_or_supplier,self,cr,uid),
                    partial(send_internal_alerts,self,cr,uid),
                    lambda (p,v,l):([p.id],v,l) if p else (ids,v,l)
            )(vals)
            _logger.info("partner match ids:{}".format(ids))
        else:
            _logger.info("partner aangemeld ids:{}".format(ids))
            log = {
                'alert':[],
                'renewal':datas.get('membership_renewal', False)
            }
            ids,vals,log = compose(
                    lambda ids:(self.browse(cr,uid,ids[0],context=context),vals,log),
                    partial(verify_if_customer_or_supplier,self,cr,uid),
                    partial(send_internal_alerts,self,cr,uid),
                    lambda (p,v,l):([p.id],v,l)
            )(ids)

        if 'alert_website' in log:
            _logger.info("website alert:{}".format(website_alert))
            return {'id':0,'alert_message':website_alert}
        else:
            ids = self._web_membership_partner(cr,uid,ids,vals,context=context)

        methods = {'OGONE':self.create_membership_invoice,
                   'SEPA':self.create_web_membership_mandate_invoice,
                   'OFFLINE':self.create_membership_invoice,}
        method = datas['method'].upper() if 'method' in datas else None
        if method in methods:
            inv_ids = methods[method](cr,uid,ids,selected_product_id=product_id,datas=datas,context=context)
        else:
            inv_ids = 0

        if inv_ids:
            invoice_obj = self.pool.get('account.invoice')
            for invoice in invoice_obj.browse(cr,uid,inv_ids,context=context):
                #ogone_log_obj = self.pool.get('ogone.log')
                #ogone_log_vals = {
                #    'invoice_id':invoice.id,
                #    'date_created':time.strftime('%Y-%m-%d %H:%M:%S'),
                #}
                #ogone_log_obj.create(cr, uid, ogone_log_vals, context=context)
                return {'id':invoice.partner_id.id,'invoice_id':invoice.id,'reference':invoice.reference}
        else:
            return {'id':ids[0]}
Пример #4
0
    def make_invoices(self, cr, uid, ids, context=None):
        """
             To make invoices.

             @param self: The object pointer.
             @param cr: A database cursor
             @param uid: ID of the user currently logged in
             @param ids: the ID or list of IDs
             @param context: A standard dictionary

             @return: A dictionary which of fields with values.

        """
        context = context or {}
        invoices = {}
        sales_order_line_obj = self.pool.get('sale.order.line')
        wf_service = netsvc.LocalService('workflow')

        for line in sales_order_line_obj.browse(cr, uid, context.get('active_ids', []), context=context):
            order = line.order_id
            if line.invoiced:
                warn = _('Invoice cannot be created for Sales Order {}. A Sales Order Line is already invoiced!').format(order.name)
                raise osv.except_osv(_('Warning!'), warn)
            if line.delivered_qty == 0 or line.delivered_flag == False:
                warn = _('Invoice cannot be created for Sales Order {}. A Sales Order Line is not ready for invoice!').format(order.name)
                raise osv.except_osv(_('Warning!'), warn)
            dif_qty = line.product_uom_qty - line.delivered_qty
            original_line_state = line.state
            if dif_qty < 0:
                sales_order_line_obj.write(cr, uid, [line.id], {'state':'done'})
                context['use_delivered_qty'] = True
            else:
                sales_order_line_obj.write(cr, uid, [line.id], {'product_uom_qty':line.delivered_qty, 'state':'done'})
                context['use_delivered_qty'] = False
            if not order in invoices:
                invoices[order] = []
            inv_line_id = sales_order_line_obj.invoice_line_create(cr, uid, [line.id], context=context)
            sales_order_line_obj.write(cr, uid, [line.id], {'invoice_line_id':inv_line_id[0]})
            invoices[order].append((inv_line_id[0],line.id))

            if dif_qty > 0:
                newline = sales_order_line_obj.copy(cr, uid, line.id, {
                   'product_uom_qty':dif_qty,
                   'delivered_qty': 0,
                   'delivered_flag': False,
                   'delivered_text': '',
                })
                sales_order_line_obj.write(cr, uid, [newline], {'state':original_line_state})

        for order, lines in invoices.items():
            inv_lines = [i[0] for i in lines]
            so_lines = [i[1] for i in lines]
            inv = self._prepare_invoice(cr, uid, order, inv_lines)
            inv_id = self.pool.get('account.invoice').create(cr, uid, inv)
            cr.execute('INSERT INTO sale_order_invoice_rel \
                    (order_id,invoice_id) values (%s,%s)', (order.id, inv_id))
            sale_invoice_vals = {
                'order_id': order.id,
                'invoice_id': inv_id,
            }
            order_done = compose(
                partial(map,lambda l:False if l.state in ['manual','confirmed'] and l.id not in so_lines else True),
                sorted,
                min)(order.order_line)
            # sale.order is done if no lines exists before the done state ( manual, confirmed )
            order.write({'state':'done'}) if order_done else True
            sale_invoice_id = self.pool.get('sale.invoice').create(cr,uid,sale_invoice_vals)
            wf_service.trg_validate(uid, 'sale.invoice', sale_invoice_id, 'sale_invoiced', cr)

        if context.get('open_invoices', False):
            return self.open_invoices(cr, uid, ids, inv_id, context=context)
        else:
            return {'type': 'ir.actions.act_window_close'}