123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace Scm\Lib;
- use Dever;
- class Unit
- {
- private $default = array(
- 'value' => -1,
- 'name' => '请选择',
- );
-
- public function get_api()
- {
-
- $level_total = 2;
-
- $level_num = Dever::input('level_num');
-
- $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;
- }
-
- 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;
- }
-
- 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;
- }
- }
|