Esempio n. 1
0
    /**
     * Create project shelve and cabinet
     * @param $projectId
     * @param $year
     */
    public static function createProjectStorages($projectId, $year)
    {
        $project = Projects::model()->findByPk($projectId);

        // check existing project cabinet
        $cabinet = Storages::model()->findByAttributes(array(
            'Storage_Type' => self::CABINET,
            'Created_By' => '0',
            'Project_ID' => $projectId,
            'Year' => $year,
        ));
        if (!$cabinet) {
            // if there is not cabinet for this project create it
            $cabinet = new Storages();
            $cabinet->Storage_Name = $project->Project_Name;
            $cabinet->Project_ID = $projectId;
            $cabinet->Client_ID = Yii::app()->user->clientID;
            $cabinet->Year = $year;
            $cabinet->Created_By = 0;
            $cabinet->Row_Num = 0;
            $cabinet->Storage_Type = self::CABINET;
            $cabinet->Access_Type = self::HAS_ACCESS;
            if ($cabinet->validate()) {
                $cabinet->save();
            }
        }

        // check existing project shelf
        $shelf = Storages::model()->findByAttributes(array(
            'Storage_Type' => self::SHELF,
            'Created_By' => '0',
            'Project_ID' => $projectId,
            'Year' => $year,
        ));
        if (!$shelf) {
            // if there is not cabinet for this project create it
            $shelf = new Storages();
            $shelf->Storage_Name = $project->Project_Name;
            $shelf->Project_ID = $projectId;
            $shelf->Client_ID = Yii::app()->user->clientID;
            $shelf->Year = $year;
            $shelf->Created_By = 0;
            $shelf->Row_Num = 0;
            $shelf->Storage_Type = self::SHELF;
            $shelf->Access_Type = self::HAS_ACCESS;
            if ($shelf->validate()) {
                $shelf->save();
            }
        }

        $countW9s = W9::getCountOfAvailableW9sOfYear($year);
        if ($countW9s > 0) {
            // also crete W9 book if it does not exist and there are Company's W9s
            $w9Binder = Sections::model()->findByAttributes(array(
                'Section_Type' => self::SHELF,
                'Created_By' => '0',
                'Storage_ID' => $shelf->Storage_ID,
                'Folder_Cat_ID' => Sections::W9_BOOK,
            ));

            if (!$w9Binder) {
                $w9Binder = new Sections();
                $w9Binder->Storage_ID = $shelf->Storage_ID;
                $w9Binder->Section_Name = "W9s";
                $w9Binder->Vendor_ID = 0;
                $w9Binder->Created_By = 0;
                $w9Binder->Section_Type = self::SHELF;
                $w9Binder->Folder_Cat_ID = Sections::W9_BOOK;
                $w9Binder->Access_Type = self::HAS_ACCESS;
                if ($w9Binder->validate()) {
                    $w9Binder->save();

                    $tab = new Subsections();
                    $tab->Section_ID = $w9Binder->Section_ID;
                    $tab->Subsection_Name = 'Tab 1';
                    $tab->Subsection_Type = self::SHELF;
                    $tab->Created_By = 0;
                    $tab->Access_Type = self::HAS_ACCESS;
                    $tab->save();
                }
            }
        }
    }
Esempio n. 2
0
    /**
     * Get count of documents in storage
     * @param $id
     * @param $rowType
     * @return mixed
     */
    public static function getDocumentsCount($id, $rowType)
    {
        $where = "(`ld`.`Access_Type` = '" . Storages::HAS_ACCESS . "' OR `ds`.`User_ID` = '" . Yii::app()->user->userID ."') ";
        $where .= " AND `st`.`Client_ID` = '" . Yii::app()->user->clientID ."' ";
        if (Yii::app()->user->projectID != 'all') {
            $where .= " AND `st`.`Project_ID` = '" . Yii::app()->user->projectID ."' ";
        }

        if ($rowType == 'storage') {
            $where .= " AND `st`.`Storage_ID` = '" . $id ."' ";
        } else if ($rowType == 'section') {
            $where .= " AND `sc`.`Section_ID` = '" .$id ."' ";
        } else {
            $where .= " AND `ss`.`Subsection_ID` = '" . $id ."' ";
        }

        if (Yii::app()->user->id == 'user') {
            $where .= " AND `ds`.`User_ID` = '" . Yii::app()->user->userID . "'";
        }

        $query = "SELECT count(*) as count
                  FROM `library_docs` as ld
                  LEFT JOIN `documents` as ds ON `ds`.`Document_ID` = `ld`.`Document_ID`
                  LEFT JOIN `subsections` as ss ON `ss`.`Subsection_ID` = `ld`.`Subsection_ID`
                  LEFT JOIN `sections` as sc ON `sc`.`Section_ID` = `ss`.`Section_ID`
                  LEFT JOIN `storages` as st ON `st`.`Storage_ID` = `sc`.`Storage_ID`
                  WHERE $where";

        $connection=Yii::app()->db;
        $command=$connection->createCommand($query);
        $row=$command->queryRow();
        $count = $row['count'];

        // add unassigned W9s of Vendor folder and W9 book binder
        if ($rowType == 'storage') {
            $storage = Storages::model()->findByPk($id);
            if ($storage->Storage_Type == Storages::SHELF) {
                $count += W9::getCountOfAvailableW9sOfYear($storage->Year);
            } else {
                $condition = new CDbCriteria();
                $condition->condition = "t.Storage_ID = '" . $id . "'";
                $condition->addCondition("t.Folder_Cat_ID = '" . self::VENDOR_DOCUMENTS . "'");
                $vendorFolders = Sections::model()->findAll($condition);
                foreach ($vendorFolders as $vendorFolder) {
                    $w9 = W9::getCompanyW9Doc($vendorFolder->Vendor_ID);
                    if ($w9) {
                        $count++;
                    }
                }
            }
        } else if ($rowType == 'section') {
            $section = Sections::model()->with('storage')->findByPk($id);
            if ($section->Folder_Cat_ID == self::W9_BOOK) {
                $count += W9::getCountOfAvailableW9sOfYear($section->storage->Year);
            } else if ($section->Folder_Cat_ID == self::VENDOR_DOCUMENTS) {
                $w9 = W9::getCompanyW9Doc($section->Vendor_ID);
                if ($w9) {
                    $count++;
                }
            }
        } else {
            $subsection = Subsections::model()->with('user', 'section.storage')->findByPk($id);
            if ($subsection->section->Folder_Cat_ID == self::W9_BOOK && $subsection->Created_By == 0) {
                $count += W9::getCountOfAvailableW9sOfYear($subsection->section->storage->Year);
            } else if ($subsection->section->Folder_Cat_ID == self::VENDOR_DOCUMENTS && $subsection->Created_By == 0) {
                $w9 = W9::getCompanyW9Doc($subsection->section->Vendor_ID);
                if ($w9) {
                    $count++;
                }
            }
        }


        return $count;
    }