Commit 0c25be75 authored by hangjun83's avatar hangjun83

罗恩工具类更新

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