purchase_order.py 2.37 KB
Newer Older
1 2 3
# -*- coding: utf-8 -*-

from openerp import api, models, fields
4 5
from openerp.tools.translate import _
from openerp.exceptions import UserError
6 7 8 9 10


class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    def verify_product_line_unicity(self):
        pdts = {}
        for order in self:
            for line in order.order_line:
                if not line.product_id.id in pdts:
                    pdts[line.product_id.id] = 0
                pdts[line.product_id.id] += 1
        pids = []
        for pid, nb in pdts.items():
            if nb > 1:
                pids.append(pid)
        if len(pids) > 0:
            products = self.env['product.product'].search([('id', 'in', pids)])
            message = ''
            for p in products:
                message += p.display_name + ' (' + str(p.product_tmpl_id.id) + ')'
                message += ' --> ' + str(pdts[p.id]) + " lines\n"
            raise UserError(_(message))
    
30 31 32 33 34
    @api.multi
    def write(self, vals):
        res = super(PurchaseOrder, self).write(vals)
        if 'state' in vals and vals['state'] == 'purchase':
            import requests
35 36 37
            # First of all, verify that no product has been added twice or more
            # an error is raised if a product appears more than once
            self.verify_product_line_unicity()
38 39 40 41 42
            conf = self.env['ir.config_parameter']
            export_url = str(conf.get_param('cagette_purchase.orders_proxy_url')).strip()
            export_url += '/export/' + str(self.id)
            requests.get(export_url)
        return res
43 44 45 46 47 48 49 50 51 52

    @api.multi
    def get_received_orders_between_dates(self, date_from, date_to):
        res = {}
        sql = """
            SELECT po.name as id_po, sp.date_done, po.amount_untaxed, po.amount_total, po.state, rp.name as supplier_name
            FROM purchase_order as po
            LEFT JOIN stock_picking as sp ON po.name=sp.origin
            LEFT JOIN res_partner as rp ON po.partner_id=rp.id
            WHERE sp.date_done IS NOT NULL
53 54
            AND sp.date_done >= '{date_from}'
            AND sp.date_done <= '{date_to}'
55 56 57 58 59 60 61 62 63 64 65 66
            ORDER BY sp.date_done ASC
        """

        sql = sql.format(date_from=date_from, date_to=date_to)

        try:
            self.env.cr.execute(sql)
            res["data"] = self.env.cr.dictfetchall()
        except Exception as e:
            res["error"] = str(e)

        return res