Deleting all items of a selecte ID at the same time

66 views
Skip to first unread message

mostwanted

unread,
Jan 6, 2019, 7:31:31 AM1/6/19
to web2py-users
I have a view that create an invoice, at present i can only delete 1 item at a time from the invoice but what i want is to be able to delete all items from the invoice at the same time by clicking a link, below is my code.

INVOICE CODE:
<div id="pdf"><a href="javascript:genPDF()">Click to Download as PDF</a></div>
   
<div id="invoicing">
       
<div id="letterHead">
       
<span class="letterHead invoice">
 
<img src="{{=URL('static', 'images/logoicon.png')}}" width="100px" height="100px"/> <span style="font-weight: bold;">COMPANY</span><br />
            TELEPHONE
: ............<br />
            EM@IL: .............@gmail.com
<br />
            CELL: ............
<br />
            POSTAL-ADDRESS:..............
<br />
       
</span>
           
       
<span class="customerInv">Invoice No: ___________<br />
         Contact: {{=invoice.proxy}}
<br />
         Company: {{=invoice.company}}
<br />
            Tel: {{=invoice.tel}}
<br />
            Postal Address: {{=invoice.postalAddress}}
           
</span>
</div>
       
<hr />
<div>
<table>
<tr>
   
<th>Order</th>
   
<th>Quantity</th>
   
<th>Order Price(VAT Exclusive)</th>
   
<th>Amount</th>
</tr>
{{for invoice in customer:}}
   
<tr>
   
<td>{{=invoice.client_order}}</td>
   
<td>{{=invoice.quantity}}</td>
   
<td>{{=MoneyFormat(invoice.beforeVat)}}</td>
   
<td>{{=MoneyFormat(invoice.quantity*invoice.beforeVat)}}</td>
</tr>
    {{pass}}

    {{for price in pricing:
    try:
    vat=round(price.total*0.12, 2)
        totalPrice=price.total+vat
    except:
    redirect(URL('notInvoiced'))
        }}
    {{pass}}
   
<tr>
       
<td style="font-weight: bold; font-size: 14px; color: blue;" width: 29px;>Sub-Total:</td><td style="font-weight: bold; font-size: 14px; color: blue;" width: 29px>{{=MoneyFormat(price.total)}}</td><td style="font-weight: bold; font-size: 14px; color: black;" width: 29px;>(VAT EXCLUSIVE)</td>
   
</tr>
   
<tr>
       
<td style="font-weight: bold; font-size: 14px; color: red;" width: 29px;>12% VAT: </td><td style="font-weight: bold; font-size: 14px; color: red;" width: 29px>{{=MoneyFormat(vat)}}</td>
   
</tr>
   
<tr>
       
<td style="font-weight: bold; font-size: 14px; color: green;" width: 29px;>Total: </td><td style="font-weight: bold; font-size: 14px; color: green;" width: 29px>{{=MoneyFormat(totalPrice)}}</td><td style="font-weight: bold; font-size: 14px; color: black;" width: 29px;>(VAT INCLUSIVE)</td>
   
</tr>
    {{pass}}
    {{pass}}
</table>
   
<br />
       
<div style="font-weight: bold;">
    Client Sinature: ____________________
<br />
    {{=invoice.Booked_By.first_name}} {{=invoice.Booked_By.last_name}}
<br />
    Date & Time: {{=invoice.Booking_Date}}
<br />
   
</div>
   
<center><span style="font-weight: bold; font-style: italic;">
    Thank you for your business!
<br />
    Company
   
</span></center>
       
</div>
</div>
<script>
    $
('document').ready(function(){
    $
(".delete").click(function(){
       
return confirm('Are you sure you want to delete this this INVOICE?!');
   
});
   
});
</script>

#BY CLICKING THE LINK BELOW I WANT TO BE ABLE TO DELETE ALL THE INVOICE ITEMS AT ONCE
<div id="delete"><a class="delete" href="{{=URL('deleteInvoice', args=invoice.id)}}">Delete Invoice</a></div>

<br />
<div id="editor"></div>
<button onclick="topFunction()" id="myBtn" title="Go to top">TOP</button>


INVOICE DELETING CONTROLLER
def deleteInvoice():
#THE .first() FUNCTION IN THE LINE BELOW ONLY ALLOWS ME TO DELETE ITEMS ONE AT A TIME
    query = db(db.invoice.id==request.args(0, cast=int)).select().first()
    remove
= db(db.invoice.id==query).delete()
   
if remove:
        redirect
(URL('registeredClients'))
   
return locals()

The .first() function in the green highlighted line in the above controller only allows me to delete the invoice items one at a time, is there another function i can use that will allow me to delete them all at once? By deleteng the invoice items this will delete the entire invoice allowing the user to implement a new invoice.

Please help!

Mostwanted

Anthony

unread,
Jan 6, 2019, 9:03:53 AM1/6/19
to web2py-users
INVOICE DELETING CONTROLLER
def deleteInvoice():
#THE .first() FUNCTION IN THE LINE BELOW ONLY ALLOWS ME TO DELETE ITEMS ONE AT A TIME
    query = db(db.invoice.id==request.args(0, cast=int)).select().first()
    remove
