Express.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\express;
  13. use app\model\BaseModel;
  14. /**
  15. * 物流配送
  16. */
  17. class Express extends BaseModel
  18. {
  19. const express_type = [
  20. 'express'=>["name" => "express", "title" => "物流配送"],
  21. 'store'=>["name" => "store", "title" => "门店自提"],
  22. 'local'=>["name" => "local", "title" => "外卖配送"],
  23. ];
  24. /**
  25. * 计算费用
  26. * @param array $shop_goods
  27. * @param array $data
  28. */
  29. public function calculate($shop_goods, $data)
  30. {
  31. //模板分组
  32. $template_array = [];
  33. foreach ($shop_goods['goods_list'] as $k => $v)
  34. {
  35. if($v['is_free_shipping'] == 1)
  36. {
  37. continue;
  38. }
  39. if(isset($template_array[$v['shipping_template']]))
  40. {
  41. $template_array[$v['shipping_template']] = [
  42. 'num' => $template_array[$v['shipping_template']]['num'] + $v['num'],
  43. 'weight' => $template_array[$v['shipping_template']]['weight'] + $v['weight']*$v['num'],
  44. 'volume' => $template_array[$v['shipping_template']]['volume'] + $v['volume']*$v['num'],
  45. ];
  46. }else{
  47. $template_array[$v['shipping_template']] = [
  48. 'num' => $v['num'],
  49. 'weight' => $v['weight']*$v['num'],
  50. 'volume' => $v['volume']*$v['num'],
  51. ];
  52. }
  53. }
  54. $express_template = new ExpressTemplate();
  55. $price = 0;
  56. foreach ($template_array as $k_template => $v_template)
  57. {
  58. if($k_template == 0)
  59. {
  60. //默认模板
  61. $template_info = $express_template->getDefaultTemplate($shop_goods['site_id']);
  62. }else{
  63. //默认模板
  64. $template_info = $express_template->getExpressTemplateInfo($k_template, $shop_goods['site_id']);
  65. }
  66. //判断模板是否配置完善
  67. if(empty($template_info["data"]))
  68. {
  69. // continue;
  70. return $this->error([], "TEMPLATE_EMPTY");
  71. }
  72. $template_info = $template_info["data"];
  73. //开始计算
  74. $is_exist_template = false;
  75. foreach ($template_info['template_item'] as $k_item => $v_item)
  76. {
  77. if(strpos($v_item['area_ids'] , '"'.$data['member_address']['district_id'].'"') !== false)
  78. {
  79. $is_exist_template = true;
  80. //运算方式
  81. switch($template_info['fee_type'])
  82. {
  83. case 1:
  84. $tag = $v_template['weight'];
  85. break;
  86. case 2:
  87. $tag = $v_template['volume'];
  88. break;
  89. case 3:
  90. $tag = $v_template['num'];
  91. break;
  92. default:
  93. break;
  94. }
  95. //开始计算
  96. if ($tag <= $v_item['snum']) {
  97. $price += $v_item['sprice'];
  98. } else {
  99. $ext_tag = $tag - $v_item['snum'];
  100. if ($v_item['xnum'] == 0) {
  101. $v_item['xnum'] = 1;
  102. }
  103. if (($ext_tag * 100) % ($v_item['xnum'] * 100) == 0) {
  104. $ext_data = $ext_tag / $v_item['xnum'];
  105. } else {
  106. $ext_data = floor($ext_tag / $v_item['xnum']) + 1;
  107. }
  108. $price += $v_item['sprice'] + $ext_data * $v_item['xprice'];
  109. }
  110. break;
  111. }
  112. }
  113. if($is_exist_template == false){
  114. return $this->error('', "TEMPLATE_AREA_EXIST");
  115. }
  116. }
  117. return $this->success(["delivery_fee" => $price]);
  118. }
  119. }