transport.model.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * 运费模板
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class transportModel extends Model
  11. {
  12. public function __construct(){
  13. parent::__construct();
  14. }
  15. /**
  16. * 增加运费模板
  17. *
  18. * @param unknown_type $data
  19. * @return unknown
  20. */
  21. public function addTransport($data){
  22. return $this->table('transport')->insert($data);
  23. }
  24. /**
  25. * 增加各地区详细运费设置
  26. *
  27. * @param unknown_type $data
  28. * @return unknown
  29. */
  30. public function addExtend($data){
  31. return $this->table('transport_extend')->insertAll($data);
  32. }
  33. /**
  34. * 取得一条运费模板信息
  35. *
  36. * @return unknown
  37. */
  38. public function getTransportInfo($condition){
  39. return $this->table('transport')->where($condition)->find();
  40. }
  41. /**
  42. * 取得一条运费模板扩展信息
  43. *
  44. * @return unknown
  45. */
  46. public function getExtendInfo($condition){
  47. return $this->table('transport_extend')->where($condition)->select();
  48. }
  49. /**
  50. * 删除运费模板
  51. *
  52. * @param unknown_type $id
  53. * @return unknown
  54. */
  55. public function delTansport($condition){
  56. try {
  57. $trans = new trans_wapper($this,__METHOD__);
  58. $delete = $this->table('transport')->where($condition)->delete();
  59. if ($delete) {
  60. $delete = $this->table('transport_extend')->where(array('transport_id'=>$condition['id']))->delete();
  61. }
  62. if (!$delete) throw new Exception();
  63. $trans->commit();
  64. }catch (Exception $e){
  65. $trans->rollback();
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * 删除运费模板扩展信息
  72. *
  73. * @param unknown_type $transport_id
  74. * @return unknown
  75. */
  76. public function delExtend($transport_id){
  77. return $this->table('transport_extend')->where(array('transport_id'=>$transport_id))->delete();
  78. }
  79. /**
  80. * 取得运费模板列表
  81. *
  82. * @param unknown_type $condition
  83. * @param unknown_type $page
  84. * @param unknown_type $order
  85. * @return unknown
  86. */
  87. public function getTransportList($condition=array(), $pagesize = '', $order = 'id desc'){
  88. return $this->table('transport')->where($condition)->order($order)->page($pagesize)->select();
  89. }
  90. /**
  91. * 取得扩展信息列表
  92. *
  93. * @param unknown_type $condition
  94. * @param unknown_type $order
  95. * @return unknown
  96. */
  97. public function getExtendList($condition=array(), $order='is_default'){
  98. return $this->table('transport_extend')->where($condition)->order($order)->select();
  99. }
  100. public function transUpdate($data){
  101. return $this->table('transport')->where($condition)->update($data);
  102. }
  103. /**
  104. * 检测运费模板是否正在被使用
  105. *
  106. */
  107. public function isUsing($id){
  108. if (!is_numeric($id)) return false;
  109. $goods_info = $this->table('goods')->where(array('transport_id'=>$id))->field('goods_id')->find();
  110. return $goods_info ? true : false;
  111. }
  112. /**
  113. * 计算某地区某运费模板ID下的商品总运费,如果运费模板不存在或,按免运费处理
  114. *
  115. * @param int $transport_id
  116. * @param int $buy_num
  117. * @param int $area_id
  118. * @return number/boolean
  119. */
  120. public function calc_transport($transport_id, $buy_num, $area_id) {
  121. if (empty($transport_id) || empty($buy_num) || empty($area_id)) return 0;
  122. $extend_list = $this->getExtendList(array('transport_id'=>$transport_id));
  123. if (empty($extend_list)) {
  124. return 0;
  125. } else {
  126. return $this->calc_unit($area_id,$buy_num,$extend_list);
  127. }
  128. }
  129. /**
  130. * 计算某个具单元的运费
  131. *
  132. * @param 配送地区 $area_id
  133. * @param 购买数量 $num
  134. * @param 运费模板内容 $extend
  135. * @return number 总运费
  136. */
  137. private function calc_unit($area_id, $num, $extend)
  138. {
  139. if (!empty($extend) && is_array($extend))
  140. {
  141. foreach ($extend as $v)
  142. {
  143. if (strpos($v['area_id'],",".$area_id.",") !== false)
  144. {
  145. if ($num <= $v['snum']){
  146. //在首件数量范围内
  147. $calc_total = $v['sprice'];
  148. }else{
  149. //超出首件数量范围,需要计算续件
  150. $calc_total = sprintf('%.2f',($v['sprice'] + ceil(($num-$v['snum'])/$v['xnum'])*$v['xprice']));
  151. }
  152. }
  153. if ($v['is_default']==1)
  154. {
  155. if ($num <= $v['snum']){
  156. //在首件数量范围内
  157. $calc_default_total = $v['sprice'];
  158. }else{
  159. //超出首件数量范围,需要计算续件
  160. $calc_default_total = sprintf('%.2f',($v['sprice'] + ceil(($num-$v['snum'])/$v['xnum'])*$v['xprice']));
  161. }
  162. }
  163. }
  164. //如果运费模板中没有指定该地区,取默认运费
  165. if (!isset($calc_total) && isset($calc_default_total)){
  166. $calc_total = $calc_default_total;
  167. }
  168. }
  169. return $calc_total;
  170. }
  171. }
  172. ?>