Пример #1
0
 public function change_quantity($direction, $rows)
 {
     $exists_map = ['product_unique_id' => ['IN', get_array_by_field($rows, 'product_unique_id')], 'storage_id' => ['IN', get_array_by_field($rows, 'storage_id')]];
     $exists = $this->where($exists_map)->select();
     $exists_keys = [];
     foreach ($exists as $v) {
         array_push($exists_keys, sprintf('%s.%s', $v['storage_id'], $v['product_unique_id']));
     }
     foreach ($rows as $row) {
         $item_key = sprintf('%s.%s', $row['storage_id'], $row['product_unique_id']);
         if (in_array($item_key, $exists_keys)) {
             $map = ['product_unique_id' => $row['product_unique_id'], 'storage_id' => $row['storage_id']];
             $method = $direction === '+' ? 'setInc' : 'setDec';
             $this->where($map)->{$method}('balance', $row['quantity']);
         } else {
             $data = ['balance' => $row['quantity'], 'product_id' => $row['product_id'], 'product_unique_id' => $row['product_unique_id'], 'storage_id' => $row['storage_id']];
             if (false === $this->create($data)) {
                 return false;
             }
             if (!$this->add()) {
                 return false;
             }
         }
     }
     return true;
 }
Пример #2
0
 public function on_list()
 {
     $data = parent::on_list(true);
     $actives = D('Home/CompanyActiveApps')->where(['company_id' => get_current_company_id()])->select();
     $actives = get_array_by_field($actives, 'app_id');
     $cleared_list = [];
     $i = 0;
     foreach ($data[1] as $k => $v) {
         // 根据是否启用状态过滤
         if (false !== $this->filter_only_active_status) {
             if ($this->filter_only_active_status > 0 && !in_array($v['id'], $actives)) {
                 continue;
             }
             if ($this->filter_only_active_status < 1 && in_array($v['id'], $actives)) {
                 continue;
             }
         }
         $cleared_list[$i] = $v;
         if (in_array($v['id'], $actives)) {
             $cleared_list[$i]['is_active'] = true;
         } else {
             $cleared_list[$i]['is_active'] = false;
         }
         $i++;
     }
     $data[1] = $cleared_list;
     $this->response($data, 'app', true);
 }
Пример #3
0
 public function on_list()
 {
     $data = parent::on_list(true);
     $actives = D('Home/CompanyActiveApps')->where(['company_id' => get_current_company_id()])->select();
     $actives = get_array_by_field($actives, 'app_id');
     foreach ($data[1] as $k => $v) {
         if (in_array($v['id'], $actives)) {
             $data[1][$k]['is_active'] = true;
         } else {
             $data[1][$k]['is_active'] = false;
         }
     }
     $this->response($data, 'app', true);
 }
 public function assign_last_progress_to_list($source_data)
 {
     $items_ids = get_array_by_field($source_data, 'id');
     $workflow_ids = get_array_by_field($source_data, 'workflow_id');
     $model = D('Bpm/WorkflowProgress', 'Model');
     $sub_map = ['source_id' => ['IN', $items_ids], 'workflow_id' => ['IN', $workflow_ids]];
     $sub_query = $model->field('id,node_label,node_status_label,source_id,created,workflow_node_id')->where($sub_map)->order('created DESC, id DESC')->buildSql();
     $progress_data = $this->table($sub_query . ' temp')->group('source_id')->select();
     $progress_data = get_array_to_ka($progress_data, 'source_id');
     foreach ($source_data as $k => $v) {
         $source_data[$k]['workflow_node_label'] = $progress_data[$v['id']]['node_label'];
         $source_data[$k]['workflow_node_status_label'] = $progress_data[$v['id']]['node_status_label'] ? $progress_data[$v['id']]['node_status_label'] : $progress_data[$v['id']]['node_label'];
     }
     return $source_data;
 }
Пример #5
0
 public function assign_to($rows, $source_model)
 {
     $row_ids = get_array_by_field($rows, 'id');
     $map = ['source_id' => ['IN', $row_ids], 'source_model' => $source_model];
     $map_model = D('ProductAttribute/ProductAttributeMap', 'Model');
     $mapping = $map_model->where($map)->select();
     $mapping = get_array_to_ka($mapping, 'source_id');
     foreach ($rows as $k => $row) {
         if (array_key_exists($row['id'], $mapping)) {
             $field = $mapping[$row['id']]['attribute_field'];
             $rows[$k][$field] = $mapping[$row['id']]['product_attribute_content_id'];
             $rows[$k][$field . '__label__'] = $mapping[$row['id']]['content'];
         }
     }
     return $rows;
 }
