public function store() { $nominal = Input::get('nominal'); $nominal = str_replace(",", ".", $nominal); $nominal = str_replace(".", "", $nominal); $nominal = substr($nominal, 0, -2); $charge = new Charge(); $charge->project_id = Auth::user()->curr_project_id; $charge->name = Input::get('name'); $charge->nominal = $nominal; $charge->description = Input::get('description'); $charge->save(); Session::flash('message', 'Sukses menambahkan Charge baru!'); }
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()); } } }
/** * Store a newly created charge in storage. * * @return Response */ public function store() { $validator = Validator::make($data = Input::all(), Charge::$rules); if ($validator->fails()) { return Redirect::back()->withErrors($validator)->withInput(); } $charge = new Charge(); $charge->name = Input::get('name'); $charge->category = Input::get('category'); $charge->calculation_method = Input::get('calculation_method'); $charge->payment_method = Input::get('payment_method'); $charge->percentage_of = Input::get('percentage_of'); $charge->amount = Input::get('amount'); if (Input::get('fee') == '1') { $charge->fee = TRUE; } else { $charge->fee = FALSE; } $charge->save(); return Redirect::route('charges.index'); }
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(); }
/** * executeRequest() * * Execute the received request. */ private function executeRequest() { switch ($this->requestType) { case self::GET_USER_REQUEST: case self::GET_VEHICLES_REQUEST: try { $user = Doctrine_Core::getTable('sfGuardUser')->createQuery('u')->where('u.api_key = ?', $this->requestApiKey)->andwhere('u.username = ?', $this->requestUsername)->execute(); if (sizeof($user) == 1) { $this->responseUser = $user[0]; $this->responseVehicles = Doctrine_Core::getTable('Vehicle')->createQuery('v')->where('v.user_id = ?', $user[0]->getId())->execute(); } else { $this->setError(211); } } catch (Exception $e) { $this->setError(230); } break; case self::SET_CHARGE_REQUEST: try { $user = Doctrine_Core::getTable('sfGuardUser')->createQuery('u')->where('u.api_key = ?', $this->requestApiKey)->andwhere('u.username = ?', $this->requestUsername)->execute(); if (sizeof($user) == 1) { $this->responseUser = $user[0]; $this->responseVehicles = Doctrine_Core::getTable('Vehicle')->createQuery('v')->where('v.user_id = ?', $user[0]->getId())->andwhere('v.id = ?', $this->requestVehicle)->execute(); if (sizeof($this->responseVehicles) == 1) { $charge = new Charge(); $charge->setVehicleId($this->responseVehicles[0]->getId()); $charge->setUserId($this->responseUser->getId()); $charge->setCategoryId($this->requestCategory); $charge->setDate($this->requestDate); $charge->setKilometers($this->requestKilometers); $charge->setAmount($this->requestAmount); $charge->setComment($this->requestComment); $charge->setQuantity($this->requestQuantity); $charge->save(); } else { $this->setError(220); } } else { $this->setError(211); } } catch (Exception $e) { $this->setError(230); } break; default: $this->setError(500); break; } }
/** * Duplicate a charge */ public function duplicate($id) { $charge = $this->dataExist($id); $newCharge = new Charge(); $newCharge->date_charge = date('Y-m-d'); $date = new DateTime($newCharge->date_charge); $date->modify('+1 month'); $newCharge->deadline = $date->format('Y-m-d'); $newCharge->organisation_id = $charge->organisation_id; if ($newCharge->save()) { foreach ($charge->items as $item) { $addItem = new ChargeItem(); $addItem->insert(array('charge_id' => $newCharge->id, 'description' => $item->description, 'amount' => $item->amount, 'vat_types_id' => $item->vat_types_id)); } foreach ($charge->tags as $tag) { $chargeTag = new ChargeTag(); $chargeTag->charge_id = $newCharge->id; $chargeTag->tag_id = $tag->id; $chargeTag->save(); } return Redirect::route('charge_modify', $newCharge->id); } }
/** * Billing and Print */ public function actionGeneratePrint() { $model = Book::model()->findByPK($_GET["id"]); // Change Book status $model->Type = 1; $model->save(); $items = array(); $startDate = strtotime($model->Start); $finishDate = strtotime($model->Finish); $roomID = $model->RoomID; $Pax = $model->Pax; // Create Invoice $modelInvoice = new Invoice(); $modelInvoice->UserID = Yii::app()->user->ID; $modelInvoice->CustomerID = $model->CustomerID; $modelInvoice->Date = date("Y-m-d"); $criteriaNextNumber = new CDbCriteria(); $criteriaNextNumber->condition = 'UserID = :userid'; $criteriaNextNumber->params = array(':userid' => Yii::app()->user->ID); $criteriaNextNumber->order = 'Number Desc'; $modelInvoice->Number = Invoice::model()->find($criteriaNextNumber)->ID + 1; $modelInvoice->SerieID = Serie::model()->findByAttributes(array('Pred' => '1'))->ID; $modelInvoice->BookID = $model->ID; $modelInvoice->save(); $days = round(($finishDate - $startDate) / 86400); $price = 0; // Calculate Room price if (Room::model()->findByPK($roomID)->Type == 0) { for ($i = 0; $i < $days; $i++) { $date = $startDate + $i * 86400; $season = Book::model()->getSeason(date("Y-m-d", $date)); $modelCharge = new Charge(); $modelCharge->UserID = Yii::app()->user->ID; $modelCharge->InvoiceID = $modelInvoice->ID; $modelCharge->IVA = User::model()->findByPK(Yii::app()->user->ID)->IVA; switch ($season) { case 0: $items[] = array($model->room->PriceLow, $model->room->Name . " (Temporada Baja)."); $price = $price + Room::model()->findByPK($roomID)->PriceLow; $modelCharge->Text = $model->room->Name . " (Temporada Baja)."; $modelCharge->Price = Room::model()->findByPK($roomID)->PriceLow; break; case 1: $items[] = array($model->room->PriceMed, $model->room->Name . " (Temporada Media)."); $price = $price + Room::model()->findByPK($roomID)->PriceMed; $modelCharge->Text = $model->room->Name . " (Temporada Media)."; $modelCharge->Price = Room::model()->findByPK($roomID)->PriceMed; break; case 2: $items[] = array($model->room->PriceBig, $model->room->Name . " (Temporada Alta)."); $price = $price + Room::model()->findByPK($roomID)->PriceBig; $modelCharge->Text = $model->room->Name . " (Temporada Alta)."; $modelCharge->Price = Room::model()->findByPK($roomID)->PriceBig; break; } $modelCharge->save(); } } else { for ($i = 0; $i < $days; $i++) { $date = $startDate + $i * 86400; $season = Book::model()->getSeason(date("Y-m-d", $date)); $modelCharge = new Charge(); $modelCharge->UserID = Yii::app()->user->ID; $modelCharge->InvoiceID = $modelInvoice->ID; $modelCharge->IVA = User::model()->findByPK(Yii::app()->user->ID)->IVA; switch ($season) { case 0: $items[] = array(Room::model()->findByPK($roomID)->PriceLow * $Pax, $model->room->Name . " (" . $Pax . " personas Temporada Baja)."); $price = $price + Room::model()->findByPK($roomID)->PriceLow * $Pax; $modelCharge->Text = $model->room->Name . " (" . $Pax . " personas Temporada Baja)."; $modelCharge->Price = Room::model()->findByPK($roomID)->PriceLow * $Pax; break; case 1: $items[] = array(Room::model()->findByPK($roomID)->PriceMed * $Pax, $model->room->Name . " (" . $Pax . " personas Temporada Media)."); $price = $price + Room::model()->findByPK($roomID)->PriceMed * $Pax; $modelCharge->Text = $model->room->Name . " (" . $Pax . " personas Temporada Media)."; $modelCharge->Price = Room::model()->findByPK($roomID)->PriceMed * $Pax; break; case 2: $items[] = array(Room::model()->findByPK($roomID)->PriceBig * $Pax, $model->room->Name . " (" . $Pax . " personas Temporada Alta)."); $price = $price + Room::model()->findByPK($roomID)->PriceBig * $Pax; $modelCharge->Text = $model->room->Name . " (" . $Pax . " personas Temporada Alta)."; $modelCharge->Price = Room::model()->findByPK($roomID)->PriceBig * $Pax; break; } $modelCharge->save(); } } // Add extras to price if ($model->ID > 0) { $criteria = new CDbCriteria(); $criteria->condition = "UserID = :userid AND BookID = :bookid"; $criteria->params = array(':userid' => Yii::app()->user->ID, ':bookid' => $model->ID); $modelExtra = Extra::model()->findAll($criteria); foreach ($modelExtra as $extra) { $modelCharge = new Charge(); $modelCharge->UserID = Yii::app()->user->ID; $modelCharge->InvoiceID = $modelInvoice->ID; $modelCharge->IVA = User::model()->findByPK(Yii::app()->user->ID)->IVA; $items[] = array($extra->Value, $extra->Name . " (Suplemento)"); $modelCharge->Text = $extra->Name . " (Suplemento)"; $modelCharge->Price = $extra->Value; $price = $price + $extra->Value; $modelCharge->save(); } } // Apply discount to price if ($model->ID > 0) { $modelDiscount = Discount::model()->findAll($criteria); foreach ($modelDiscount as $discount) { $modelCharge = new Charge(); $modelCharge->UserID = Yii::app()->user->ID; $modelCharge->InvoiceID = $modelInvoice->ID; $modelCharge->IVA = User::model()->findByPK(Yii::app()->user->ID)->IVA; $items[] = array(-($discount->Value * $price) / 100, $discount->Name . " (Descuento)"); $modelCharge->Text = $discount->Name . " (Descuento)"; $modelCharge->Price = -($discount->Value * $price / 100); $price = $price + -($discount->Value * $price) / 100; $modelCharge->save(); } } // Print invoice set_time_limit(600); # mPDF $pdf = Yii::app()->ePdf->mpdf('', 'A4', '', '', '', '', '', '', '', '', 'P'); $pdf->writeHTMLfooter = false; $pdf->writeHTMLheader = false; $pdf->DeflMargin = 25; $pdf->DefrMargin = 25; $pdf->tMargin = 15; $pdf->bMargin = 15; $pdf->w = 297; //manually set width $pdf->h = 209.8; //manually set height $pdf->WriteHTML($this->renderPartial('printbill', array('model' => $model, 'items' => $items, 'price' => $price), true)); # Outputs ready PDF $pdf->Output('Factura_' . date("d/m/Y") . '.pdf', 'D'); }
/** * Add charge to Invoice */ public function actionAddCharge() { $invoiceID = $_POST["invoice"]; $name = $_POST["charge"]; $value = $_POST["value"]; $model = new Charge(); $model->InvoiceID = $invoiceID; $model->Text = $name; $model->Price = $value; $model->IVA = User::model()->findByPK(Yii::app()->user->ID)->IVA; $model->UserID = Yii::app()->user->ID; if ($model->save()) { $criteria = new CDbCriteria(); $criteria->condition = "UserID = :userid AND InvoiceID = :invoiceid"; $criteria->params = array(':userid' => Yii::app()->user->ID, ':invoiceid' => $invoiceID); $modelCharges = Charge::model()->findAll($criteria); foreach ($modelCharges as $charge) { echo "<p><span class='deleteCharge' id='" . $charge->ID . "'>X</span>" . $charge->Text . " | " . str_replace('.', ',', $charge->Price) . "€</p>"; } } else { var_dump($model->errors); } echo ' <script> $(".deleteCharge").click(function(){ if(confirm("\\u00bfSeguro que quieres eliminar este concepto?")) { var request = $.ajax({ url: "' . $this->createURL("invoice/deleteCharge") . '", type: "POST", dataType: "html", data: { charge : $(this).attr("id"), invoice : ' . $model->InvoiceID . ' } }); request.done(function(msg) { $("#concepts").html(msg); }); } }); </script> '; }