= db(db.invoice.id==query).delete()

Above, query is the entire Row object of a single invoice -- it is not a query nor an ID and does not make sense to use it as you are in the second line. Further, you already have the invoice ID in request.args(0), so there is no reason to query the DB for that record just to get its ID again.

As for "deleting all the invoice items", what do you mean by that? Do you want to delete all invoices in the db.invoice table? Or is there a separate table of invoice items that references the db.invoice table?

Anthony

mostwanted

unread,
Jan 6, 2019, 9:15:41 AM1/6/19
to web2py-users
Hey Anthony, thanks for the heads up in the garbage code in the controller i will address it, with regards to deleting invoices i want to be able to delete all invoices in the db.invoice table but only for a specific selected client. I want to be able to delete an entire invoice by clicking the link once.

Anthony

unread,
Jan 6, 2019, 9:35:36 AM1/6/19
to web2py-users
On Sunday, January 6, 2019 at 9:15:41 AM UTC-5, mostwanted wrote:
Hey Anthony, thanks for the heads up in the garbage code in the controller i will address it, with regards to deleting invoices i want to be able to delete all invoices in the db.invoice table but only for a specific selected client. I want to be able to delete an entire invoice by clicking the link once.

Then you would need a query like:

db(db.invoice.client = some_client_id).delete()

You would want to make sure the current user is authorized to delete invoices for some_client_id (specifically, if some_client_id comes from the browser, be sure to validate it).

Also, in the UI, you should probably make it clear what will happen, as "Delete Invoice" makes me think I'm deleting only the current invoice, not all invoices.

Anthony

mostwanted

unread,
Jan 7, 2019, 2:06:43 AM1/7/19
to web2py-users
My delete function is not working, I think I am doing doing something wrong but i'm not sure where, I thought the code below would get me home but nothing is happening, nothing is getting deleted! Please advice:

DELETE FUNCTION CONTROLLER CODE:
def deleteInvoice():
    query
=db.invoice(request.args(0, cast=int))
    remove
=db(db.invoice.customer==query.id).delete()

   
if remove:
        redirect
(URL('registeredClients'))
   
return locals()

Anthony

unread,
Jan 7, 2019, 5:05:15 PM1/7/19
to web2py-users
On Monday, January 7, 2019 at 2:06:43 AM UTC-5, mostwanted wrote:
My delete function is not working, I think I am doing doing something wrong but i'm not sure where, I thought the code below would get me home but nothing is happening, nothing is getting deleted! Please advice:

DELETE FUNCTION CONTROLLER CODE:
def deleteInvoice():
    query
=db.invoice(request.args(0, cast=int))
    remove
=db(db.invoice.customer==query.id).delete()

Again, the first line serves no purpose, as it is simply used to retrieve the ID that you already have. Just do:

    if db(db.invoice.customer == request.args(0, cast=int)).delete():
        redirect
(URL('registeredClients'))

If that's not working, presumably request.args(0) is not the value of any "customer" in the db.invoice table, so your bug lies in the code that produces the URL that leads here.

Also, it doesn't look like your code does any validation to determine if the user is allowed to delete all of these customer invoices. You should probably confirm that the customer ID submitted is the customer ID of the current user (you could use a digitally signed URL, a session variable, or do a lookup).

Anthony

mostwanted

unread,
Jan 8, 2019, 12:44:48 AM1/8/19
to web...@googlegroups.com
With regards to validation considering the scope of the app i'm working on, i will be using @auth.requires_membership() to control and restrict who does what but thanks for bringing up digitally signed URL, i will be looking extensively in to that, its an interesting topic.

I have changed my code, i have been specific with my ID:
if db(db.invoice.id == request.args(0, cast=int)).delete():
          redirect
(URL('registeredClients'))
It does the deleting but the thing is it deletes the items that appear in my invoice one at a time, if i have 10 items i have to keep clicking the delete link 10 times until the invoice is no more but it would be great if i could be able to erase all the 10 items by clicking once, more like if i were using this
if db(db.invoice.id > 0).delete():
          redirect
(URL('registeredClients'))
 but only specific to only one invoice for the selected client and not erasing everything in the database

mostwanted

unread,
Jan 18, 2020, 4:41:19 PM1/18/20
to web2py-users
Finally i was able to solve this problem! I used a session to carry forward the details of the company whose quotations I wanted to delete. It is finally working as I wanted. Thanks again Anthony, you previously hinted it but it didnt get it back then, when i went back to the problem a year later well rested I understood what you meant.

def View_Invoice():
   
ref=session.ref2
    invoiceDetails
=db.Client_Details(request.args(0, cast=int))
    session.detailsInvoice=invoiceDetails
    resCompany
=db(db.resident_company).select()
    customer
=db(db.invoice.customer==invoiceDetails.id).select(db.invoice.ALL)
    pricing
=db(db.invoice.customer==invoiceDetails.id).select(db.invoice.totalPrice.sum().with_alias('total'))
   
return locals()

def deleteInvoice():
    if db(db.invoice.customer == session.detailsInvoice).delete():

          redirect
(URL('registeredClients'))
   
return locals()

Regards;
Mostwanted
Reply all
Reply to author
Forward
0 new messages