Example #1
0
 public function update_customer_head($ids, $uid)
 {
     if (!$ids) {
         return;
     }
     $company_id = get_current_company_id();
     // 原始数据
     $source = array();
     if (!$uid) {
         $source = $this->where(array('id' => array("IN", $ids)))->select();
         $source = get_array_to_kv($source, 'user_id', 'id');
     }
     // 往来单位
     $contacts_company = D('ContactsCompany/ContactsCompany', 'Service')->where(array('id' => array("IN", $ids)))->select();
     $contacts_company = get_array_to_ka($contacts_company);
     foreach ($ids as $id) {
         $map = array('contacts_company_id' => $id, 'company_id' => $company_id);
         $exists = $this->where($map)->find();
         if ($exists) {
             // 已存在更新负责人ID
             $this->where($map)->save(array('head_id' => $uid));
         } else {
             // 不存在新增一条
             $data = array('contacts_company_id' => $id, 'head_id' => $uid, 'user_id' => $uid, 'crm_clue_id' => null, 'company_id' => $company_id, 'source_from' => $contacts_company[$id]['source_form']);
             $this->add($data);
         }
     }
     $head_model = D('Crm/CustomerHeadLog');
     $type = $uid ? 1 : 2;
     foreach ($ids as $id) {
         $user_id = $uid ? $uid : $source[$id];
         $head_model->add(array('type' => $type, 'company_id' => $company_id, 'customer_id' => $id, 'user_id' => $user_id));
     }
     return true;
 }
 public function assign_to_by_product_unique($rows)
 {
     if (!AppService::is_app_active('productAttribute')) {
         return [];
     }
     $attr_content_ids = [];
     foreach ($rows as $k => $row) {
         $unique_id = $row['product_unique_id'];
         if (!$unique_id) {
             continue;
         }
         $tmp = explode('|', $unique_id);
         $rows[$k]['attribute_content_ids'] = [];
         foreach ($tmp as $attr) {
             list($attr_id, $attr_content_id) = explode('_', $attr);
             array_push($attr_content_ids, $attr_content_id);
             array_push($rows[$k]['attribute_content_ids'], $attr_content_id);
         }
     }
     if (!$attr_content_ids) {
         return $rows;
     }
     $attribute_contents = D('ProductAttribute/ProductAttributeContent', 'Model')->where(['id' => ['IN', $attr_content_ids]])->select();
     $attribute_contents = get_array_to_ka($attribute_contents, 'id');
     foreach ($rows as $k => $row) {
         foreach ($row['attribute_content_ids'] as $ac_id) {
             if (array_key_exists($ac_id, $attribute_contents)) {
                 $attribute_field = $attribute_contents[$ac_id]['alias'];
                 $attribute_value = $attribute_contents[$ac_id]['content'];
                 $rows[$k][$attribute_field] = $attribute_value;
             }
         }
     }
     return $rows;
 }
Example #3
0
 public function get_authed_nodes_by_role($role_id)
 {
     $cache_key = 'authed/nodes/role/' . $role_id;
     $cached = S($cache_key);
     if (!DEBUG && $cached) {
         return $cached;
     }
     $nodes = $this->where(array('auth_role_id' => $role_id))->select();
     $data = get_array_to_ka($nodes, 'id');
     S($cache_key, $data);
     return $data;
 }
Example #4
0
 public function enable($alias)
 {
     if (!is_array($alias)) {
         $alias = [$alias];
     }
     $apps = array_diff($alias, array_diff(self::$activeApps, self::$baseApps));
     $company_id = get_current_company_id();
     $company_service = D('Account/Company');
     $buyModel = D('Account/CompanyBuyApps');
     $company_info = $company_service->where(['id' => $company_id])->find();
     $buy_history = $buyModel->where(['company_id' => $company_id])->select();
     $buy_history = get_array_to_ka($buy_history, 'app_id');
     if (!$apps) {
         return true;
     }
     $apps_info = $this->where(array('alias' => array('IN', $apps)))->select();
     if (!$apps_info) {
         return true;
     }
     $model = D('Account/CompanyActiveApps');
     foreach ($apps_info as $app) {
         if (!array_key_exists($app['id'], $buy_history)) {
             if ($company_info['balance'] < $app['price']) {
                 $this->error_code = 1001;
                 $this->error = __('home.Balance not full');
                 return false;
             }
             $company_service->where(['id' => $company_id])->setDec('balance', $app['price']);
         }
         // 检测依赖
         if (trim($app['requirements'])) {
             $requirements = explode(',', $app['requirements']);
             foreach ($requirements as $req) {
                 if ($req != $app['alias'] && !self::is_app_active($req)) {
                     if (!$this->enable($req)) {
                         $this->error = $this->getError();
                         return false;
                     }
                 }
             }
         }
         if (!$model->add(array('company_id' => $company_id, 'app_id' => $app['id']))) {
             return false;
         }
         if (!array_key_exists($app['id'], $buy_history)) {
             $buyModel->add(['company_id' => $company_id, 'app_id' => $app['id']]);
         }
     }
     return 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;
 }
 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;
 }
Example #7
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);
 }
 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);
 }
Example #9
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();
 }
