Skip to content

Commit 1ff461f

Browse files
committed
10 Feb 2024
1) Added customer id as foreign key into deliveries table. 2) Delivery Order Form - "Customer" is the company itself. "Deliver To" is whoever/whichever department receiving the order. 3) Delivery Order & QR report pages - Auto print on load.
1 parent e144909 commit 1ff461f

File tree

8 files changed

+130
-85
lines changed

8 files changed

+130
-85
lines changed

TODO.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ STORAGE BOXX TO DO / WISH LIST
1414
* LIB-Items > function save > also update PO items.
1515
* More reports - delivery/sales, purchases.
1616
* Print QR/NFC after import items
17-
* Window.print() > QR and DO
1817
* Add tutorial videos.

assets/PAGE-deliver.js

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,26 @@ var dlv = {
4747
}
4848

4949
// (D3) ATTACH CUSTOMER AUTOCOMPLETE
50-
autocomplete.attach({
51-
target : document.getElementById("d-name"),
52-
mod : "autocomplete", act : "cus",
53-
data : { more : 1 },
54-
onpick : cus => {
55-
document.getElementById("d-name").value = cus.n;
56-
cus.v = JSON.parse(cus.v);
57-
[
58-
["d-tel", "t"],
59-
["d-email", "e"],
60-
["d-address", "a"],
61-
].forEach(r => {
62-
let field = document.getElementById(r[0]);
63-
if (field.value=="") { field.value = cus.v[r[1]]; }
64-
});
65-
}
66-
});
50+
var hcusname = document.getElementById("cus-name");
51+
if (!hcusname.disabled) {
52+
autocomplete.attach({
53+
target : hcusname,
54+
mod : "autocomplete", act : "cus",
55+
data : { more : 1 },
56+
onpick : cus => {
57+
dlv.ccus(false);
58+
hcusname.value = cus.n;
59+
cus.v = JSON.parse(cus.v);
60+
[
61+
["cus-id", "i"],
62+
["d-name", "n"],
63+
["d-tel", "t"],
64+
["d-email", "e"],
65+
["d-address", "a"],
66+
].forEach(r => document.getElementById(r[0]).value = cus.v[r[1]]);
67+
}
68+
});
69+
}
6770

6871
// (D4) ATTACH ADD ITEM AUTOCOMPLETE
6972
autocomplete.attach({
@@ -88,16 +91,34 @@ var dlv = {
8891
}
8992
}),
9093

