| 1 |
# -*- coding: utf-8 -*- |
|---|
| 2 |
|
|---|
| 3 |
from base import Base |
|---|
| 4 |
from productlines import ProdCust_Cust |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
ACCOUNT_PROXY_STRING = "< automatic >" |
|---|
| 8 |
|
|---|
| 9 |
DEFAULT_CUSTOMER_NAME = "< New Customer >" |
|---|
| 10 |
DEFAULT_TERMS = "Net 10" |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
class Customers(Base): |
|---|
| 14 |
def initProperties(self): |
|---|
| 15 |
self.super() |
|---|
| 16 |
self._module_id = self.Application.constants.modules.MODULE_ID_CUSTOMERS |
|---|
| 17 |
self.Caption = "Customers" |
|---|
| 18 |
self.DataSource = "customers" |
|---|
| 19 |
self.DataStructure = ( |
|---|
| 20 |
# (field_alias, field_type, pk, table_name, field_name, field_scale) |
|---|
| 21 |
("id", "C", True, "customers", "id"), |
|---|
| 22 |
("account", "C", False, "customers", "account"), |
|---|
| 23 |
("name", "C", False, "customers", "name"), |
|---|
| 24 |
("attention", "C", False, "customers", "attention"), |
|---|
| 25 |
("address1", "C", False, "customers", "address1"), |
|---|
| 26 |
("address2", "C", False, "customers", "address2"), |
|---|
| 27 |
("address3", "C", False, "customers", "address3"), |
|---|
| 28 |
("city", "C", False, "customers", "city"), |
|---|
| 29 |
("state", "C", False, "customers", "state"), |
|---|
| 30 |
("zip", "C", False, "customers", "zip"), |
|---|
| 31 |
("country", "C", False, "customers", "country"), |
|---|
| 32 |
("phone", "C", False, "customers", "phone"), |
|---|
| 33 |
("fax", "C", False, "customers", "fax"), |
|---|
| 34 |
("cell", "C", False, "customers", "cell"), |
|---|
| 35 |
("url", "C", False, "customers", "url"), |
|---|
| 36 |
("email", "C", False, "customers", "email"), |
|---|
| 37 |
("terms", "C", False, "customers", "terms"), |
|---|
| 38 |
("tax_id", "C", False, "customers", "tax_id"), |
|---|
| 39 |
("notes", "M", False, "customers", "notes"), |
|---|
| 40 |
) |
|---|
| 41 |
|
|---|
| 42 |
self.DefaultValues["account"] = ACCOUNT_PROXY_STRING |
|---|
| 43 |
self.DefaultValues["name"] = DEFAULT_CUSTOMER_NAME |
|---|
| 44 |
self.DefaultValues["terms"] = DEFAULT_TERMS |
|---|
| 45 |
self.KeyField = "id" |
|---|
| 46 |
|
|---|
| 47 |
self.bizProdCust = ProdCust_Cust(self.Application.dbConnection) |
|---|
| 48 |
self.addChild(self.bizProdCust) |
|---|
| 49 |
|
|---|
| 50 |
if self.Application.showCustomerCategories(): |
|---|
| 51 |
self.bizCustomerCategory = CustomerCategory(self.Application.dbConnection) |
|---|
| 52 |
self.addChild(self.bizCustomerCategory) |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
def getNextAccountNumber(self): |
|---|
| 56 |
return str(self.Application.getNextNumber("customers.account")) |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
def setAccount(self, account_number): |
|---|
| 60 |
self.Record.account = account_number |
|---|
| 61 |
|
|---|
| 62 |
def afterNew(self): |
|---|
| 63 |
self.bizProdCust.new() |
|---|
| 64 |
self.bizProdCust.Record.prod_id = self.Application.constants.DEFAULT_PRODUCT_LINE_ID |
|---|
| 65 |
self.bizProdCust.fillDerivedFields() |
|---|
| 66 |
|
|---|
| 67 |
def onSaveNew(self): |
|---|
| 68 |
if self.Record.account == ACCOUNT_PROXY_STRING: |
|---|
| 69 |
self.Record.account = self.getNextAccountNumber() |
|---|
| 70 |
self.save() |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
def setBaseSQL(self): |
|---|
| 74 |
self.addFrom("customers") |
|---|
| 75 |
self.addFieldsFromDataStructure() |
|---|
| 76 |
self.addGroupBy("customers.id") |
|---|
| 77 |
self.addOrderBy("customers.name") |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
class CustomerCategories(Base): |
|---|
| 81 |
def initProperties(self): |
|---|
| 82 |
self.super() |
|---|
| 83 |
self._module_id = self.Application.constants.modules.MODULE_ID_CUSTOMER_CATEGORIES |
|---|
| 84 |
self.Caption = "Customer Categories" |
|---|
| 85 |
self.DataSource = "customer_categories" |
|---|
| 86 |
self.DataStructure = ( |
|---|
| 87 |
# (field_alias, field_type, pk, table_name, field_name, field_scale) |
|---|
| 88 |
("id", "C", True, "customer_categories", "id"), |
|---|
| 89 |
("name", "C", False, "customer_categories", "name"), |
|---|
| 90 |
("notes", "M", False, "customer_categories", "notes"),) |
|---|
| 91 |
self.KeyField = "id" |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
def setBaseSQL(self): |
|---|
| 95 |
self.addFrom("customer_categories") |
|---|
| 96 |
self.addFieldsFromDataStructure() |
|---|
| 97 |
self.addGroupBy("customer_categories.id") |
|---|
| 98 |
self.addOrderBy("customer_categories.name") |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
class CustomerCategory(Base): |
|---|
| 103 |
def initProperties(self): |
|---|
| 104 |
self.super() |
|---|
| 105 |
self.Caption = "Customer Category" |
|---|
| 106 |
self.DataSource = "customer_category" |
|---|
| 107 |
self.DataStructure = ( |
|---|
| 108 |
# (field_alias, field_type, pk, table_name, field_name, field_scale) |
|---|
| 109 |
("id", "C", True, "customer_category", "id"), |
|---|
| 110 |
("customer_id", "C", False, "customer_category", "customer_id"), |
|---|
| 111 |
("category_id", "C", False, "customer_category", "category_id"), |
|---|
| 112 |
("category_name", "C", False, "customer_categories", "name")) |
|---|
| 113 |
self.KeyField = "id" |
|---|
| 114 |
self.LinkField = "customer_id" |
|---|
| 115 |
self.FillLinkFromParent = True |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
def setBaseSQL(self): |
|---|
| 119 |
self.super() |
|---|
| 120 |
self.setFromClause("""customer_category |
|---|
| 121 |
left join customer_categories |
|---|
| 122 |
on customer_categories.id = customer_category.category_id""") |
|---|
| 123 |
self.addFieldsFromDataStructure() |
|---|
| 124 |
self.addGroupBy("customer_category.id") |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
def addLink(self, category_id): |
|---|
| 128 |
ds = self.getDataSet() |
|---|
| 129 |
if ds: |
|---|
| 130 |
results = ds.execute(""" |
|---|
| 131 |
select count(*) as exist_count |
|---|
| 132 |
from dataset |
|---|
| 133 |
where customer_id = '%s' |
|---|
| 134 |
and category_id = '%s' |
|---|
| 135 |
""" % (self.Parent.getPK(), category_id)) |
|---|
| 136 |
|
|---|
| 137 |
if results and results[0]["exist_count"] > 0: |
|---|
| 138 |
# already linked: ignore |
|---|
| 139 |
return |
|---|
| 140 |
results = self.executeSafe(""" |
|---|
| 141 |
select name |
|---|
| 142 |
from customer_categories |
|---|
| 143 |
where id = "%s" """ % category_id) |
|---|
| 144 |
try: |
|---|
| 145 |
category_name = results.Record.name |
|---|
| 146 |
except: |
|---|
| 147 |
category_name = category_id |
|---|
| 148 |
self.new() |
|---|
| 149 |
self.Record.customer_id = self.Parent.getPK() |
|---|
| 150 |
self.Record.category_id = category_id |
|---|
| 151 |
self.Record.category_name = category_name |
|---|