Exemple #1
0
 function testDestroyAssociatedRecords()
 {
     $c = new Company();
     $c->set(array('name' => 'destroy_test'));
     $c->save();
     $p = new Project();
     $p->set(array('company_id' => $c->id, 'name' => 'destroy_project_test'));
     $p->save();
     $e = new Estimate();
     $e->set(array('project_id' => $p->id, 'name' => 'destroy_estimate_test'));
     $e->save();
     $h = new Hour();
     $h->set(array('estimate_id' => $e->id, 'name' => 'destroy_hour_test'));
     $h->save();
     $ch = new Charge();
     $ch->set(array('company_id' => $c->id, 'name' => 'destroy_charge_test'));
     $ch->save();
     $con = new SupportContract();
     $con->set(array('company_id' => $c->id, 'name' => 'destroy_contract_test'));
     $con->save();
     $sup_hr = new Hour();
     $sup_hr->set(array('support_contract_id' => $con->id, 'description' => 'destroy_support_hour_test'));
     $sup_hr->save();
     $pay = new Payment();
     $pay->set(array('company_id' => $c->id, 'name' => 'destroy_payment_test'));
     $pay->save();
     $deleted_items = array('company' => $c->id, 'project' => $p->id, 'estimate' => $e->id, 'hour' => $h->id, 'support_hour' => $sup_hr->id, 'charge' => $ch->id, 'support_contract' => $con->id, 'payment' => $pay->id);
     $c->destroyAssociatedRecords();
     $c->delete();
     $dbcon = AMP::getDb();
     foreach ($deleted_items as $table => $id) {
         if ($table == 'support_hour') {
             $table = 'hour';
         }
         $sql = 'SELECT * FROM ' . $table . ' WHERE id = ' . $id;
         if ($records = $dbcon->Execute($sql)) {
             $this->assertEqual($records->RecordCount(), 0, "{$table} not deleted correctly: %s");
         } else {
             trigger_error($dbcon->ErrorMsg());
         }
     }
 }
 /**
  * Delete Company
  *
  * @param void
  * @return null
  */
 function delete()
 {
     if ($this->active_company->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_company->canDelete($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN, null, true, $this->request->isApiCall());
     }
     // if
     if ($this->active_company->isNew() || $this->active_company->isOwner()) {
         $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
     }
     // if
     if ($this->request->isSubmitted()) {
         $old_name = $this->active_company->getName();
         $delete = $this->active_company->delete();
         if ($delete && !is_error($delete)) {
             if ($this->request->isApiCall()) {
                 $this->httpOk();
             } else {
                 flash_success("Company ':name' has been deleted", array('name' => $old_name));
                 $this->redirectTo('people');
             }
             // if
         } else {
             if ($this->request->isApiCall()) {
                 $this->httpError(HTTP_ERR_OPERATION_FAILED, null, true, $this->request->isApiCall());
             } else {
                 flash_error("Failed to delete :name", array('name' => $old_name));
                 $this->redirectTo('people');
             }
             // if
         }
         // if
     } else {
         $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, $this->request->isApiCall());
     }
     // if
 }