91-
// (E) ADD ITEM ROW
94+
// (E) TOGGLE CUSTOMER CHANGE
95+
ccus : reset => {
96+
// (E1) GET HTML ELEMENTS
97+
let n = document.getElementById("cus-name"),
98+
i = document.getElementById("cus-id"),
99+
c = document.getElementById("cus-change");
100+
// @TODO
101+
if (reset) {
102+
n.value = "";
103+
n.disabled = false;
104+
i.value = "";
105+
c.classList.add("d-none");
106+
} else {
107+
n.disabled = true;
108+
c.classList.remove("d-none");
109+
}
110+
},
111+
112+
// (F) ADD ITEM ROW
92113
addItem : (sku, name, unit, price, qty) => {
93-
// (E1) CHECK DUPLICATE ITEM
114+
// (F1) CHECK DUPLICATE ITEM
94115
if (dlv.iList[sku]) {
95116
cb.modal("Already Added", `[${sku}] ${name} is already added.`);
96117
}
97118

98-
// (E2) ADD NEW ROW
119+
// (F2) ADD NEW ROW
99120
else {
100-
// (E2-1) ITEM ROW HTML
121+
// (F2-1) ITEM ROW HTML
101122
let row = document.createElement("div");
102123
row.className = "iRow d-flex align-items-center border p-2";
103124
row.innerHTML =
@@ -115,30 +136,30 @@ var dlv = {
115136
<label>PRICE</label>
116137
</div>`;
117138

118-
// (E2-2) SORTABLE
139+
// (F2-2) SORTABLE
119140
row.draggable = true;
120141
row.ondragstart = () => dlv.ddfrom = row;
121142
row.ondragover = e => e.preventDefault();
122143
row.ondrop = dlv.isort;
123144

124-
// (E2-3) APPEND TO LIST
145+
// (F2-3) APPEND TO LIST
125146
document.getElementById("dlv-items").appendChild(row);
126147
dlv.iList[sku] = 1;
127148
}
128149
},
129150

130-
// (F) DRAG-N-DROP SORT ITEM
151+
// (G) DRAG-N-DROP SORT ITEM
131152
ddfrom : null, // current item being dragged
132153
ddto : null, // dropped at this item
133154
ddget : r => r.classList.contains("iRow") ? r : dlv.ddget(r.parentElement), // get proper drop target
134155
isort : e => {
135-
// (F1) GET ELEMENTS
156+
// (G1) GET ELEMENTS
136157
e.preventDefault();
137158
let iList = document.getElementById("dlv-items"),
138159
iAll = iList.querySelectorAll(".iRow");
139160
dlv.ddto = dlv.ddget(e.target);
140161

141-
// (F2) REORDER ITEM
162+
// (G2) REORDER ITEM
142163
if (iAll.length>1 && dlv.ddfrom!=dlv.ddto) {
143164
let currentpos = 0, droppedpos = 0;
144165
for (let i=0; i<iAll.length; i++) {
@@ -153,55 +174,60 @@ var dlv = {
153174
}
154175
},
155176

156-
// (G) REMOVE ITEM ROW
177+
// (H) REMOVE ITEM ROW
157178
delItem : (row, sku) => {
158179
row.parentElement.remove();
159180
delete dlv.iList[sku];
160181
},
161182

162-
// (H) GET ITEM FROM SERVER & ADD TO LIST
183+
// (I) GET ITEM FROM SERVER & ADD TO LIST
163184
addGet : sku => cb.api({
164185
mod : "items", act : "get",
165186
data : { sku : sku },
166187
passmsg : false,
167188
onpass : res => {
168-
// (H1) INVALID SKU
189+
// (I1) INVALID SKU
169190
if (res.data==null) {
170191
cb.modal("Invalid Item", `${sku} is not found in the database.`);
171192
}
172193

173-
// (H2) OK - ADD ITEM
194+
// (I2) OK - ADD ITEM
174195
else {
175196
let i = res.data;
176197
dlv.addItem(i.item_sku, i.item_name, i.item_unit, i.item_price, 1);
177198
}
178199
}
179200
}),
180201

181-
// (I) ADD ITEM WITH QR CODE
202+
// (J) ADD ITEM WITH QR CODE
182203
addQR : () => {
183204
if (qrscan.scanner==null) { qrscan.init(dlv.addGet); }
184205
qrscan.show();
185206
},
186207

187-
// (J) SAVE DELIVERY
208+
// (K) SAVE DELIVERY
188209
save : () => {
189-
// (J1) GET DATA
210+
// (K1) GET DATA
190211
var data = {
212+
cid : document.getElementById("cus-id").value,
191213
name : document.getElementById("d-name").value,
192214
tel : document.getElementById("d-tel").value,
193215
email : document.getElementById("d-email").value,
194216
address : document.getElementById("d-address").value,
195217
date : document.getElementById("d-date").value,
196218
notes : document.getElementById("d-notes").value
197219
};
220+
if (data.cid=="") {
221+
cb.modal("No customer specified", "Please select a customer.");
222+
return false;
223+
}
198224
var id = document.getElementById("d-id").value;
199225
if (id!="") {
200226
data.id = id;
201227
data.stat = document.getElementById("d-stat").value;
202228
}
203229

204-
// (J2) GET ITEMS
230+
// (K2) GET ITEMS
205231
let items = document.querySelectorAll("#dlv-items .iRow");
206232
if (items.length==0) {
207233
cb.modal("No Items", "Please add at least one item.");
@@ -218,7 +244,7 @@ var dlv = {
218244
}
219245
data.items = JSON.stringify(data.items);
220246

221-
// (J3) AJAX
247+
// (K3) AJAX
222248
cb.api({
223249
mod : "delivery", act : "save",
224250
data : data,
@@ -228,22 +254,22 @@ var dlv = {
228254
return false;
229255
},
230256

231-
// (K) PRINT DELIVERY ORDER
257+
// (L) PRINT DELIVERY ORDER
232258
print : id => {
233259
document.getElementById("dlv-print-id").value = id;
234260
document.getElementById("dlv-print").submit();
235261
}
236262
};
237263

238-
// (L) INIT MANAGE DELIVERIES
264+
// (M) INIT MANAGE DELIVERIES
239265
window.addEventListener("load", () => {
240-
// (L1) EXTRA STYLES FOR "ADD/EDIT ITEMS LIST"
266+
// (M1) EXTRA STYLES FOR "ADD/EDIT ITEMS LIST"
241267
document.head.appendChild(document.createElement("style")).innerHTML=".iQty,.iPrice{width:80px}";
242268

243-
// (L2) LIST DELIVERIES
269+
// (M2) LIST DELIVERIES
244270
dlv.list();
245271

246-
// (L3) ATTACH AUTOCOMPLETE
272+
// (M3) ATTACH AUTOCOMPLETE
247273
autocomplete.attach({
248274
target : document.getElementById("dlv-search"),
249275
mod : "autocomplete", act : "deliver",

lib/LIB-Delivery.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
class Delivery extends Core {
33
// (A) ADD OR UPDATE DELIVERY ORDER
44
// * ADAPTS USER FROM SESSION!
5+
// $cid : customer id
56
// $name : customer name
67
// $tel : customer tel
78
// $email : customer email
@@ -11,16 +12,16 @@ class Delivery extends Core {
1112
// $note : delivery notes
1213
// $stat : delivery status
1314
// $id : delivery id, update only
14-
function save ($name, $tel, $email, $address, $date, $items, $notes=null, $stat=0, $id=null) {
15+
function save ($cid, $name, $tel, $email, $address, $date, $items, $notes=null, $stat=0, $id=null) {
1516
// (A1) START & DATA
1617
$this->DB->start();
17-
$data = [$name, $tel, $email, $address, $notes, $date];
18+
$data = [$cid, $name, $tel, $email, $address, $notes, $date];
1819

1920
// (A2) NEW DELIVERY ORDER
2021
if ($id==null) {
2122
$data[] = 0;
2223
$this->DB->insert("deliveries",
23-
["d_name", "d_tel", "d_email", "d_address", "d_notes", "d_date", "d_status"],
24+
["cus_id", "d_name", "d_tel", "d_email", "d_address", "d_notes", "d_date", "d_status"],
2425
$data
2526
);
2627
$id = $this->DB->lastID;
@@ -31,7 +32,7 @@ function save ($name, $tel, $email, $address, $date, $items, $notes=null, $stat=
3132
$data[] = $stat;
3233
$data[] = $id;
3334
$this->DB->update("deliveries",
34-
["d_name", "d_tel", "d_email", "d_address", "d_notes", "d_date", "d_status"],
35+
["cus_id", "d_name", "d_tel", "d_email", "d_address", "d_notes", "d_date", "d_status"],
3536
"`d_id`=?", $data
3637
);
3738
$this->DB->delete("deliveries_items", "`d_id`=?", [$id]);
@@ -92,10 +93,13 @@ function save ($name, $tel, $email, $address, $date, $items, $notes=null, $stat=
9293
function get ($id) {
9394
// (B1) MAIN ORDER
9495
$d = $this->DB->fetch(
95-
"SELECT * FROM `deliveries` WHERE `d_id`=?", [$id]
96+
"SELECT d.*, c.`cus_name`
97+
FROM `deliveries` d
98+
LEFT JOIN `customers` c USING (`cus_id`)
99+
WHERE `d_id`=?", [$id]
96100
);
97101
if (!is_array($d)) {
98-
$this->error = "Invalid delivery";
102+
$this->error = "Invalid delivery order";
99103
return false;
100104
}
101105

@@ -116,12 +120,12 @@ function get ($id) {
116120
return $d;
117121
}
118122

119-
// (C) GET ALL OR SEARCH DELIVERIES
123+
// (C) GET ALL OR SEARCH DELIVERY ORDERS
120124
// $search : optional, customer name
121125
// $page : optional, current page number
122126
function getAll ($search=null, $page=null) {
123127
// (C1) PARITAL DELIVERIES SQL + DATA
124-
$sql = "FROM `deliveries`";
128+
$sql = "FROM `deliveries` d LEFT JOIN `customers` c USING (`cus_id`)";
125129
$data = null;
126130
if ($search != null) {
127131
$sql .= " WHERE `d_name` LIKE ?";
@@ -137,6 +141,6 @@ function getAll ($search=null, $page=null) {
137141
}
138142

139143
// (C3) RESULTS
140-
return $this->DB->fetchAll("SELECT * $sql", $data, "d_id");
144+
return $this->DB->fetchAll("SELECT d.*, c.`cus_name` $sql", $data, "d_id");
141145
}
142146
}

lib/SQL-Storage-Boxx-0.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ ALTER TABLE `customers`
149149
-- (J) DELIVERIES
150150
CREATE TABLE `deliveries` (
151151
`d_id` bigint(20) NOT NULL,
152+
`cus_id` bigint(20) NOT NULL,
152153
`d_name` varchar(255) NOT NULL,
153154
`d_tel` varchar(32) NOT NULL,
154155
`d_email` varchar(255) NOT NULL,
@@ -160,6 +161,7 @@ CREATE TABLE `deliveries` (
160161

161162
ALTER TABLE `deliveries`
162163
ADD PRIMARY KEY (`d_id`),
164+
ADD KEY `cus_id` (`cus_id`),
163165
ADD KEY `d_name` (`d_name`);
164166

165167
ALTER TABLE `deliveries`

0 commit comments

Comments
 (0)