Unit.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace Scm\Lib;
  3. use Dever;
  4. class Unit
  5. {
  6. private $default = array(
  7. 'value' => -1,
  8. 'name' => '请选择',
  9. );
  10. /**
  11. * 获取单位
  12. *
  13. * @return mixed
  14. */
  15. public function get_api()
  16. {
  17. # 联动总数
  18. $level_total = 2;
  19. # 当前联动级别
  20. $level_num = Dever::input('level_num');
  21. # 一般为id
  22. $level_id = Dever::input('level_id');
  23. # 是否是搜索列表页
  24. $level_search = Dever::input('level_search');
  25. $default = $this->default;
  26. if ($level_id < 0) {
  27. Dever::alert('error');
  28. }
  29. $config = Dever::db('scm/unit')->config['config_type'];
  30. # 联动
  31. if ($level_num == 1) {
  32. $data = array();
  33. foreach ($config as $k => $v) {
  34. $d['value'] = $k;
  35. $d['name'] = $v;
  36. $data[] = $d;
  37. }
  38. } elseif ($level_num == 2) {
  39. $data = Dever::db('scm/unit')->getAll(array('type' => $level_id));
  40. }
  41. if (!$data) {
  42. Dever::alert('error');
  43. }
  44. if ($level_search || $level_num >= 1) {
  45. array_unshift($data, $default);
  46. }
  47. $result['level_total'] = $level_total;
  48. $result['list'] = $data;
  49. return $result;
  50. }
  51. # 添加新单位
  52. public function add_api()
  53. {
  54. $where['name'] = Dever::input('value');
  55. if (!$where['name']) {
  56. Dever::alert('请输入单位名称');
  57. }
  58. $info = Dever::db('scm/unit')->find($where);
  59. if (!$info) {
  60. $info['id'] = Dever::db('scm/unit')->insert($where);
  61. }
  62. return $info;
  63. }
  64. # 获取库中的基础单位的总价和总数
  65. public function getData($goods_id, $sku_id, $unit_id = false, $supplier_id = false, $cash_col = false, $table = false, $where = array())
  66. {
  67. $where['goods_id'] = $goods_id;
  68. $where['sku_id'] = $sku_id;
  69. # 仓库
  70. $servicer_store_id = Dever::input('servicer_store_id');
  71. if ($servicer_store_id) {
  72. $where['servicer_store_id'] = $servicer_store_id;
  73. $table = 'scm_servicer/store_goods_list';
  74. }
  75. $result = array();
  76. $result['num'] = $result['cash'] = 0;
  77. if ($table) {
  78. $list = Dever::db($table)->getUnit($where);
  79. if ($list) {
  80. foreach ($list as $k => $v) {
  81. $base = $this->convertBase($goods_id, $sku_id, $v['unit_id'], $v['total'], $v['cash'], $supplier_id);
  82. if ($base) {
  83. $result['num'] += $base['num'];
  84. $result['cash'] += $base['cash'];
  85. }
  86. }
  87. }
  88. }
  89. if ($unit_id) {
  90. if ($cash_col) {
  91. $result['cash'] = $cash_col;
  92. }
  93. $unit = $this->convertUnit($goods_id, $sku_id, $unit_id, $result['num'], $result['cash'], $supplier_id);
  94. if ($unit) {
  95. $result['num'] = $unit['num'];
  96. $result['cash'] = $unit['cash'];
  97. }
  98. }
  99. $result['num'] = Dever::number($result['num'], 2);
  100. $result['cash'] = Dever::number($result['cash'], 2);
  101. return $result;
  102. }
  103. # 将单位转换成基础单位,并获取基础价格和基础数量 unit_id 当前单位,num当前单位数量, cash当前单位价格
  104. public function convertBase($goods_id, $sku_id, $unit_id, $num, $cash, $supplier_id = false)
  105. {
  106. $result = $this->getInfo($goods_id, $sku_id, $unit_id, $num, $cash, $supplier_id);
  107. if ($result) {
  108. # 基础单位和当前单位不同
  109. if ($result['unit']) {
  110. $unit = $result['unit'];
  111. if ($unit && $unit['discount'] > 0) {
  112. $result['cash'] = $result['cash'] / $unit['radio'] / $unit['discount'];
  113. $result['num'] = $num * $unit['radio'];
  114. }
  115. }
  116. $result = Dever::number(array(array('cash', 'num'), $result));
  117. }
  118. return $result;
  119. }
  120. # 获取当前单位对应基础单位的价格和数量 unit_id 当前单位,base_num基础单位数量, base_cash基础单位价格
  121. public function convertUnit($goods_id, $sku_id, $unit_id, $base_num, $base_cash, $supplier_id = false)
  122. {
  123. $result = $this->getInfo($goods_id, $sku_id, $unit_id, $base_num, $base_cash, $supplier_id);
  124. if ($result) {
  125. # 基础单位和当前单位不同
  126. if ($result['unit']) {
  127. $unit = $result['unit'];
  128. $result['unit_id'] = $unit['unit_id'];
  129. if ($unit && $unit['discount'] > 0) {
  130. $result['cash'] = $result['cash'] * $unit['radio'] * $unit['discount'];
  131. $result['num'] = round($result['num'] / $unit['radio'], 2);
  132. }
  133. }
  134. $result = Dever::number(array(array('cash', 'num'), $result));
  135. }
  136. return $result;
  137. }
  138. private function getInfo($goods_id, $sku_id, $unit_id, $num, $cash, $supplier_id = false)
  139. {
  140. $goods_info = Dever::load('scm_product/lib/info')->getBaseInfo($goods_id, $sku_id);
  141. $result = array();
  142. if ($goods_info) {
  143. # 获取供应商供货价
  144. if ($supplier_id) {
  145. $where['supplier_id'] = $supplier_id;
  146. $where['goods_id'] = $goods_id;
  147. $where['sku_id'] = $sku_id;
  148. $sku = Dever::db('scm_supplier/goods')->getOne($where);
  149. }
  150. if (!is_numeric($cash)) {
  151. $cash = isset($sku[$cash]) && $sku[$cash] ? $sku[$cash] : $goods_info[$cash];
  152. }
  153. # 当前单位和价格
  154. $result['unit_id'] = $unit_id;
  155. $result['num'] = $num;
  156. $result['cash'] = $cash;
  157. # 单位基本信息
  158. $result['unit'] = array();
  159. if ($unit_id != $goods_info['unit_id']) {
  160. $result['unit_id'] = $goods_info['unit_id'];
  161. $result['unit'] = Dever::db('scm_product/info_unit')->find(array('info_id' => $goods_info['id'], 'unit_id' => $unit_id));
  162. }
  163. }
  164. return $result;
  165. }
  166. }