Пример #6
0
 public function on_list()
 {
     $data = parent::on_list(true);
     $list = $data[1];
     $product_ids = get_array_by_field($list, 'product_id');
     $map = ['product_id' => ['IN', $product_ids]];
     $total_balance = D('Storage/Stock')->field('SUM(balance) as total_balance, product_id, product_unique_id')->where($map)->group('product_unique_id')->select();
     $total_balance = get_array_to_ka($total_balance, 'product_unique_id');
     foreach ($list as $k => $v) {
         if (array_key_exists($v['product_unique_id'], $total_balance)) {
             $list[$k]['total_balance'] = $total_balance[$v['product_unique_id']]['total_balance'];
         }
     }
     $data[1] = reIndex($list);
     $this->response($data, 'stock', true);
 }
Пример #7
0
 public function get_authed_nodes($user_id = null)
 {
     $user_id = $user_id ? $user_id : get_current_user_id();
     $cache_key = 'authed/nodes/' . $user_id;
     $cached = S($cache_key);
     if (!DEBUG && $cached) {
         return $cached;
     }
     $role_map_model = D('Account/AuthUserRole');
     $user_roles = $role_map_model->where(array('user_id' => $user_id))->select();
     $roles = get_array_by_field($user_roles, "auth_role_id");
     if (!$roles) {
         return array();
     }
     $authorize_model = D('Account/Authorize', 'Model');
     $nodes = $authorize_model->where(array('auth_role_id' => array('IN', $roles)))->select();
     $data = get_array_to_ka($nodes, 'id');
     S($cache_key, $data);
     return $data;
 }
Пример #8
0
 public static function by_user($map)
 {
     $uid = get_current_user_id();
     switch ($map['by_user']) {
         case "i_created":
             $map['user_id'] = $uid;
             break;
         case "i_headed":
             $map['head_id'] = $uid;
             break;
         case "sub_created":
             $subs = D('Account/User')->get_all_subordinates();
             $map['user_id'] = ['IN', get_array_by_field($subs, 'id')];
             break;
         case "sub_headed":
             $subs = D('Account/User')->get_all_subordinates();
             $map['head_id'] = ['IN', get_array_by_field($subs, 'id')];
             break;
     }
     unset($map['by_user']);
     return $map;
 }
Пример #9
0
 public function _EM_get_data()
 {
     // 查询时间段,默认为本月
     if (I('get.st') && I('get.et')) {
         $start_time = date('Y-m-d 00:00:00', strtotime(I('get.st')));
         $end_time = date("Y-m-d 23:59:59", strtotime(I('get.et')));
     } else {
         $start_time = date('Y-m-01', CTS);
         $end_time = date("Y-m-d 23:59:59", strtotime("{$start_time} +1 month -1 day"));
     }
     // 排行榜数量
     $limit = I('get.limit') ? I('get.limit') : 10;
     // 排序按照
     if (I('get.sort_by') == 'amount') {
         $list_order = 'total_amount DESC, total_quantity DESC';
     } else {
         $list_order = 'total_quantity DESC, total_amount DESC';
     }
     $order_service = D('Sale/Orders');
     $detail_service = D('Sale/OrdersDetail');
     $product_service = D('Product/Product');
     $map = ['created' => ['BETWEEN', [$start_time, $end_time]], 'status' => OrdersService::STATUS_COMPLETE];
     $orders_in_range = $order_service->where($map)->select();
     $orders_in_range = get_array_by_field($orders_in_range, "id");
     // 所有已完成销售详情
     $all_details = $detail_service->field('SUM(subtotal_amount) AS total_amount, SUM(quantity) AS total_quantity, product_unique_id, product_id')->group('`product_unique_id`')->where(['orders_id' => ['IN', $orders_in_range]])->limit($limit)->order($list_order)->select();
     $product_ids = get_array_by_field($all_details, 'product_id');
     $products = $product_service->where(['id' => ['IN', $product_ids]])->select();
     $products = get_array_to_ka($products, 'id');
     $response_data = ['subtext' => sprintf('%s ~ %s', $start_time, $end_time), 'data' => ['amount' => [], 'quantity' => []], 'xAxis' => []];
     foreach ($all_details as $detail) {
         array_push($response_data['xAxis'], $products[$detail['product_id']]['name']);
         array_push($response_data['data']['amount'], $detail['total_amount'] ? (double) $detail['total_amount'] : 0);
         array_push($response_data['data']['quantity'], $detail['total_quantity'] ? (double) $detail['total_quantity'] : 0);
     }
     $this->response($response_data);
 }
