public static function add($order_code, $product_sku, $product_num) { if (!$order_code || !$product_sku || !$product_num) { return false; } $stock_ids = ProductsStock::getSkuOutStock($product_sku, $product_num); if (!$stock_ids) { return false; } DB::tranBegin(); //事务开始 foreach ($stock_ids as $id => $stocks) { $exp_date = isset($stocks['exp_date']) ? (int) $stocks['exp_date'] : 0; $stock = isset($stocks['stock']) ? (int) $stocks['stock'] : 0; ProductsStockLock::insert(array('order_code' => $order_code, 'product_sku' => $product_sku, 'product_num' => $stock, 'exp_date' => $exp_date, 'create_time' => time(), 'status' => 0)); ProductsStock::reduce("id='{$id}'", 'stock', $stock); } $commit_status = DB::tranEnd(); return $commit_status; }
public static function updateOrderProductStock($order_code) { if (!$order_code) { return false; } $stocklocks = ProductsStockLock::getList("order_code='{$order_code}'", '*', 'id ASC'); if (!$stocklocks) { return false; } foreach ($stocklocks as $stocklock) { $product_sku = $stocklock['product_sku']; $product_num = $stocklock['product_num']; $exp_date = $stocklock['exp_date']; $orderpro = OrdersProducts::getOne(array("order_code" => $order_code, 'product_sku' => $product_sku), 'id, exp_date'); $id = isset($orderpro['id']) ? (int) $orderpro['id'] : 0; $expdate = isset($orderpro['exp_date']) ? (int) $orderpro['exp_date'] : 0; if ($id && !$expdate) { OrdersProducts::update($id, array('product_num' => $product_num, 'exp_date' => $exp_date)); } else { $data = array('order_code' => $order_code, 'product_sku' => $product_sku, 'product_num' => $product_num, 'exp_date' => $stocklock['exp_date']); OrdersProducts::insert($data); } } }
function orderNotify() { $orders = Orders::publicPayOrderSignVerify(); $order_code = isset($orders['order_code']) ? $orders['order_code'] : ''; $order_id = isset($orders['id']) ? (int) $orders['id'] : 0; $order_status = isset($orders['order_status']) ? (int) $orders['order_status'] : 0; $pay_transaction_id = isset($orders['pay_transaction_id']) ? $orders['pay_transaction_id'] : ''; $paid = isset($orders['paid']) ? $orders['paid'] : 0; $paymethod_code = $orders['paymethod_code']; if (1 == $order_status) { $pay_method = PaymentMethod::getOne(array('code' => $paymethod_code), 'id'); $pay_method = isset($pay_method['id']) ? (int) $pay_method['id'] : 0; $sk_order_code = null; $order_type = substr($order_code, -2, 2); if ('YK' == $order_type) { $sk_order_code = str_replace('YK', 'SK', $order_code); } DB::tranBegin('notify'); //事务开始 Orders::update(array('order_code' => $order_code), array('paid' => $paid, 'order_status' => 2, 'pay_method' => $pay_method, 'pay_transaction_id' => $pay_transaction_id, 'pay_time' => time())); if ($sk_order_code) { Orders::update(array('order_code' => $sk_order_code), array('order_status' => 5)); } ProductsStockLock::changeStatus($order_code, 1); OrdersProducts::updateOrderProductStock($order_code); $commit_status = DB::tranEnd('notify'); if (!$commit_status) { ABase::toJson(1, 'FAIL', $orders); } } ABase::toJson(0, 'SUCCESS', $orders); }
//先把状态改成 订单取消,回滚完库存把状态改成7:订单关闭 ABase::log("Order ids in 1 hour : " . serialize($ids), 'cron_'); $time = time() - 10800; //3小时前的订单库存回收,考虑订单生成后还差一分钟订单失效时用户点击付款,并在付款操作时又等待了2小时 $orders = Orders::getList("create_time<{$time} AND order_status=6", 'id,order_code'); //首先在1个小时内使订单失效 foreach ($orders as $k => $order) { $oid = $order['id']; $order_code = $order['order_code']; $order_type = substr($order_code, -2, 2); if ('SK' != $order_type) { unset($orders[$k]); continue; } //DB::Debug(); $stocks = ProductsStockLock::getList("order_code='{$order_code}' AND status=0"); ABase::log("Stocks in 3 hour order: " . serialize($stocks), 'cron_'); DB::tranBegin('stock_recycle'); foreach ($stocks as $stock) { $sid = $stock['id']; $product_sku = $stock['product_sku']; $product_num = $stock['product_num']; $exp_date = $stock['exp_date']; ProductsStock::increase("product_sku='{$product_sku}' AND exp_date='{$exp_date}'", "stock", $product_num); ProductsStockLock::update($sid, array('status' => 2)); } Orders::update($oid, array('order_status' => 7)); $commit_status = DB::tranEnd('stock_recycle'); } ABase::log("Orders in 3 hour : " . serialize($orders), 'cron_'); //---------- END 过期未支付订单库存回收
public static function cancelOrder($order_code, $user) { $order_type = substr($order_code, -2, 2); if ('SK' != $order_type) { return false; } $order = Orders::getOne(array('order_code' => $order_code), 'id, order_status'); if (1 != $order['order_status']) { return false; } $oid = $order['id']; //DB::Debug(); $stocks = ProductsStockLock::getList("order_code='{$order_code}' AND status=0"); ABase::log("Stocks in cancelOrder: " . serialize($stocks), 'cancel_order_'); DB::tranBegin('Orders.cancelOrder'); //事务开始 OrdersLog::insert(array('order_code' => $order_code, 'ex_status' => '取消订单', 'admin' => $user, 'action' => '订单取消', 'ip' => Func::getIP(), 'create_time' => time())); foreach ($stocks as $stock) { $sid = $stock['id']; $product_sku = $stock['product_sku']; $product_num = $stock['product_num']; $exp_date = $stock['exp_date']; ProductsStock::increase("product_sku='{$product_sku}' AND exp_date='{$exp_date}'", "stock", $product_num); ProductsStockLock::update($sid, array('status' => 2)); } Orders::update($oid, array('order_status' => 7)); $commit_status = DB::tranEnd('Orders.cancelOrder'); }
$data = array('order_code' => $order_code, 'user_code' => $user_code, 'discount' => $discount, 'amount' => $newtotal, 'receiver_address_id' => $address_id, 'create_time' => time()); $order_id = Orders::insert($data); foreach ($sku_ids as $sku_id => $qt) { $product_sku = ProductsSku::getData($sku_id, 'sku'); $product_sku = isset($product_sku['sku']) ? $product_sku['sku'] : ''; $stocknum = ProductsStock::getSkuStock($product_sku); if (!$product_sku || $stocknum < $qt) { DB::rollBack(); Templates::Assign('errormsg', "订单保存失败,[{$product_sku}] 库存不足,错误代码 PY001"); Templates::Display('error.html'); exit; } $data = array('order_code' => $order_code, 'product_sku' => $product_sku, 'product_num' => $qt); OrdersProducts::insert($data); Cart::clearCart($userid, $sku_id); $locked = ProductsStockLock::add($order_code, $product_sku, $qt); if (!$locked) { break; } } if (!$locked) { DB::tranStop(); } $commit_status = DB::tranEnd('pay'); //--- end tran if (!$order_id || !$commit_status) { Templates::Assign('errormsg', "订单保存失败,[{$product_sku}] 库存不足,错误代码 PAY002"); Templates::Display('error.html'); exit; } } else {