first commit
This commit is contained in:
116
admin/SplitOrder.php
Normal file
116
admin/SplitOrder.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
||||
use KupShop\OrderingBundle\Util\Order\OrderItemEditTrait;
|
||||
use KupShop\OrderingBundle\Util\Order\OrderUtil;
|
||||
use Query\Operator;
|
||||
|
||||
class SplitOrder extends Window
|
||||
{
|
||||
use DatabaseCommunication;
|
||||
use OrderItemEditTrait;
|
||||
|
||||
public function get_vars()
|
||||
{
|
||||
$vars = parent::get_vars();
|
||||
$ID = $this->getID();
|
||||
|
||||
$order = $this->prepareOrder($ID);
|
||||
$vars['products'] = $order->fetchItems();
|
||||
$vars['order'] = $order;
|
||||
$vars['body']['data']['id'] = $ID;
|
||||
|
||||
return $vars;
|
||||
}
|
||||
|
||||
public function handleUpdate()
|
||||
{
|
||||
$ID_OLD = $this->getID();
|
||||
$ID_NEW = null;
|
||||
$data = $this->getData();
|
||||
|
||||
$order_old = $this->prepareOrder($ID_OLD);
|
||||
$order_new = null;
|
||||
|
||||
if (findModule(\Modules::ORDER_PAYMENT) && abs($order_old->getPayments()) > 1) {
|
||||
// Uhrazenou objednávku již nelze rozdělit
|
||||
$this->returnError('Objednávku nelze rozdělit, protože k ní existuje platba, která doposud nebyla vrácena! Nejprve vraťte platby.');
|
||||
}
|
||||
if (intval($order_old['storno']) == 1) {
|
||||
// Stornovanou objednávku již nelze rozdělit
|
||||
$this->returnError(getTextString('SplitOrder', 'errorCanceledCantEdit'));
|
||||
}
|
||||
$order_new = sqlGetConnection()->transactional(function () use ($data, $ID_OLD, $order_old, $order_new, $ID_NEW) {
|
||||
$orderUtil = ServiceContainer::getService(OrderUtil::class);
|
||||
|
||||
$order_new = $orderUtil->copyOrder($ID_OLD, ['status_payed', 'status_dispatch', 'status_storno']);
|
||||
sqlQueryBuilder()->update('orders')->directValues(['date_created' => $order_old['date_created']->format('Y-m-d H:i:s')])
|
||||
->where(Operator::equals(['id' => $order_new['id']]))->execute();
|
||||
$ID_NEW = $order_new['id'];
|
||||
|
||||
$order_items = sqlQueryBuilder()->select('*')
|
||||
->from('order_items')
|
||||
->where(Operator::equals(['id_order' => $ID_OLD]))
|
||||
->execute()
|
||||
->fetchAll();
|
||||
|
||||
foreach ($order_items as $item_old) {
|
||||
$get_item = $data['items'][$item_old['id']];
|
||||
|
||||
if (floatval($get_item['pieces_new']) > 0) {
|
||||
$old_pcs = $item_old['pieces'] - $get_item['pieces_new'];
|
||||
if ($old_pcs == 0) {
|
||||
// na novou objednávku přesunout celou položku
|
||||
sqlQueryBuilder()->update('order_items')
|
||||
->directValues(['id_order' => $ID_NEW])
|
||||
->where(Operator::equals(['id' => $item_old['id']]))
|
||||
->execute();
|
||||
} else {
|
||||
// na novou objednávku přesunout část položek a přepočítat
|
||||
$item_new_data = $item_old;
|
||||
$item_new_data['id_order'] = $ID_NEW;
|
||||
$item_new_data['pieces'] = $get_item['pieces_new'];
|
||||
$item_new_data['pieces_reserved'] = $get_item['pieces_new'];
|
||||
$item_new_data['total_price'] = $get_item['pieces_new'] * $item_old['piece_price'];
|
||||
|
||||
unset($item_new_data['id'], $item_new_data['pohoda_id']);
|
||||
sqlQueryBuilder()->insert('order_items')
|
||||
->directValues($item_new_data)
|
||||
->execute();
|
||||
|
||||
// z původní objednávky odebrat část položek a přepočítat
|
||||
sqlQueryBuilder()->update('order_items')
|
||||
->directValues([
|
||||
'pieces' => $item_old['pieces'] - $get_item['pieces_new'],
|
||||
'pieces_reserved' => $item_old['pieces_reserved'] - $get_item['pieces_new'],
|
||||
'total_price' => $item_old['piece_price'] * ($item_old['pieces'] - $get_item['pieces_new']),
|
||||
])
|
||||
->where(Operator::equals(['id' => $item_old['id']]))
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$order_old->recalculate();
|
||||
$order_new->recalculate();
|
||||
|
||||
return $order_new;
|
||||
});
|
||||
writeDownActivity(sprintf(getTextString('SplitOrder', 'activitySplit'), $order_old['order_no'], $order_new['order_no']));
|
||||
|
||||
$order_old->logHistory(sprintf(getTextString('SplitOrder', 'splitOldNote'), $order_new['order_no']));
|
||||
$order_new->logHistory(sprintf(getTextString('SplitOrder', 'splitNewNote'), $order_old['order_no']));
|
||||
|
||||
$this->redirect(['s' => 'orders.php', 'ID' => $order_new['id'], 'acn' => 'edit', 'force_resize' => 1, 'reload_parent' => 1]);
|
||||
}
|
||||
|
||||
public function prepareOrder($orderId)
|
||||
{
|
||||
$order = new Order();
|
||||
$order->createFromDB($orderId);
|
||||
|
||||
return $order;
|
||||
}
|
||||
}
|
||||
|
||||
return SplitOrder::class;
|
||||
Reference in New Issue
Block a user