def import_prospect(target_term, department_id, owner_id, imported_filename, memo, adapter): """ @param target_term: Term id @param department_id: Department id @param owner_id: Owner id @param memo: Memo @param adapter: Import adapter @param imported_filename: Imported file name @type adapter: ImportAdapter @return: """ target_term = int(target_term) department_id = int(department_id) term = FiscalTerm.objects.get(pk=target_term) # Get all of id which will be delete: the same term, department and owner_id query = "select id from biz_prospect where " \ "import_history_id in (select id from import_history where data_type=%s " \ "and target_term=%s and user_id=%s) and " \ "id in (select base_data_id from base_data_auth_meta_rel " \ "where base_data_name=%s and meta_type_id=%s and meta_id=%s)" biz_prospect_ids = DbAgent.get_data_list(query, [ ImportDataType.BizProspect.value, target_term, owner_id, AuthBaseDataName.BizProspect.value, AuthMetaTypeDefine.Department.code, department_id ]) # Delete history data BizProspect.objects.filter(id__in=biz_prospect_ids).delete() # Delete auth base meta relation BaseDataAuthMetaRel.objects.filter( base_data_id__in=biz_prospect_ids, base_data_name=AuthBaseDataName.BizProspect.value).delete() # Delete base meta data relation BaseDataMetaRel.objects.filter( base_data_id__in=biz_prospect_ids, base_data_name=BaseDataName.BizProspect.value).delete() # New import history ih = ImportHistory() ih.target_term_id = target_term ih.data_type = ImportDataType.BizProspect.value ih.user_id = owner_id if memo and memo != "": ih.memo = memo ih.imported_filename = imported_filename ih.save() # Auth base meta relation for import history auth_meta = BaseDataAuthMetaRel() auth_meta.base_data_name = AuthBaseDataName.ImportHistory.value auth_meta.base_data_id = ih.id auth_meta.meta_type_id = AuthMetaTypeDefine.Department.code auth_meta.meta_id = department_id auth_meta.save() # Import data data = adapter.get_data() biz_prospects = [] for rec in data: # check accrual_date is datetime or date if isinstance(rec['accrual_date'], datetime): rec['accrual_date'] = rec['accrual_date'].date() if not rec['accrual_date']: continue # Check if data not in term if rec['accrual_date'] < term.start_date or rec[ 'accrual_date'] > term.end_date: continue biz_prospect = BizProspect() biz_prospect.owner_id = owner_id biz_prospect.type = rec['type'] biz_prospect.title = rec['title'] biz_prospect.amount = rec['amount'] biz_prospect.opportunity_stage = rec['opportunity_stage'] biz_prospect.target_id = rec['target_id'] biz_prospect.accrual_date = rec['accrual_date'] biz_prospect.settlement_date = rec['settlement_date'] biz_prospect.import_history_id = ih.id biz_prospects.append(biz_prospect) BizProspect.objects.bulk_create(biz_prospects) # Get list id of imported data query = "select id from biz_prospect where " \ "import_history_id = %s order by id" biz_prospect_ids = DbAgent.get_data_list(query, [ih.id]) # Auth base meta relation for prospect data auth_metas = [] for base_data_id in biz_prospect_ids: # Update auth meta data auth_meta = BaseDataAuthMetaRel() auth_meta.base_data_name = AuthBaseDataName.BizProspect.value auth_meta.base_data_id = base_data_id auth_meta.meta_type_id = AuthMetaTypeDefine.Department.code auth_meta.meta_id = department_id auth_metas.append(auth_meta) BaseDataAuthMetaRel.objects.bulk_create(auth_metas) # Update meta data adapter.insert_meta_data_relation(biz_prospect_ids) # Write log LogOperation.log(LogModule.BizProspect, LogType.Insert, LogResult.Success, ih.id) return ih.id
def import_biz_budget(target_term, department_id, owner_id, imported_filename, memo, adapter): """ Process data from file excel @param target_term: Term id @param department_id: Department id @param owner_id: Owner id @param memo: Memo @param adapter: Import adapter @param imported_filename: Imported file name @type target_term: int @type department_id: int @type owner_id: int @type imported_filename: str @type memo: str @type adapter: ImportAdapter @return: """ target_term = int(target_term) department_id = int(department_id) # Get all of id which will be delete: the same term, department and owner_id query = "select id from biz_budget where " \ "import_history_id in (select id from import_history where data_type=%s " \ "and target_term=%s and user_id=%s) and " \ "id in (select base_data_id from base_data_auth_meta_rel " \ "where base_data_name=%s and meta_type_id=%s and meta_id=%s)" biz_budget_ids = DbAgent.get_data_list(query, [ ImportDataType.BizBudget.value, target_term, owner_id, AuthBaseDataName.BizBudget.value, AuthMetaTypeDefine.Department.code, department_id ]) # Delete history data BizBudget.objects.filter(id__in=biz_budget_ids).delete() # Delete auth base meta relation BaseDataAuthMetaRel.objects.filter( base_data_id__in=biz_budget_ids, base_data_name=AuthBaseDataName.BizBudget.value).delete() # Delete base meta data relation BaseDataMetaRel.objects.filter( base_data_id__in=biz_budget_ids, base_data_name=BaseDataName.BizBudget.value).delete() # New import history ih = ImportHistory() ih.target_term_id = target_term ih.data_type = ImportDataType.BizBudget.value ih.user_id = owner_id if memo and memo != "": ih.memo = memo ih.imported_filename = imported_filename ih.save() # Auth base meta relation for import history auth_meta = BaseDataAuthMetaRel() auth_meta.base_data_name = AuthBaseDataName.ImportHistory.value auth_meta.base_data_id = ih.id auth_meta.meta_type_id = AuthMetaTypeDefine.Department.code auth_meta.meta_id = department_id auth_meta.save() # Import data data = adapter.get_data() biz_budgets = [] periods = FiscalTermLogic.get_term_period_list(target_term) periods = list(periods) for rec in data: for i in range(12): if i < len(periods): biz_budget = BizBudget() biz_budget.owner_id = owner_id biz_budget.import_history_id = ih.id biz_budget.target_id = rec['target_id'] biz_budget.plan_type = rec['plan_type'] biz_budget.account_id = rec['account'] biz_budget.fiscal_period_id = periods[i].id biz_budget.amount = rec['amount{0}'.format(i + 1)] biz_budgets.append(biz_budget) BizBudget.objects.bulk_create(biz_budgets) # Get list id of imported data query = "select id from biz_budget where " \ "import_history_id = %s order by id" biz_budget_ids = DbAgent.get_data_list(query, [ih.id]) # Auth base meta relation for prospect data auth_metas = [] for base_data_id in biz_budget_ids: # Update auth meta data auth_meta = BaseDataAuthMetaRel() auth_meta.base_data_name = AuthBaseDataName.BizBudget.value auth_meta.base_data_id = base_data_id auth_meta.meta_type_id = AuthMetaTypeDefine.Department.code auth_meta.meta_id = department_id auth_metas.append(auth_meta) BaseDataAuthMetaRel.objects.bulk_create(auth_metas) # Update meta data adapter.insert_meta_data_relation(biz_budget_ids, 12) # Write log LogOperation.log(LogModule.BizBudget, LogType.Insert, LogResult.Success, ih.id) return ih.id