Пример #10
0
 public function edit_bill($id, $meta, $rows)
 {
     if (!$this->check_params()) {
         $this->error = __('common.System Error');
         return false;
     }
     $old_meta = $this->where(['id' => $id])->find();
     // 获得当前数据状态,判断是否可修改
     if (in_array($old_meta['status'], $this->LOCKED_STATUS)) {
         $this->error = __('workflow.Can not be edit this item because the data is in progress');
         return false;
     }
     /*
      * 产品属性
      * */
     if (AppService::is_app_active('productAttribute')) {
         $attribute_active = true;
         $attribute_service = D('ProductAttribute/ProductAttribute');
         // 所有的产品属性字段
         $attribute_fields = $attribute_service->get_attribute_fields();
     }
     // 更新meta信息
     // 统计
     if (!$meta['quantity']) {
         $quantity = 0;
         $row_quantity = get_array_by_field($rows, 'quantity');
         foreach ($row_quantity as $v) {
             $quantity += $v;
         }
         $meta['quantity'] = decimal_scale($quantity);
     }
     foreach ($meta as $k => $v) {
         if (is_array($v)) {
             unset($meta[$k]);
         }
     }
     $this->startTrans();
     if (false === $this->where(['id' => $id])->save($meta)) {
         $this->error = __('storage.Trigger when save meta data');
         $this->rollback();
         return false;
     }
     // 获取已存在数据
     $detail_service = D($this->detail_model);
     $exists_rows = $detail_service->where([$this->detail_main_foreign => $id])->select();
     $exists_rows = get_array_to_ka($exists_rows, 'product_unique_id');
     // 已存在ID数组
     $exists_ids = get_array_by_field($exists_rows, 'id');
     // 修改的ID数组
     $edited_ids = '';
     $rows = $this->merge_same_rows($rows, $attribute_fields);
     /*
      * 更新行信息
      * 1、 已存在行,尝试判断数量等信息
      * 2、 未存在行插入
      * 3、 删除行删除
      * */
     foreach ($rows as $row) {
         $row['product_unique_id'] = $product_unique_id = generate_product_unique_id($row, $attribute_fields);
         if (array_key_exists($product_unique_id, $exists_rows)) {
             // 存在行
             //                $row['id'] = $exists_rows[$product_unique_id]['id'];
             $detail_service->create($row);
             $detail_service->save();
             //                $detail_service->where([
             //                    'id' => $exists_rows[$product_unique_id]['id']
             //                ])->save($row);
             //
             //                echo $detail_service->getLastSql();exit;
             array_push($edited_ids, $exists_rows['id']);
             $row['id'] = $exists_rows[$product_unique_id]['id'];
         } else {
             // 不存在行 插入
             $row[$this->detail_main_foreign] = $id;
             $detail_service->create($row);
             $row['id'] = $row_id = $detail_service->add();
         }
         // 更新属性map
         if ($attribute_active) {
             $this->record_detail_attribute($id, $row, $attribute_fields);
         }
     }
     // 删除未使用的明细条目
     $need_delete = array_diff($exists_ids, $edited_ids);
     $detail_service->where(['id' => ['IN', $need_delete]])->delete();
     $this->commit();
 }
