提交 3e814ebd 编写于 作者: D devil_gong

库存扣除

上级 2a628b66
......@@ -18,7 +18,7 @@
<input type="number" name="{{$data.admin_page_number.only_tag}}" placeholder="{{$data.admin_page_number.name}}" pattern="{{:L('common_regex_page_number')}}" data-validation-message="{{$data.admin_page_number.error_tips}}" class="am-radius" <present name="data"> value="{{$data.admin_page_number.value}}"</present> required />
</div>
<div class="am-form-group">
<label>{{$data.common_is_deduction_inventory.name}}</label>
<label>{{$data.common_is_deduction_inventory.name}}<span class="fs-12 fw-100 cr-999">({{$data.common_is_deduction_inventory.describe}})</span></label>
<select name="{{$data.common_is_deduction_inventory.only_tag}}" class="am-radius chosen-select c-p" data-validation-message="{{$data.common_is_deduction_inventory.error_tips}}" required>
<foreach name="common_is_text_list" item="v">
<option value="{{$v.id}}" <if condition="isset($data['common_is_deduction_inventory']['value']) and $data['common_is_deduction_inventory']['value'] eq $v['id']">selected</if>>{{$v.name}}</option>
......
......@@ -418,9 +418,9 @@ return array(
// 扣除库存规则
'common_deduction_inventory_rules_list' => array(
0 => array('id' => 0, 'name' => '订单提交成功', 'checked' => true),
0 => array('id' => 0, 'name' => '订单确认成功', 'checked' => true),
1 => array('id' => 1, 'name' => '订单支付成功'),
2 => array('id' => 2, 'name' => '订单发货'),
2 => array('id' => 2, 'name' => '订单发货'),
),
// 是否已读
......
......@@ -715,5 +715,109 @@ class BuyService
}
return self::CartTotal(['user_id'=>$params['user']['id']]);
}
/**
* 库存扣除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-11-09
* @desc description
* @param [array] $params [输入参数]
*/
public static function OrderInventoryDeduct($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'order_id',
'error_msg' => '订单id有误',
],
[
'checked_type' => 'empty',
'key_name' => 'order_data',
'error_msg' => '订单更新数据不能为空',
],
[
'checked_type' => 'is_array',
'key_name' => 'order_data',
'error_msg' => '订单更新数据有误',
]
];
$ret = params_checked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 是否扣除库存
$common_is_deduction_inventory = MyC('common_is_deduction_inventory', 0);
if($common_is_deduction_inventory != 1)
{
return DataReturn('未开启扣除库存', 0);
}
// 扣除库存规则
$common_deduction_inventory_rules = MyC('common_deduction_inventory_rules', 1);
switch($common_deduction_inventory_rules)
{
// 订单确认成功
case 0 :
if($params['order_data']['status'] != 1)
{
return DataReturn('当前订单状态未操作确认不扣除库存['.$params['order_id'].']', 0);
}
break;
// 订单支付成功
case 1 :
if($params['order_data']['status'] != 2)
{
return DataReturn('当前订单状态未操作支付不扣除库存['.$params['order_id'].']', 0);
}
break;
// 订单发货
case 2 :
if($params['order_data']['status'] != 3)
{
return DataReturn('当前订单状态未操作发货不扣除库存['.$params['order_id'].']', 0);
}
break;
}
// 获取订单商品
$order_detail = M('OrderDetail')->field('goods_id,buy_number')->where(['order_id'=>$params['order_id']])->select();
if(!empty($order_detail))
{
$goods_m = M('Goods');
$log_m = M('OrderGoodsInventoryLog');
foreach($order_detail as $v)
{
$goods = $goods_m->field('is_deduction_inventory,inventory')->find();
if(isset($goods['is_deduction_inventory']) && $goods['is_deduction_inventory'] == 1)
{
// 扣除操作
if(!$goods_m->where(['id'=>$v['goods_id']])->setDec('inventory', $v['buy_number']))
{
return DataReturn('库存扣减失败['.$params['order_id'].'-'.$v['goods_id'].']', -10);
}
// 扣除日志添加
$log_data = [
'order_id' => $params['order_id'],
'goods_id' => $v['goods_id'],
'order_status' => $params['order_data']['status'],
'original_inventory' => $goods['inventory'],
'new_inventory' => $goods['inventory']+$v['buy_number'],
'add_time' => time(),
];
$log_m->add($log_data);
}
}
}
}
}
?>
\ No newline at end of file
......@@ -4,6 +4,7 @@ namespace Service;
use Service\GoodsService;
use Service\ResourcesService;
use Service\BuyService;
/**
* 订单服务层
......@@ -356,10 +357,17 @@ class OrderService
// 添加状态日志
if(self::OrderHistoryAdd($params['order']['id'], 2, $params['order']['status'], '支付', 0, '系统'))
{
// 库存扣除
$ret = BuyService::OrderInventoryDeduct(['order_id'=>$params['order']['id'], 'order_data'=>$upd_data]);
if($ret['status'] != 0)
{
// 事务回滚
$m->rollback();
return DataReturn($ret['msg'], -10);
}
// 提交事务
$m->commit();
// 成功
return DataReturn('支付成功', 0);
}
}
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册