function getordcode() { $Ord = M('Orderlist'); $numbers = range(10, 99); shuffle($numbers); $code = array_slice($numbers, 0, 4); $ordcode = $code[0] . $code[1] . $code[2] . $code[3]; $oldcode = $Ord->where("ordcode='" . $ordcode . "'")->getField('ordcode'); if ($oldcode) { getordcode(); } else { return $ordcode; } }
/** * 生成订单 * @param array $manifest 购物清单 * @return array|null */ public function gen_order($manifest) { $this->startTrans(); $order_num = getordcode(); $orders = array(); //查询收货地址 $address = M('address')->alias('addr')->field('addr.name, addr.phone, addr.postcode, addr.address, p.id as province, c.id as city, a.id as area')->join('left join area p on p.id = addr.province')->join('left join area c on c.id = addr.city')->join('left join area a on a.id = addr.area')->where(array('addr.id' => $manifest['address_id']))->find(); foreach ($manifest['items'] as $item) { $products = array(); $item['total'] = 0; //查询产品信息 foreach ($item['goods'] as $g) { switch ($g['type']) { case 'book': //产品类型为资料(分为有图书和没图书的情况) $books = M('book')->where(array('cid' => $g['id'], 'type' => 1))->select(); if (!empty($books)) { //存在图书,并可能存在多本 foreach ($books as $book) { $product = array(); $product['table_id'] = $book['id']; $product['table_name'] = 'book'; $product['title'] = $book['title']; $product['price'] = $book['price']; $product['member_id'] = $book['member_id']; $product['quantity'] = $g['quantity']; $products[] = $product; $item['total'] += $product['price'] * $product['quantity']; } } else { //不寻在图书,则产品为需求本身 $demand = M('demand')->find($g['id']); if (empty($demand)) { $this->rollback(); return false; } $product = array(); $product['table_id'] = $demand['id']; $product['table_name'] = 'demand'; $product['title'] = $demand['description']; $product['price'] = $demand['cost']; $product['member_id'] = $demand['member_id']; $product['quantity'] = $g['quantity']; $products[] = $product; $item['total'] += $product['price'] * $product['quantity']; } break; case 'bid': break; } } $group_num = getordcode(); $order_data = array('order_number' => $order_num, 'group_num' => $group_num, 'from_member_id' => $manifest['uid'], 'to_member_id' => $item['business_id'], 'total' => round($item['total'], 2), 'province' => $address['province'], 'city' => $address['city'], 'area' => $address['area'], 'address' => $address['address'], 'postcode' => $address['postcode'], 'name' => $address['name'], 'phone' => $address['phone'], 'content' => $item['remark'], 'shipping_template_id' => $item['shipping_template_id'], 'shipping' => $item['shipping_price'], 'add_time' => date('Y-m-d H:i:s')); $new_order_id = $this->add($order_data); if (!$new_order_id) { $this->rollback(); return false; } $order_data['id'] = $new_order_id; $orders[] = $order_data; foreach ($products as $product) { //添加订单商品 $product_data[] = array('order_id' => $new_order_id, 'table_name' => $product['table_name'], 'table_id' => $product['table_id'], 'title' => $product['title'], 'price' => $product['price'], 'quantity' => $product['quantity'], 'add_time' => date('Y-m-d H:i:s')); } } $add_product_success = M('order_product')->addAll($product_data); if (!$add_product_success) { $this->rollBack(); return false; } return $this->commit() ? $orders : false; }
public function create_order_get() { if (empty($this->cart)) { //5016购物车没有商品 $this->error(5016); } $id = intval(I('get.id', '')); $m = M(); $map['id'] = $id; $map['member_id'] = $this->uid; $resource = $m->table('address')->where($map)->find(); if (empty($resource)) { //5015地址不存在 $this->error(5015); } $order_number = getordcode(); $total = 0; while (list($business, $commodity) = each($this->cart)) { list($id, $commodity) = each($commodity); $total += $commodity['price']; $pro_arr = explode('_', $id); $order_product[] = array('order_id' => $order_number, 'table_name' => $pro_arr[0], 'table_id' => $pro_arr[1], 'title' => $commodity['name'], 'price' => $commodity['price'], 'quantity' => $commodity['num'], 'status' => 1, 'add_time' => date('Y-m-d H:i:s')); } p($order_product); $m->table('order_product')->addAll($order_product); $this->success(); }