Skip to content

Commit

Permalink
materials transfer implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rdex committed Apr 9, 2014
1 parent 004de05 commit db4d8cb
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 5 deletions.
150 changes: 150 additions & 0 deletions apps/frontend/modules/MaterialMovement/actions/actions.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/**
* MaterialMovement actions.
*
* @package xxi
* @subpackage MaterialMovement
* @author Saritskiy Roman
* @version SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
*/
class MaterialMovementActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
$this->material_movements = Doctrine_Query::create()
->from('MaterialMovement m')
->execute()
;
}

public function executeNewTransfer(sfWebRequest $request)
{
$this->warehouse = Doctrine_Core::getTable("Warehouse")->find($request->getParameter("from"));
$this->forward404Unless($this->warehouse);
$this->balance = $this->warehouse->getBalance();

if ($request->isMethod("post")) {
$transfer = MaterialMovementTransfer::createFromArray($request->getParameter("Transfer"));
$transfer->save();

$movement = MaterialMovement::createFromArray([
"from_id" => $request->getParameter("from"),
"to_id" => $request->getParameter("to"),
"type" => "transfer",
"transfer_id" => $transfer->getId(),
]);
$movement->save();

$list = new Doctrine_Collection("MaterialMovementMaterials");
foreach ($request->getParameter("materials") as $id => $amount) {
if ($amount > 0) {
$material = &$this->balance[$id];
while ($amount) {
arsort($material["amounts"]);
array_filter($material["amounts"]);

list($price, $availableToMove) = [array_keys($material["amounts"])[0], current($material["amounts"])];

$moved = $availableToMove >= $amount
? $amount
: $availableToMove
;

$materialsMovement = MaterialMovementMaterials::createFromArray([
"movement_id" => $movement->getId(),
"material_id" => $id,
"amount" => $moved,
"price" => $price,
]);
$list->add($materialsMovement);

$amount -= $moved;
$material["amount"] -= $moved;
$material["amounts"][$price] -= $moved;
}
}
}
$list->save();

$this->redirect("warehouse/index");
} else {
$this->form = new sfForm();
$this->form->getWidgetSchema()
->offsetSet("to", new sfWidgetFormDoctrineChoice([
"model" => "Warehouse",
"add_empty" => false,
"query" => Doctrine_Query::create()
->from("Warehouse w")
->addWhere("w.id != ?", $request->getParameter("from"))
->addOrderby("w.name"),
]))
->offsetSet("from", new sfWidgetFormInputHidden())
;

$this->form->embedForm("Transfer", new MaterialMovementTransferForm());
$this->form->getWidgetSchema()
->setLabels([
"to" => "Склад-получатель",
"Transfer" => "Комментарий к перемещению",
])
->setDefaults([
"from" => $request->getParameter("from"),
])
;
}
}

public function executeNew(sfWebRequest $request)
{
$this->form = new MaterialMovementForm();
}

public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST));

$this->form = new MaterialMovementForm();

$this->processForm($request, $this->form);

$this->setTemplate('new');
}

public function executeEdit(sfWebRequest $request)
{
$this->forward404Unless($material_movement = Doctrine_Core::getTable('MaterialMovement')->find(array($request->getParameter('id'))), sprintf('Object material_movement does not exist (%s).', $request->getParameter('id')));
$this->form = new MaterialMovementForm($material_movement);
}

public function executeUpdate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
$this->forward404Unless($material_movement = Doctrine_Core::getTable('MaterialMovement')->find(array($request->getParameter('id'))), sprintf('Object material_movement does not exist (%s).', $request->getParameter('id')));
$this->form = new MaterialMovementForm($material_movement);

$this->processForm($request, $this->form);

$this->setTemplate('edit');
}

public function executeDelete(sfWebRequest $request)
{
$request->checkCSRFProtection();

$this->forward404Unless($material_movement = Doctrine_Core::getTable('MaterialMovement')->find(array($request->getParameter('id'))), sprintf('Object material_movement does not exist (%s).', $request->getParameter('id')));
$material_movement->delete();

$this->redirect('MaterialMovement/index');
}

protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid()) {
$material_movement = $form->save();

$this->redirect('MaterialMovement/edit?id='.$material_movement->getId());
}
}
}
27 changes: 27 additions & 0 deletions apps/frontend/modules/MaterialMovement/templates/_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>

<form action="<?php echo url_for('MaterialMovement/'
. ($form->getObject()->isNew() ? 'create' : 'update')
. (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))
?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>

<?php echo $form->renderUsing('bootstrap') ?>

<?php if (!$form->getObject()->isNew()): ?>
<input type="hidden" name="sf_method" value="put" />
<?php endif ?>

<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<a href="<?php echo url_for('MaterialMovement/index') ?>" class="btn">Back to list</a>

<?php if (!$form->getObject()->isNew()): ?>
<?php echo link_to('Delete', 'MaterialMovement/delete?id='.$form->getObject()->getId(), array(
'method' => 'delete',
'confirm' => 'Are you sure?',
'class' => 'btn btn-warning pull-right',
)) ?>
<?php endif ?>
</div>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php slot('title', 'Edit Material movement') ?>

<h1 class="page-header">
Edit Material movement
</h1>

<?php include_partial('form', array('form' => $form)) ?>
48 changes: 48 additions & 0 deletions apps/frontend/modules/MaterialMovement/templates/indexSuccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php slot('title', 'Material movements List') ?>

