Unit.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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, $cash_col = false)
  66. {
  67. $where['goods_id'] = $goods_id;
  68. $where['sku_id'] = $sku_id;
  69. # 获取当前库存
  70. # 仓库
  71. $servicer_store_id = Dever::input('servicer_store_id');
  72. if ($servicer_store_id) {
  73. $where['servicer_store_id'] = $servicer_store_id;
  74. $table = 'scm_servicer/store_goods';
  75. }
  76. # 门店
  77. $seller_shop_id = Dever::input('seller_shop_id');
  78. if ($seller_shop_id) {
  79. $where['seller_shop_id'] = $seller_shop_id;
  80. $table = 'scm_seller/shop_goods';
  81. }
  82. $result = array();
  83. $result['num'] = $result['cash'] = 0;
  84. if ($table) {
  85. $list = Dever::db($table)->getUnit($where);
  86. if ($list) {
  87. foreach ($list as $k => $v) {
  88. $base = $this->convertBase($goods_id, $sku_id, $v['unit_id'], $v['total'], $v['cash']);
  89. if ($base) {
  90. $result['num'] += $base['num'];
  91. $result['cash'] += $base['cash'];
  92. }
  93. }
  94. }
  95. }
  96. if ($unit_id) {
  97. if ($cash_col) {
  98. $result['cash'] = $cash_col;
  99. }
  100. $unit = $this->convertUnit($goods_id, $sku_id, $unit_id, $result['num'], $result['cash']);
  101. if ($unit) {
  102. $result['num'] = $unit['num'];
  103. $result['cash'] = $unit['cash'];
  104. }
  105. }
  106. $result['num'] = Dever::number($result['num'], 2);
  107. $result['cash'] = Dever::number($result['cash'], 2);
  108. return $result;
  109. }
  110. # 将单位转换成基础单位,并获取基础价格和基础数量 unit_id 当前单位,num当前单位数量, cash当前单位价格
  111. public function convertBase($goods_id, $sku_id, $unit_id, $num, $cash)
  112. {
  113. $result = $this->getInfo($goods_id, $sku_id, $unit_id, $num, $cash);
  114. if ($result) {
  115. # 基础单位和当前单位不同
  116. if ($result['unit']) {
  117. $unit = $result['unit'];
  118. if ($unit && $unit['discount'] > 0) {
  119. $result['cash'] = $result['cash'] / $unit['radio'] / $unit['discount'];
  120. $result['num'] = $num * $unit['radio'];
  121. }
  122. }
  123. $result = Dever::number(array(array('cash', 'num'), $result));
  124. }
  125. return $result;
  126. }
  127. # 获取当前单位对应基础单位的价格和数量 unit_id 当前单位,base_num基础单位数量, base_cash基础单位价格
  128. public function convertUnit($goods_id, $sku_id, $unit_id, $base_num, $base_cash)
  129. {
  130. $result = $this->getInfo($goods_id, $sku_id, $unit_id, $base_num, $base_cash);
  131. if ($result) {
  132. # 基础单位和当前单位不同
  133. if ($result['unit']) {
  134. $unit = $result['unit'];
  135. $result['unit_id'] = $unit['unit_id'];
  136. if ($unit && $unit['discount'] > 0) {
  137. $result['cash'] = $result['cash'] * $unit['radio'] * $unit['discount'];
  138. $result['num'] = round($result['num'] / $unit['radio'], 2);
  139. }
  140. }
  141. $result = Dever::number(array(array('cash', 'num'), $result));
  142. }
  143. return $result;
  144. }
  145. private function getInfo($goods_id, $sku_id, $unit_id, $num, $cash)
  146. {
  147. $goods_info = Dever::load('scm_product/lib/info')->getBaseInfo($goods_id, $sku_id);
  148. $result = array();
  149. if ($goods_info) {
  150. # 获取供应商供货价
  151. $supplier_id = Dever::input('supplier_id');
  152. if ($supplier_id) {
  153. $where['supplier_id'] = $supplier_id;
  154. $where['goods_id'] = $goods_id;
  155. $where['sku_id'] = $sku_id;
  156. $sku = Dever::db('scm_supplier/goods')->getOne($where);
  157. }
  158. # 获取经销商采购价
  159. $seller_id = Dever::input('seller_id');
  160. if ($seller_id) {
  161. $seller = Dever::db('scm_seller/info')->find($seller_id);
  162. if ($seller && $seller['price_id'] > 0) {
  163. list($sku['price'], $sku['buy_price']) = Dever::load('scm_product/lib/price')->get($seller['price_id'], $goods_id, $sku_id, $goods_info['price'], $goods_info['buy_price']);
  164. }
  165. }
  166. if (!is_numeric($cash)) {
  167. $cash = isset($sku[$cash]) && $sku[$cash] ? $sku[$cash] : $goods_info[$cash];
  168. }
  169. # 当前单位和价格
  170. $result['unit_id'] = $unit_id;
  171. $result['num'] = $num;
  172. $result['cash'] = $cash;
  173. # 单位基本信息
  174. $result['unit'] = array();
  175. if ($unit_id != $goods_info['unit_id']) {
  176. $result['unit_id'] = $goods_info['unit_id'];
  177. $result['unit'] = Dever::db('scm_product/info_unit')->find(array('info_id' => $goods_info['id'], 'unit_id' => $unit_id));
  178. }
  179. }
  180. return $result;
  181. }
  182. }