Example #1
0
    /**
     * Delete document with rows in relative tables
     * @param $documentId
     */
    public static function deleteDocument($documentId) {
        $document = Documents::model()->findByPk($documentId);
        if ($document) {
            if ($document->Document_Type == self::W9) {
                $w9s = W9::model()->findAllByAttributes(array(
                    'Document_ID' => $documentId,
                ));
                foreach ($w9s as $w9) {
                    W9::deleteW9($w9->W9_ID);
                }
            } else if ($document->Document_Type == self::AP) {
                $ap = Aps::model()->findByAttributes(array(
                    'Document_ID' => $documentId,
                ));
                if ($ap) {
                    Aps::deleteAP($ap->AP_ID);
                }
            } else if ($document->Document_Type ==  self::PM) {
                $payment = Payments::model()->findByAttributes(array(
                    'Document_ID' => $documentId,
                ));
                if ($payment) {
                    Payments::deletePayment($payment->Payment_ID);
                }
            } else if ($document->Document_Type == self::PO) {
                $po = Pos::model()->findByAttributes(array(
                    'Document_ID' => $documentId,
                ));
                if ($po) {
                    Pos::deletePO($po->PO_ID);
                }
            } else if ($document->Document_Type == self::PC) {
                $pc = Pcs::model()->findByAttributes(array(
                    'Document_ID' => $documentId,
                ));
                if ($pc) {
                    Pcs::deletePC($pc->PC_ID);
                }
            } else if ($document->Document_Type == self::AR) {
                $ar = Ars::model()->findByAttributes(array(
                    'Document_ID' => $documentId,
                ));
                if ($ar) {
                    Ars::deleteAR($ar->AR_ID);
                }
            } else if ($document->Document_Type == self::PR) {
                $payroll = Payrolls::model()->findByAttributes(array(
                    'Document_ID' => $documentId,
                ));
                if ($payroll) {
                    Payrolls::deletePayroll($payroll->Payroll_ID);
                }
            }  else {
                $image = $document->image;
                $image->delete();
                $document->delete();

                // delete thumbnail
                $filePath = 'protected/data/thumbs/' . $documentId . '.jpg';
                if (file_exists($filePath)) {
                    @unlink($filePath);
                }

                // delete library links
                LibraryDocs::deleteDocumentLinks($documentId);
            }
        }
    }