Пример #11
0
 public function __construct()
 {
     //支持方法
     if (!$this->allowMethod) {
         $this->allowMethod = explode(",", strtolower(SUPPORTED_METHOD));
     }
     //session token
     if (I('server.HTTP_TOKEN')) {
         session(array('id' => I("server.HTTP_TOKEN"), 'expire' => 1800));
         session('[start]');
         $this->user = I("session.user");
         $this->is_super_user = $this->user['is_super_user'];
         if (isset($_SESSION['user']) && (!get_current_company_id() || !get_current_user_id())) {
             return $this->login_required();
         }
     }
     tag('before_controller_construct');
     parent::__construct();
     // 当前请求 =》 auth_node
     $this->current_action_all = sprintf("%s.%s.%s.%s", lcfirst(MODULE_NAME), lcfirst(CONTROLLER_NAME), lcfirst(ACTION_NAME), $this->_method);
     //当前用户所属公司
     if ($this->user or true) {
         $this->company = D("Account/Company")->relation(true)->find(get_current_company_id());
     }
     //当前公司启用应用
     $this->activeApps = $this->baseApps;
     if ($this->company) {
         $this->activeApps = array_merge($this->activeApps, get_array_by_field($this->company['apps'], "alias"));
     }
     //启用应用
     AppService::active($this->activeApps, $this->baseApps);
     //当前模块前端别名
     $this->module_alias = __(sprintf('%s.%s', lcfirst(MODULE_NAME), lcfirst(CONTROLLER_NAME)));
     //导入非当前应用的插件及函数等信息
     foreach ($this->activeApps as $app) {
         $app = ucfirst($app);
         if ($app == APP_NAME or $app == "common") {
             continue;
         }
         // 插件
         if (is_file(APPLICATION_PATH . $app . '/Conf/tags.php')) {
             \Think\Hook::import(require APPLICATION_PATH . $app . '/Conf/tags.php');
         }
         // 函数
         if (is_file(APPLICATION_PATH . $app . '/Common/function.php')) {
             require_once APPLICATION_PATH . $app . '/Common/function.php';
         }
     }
     //基本运行配置
     $this->bootstrapConfigs = parse_yml(ENTRY_PATH . '/config.yaml');
     //当前接口版本
     if (I('server.HTTP_API_VERSION')) {
         define('API_VERSION', I('server.HTTP_API_VERSION'));
     } else {
         define('API_VERSION', false);
     }
     //当前语言
     $this->currentLanguage = $this->bootstrapConfigs["default_language"] ? $this->bootstrapConfigs["default_language"] : 'zh-cn';
     if (I("server.HTTP_CLIENT_LANGUAGE")) {
         $this->currentLanguage = I("server.HTTP_CLIENT_LANGUAGE");
     }
     if (I('get.lang')) {
         $this->currentLanguage = I('get.lang');
     }
     define('CURRENT_LANGUAGE', $this->currentLanguage);
     /*
      * 解析应用配置
      * * */
     $cachedAppConfig = S("configs/app/all");
     if (DEBUG or !$cachedAppConfig) {
         foreach (new RecursiveFileFilterIterator(APP_PATH, "config.yml") as $item) {
             $app = lcfirst(basename(dirname($item)));
             $this->appConfigs[$app] = parse_yml($item);
         }
         S("configs/app/all", $this->appConfigs);
     } else {
         if (!DEBUG && $cachedAppConfig) {
             $this->appConfigs = $cachedAppConfig;
         }
     }
     AppService::$allAppConfigs = $this->appConfigs;
     foreach ($this->appConfigs as $app => $config) {
         $this->bootstrapConfigs['auth_dont_need_login'] = array_merge_recursive($this->bootstrapConfigs['auth_dont_need_login'], (array) $config['auth_dont_need_login']);
         $this->bootstrapConfigs['auth_dont_need_check'] = array_merge_recursive($this->bootstrapConfigs['auth_dont_need_check'], (array) $config['auth_dont_need_check']);
     }
     // 当前表名
     if (!$this->model_name) {
         $this->model_name = ucfirst(CONTROLLER_NAME);
     }
     // 获得用户已授权节点
     $authed_nodes = session('authed_nodes');
     if (DEBUG || !$authed_nodes) {
         $authed_nodes = D('Account/Authorize')->get_authed_nodes();
         session('authed_nodes', $authed_nodes);
     }
     self::$authed_nodes = $authed_nodes;
     AuthorizeService::set_authed_nodes($authed_nodes);
     //当前动作权限检测
     // @todo event, event_get
     $current_node_auth_flag = DEBUG ? 1 : $this->check_permission();
     if (substr($this->_method, 0, 5) !== 'event' && false === $current_node_auth_flag) {
         $node_lang = __(lcfirst(MODULE_NAME) . '.METHODS.' . $this->_method) . ' ' . __(lcfirst(MODULE_NAME) . '.' . ucfirst(CONTROLLER_NAME));
         return $this->httpError(403, __("common.Permission Denied") . ": " . $node_lang . "({$this->current_action_all})");
     }
     $this->current_node_auth_flag = (int) $current_node_auth_flag;
     tag('after_controller_construct');
 }
Пример #12
0
 public function change_user_role($uid, $role_ids)
 {
     $auth_roles = D('Account/AuthRole')->where([])->select();
     $auth_roles = get_array_by_field($auth_roles, 'id');
     $role_ids = array_intersect($role_ids, $auth_roles);
     $auth_role_service = D('Account/AuthUserRole');
     $auth_role_service->where(['user_id' => $uid])->delete();
     $company_id = get_current_company_id();
     foreach ($role_ids as $role_id) {
         $auth_role_service->add(['user_id' => $uid, 'company_id' => $company_id, 'auth_role_id' => $role_id]);
     }
 }
