Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
php_utils
Project
Project
Details
Activity
Releases
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
bp
php_utils
Commits
086d63f1
Commit
086d63f1
authored
Aug 04, 2021
by
wanjilong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: 增加优惠券分摊计算策略
parent
98d1aea6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
ShareWorker.php
src/Strategy/ShareWorker.php
+62
-0
No files found.
src/Strategy/ShareWorker.php
0 → 100644
View file @
086d63f1
<?php
namespace
Api\PhpUtils\Strategy
;
class
ShareWorker
{
/**
* @param $items = ['sku_1'=>100, 'sku_2'=>85]
* @param $tip = 100
* @throws \ErrorException
* 根据商品价格等比做优惠金额的分摊, 做四舍五入。
*/
public
static
function
coupon
(
$items
,
$total_tip
)
{
if
(
!
is_array
(
$items
)
||
$total_tip
<=
0
)
{
throw
new
\ErrorException
(
'输入分摊条件失败,请确认'
,
'3001'
);
}
$total
=
0
;
foreach
(
$items
as
$k
=>
$price
)
{
if
(
!
is_int
(
$price
)
||
$price
<
0
)
{
throw
new
\ErrorException
(
'存在金额非法,请核对金额'
,
'3002'
);
}
$total
+=
$price
;
}
$ret
=
[];
$cleared
=
$cleared_tip
=
0
;
foreach
(
$items
as
$k
=>
$price
)
{
$left
=
$total
-
$cleared
;
$ret
[
$k
]
=
intval
(
round
(
$price
/
$left
*
(
$total_tip
-
$cleared_tip
)));
$cleared_tip
+=
$ret
[
$k
];
$cleared
+=
$price
;
}
return
$ret
;
}
/**
* @param $mutiArr = [
['goods'=>['a1'=>600, 'a2'=>100, 'a3'=>1300], 'tip'=>100],
['goods'=>['a1'=>600, 'a2'=>600, 'a3'=>600], 'tip'=>4],
];
* @return array = [
['a1' => 30, 'a2' => 5, 'a3' => 65],
['a1' => 1, 'a2' => 2, 'a3' => 1]
];
* @throws \ErrorException
* 批量计算奖励,
*/
public
static
function
batchCoupon
(
$mutiArr
)
{
$ret
=
[];
foreach
(
$mutiArr
as
$k
=>
$item
)
{
$ret
[
$k
]
=
self
::
coupon
(
$item
[
'goods'
],
$item
[
'tip'
]);
}
return
$ret
;
}
}
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