Example #10
0
 public function exec($source_id, $meta_data = [], $node = null, $workflow = null, $auto = false)
 {
     if (!is_array($node) and $node > 0) {
         $node = D('Bpm/WorkflowNode')->where(['id' => $node])->find();
     } else {
         if (!$node) {
             // 自动获取下一节点 @todo
         }
     }
     if (!$node) {
         $this->error = __('bpm.Workflow node not found');
         return false;
     }
     if (!is_array($workflow) || !$workflow['nodes']) {
         $workflow = $this->get_workflow($node['workflow_id']);
     }
     if (!$workflow) {
         $this->error = __('bpm.Workflow not found') . ': ' . $node['workflow_id'];
         return false;
     }
     /*
      * 当前执行节点
      * */
     $this->current_node = $node;
     /*
      * 当前工作流所有节点
      * */
     $current_workflow_nodes = get_array_to_ka($workflow['nodes']);
     /*
      * 检测当前执行节点是否为上一节点的next_nodes/condition_true_nodes/condition_false_nodes
      * */
     $progress_service = D('Bpm/WorkflowProgress');
     $latest_progress = $progress_service->get_latest_progress($workflow['id'], $source_id);
     if ($latest_progress) {
         $latest_node_next_nodes = array_merge((array) $current_workflow_nodes[$latest_progress['workflow_node_id']]['next_nodes'], (array) $current_workflow_nodes[$latest_progress['workflow_node_id']]['condition_true_nodes'], (array) $current_workflow_nodes[$latest_progress['workflow_node_id']]['condition_false_nodes']);
         if (!in_array($this->current_node['id'], $latest_node_next_nodes)) {
             $this->error = __('bpm.Not a verified workflow node');
             return false;
         }
     }
     // 获取源数据
     list($app, $module) = explode('.', $workflow['module']);
     $source_model = sprintf('%s/%s', ucfirst($app), ucfirst($module));
     $source_data = D($source_model)->where(['id' => $source_id])->find();
     $method = 'exec_' . $node['action_type'];
     /*
      * 根据不同动作类型执行
      * */
     $exec_result = $this->{$method}($node['action'], $source_model, $source_data);
     /*
      * 非条件判断节点执行结果为false时,判定执行失败,中断。
      * */
     if (false === $exec_result && $node['node_type'] !== 'cond') {
         return false;
     }
     /*
      * 执行结果中不包含中断信号
      * */
     if (!$exec_result['pause']) {
         $data = ['source_id' => $source_id, 'workflow_id' => $node['workflow_id'], 'workflow_node_id' => $node['id']];
         $data = array_merge($meta_data, $data);
         // 写入进程表
         if (false === $progress_service->create($data)) {
             $this->error = __('bpm.Unverified Progress Object');
             return false;
         }
         if (false === $progress_service->add()) {
             $this->error = __('bpm.Log workflow progress failed');
             return false;
         }
     } else {
         $response = $exec_result;
         if (is_array($response) && $response) {
             return $this->response($response);
         } else {
             return $response;
         }
     }
     /*
      * 不同的节点类型
      * */
     // 条件判断节点
     if ($node['node_type'] === 'cond') {
         if ($exec_result === true) {
             // 条件为真时,执行condition_true_nodes
             $condition_next_nodes = $node['condition_true_nodes'];
         } else {
             // 否则执行condition_true_nodes
             $condition_next_nodes = $node['condition_false_nodes'];
         }
         // 自动执行下一节点
         if ($condition_next_nodes) {
             $condition_next_nodes = explode(',', $condition_next_nodes);
             foreach ($condition_next_nodes as $nid) {
                 $this->exec($source_id, $meta_data, $nid, null, true);
             }
         }
     }
     /*
      * 执行结果类型
      *
      *  「1」 redirect 跳转至某一页面 uri: storage/stockIn/confirm/:id
      *  「2」 message 显示一个提示信息  level=info|success|danger..., content
      *
      * $exec_result = [
      *  type: 类型
      *  pause: 是否中断
      *  other_params
      * ];
      * */
     $response = $exec_result;
     /*
      * 检测后续节点是否自动执行
      * */
     $next_nodes_ids = explode(',', $node['next_nodes']);
     $next_nodes = [];
     foreach ($current_workflow_nodes as $node_id => $node_info) {
         if (in_array($node_id, $next_nodes_ids)) {
             $next_nodes[$node_id] = $node_info;
         }
     }
     foreach ($next_nodes as $node_id => $node_info) {
         if ($node_info['executor'] === 'a:a') {
             $this->exec($source_id, $meta_data, $node_id);
         }
     }
     if (is_array($response) && $response) {
         return $this->response($response);
     } else {
         return $response;
     }
 }
Example #11
0
 public static function data_format($data, $model, $is_table = false, $app = MODULE_NAME)
 {
     if (!$model && !$is_table) {
         return $data;
     }
     if (!$is_table) {
         $table = $model->getProperty('tableName') ? $model->getProperty('tableName') : array_shift(explode(' ', $model->getTableName()));
     } else {
         $table = $model;
     }
     $schemas = SchemaService::getSchemaByApp($app);
     $is_single = is_assoc($data);
     $schema = get_array_to_ka($schemas[$table]['structure'], 'field');
     // 单条数据
     if ($is_single) {
         $data = array($data);
     }
     foreach ($data as $k => $item) {
         foreach ($item as $field => $value) {
             if ($field === "id") {
                 $data[$k][$field] = (int) $value;
                 continue;
             }
             $is_exists = array_key_exists($field, $schema);
             // 数据表结构中包含字段
             if ($is_exists) {
                 $structure = $schema[$field];
                 $data[$k][$field] = self::data_field_format($value, $structure['type']);
             }
         }
     }
     return $is_single ? $data[0] : $data;
 }