Esempio n. 1
0
function createRecords($obj)
{
    global $adb, $VTIGER_BULK_SAVE_MODE;
    $moduleName = $obj->module;
    $moduleHandler = vtws_getModuleHandlerFromName($moduleName, $obj->user);
    $moduleMeta = $moduleHandler->getMeta();
    $moduleObjectId = $moduleMeta->getEntityId();
    $moduleFields = $moduleMeta->getModuleFields();
    include_once 'include/fields/InventoryLineField.php';
    $ilfields = new InventoryLineField();
    $moduleFields = array_merge($moduleFields, $ilfields->getInventoryLineFieldsByObject());
    $focus = CRMEntity::getInstance($moduleName);
    $wsrs = $adb->pquery('select id from vtiger_ws_entity where name=?', array('Products'));
    if ($wsrs and $adb->num_rows($wsrs) == 1) {
        $pdowsid = $adb->query_result($wsrs, 0, 0) . 'x';
    } else {
        $pdowsid = '0x';
    }
    $wsrs = $adb->pquery('select id from vtiger_ws_entity where name=?', array('Services'));
    if ($wsrs and $adb->num_rows($wsrs) == 1) {
        $srvwsid = $adb->query_result($wsrs, 0, 0) . 'x';
    } else {
        $srvwsid = '0x';
    }
    $wsrs = $adb->pquery('select id from vtiger_ws_entity where name=?', array('Users'));
    if ($wsrs and $adb->num_rows($wsrs) == 1) {
        $usrwsid = $adb->query_result($wsrs, 0, 0) . 'x';
    } else {
        $usrwsid = '0x';
    }
    $tableName = Import_Utils::getDbTableName($obj->user);
    $sql = 'SELECT * FROM ' . $tableName . ' WHERE status = ' . Import_Data_Controller::$IMPORT_RECORD_NONE . ' GROUP BY subject';
    if ($obj->batchImport) {
        $importBatchLimit = GlobalVariable::getVariable('Import_Batch_Limit', 100);
        $sql .= ' LIMIT ' . $importBatchLimit;
    }
    $result = $adb->query($sql);
    $numberOfRecords = $adb->num_rows($result);
    if ($numberOfRecords <= 0) {
        return;
    }
    $fieldMapping = $obj->fieldMapping;
    $fieldColumnMapping = $moduleMeta->getFieldColumnMapping();
    for ($i = 0; $i < $numberOfRecords; ++$i) {
        $row = $adb->raw_query_result_rowdata($result, $i);
        $rowId = $row['id'];
        $entityInfo = null;
        $fieldData = array();
        $lineItems = array();
        $subject = $row['subject'];
        $sql = 'SELECT * FROM ' . $tableName . ' WHERE status = ' . Import_Data_Controller::$IMPORT_RECORD_NONE . ' AND subject = "' . str_replace("\"", "\\\"", $subject) . '"';
        $subjectResult = $adb->query($sql);
        $count = $adb->num_rows($subjectResult);
        $subjectRowIDs = array();
        for ($j = 0; $j < $count; ++$j) {
            $subjectRow = $adb->raw_query_result_rowdata($subjectResult, $j);
            if ($subjectRow['productid'] == '' || $subjectRow['quantity'] == '' || $subjectRow['listprice'] == '') {
                continue;
            }
            array_push($subjectRowIDs, $subjectRow['id']);
            $lineItemData = array();
            $lineItemData['discount'] = 0;
            foreach ($fieldMapping as $fieldName => $index) {
                if ($moduleFields[$fieldName]->getTableName() == 'vtiger_inventoryproductrel') {
                    if ($fieldName == 'productid') {
                        $fieldValue = $subjectRow[$fieldName];
                        if (strpos($fieldValue, '::::') > 0) {
                            $fieldValueDetails = explode('::::', $fieldValue);
                        } else {
                            if (strpos($fieldValue, ':::') > 0) {
                                $fieldValueDetails = explode(':::', $fieldValue);
                            } else {
                                $fieldValueDetails = $fieldValue;
                            }
                        }
                        if (count($fieldValueDetails) > 1) {
                            $referenceModuleName = trim($fieldValueDetails[0]);
                            $entityLabel = trim($fieldValueDetails[1]);
                            $entityId = getEntityId($referenceModuleName, $entityLabel);
                        } else {
                            $referencedModules = array('Products', 'Services');
                            $entityLabel = $fieldValue;
                            foreach ($referencedModules as $referenceModule) {
                                $referenceModuleName = $referenceModule;
                                $referenceEntityId = getEntityId($referenceModule, $entityLabel);
                                if ($referenceEntityId != 0) {
                                    $entityId = $referenceEntityId;
                                    break;
                                }
                            }
                        }
                        if (empty($entityId) and in_array($referenceModuleName, array('Products', 'Services'))) {
                            $pdinfo = array();
                            if ($referenceModuleName == 'Services') {
                                $pdoinfo['servicename'] = $entityLabel;
                            } else {
                                $pdoinfo['productname'] = $entityLabel;
                            }
                            $pdoinfo['assigned_user_id'] = $usrwsid . $obj->user->id;
                            try {
                                $VTIGER_BULK_SAVE_MODE = false;
                                $psInfo = vtws_create($referenceModuleName, $pdoinfo, $obj->user);
                                $VTIGER_BULK_SAVE_MODE = true;
                                $entityId = $psInfo['id'];
                            } catch (Exception $e) {
                                continue 2;
                                // ignore this line
                            }
                        } else {
                            if ($referenceModuleName == 'Services') {
                                $entityId = $srvwsid . $entityId;
                            } else {
                                $entityId = $pdowsid . $entityId;
                            }
                        }
                        $lineItemData[$fieldName] = $entityId;
                    } else {
                        $lineItemData[$fieldName] = $subjectRow[$fieldName];
                        if ($fieldName == 'discount_amount' and $subjectRow[$fieldName] > 0) {
                            $lineItemData['discount'] = 1;
                            $lineItemData['discount_type'] = 'amount';
                        }
                        if ($fieldName == 'discount_percent' and $subjectRow[$fieldName] > 0) {
                            $lineItemData['discount'] = 1;
                            $lineItemData['discount_percentage'] = $subjectRow[$fieldName];
                            $lineItemData['discount_type'] = 'percentage';
                        }
                        if ($fieldName == 'quantity') {
                            $lineItemData['qty'] = $subjectRow[$fieldName];
                        }
                    }
                }
            }
            array_push($lineItems, $lineItemData);
        }
        foreach ($fieldMapping as $fieldName => $index) {
            $fieldData[$fieldName] = $row[strtolower($fieldName)];
            if ($fieldName == 'hdnTaxType') {
                $fieldData['taxtype'] = $row[strtolower($fieldName)];
            }
            if ($fieldName == 'txtAdjustment' and $fieldData[$fieldName] != 0) {
                $fieldData['adjustmentType'] = 'add';
                $fieldData['adjustment'] = $fieldData[$fieldName];
            }
        }
        if (!array_key_exists('assigned_user_id', $fieldData)) {
            $fieldData['assigned_user_id'] = $obj->user->id;
        }
        if (!empty($lineItems)) {
            if (method_exists($focus, 'importRecord')) {
                $entityInfo = $focus->importRecord($obj, $fieldData, $lineItems);
            }
        }
        if ($entityInfo == null) {
            $entityInfo = array('id' => null, 'status' => $obj->getImportRecordStatus('failed'));
        }
        foreach ($subjectRowIDs as $id) {
            $obj->importedRecordInfo[$id] = $entityInfo;
            $obj->updateImportStatus($id, $entityInfo);
        }
    }
    unset($result);
    return true;
}
 public function getImportableFields($moduleName)
 {
     $focus = CRMEntity::getInstance($moduleName);
     if (method_exists($focus, 'getImportableFields')) {
         $importableFields = $focus->getImportableFields();
     } else {
         // if we are merging, then selected merge fields must be in selectable fields, so we fill in the mergeFields
         $mergeFields = array();
         if (isset($_REQUEST['merge_type']) and !empty($_REQUEST['merge_fields'])) {
             $json = vtlib_purify($_REQUEST['merge_fields']);
             $mergeFields = json_decode($json);
         }
         $moduleFields = $this->getAccessibleFields($moduleName);
         $importableFields = array();
         foreach ($moduleFields as $fieldName => $fieldInstance) {
             if ($this->isEditableField($fieldInstance) && ($fieldInstance->getTableName() != 'vtiger_crmentity' || $fieldInstance->getColumnName() != 'modifiedby') || $fieldInstance->getUIType() == '70' && $fieldName != 'modifiedtime' || in_array($fieldName, $mergeFields)) {
                 $importableFields[$fieldName] = $fieldInstance;
             }
         }
     }
     if (in_array($moduleName, getInventoryModules())) {
         include_once 'include/fields/InventoryLineField.php';
         $ilfields = new InventoryLineField();
         $importableFields = array_merge($importableFields, $ilfields->getInventoryLineFieldsByObject());
     }
     return $importableFields;
 }