models.py 2.22 KB
Newer Older
Damien Moulard committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from django.db import models
from outils.common_imports import *

from outils.common import OdooAPI


class CagetteSales(models.Model):
    """Class to manage operations on envelops"""

    def __init__(self):
        """Init with odoo id."""
        self.o_api = OdooAPI()

    def get_sales(self, date_from, date_to):
        res = []

17 18
        # Get pos orders
        cond = [['date_order', '>=', date_from], ['date_order', '<=', date_to]]
19
        fields = ['partner_id', 'statement_ids', 'name']
20 21
        orders = self.o_api.search_read('pos.order', cond, fields, limit=50000)

Damien Moulard committed
22 23
        # Get bank statements of these sessions
        statements = []
24
        statements_partners = {}
25
        statements_orders = {}
26 27 28
        for o in orders:
            statements = statements + o["statement_ids"]
            for s in o["statement_ids"]:
29 30 31 32 33
                if o["partner_id"] is False:
                    statements_partners[s] = "Membre non renseigné"
                else:
                    statements_partners[s] = o["partner_id"][1]
    
34
                statements_orders[s] = o["name"]
35

Damien Moulard committed
36
        # Get payment lines
37
        cond = [['id', 'in', statements]]
38
        fields = ["amount", "journal_id", "create_date", "meal_voucher_issuer"]
39
        payments = self.o_api.search_read('account.bank.statement.line', cond, fields, order="create_date ASC", limit=50000)
Damien Moulard committed
40 41
        try:
            for payment in payments:
42
                res.append({
43
                            "partner": statements_partners[payment["id"]],
Damien Moulard committed
44
                            "create_date": payment["create_date"],
45
                            "pos_order_name": statements_orders[payment["id"]],
Damien Moulard committed
46 47 48 49
                            "total_amount": round(float(payment["amount"]), 2),
                            "payments": [
                                {
                                    "amount": round(float(payment["amount"]), 2),
50 51
                                    "journal_id": payment["journal_id"],
                                    "meal_voucher_issuer": payment["meal_voucher_issuer"]
52
                                }
Damien Moulard committed
53
                            ]
54
                           })
Damien Moulard committed
55
        except Exception as e:
56
            coop_logger.error("get_sales %s", str(e))
Damien Moulard committed
57 58

        return res