1 package SL::Controller::Order;
4 use parent qw(SL::Controller::Base);
19 use SL::Helper::DateTime;
21 use List::Util qw(max first);
22 use List::MoreUtils qw(none pairwise);
24 use Rose::Object::MakeMethods::Generic
26 'scalar --get_set_init' => [ qw(order valid_types type cv js p) ],
31 __PACKAGE__->run_before('_check_auth');
33 __PACKAGE__->run_before('_setup',
34 only => [ qw(edit update save) ]);
44 $self->order->transdate(DateTime->now_local());
49 title => $self->type eq _sales_order_type() ? $::locale->text('Add Sales Order')
50 : $self->type eq _purchase_order_type() ? $::locale->text('Add Purchase Order')
52 %{$self->{template_args}}
62 title => $self->type eq _sales_order_type() ? $::locale->text('Edit Sales Order')
63 : $self->type eq _purchase_order_type() ? $::locale->text('Edit Purchase Order')
65 %{$self->{template_args}}
75 title => $self->type eq _sales_order_type() ? $::locale->text('Edit Sales Order')
76 : $self->type eq _purchase_order_type() ? $::locale->text('Edit Purchase Order')
78 %{$self->{template_args}}
87 my @redirect_params = (
90 id => $self->order->id,
93 $self->redirect_to(@redirect_params);
96 sub action_customer_vendor_changed {
99 if ($self->cv eq 'customer') {
100 $self->order->customer(SL::DB::Manager::Customer->find_by_or_create(id => $::form->{cv_id}));
102 } elsif ($self->cv eq 'vendor') {
103 $self->order->vendor(SL::DB::Manager::Vendor->find_by_or_create(id => $::form->{cv_id}));
106 if ($self->order->{$self->cv}->contacts && scalar @{ $self->order->{$self->cv}->contacts } > 0) {
107 $self->js->show('#cp_row');
109 $self->js->hide('#cp_row');
112 if ($self->order->{$self->cv}->shipto && scalar @{ $self->order->{$self->cv}->shipto } > 0) {
113 $self->js->show('#shipto_row');
115 $self->js->hide('#shipto_row');
119 ->replaceWith('#order_cp_id', $self->build_contact_select)
120 ->replaceWith('#order_shipto_id', $self->build_shipto_select)
121 ->val('#order_taxzone_id', $self->order->{$self->cv}->taxzone_id)
122 ->focus('#order_' . $self->cv . ' _id')
126 sub action_add_item {
129 my $form_attr = $::form->{add_item};
130 my $item = SL::DB::OrderItem->new;
131 $item->assign_attributes(%$form_attr);
133 my $part = SL::DB::Part->new(id => $form_attr->{parts_id})->load;
134 my $cv_class = "SL::DB::" . ucfirst($self->cv);
135 my $cv_discount = $::form->{$self->cv . '_id'}? $cv_class->new(id => $::form->{$self->cv . '_id'})->load->discount :0.0;
138 $new_attr{id} = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
139 $new_attr{part} = $part;
140 $new_attr{qty} = 1.0 if ! $item->{qty};
141 $new_attr{unit} = $part->unit;
142 $new_attr{sellprice} = $part->sellprice if ! $item->{sellprice};
143 $new_attr{discount} = $cv_discount if ! $item->{discount};
144 $item->assign_attributes(%new_attr);
146 # add_items seems to fail if no items had been added before
147 if ($self->order->items) {
148 $self->order->add_items($item);
150 $self->order->items([$item]);
155 my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item);
158 ->append('#row_table_id tbody', $row_as_html)
159 ->focus('#row_table_id tr:last [id$="parts_id_name"]')
160 ->off('[id^="order_orderitems"][id$="parts_id"]', 'change', 'set_item_values')
161 ->on('[id^="order_orderitems"][id$="parts_id"]', 'change', 'set_item_values')
162 ->html('#netamount_id', $::form->format_amount(\%::myconfig, $self->order->netamount, -2))
163 ->html('#amount_id', $::form->format_amount(\%::myconfig, $self->order->amount, -2))
165 ->insertBefore($self->build_tax_rows, '#amount_row_id')
169 sub action_set_item_values {
172 my $is_new = $::form->{item_id} =~ m{^new_};
173 my $item_id = $::form->{item_id};
175 my $item = first {$_->id eq $::form->{item_id}} @{$self->order->items};
176 my $form_attr = first {$_->{id} eq $::form->{item_id}} @{ $::form->{order}->{orderitems} };
178 delete $form_attr->{id};
180 my $part = SL::DB::Part->new(id => $form_attr->{parts_id})->load;
182 my $cv_class = "SL::DB::" . ucfirst($self->cv);
183 my $cv_discount = $::form->{cv_id}? $cv_class->new(id => $::form->{$self->cv . '_id'})->load->discount :0.0;
187 $new_attr{sellprice} = $part->sellprice if ! $form_attr->{sellprice_as_number};
188 $new_attr{discount} = $cv_discount if ! $form_attr->{discount_as_percent};
189 $new_attr{unit} = $part->unit if ! $form_attr->{unit};
190 $new_attr{qty} = 1.0 if ! $form_attr->{qty_as_number};
192 $item->assign_attributes(%new_attr);
198 ->val( '#' . $::form->{qty_dom_id}, $item->qty_as_number)
199 ->val( '#' . $::form->{unit_dom_id}, $item->unit)
200 ->val( '#' . $::form->{sellprice_dom_id}, $item->sellprice_as_number)
201 ->val( '#' . $::form->{discount_dom_id}, $item->discount_as_percent)
202 ->run('display_linetotal', $::form->{item_id}, $::form->format_amount(\%::myconfig, $item->{linetotal}, -2))
203 ->html('#netamount_id', $::form->format_amount(\%::myconfig, $self->order->netamount, -2))
204 ->html('#amount_id', $::form->format_amount(\%::myconfig, $self->order->amount, -2))
206 ->insertBefore($self->build_tax_rows, '#amount_row_id')
215 sub init_valid_types {
216 [ _sales_order_type(), _purchase_order_type() ];
222 if (none { $::form->{type} eq $_ } @{$self->valid_types}) {
223 die "Not a valid type for order";
226 $self->type($::form->{type});
232 my $cv = $self->type eq _sales_order_type() ? 'customer'
233 : $self->type eq _purchase_order_type() ? 'vendor'
234 : die "Not a valid type for order";
254 my $right_for = { map { $_ => $_.'_edit' } @{$self->valid_types} };
256 my $right = $right_for->{ $self->type };
257 $right ||= 'DOES_NOT_EXIST';
259 $::auth->assert($right);
262 sub build_contact_select {
265 $self->p->select_tag('order.cp_id', [ $self->order->{$self->cv}->contacts ],
266 value_key => 'cp_id',
267 title_key => 'full_name_dep',
268 default => $self->order->cp_id,
270 style => 'width: 300px',
274 sub build_shipto_select {
277 $self->p->select_tag('order.shipto_id', [ $self->order->{$self->cv}->shipto ],
278 value_key => 'shipto_id',
279 title_key => 'displayable_id',
280 default => $self->order->shipto_id,
282 style => 'width: 300px',
290 foreach my $tax (@{ $self->{taxes} }) {
291 $rows_as_html .= $self->p->render('order/tabs/_tax_row', TAX => $tax);
293 return $rows_as_html;
300 my $order = SL::DB::Manager::Order->find_by_or_create(id => $::form->{id});
302 $order->assign_attributes(%{$::form->{order}});
311 # bb: todo: currency later
312 $self->order->currency_id($::instance_conf->get_currency_id());
314 my %pat = $self->order->calculate_prices_and_taxes();
316 foreach my $tax_chart_id (keys %{ $pat{taxes} }) {
317 my $tax = SL::DB::Manager::Tax->find_by(chart_id => $tax_chart_id);
318 push(@{ $self->{taxes} }, { amount => $pat{taxes}->{$tax_chart_id},
322 pairwise { $a->{linetotal} = $b->{linetotal} } @{$self->order->items}, @{$pat{items}};
328 my $db = $self->order->db;
332 $self->order->save();
333 }) || die($db->error);
340 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
341 $self->{all_employees} = SL::DB::Manager::Employee->get_all(where => [ or => [ id => $self->order->employee_id,
344 $self->{all_projects} = SL::DB::Manager::Project->get_all(where => [ or => [ id => $self->order->globalproject_id,
346 sort_by => 'projectnumber');
348 $self->{current_employee_id} = SL::DB::Manager::Employee->current->id;
351 sub _sales_order_type {
355 sub _purchase_order_type {