-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.sql
More file actions
276 lines (264 loc) · 7.32 KB
/
views.sql
File metadata and controls
276 lines (264 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
USE cncrepair;
CREATE VIEW specific_items AS
(SELECT itemtypes.modelnum,
itemtypes.make,
0 AS assembly,
items.id,
specificassemblies.parent,
items.serial,
items.barcode,
cust_location.name AS atcust,
items.atcustomer,
items.status+0 AS status,
items.status AS txtstatus,
items.onhold,
items.itemtype_id,
items.rack,
items.shelf,
items.stock,
items.owner AS owner_id,
cust_owner.name AS owner,
DATE_FORMAT(items.lastseen, "%b %d, %Y %T") AS timeready,
items.readyfor,
items.priority+0 AS priority,
DATE_FORMAT(items.lastseen, "%b %d, %Y") AS lastseen
FROM (((items INNER JOIN itemtypes ON items.itemtype_id=itemtypes.id)
LEFT JOIN customers AS cust_owner ON items.owner=cust_owner.id)
LEFT JOIN customers AS cust_location ON items.atcustomer=cust_location.id)
LEFT JOIN specificassemblies ON items.id=specificassemblies.child)
UNION
(SELECT itemtypes.modelnum,
itemtypes.make,
1 AS assembly,
0 AS id,
phantomitems.id AS parent,
'' AS serial,
'' AS barcode,
'' AS atcust,
0 AS atcustomer,
'' AS status,
'' AS txtstatus,
'' AS onhold,
phantomitems.itemtype_id,
'' AS rack,
'' AS shelf,
'' AS stock,
'' AS owner_id,
'' AS owner,
'' AS timeready,
'' AS readyfor,
'' AS priority,
'' AS lastseen
FROM phantomitems INNER JOIN itemtypes ON phantomitems.itemtype_id=itemtypes.id)
ORDER BY parent ASC, assembly DESC, modelnum;
CREATE VIEW incoming AS
(SELECT quoteitems.id,
quoteitems.quote_id,
quoteitems.itemtype_id,
quoteitems.type,
quoteitems.quantity,
quoteitems.print,
quoteitems.description,
customers.name,
itemtypes.modelnum,
itemtypes.make
FROM ((quotes INNER JOIN quoteitems ON quotes.id=quoteitems.quote_id)
INNER JOIN customers ON quotes.billto=customers.id)
INNER JOIN itemtypes ON quoteitems.itemtype_id=itemtypes.id
WHERE TIMESTAMPDIFF(DAY, quotes.created, CURRENT_TIMESTAMP()) < 30)
UNION
(SELECT 0,
0,
itemtype_id,
'owed',
1,
'regular',
description,
name,
modelnum,
make
FROM owed_item_list);
CREATE VIEW quote_view AS
SELECT quoteitems.*,
quotes.billto,
customers.name,
quotes.phone_id,
quotes.email_id,
quotes.total,
quotes.created,
quotes.sent,
quotes.itemtotal,
quotes.notes,
quotes.publicnotes,
quotes.purchaseorder,
users.name AS createdby,
itemtypes.modelnum,
itemtypes.make,
itemtypes.details,
itemtypes.packing,
itemtypes.assembly,
itemtypes.repair,
itemtypes.repairrate,
itemtypes.exch,
itemtypes.exchrate,
itemtypes.sale,
itemtypes.salerate
FROM (((quotes INNER JOIN quoteitems ON quotes.id=quoteitems.quote_id)
INNER JOIN customers ON quotes.billto=customers.id)
LEFT JOIN users ON users.id=quotes.createdby)
INNER JOIN itemtypes ON quoteitems.itemtype_id=itemtypes.id;
CREATE VIEW invoice_view AS
SELECT invoiceitems.*,
invoices.billto,
customers.name,
invoices.phone_id,
invoices.email_id,
invoices.total,
invoices.created,
invoices.sent,
invoices.itemtotal,
invoices.notes,
invoices.publicnotes,
invoices.purchaseorder,
users.name AS createdby,
itemtypes.modelnum,
itemtypes.make,
itemtypes.details,
itemtypes.packing,
itemtypes.assembly,
itemtypes.repair,
itemtypes.repairrate,
itemtypes.exch,
itemtypes.exchrate,
itemtypes.sale,
itemtypes.salerate,
itemtypes.madein,
htscodes.hts
FROM ((((invoices INNER JOIN invoiceitems ON invoices.id=invoiceitems.inv_id)
INNER JOIN customers ON invoices.billto=customers.id)
LEFT JOIN users ON users.id=invoices.createdby)
INNER JOIN itemtypes ON invoiceitems.itemtype_id=itemtypes.id)
LEFT JOIN htscodes ON itemtypes.hts=htscodes.id;
CREATE VIEW history_log AS
SELECT histories.id,
histories.item_id,
items.itemtype_id,
iitems.inv_id,
histories.invoiceitem_id,
iitems.type AS service,
siitems.inv_id as ship_inv_id,
histories.ship_invoiceitem_id,
camefrom AS cust_from_id,
customers.name AS camefrom,
histories.shippedto,
DATE_FORMAT(histories.unpacked, "%b %d, %Y") AS unpacked,
DATE_FORMAT(histories.received, "%b %d, %Y %T") AS received,
DATE_FORMAT(histories.cleaned, "%b %d, %Y %T") AS cleaned,
DATE_FORMAT(histories.repaired, "%b %d, %Y %T") AS repaired,
DATE_FORMAT(histories.tested, "%b %d, %Y %T") AS tested,
histories.testedok,
DATE_FORMAT(histories.shipped, "%b %d, %Y") AS shipped,
histories.carrier,
histories.trackingnum
FROM (((histories INNER JOIN items ON histories.item_id=items.id)
LEFT JOIN customers ON histories.camefrom=customers.id)
LEFT JOIN invoiceitems AS iitems ON histories.invoiceitem_id=iitems.id)
LEFT JOIN invoiceitems AS siitems ON histories.ship_invoiceitem_id=siitems.id
ORDER BY histories.received DESC;
CREATE VIEW notes_view AS
SELECT notes.*,
users.name,
DATE_FORMAT(notes.time_noted, "%b %d, %Y") AS note_date,
histories.item_id
FROM (notes INNER JOIN users ON notes.user_id=users.id)
INNER JOIN histories ON notes.history_id=histories.id
WHERE histories.shipped = 0
ORDER BY notes.time_noted DESC;
CREATE VIEW assembly_view AS
SELECT assemblies.*,
itemtypes.*
FROM assemblies INNER JOIN itemtypes ON assemblies.child=itemtypes.id;
CREATE VIEW specificassembly_view AS
SELECT specificassemblies.*,
itemtypes.modelnum,
itemtypes.make,
itemtypes.assembly,
items.id,
items.serial,
items.barcode,
items.atcustomer,
items.status+0 AS status,
items.status AS txtstatus,
items.itemtype_id,
items.rack,
items.shelf,
items.stock,
items.owner AS owner_id,
DATE_FORMAT(items.lastseen, "%b %d, %Y %T") AS timeready,
items.readyfor,
items.priority+0 AS priority,
DATE_FORMAT(items.lastseen, "%b %d, %Y") AS lastseen
FROM (specificassemblies INNER JOIN items ON specificassemblies.child=items.id)
INNER JOIN itemtypes ON items.itemtype_id=itemtypes.id;
CREATE VIEW rec_inv_list AS
SELECT items.id AS item_id,
items.itemtype_id,
customers.name AS billto,
invoiceitems.id AS invoiceitem_id,
invoiceitems.inv_id,
invoiceitems.type,
DATE_FORMAT(invoices.created, "%b %d, %Y") AS created,
invoices.shipname,
(invoiceitems.quantity - invoiceitems.qtyreceived) AS qtyremaining
FROM ((items INNER JOIN invoiceitems ON items.itemtype_id=invoiceitems.itemtype_id)
INNER JOIN invoices ON invoiceitems.inv_id=invoices.id)
INNER JOIN customers ON invoices.billto=customers.id
ORDER BY invoices.created DESC;
CREATE VIEW ship_inv_list AS
SELECT items.id AS item_id,
items.itemtype_id,
customers.name AS billto,
invoiceitems.id AS invoiceitem_id,
invoiceitems.inv_id,
invoiceitems.type,
DATE_FORMAT(invoices.created, "%b %d, %Y") AS created,
invoices.shipname,
(invoiceitems.quantity - invoiceitems.shipped) AS qtyremaining
FROM ((items INNER JOIN invoiceitems ON items.itemtype_id=invoiceitems.itemtype_id)
INNER JOIN invoices ON invoiceitems.inv_id=invoices.id)
INNER JOIN customers ON invoices.billto=customers.id
ORDER BY invoices.created DESC;
CREATE VIEW owed_item_list AS
SELECT oweditems.*,
customers.name,
customers.id AS cust_id,
itemtypes.modelnum,
itemtypes.make,
itemtypes.description,
items.barcode,
DATE_FORMAT(TIMESTAMPADD(DAY, 15, oweditems.shipped), "%b %d, %Y") AS due_date
FROM (((oweditems INNER JOIN invoices ON oweditems.inv_id=invoices.id)
INNER JOIN customers ON invoices.billto=customers.id)
INNER JOIN itemtypes ON oweditems.itemtype_id=itemtypes.id)
LEFT JOIN items ON oweditems.item_sent=items.id;
CREATE VIEW quote_search AS
(SELECT DISTINCT quote_id AS refnum,
name,
created AS ts,
createdby,
"" AS purchaseorder,
DATE_FORMAT(created, "%b %d, %Y") AS created,
DATE_FORMAT(sent, "%b %d, %Y") AS sent,
total,
itemtotal,
"Quote" AS type FROM quote_view)
UNION
(SELECT DISTINCT inv_id AS refnum,
name,
createdby,
created AS ts, purchaseorder,
DATE_FORMAT(created, "%b %d, %Y") AS created,
DATE_FORMAT(sent, "%b %d, %Y") AS sent,
total,
itemtotal,
"Invoice" AS type FROM invoice_view);