views.py 6.99 KB
Newer Older
Administrator committed
1 2 3 4
from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from django.template import loader
5
from django.views.decorators.csrf import csrf_exempt
Administrator committed
6 7
from members.models import CagetteUser
from .models import CagetteInventory
8
from outils.common_imports import *
Administrator committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import json

def home(request):
    """Page de selection de la commande suivant un fournisseurs"""
    context = {'title': 'Inventaires',
               'TOOLS_SERVER': settings.TOOLS_SERVER,
               'couchdb_server': settings.COUCHDB['url'],
               'db': settings.COUCHDB['dbs']['inventory']}
    template = loader.get_template('inventory/index.html')

    return HttpResponse(template.render(context, request))

def custom_lists(request):
    """Affichage des listes de produits à inventorier"""
    lists = CagetteInventory.get_custom_lists()

    context = {'title': 'Listes de produits à inventorier',
               'lists' : json.dumps(lists),
              }
    template = loader.get_template('inventory/custom_lists.html')

    return HttpResponse(template.render(context, request))

32 33 34 35 36 37 38 39 40
def delete_custom_list(request):
    """Custom list file will be removed."""
    try:
        res = CagetteInventory.remove_custom_inv_file(request.POST.get('id'))
        return JsonResponse({'success': True})
    except Exception as e:
        coop_looger.error("delete_custom_list : %s", str(e))
        return JsonResponse({'error': str(e)}, status=500)

Administrator committed
41 42 43 44 45 46 47 48 49
def custom_list_inventory(request, id):
    """Inventaire d'une liste de produits"""
    products = CagetteInventory.get_custom_list_products(id)

    if 'error' in products:
        products['data'] = []

    context = {'title': 'Inventaire',
               'products' : json.dumps(products['data']),
50
               'ahead_shelfs_ids': json.dumps(getattr(settings, 'SHELFS_TO_BE_AHEAD_IN_SELECT_LIST', []))
Administrator committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
              }

    # Reuse shelf inventory template: same process
    template = loader.get_template('shelfs/shelf_inventory.html')

    return HttpResponse(template.render(context, request))

def get_custom_list_data(request):
    id = request.GET.get('id', '')

    try:
        lists = CagetteInventory.get_custom_lists(id)
        return JsonResponse({'res': lists[0]})
    except Exception as e:
        return JsonResponse(id, status=500)

67 68 69 70 71 72 73 74 75 76 77
def inventory_process_state(request, list_id):
    res = {}

    try:
        res['state'] = CagetteInventory.get_custom_list_inv_status(list_id)
    except Exception as e:
        # Exception raised: no file found ; inventory is done
        res['state'] = 'done'

    return JsonResponse({'res': res})

Administrator committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
def do_custom_list_inventory(request):
    res = {}
    data = json.loads(request.body.decode())

    inventory_data = {
        'id': data['id'],
        'name': 'Inventaire personnalisé du '+ data['datetime_created'],
        'user_comments': data['user_comments'],
        'products': data['list_processed']
    }

    try:
        if data['inventory_status'] == '' :
            # First step: update inventory file
            res = CagetteInventory.update_custom_inv_file(inventory_data)
        else:
            # Get data from step 1
            full_inventory_data = CagetteInventory.get_full_inventory_data(inventory_data)

            # Proceed with inventory
98
            res['inventory'] = CagetteInventory.update_products_stock(full_inventory_data)
Administrator committed
99 100 101 102 103

            # remove file
            CagetteInventory.remove_custom_inv_file(inventory_data['id'])
    except Exception as e:
        res['error'] = {'inventory' : str(e)}
104
        coop_logger.error("Enregistrement inv. personnalisé : %s", str(e))
Administrator committed
105 106 107 108 109 110

    if 'error' in res:
        return JsonResponse(res, status=500)
    else:
        return JsonResponse({'res': res})

111
@csrf_exempt
112 113 114
def generate_inventory_list(request):
    """Responding to Odoo ajax call (no csrf)."""
    res = {}
115
    default_partners_id = []
116 117 118
    try:
        lines = json.loads(request.POST.get('lines'))
        ltype = request.POST.get('type')
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    except Exception as e:
        try:
            # POST.get() returns None when request from django
            data = json.loads(request.body.decode())
            lines = data["lines"]
            ltype = data["type"]
            if "partners_id" in data:
                default_partners_id = data["partners_id"]
        except Exception as ee:
            res['error'] = str(ee)
            coop_looger.error("generate_inventory_list : %s", str(e))
            return JsonResponse(res, status=500)
        
    try:
        res = CagetteInventory.create_custom_inv_file(lines, ltype, default_partners_id)
134 135 136 137 138 139 140 141
    except Exception as e:
        res['error'] = str(e)
        coop_looger.error("generate_inventory_list : %s", str(e))
    if 'error' in res:
        return JsonResponse(res, status=500)
    else:
        return JsonResponse({'res': res})

Administrator committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
def get_credentials(request):
    """Receiving user mail + password, returns id, rights and auth token"""
    return JsonResponse(CagetteUser.get_credentials(request))

def get_product_categories(request):
    return JsonResponse(CagetteInventory.get_product_categories(), safe=False)

def create_inventory(request):
    res = {}
    if CagetteUser.are_credentials_ok(request):
        import json
        cats = json.loads(request.POST.get('cats'))
        res['products'] = CagetteInventory.get_products_from_cats(cats)
    else:
        res['msg'] = 'Forbidden'
    return JsonResponse(res)

def update_odoo_stock(request):
    res = {}
    if CagetteUser.are_credentials_ok(request):
        try:
            doc_id = request.POST.get('doc_id')
            res['action'] = CagetteInventory.update_stock_with_inventory_data(doc_id)
        except Exception as e:
            res['msg'] = str(e)
    else:
        res['msg'] = 'Forbidden'
    return JsonResponse(res)

def raz_archived_stock(request):
    res = {}
    if CagetteUser.are_credentials_ok(request):
        try:
            res['action'] = CagetteInventory.raz_archived_stock()
        except Exception as e:
            res['msg'] = str(e)
    else:
        res['msg'] = 'Forbidden'
    return JsonResponse(res)

def raz_negative_stock(request):
    res = {}
    if CagetteUser.are_credentials_ok(request):
        try:
            res['action'] = CagetteInventory.raz_negative_stock()
        except Exception as e:
            res['msg'] = str(e)
    else:
        res['msg'] = 'Forbidden'
    return JsonResponse(res)
def raz_not_saleable(request):
    res = {}
    if CagetteUser.are_credentials_ok(request):
        try:
            res['action'] = CagetteInventory.raz_not_saleable_stock()
        except Exception as e:
            res['msg'] = str(e)
    else:
        res['msg'] = 'Forbidden'
    return JsonResponse(res)

def cancel_buggy_pos_sales_waiting_transfer(request):
    res = {}

    try:
        res['action'] = CagetteInventory.cancel_buggy_pos_sales_waiting_transfer()
    except Exception as e:
        res['msg'] = str(e)

    return JsonResponse(res)

def process_pos_sales_waiting_transfer(request):
    # TODO : priority is to stop what's making error !!
    return JsonResponse(res)