Exemple #3
0
 function testCalculateBalanceWithDateRange()
 {
     #Company
     $cp = new Company();
     $cp->set(array('name' => 'Test Company', 'status' => 'active'));
     $cp->save();
     $pb = new CompanyPreviousBalance();
     $pb->set(array('company_id' => $cp->id, 'amount' => 600.25, 'date' => '2010-01-30'));
     $pb->save();
     $this->assertWithinMargin($pb->getAmount(), '600.25', '.01');
     ######### Support
     $sc = new SupportContract();
     $sc->set(array('company_id' => $cp->id, 'domain_name' => 'Test', 'start_date' => '2010-01-01', 'end_date' => '2010-04-30', 'hourly_rate' => '120', 'support_hours' => '.5', 'monthly_rate' => '50'));
     $sc->save();
     # add support hours
     # before previous balance
     $h = new Hour();
     $h->set(array('description' => 'Test', 'support_contract_id' => $sc->id, 'date' => '2010-01-20', 'hours' => '2.5'));
     $h->save();
     # in range
     $h = new Hour();
     $h->set(array('description' => 'Test', 'support_contract_id' => $sc->id, 'date' => '2010-02-20', 'hours' => '2.5'));
     $h->save();
     # in range
     $h = new Hour();
     $h->set(array('description' => 'Test', 'support_contract_id' => $sc->id, 'date' => '2010-03-20', 'hours' => '2.5'));
     $h->save();
     # out of range
     $h = new Hour();
     $h->set(array('description' => 'Test', 'support_contract_id' => $sc->id, 'date' => '2010-05-20', 'hours' => '2'));
     $h->save();
     ### Support Totals = in range is 2 months x 50 = 100, 4 @ 120 = 480 = 580
     $date_range = array('start_date' => '2010-01-01', 'end_date' => '2010-03-31');
     $total = $cp->calculateSupportTotal($date_range);
     $this->assertEqual($total, 580);
     ###### Project
     $pj = new Project();
     $pj->set(array('name' => 'Test Project', 'company_id' => $cp->id, 'hourly_rate' => '120'));
     $pj->save();
     # Add an Estimate item #1
     $es1 = new Estimate();
     $es1->set(array('project_id' => $pj->id, 'name' => 'Test Estimate 1', 'high_hours' => '10', 'low_hours' => '5'));
     $es1->save();
     # Add an Estimate item #2
     $es2 = new Estimate();
     $es2->set(array('project_id' => $pj->id, 'name' => 'Test Estimate 2', 'high_hours' => '10', 'low_hours' => '5'));
     $es2->save();
     # Add some before previous balance hours for #1 - 5 hours at 120 = 600
     $hr = new Hour();
     $hr->set(array('estimate_id' => $es1->id, 'description' => 'Test Hours for Estimate 1', 'date' => '2010-01-15', 'hours' => '5'));
     $hr->save();
     # Add some in range hours for #1 - 5 hours at 120 = 600
     $hr = new Hour();
     $hr->set(array('estimate_id' => $es1->id, 'description' => 'Test Hours for Estimate 1', 'date' => '2010-02-15', 'hours' => '5'));
     $hr->save();
     # Add some in range hours for #2 - 5 hours at 120 = 600
     $hr = new Hour();
     $hr->set(array('estimate_id' => $es2->id, 'description' => 'Test Hours for Estimate 2', 'date' => '2010-02-15', 'hours' => '5'));
     $hr->save();
     # Add some out of range hours for #2 - 5 hours at 120 = 600
     $hr = new Hour();
     $hr->set(array('estimate_id' => $es2->id, 'description' => 'Test Hours for Estimate 2', 'date' => '2010-05-15', 'hours' => '5'));
     $hr->save();
     ## Project Totals = In range 1200, out of range 1800
     $date_range = array('start_date' => '2010-01-01', 'end_date' => '2010-03-31');
     $total = $cp->calculateProjectsTotal($date_range);
     $this->assertEqual($total, 1200);
     #Charge
     # before previous balance
     $cr = new Charge();
     $cr->set(array('name' => 'Test', 'company_id' => $cp->id, 'date' => '2010-01-10', 'amount' => '20.50'));
     $cr->save();
     # in date range
     $cr = new Charge();
     $cr->set(array('name' => 'Test', 'company_id' => $cp->id, 'date' => '2010-03-14', 'amount' => '50.25'));
     $cr->save();
     # in date range
     $cr = new Charge();
     $cr->set(array('name' => 'Test', 'company_id' => $cp->id, 'date' => '2010-03-20', 'amount' => '50'));
     $cr->save();
     # out of date range
     $cr = new Charge();
     $cr->set(array('name' => 'Test', 'company_id' => $cp->id, 'date' => '2010-05-15', 'amount' => '50'));
     $cr->save();
     # Total Charges = in range 100.25, out of range 150.25
     $date_range = array('start_date' => '2010-01-01', 'end_date' => '2010-03-31');
     $charge_total = $cp->calculateChargesTotal($date_range);
     $this->assertEqual($charge_total, 100.25);
     ## Test Total Costs
     # Charges 100.25 + project 1200 + support 580
     $date_range = array('start_date' => '2010-01-01', 'end_date' => '2010-03-31');
     $total = $cp->calculateCosts($date_range);
     $this->assertEqual($total, 1880.25);
     ## Payments
     # add payment before previous balance date
     $py = new Payment();
     $py->set(array('company_id' => $cp->id, 'date' => '2010-01-22', 'amount' => '20.50'));
     $py->save();
     # add payment in range
     $py = new Payment();
     $py->set(array('company_id' => $cp->id, 'date' => '2010-02-10', 'amount' => '20.00'));
     $py->save();
     # add payment in range
     $py = new Payment();
     $py->set(array('company_id' => $cp->id, 'date' => '2010-03-01', 'amount' => '120.00'));
     $py->save();
     # add payment out of range
     $py = new Payment();
     $py->set(array('company_id' => $cp->id, 'date' => '2010-04-01', 'amount' => '20.25'));
     $py->save();
     # Total Payments are 20 + 120 = 140 in range and after previous balance
     $date_range = array('start_date' => '2010-01-01', 'end_date' => '2010-03-31');
     $payment_total = $cp->calculatePaymentsTotal($date_range);
     $this->assertEqual($payment_total, 140);
     #### fails because the previous balance isn't getting included FIX ME!!
     # Total Balance Costs 1880.25 - Payments 140 + Previous balance 600.25 = 2340.5
     $date_range = array('start_date' => '2010-01-01', 'end_date' => '2010-03-31');
     $balance = $cp->calculateBalance($date_range);
     $this->assertWithinMargin($balance, 2340.5, 0.001);
     ## clean up
     $cp->destroyAssociatedRecords();
     $cp->delete();
 }
