Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
haiyen-payment
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Giang Tran
haiyen-payment
Commits
57a8a53f
Commit
57a8a53f
authored
Aug 31, 2026
by
tdgiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tax-aware random product combo picker for Sổ Bán Lẻ auto-order
parent
00a6931c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
0 deletions
+71
-0
productComboPicker.js
app/libs/productComboPicker.js
+71
-0
No files found.
app/libs/productComboPicker.js
0 → 100644
View file @
57a8a53f
"use strict"
;
var
DEFAULT_TOLERANCE
=
50000
;
var
DEFAULT_MAX_ATTEMPTS
=
20
;
var
DEFAULT_MAX_QTY_PER_LINE
=
3
;
function
shuffle
(
list
,
random
)
{
var
arr
=
list
.
slice
();
for
(
var
i
=
arr
.
length
-
1
;
i
>
0
;
i
--
)
{
var
j
=
Math
.
floor
(
random
()
*
(
i
+
1
));
var
tmp
=
arr
[
i
];
arr
[
i
]
=
arr
[
j
];
arr
[
j
]
=
tmp
;
}
return
arr
;
}
function
computeLineTotal
(
product
,
qty
)
{
var
rate
=
product
.
tax
&&
typeof
product
.
tax
.
rate
===
"number"
?
product
.
tax
.
rate
:
0
;
return
Math
.
round
(
qty
*
product
.
price
*
(
1
+
rate
/
100
));
}
function
pickProductCombo
(
products
,
targetAmount
,
options
)
{
options
=
options
||
{};
var
tolerance
=
typeof
options
.
tolerance
===
"number"
?
options
.
tolerance
:
DEFAULT_TOLERANCE
;
var
maxAttempts
=
options
.
maxAttempts
||
DEFAULT_MAX_ATTEMPTS
;
var
random
=
options
.
random
||
Math
.
random
;
var
maxQtyPerLine
=
options
.
maxQtyPerLine
||
DEFAULT_MAX_QTY_PER_LINE
;
var
candidates
=
products
.
filter
(
function
(
p
)
{
return
p
.
qty
>
0
&&
p
.
price
>
0
;
});
if
(
candidates
.
length
===
0
)
{
return
null
;
}
var
minAcceptable
=
Math
.
max
(
1
,
targetAmount
-
tolerance
);
for
(
var
attempt
=
0
;
attempt
<
maxAttempts
;
attempt
++
)
{
var
shuffled
=
shuffle
(
candidates
,
random
);
var
total
=
0
;
var
lines
=
[];
for
(
var
i
=
0
;
i
<
shuffled
.
length
;
i
++
)
{
var
product
=
shuffled
[
i
];
var
maxQty
=
Math
.
min
(
maxQtyPerLine
,
product
.
qty
);
var
qty
=
1
+
Math
.
floor
(
random
()
*
maxQty
);
var
lineTotal
=
computeLineTotal
(
product
,
qty
);
if
(
total
+
lineTotal
>
targetAmount
)
{
continue
;
}
total
+=
lineTotal
;
lines
.
push
({
product_id
:
product
.
id
,
qty
:
qty
});
if
(
total
>=
minAcceptable
)
{
break
;
}
}
if
(
total
>=
minAcceptable
&&
total
<=
targetAmount
)
{
return
{
lines
:
lines
,
total
:
total
};
}
}
return
null
;
}
module
.
exports
=
{
pickProductCombo
:
pickProductCombo
,
computeLineTotal
:
computeLineTotal
,
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment