123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace Scm\Lib;
- use Dever;
- class Unit
- {
- private $default = array(
- 'value' => -1,
- 'name' => '请选择',
- );
- /**
- * 获取单位
- *
- * @return mixed
- */
- public function get_api()
- {
- # 联动总数
- $level_total = 2;
- # 当前联动级别
- $level_num = Dever::input('level_num');
- # 一般为id
- $level_id = Dever::input('level_id');
- # 是否是搜索列表页
- $level_search = Dever::input('level_search');
- $default = $this->default;
- if ($level_id < 0) {
- Dever::alert('error');
- }
- $config = Dever::db('scm/unit')->config['config_type'];
- # 联动
- if ($level_num == 1) {
- $data = array();
- foreach ($config as $k => $v) {
- $d['value'] = $k;
- $d['name'] = $v;
- $data[] = $d;
- }
- } elseif ($level_num == 2) {
- $data = Dever::db('scm/unit')->getAll(array('type' => $level_id));
- }
- if (!$data) {
- Dever::alert('error');
- }
- if ($level_search || $level_num >= 1) {
- array_unshift($data, $default);
- }
- $result['level_total'] = $level_total;
- $result['list'] = $data;
- return $result;
- }
- # 添加新单位
- public function add_api()
- {
- $where['name'] = Dever::input('value');
- if (!$where['name']) {
- Dever::alert('请输入单位名称');
- }
- $info = Dever::db('scm/unit')->find($where);
- if (!$info) {
- $info['id'] = Dever::db('scm/unit')->insert($where);
- }
- return $info;
- }
- # 获取库中的基础单位的总价和总数
- public function getData($goods_id, $sku_id, $unit_id = false, $cash_col = false)
- {
- $where['goods_id'] = $goods_id;
- $where['sku_id'] = $sku_id;
- # 获取当前库存
- # 仓库
- $servicer_store_id = Dever::input('servicer_store_id');
- if ($servicer_store_id) {
- $where['servicer_store_id'] = $servicer_store_id;
- $table = 'scm_servicer/store_goods';
- }
- # 门店
- $seller_shop_id = Dever::input('seller_shop_id');
- if ($seller_shop_id) {
- $where['seller_shop_id'] = $seller_shop_id;
- $table = 'scm_seller/shop_goods';
- }
- $result = array();
- $result['num'] = $result['cash'] = 0;
- if ($table) {
- $list = Dever::db($table)->getUnit($where);
- if ($list) {
- foreach ($list as $k => $v) {
- $base = $this->convertBase($goods_id, $sku_id, $v['unit_id'], $v['total'], $v['cash']);
- if ($base) {
- $result['num'] += $base['num'];
- $result['cash'] += $base['cash'];
- }
- }
- }
- }
- if ($unit_id) {
- if ($cash_col) {
- $result['cash'] = $cash_col;
- }
- $unit = $this->convertUnit($goods_id, $sku_id, $unit_id, $result['num'], $result['cash']);
- if ($unit) {
- $result['num'] = $unit['num'];
- $result['cash'] = $unit['cash'];
- }
- }
- $result['num'] = Dever::number($result['num'], 2);
- $result['cash'] = Dever::number($result['cash'], 2);
- return $result;
- }
- # 将单位转换成基础单位,并获取基础价格和基础数量 unit_id 当前单位,num当前单位数量, cash当前单位价格
- public function convertBase($goods_id, $sku_id, $unit_id, $num, $cash)
- {
- $result = $this->getInfo($goods_id, $sku_id, $unit_id, $num, $cash);
- if ($result) {
- # 基础单位和当前单位不同
- if ($result['unit']) {
- $unit = $result['unit'];
- if ($unit && $unit['discount'] > 0) {
- $result['cash'] = $result['cash'] / $unit['radio'] / $unit['discount'];
- $result['num'] = $num * $unit['radio'];
- }
- }
- $result = Dever::number(array(array('cash', 'num'), $result));
- }
- return $result;
- }
- # 获取当前单位对应基础单位的价格和数量 unit_id 当前单位,base_num基础单位数量, base_cash基础单位价格
- public function convertUnit($goods_id, $sku_id, $unit_id, $base_num, $base_cash)
- {
- $result = $this->getInfo($goods_id, $sku_id, $unit_id, $base_num, $base_cash);
- if ($result) {
- # 基础单位和当前单位不同
- if ($result['unit']) {
- $unit = $result['unit'];
- $result['unit_id'] = $unit['unit_id'];
- if ($unit && $unit['discount'] > 0) {
- $result['cash'] = $result['cash'] * $unit['radio'] * $unit['discount'];
- $result['num'] = round($result['num'] / $unit['radio'], 2);
- }
- }
- $result = Dever::number(array(array('cash', 'num'), $result));
- }
- return $result;
- }
- private function getInfo($goods_id, $sku_id, $unit_id, $num, $cash)
- {
- $goods_info = Dever::load('scm_product/lib/info')->getBaseInfo($goods_id, $sku_id);
- $result = array();
- if ($goods_info) {
- # 获取供应商供货价
- $supplier_id = Dever::input('supplier_id');
- if ($supplier_id) {
- $where['supplier_id'] = $supplier_id;
- $where['goods_id'] = $goods_id;
- $where['sku_id'] = $sku_id;
- $sku = Dever::db('scm_supplier/goods')->getOne($where);
- }
- # 获取经销商采购价
- $seller_id = Dever::input('seller_id');
- if ($seller_id) {
- $seller = Dever::db('scm_seller/info')->find($seller_id);
- if ($seller && $seller['price_id'] > 0) {
- 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']);
- }
- }
- if (!is_numeric($cash)) {
- $cash = isset($sku[$cash]) && $sku[$cash] ? $sku[$cash] : $goods_info[$cash];
- }
- # 当前单位和价格
- $result['unit_id'] = $unit_id;
- $result['num'] = $num;
- $result['cash'] = $cash;
- # 单位基本信息
- $result['unit'] = array();
- if ($unit_id != $goods_info['unit_id']) {
- $result['unit_id'] = $goods_info['unit_id'];
- $result['unit'] = Dever::db('scm_product/info_unit')->find(array('info_id' => $goods_info['id'], 'unit_id' => $unit_id));
- }
- }
- return $result;
- }
- }
|