1
0

delivery.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @copyright (c) 2011 aircheng.com
  4. * @file article.php
  5. * @brief 订单中配送方式的计算
  6. * @author relay
  7. * @date 2011-02-24
  8. * @version 0.6
  9. */
  10. class Delivery
  11. {
  12. //用户ID
  13. public static $user_id = 0;
  14. //首重重量
  15. private static $firstWeight = 0;
  16. //次重重量
  17. private static $secondWeight = 0;
  18. /**
  19. * 根据重量计算给定价格
  20. * @param $weight float 总重量
  21. * @param $firstFee float 首重费用
  22. * @param $second float 次重费用
  23. */
  24. private static function getFeeByWeight($weight,$firstFee,$secondFee)
  25. {
  26. //当商品重量小于或等于首重的时候
  27. if($weight <= self::$firstWeight)
  28. {
  29. return $firstFee;
  30. }
  31. //当商品重量大于首重时,根据次重进行累加计算
  32. $num = ceil(($weight - self::$firstWeight)/self::$secondWeight);
  33. return $firstFee + $secondFee * $num;
  34. }
  35. /**
  36. * @brief 配送方式计算管理模块
  37. * @param $province int 省份的ID
  38. * @param $delivery_id int 配送方式ID
  39. * @param $goods_id array 商品ID
  40. * @param $product_id array 货品ID
  41. * @param $num array 商品数量
  42. * @return array(
  43. * id => 配送方式ID,
  44. * name => 配送方式NAME,
  45. * description => 配送方式描述,
  46. * if_delivery => 0:支持配送;1:不支持配送,
  47. * price => 实际运费总额,
  48. * protect_price => 商品保价总额,
  49. * org_price => 原始运费总额,
  50. * seller_price => array(商家ID => 实际运费),
  51. * seller_protect_price => array(商家ID => 商品保价),
  52. * seller_org_price => array(商家ID => 原始运费),
  53. * )
  54. */
  55. public static function getDelivery($province,$delivery_id,$goods_id,$product_id = 0,$num = 1)
  56. {
  57. //获取默认的配送方式信息
  58. $delivery = new IModel('delivery');
  59. $deliveryDefaultRow = $delivery->getObj('is_delete = 0 and status = 1 and id = '.$delivery_id);
  60. if(!$deliveryDefaultRow)
  61. {
  62. return "配送方式不存在";
  63. }
  64. //最终返回结果
  65. $result = array(
  66. 'id' => $deliveryDefaultRow['id'],
  67. 'name' => $deliveryDefaultRow['name'],
  68. 'description' => $deliveryDefaultRow['description'],
  69. 'if_delivery' => 0,
  70. 'org_price' => 0,
  71. 'price' => 0,
  72. 'protect_price' => 0,
  73. 'seller_org_price' => array(),
  74. 'seller_price' => array(),
  75. 'seller_protect_price' => array(),
  76. );
  77. //读取全部商品,array('goodsSum' => 商品总价,'weight' => 商品总重量)
  78. $sellerGoods = array();
  79. $goods_id = is_array($goods_id) ? $goods_id : array($goods_id);
  80. $product_id = is_array($product_id)? $product_id : array($product_id);
  81. $num = is_array($num) ? $num : array($num);
  82. foreach($goods_id as $key => $gid)
  83. {
  84. $pid = $product_id[$key];
  85. $gnum = $num[$key];
  86. if($pid > 0)
  87. {
  88. $goodsRow = Api::run("getProductInfo",array('#id#',$pid));
  89. if(!$goodsRow)
  90. {
  91. return "计算商品运费货品ID【".$pid."】信息不存在";
  92. }
  93. }
  94. else
  95. {
  96. $goodsRow = Api::run("getGoodsInfo",array('#id#',$gid));
  97. if(!$goodsRow)
  98. {
  99. return "计算商品运费商品ID【".$gid."】信息不存在";
  100. }
  101. }
  102. if(!isset($sellerGoods[$goodsRow['seller_id']]))
  103. {
  104. $sellerGoods[$goodsRow['seller_id']] = array('goodsSum' => 0,'weight' => 0,'isDeliveryFee' => 1);//个别商品免运费但是要看订单中其他商品设置
  105. }
  106. //商品免运费
  107. if(isset($goodsRow['is_delivery_fee']) && $goodsRow['is_delivery_fee'] == 1)
  108. {
  109. $goodsRow['weight'] = 0;
  110. }
  111. else
  112. {
  113. $sellerGoods[$goodsRow['seller_id']]['isDeliveryFee'] = 0;
  114. }
  115. $sellerGoods[$goodsRow['seller_id']]['weight'] += $goodsRow['weight'] * $gnum;
  116. $sellerGoods[$goodsRow['seller_id']]['goodsSum']+= $goodsRow['sell_price'] * $gnum;
  117. }
  118. //根据商家不同计算运费
  119. $deliveryExtendDB = new IModel('delivery_extend');
  120. foreach($sellerGoods as $seller_id => $data)
  121. {
  122. $weight = $data['weight'];//计算运费
  123. $goodsSum = $data['goodsSum'];//计算保价
  124. $isDeliveryFee = $data['isDeliveryFee'];//是否都是免运费商品
  125. //使用商家配置的物流运费
  126. $seller_id = IFilter::act($seller_id,'int');
  127. $deliverySellerRow = $deliveryExtendDB->getObj('delivery_id = '.$delivery_id.' and seller_id = '.$seller_id);
  128. $deliveryRow = $deliverySellerRow ? $deliverySellerRow : $deliveryDefaultRow;
  129. //设置首重和次重
  130. self::$firstWeight = $deliveryRow['first_weight'];
  131. self::$secondWeight = $deliveryRow['second_weight'];
  132. $deliveryRow['if_delivery'] = '0';
  133. //当配送方式是统一配置的时候,不进行区分地区价格
  134. if($deliveryRow['price_type'] == 0)
  135. {
  136. $deliveryRow['price'] = self::getFeeByWeight($weight,$deliveryRow['first_price'],$deliveryRow['second_price']);
  137. }
  138. //当配送方式为指定区域和价格的时候
  139. else
  140. {
  141. $matchKey = '';
  142. $flag = false;
  143. //每项都是以';'隔开的省份ID
  144. $area_groupid = unserialize($deliveryRow['area_groupid']);
  145. if($area_groupid)
  146. {
  147. foreach($area_groupid as $key => $item)
  148. {
  149. //匹配到了特殊的省份运费价格
  150. if(strpos($item,';'.$province.';') !== false)
  151. {
  152. $matchKey = $key;
  153. $flag = true;
  154. break;
  155. }
  156. }
  157. }
  158. //匹配到了特殊的省份运费价格
  159. if($flag)
  160. {
  161. //获取当前省份特殊的运费价格
  162. $firstprice = unserialize($deliveryRow['firstprice']);
  163. $secondprice = unserialize($deliveryRow['secondprice']);
  164. $deliveryRow['price'] = self::getFeeByWeight($weight,$firstprice[$matchKey],$secondprice[$matchKey]);
  165. }
  166. else
  167. {
  168. //判断是否设置默认费用了
  169. if($deliveryRow['open_default'] == 1)
  170. {
  171. $deliveryRow['price'] = self::getFeeByWeight($weight,$deliveryRow['first_price'],$deliveryRow['second_price']);
  172. }
  173. else
  174. {
  175. $deliveryRow['price'] = '0';
  176. $deliveryRow['if_delivery'] = '1';
  177. }
  178. }
  179. }
  180. $deliveryRow['org_price'] = $deliveryRow['price'];
  181. //计算保价
  182. if($deliveryRow['is_save_price'] == 1)
  183. {
  184. $tempProtectPrice = $goodsSum * ($deliveryRow['save_rate'] * 0.01);
  185. $deliveryRow['protect_price'] = ($tempProtectPrice <= $deliveryRow['low_price']) ? $deliveryRow['low_price'] : $tempProtectPrice;
  186. }
  187. else
  188. {
  189. $deliveryRow['protect_price'] = 0;
  190. }
  191. //无法送达
  192. if($deliveryRow['if_delivery'] == 1)
  193. {
  194. $deliveryRow['id'] = $deliveryDefaultRow['id'];
  195. $deliveryRow['name'] = $deliveryDefaultRow['name'];
  196. $deliveryRow['description'] = $deliveryDefaultRow['description'];
  197. return $deliveryRow;
  198. }
  199. //更新最终数据
  200. $result['org_price'] += $deliveryRow['org_price'];
  201. $result['price'] += $isDeliveryFee == 1 ? 0 : $deliveryRow['price'];
  202. $result['protect_price'] += $deliveryRow['protect_price'];
  203. $result['seller_org_price'][$seller_id] = $deliveryRow['org_price'];
  204. $result['seller_price'][$seller_id] = $deliveryRow['price'];
  205. $result['seller_protect_price'][$seller_id] = $deliveryRow['protect_price'];
  206. }
  207. return $result;
  208. }
  209. }