Commit 40c47280 authored by hangjun83's avatar hangjun83

罗恩工具类更新

parent 36251951
......@@ -340,7 +340,7 @@ class RhawnOrdersService
$pstk_ids[] = $pstkArr[0];
}
//分组查询发货数量
$dpdSum = $dbConnect->table('dpdetail')
/*$dpdSum = $dbConnect->table('dpdetail')
->whereIn('pstk_id', $pstk_ids)
->where('dpd_invoiced', '=', 0)
->groupBy('pstk_id')
......@@ -359,11 +359,11 @@ class RhawnOrdersService
throw new \LogicException('退货数量大于购买数量', 500);
}
}
});
});*/
}
// 如果是期货,强行初始一个数组执行一次
if($futuresNum > 0){
if($futuresNum > 0 && empty($refundNums)){
$refundNums[0] = $futuresNum;
}
......@@ -371,174 +371,202 @@ class RhawnOrdersService
foreach ($refundNums as $pstk_id => $num) {
$currentProcessNums = $num;
while (true) {
// 如果有期货数量优先处理
if ($futuresNum > 0) {
$currentProcessNums = $futuresNum;
}
//修改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();
}
// 判断有期货的时候退货数量是否大于soitem的数量
if($futuresNum > 0 && $futuresNum > $soItem['si_assigned_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'] - $soItem['si_assigned_num'])){
throw new \LogicException('现货退货数量大于已分配数量', 500);
}
if($futuresNum > 0) {
if ($futuresNum > ($soItem['si_num'] - $dpdSum->num)) {
throw new \LogicException('期货退货数量大于可退货数量', 500);
}
}
}
//更新原本数据
$updateArr = [];
$createArr = [];
$updatedItem = [];
// 判断现货退货数量是否大于soitem的数量
if ($num > $soItem['si_num']) {
throw new \LogicException('现货退货数量大于可退货数量', 500);
}
//如果取消的是全部
if (($currentProcessNums - $orderitem['si_num']) == 0) {
// 如果需要手续费
if (isset($refundInfo->handle_fee) && $refundInfo->handle_fee > 0) {
// 如果有期货数量优先处理
if ($futuresNum > 0) {
$currentProcessNums = $futuresNum;
}else{
$currentProcessNums = $num;
}
$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 = [];
$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'];
}
$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 {
} else {
$updatedItem['cancel_si_id'] = $orderitem['si_id'];
}
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'];
}
} // 部分退货
else {
$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 ($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'];
}
// 新增
$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 {
$createArr['si_assigned_num'] = $createArr['si_num'];
}
$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);
if ($createArr['si_amount'] < 0) {
throw new \LogicException('手续费异常,处理失败!', 500);
}
}
$updateArr['si_discount'] = bcdiv($updateArr['si_amount'], $updateArr['si_num'], 2);
// 新增
$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;
if (!empty($updateArr)) {
$update = $service->update($updateArr, $soItem['si_id']);
$updatedItem['update'] = $update->toArray();
} else {
$createArr['si_assigned_num'] = $createArr['si_num'];
$updatedItem['update'] = $orderitem;
}
if ($createArr['si_amount'] < 0) {
throw new \LogicException('手续费异常,处理失败!', 500);
}
}
if (!empty($updateArr)) {
$update = $service->update($updateArr, $soItem['si_id']);
$updatedItem['update'] = $update->toArray();
} else {
$updatedItem['update'] = $orderitem;
}
//判断是否能除尽
if (!empty($updateArr)) {
$amount = bcmul($updateArr['si_discount'], $updateArr['si_num'], 2);
if ($updateArr['si_amount'] != $amount) {
$note[] = 'si_id = ' . $soItem['si_id'] . '的数据有除不尽的情况,请手工处理';
//判断是否能除尽
if (!empty($updateArr)) {
$amount = bcmul($updateArr['si_discount'], $updateArr['si_num'], 2);
if ($updateArr['si_amount'] != $amount) {
$note[] = 'si_id = ' . $soItem['si_id'] . '的数据有除不尽的情况,请手工处理';
}
}
}
if (!empty($createArr)) {
$newItem = $service->create($createArr);
$updatedItem['create'] = $newItem->toArray();
// 如果是部分退货
if ($currentProcessNums > 0 || ($currentProcessNums == 0 && $refundInfo->handle_fee > 0)) {
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'];
}
} else {
$updatedItem['create'] = $soItem;
$updatedItem['cancel_si_id'] = $updatedItem['create']['si_id'];
}
$updatedItem['total'] = $currentAmount;
// 如果没有期货数量,dpdetail只处理现货数量
if ($futuresNum == 0) {
// 获取实际现货数量
$dpdDetail = $dbConnect->table('dpdetail')
->where('si_id', $orderitem['si_id'])
->where('pstk_id', $pstk_id)
->get()->first();
//$dpdDetail = $dpdDetail->toArray();
if($dpdDetail) {
// 更新当前记录
$dpUpdateArr = [];
$dpUpdateArr['dpd_num'] = $dpdDetail->dpd_num - $currentProcessNums;
$dpUpdateArr['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $dpUpdateArr['dpd_num'], 2);
$dbConnect->table('dpdetail')
->where('dpd_id',$dpdDetail->dpd_id)
->update($dpUpdateArr);
// 如果是部分退货
if ($currentProcessNums < $dpdDetail->dpd_num) {
//添加新的取消记录
$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);
$updatedItem['total'] = $currentAmount;
// 如果没有期货数量,dpdetail只处理现货数量
if ($futuresNum == 0) {
// 获取实际现货数量
$dpdDetail = $dbConnect->table('dpdetail')
->where('si_id', $orderitem['si_id'])
->where('pstk_id', $pstk_id)
->get()->first();
//$dpdDetail = $dpdDetail->toArray();
if ($dpdDetail) {
// 更新当前记录
$dpUpdateArr = [];
$dpUpdateArr['dpd_num'] = $dpdDetail->dpd_num - $currentProcessNums;
$dpUpdateArr['dpd_amount'] = bcmul($updatedItem['update']['si_discount'], $dpUpdateArr['dpd_num'], 2);
$dbConnect->table('dpdetail')
->where('dpd_id', $dpdDetail->dpd_id)
->update($dpUpdateArr);
// 如果是部分退货
if ($currentProcessNums < $dpdDetail->dpd_num) {
//添加新的取消记录
$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);
//预存款和退款处理
$this->cancelAndTransPrePay($dbConnect, $updatedItem['cancel_si_id'], $refundInfo);
if ($currentProcessNums > 0) {
if ($futuresNum > 0) {
$futuresNum = 0;
$currentProcessNums = 0;
if(empty($numArr)) break;
} else {
break;
}
$currentProcessNums = 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