function save() { $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : array(); $order_code = isset($_REQUEST['order_code']) ? $_REQUEST['order_code'] : ''; $user_code = isset($_SESSION['user_code']) ? $_SESSION['user_code'] : ''; $bh_order_code = str_replace('SK', 'BH', $order_code); $order_bh = Orders::getOne(array('order_code' => $bh_order_code)); if ($order_bh) { $time = $order_bh['create_time']; $time = date('Y-m-d H:i:s', $time); Templates::Assign('errormsg', "订单已在 {$time} 补过一次货,不能再补货了。"); Templates::Display('error.html'); exit; } $orders = Orders::getOne(array('order_code' => $order_code, 'order_status' => 5), 'id, order_status'); $order_status = isset($orders['order_status']) ? (int) $orders['order_status'] : 0; if (!$orders) { Templates::Assign('errormsg', "订单未找到,请核对该订单能补货 ,订单号: {$order_code}, order_status : {$order_status}"); Templates::Display('error.html'); exit; } DB::Debug(); DB::tranBegin('replen'); //事务开始 Orders::createBHOrder($order_code); //生成补货订单 foreach ($ids as $id) { $replen = OrdersBH::getData($id); $qt = $replen['num']; $sku = $replen['sku']; $status = ProductsStock::outStockInsertOrderProduct($bh_order_code, $sku, $qt); if (!$status) { DB::rollBack(); Templates::Assign('errormsg', "补货保存失败,[{$sku}] 库存不足,错误代码 BH001"); Templates::Display('error.html'); exit; } OrdersBH::update($id, array('status' => 1)); } $commit_status = DB::tranEnd('replen'); header("Location: /order/?order_code={$order_code}"); }
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 outStockInsertOrderProduct($order_code, $sku, $qt) { //DB::Debug(); $stocknum = ProductsStock::getSkuStock($sku); //检查库存是否充足 if ($stocknum < $qt) { return false; } DB::tranBegin('outStockInsertOrderProduct'); //事务开始 $stock_ids = ProductsStock::getSkuOutStock($sku, $qt); //查询库存中的批号 if (!$stock_ids) { return false; } foreach ($stock_ids as $sid => $stocks) { $exp_date = isset($stocks['exp_date']) ? (int) $stocks['exp_date'] : 0; $stock = isset($stocks['stock']) ? (int) $stocks['stock'] : 0; if ($stock < 1) { continue; } ProductsStock::reduce("id='{$sid}'", 'stock', $stock); //从库存和批号中减去 OrdersProducts::insert(array('order_code' => $order_code, 'product_sku' => $sku, 'product_num' => $stock, 'exp_date' => $exp_date)); } $commit_status = DB::tranEnd('outStockInsertOrderProduct'); return $commit_status; }
if (!$sku) { header("Location: /html/404.html?004"); exit; } $item_code = $sku['item']; $item = ProductsItem::getOne(array('item' => $item_code, 'status' => 1)); if (!$item) { header("Location: /html/404.html?005"); exit; } $sku['iteminfo'] = $item; $sku_code = $sku['sku']; $pics = ProductsPic::getSkuPics($sku_code, 'path', 'sort DESC LIMIT 0,3'); $sku['pics'] = $pics; $sku['order_num'] = OrdersProducts::getSkuAllNum($sku_code); $sku['stock'] = ProductsStock::getSkuStock($sku_code); $skus = ProductsSku::getItemSkus($item_code, "id,sku,sku_name"); $skus = ProductsSku::getProductsPic($skus); $sku['skus'] = $skus; $userid = isset($_SESSION['userid']) ? (int) $_SESSION['userid'] : 0; $history = array(); if ($userid) { $history = UsersHistory::getProductViewLog($userid, $id); } if (!$history) { $history = ProductsSku::getRand($item_code); } $history = ProductsSku::getProductsPic($history); $history = ProductsSku::getProductsItemTitle($history); ProductsSku::increase("id={$id}", 'view_num'); $log = array('op' => 'view', 'id' => $id, 'url' => Func::getCurrentURL());
//先把状态改成 订单取消,回滚完库存把状态改成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'); }
$title = "合并| {$len} 笔订单"; } $newtotal = $total * $discount; $address = ReceiverAddress::getData($addressid); unset($address['id']); $address['module_id'] = 3; //---DB::Debug(); DB::tranBegin('pay'); //事务开始 $address_id = ReceiverAddress::insert($address); $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) {