Пример #1
0
 /**
  * 生成订单
  * @param array $input
  * @throws Exception
  * @return array array(支付单sn,订单列表)
  */
 private function _createOrderStep4()
 {
     extract($this->_order_data);
     $member_id = $this->_member_info['member_id'];
     $member_name = $this->_member_info['member_name'];
     $member_email = $this->_member_info['member_email'];
     $model_order = Model('order');
     //存储生成的订单数据
     $order_list = array();
     //存储通知信息
     $notice_list = array();
     //每个店铺订单是货到付款还是线上支付,店铺ID=>付款方式[在线支付/货到付款]
     $store_pay_type_list = $this->_logic_buy_1->getStorePayTypeList(array_keys($store_cart_list), $input_if_offpay, $input_pay_name);
     foreach ($store_pay_type_list as $k => &$v) {
         if (empty($input_if_offpay_batch[$k])) {
             $v = 'online';
         }
     }
     $pay_sn = $this->_logic_buy_1->makePaySn($member_id);
     $order_pay = array();
     $order_pay['pay_sn'] = $pay_sn;
     $order_pay['buyer_id'] = $member_id;
     $order_pay_id = $model_order->addOrderPay($order_pay);
     if (!$order_pay_id) {
         throw new Exception('订单保存失败[未生成支付单]');
     }
     //收货人信息
     list($reciver_info, $reciver_name) = $this->_logic_buy_1->getReciverAddr($input_address_info);
     foreach ($store_cart_list as $store_id => $goods_list) {
         //取得本店优惠额度(后面用来计算每件商品实际支付金额,结算需要)
         $promotion_total = !empty($store_promotion_total[$store_id]) ? $store_promotion_total[$store_id] : 0;
         //本店总的优惠比例,保留3位小数
         $should_goods_total = $store_final_order_total[$store_id] - $store_freight_total[$store_id] + $promotion_total;
         $promotion_rate = abs(number_format($promotion_total / $should_goods_total, 5));
         if ($promotion_rate <= 1) {
             $promotion_rate = floatval(substr($promotion_rate, 0, 5));
         } else {
             $promotion_rate = 0;
         }
         //每种商品的优惠金额累加保存入 $promotion_sum
         $promotion_sum = 0;
         $order = array();
         $order_common = array();
         $order_goods = array();
         $order['order_sn'] = $this->_logic_buy_1->makeOrderSn($order_pay_id);
         $order['pay_sn'] = $pay_sn;
         $order['store_id'] = $store_id;
         $order['store_name'] = $goods_list[0]['store_name'];
         $order['buyer_id'] = $member_id;
         $order['buyer_name'] = $member_name;
         $order['buyer_email'] = $member_email;
         $order['add_time'] = TIMESTAMP;
         $order['payment_code'] = $store_pay_type_list[$store_id];
         $order['order_state'] = $store_pay_type_list[$store_id] == 'online' ? ORDER_STATE_NEW : ORDER_STATE_PAY;
         $order['order_amount'] = $store_final_order_total[$store_id];
         $order['shipping_fee'] = $store_freight_total[$store_id];
         $order['goods_amount'] = $order['order_amount'] - $order['shipping_fee'];
         $order['order_from'] = $order_from;
         //中山小修改货到付款bug>>>
         if ($order['payment_code'] == "") {
             $order['payment_code'] = "offline";
         }
         //中山小修改货到付款bug<<<
         $order_id = $model_order->addOrder($order);
         if (!$order_id) {
             throw new Exception('订单保存失败[未生成订单数据]');
         }
         $order['order_id'] = $order_id;
         $order_list[$order_id] = $order;
         $order_common['order_id'] = $order_id;
         $order_common['store_id'] = $store_id;
         $order_common['order_message'] = $input_pay_message[$store_id];
         //代金券
         if (isset($input_voucher_list[$store_id])) {
             $order_common['voucher_price'] = $input_voucher_list[$store_id]['voucher_price'];
             $order_common['voucher_code'] = $input_voucher_list[$store_id]['voucher_code'];
         }
         $order_common['reciver_info'] = $reciver_info;
         $order_common['reciver_name'] = $reciver_name;
         $order_common['reciver_city_id'] = $input_city_id;
         //发票信息
         $order_common['invoice_info'] = $this->_logic_buy_1->createInvoiceData($input_invoice_info);
         //保存促销信息
         if (is_array($store_mansong_rule_list[$store_id])) {
             $order_common['promotion_info'] = addslashes($store_mansong_rule_list[$store_id]['desc']);
         }
         $order_id = $model_order->addOrderCommon($order_common);
         if (!$order_id) {
             throw new Exception('订单保存失败[未生成订单扩展数据]');
         }
         //生成order_goods订单商品数据
         $i = 0;
         foreach ($goods_list as $goods_info) {
             if (!$goods_info['state'] || !$goods_info['storage_state']) {
                 throw new Exception('部分商品已经下架或库存不足,请重新选择');
             }
             if (!intval($goods_info['bl_id'])) {
                 //如果不是优惠套装
                 $order_goods[$i]['order_id'] = $order_id;
                 $order_goods[$i]['goods_id'] = $goods_info['goods_id'];
                 $order_goods[$i]['store_id'] = $store_id;
                 $order_goods[$i]['goods_name'] = $goods_info['goods_name'];
                 $order_goods[$i]['goods_price'] = $goods_info['goods_price'];
                 $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
                 $order_goods[$i]['goods_image'] = $goods_info['goods_image'];
                 $order_goods[$i]['buyer_id'] = $member_id;
                 if ($goods_info['ifgroupbuy']) {
                     $ifgroupbuy = true;
                     $order_goods[$i]['goods_type'] = 2;
                 } elseif ($goods_info['ifxianshi']) {
                     $order_goods[$i]['goods_type'] = 3;
                 } elseif ($goods_info['ifzengpin']) {
                     $order_goods[$i]['goods_type'] = 5;
                 } else {
                     $order_goods[$i]['goods_type'] = 1;
                 }
                 $order_goods[$i]['promotions_id'] = $goods_info['promotions_id'] ? $goods_info['promotions_id'] : 0;
                 //zmr>>>
                 $order_goods[$i]['commis_rate'] = floatval($store_gc_id_commis_rate_list[$store_id][$goods_info['gc_id']]);
                 //zmr<<<
                 $order_goods[$i]['gc_id'] = $goods_info['gc_id'];
                 //计算商品金额
                 $goods_total = $goods_info['goods_price'] * $goods_info['goods_num'];
                 //计算本件商品优惠金额
                 $promotion_value = floor($goods_total * $promotion_rate);
                 $order_goods[$i]['goods_pay_price'] = $goods_total - $promotion_value;
                 $promotion_sum += $promotion_value;
                 $i++;
                 //存储库存报警数据
                 if ($goods_info['goods_storage_alarm'] >= $goods_info['goods_storage'] - $goods_info['goods_num']) {
                     $param = array();
                     $param['common_id'] = $goods_info['goods_commonid'];
                     $param['sku_id'] = $goods_info['goods_id'];
                     $notice_list['goods_storage_alarm'][$goods_info['store_id']] = $param;
                 }
             } elseif (!empty($goods_info['bl_goods_list']) && is_array($goods_info['bl_goods_list'])) {
                 //优惠套装
                 foreach ($goods_info['bl_goods_list'] as $bl_goods_info) {
                     $order_goods[$i]['order_id'] = $order_id;
                     $order_goods[$i]['goods_id'] = $bl_goods_info['goods_id'];
                     $order_goods[$i]['store_id'] = $store_id;
                     $order_goods[$i]['goods_name'] = $bl_goods_info['goods_name'];
                     $order_goods[$i]['goods_price'] = $bl_goods_info['bl_goods_price'];
                     $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
                     $order_goods[$i]['goods_image'] = $bl_goods_info['goods_image'];
                     $order_goods[$i]['buyer_id'] = $member_id;
                     $order_goods[$i]['goods_type'] = 4;
                     $order_goods[$i]['promotions_id'] = $bl_goods_info['bl_id'];
                     $order_goods[$i]['commis_rate'] = floatval($store_gc_id_commis_rate_list[$store_id][$goods_info['gc_id']]);
                     $order_goods[$i]['gc_id'] = $bl_goods_info['gc_id'];
                     //计算商品实际支付金额(goods_price减去分摊优惠金额后的值)
                     $goods_total = $bl_goods_info['bl_goods_price'] * $goods_info['goods_num'];
                     //计算本件商品优惠金额
                     $promotion_value = floor($goods_total * $promotion_rate);
                     $order_goods[$i]['goods_pay_price'] = $goods_total - $promotion_value;
                     $promotion_sum += $promotion_value;
                     $i++;
                     //存储库存报警数据
                     if ($bl_goods_info['goods_storage_alarm'] >= $bl_goods_info['goods_storage'] - $goods_info['goods_num']) {
                         $param = array();
                         $param['common_id'] = $bl_goods_info['goods_commonid'];
                         $param['sku_id'] = $bl_goods_info['goods_id'];
                         $notice_list['goods_storage_alarm'][$bl_goods_info['store_id']] = $param;
                     }
                 }
             }
         }
         //将因舍出小数部分出现的差值补到最后一个商品的实际成交价中(商品goods_price=0时不给补,可能是赠品)
         if ($promotion_total > $promotion_sum) {
             $i--;
             for ($i; $i >= 0; $i--) {
                 if (floatval($order_goods[$i]['goods_price']) > 0) {
                     $order_goods[$i]['goods_pay_price'] -= $promotion_total - $promotion_sum;
                     break;
                 }
             }
         }
         $insert = $model_order->addOrderGoods($order_goods);
         if (!$insert) {
             throw new Exception('订单保存失败[未生成商品数据]');
         }
         //存储商家发货提醒数据
         if ($store_pay_type_list[$store_id] == 'offline') {
             $notice_list['new_order'][$order['store_id']] = array('order_sn' => $order['order_sn']);
         }
     }
     //保存数据
     $this->_order_data['pay_sn'] = $pay_sn;
     $this->_order_data['order_list'] = $order_list;
     $this->_order_data['notice_list'] = $notice_list;
     $this->_order_data['ifgroupbuy'] = $ifgroupbuy;
 }