Пример #13
0
 public function get_department_users($department_id, $include_sub = true)
 {
     $department_id_all = ['department_id'];
     if ($include_sub) {
         $tree = $this->get_tree($department_id);
         $department_id_all = array_merge((array) get_array_by_field($tree, 'id'), $department_id_all);
     }
     $map = ['department_id' => ['IN', $department_id_all]];
     return D('Account/User')->where($map)->select();
 }
Пример #14
0
 public function _EM_get_data_by_date()
 {
     // 支持年,月,日三中纬度,默认为当月
     $start_time = null;
     $end_time = null;
     $date_prefix = '';
     $start_timestamp = null;
     $end_timestamp = null;
     if (I('get.st') && I('get.et')) {
         $start_timestamp = strtotime(I('get.st'));
         $end_timestamp = strtotime(I('get.et'));
         $start_time = date('Y-m-d', $start_timestamp);
         $end_time = date('Y-m-d', $end_timestamp);
         switch (I('get.dimension')) {
             case "year":
                 $date_item = 'Y';
                 $range_start = date('Y', $start_timestamp);
                 $range_end = date('Y', $end_timestamp);
                 break;
             case "month":
                 $date_item = 'm';
                 //                    $date_prefix = date('Y-', CTS);
                 $range_start = date('m', $start_timestamp);
                 $range_end = date('m', $end_timestamp);
                 break;
             default:
                 $date_item = 'd';
                 //                    $date_prefix = date('m-', CTS);
                 $range_start = date('d', $start_timestamp);
                 $range_end = date('d', $end_timestamp);
                 break;
         }
     } else {
         switch (I('get.dimension')) {
             case "year":
                 $date_item = 'Y';
                 $start_time = date('Y-01-01 00:00:00', strtotime(date('Y-m-d H:i:s', CTS) . ' -6 year'));
                 $end_time = date("Y-12-31 23:59:59", CTS);
                 $range_start = date('Y', strtotime($start_time));
                 $range_end = date('Y', CTS);
                 break;
             case "month":
                 $date_item = 'm';
                 $start_time = date('Y-01-01 00:00:00', CTS);
                 $end_time = date("Y-12-31 23:59:59", CTS);
                 $date_prefix = date('Y-', CTS);
                 $range_start = 1;
                 $range_end = 12;
                 break;
             default:
                 $date_item = 'd';
                 $start_time = date('Y-m-01 00:00:00', CTS);
                 $end_time = date("Y-m-d 23:59:59", strtotime("{$start_time} +1 month -1 day"));
                 $date_prefix = date('m-', CTS);
                 $range_start = 1;
                 $range_end = date($date_item, strtotime($end_time));
                 break;
         }
     }
     // 时间序列数组
     $date_range = range($range_start, $range_end);
     $map = ['created' => ['BETWEEN', [$start_time, $end_time]], 'status' => OrdersService::STATUS_COMPLETE];
     // 按部门查看
     if (I('get.dept')) {
         $users = D('Account/Department')->get_department_users(I('get.dept'));
         $user_ids = get_array_by_field($users, 'id');
         $map['user_id'] = ['IN', (array) $user_ids];
     }
     $orders_tmp = D('Sale/Orders')->where($map)->select();
     $orders_cleaned = [];
     foreach ($orders_tmp as $order) {
         $key = 'date_' . date($date_item, strtotime($order['created']));
         if (!$orders_cleaned[$key]) {
             $orders_cleaned[$key] = ['amount' => $order['net_receive'], 'quantity' => $order['quantity']];
             continue;
         }
         $orders_cleaned[$key]['amount'] += $order['net_receive'];
         $orders_cleaned[$key]['quantity'] += $order['quantity'];
     }
     $response_data = ['subtext' => sprintf('%s ~ %s', $start_time, $end_time), 'data' => ['amount' => [], 'quantity' => []], 'xAxis' => []];
     foreach ($date_range as $date) {
         $date = $date < 10 ? '0' . $date : $date;
         $key = 'date_' . $date;
         array_push($response_data['xAxis'], $date_prefix . $date);
         array_push($response_data['data']['amount'], $orders_cleaned[$key]['amount'] ? (double) $orders_cleaned[$key]['amount'] : 0);
         array_push($response_data['data']['quantity'], $orders_cleaned[$key]['quantity'] ? (double) $orders_cleaned[$key]['quantity'] : 0);
     }
     $this->response($response_data);
 }