示例#1
0
    def savebasket(self):
        wholesale = False
        if "repoze.who.identity" in request.environ:
            user = request.environ.get('repoze.who.identity')['user']
            wholesale_tag = Session.query(UserTag).filter_by(tag='wholesale').one()
            if wholesale_tag in user.tags:
                wholesale = True
                c.wholesale=True

        values = dict(request.params)
        action = request.params.getone('action')
        del values['action']

        if action == _('Remove all items from basket'):
            session['basket'] = {}
            h.flash(_('All items removed from basket'))
            session.save()            
            redirect(url(controller='product', action='list'))

        if action.startswith(_('Remove item')):
            gid = int(action.split(' ')[-1])
            for item in values:
                if item.startswith('quantityid-%s'%gid):
                    id = int(values[item])
            del session['basket'][id]
            session.save()
            newvalues = remove_item(values,action,'quantity')
            newvalues = remove_item(newvalues,action,'quantityid')
            return render_editbasket(self.menu_items,values=newvalues)

        if action == _('Buy'):
            identity = request.environ.get('repoze.who.identity')            
            if identity is None:
                redirect(
                    url(controller='account',
                        action='login',
                        came_from=url(controller='product', action='editbasket')
                        )
                )
            schema = Editbasket()
            try:
                result = schema.to_python(dict(request.params), c)
            except Invalid, e:
                return render_editbasket(self.menu_items,values=values, errors=variabledecode.variable_encode(
                    e.unpack_errors() or {},
                    add_repetitions=False)
                                         )               
            else:
                for i in range(len(result['quantityid'])):
                    session['basket'][result['quantityid'][i]] = result['quantity'][i]
                session.save()
                # create pending invoices. email to customer and email to sales
                user = Session.query(User).filter_by(id=session['user']).one()
                items=[]
                dt = datetime.now()
                productsmsg= ""
                total = 0
                invoice = Invoice(dt,user,None,None,0,[],True)
                for id in session['basket']:
                    product = Session.query(Product).filter_by(id=id).one()                    
                    quantity = session['basket'][id]
                    if wholesale:
                        price = product.wholesale_price
                    else:
                        price = product.sell_price
                    totprice = quantity * price
                    productsmsg +='<p>'+_('Code')+' :'+str(product.code)+'<br/>'+_('Name')+' :' + product.name+'<br/>'+_('Unit price')+' :'+str(price)+'<br/>'+_('Quantity')+' :'+str(quantity)+'<br/>'+_('Total price')+' :'+str(totprice)+'<br/>'+'</p>'+'<hr>'
                    total += totprice
                    item = Invoice_item(product,
                                        invoice,                                 
                                        quantity,
                                        totprice,
                                        unitprice=price
                                        )
                    items.append(item)
                invoice.invoice_items = items
                invoice.total_price = total
                Session.add(invoice)
                #Session.commit()

                customer_message = Message(session['site_settings']['invoicealertfrom'],
                                           #user.emails[0].email_address, 
                                           "*****@*****.**",
                                           _("your order details from Kazhal"),
                                           encoding='utf-8')		    
                plain = _("You placed an order of the following items with Kazhal")+' :'+ productsmsg + "<br/>"+ _("Total price")+' :'+str(total) +"<br/>"+_("our staff will contact you to confirm the purchase.<br/>Thank you.<br/>Kazhal")		
                c.usermsg =  _("You placed an order of the following items with Kazhal")+_("our staff will contact you to confirm the purchase.<br/>Thank you.<br/>Kazhal")		
                c.user = user
                c.invoice = invoice
                c.total = total		
                customerHtml = render(_('/derived/emails/customer_invoice.html'))
                customer_message.rich = customerHtml
                customer_message.plain = plain

                splain = _("User")+u' :'+user.user_name +u'<br/>'+_("Phone")+u' :'+unicode([int(phone.phone_number) for phone in user.phones])[1:-1]+u'<br/>'+ _("Items")+u' :'+productsmsg+u'<br/>'+_("Total price")+u" :"+unicode(total)+u'<br/>'
                splain += unicode(h.link_to(_("Click here to confirm the order."),((request.application_url)+url(controller='invoice',action='confirm',id=invoice.id))))
                c.usermsg = unicode(h.link_to(_("Click here to confirm the order."),((request.application_url)+url(controller='invoice',action='confirm',id=invoice.id))))
                salesHtml = render(_('/derived/emails/customer_invoice.html'))
                sales_messages=[]
                for email in session['site_settings']['invoicealertmail'].split(','):
                    sales_message=Message(session['site_settings']['invoicealertfrom'],
                                          email,
                                          _("User ")+user.user_name+_(" placed an order"),
                                          encoding='utf-8')
                    sales_message.plain=splain		
                    sales_message.rich = salesHtml
                    sales_messages.append(sales_message)
                try:			    
                    customer_message.send()		
                    for message in sales_messages:
                        message.send()		    
                except: 
                    Session.rollback()		
                    h.flash(_('For some technical reasons we are unable to accept orders online for now please contact us by phone.(SMTP Error)'))
                    redirect(url(controller='product', action='editbasket'))
                Session.commit()
                session['basket']={}
                session.save()
                h.flash(_('An email has beed sent to you with the detail of your purchase our staff will call you for further details'))		    
                redirect(url(controller='product', action='list'))
示例#2
0
                try:			    
                    customer_message.send()		
                    for message in sales_messages:
                        message.send()		    
                except: 
                    Session.rollback()		
                    h.flash(_('For some technical reasons we are unable to accept orders online for now please contact us by phone.(SMTP Error)'))
                    redirect(url(controller='product', action='editbasket'))
                Session.commit()
                session['basket']={}
                session.save()
                h.flash(_('An email has beed sent to you with the detail of your purchase our staff will call you for further details'))		    
                redirect(url(controller='product', action='list'))

        if action == _('Save'):
            schema = Editbasket()
            try:
                result = schema.to_python(dict(request.params), c)
            except Invalid, e:
                return render_editbasket(self.menu_items,values=values, errors=variabledecode.variable_encode(                    
                    e.unpack_errors() or {},
                    add_repetitions=False)
                                         )               
            else:  
                for i in range(len(result['quantityid'])):
                    session['basket'][result['quantityid'][i]] = result['quantity'][i]
                session.save()
                redirect(url(controller='product', action='list'))

    def search(self):
        came_from =  str(request.GET.get('came_from', 'list'))