Exemple #4
0
<?php

require_once './global.inc.php';
verify_oauth_session_exists();
if (HttpSession::currentUser()->getOrganization()->access_level < 4) {
    echo "Access denied";
    die;
}
$company_id = $_POST['company_id'];
$result = Company::delete($company_id);
echo json_encode($result);
Exemple #5
0
        Flight::ok($object);
    } catch (Exception $exception) {
        Flight::error($exception);
    }
});
Flight::route('PUT /v1/main/company/@id', function ($id) {
    try {
        $object = Company::update($id);
        Flight::ok($object);
    } catch (Exception $exception) {
        Flight::error($exception);
    }
});
Flight::route('DELETE /v1/main/company/@id', function ($id) {
    try {
        $object = Company::delete($id);
        Flight::ok($object);
    } catch (Exception $exception) {
        Flight::error($exception);
    }
});
//=============================================================================
//CompanyInfo
//=============================================================================
Flight::route('GET /v1/main/companyinfo', function () {
    $company = Flight::request()->query->company;
    try {
        if ($company) {
            $object = CompanyInfo::selectByCompany($company);
        } else {
            $object = CompanyInfo::selectAll();
Exemple #6
0
                        if ($action == "update" && Company::exist($key)) {
                            //存在,并且需要更新
                            Company::update($company);
                        } else {
                            Company::insert($company);
                        }
                    }
                }
            }
        } else {
            if ($action == "del") {
                $id = isset($_REQUEST["companyId"]) ? $_REQUEST["companyId"] : 0;
                //是否允许重置添
                $company = new Company(_NONE);
                $company->id = $id;
                Company::delete($company);
            }
        }
    } catch (Exception $e) {
        $result = $e->getMessage();
        Tool::logger(__METHOD__, __LINE__, sprintf("保存公司信息失败: %s", $e->getMessage()), _LOG_ERROR);
    }
} else {
    if ($dataType == "file") {
        try {
            $file = new DocFile(_NONE);
            $file->savedPath = $_REQUEST["file_path"];
            DocFile::delete($file);
            if (is_file("../" . $file->savedPath)) {
                @unlink("../" . $file->savedPath);
            }
 private function importCompany($fileNameAndPath)
 {
     $allAccounts = array();
     $dom = new domDocument();
     if (!$dom->load($fileNameAndPath)) {
         throw new CException(Yii::t('lazy8', 'input file could not be xml parsed'));
     }
     $root = $dom->documentElement;
     if ($root->nodeName != "lazy8webport") {
         $this->hasErrors = true;
         $this->errors = array(array(Yii::t('lazy8', 'Upload failed.  This is not a valid file.'), Yii::t('lazy8', 'Select a file and try again')));
         $this->render('showimport');
         return 0;
     }
     if ($root->getAttribute('version') > 1.0) {
         $this->errors = array(array(Yii::t('lazy8', 'There maybe problems because this is a file version greater then this programs version'), Yii::t('lazy8', 'Select a file and try again')));
     }
     $nodeCompanys = $root->getElementsByTagName('company');
     unset($root);
     unset($dom);
     $this->lastImportedPeriod = 0;
     foreach ($nodeCompanys as $nodeCompany) {
         //make sure the company code is unique
         $modelCompany = new Company();
         $code = $nodeCompany->getAttribute('code');
         $code--;
         //make sure the company code is valid. Change if not.
         do {
             $code++;
             $comptest = Company::model()->find(array('condition' => 'code=' . $code));
         } while ($comptest !== null);
         //create the company
         $modelCompany = new Company();
         $modelCompany->code = $code;
         $modelCompany->name = $nodeCompany->getAttribute('name');
         $modelCompany->lastAbsTransNum = $nodeCompany->getAttribute('lastAbsTransNum');
         if (!$modelCompany->save()) {
             throw new CException(Yii::t('lazy8', 'Could not create the company, bad paramters') . ';' . var_export($modelCompany->getErrors()));
         }
         try {
             $allAccounts = array();
             $nodesAccountTypes = $nodeCompany->getElementsByTagName('accounttype');
             foreach ($nodesAccountTypes as $nodeAccountType) {
                 $modelAccountType = new AccountType();
                 $modelAccountType->companyId = $modelCompany->id;
                 $modelAccountType->code = $nodeAccountType->getAttribute('code');
                 $modelAccountType->name = $nodeAccountType->getAttribute('name');
                 $modelAccountType->sortOrder = $nodeAccountType->getAttribute('sortorder');
                 $modelAccountType->isInBalance = $nodeAccountType->getAttribute('isinbalance') == "1" ? 1 : 0;
                 if (!$modelAccountType->save()) {
                     $modelCompany->delete();
                     throw new CException(Yii::t('lazy8', 'Could not create the AccountType, bad paramters') . ';name=' . $modelAccountType->name . ';' . serialize($modelAccountType->getErrors()));
                 }
                 $nodesAccounts = $nodeAccountType->getElementsByTagName('account');
                 foreach ($nodesAccounts as $nodeAccount) {
                     $modelAccount = new Account();
                     $modelAccount->companyId = $modelCompany->id;
                     $modelAccount->code = $nodeAccount->getAttribute('code');
                     $modelAccount->accountTypeId = $modelAccountType->id;
                     $modelAccount->name = $nodeAccount->getAttribute('name');
                     if (!$modelAccount->save()) {
                         $modelCompany->delete();
                         throw new CException(Yii::t('lazy8', 'Could not create the Account, bad paramters') . ';' . serialize($modelAccount->getErrors()));
                     }
                     $allAccounts[$modelAccount->code] = $modelAccount->id;
                     unset($nodeAccount);
                     unset($modelAccount);
                 }
                 unset($modelAccountType);
                 unset($nodeAccountType);
             }
             unset($nodesAccountTypes);
             $allCustomers = array();
             $nodesCustomers = $nodeCompany->getElementsByTagName('customer');
             foreach ($nodesCustomers as $nodeCustomer) {
                 $modelCustomer = new Customer();
                 $modelCustomer->companyId = $modelCompany->id;
                 $modelCustomer->code = $nodeCustomer->getAttribute('code');
                 $modelCustomer->accountId = $this->FindAccountIdFromCode($nodeCustomer->getAttribute('accountcode'), $modelCompany->id, $allAccounts, 'customercode=' . $modelCustomer->code, $this->errors, true);
                 $modelCustomer->name = $nodeCustomer->getAttribute('name');
                 $modelCustomer->desc = $nodeCustomer->getAttribute('desc');
                 if (!$modelCustomer->save()) {
                     $modelCompany->delete();
                     throw new CException(Yii::t('lazy8', 'Could not create the Customer, bad paramters') . ';' . serialize($modelCustomer->getErrors()));
                 }
                 $allCustomers[$modelCustomer->code] = $modelCustomer->id;
                 unset($modelCustomer);
                 unset($nodeCustomer);
             }
             unset($nodesCustomers);
             $nodesPeriods = $nodeCompany->getElementsByTagName('period');
             foreach ($nodesPeriods as $nodePeriod) {
                 $modelPeriod = new Period();
                 $modelPeriod->companyId = $modelCompany->id;
                 $modelPeriod->dateStart = $nodePeriod->getAttribute('datestart');
                 $modelPeriod->dateEnd = $nodePeriod->getAttribute('dateend');
                 $modelPeriod->lastPeriodTransNum = $nodePeriod->getAttribute('lastperiodtransnum');
                 if (!$modelPeriod->save()) {
                     $modelCompany->delete();
                     throw new CException(Yii::t('lazy8', 'Could not create the period, bad paramters') . ';' . serialize($modelPeriod->getErrors()));
                 }
                 $this->lastImportedPeriod = $modelPeriod->id;
                 $nodesTransactions = $nodePeriod->getElementsByTagName('transaction');
                 foreach ($nodesTransactions as $nodeTransaction) {
                     $modelTransaction = new Trans();
                     $modelTransaction->companyId = $modelCompany->id;
                     $modelTransaction->companyNum = $nodeTransaction->getAttribute('code');
                     $modelTransaction->periodId = $modelPeriod->id;
                     $modelTransaction->periodNum = $nodeTransaction->getAttribute('periodnum');
                     $modelTransaction->regDate = $nodeTransaction->getAttribute('regdate');
                     $modelTransaction->invDate = $nodeTransaction->getAttribute('invdate');
                     $modelTransaction->notes = $nodeTransaction->getAttribute('notes');
                     $modelTransaction->fileInfo = $nodeTransaction->getAttribute('fileinfo');
                     if (!$modelTransaction->save()) {
                         $modelCompany->delete();
                         throw new CException(Yii::t('lazy8', 'Could not create the Transaction, bad paramters') . ';' . serialize($modelTransaction->getErrors()));
                     }
                     $nodesTransactionAmounts = $nodeTransaction->getElementsByTagName('amount');
                     foreach ($nodesTransactionAmounts as $nodeTransactionAmount) {
                         $modelTransRow = new TransRow();
                         $modelTransRow->transId = $modelTransaction->id;
                         $modelTransRow->accountId = $this->FindAccountIdFromCode($nodeTransactionAmount->getAttribute('accountcode'), $modelCompany->id, $allAccounts, 'TransCode=' . $modelTransaction->companyNum, $this->errors, true, true);
                         $modelTransRow->customerId = $this->FindCustomerIdFromCode($nodeTransactionAmount->getAttribute('customercode'), $modelCompany->id, $allCustomers, 'TransCode=' . $modelTransaction->companyNum, $this->errors, true);
                         $modelTransRow->notes = $nodeTransactionAmount->getAttribute('notes');
                         $modelTransRow->amount = $nodeTransactionAmount->getAttribute('amount');
                         if (!$modelTransRow->save()) {
                             $modelCompany->delete();
                             throw new CException(Yii::t('lazy8', 'Could not create the TransactionAmount, bad paramters') . ';' . serialize($modelTransRow->getErrors()));
                         }
                         unset($modelTransRow);
                     }
                     unset($modelTransaction);
                     unset($nodesTransactionAmounts);
                     unset($nodeTransaction);
                 }
                 unset($modelPeriod);
                 unset($nodePeriod);
                 unset($nodesTransactions);
             }
             unset($nodesPeriods);
         } catch (Exception $e) {
             $modelCompany->delete();
             throw $e;
         }
         $errors = array();
         //we ignore the errors...
         yii::app()->onImport(new Lazy8Event(array('importobject' => 'Company', 'root' => $nodeCompany, 'errors' => $errors), $modelCompany->id));
         unset($nodeCompany);
     }
     unset($nodeCompanys);
     ChangeLog::addLog('ADD', 'Company', 'Imported company ' . $modelCompany->toString());
     return $modelCompany->id;
 }