def generate_purchase_requisition_reminders(self, cr, uid, context=None):
        """Generate daily purchase requisition reminders"""
        line_obj = self.pool.get('purchase.requisition.line')
        msg_obj = self.pool.get('mail.message')

        # get draft purchase requisitions
        draft_prl = line_obj.search(cr, uid, [('state','=','draft'),])
        purchase_req_list = []
        for line in line_obj.browse(cr, uid, draft_prl):
            purchase_req_list.append((line.purchase_responsible_id,line.id))

        # group purchase requisition by user
        purchase_req_per_user = defaultdict(list)
        [purchase_req_per_user[k].append(v) for k, v in purchase_req_list]

        def get_detail_lines_html_body():
            base_url = self.pool.get('ir.config_parameter').get_param(cr, SUPERUSER_ID, 'web.base.url')
            link = ("<b><a href='{}?db={}#view_type=list&model=purchase.requisition.line&menu_id=470'>{}</a></b> ")
            line = link.format(base_url, 
                               cr.dbname,
                               _('%s concept inkoopaanvraaglijn(en)<br>')%(len(purchase_requisition_list)))
            yield line

        html_body_end = "<span><p><p/>"+_('Send from host %s - db %s')%(get_eth0(),cr.dbname)+"</span>"

        for user,purchase_requisition_list in purchase_req_per_user.items():
            msg_vals = {'subject': _('Purchase Requisition Reminder'),
                        'body': ''.join(get_detail_lines_html_body()) + html_body_end,
                        'type': 'notification',
                        'notified_partner_ids': [(6,0,[user.partner_id.id])],
                    }
            msg_obj.create(cr, uid, msg_vals)
            self.send_purchase_requisition_reminders_email(cr, uid, user, msg_vals, context=context)
    def generate_project_task_reminders(self, cr, uid, context=None):
        """Generates daily project task reminders"""
        if context == None:
            context = {}

        msg_obj = self.pool.get('mail.message')
        item_obj = self.pool.get('project.task')
        user_obj = self.pool.get('res.users')

        # we need to check all users
        users = user_obj.search(cr, uid, [])

        html_body_end = "<span><p><p/>"+_('Send from host %s - db %s')%(get_eth0(),cr.dbname)+"</span>"
        link = "<b><a href='{}?db={}#id={}&view_type=form&model={}&menu_id={}&action={}'>{}</a></b><br>"
        base_url = self.pool.get('ir.config_parameter').get_param(cr, SUPERUSER_ID, 'web.base.url')
        time_now = datetime.datetime.today().strftime('%Y-%m-%d')
        for user in user_obj.browse(cr, uid, users):

            task_items = []
            domain_filter = [('state', '!=', 'cancelled'),
                             ('state', '!=', 'done'),
                             ('user_id','=',user.id),
                             ('date_deadline','=',time_now)]

            items = item_obj.search(cr, uid, domain_filter)
            for item in item_obj.browse(cr, uid, items):
                task_items.append([item.id,item.name])

            context.update({'lang': user.lang})

            if task_items:
                task_items_links = ""
                for item in task_items:                 
                    task_items_links += link.format(base_url,cr.dbname,item[0],'project.task',288,336,item[1])
                body = _("You have {0} {1} {2}").format(len(task_items),_('project tasks with deadline today:<br>'),task_items_links)
                msg_vals = {
                    'subject': _("Deadline Project Task Reminder"),
                    'body': body + html_body_end,
                    'type': 'notification',
                    'notified_partner_ids': [(6,0,[user.partner_id.id])],
                }
                msg_obj.create(cr, uid, msg_vals)
                self.send_project_task_reminders_email(cr, uid, user, msg_vals, context=context)

        return True
 def get_html_body():
     base_url = self.pool.get('ir.config_parameter').get_param(cr, SUPERUSER_ID, 'web.base.url')
     link = ("<b><a href='{}?db={}#id={}&view_type=form&model=purchase.order&action=458'>{}</a></b>")
     line = link.format(base_url, cr.dbname, po.id, po.name)
     html_body_end = "<span><p><p/>"+_('Send from host %s - db %s')%(get_eth0(),cr.dbname)+"</span>"
     yield line + html_body_end
    def generate_purchase_approval_reminders(self, cr, uid, context=None):
        """Generates daily purchase approval reminders"""
        if context == None:
            context = {}

        msg_obj = self.pool.get('mail.message')
        item_obj = self.pool.get('purchase.approval.item')
        user_obj = self.pool.get('res.users')

        # we need to check all users because invoice to complete is possible for everybody
        users = user_obj.search(cr, uid, [])

        html_body_end = "<span><p><p/>"+_('Send from host %s - db %s')%(get_eth0(),cr.dbname)+"</span>"
        link = "<b><a href='{}?db={}#view_type=list&model={}&menu_id={}'>{}</a></b>"
        base_url = self.pool.get('ir.config_parameter').get_param(cr, SUPERUSER_ID, 'web.base.url')

        companies = self.pool.get('res.company').search(cr, uid, [])
        for user in user_obj.browse(cr, uid, users):

            domain_filter = [('state','=','waiting'),
                             '|',('line_next_approver_id','=',user.id),
                             '&',('line_next_approver_id','!=',False),
                                 ('line_next_approver_id.approval_substitute_id','=',user.id)]

            for company in self.pool.get('res.company').browse(cr, uid, companies):

                po_items = []
                inv_items = []
                inv_comp = []

                items = item_obj.search(cr, uid, domain_filter)

                # Find the PO and Invoices waiting approval
                for item in item_obj.browse(cr, uid, items):
                    if item.purchase_order_id and item.company_id.id == company.id:
                        po_items.append(item.purchase_order_id.id)
                    elif item.invoice_id and item.company_id.id == company.id: 
                        inv_items.append(item.invoice_id.id)

                # Find the invoices to comlete
                inv_comp = self.pool.get('account.invoice').search(cr, uid, [('dimension_user_id','=',user.id),('company_id','=',company.id)])

                context.update({'lang': user.lang})

                if po_items:
                    po_items_link = link.format(base_url,cr.dbname,'purchase.approval.item',728,_('Purchase Orders waiting approval'))
                    body = _("You have {0} {1} for company {2}").format(len(po_items),po_items_link,company.name)
                    msg_vals = {
                        'subject': _("Purchase Approval Reminder"),
                        'body': body + html_body_end,
                        'type': 'notification',
                        'notified_partner_ids': [(6,0,[user.partner_id.id])],
                    }
                    msg_obj.create(cr, uid, msg_vals)
                    self.send_purchase_approval_reminders_email(cr, uid, user, msg_vals, context=context)

                if inv_items:
                    inv_items_link = link.format(base_url,cr.dbname,'purchase.approval.item',729,_('Invoices waiting approval'))
                    body = _("You have {0} {1} for company {2}").format(len(inv_items),inv_items_link,company.name)
                    msg_vals = {
                        'subject': _("Invoice Approval Reminder"),
                        'body': body + html_body_end,
                        'type': 'notification',
                        'notified_partner_ids': [(6,0,[user.partner_id.id])],
                    }
                    msg_obj.create(cr, uid, msg_vals)
                    self.send_purchase_approval_reminders_email(cr, uid, user, msg_vals, context=context)

                if inv_comp:
                    inv_comp_link = link.format(base_url,cr.dbname,'account.invoice',526,_('Invoices to complete'))
                    body = _("You have {0} {1} for company {2}").format(len(inv_comp),inv_comp_link,company.name)
                    msg_vals = {
                        'subject': _("Invoice to Complete Reminder"),
                        'body': body + html_body_end,
                        'type': 'notification',
                        'notified_partner_ids': [(6,0,[user.partner_id.id])],
                    }
                    msg_obj.create(cr, uid, msg_vals)
                    self.send_purchase_approval_reminders_email(cr, uid, user, msg_vals, context=context)


        return True