Revision 9624f512
Von Tamino Steinert vor 10 Monaten hinzugefügt
SL/Controller/Order.pm | ||
---|---|---|
1 | 1 |
package SL::Controller::Order; |
2 | 2 |
|
3 | 3 |
use strict; |
4 |
use parent qw(SL::Controller::Base); |
|
4 |
use parent qw(SL::Controller::RecordBase);
|
|
5 | 5 |
|
6 | 6 |
use SL::Helper::Flash qw(flash flash_later); |
7 | 7 |
use SL::HTML::Util; |
... | ... | |
62 | 62 |
use Rose::Object::MakeMethods::Generic |
63 | 63 |
( |
64 | 64 |
scalar => [ qw(item_ids_to_delete is_custom_shipto_to_delete) ], |
65 |
'scalar --get_set_init' => [ qw(order valid_types type cv p all_price_factors
|
|
65 |
'scalar --get_set_init' => [ qw(valid_types type cv p all_price_factors |
|
66 | 66 |
search_cvpartnumber show_update_button |
67 | 67 |
part_picker_classification_ids |
68 | 68 |
is_final_version type_data) ], |
... | ... | |
81 | 81 |
# actions |
82 | 82 |
# |
83 | 83 |
|
84 |
# add a new order |
|
85 |
sub action_add { |
|
86 |
my ($self) = @_; |
|
87 |
|
|
88 |
$self->order(SL::Model::Record->update_after_new($self->order)); |
|
89 |
|
|
90 |
$self->pre_render(); |
|
91 |
|
|
92 |
if (!$::form->{form_validity_token}) { |
|
93 |
$::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_ORDER_SAVE())->token; |
|
94 |
} |
|
95 |
|
|
96 |
$self->render( |
|
97 |
'order/form', |
|
98 |
title => $self->type_data->text('add'), |
|
99 |
%{$self->{template_args}} |
|
100 |
); |
|
101 |
} |
|
102 |
|
|
103 | 84 |
sub action_add_from_record { |
104 | 85 |
my ($self) = @_; |
105 | 86 |
my $from_type = $::form->{from_type}; |
... | ... | |
1657 | 1638 |
# helpers |
1658 | 1639 |
# |
1659 | 1640 |
|
1641 |
sub base_template_folder {'order'} |
|
1642 |
|
|
1643 |
sub order { # instead of init_order -> init_record |
|
1644 |
my $self = shift; |
|
1645 |
$self->record(@_); |
|
1646 |
} |
|
1647 |
|
|
1648 |
sub init_record { |
|
1649 |
my ($self) = @_; |
|
1650 |
|
|
1651 |
my $form_periodic_invoices_config = delete $::form->{order}->{periodic_invoices_config}; |
|
1652 |
|
|
1653 |
my $record = $self->SUPER::init_record(); |
|
1654 |
|
|
1655 |
if ($record->is_type(SALES_ORDER_TYPE())) { |
|
1656 |
if (my $periodic_invoices_config_attrs = $form_periodic_invoices_config ? SL::YAML::Load($form_periodic_invoices_config) : undef) { |
|
1657 |
my $periodic_invoices_config = $record->periodic_invoices_config || $record->periodic_invoices_config(SL::DB::PeriodicInvoicesConfig->new); |
|
1658 |
$periodic_invoices_config->assign_attributes(%$periodic_invoices_config_attrs); |
|
1659 |
} |
|
1660 |
} |
|
1661 |
|
|
1662 |
return $record; |
|
1663 |
} |
|
1664 |
|
|
1660 | 1665 |
sub init_valid_types { |
1661 | 1666 |
$_[0]->type_data->valid_types; |
1662 | 1667 |
} |
... | ... | |
1699 | 1704 |
SL::Presenter->get; |
1700 | 1705 |
} |
1701 | 1706 |
|
1702 |
sub init_order { |
|
1703 |
$_[0]->make_order; |
|
1704 |
} |
|
1705 |
|
|
1706 | 1707 |
sub init_all_price_factors { |
1707 | 1708 |
SL::DB::Manager::PriceFactor->get_all; |
1708 | 1709 |
} |
... | ... | |
1865 | 1866 |
return $self->order; |
1866 | 1867 |
} |
1867 | 1868 |
|
1868 |
# load or create a new order object |
|
1869 |
# |
|
1870 |
# And assign changes from the form to this object. |
|
1871 |
# If the order is loaded from db, check if items are deleted in the form, |
|
1872 |
# remove them form the object and collect them for removing from db on saving. |
|
1873 |
# Then create/update items from form (via make_item) and add them. |
|
1874 |
sub make_order { |
|
1875 |
my ($self) = @_; |
|
1876 |
|
|
1877 |
# add_items adds items to an order with no items for saving, but they cannot |
|
1878 |
# be retrieved via items until the order is saved. Adding empty items to new |
|
1879 |
# order here solves this problem. |
|
1880 |
my $order; |
|
1881 |
$order = SL::DB::Order->new(id => $::form->{id})->load(with => [ 'orderitems', 'orderitems.part' ]) if $::form->{id}; |
|
1882 |
$order ||= SL::DB::Order->new(orderitems => [], |
|
1883 |
record_type => $::form->{type}, |
|
1884 |
currency_id => $::instance_conf->get_currency_id(),); |
|
1885 |
|
|
1886 |
my $cv_id_method = $order->type_data->properties('customervendor'). '_id'; |
|
1887 |
if (!$::form->{id} && $::form->{$cv_id_method}) { |
|
1888 |
$order->$cv_id_method($::form->{$cv_id_method}); |
|
1889 |
$order = SL::Model::Record->update_after_customer_vendor_change($order); |
|
1890 |
} |
|
1891 |
|
|
1892 |
my $form_orderitems = delete $::form->{order}->{orderitems}; |
|
1893 |
my $form_periodic_invoices_config = delete $::form->{order}->{periodic_invoices_config}; |
|
1894 |
|
|
1895 |
$order->assign_attributes(%{$::form->{order}}); |
|
1896 |
|
|
1897 |
$self->setup_custom_shipto_from_form($order, $::form); |
|
1898 |
|
|
1899 |
if (my $periodic_invoices_config_attrs = $form_periodic_invoices_config ? SL::YAML::Load($form_periodic_invoices_config) : undef) { |
|
1900 |
my $periodic_invoices_config = $order->periodic_invoices_config || $order->periodic_invoices_config(SL::DB::PeriodicInvoicesConfig->new); |
|
1901 |
$periodic_invoices_config->assign_attributes(%$periodic_invoices_config_attrs); |
|
1902 |
} |
|
1903 |
|
|
1904 |
# remove deleted items |
|
1905 |
$self->item_ids_to_delete([]); |
|
1906 |
foreach my $idx (reverse 0..$#{$order->orderitems}) { |
|
1907 |
my $item = $order->orderitems->[$idx]; |
|
1908 |
if (none { $item->id == $_->{id} } @{$form_orderitems}) { |
|
1909 |
splice @{$order->orderitems}, $idx, 1; |
|
1910 |
push @{$self->item_ids_to_delete}, $item->id; |
|
1911 |
} |
|
1912 |
} |
|
1913 |
|
|
1914 |
my @items; |
|
1915 |
my $pos = 1; |
|
1916 |
foreach my $form_attr (@{$form_orderitems}) { |
|
1917 |
my $item = make_item($order, $form_attr); |
|
1918 |
$item->position($pos); |
|
1919 |
push @items, $item; |
|
1920 |
$pos++; |
|
1921 |
} |
|
1922 |
$order->add_items(grep {!$_->id} @items); |
|
1923 |
|
|
1924 |
return $order; |
|
1925 |
} |
|
1926 |
|
|
1927 |
# create or update items from form |
|
1928 |
# |
|
1929 |
# Make item objects from form values. For items already existing read from db. |
|
1930 |
# Create a new item else. And assign attributes. |
|
1931 |
sub make_item { |
|
1932 |
my ($record, $attr) = @_; |
|
1933 |
|
|
1934 |
my $item; |
|
1935 |
$item = first { $_->id == $attr->{id} } @{$record->items} if $attr->{id}; |
|
1936 |
|
|
1937 |
my $is_new = !$item; |
|
1938 |
|
|
1939 |
# add_custom_variables adds cvars to an orderitem with no cvars for saving, but |
|
1940 |
# they cannot be retrieved via custom_variables until the order/orderitem is |
|
1941 |
# saved. Adding empty custom_variables to new orderitem here solves this problem. |
|
1942 |
$item ||= SL::DB::OrderItem->new(custom_variables => []); |
|
1943 |
|
|
1944 |
$item->assign_attributes(%$attr); |
|
1945 |
|
|
1946 |
if ($is_new) { |
|
1947 |
my $texts = get_part_texts($item->part, $record->language_id); |
|
1948 |
$item->longdescription($texts->{longdescription}) if !defined $attr->{longdescription}; |
|
1949 |
$item->project_id($record->globalproject_id) if !defined $attr->{project_id}; |
|
1950 |
$item->lastcost($record->is_sales ? $item->part->lastcost : 0) if !defined $attr->{lastcost_as_number}; |
|
1951 |
} |
|
1952 |
|
|
1953 |
return $item; |
|
1954 |
} |
|
1955 |
|
|
1956 | 1869 |
# create a new item |
1957 | 1870 |
# |
1958 | 1871 |
# This is used to add one item |
Auch abrufbar als: Unified diff
S:C:Order: verwende Action 'add' aus RecordBase