<h1 class="page-header">
Material movements List
</h1>

<div class="btn-toolbar">
<div class="btn-group">
<a href="<?php echo url_for('MaterialMovement/new') ?>" class="btn btn-primary">New</a>
</div>
</div>

<table class="table table-condensed table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Type</th>
<th>From</th>
<th>To</th>
<th>Transfer</th>
<th>Arrival</th>
<th>Utilization</th>
<th>Writeoff</th>
<th>Created at</th>
<th>Updated at</th>
<th>Created by</th>
<th>Updated by</th>
<th>Deleted at</th>
</tr>
</thead>
<tbody><?php foreach ($material_movements as $material_movement): ?>
<tr>
<td><a href="<?php echo url_for('MaterialMovement/edit?id='.$material_movement->getId()) ?>"><?php echo $material_movement->getId() ?></a></td>
<td><?php echo $material_movement->getType() ?></td>
<td><?php echo $material_movement->getFromId() ?></td>
<td><?php echo $material_movement->getToId() ?></td>
<td><?php echo $material_movement->getTransferId() ?></td>
<td><?php echo $material_movement->getArrivalId() ?></td>
<td><?php echo $material_movement->getUtilizationId() ?></td>
<td><?php echo $material_movement->getWriteoffId() ?></td>
<td><?php echo $material_movement->getCreatedAt() ?></td>
<td><?php echo $material_movement->getUpdatedAt() ?></td>
<td><?php echo $material_movement->getCreatedBy() ?></td>
<td><?php echo $material_movement->getUpdatedBy() ?></td>
<td><?php echo $material_movement->getDeletedAt() ?></td>
</tr>
<?php endforeach; ?></tbody>
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php slot('title', 'New Material movement') ?>

<h1 class="page-header">
New Material movement
</h1>

<?php include_partial('form', array('form' => $form)) ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<form action="?submit" method="post">
<?php echo $form->renderUsing("bootstrap"); ?>

<fieldset>
<legend>Список материалов</legend>
<?php foreach ($balance->getRawValue() as $material): ?>
<div class="control-group">
<label for="materials[<?php echo $material["id"]; ?>]" class="control-label"><?php echo $material["name"]; ?></label>
<div class="controls">
<div class="input-append copy-number-inputs-max-to-value">
<input type="number" value="0" min="0" max="<?php echo $material["amount"]; ?>" step="0.0001" class="span2" name="materials[<?php echo $material["id"]; ?>]" id="materials[<?php echo $material["id"]; ?>]">
<button class="btn" type="button">Выбрать всё (<?php echo $material["amount"]; ?>)</button>
</div>
</div>
</div>
<?php endforeach ?>
</fieldset>

<div class="form-actions">
<button type="submit" class="btn btn-primary">Переместить</button>
</div>
</form>

<script>
$(function() {
$(".copy-number-inputs-max-to-value .btn").click(function(e) {
e.preventDefault();

var $this = $(this)
, input = $this.siblings("input[type=number]")
;

if (input.length) {
input.val(input.attr("max"));
}
});
});
</script>
11 changes: 7 additions & 4 deletions apps/frontend/modules/warehouse/templates/indexSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
<?php if ($warehouses and count($warehouses)): ?>
<?php foreach ($warehouses as $warehouse): ?>
<h3>
<?php echo $warehouse ?>
<?php if ($sf_user->hasCredential("can edit warehouses")): ?>
<small>
<a href="<?php echo url_for('warehouse/edit?id='.$warehouse->getId()) ?>">Редактировать</a>
</small>
<a href="<?php echo url_for('warehouse/edit?id='.$warehouse->getId()) ?>"><?php echo $warehouse ?></a>
<?php else: ?>
<?php echo $warehouse ?>
<?php endif ?>
<?php if ($sf_user->hasCredential("can transfer material")): ?>
<a href="<?php echo url_for("MaterialMovement/newTransfer?from=" . $warehouse->getId()); ?>" class="btn btn-small">Переместить материалы</a>
<a href="<?php echo url_for("MaterialMovement/newWriteoff?from=" . $warehouse->getId()); ?>" class="btn btn-small">Списать материалы</a>
<?php endif ?>
</h3>
<?php
Expand Down
4 changes: 4 additions & 0 deletions lib/form/doctrine/MaterialMovementTransferForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ public function configure()
, $this['created_by']
, $this['updated_by']
);

$this->getWidgetSchema()->setLabels([
"name" => " ",
]);
}
}
4 changes: 3 additions & 1 deletion lib/model/doctrine/Warehouse.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function getBalance()
}
}

return $balance;
return array_filter($balance, function($material) {
return $material["amount"] > 0;
});
}
}
19 changes: 19 additions & 0 deletions test/functional/frontend/MaterialMovementActionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

include(dirname(__FILE__).'/../../bootstrap/functional.php');

$browser = new sfTestFunctional(new sfBrowser());

$browser->
get('/MaterialMovement/index')->

with('request')->begin()->
isParameter('module', 'MaterialMovement')->
isParameter('action', 'index')->
end()->

with('response')->begin()->
isStatusCode(200)->
checkElement('body', '!/This is a temporary page/')->
end()
;

0 comments on commit db4d8cb

Please sign in to comment.