Esempio n. 1
0
def add_contact(request, company):
    # add a new contact
    c = get_object_or_404(Company, url_name=company)
    
    # check permissions: needs to be manager
    if not has_permission(request.user, c, 'contact', 'edit'):
        return no_permission_view(request, c, _("You have no permission to add contacts."))

    t = request.GET.get('type')
    if not t:
        t = 'Individual'  # the default

    context = {
        'add': True,
        'type': t,
        'company': c,
        'title': _("Add contact"),
        'site_title': g.MISC['site_title'],
        'date_format_js': get_date_format(request.user, c, 'js')
    }

    if request.method == 'POST':
        # submit data
        form = ContactForm(request.POST)
        form.user = request.user
        form.company = c

        if form.is_valid():
            # create a new Contact
            contact = Contact(
                type=form.cleaned_data.get('type'),
                company_name=form.cleaned_data.get('company_name'),
                first_name=form.cleaned_data.get('first_name'),
                last_name=form.cleaned_data.get('last_name'),
                sex=form.cleaned_data.get('sex'),
                street_address=form.cleaned_data.get('street_address'),
                postcode=form.cleaned_data.get('postcode'),
                city=form.cleaned_data.get('city'),
                state=form.cleaned_data.get('state'),
                country=form.cleaned_data.get('country'),
                email=form.cleaned_data.get('email'),
                phone=form.cleaned_data.get('phone'),
                vat=form.cleaned_data.get('vat'),
                date_of_birth=form.cleaned_data.get('date_of_birth'),
                
                created_by=request.user,
                company=c
            )
            contact.save()

            return redirect('pos:list_contacts', company=c.url_name)
    else:
        form = ContactForm(initial={'type': t, 'country': c.country})
        form.user = request.user
        form.company = c

    context['form'] = form
    
    return render(request, 'pos/manage/contact.html', context)
Esempio n. 2
0
def update_document(request, company):
    try:
        c = Company.objects.get(url_name=company)
    except Company.DoesNotExist:
        return JsonError(_("Company does not exist."))

    # permissions
    if not has_permission(request.user, c, 'document', 'edit'):
        return JsonError(_("You have no permission to add contacts"))

    data = JsonParse(request.POST.get('data'))
    supplier = data['supplier']
    supplier = supplier.split(',')

    contact = None
    if len(supplier) > 0:
        vat = supplier[-1].strip()

        if not vat.isdigit():
            return JsonError("invalid_supplier")

        try:
            contact = Contact.objects.get(vat=vat)
        except Contact.DoesNotExist:
            Contact.copy_from_contact_registry(request, c, vat)

            try:
                contact = Contact.objects.get(vat=vat)
            except Contact.DoesNotExist:
                return JsonError("contact_does_not_exists")

    # TODO: validate with forms

    try:
        document = Document.objects.get(id=data['document_id'], company=c)
        document.number = data['document_number']
        document.entry_date = data['entry_date']
        document.document_date = data['document_date']

        if contact is not None:
            document.supplier = contact

        document.updated_by = request.user
        document.save()
    except Exception as e:
        print "Error updating document"
        print e

        return JsonError('could_not_save_document')

    return JsonOk()
Esempio n. 3
0
def quick_contacts(request, company):
    """
        creates contact on the fly (while creating bill on the terminal)
        contact data is in request.POST
    """
    try:
        c = Company.objects.get(url_name=company)
    except Company.DoesNotExist:
        return JsonError(_("Company does not exist."))

    # permissions
    if not has_permission(request.user, c, 'contact', 'edit'):
        return JsonError(_("You have no permission to add contacts"))

    contact = JsonParse(request.POST.get('data'))

    if not contact:
        return JsonError(_("No data in request"))

    r = validate_contact(request.user, c, contact)
    if not r['status']:
        return JsonError(r['message'])

    contact = r['data']

    if int(contact.get('id')) == -1:
        # it's a new contact: use .get() and forget about different types
        try:
            obj = Contact(
                created_by=request.user,
                company=c,

                type=contact.get('type'),
                company_name=contact.get('company_name'),
                first_name=contact.get('first_name'),
                last_name=contact.get('last_name'),
                sex=contact.get('sex'),
                date_of_birth=contact.get('date_of_birth'),
                street_address=contact.get('street_address'),
                postcode=contact.get('postcode'),
                city=contact.get('city'),
                state=contact.get('state'),
                country=contact.get('country'),
                email=contact.get('email'),
                phone=contact.get('phone'),
                vat=contact.get('vat')
            )

            obj.save()
        except Exception, e:
            return JsonResponse(_("Saving contact failed") + ": " + str(e))
Esempio n. 4
0
def save_document(request, company):
    try:
        c = Company.objects.get(url_name=company)
    except Company.DoesNotExist:
        return JsonError(_("Company does not exist."))

    # permissions
    if not has_permission(request.user, c, 'document', 'edit'):
        return JsonError(_("You have no permission to add contacts"))

    data = JsonParse(request.POST.get('data'))
    supplier = data['supplier']
    supplier = supplier.split(',')

    if len(supplier) == 0:
        return JsonError("invalid_supplier")

    vat = supplier[-1].strip()

    if not vat.isdigit():
        return JsonError("invalid_supplier")

    try:
        contact = Contact.objects.get(vat=vat)
    except Contact.DoesNotExist:
        Contact.copy_from_contact_registry(request, c, vat)

        try:
            contact = Contact.objects.get(vat=vat)
        except Contact.DoesNotExist:
            return JsonError("contact_does_not_exists")

    # TODO: validate with forms

    try:
        document = Document(
            company=c,
            number=data['document_number'],
            entry_date=data['entry_date'],
            document_date=data['document_date'],
            supplier=contact,
            created_by=request.user
        )
        document.save()
    except Exception as e:
        print "Error saving document"
        print e

        return JsonError('could_not_save_document')


    documents = Document.objects.filter(company=c).order_by('-number')
    page = 1
    i = 0

    for d in documents:
        if i > 0 and i%g.MISC['documents_per_page'] == 0:
            page += 1

        if d == document:
            break

        i += 1

    redirect_url = reverse('pos:manage_documents', kwargs={'company': c.url_name, 'page': page}) + "#" + str(document.id)

    return JsonOk(extra={'redirect_url': redirect_url})