|
|
|
@ -26,64 +26,59 @@ class OrderService |
|
|
|
|
*/ |
|
|
|
|
public function productPayRecord($warehouse_id, $time = 0){ |
|
|
|
|
$time = $time ? $time : time(); |
|
|
|
|
//卖单-待售卖 |
|
|
|
|
$seller_order = Db::name('order') |
|
|
|
|
->field('user_id,sum(pay_amount) as pay_amount') |
|
|
|
|
->whereTime('createtime', 'today') |
|
|
|
|
->where('warehouse_id', $warehouse_id) |
|
|
|
|
->where('order_type', 1) |
|
|
|
|
->where('status', 3) |
|
|
|
|
->group('user_id') |
|
|
|
|
->select(); |
|
|
|
|
//买单-支付 |
|
|
|
|
$buyer_order = Db::name('order') |
|
|
|
|
->field('user_id,sum(pay_amount) as pay_amount') |
|
|
|
|
->whereTime('createtime', 'today') |
|
|
|
|
->where('warehouse_id', $warehouse_id) |
|
|
|
|
->where('order_type', 0) |
|
|
|
|
->where('status', 0) |
|
|
|
|
->group('user_id') |
|
|
|
|
->select(); |
|
|
|
|
$buyer_order = array_column($buyer_order, null, "pay_amount"); |
|
|
|
|
|
|
|
|
|
$order = Db::name('order') |
|
|
|
|
->field('user_id,sum(actual_amount) as pay_amount') |
|
|
|
|
->whereTime('createtime', 'today') |
|
|
|
|
->where('warehouse_id', $warehouse_id) |
|
|
|
|
->whereIn('status', [0,3]) |
|
|
|
|
->group('user_id') |
|
|
|
|
->select(); |
|
|
|
|
|
|
|
|
|
$buyer_order = []; |
|
|
|
|
$seller_order = []; |
|
|
|
|
foreach ($order as $key => $value) { |
|
|
|
|
if ($value['pay_amount'] > 0) { |
|
|
|
|
$seller_order[] = $value; |
|
|
|
|
} elseif ($value['pay_amount'] < 0) { |
|
|
|
|
$buyer_order[] = $value; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
$pay_record = []; |
|
|
|
|
foreach ($seller_order as $seller) { |
|
|
|
|
if (isset($buyer_order[$seller['pay_amount']])) { |
|
|
|
|
$pay_record[] = [ |
|
|
|
|
'payer_id' => $buyer_order[$seller['pay_amount']]['user_id'], |
|
|
|
|
'payee_id' => $seller['user_id'], |
|
|
|
|
'amount' => $seller['pay_amount'], |
|
|
|
|
'createtime' => $time, |
|
|
|
|
'warehouse_id' => $warehouse_id, |
|
|
|
|
'date' => date("Y-m-d", $time), |
|
|
|
|
]; |
|
|
|
|
unset($buyer_order[$seller['pay_amount']]); |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
foreach ($buyer_order as $key => &$buyer) { |
|
|
|
|
if ($seller['pay_amount'] > $buyer['pay_amount']) { |
|
|
|
|
$pay_record[] = [ |
|
|
|
|
'payer_id' => $buyer['user_id'], |
|
|
|
|
'payee_id' => $seller['user_id'], |
|
|
|
|
'amount' => $buyer['pay_amount'], |
|
|
|
|
'createtime' => $time, |
|
|
|
|
'warehouse_id' => $warehouse_id, |
|
|
|
|
'date' => date("Y-m-d", $time), |
|
|
|
|
]; |
|
|
|
|
unset($seller_order[$key]); |
|
|
|
|
} else { |
|
|
|
|
$pay_record[] = [ |
|
|
|
|
'payer_id' => $buyer['user_id'], |
|
|
|
|
'payee_id' => $seller['user_id'], |
|
|
|
|
'amount' => $seller['pay_amount'], |
|
|
|
|
'createtime' => $time, |
|
|
|
|
'warehouse_id' => $warehouse_id, |
|
|
|
|
'date' => date("Y-m-d", $time), |
|
|
|
|
]; |
|
|
|
|
$buyer['pay_amount'] = $buyer['pay_amount'] - $seller['pay_amount']; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if ($seller['pay_amount'] <= 0) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
foreach ($buyer_order as $key => $buyer) { |
|
|
|
|
if ($seller['pay_amount'] >= abs($buyer['pay_amount'])) { |
|
|
|
|
$pay_record[] = [ |
|
|
|
|
'payer_id' => $buyer['user_id'], |
|
|
|
|
'payee_id' => $seller['user_id'], |
|
|
|
|
'amount' => abs($buyer['pay_amount']), |
|
|
|
|
'createtime' => $time, |
|
|
|
|
'warehouse_id' => $warehouse_id, |
|
|
|
|
'date' => date("Y-m-d", $time), |
|
|
|
|
]; |
|
|
|
|
$seller['pay_amount'] = $seller['pay_amount'] + $buyer['pay_amount']; |
|
|
|
|
unset($buyer_order[$key]); |
|
|
|
|
} else { |
|
|
|
|
$pay_record[] = [ |
|
|
|
|
'payer_id' => $buyer['user_id'], |
|
|
|
|
'payee_id' => $seller['user_id'], |
|
|
|
|
'amount' => $seller['pay_amount'], |
|
|
|
|
'createtime' => $time, |
|
|
|
|
'warehouse_id' => $warehouse_id, |
|
|
|
|
'date' => date("Y-m-d", $time), |
|
|
|
|
]; |
|
|
|
|
$buyer['pay_amount'] = $buyer['pay_amount'] + $seller['pay_amount']; |
|
|
|
|
$seller['pay_amount'] = 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
// echo "<pre>"; |
|
|
|
|
// print_r($pay_record); |
|
|
|
|
// exit(); |
|
|
|
|
unset($seller); |
|
|
|
|
$ret = Db::name('income_expend')->insertAll($pay_record); |
|
|
|
|
return $ret; |
|
|
|
|