TransportModel.class.php 6.3 KB

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