Anand Doshi | f7e5648 | 2013-08-29 17:46:40 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
| 6 | from webnotes.utils import cint, formatdate |
| 7 | import json |
| 8 | |
| 9 | def get_transaction_list(doctype, start): |
| 10 | # find customer id |
| 11 | customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, |
| 12 | "customer") |
| 13 | |
| 14 | if customer: |
| 15 | transactions = webnotes.conn.sql("""select name, creation, currency, grand_total_export |
| 16 | from `tab%s` where customer=%s and docstatus=1 |
| 17 | order by creation desc |
Anand Doshi | 6c8ef77 | 2013-08-30 18:23:50 +0530 | [diff] [blame] | 18 | limit %s, 20""" % (doctype, "%s", "%s"), (customer, cint(start)), as_dict=True) |
Anand Doshi | f7e5648 | 2013-08-29 17:46:40 +0530 | [diff] [blame] | 19 | for doc in transactions: |
| 20 | doc.items = ", ".join(webnotes.conn.sql_list("""select item_name |
| 21 | from `tab%s Item` where parent=%s limit 5""" % (doctype, "%s"), doc.name)) |
| 22 | doc.creation = formatdate(doc.creation) |
| 23 | return transactions |
| 24 | else: |
| 25 | return [] |
| 26 | |
| 27 | def get_common_args(): |
| 28 | return { |
| 29 | "global_number_format": webnotes.conn.get_default("number_format") or "#,###.##", |
| 30 | "currency": webnotes.conn.get_default("currency"), |
| 31 | "currency_symbols": json.dumps(dict(webnotes.conn.sql("""select name, symbol |
| 32 | from tabCurrency where ifnull(enabled,0)=1"""))) |
| 33 | } |
| 34 | |
| 35 | @webnotes.whitelist() |
| 36 | def get_orders(start=0): |
| 37 | return get_transaction_list("Sales Order", start) |
| 38 | |
| 39 | def order_list_args(): |
| 40 | args = get_common_args() |
| 41 | args.update({ |
| 42 | "title": "My Orders", |
| 43 | "method": "website.helpers.transaction.get_orders", |
| 44 | "icon": "icon-list", |
| 45 | "empty_list_message": "No Orders Yet", |
| 46 | "page": "order", |
| 47 | }) |
| 48 | return args |
| 49 | |
| 50 | @webnotes.whitelist() |
| 51 | def get_invoices(start=0): |
| 52 | return get_transaction_list("Sales Invoice", start) |
| 53 | |
| 54 | def invoice_list_args(): |
| 55 | args = get_common_args() |
| 56 | args.update({ |
| 57 | "title": "Invoices", |
| 58 | "method": "website.helpers.transaction.get_invoices", |
| 59 | "icon": "icon-file-text", |
| 60 | "empty_list_message": "No Invoices Found", |
| 61 | "page": "invoice" |
| 62 | }) |
Anand Doshi | 6c8ef77 | 2013-08-30 18:23:50 +0530 | [diff] [blame] | 63 | return args |
| 64 | |
| 65 | @webnotes.whitelist() |
| 66 | def get_shipments(start=0): |
| 67 | return get_transaction_list("Delivery Note", start) |
| 68 | |
| 69 | def shipment_list_args(): |
| 70 | args = get_common_args() |
| 71 | args.update({ |
| 72 | "title": "Shipments", |
| 73 | "method": "website.helpers.transaction.get_shipments", |
| 74 | "icon": "icon-truck", |
| 75 | "empty_list_message": "No Shipments Found", |
| 76 | "page": "shipment" |
| 77 | }) |
| 78 | return args |
| 79 | |
| 80 | @webnotes.whitelist() |
| 81 | def get_tickets(start=0): |
| 82 | tickets = webnotes.conn.sql("""select name, subject, status, creation |
| 83 | from `tabSupport Ticket` where raised_by=%s |
| 84 | order by modified desc |
| 85 | limit %s, 20""", (webnotes.session.user, cint(start)), as_dict=True) |
| 86 | for t in tickets: |
| 87 | t.creation = formatdate(t.creation) |
| 88 | |
| 89 | return tickets |
| 90 | |
| 91 | def ticket_list_args(): |
| 92 | return { |
| 93 | "title": "My Tickets", |
| 94 | "method": "website.helpers.transaction.get_tickets", |
| 95 | "icon": "icon-ticket", |
| 96 | "empty_list_message": "No Tickets Raised", |
| 97 | "page": "ticket" |
| 98 | } |
Anand Doshi | d5c18d4 | 2013-09-04 15:07:00 +0530 | [diff] [blame] | 99 | |
| 100 | def get_transaction_args(doctype, name): |
| 101 | customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, |
| 102 | "customer") |
| 103 | |
| 104 | bean = webnotes.bean(doctype, name) |
| 105 | if bean.doc.customer != customer: |
| 106 | return { |
| 107 | "doc": {"name": "Not Allowed"} |
| 108 | } |
| 109 | else: |
| 110 | return { |
| 111 | "doc": bean.doc, |
| 112 | "doclist": bean.doclist, |
| 113 | "webnotes": webnotes, |
| 114 | "utils": webnotes.utils |
| 115 | } |
| 116 | |
| 117 | def get_order_args(): |
Anand Doshi | 85ef502 | 2013-09-04 16:46:08 +0530 | [diff] [blame] | 118 | args = get_transaction_args("Sales Order", webnotes.form_dict.name) |
| 119 | args.update({ |
| 120 | "parent_link": "orders", |
| 121 | "parent_title": "My Orders" |
| 122 | }) |
| 123 | return args |
Anand Doshi | d5c18d4 | 2013-09-04 15:07:00 +0530 | [diff] [blame] | 124 | |
| 125 | def get_invoice_args(): |
Anand Doshi | 85ef502 | 2013-09-04 16:46:08 +0530 | [diff] [blame] | 126 | args = get_transaction_args("Sales Invoice", webnotes.form_dict.name) |
| 127 | args.update({ |
| 128 | "parent_link": "invoices", |
| 129 | "parent_title": "Invoices" |
| 130 | }) |
| 131 | return args |
Anand Doshi | d5c18d4 | 2013-09-04 15:07:00 +0530 | [diff] [blame] | 132 | |
| 133 | def get_shipment_args(): |
Anand Doshi | 85ef502 | 2013-09-04 16:46:08 +0530 | [diff] [blame] | 134 | args = get_transaction_args("Delivery Note", webnotes.form_dict.name) |
| 135 | args.update({ |
| 136 | "parent_link": "shipments", |
| 137 | "parent_title": "Shipments" |
| 138 | }) |
| 139 | return args |