TransportModel.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Seller\Model;
  15. class TransportModel {
  16. public function __construct()
  17. {
  18. }
  19. /**
  20. * 计算某地区某运费模板ID下的商品总运费,如果运费模板不存在或,按免运费处理
  21. *
  22. * @param int $transport_id 运费模版id
  23. * @param int $quantity 商品件数
  24. * @param int $buy_num 商品重量
  25. * @param int $area_id 地区id
  26. * @return number/boolean
  27. */
  28. public function calc_transport($transport_id, $quantity, $buy_num, $area_id) {
  29. //$good['transport_id'], $good['quantity'], $address
  30. if (empty($transport_id) || empty($area_id)) return 0;
  31. $extend_list = M('lionfish_comshop_shipping')->where( array('id' => $transport_id ) )->select();
  32. // lionfish_comshop_shipping
  33. if (empty($extend_list)) {
  34. return 0;
  35. } else {
  36. return $this->calc_unit($area_id,$quantity, $buy_num,$extend_list);
  37. }
  38. }
  39. /**
  40. * 计算某个具单元的运费
  41. *
  42. * @param 配送地区 $area_id
  43. * @param 购买数量 $quantity
  44. * @param 购买重量 $weight
  45. * @param 运费模板内容 $extend
  46. * @return number 总运费
  47. ($area_id,$quantity, $buy_num,$extend_list);
  48. */
  49. private function calc_unit($area_id, $quantity, $weight, $extend){
  50. $area_info = M('lionfish_comshop_area')->where( array('id' => $area_id ) )->find();
  51. if (!empty($extend) && is_array($extend)){
  52. $calc_total=array(
  53. 'error'=>'该地区不配送!!'
  54. );
  55. $defult_extend = array();
  56. foreach ($extend as $v) {
  57. /**
  58. * strpos函数返回字符串在另一个字符串中第一次出现的位置,没有该字符返回false
  59. * 参数1,字符串
  60. * 参数2,要查找的字符
  61. */
  62. $area_price = unserialize($v['areas']);
  63. if( !empty($area_info['code']) && !empty($area_price['citys_code']) && in_array($area_info['code'], $area_price['citys_code']) )
  64. {
  65. unset($calc_total['error']);
  66. $frist = $area_price['frist'];
  67. $frist_price = $area_price['frist_price'];
  68. $second = $area_price['second'];
  69. $second_price = $area_price['second_price'];
  70. //按照重量
  71. if($v['type'] == 1)
  72. {
  73. if ($weight <= $frist){
  74. //在首重数量范围内
  75. $calc_total['price'] = $frist_price;
  76. }else{
  77. //超出首重数量范围,需要计算续重
  78. $calc_total['price'] = sprintf('%.2f',($frist_price + ceil(($weight-$frist)/$second)*$second_price));
  79. }
  80. return $calc_total['price'];
  81. }else if($v['type'] == 2){
  82. //按照件数 firstnum firstnumprice secondnum secondnumprice
  83. if ($quantity <= $frist){
  84. //在首重数量范围内
  85. $calc_total['price'] = $frist_price;
  86. }else{
  87. //超出首重数量范围,需要计算续重
  88. $calc_total['price'] = sprintf('%.2f',($frist_price + ceil(($quantity-$frist)/$second)*$second_price));
  89. }
  90. return $calc_total['price'];
  91. }
  92. }else{
  93. //使用默认的
  94. unset($calc_total['error']);
  95. //按照重量
  96. if($v['type'] == 1)
  97. {
  98. if ($weight <= $v['firstweight']){
  99. //在首重数量范围内
  100. $calc_total['price'] = $v['firstprice'];
  101. }else{
  102. //超出首重数量范围,需要计算续重
  103. $calc_total['price'] = sprintf('%.2f',($v['firstprice'] + ceil(($weight-$v['firstweight'])/$v['secondweight'])*$v['secondprice']));
  104. }
  105. return $calc_total['price'];
  106. }else if($v['type'] == 2){
  107. //按照件数 firstnum firstnumprice secondnum secondnumprice
  108. if ($quantity <= $v['firstnum']){
  109. //在首重数量范围内
  110. $calc_total['price'] = $v['firstnumprice'];
  111. }else{
  112. //超出首重数量范围,需要计算续重
  113. $calc_total['price'] = sprintf('%.2f',($v['firstnumprice'] + ceil(($quantity-$v['firstnum'])/$v['secondnum'])* $v['secondnumprice']));
  114. }
  115. return $calc_total['price'];
  116. }
  117. }
  118. if (strpos($v['area_id'],",".$area_id.",") !== false){
  119. unset($calc_total['error']);
  120. if ($num <= $v['snum']){
  121. //在首重数量范围内
  122. $calc_total['price'] = $v['sprice'];
  123. }else{
  124. //超出首重数量范围,需要计算续重
  125. $calc_total['price'] = sprintf('%.2f',($v['sprice'] + ceil(($num-$v['snum'])/$v['xnum'])*$v['xprice']));
  126. }
  127. return $calc_total['price'];
  128. }
  129. }
  130. return 0;
  131. }
  132. }
  133. }