Commit 0c25be75 authored by hangjun83's avatar hangjun83

罗恩工具类更新

parent 8dc2f273
...@@ -329,26 +329,35 @@ class RhawnOrdersService ...@@ -329,26 +329,35 @@ class RhawnOrdersService
$dbConnect->beginTransaction(); $dbConnect->beginTransaction();
// 处理订单退货项 // 处理订单退货项
foreach($sOrder->toArray() as $orderitem){ foreach($sOrder->toArray() as $key => $orderitem){
if($refundInfo->item_id == $orderitem['si_id']) { if(in_array($orderitem['si_id'],explode(',',$refundInfo->item_id))) {
// 先处理源数据 // 先处理源数据
$currentPrice = floatval($orderitem['si_discount']); $currentPrice = floatval($orderitem['si_discount']);
$currentAmount = floatval($orderitem['si_amount']); $currentAmount = floatval($orderitem['si_amount']);
// 退货剩余数量 // 退货剩余数量
$numArr = !empty($refundInfo->refund_nums) ? explode(',', $refundInfo->refund_nums) : ''; $numArr = !empty($refundInfo->refund_nums) ? explode(',', $refundInfo->refund_nums) : '';
$futuresNum = $refundInfo->futures_num && $refundInfo->futures_num > 0 ? $refundInfo->futures_num : 0 ; $futuresNum = $refundInfo->futures_num && $refundInfo->futures_num > 0 ? $refundInfo->futures_num : 0;
$refundNums = [];
// 预判断退货数量 // 预判断退货数量
if (!empty($numArr)) { if (!empty($numArr)) {
$pstk_ids = []; $pstk_ids = [];
$refundNums = [];
foreach ($numArr as $num) { foreach ($numArr as $num) {
$pstkArr = explode('|', $num); $pstkArr = explode('|', $num);
$refundNums[$pstkArr[0]] = $pstkArr[1]; $refundNumsArr[$pstkArr[0]] = $pstkArr[1];
$pstk_ids[] = $pstkArr[0]; $pstk_ids[] = $pstkArr[0];
} }
$dpd = $dbConnect->table('dpdetail')
->whereIn('pstk_id', $pstk_ids)
->where('si_id',$orderitem['si_id'])
->where('dpd_invoiced', '=', 0)
->get();
foreach($dpd->toArray() as $d){
$refundNums[$d->pstk_id] = $refundNumsArr[$d->pstk_id];
}
//分组查询发货数量 //分组查询发货数量
/*$dpdSum = $dbConnect->table('dpdetail') /*$dpdSum = $dbConnect->table('dpdetail')
->whereIn('pstk_id', $pstk_ids) ->whereIn('pstk_id', $pstk_ids)
...@@ -373,7 +382,7 @@ class RhawnOrdersService ...@@ -373,7 +382,7 @@ class RhawnOrdersService
} }
// 如果是期货,强行初始一个数组执行一次 // 如果是期货,强行初始一个数组执行一次
if($futuresNum > 0 && empty($refundNums)){ if ($futuresNum > 0 && empty($refundNums)) {
$refundNums[0] = $futuresNum; $refundNums[0] = $futuresNum;
} }
...@@ -381,235 +390,233 @@ class RhawnOrdersService ...@@ -381,235 +390,233 @@ class RhawnOrdersService
foreach ($refundNums as $pstk_id => $num) { foreach ($refundNums as $pstk_id => $num) {
while (true) { //修改item数据项
$service = app(RhawnSoitemsRepository::class);
$soItem = $service->findWhere(['si_id' => $orderitem['si_id'], 'si_if_cancel' => 0]);
$soItem = current($soItem->toArray());
if (empty($soItem) || count($soItem) == 0) {
throw new \LogicException('购买项数据id:' . $orderitem['si_id'] . '数据不存在或已取消!', 500);
}
//修改item数据项 //如果有退货期货,检查退货期货数量是否大于当前期货数量
$service = app(RhawnSoitemsRepository::class); if ($futuresNum > 0) {
$soItem = $service->findWhere(['si_id' => $orderitem['si_id'], 'si_if_cancel' => 0]); if ($pstk_id > 0) {
$soItem = current($soItem->toArray()); $dpdSum = $dbConnect->table('dpdetail')
if (empty($soItem) || count($soItem) == 0) { ->where('pstk_id', $pstk_id)
throw new \LogicException('购买项数据id:' . $orderitem['si_id'] . '数据不存在或已取消!', 500); ->where('si_id', $refundInfo->item_id)
->where('dpd_invoiced', '=', 0)
->groupBy('pstk_id')
->select(DB::raw("sum(dpd_num) as num"),)
->get()->first();
} else {
$dpdSum = $dbConnect->table('dpdetail')
->where('si_id', $refundInfo->item_id)
->where('dpd_invoiced', '=', 0)
->select(DB::raw("sum(dpd_num) as num"),)
->get()->first();
} }
//如果有退货期货,检查退货期货数量是否大于当前期货数量 if (($soItem['si_num'] - $dpdSum->num) == 0) {
if ($futuresNum > 0) { $futuresNum = 0;
if($pstk_id > 0){ if ($pstk_id == 0) {
$dpdSum = $dbConnect->table('dpdetail') throw new \LogicException('当前数据不存在期货,无法对期货进行退货', 500);
->where('pstk_id', $pstk_id)
->where('si_id', $refundInfo->item_id)
->where('dpd_invoiced', '=', 0)
->groupBy('pstk_id')
->select(DB::raw("sum(dpd_num) as num"),)
->get()->first();
}else{
$dpdSum = $dbConnect->table('dpdetail')
->where('si_id',$refundInfo->item_id)
->where('dpd_invoiced', '=', 0)
->select(DB::raw("sum(dpd_num) as num"),)
->get()->first();
}
if(($soItem['si_num'] - $dpdSum->num) == 0){
$futuresNum = 0;
if($pstk_id == 0){
throw new \LogicException('当前数据不存在期货,无法对期货进行退货', 500);
}
}
if($futuresNum > 0) {
if ($futuresNum > ($soItem['si_num'] - $dpdSum->num)) {
throw new \LogicException('期货退货数量大于可退货数量', 500);
}
} }
} }
// 判断现货退货数量是否大于soitem的数量
if ($num > $soItem['si_num']) {
throw new \LogicException('现货退货数量大于可退货数量', 500);
}
// 如果有期货数量优先处理
if ($futuresNum > 0) { if ($futuresNum > 0) {
$currentProcessNums = $futuresNum; if ($futuresNum > ($soItem['si_num'] - $dpdSum->num)) {
}else{ throw new \LogicException('期货退货数量大于可退货数量', 500);
$currentProcessNums = $num; }
} }
}
//更新原本数据 // 判断现货退货数量是否大于soitem的数量
$updateArr = []; if ($num > $soItem['si_num']) {
$createArr = []; throw new \LogicException('现货退货数量大于可退货数量', 500);
$updatedItem = []; }
//如果取消的是全部
if (($currentProcessNums - $orderitem['si_num']) == 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_discount'] = bcdiv($createArr['si_amount'], $orderitem['si_num'], 2);
$updateArr['si_num'] = $currentProcessNums;
//$updateArr['si_assigned_num'] = $updateArr['si_num'];
$updateArr['si_amount'] = $refundInfo->handle_fee;
$updateArr['si_discount'] = bcdiv($updateArr['si_amount'], $currentProcessNums, 2);
} else {
$updatedItem['cancel_si_id'] = $orderitem['si_id'];
}
} // 部分退货 // 如果有期货数量优先处理
else { if ($futuresNum > 0) {
$currentProcessNums = $futuresNum;
} else {
$currentProcessNums = $num;
}
if ($futuresNum > 0) { //更新原本数据
$updateArr['si_num'] = $orderitem['si_num'] - $futuresNum; $updateArr = [];
//$updateArr['si_assigned_num'] = $orderitem['si_assigned_num'] - $futuresNum; $createArr = [];
} else { $updatedItem = [];
$updateArr['si_num'] = bcsub($orderitem['si_num'], $currentProcessNums); // 如果数量只有1,且退货数量相等
$updateArr['si_assigned_num'] = $updateArr['si_num'];
}
$updateArr['si_amount'] = bcmul( //如果取消的是全部
$orderitem['si_discount'], if (($currentProcessNums - $orderitem['si_num']) == 0) {
$updateArr['si_num'], // 如果需要手续费
2 if (isset($refundInfo->handle_fee) && $refundInfo->handle_fee > 0) {
);
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);
// 新增
$createArr = $soItem; $createArr = $soItem;
unset($createArr['si_id']); unset($createArr['si_id']);
$createArr['si_num'] = $currentProcessNums; $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_discount'] = bcdiv($createArr['si_amount'], $orderitem['si_num'], 2);
if (isset($refundInfo->handle_fee) && floatval($refundInfo->handle_fee) > 0) { $updateArr['si_num'] = $currentProcessNums;
$createArr['si_amount'] = abs(bcsub($currentAmount, $updateArr['si_amount'], 2)); //$updateArr['si_assigned_num'] = $updateArr['si_num'];
$createArr['si_discount'] = bcdiv($createArr['si_amount'], $createArr['si_num'], 2); $updateArr['si_amount'] = $refundInfo->handle_fee;
} // 没有手续费的情况下 $updateArr['si_discount'] = bcdiv($updateArr['si_amount'], $currentProcessNums, 2);
else {
$createArr['si_discount'] = $soItem['si_discount'];
$createArr['si_amount'] = bcmul($soItem['si_discount'], $currentProcessNums, 2);
}
$createArr['si_num'] = $currentProcessNums;
if ($futuresNum > 0) {
$createArr['si_assigned_num'] = 0;
} else {
$createArr['si_assigned_num'] = $createArr['si_num'];
}
if ($createArr['si_amount'] < 0) { } else {
throw new \LogicException('手续费异常,处理失败!', 500); $updatedItem['cancel_si_id'] = $orderitem['si_id'];
}
} }
if (!empty($updateArr)) { } // 部分退货
$update = $service->update($updateArr, $soItem['si_id']); else {
$updatedItem['update'] = $update->toArray();
if ($futuresNum > 0) {
$updateArr['si_num'] = $orderitem['si_num'] - $futuresNum;
//$updateArr['si_assigned_num'] = $orderitem['si_assigned_num'] - $futuresNum;
} else { } else {
$updatedItem['update'] = $orderitem; $updateArr['si_num'] = bcsub($orderitem['si_num'], $currentProcessNums); // 如果数量只有1,且退货数量相等
$updateArr['si_assigned_num'] = $updateArr['si_num'];
} }
//判断是否能除尽 $updateArr['si_amount'] = bcmul(
if (!empty($updateArr)) { $orderitem['si_discount'],
$amount = bcmul($updateArr['si_discount'], $updateArr['si_num'], 2); $updateArr['si_num'],
//if ($updateArr['si_amount'] != $amount) { 2
if (bccomp($updateArr['si_amount'],$amount) != 0) { );
$note[] = 'si_id = ' . $soItem['si_id'] . '的数据有除不尽的情况,请手工处理'; 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);
if (!empty($createArr)) { // 新增
$newItem = $service->create($createArr); $createArr = $soItem;
$updatedItem['create'] = $newItem->toArray(); unset($createArr['si_id']);
// 如果是部分退货 $createArr['si_num'] = $currentProcessNums;
if ($currentProcessNums > 0 || ($currentProcessNums == 0 && $refundInfo->handle_fee > 0)) {
$updatedItem['cancel_si_id'] = $updatedItem['create']['si_id']; 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 {
$createArr['si_discount'] = $soItem['si_discount'];
$createArr['si_amount'] = bcmul($soItem['si_discount'], $currentProcessNums, 2);
}
$createArr['si_num'] = $currentProcessNums;
if ($futuresNum > 0) {
$createArr['si_assigned_num'] = 0;
} else { } else {
$updatedItem['create'] = $soItem; $createArr['si_assigned_num'] = $createArr['si_num'];
$updatedItem['cancel_si_id'] = $updatedItem['create']['si_id'];
} }
$updatedItem['total'] = $currentAmount; if ($createArr['si_amount'] < 0) {
throw new \LogicException('手续费异常,处理失败!', 500);
}
}
// 如果没有期货数量,dpdetail只处理现货数量 if (!empty($updateArr)) {
if ($futuresNum == 0) { $update = $service->update($updateArr, $soItem['si_id']);
$updatedItem['update'] = $update->toArray();
} else {
$updatedItem['update'] = $orderitem;
}
// 获取实际现货数量 //判断是否能除尽
$dpdDetail = $dbConnect->table('dpdetail') if (!empty($updateArr)) {
->where('si_id', $orderitem['si_id']) $amount = bcmul($updateArr['si_discount'], $updateArr['si_num'], 2);
->where('pstk_id', $pstk_id) //if ($updateArr['si_amount'] != $amount) {
->get()->first(); if (bccomp($updateArr['si_amount'], $amount) != 0) {
$note[] = 'si_id = ' . $soItem['si_id'] . '的数据有除不尽的情况,请手工处理';
}
}
if ($dpdDetail) { if (!empty($createArr)) {
// 更新当前记录 $newItem = $service->create($createArr);
$dpUpdateArr = []; $updatedItem['create'] = $newItem->toArray();
$dpUpdateArr['dpd_num'] = $dpdDetail->dpd_num - $currentProcessNums; // 如果是部分退货
$dpUpdateArr['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $dpUpdateArr['dpd_num'], 2); if ($currentProcessNums > 0 || ($currentProcessNums == 0 && $refundInfo->handle_fee > 0)) {
// 出现全部退货 $updatedItem['cancel_si_id'] = $updatedItem['create']['si_id'];
if($dpUpdateArr['dpd_num'] == 0){ }
if($refundInfo->handle_fee > 0){ } else {
$dpUpdateArr['dpd_num'] = $dpdDetail->dpd_num; $updatedItem['create'] = $soItem;
$dpUpdateArr['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $currentProcessNums, 2); $updatedItem['cancel_si_id'] = $updatedItem['create']['si_id'];
} // 如果没有手续费,只修改对应的si_id }
else{
$dpUpdateArr['si_id'] = $updatedItem['create']['si_id']; $updatedItem['total'] = $currentAmount;
$dpUpdateArr['dpd_amount'] = $dpdDetail->dpd_amount; echo $futuresNum;
$dpUpdateArr['dpd_num'] = $dpdDetail->dpd_num;
} // 如果没有期货数量,dpdetail只处理现货数量
} // 如果不是全部退货 if ($futuresNum == 0) {
else{
if($refundInfo->handle_fee > 0){ // 获取实际现货数量
$otherDpdDetail = $dbConnect->table('dpdetail') $dpdDetail = $dbConnect->table('dpdetail')
->where('si_id', $orderitem['si_id']) ->where('si_id', $orderitem['si_id'])
->whereNotIn('pstk_id', [$pstk_id]) ->where('pstk_id', $pstk_id)
->get()->first(); ->get()->first();
if($otherDpdDetail){
$otherUpdate = []; if ($dpdDetail) {
$otherUpdate['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $otherDpdDetail->dpd_num, 2); // 更新当前记录
$dbConnect->table('dpdetail') $dpUpdateArr = [];
->where('dpd_id', $otherDpdDetail->dpd_id) $dpUpdateArr['dpd_num'] = $dpdDetail->dpd_num - $currentProcessNums;
->update($otherUpdate); $dpUpdateArr['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $dpUpdateArr['dpd_num'], 2);
} // 出现全部退货
} if ($dpUpdateArr['dpd_num'] == 0) {
if ($refundInfo->handle_fee > 0) {
$dpUpdateArr['dpd_num'] = $dpdDetail->dpd_num;
$dpUpdateArr['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $currentProcessNums, 2);
} // 如果没有手续费,只修改对应的si_id
else {
$dpUpdateArr['si_id'] = $updatedItem['create']['si_id'];
$dpUpdateArr['dpd_amount'] = $dpdDetail->dpd_amount;
$dpUpdateArr['dpd_num'] = $dpdDetail->dpd_num;
} }
$dbConnect->table('dpdetail') } // 如果不是全部退货
->where('dpd_id', $dpdDetail->dpd_id) else {
->update($dpUpdateArr); if ($refundInfo->handle_fee > 0) {
// 如果是部分退货 $otherDpdDetail = $dbConnect->table('dpdetail')
if (($currentProcessNums < $dpdDetail->dpd_num ) || ($currentProcessNums == $dpdDetail->dpd_num && $refundInfo->handle_fee > 0)) { ->where('si_id', $orderitem['si_id'])
//添加新的取消记录 ->whereNotIn('pstk_id', [$pstk_id])
$dpCreateArr = $dpdDetail; ->get()->first();
unset($dpCreateArr->dpd_id); if ($otherDpdDetail) {
$otherUpdate = [];
$dpCreateArr->dpd_num = $currentProcessNums; $otherUpdate['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $otherDpdDetail->dpd_num, 2);
$dpCreateArr->dpd_amount = bcmul($updatedItem['create']['si_discount'], $currentProcessNums, 2); $dbConnect->table('dpdetail')
$dpCreateArr->si_id = $updatedItem['create']['si_id']; ->where('dpd_id', $otherDpdDetail->dpd_id)
$dpId = $dbConnect->table('dpdetail')->insertGetId((array)$dpCreateArr); ->update($otherUpdate);
}
} }
} }
}
//预存款和退款处理
$this->cancelAndTransPrePay($dbConnect, $updatedItem['cancel_si_id'], $refundInfo);
if ($futuresNum > 0) { $dbConnect->table('dpdetail')
$futuresNum = 0; ->where('dpd_id', $dpdDetail->dpd_id)
$currentProcessNums = 0; ->update($dpUpdateArr);
if(empty($numArr)) break; // 如果是部分退货
} else { if (($currentProcessNums < $dpdDetail->dpd_num) || ($currentProcessNums == $dpdDetail->dpd_num && $refundInfo->handle_fee > 0)) {
break; //添加新的取消记录
$dpCreateArr = $dpdDetail;
unset($dpCreateArr->dpd_id);
$dpCreateArr->dpd_num = $currentProcessNums;
$dpCreateArr->dpd_amount = bcmul($updatedItem['create']['si_discount'], $currentProcessNums, 2);
$dpCreateArr->si_id = $updatedItem['create']['si_id'];
$dpId = $dbConnect->table('dpdetail')->insertGetId((array)$dpCreateArr);
}
} }
} }
//预存款和退款处理
$this->cancelAndTransPrePay($dbConnect, $updatedItem['cancel_si_id'], $refundInfo);
if ($futuresNum > 0) {
$futuresNum = 0;
$currentProcessNums = 0;
if (empty($numArr)) break;
} else {
break;
}
} }
} }
} }
} }
$dbConnect->commit(); $dbConnect->commit();
}catch(\Exception $e){ }catch(\Exception $e){
......
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