def company_details(request, company_id): doc_path = [] # doc_path=[{"who":"Sales","status":1},{"who":"PM","status":1},{"who":"TV","status":0}] try: my_company = Company.objects.get(id=int(company_id)) except Order.DoesNotExist: raise Http404("Организация не найдена") order_types = OrderType.objects.all() current_user = request.user allowed_order_types = [] for i in order_types: allowed = False if i.requires_permission == "": allowed = True else: for my_per in [perm[1] for perm in current_user.groups.values_list()]: if my_per == i.requires_permission: allowed = True if allowed: allowed_order_types.append(i) orders = [] for i in Order.objects.all(): if i.company.id == my_company.id: orders.append({"order": i, "doc_path": doc_path_f(i)}) context = {'company': my_company, 'orders': orders, "order_types": allowed_order_types} log(request.user.last_name, "company detail " + str(my_company.id)) return render(request, "oms-form-company-detail.html", context)
def newcompany(request): current_user = request.user form = FormNewCompany() if request.method == "POST": form = FormNewCompany(request.POST) data = form.data log(current_user.last_name, "New company " + data['company_name']) if form.is_valid() or True: cmp = Company() cmp.company_name = data['company_name'] cmp.company_bin = data['company_bin'] cmp.company_inn = data['company_inn'] cmp.company_email = data['company_email'] cmp.too_id = int(data['opf']) cmp.status_id = int(data['status']) cmp.region_id = int(data['region']) cmp.address = data['address'] cmp.contact_person = data['contact_person'] cmp.contact_details = data['contact_details'] cmp.user_name = current_user.first_name + " " + current_user.last_name cmp.user_id = current_user.id cmp.save() return HttpResponseRedirect("/orders/company" + str(cmp.id)) else: print(form.errors) context = { 'user': current_user, 'toos': GeneralDirectory.objects.filter(directory="opf"), 'statuses': GeneralDirectory.objects.filter(directory="status"), 'regions': GeneralDirectory.objects.filter(directory="region"), 'errors': form.errors } #return render(request, "oms-form-newcompany.html", context, {'form': form}) return HttpResponseRedirect("/")
def close_order(request, order_id): try: order = Order.objects.get(id=int(order_id)) except Order.DoesNotExist: raise Http404("Заказ не найден") solutions = [] for i, sol in enumerate(order.order_type.solutions.split(",")): solutions.append({"id": i, "value": sol}) current_user = request.user if request.method == "POST": form = FormClose(request.POST) # files = request.FILES.getlist('file_field') the_message = "" data = form.data if not (data["comments"] is None): the_message = data["comments"] the_solution = "" for i in solutions: if i["id"] == int(data["solution"]): the_solution = i["value"] if form.is_valid() or True: a = Action() a.order_id = order.id a.action = "Закрыто: (" + the_solution + ")\n" if the_message != "": a.action += "Сообщение:" + the_message a.user_name = current_user.username a.user_id = current_user.id a.user_to_id = current_user.id a.user_to_role = "Закрыто" a.message = the_message a.save() log(request.user.last_name, "closed order " + str(order.id) + " with " + a.message) order.closed = True order.save() print("closed successfully") return HttpResponseRedirect("/orders/order" + order_id) else: print("Error: ", str(form.errors)) form = FormClose() context = {'order': order, 'all_actions': order.action_set.all(), 'user': current_user, 'solutions': solutions} # return render(request, "oms-form-close.html", context, {'form': form}) return HttpResponseRedirect("/")
def companies(request): current_user = request.user log(current_user.last_name, "companies list") all_orders = Order.objects.all() my_orders = [] filtered = [] for i in all_orders: if i.current_user_id == current_user.id or i.user_id == current_user.id: my_orders.append(i) if request.method == "POST": form = FormSearch(request.POST) data = form.data log(current_user.last_name, "companies list SEARCH " + data["search"]) if form.is_valid(): s = data['search'] filtered = [] for i in Company.objects.all().order_by('-id'): if (s.lower() in i.company_bin.lower()) or (s.lower() in i.company_name.lower()) or (s == str(i.id)) \ or (s.lower() in i.company_email): filtered.append(i) context = { 'all_companies': filtered, 'searched': s, } return render(request, "oms-form-companies-list.html", context, {'form': form}) form = FormSearch() context = { 'all_companies': Company.objects.all().order_by('-id')[:10], 'user': current_user, 'toos': GeneralDirectory.objects.filter(directory="opf"), 'statuses': GeneralDirectory.objects.filter(directory="status"), 'regions': GeneralDirectory.objects.filter(directory="region"), } return render(request, "oms-form-companies-list.html", context, {'form': form})
def order_detail_view(request, order_id): try: my_order = Order.objects.get(id=int(order_id)) except Order.DoesNotExist: raise Http404("Заказ не найден") solutions = [] for i, sol in enumerate(my_order.order_type.solutions.split(",")): solutions.append({"id": i, "value": sol}) # ------------------------- For forwarding users_with_roles = [] for i in User.objects.all(): groups = [i[1] for i in i.groups.values_list()] for j in groups: users_with_roles.append(j + ": " + i.first_name + " " + i.last_name) actions_with_files = [] for k, i in enumerate(my_order.action_set.all()): files = [] for j in Attachment.objects.all().filter(parent_id=i.id): files.append({"url": j.attachment.url, "name": j.file_name}) action = {"action": i, "files": files} actions_with_files.append(action) cloned_actions_with_files = [] company = my_order.company # ------------------- For order fill form fields = [] order = my_order order_field_values = order.fields_values if (order_field_values == None): order_field_values = "" order_fields = order_field_values.split(";;") stop_it = False for i, field in enumerate(order.order_type.fields.split(",")): field_name, field_description, field_type, directory, default, value, default_int, required = "field" + str( i), "error", "error", [], "", "", -1, False field_role = "-1" for f in Field.objects.all(): if f.id == int(field) and \ (f.field_role is None or f.field_role == "" or f.field_role == order.current_user_role): if f.required: required = True field_description = f.field_name field_type = f.field_type field_id = f.id for order_field in order_fields: try: a, b = order_field.split("::") if a == str(f.id): default = b value = b try: default_int = int(b) for gendir in GeneralDirectory.objects.all(): if gendir.id == int(b) and field_type == gendir.directory: value = gendir.value break except: default_int = -1 except: # print("v_o_m:153") pass for di in GeneralDirectory.objects.all(): if di.directory == field_type: directory.append({"id": di.id, "value": di.value}) if field_type != "error" and not stop_it: fields.append({"name": field_name.lower(), "description": field_description, "type": field_type.lower(), "directory": directory, "field_id": field_id, "default": default, "value": value, "default_int": default_int, "required": required}) # - CLONED ORDER FIELDS cloned_fields = [] if order.cloned_order: order_field_values = order.cloned_order.fields_values if order_field_values is None: order_field_values = "" order_fields = order_field_values.split(";;") # print(order_fields) for i, field in enumerate(order.cloned_order.order_type.fields.split(",")): field_name, field_description, field_type, directory, default, value, default_int, required = "field" + str( i), "error", "error", [], "", "", -1, False for f in Field.objects.all(): if f.id == int(field): field_description = f.field_name field_type = f.field_type field_id = f.id for order_field in order_fields: try: a, b = order_field.split("::") if a == str(f.id): # print("match", f.id) default = b value = b try: default_int = int(b) for gendir in GeneralDirectory.objects.all(): if gendir.id == int(b) and field_type == gendir.directory: value = gendir.value break except: default_int = -1 except: pass if field_type != "error" and not stop_it: if value != "": cloned_fields.append({"name": field_name.lower(), "description": field_description, "type": field_type.lower(), "field_id": field_id, "default": default, "value": value, "default_int": default_int, "required": required}) today = datetime.now() context = {'doc_path': doc_path_f(my_order), 'order': my_order, 'company': company, 'all_actions': actions_with_files, 'cloned_all_actions': cloned_actions_with_files, 'order_type': OrderType.objects.get(id=my_order.order_type_id).description, 'order_types': OrderType.objects.all(), 'complete': complete(my_order), 'users': users_with_roles, 'solutions': solutions, 'fields': fields, 'cloned_fields': cloned_fields, 'tomorrow_date': today + relativedelta(days=1)} log(request.user.last_name, "order detail " + str(my_order.id)) return render(request, "oms-form-order-detail.html", context)
def fill_form_action(request, order_id): fields = [] try: order = Order.objects.get(id=int(order_id)) except Order.DoesNotExist: raise Http404("Заказ не найден") order_field_values = order.fields_values if (order_field_values == None): order_field_values = "" order_fields = order_field_values.split(";;") stop_it = False for i, field in enumerate(order.order_type.fields.split(",")): field_name, field_description, field_type, directory, default, default_int, required = "field" + str( i), "error", "error", [], "", -1, False field_role = "-1" for f in Field.objects.all(): if f.id == int(field) and \ (f.field_role is None or f.field_role == "" or f.field_role == order.current_user_role): if f.required: required = True field_description = f.field_name field_type = f.field_type field_id = f.id for order_field in order_fields: try: a, b = order_field.split("::") if a == str(f.id): default = b try: default_int = int(b) except: default_int = -1 except: # print("v_o_m:153") pass for di in GeneralDirectory.objects.all(): if di.directory == field_type: directory.append({"id": di.id, "value": di.value}) if f.id == int(field) and \ (f.field_role != "" and (f.field_role is not None) and f.field_role != order.current_user_role): me_found = False for order_field in order_fields: try: a, b = order_field.split("::") if a == str(f.id): me_found = True except: # print("v_o_m:153") # if me_found: # fields = [] # else: # stop_it = True pass if field_type != "error" and not stop_it: fields.append({"name": field_name.lower(), "description": field_description, "type": field_type.lower(), "directory": directory, "field_id": field_id, "default": default, "default_int": default_int, "required": required}) current_user = request.user if request.method == "POST": # form = FormNewAction(request.POST, request.FILES) files = request.FILES.getlist('file_field') action = Action() action.order_id = order.id fields_values = [] actions_text = "" action.user_name = current_user.first_name + " " + current_user.last_name action.user_id = current_user.id action.save() for i in fields: is_changed = True try: field_files = request.FILES.getlist(i['name']) for f in field_files: instance = Attachment( parent_id=str(action.id), file_name=f.name, attachment=f ) instance.save() fields_values.append(str(i['field_id']) + "::" + str(instance.id)) actions_text += "<b>" + i[ 'description'] + ":</b> " + '<a href = "' + instance.attachment.url + '">' + instance.file_name + "</a>\n" except: print("v_o_m:249") if request.POST.get(i['name']) != "" and not (request.POST.get(i['name']) is None): found_in_order_fields = False for j in range(len(order_fields)): try: a, b = order_fields[j].split("::") if str(i['field_id']) == a: if order_fields[j] == str(i['field_id']) + "::" + request.POST.get(i['name']): is_changed = False order_fields[j] = str(i['field_id']) + "::" + request.POST.get(i['name']) found_in_order_fields = True except: print("v_o_m:262") if not found_in_order_fields: fields_values.append(str(i['field_id']) + "::" + request.POST.get(i['name'])) # actions_text += "\n" value_ = request.POST.get(i['name']) try: for gendir in GeneralDirectory.objects.all(): if gendir.id == int(value_) and i["type"] == gendir.directory: value_ = gendir.value break except: a = 5 if is_changed or len(order.action_set.all()) == 1: actions_text += "<b>" + i['description'] + ":</b> " + value_ + "\n" action.action = actions_text action.fields = ";;".join(fields_values) log(request.user.last_name, "filled form for " + str(order.id) + " with " + action.action) if actions_text.strip() != "" or files: action.save() company = order.company order.fields_values = ";;".join(order_fields + fields_values) order.save() #company.fields_values = order.fields_values company.save() for f in files: instance = Attachment( parent_id=str(action.id), file_name=f.name, attachment=f ) instance.save() log(request.user.last_name, "attached file for " + str(order.id) + " " + instance.file_name) return HttpResponseRedirect("/orders/order" + order_id) context = {'order': order, 'user': current_user, 'fields': fields} # return render(request, "oms-form-fill.html", context) return HttpResponseRedirect("/")
def forward(request, order_id): try: order = Order.objects.get(id=int(order_id)) except Order.DoesNotExist: raise Http404("Заказ не найден") users_with_roles = [] for i in User.objects.all(): groups = [i[1] for i in i.groups.values_list()] for j in groups: users_with_roles.append(j + ": " + i.first_name + " " + i.last_name) current_user = request.user if request.method == "POST": form = FormForward(request.POST, request.FILES) # files = request.FILES.getlist('file_field') the_message = "" data = form.data if not (data["message"] is None): the_message = data["message"] if form.is_valid() or 1 + 1 == 2: to_user = User() if len(data["target_text"].split(":")) < 2: log (request.user.last_name, "error forwarding, name not selected") return HttpResponseRedirect("/") to_username = data["target_text"].split(":")[1].strip() to_userrole = data["target_text"].split(":")[0] for i in User.objects.all(): if i.first_name + " " + i.last_name == to_username: to_user = i a = Action() a.order_id = order.id a.action = "Направлено: " + to_user.first_name + " " + to_user.last_name + " (" + to_userrole + ")\n" if the_message != "": a.action += "Сообщение:" + the_message a.user_name = current_user.username a.user_id = current_user.id a.user_to_id = to_user.id a.user_to_role = to_userrole a.message = the_message a.deadline = data["deadline_date"] + "T" + data["deadline_time"] a.save() log(request.user.last_name, "forward order " + str(order.id) + " to " + to_user.last_name) order.current_user_id = to_user.id order.current_user_name = to_user.first_name + " " + to_user.last_name order.current_user_role = to_userrole order.deadline = data["deadline_date"] + "T" + data["deadline_time"] order.save() send_mail( 'Вам направлен документ #' + str(order.id) + " " + str(order), 'Здравствуйте!\n\nВам направлен документ номер ' + str(order.id) + ':\n' + str(order) + "\n\n" + "Направил: " + current_user.first_name + " " + current_user.last_name + "\n" + "Сообщение: " + a.message + "\n\n" "Пожалуйста, пройдите по ссылке: http://kuanysh.kz/orders/order" + order_id, settings.EMAIL_HOST_USER, [to_user.email], fail_silently=False ) print("forwarded successfully") return HttpResponseRedirect("/orders/order" + order_id) else: print("Error: ", str(form.errors)) context = {'order': order, 'all_actions': order.action_set.all(), 'user': current_user, 'users': users_with_roles, } # return render(request, "oms-form-forward.html", context) return HttpResponseRedirect("/")
def index(request): current_user = request.user log(current_user.last_name, "index") all_orders = Order.objects.all() my_orders = [] for _order in all_orders: if _order.current_user_id == current_user.id or _order.user_id == current_user.id: my_orders.append({"order": _order, "doc_path": doc_path_f(_order)}) order_types = OrderType.objects.all() current_user = request.user allowed_order_types = [] for i in order_types: allowed = False if i.requires_permission == "": allowed = True else: for my_per in [ perm[1] for perm in current_user.groups.values_list() ]: if my_per == i.requires_permission: allowed = True if allowed: allowed_order_types.append(i) if request.method == "POST": form = FormSearch(request.POST) data = form.data log(current_user.last_name, "index SEARCH " + data['search']) if form.is_valid(): s = data['search'] filtered = [] for _order in my_orders: yes = False if (s.lower() in _order["order"].company.company_bin.lower()) or \ (s.lower() in _order["order"].company.company_name.lower()) or \ (s.lower() in str(_order["order"]).lower()) or \ (s == str(_order["order"].id)) or \ (s.lower() in _order["order"].current_user_name.lower()) or\ (s.lower() in _order["order"].user_name.lower()) or\ (s.lower() in _order["order"].company.company_email): yes = True for j in _order["order"].action_set.all(): if not (j.fields is None): if s.lower() in j.fields.lower(): yes = True if not (j.action is None): if s.lower() in j.action.lower(): yes = True for _file in Attachment.objects.all(): if (s.lower() in _file.file_name.lower()) and ( _file.parent_id == str(j.id)): yes = True if yes: filtered.append(_order) context = { 'all_orders': filtered, 'searched': s, } return render(request, "oms-form-orders-list.html", context, {'form': form}) form = FormSearch() context = { 'all_orders': my_orders[0:10], 'user': current_user, "companies": Company.objects.all(), "order_types": allowed_order_types } return render(request, "oms-form-orders-list.html", context, {'form': form})
def neworder(request, company_id=None, clone_order_id=None): order_types = OrderType.objects.all() companies = Company.objects.all() if company_id is not None: try: companies = [Company.objects.get(id=int(company_id))] except Company.DoesNotExist: raise Http404("Организация не найдена") if clone_order_id is not None: try: clone_order = Order.objects.get(pk=int(clone_order_id)) except Order.DoesNotExist: raise Http404("Заявка не найдена") current_user = request.user log(current_user.last_name, "new order") allowed_order_types = [] for i in order_types: allowed = False if i.requires_permission == "": allowed = True else: for my_per in [perm[1] for perm in current_user.groups.values_list()]: if my_per == i.requires_permission: allowed = True if allowed: allowed_order_types.append(i) form = FormNewOrder() errors = [] if request.method == "POST": form = FormNewOrder(request.POST) data = form.data if form.is_valid(): o = Order() fields = [] for order_type in OrderType.objects.all(): if order_type.id == int(data['order_type']): o.current_user_role = order_type.requires_permission fields = order_type.fields o.company_id = data['company'] # data['company_name'] o.user_id = current_user.id o.current_user_id = current_user.id o.order_type_id = int(data['order_type']) # o.fields_values = only_my_fields(Company.objects.get(id=int(data['company'])).fields_values,o.current_user_role,fields) if clone_order_id is not None: o.cloned_order_id = clone_order.id o.save() if o.order_type.num_prefix is not None: o.number = o.order_type.num_prefix + str(o.id) if clone_order_id is not None: if o.cloned_order.number is not None: o.number += o.cloned_order.number else: o.number = "N"+str(o.id) o.save() log(current_user.last_name, "new order no " + str(o.id)) a = Action() a.order_id = o.id a.action = "Инициализация заявки" if clone_order_id is not None: a.action = "Клонировано из заявки " + str(o.cloned_order.order_type) + " " + (str(o.cloned_order.id), o.cloned_order.number)[o.cloned_order.number is not None] log(request.user.last_name, "new order cloned from " + str(clone_order_id)) a.user_id = current_user.id a.user_to_id = current_user.id a.user_to_role = o.current_user_role a.user_name = current_user.first_name + " " + current_user.last_name a.save() if o.cloned_order_id is not None: o.fields_values += o.cloned_order.fields_values o.save() for act in o.cloned_order.action_set.all(): for file in Attachment.objects.filter(parent_id=str(act.id)): file1 = Attachment() file1.parent_id = str(a.id) file1.file_name = file.file_name file1.attachment = file.attachment file1.save() print("copied file " + file1.file_name) # all_orders = Order.objects.all() return HttpResponseRedirect("/orders/order"+str(o.id)) else: print(form.errors) context = {'user': current_user, 'order_types': allowed_order_types, 'companies': companies, 'errors': form.errors} #return render(request, "oms-form-neworder.html", context, {'form': form}) return HttpResponseRedirect("/")