Commit 3db41cf3 authored by hangjun83's avatar hangjun83

1、增加对期货商品的退货逻辑

parent 574d9d6c
......@@ -282,7 +282,8 @@ class BhOrdersService
$currentPrice = floatval($orderitem['si_discount']);
$currentAmount = floatval($orderitem['si_amount']);
// 退货剩余数量
$num = intval($refundInfo->refund_nums);
$num = $spotNums = intval($refundInfo->refund_nums);
$futuresNum = 0;
if($num < 0){
throw new \LogicException('退货数量大于购买数量',500);
......@@ -299,6 +300,22 @@ class BhOrdersService
throw new \LogicException('本功能不支持全部退货,如果需要全部退货请移步百化系统后台进行操作!',500);
}
// 获取实际现货数量
$dpdSum = $dbConnect->table('dpdetail')
->where('si_id',$orderitem['si_id'])
->sum('dpd_num');
// 判断是否有期货,如果有用现货数量做退货
if($sum > $dpdSum){
//期货数量
$futuresNum = $sum - $dpdSum;
if($futuresNum == $refundInfo->refund_nums){
$spotNums = 0;
}elseif($refundInfo->refund_nums > $futuresNum){
$spotNums = $refundInfo->refund_nums - $futuresNum;
}
}
//查询是否已经开票,如果开票不能进行退货
$dpdetail = $dbConnect->table('dpdetail')
->where('si_id',$orderitem['si_id'])
......@@ -311,6 +328,17 @@ class BhOrdersService
}
}
}
// 如果有期货先处理期货,然后在处理现货。期货不需要dpdetail拆单
while(true) {
$currentProcessNums = $spotNums;
// 如果有期货数量优先处理
if($futuresNum > 0){
$currentProcessNums = $futuresNum;
}
//修改item数据项
$service = app(BhSoitemsRepository::class);
$soItem = $service->findWhere(['si_id' => $orderitem['si_id'],'si_if_cancel' => 0]);
......@@ -325,59 +353,63 @@ class BhOrdersService
$updatedItem = [];
//如果取消的是全部
if(($num - $orderitem['si_num']) == 0){
if (($currentProcessNums - $orderitem['si_num']) == 0) {
// 如果需要手续费
if(isset($refundInfo->handle_fee) && $refundInfo->handle_fee > 0){
if (isset($refundInfo->handle_fee) && $refundInfo->handle_fee > 0) {
$createArr = $soItem;
unset($createArr['si_id']);
$createArr['si_num'] = $orderitem['si_num'];
$createArr['si_assigned_num'] = $createArr['si_num'];
$createArr['si_amount'] = bcsub($orderitem['si_amount'] , $refundInfo->handle_fee,2);
$createArr['si_amount'] = bcsub($orderitem['si_amount'], $refundInfo->handle_fee, 2);
$createArr['si_discount'] = bcdiv($createArr['si_amount'], $orderitem['si_num'], 2);
$updateArr['si_num'] = $refundInfo->refund_nums;
$updateArr['si_assigned_num'] = $updateArr['si_num'];
$updateArr['si_amount'] = $refundInfo->handle_fee;
$updateArr['si_discount'] = bcdiv($updateArr['si_amount'],$refundInfo->refund_nums, 2);
$updateArr['si_discount'] = bcdiv($updateArr['si_amount'], $currentProcessNums, 2);
}else{
} else {
$updatedItem['cancel_si_id'] = $orderitem['si_id'];
}
} // 部分退货
else{
else {
if($futuresNum > 0){
$orderitem['si_num'] = $orderitem['si_num'] - $futuresNum;
}
$updateArr['si_num'] = bcsub($orderitem['si_num'], $refundInfo->refund_nums); // 如果数量只有1,且退货数量相等
$updateArr['si_num'] = bcsub($orderitem['si_num'], $currentProcessNums); // 如果数量只有1,且退货数量相等
$updateArr['si_assigned_num'] = $updateArr['si_num'];
$updateArr['si_amount'] = bcmul(
$orderitem['si_discount'] ,
$orderitem['si_discount'],
$updateArr['si_num'],
2
);
if(isset($refundInfo->handle_fee) && $refundInfo->handle_fee > 0){
$updateArr['si_amount'] = bcadd($updateArr['si_amount'] , $refundInfo->handle_fee,2);
if (isset($refundInfo->handle_fee) && $refundInfo->handle_fee > 0) {
$updateArr['si_amount'] = bcadd($updateArr['si_amount'], $refundInfo->handle_fee, 2);
}
$updateArr['si_discount'] = bcdiv($updateArr['si_amount'] ,$updateArr['si_num'] , 2);
$updateArr['si_discount'] = bcdiv($updateArr['si_amount'], $updateArr['si_num'], 2);
// 新增
$createArr = $soItem;
unset($createArr['si_id']);
$createArr['si_num'] = $refundInfo->refund_nums;
$createArr['si_num'] = $currentProcessNums;
if(isset($refundInfo->handle_fee) && floatval($refundInfo->handle_fee) > 0){
if (isset($refundInfo->handle_fee) && floatval($refundInfo->handle_fee) > 0) {
$createArr['si_amount'] = abs(bcsub($currentAmount, $updateArr['si_amount'], 2));
$createArr['si_discount'] = bcdiv($createArr['si_amount'], $createArr['si_num'], 2);
} // 没有手续费的情况下
else{
else {
$createArr['si_discount'] = $soItem['si_discount'];
$createArr['si_amount'] = bcmul($soItem['si_discount'], $refundInfo->refund_nums, 2);
$createArr['si_amount'] = bcmul($soItem['si_discount'], $currentProcessNums, 2);
}
$createArr['si_num'] = $refundInfo->refund_nums;
$createArr['si_num'] = $currentProcessNums;
$createArr['si_assigned_num'] = $createArr['si_num'];
if($createArr['si_amount'] < 0){
throw new \LogicException('手续费异常,处理失败!',500);
if ($createArr['si_amount'] < 0) {
throw new \LogicException('手续费异常,处理失败!', 500);
}
}
......@@ -387,10 +419,10 @@ class BhOrdersService
if($num > 1){
$updateArr['si_discount'] = bcdiv($updateArr['si_amount'] , $num, 2);
}*/
if(!empty($updateArr)){
$update = $service->update($updateArr,$soItem['si_id']);
if (!empty($updateArr)) {
$update = $service->update($updateArr, $soItem['si_id']);
$updatedItem['update'] = $update->toArray();
}else{
} else {
$updatedItem['update'] = $orderitem;
}
......@@ -398,10 +430,10 @@ class BhOrdersService
$updatedItem['update'] = $update->toArray();*/
//判断是否能除尽
if(!empty($updateArr)){
if (!empty($updateArr)) {
$amount = bcmul($updateArr['si_discount'], $updateArr['si_num'], 2);
if($updateArr['si_amount'] != $amount){
$note[] = 'si_id = '.$soItem['si_id'] . '的数据有除不尽的情况,请手工处理';
if ($updateArr['si_amount'] != $amount) {
$note[] = 'si_id = ' . $soItem['si_id'] . '的数据有除不尽的情况,请手工处理';
}
}
......@@ -424,36 +456,38 @@ class BhOrdersService
throw new \LogicException('手续费异常,处理失败!',500);
}*/
if(!empty($createArr)){
if (!empty($createArr)) {
$newItem = $service->create($createArr);
$updatedItem['create'] = $newItem->toArray();
// 如果是部分退货
if($num > 0 || ($num == 0 && $refundInfo->handle_fee > 0)){
if ($num > 0 || ($num == 0 && $refundInfo->handle_fee > 0)) {
$updatedItem['cancel_si_id'] = $updatedItem['create']['si_id'];
}
}else{
} else {
$updatedItem['create'] = $soItem;
$updatedItem['cancel_si_id'] = $updatedItem['create']['si_id'];
}
$updatedItem['total'] = $currentAmount;
// 如果没有期货数量,dpdetail只处理现货数量
if ($futuresNum == 0) {
//修改配货单记录
if($dpdetail){
if ($dpdetail) {
$maxRecord = $minRecord = [];
$needUpdate = null;
$totalNums = $totalAmount = 0;
//针对多个dpdetail数据,进行数量判断
//默认都是没有开票过的
foreach($dpdetail as $detail){
foreach ($dpdetail as $detail) {
$detailArr = [];
$totalNums += $detail->dpd_num;
$totalAmount += $detail->dpd_amount;
foreach($detail as $key => $val){
foreach ($detail as $key => $val) {
$detailArr[$key] = $val;
}
if($detail->dpd_num == $refundInfo->refund_nums){
if ($detail->dpd_num == $currentProcessNums) {
$needUpdate = $detailArr;
break;
}
......@@ -463,40 +497,40 @@ class BhOrdersService
// 4 数量8 3条记录 4/2/2, 退货4瓶
// 5 数量8 3条记录 4/3/1, 退货4瓶
// 6 数量11 3条记录 6/4/1, 退货5瓶
if($detail->dpd_num > $refundInfo->refund_nums){
if ($detail->dpd_num > $currentProcessNums) {
$nums = $detail->dpd_num;
$maxRecord[$nums][$detail->dpd_id] = $detailArr;
}elseif($refundInfo->refund_nums > $detail->dpd_num){
$nums = $refundInfo->refund_nums - $detail->dpd_num;
} elseif ($currentProcessNums > $detail->dpd_num) {
$nums = $currentProcessNums - $detail->dpd_num;
$minRecord[$nums][$detail->dpd_id] = $detailArr;
}
}
$cancelRecord = [];
// 如果退货数量跟此明细数量相同,不需要拆分的情况下
if(!is_null($needUpdate)){
if (!is_null($needUpdate)) {
$dpUpdateArr = [];
// 如果有手续费,对价格进行更新
if($refundInfo->handle_fee > 0){
if ($refundInfo->handle_fee > 0) {
// 如果是全部退货
if($refundInfo->refund_nums == $needUpdate['dpd_num'] && count($dpdetail) == 1){
if ($currentProcessNums == $needUpdate['dpd_num'] && count($dpdetail) == 1) {
// 如果有手续费,将价格进行修改
foreach($dpdetail as $detail){
foreach ($dpdetail as $detail) {
$otherUpdate = [];
$otherUpdate['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $detail->dpd_num, 2);
$dbConnect->table('dpdetail')
->where('dpd_id',$detail->dpd_id)
->where('dpd_id', $detail->dpd_id)
->update($otherUpdate);
}
}else{
} else {
// 如果有手续费,将价格进行修改
foreach($dpdetail as $detail){
if($detail->dpd_id != $needUpdate['dpd_id']){
foreach ($dpdetail as $detail) {
if ($detail->dpd_id != $needUpdate['dpd_id']) {
$otherUpdate = [];
$otherUpdate['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $detail->dpd_num, 2);
$dbConnect->table('dpdetail')
->where('dpd_id',$detail->dpd_id)
->where('dpd_id', $detail->dpd_id)
->update($otherUpdate);
}
}
......@@ -505,60 +539,62 @@ class BhOrdersService
//将取消的记录价格进行更新
$dpUpdateArr['dpd_amount'] = bcmul($updatedItem['create']['si_discount'], $needUpdate['dpd_num'], 2);
$dbConnect->table('dpdetail')
->where('dpd_id',$needUpdate['dpd_id'])
->where('dpd_id', $needUpdate['dpd_id'])
->update($dpUpdateArr);
}
} // 如果没有手续费,不对价格进行更新
else{
else {
$dpUpdateArr['si_id'] = $updatedItem['create']['si_id'];
$dbConnect->table('dpdetail')
->where('dpd_id',$needUpdate['dpd_id'])
->where('dpd_id', $needUpdate['dpd_id'])
->update($dpUpdateArr);
}
}else{
} else {
asort($maxRecord);
arsort($minRecord); // 降序排序
$num = $refundInfo->refund_nums;
$num = $currentProcessNums;
// minrecord有值,只有退货数量大于此条记录的数量时
$cancelRecord = array_merge($cancelRecord,$this->handleDpdetailInfo($dbConnect,$num,$minRecord,$updatedItem,$refundInfo,'asort'));
$cancelRecord = array_merge($cancelRecord,$this->handleDpdetailInfo($dbConnect,$num,$maxRecord,$updatedItem,$refundInfo,'arsort'));
$cancelRecord = array_merge($cancelRecord, $this->handleDpdetailInfo($dbConnect, $num, $minRecord, $updatedItem, $refundInfo, 'asort'));
$cancelRecord = array_merge($cancelRecord, $this->handleDpdetailInfo($dbConnect, $num, $maxRecord, $updatedItem, $refundInfo, 'arsort'));
// 判断是否这次是全部取消
$dpCount = $dbConnect->table('dpdetail')
->where('si_id',$orderitem['si_id'])
->whereIn('dpd_id',$cancelRecord)->sum('dpd_num');
if($dpCount == 0 && $sum == $refundInfo->refund_nums){
->where('si_id', $orderitem['si_id'])
->whereIn('dpd_id', $cancelRecord)->sum('dpd_num');
if ($dpCount == 0 && $sum == $currentProcessNums) {
$infos = $dbConnect->table('dpdetail')
->whereIn('dpd_id',$cancelRecord)->get();
foreach($infos->toArray() as $info){
->whereIn('dpd_id', $cancelRecord)->get();
foreach ($infos->toArray() as $info) {
$update = [];
$update['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $info->dpd_num, 2);
$update['si_id'] = $orderitem['si_id'];
$dbConnect->table('dpdetail')
->where('dpd_id',$info->dpd_id)
->where('dpd_id', $info->dpd_id)
->update($update);
}
}
}
// 更新除了取消的记录以外的记录的价格
if(count($cancelRecord) > 0){
if (count($cancelRecord) > 0) {
$dpdetailInfo = $dbConnect->table('dpdetail')
->where('si_id',$orderitem['si_id'])
->whereNotIn('dpd_id',$cancelRecord)->get();
->where('si_id', $orderitem['si_id'])
->whereNotIn('dpd_id', $cancelRecord)->get();
$dpdetailInfo = $dpdetailInfo->toArray();
if(count($dpdetailInfo) > 0){
foreach($dpdetailInfo as $info){
if (count($dpdetailInfo) > 0) {
foreach ($dpdetailInfo as $info) {
$otherUpdate = [];
$otherUpdate['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $info->dpd_num, 2);
$dbConnect->table('dpdetail')
->where('dpd_id',$info->dpd_id)
->where('dpd_id', $info->dpd_id)
->update($otherUpdate);
}
}
}
}
}
//新增配货单数据
/*foreach($dpdetail as $key => $value){
......@@ -576,62 +612,62 @@ class BhOrdersService
$poItems = $dbConnect->table('poitems')
->where('p_id', $soItemArr['p_id'])
->where('pi_id', $soItemArr['pi_id'])
->where('pi_status','<>',3)
->where('pi_status', '<>', 3)
->get();
if($poItems){
foreach($poItems as $item){
if ($poItems) {
foreach ($poItems as $item) {
// 如果不是取消状态的
if($item->pi_status != 3){
if ($item->pi_status != 3) {
// 可能存在对应多个po的情况
if($item->pi_num < $refundInfo->refund_nums){
throw new \LogicException('采购订单数量不足,无法退货!',500);
if ($item->pi_num < $currentProcessNums) {
throw new \LogicException('采购订单数量不足,无法退货!', 500);
}
// 如果退货数量小于采购订单数量
if($item->pi_num > $refundInfo->refund_nums){
if ($item->pi_num > $currentProcessNums) {
// 修改当前的数量和金额
$poItemUpdateArr = [];
$poNums = bcsub($item->pi_num, $refundInfo->refund_nums);
$poNums = bcsub($item->pi_num, $currentProcessNums);
$poItemUpdateArr['pi_num'] = $poNums;
$poItemUpdateArr['pi_amount'] = bcmul($item->pi_price, $poNums, 2);
$poItemUpdateArr['pi_rnum'] = $poItemUpdateArr['pi_num'];
if($poItemUpdateArr['pi_num'] == $item->pi_rnum && $item->pi_status == 1){
if ($poItemUpdateArr['pi_num'] == $item->pi_rnum && $item->pi_status == 1) {
$poItemUpdateArr['pi_status'] = 2;
}
$dbConnect->table('poitems')
->where('pi_id',$item->pi_id)
->where('pi_id', $item->pi_id)
->update($poItemUpdateArr);
// 新增退货的采购订单信息
$poItemCreateArr = [];
foreach($item as $key => $ite){
foreach ($item as $key => $ite) {
$poItemCreateArr[$key] = $ite;
}
unset($poItemCreateArr['pi_id']);
$poItemCreateArr['pi_num'] = $refundInfo->refund_nums;
$poItemCreateArr['pi_num'] = $currentProcessNums;
$poItemCreateArr['pi_amount'] = bcmul($item->pi_price, $poItemCreateArr['pi_num'], 2);
$poItemCreateArr['pi_rnum'] = $refundInfo->refund_nums;
$poItemCreateArr['pi_rnum'] = $currentProcessNums;
$poItemCreateArr['pi_status'] = $poItemCreateArr['pi_num'] == $poItemCreateArr['pi_rnum'] ? 2 : 1;
$poItemsId = $dbConnect->table('poitems')->insertGetId($poItemCreateArr);
//将soitems表中的新增记录中的pi_id修改为新值
$dbConnect->table('soitems')
->where('si_id',$updatedItem['create']['si_id'])
->where('si_id', $updatedItem['create']['si_id'])
->update([
'pi_id' => $poItemsId
]);
if($refundInfo->cancel_porder == 1){
if ($refundInfo->cancel_porder == 1) {
// 取消采购订单
$this->cancelPorderItems($dbConnect,$poItemCreateArr['po_id'],$poItemsId);
$this->cancelPorderItems($dbConnect, $poItemCreateArr['po_id'], $poItemsId);
}
}elseif($item->pi_num == $refundInfo->refund_nums){
if($refundInfo->cancel_porder == 1){
} elseif ($item->pi_num == $currentProcessNums) {
if ($refundInfo->cancel_porder == 1) {
// 取消采购订单
$this->cancelPorderItems($dbConnect,$item->po_id,$item->pi_id);
$this->cancelPorderItems($dbConnect, $item->po_id, $item->pi_id);
}
}
}
......@@ -653,7 +689,15 @@ class BhOrdersService
*/
//预存款和退款处理
$this->cancelAndTransPrePay($dbConnect,$updatedItem['cancel_si_id'],$refundInfo);
$this->cancelAndTransPrePay($dbConnect, $updatedItem['cancel_si_id'], $refundInfo);
if($futuresNum > 0){
$futuresNum = 0;
}else{
break;
}
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment