+=head3 GetBasketAsCSV
+
+ &GetBasketAsCSV($basketno);
+
+Export a basket as CSV
+
+=cut
+
+sub GetBasketAsCSV {
+ my ($basketno) = @_;
+ my $basket = GetBasket($basketno);
+ my @orders = GetOrders($basketno);
+ my $contract = GetContract($basket->{'contractnumber'});
+ my $csv = Text::CSV->new();
+ my $output;
+
+ # TODO: Translate headers
+ my @headers = qw(contractname ordernumber line entrydate isbn author title publishercode collectiontitle notes quantity rrp);
+
+ $csv->combine(@headers);
+ $output = $csv->string() . "\n";
+
+ my @rows;
+ foreach my $order (@orders) {
+ my @cols;
+ my $bd = GetBiblioData($order->{'biblionumber'});
+ push(@cols,
+ $contract->{'contractname'},
+ $order->{'ordernumber'},
+ $order->{'entrydate'},
+ $order->{'isbn'},
+ $bd->{'author'},
+ $bd->{'title'},
+ $bd->{'publishercode'},
+ $bd->{'collectiontitle'},
+ $order->{'notes'},
+ $order->{'quantity'},
+ $order->{'rrp'},
+ );
+ push (@rows, \@cols);
+ }
+
+ # Sort by publishercode
+ # TODO: Sort by publishercode then by title
+ @rows = sort { @$a[7] cmp @$b[7] } @rows;
+
+ foreach my $row (@rows) {
+ $csv->combine(@$row);
+ $output .= $csv->string() . "\n";
+
+ }
+
+ return $output;
+
+}
+
+
+=head3 CloseBasketgroup
+
+ &CloseBasketgroup($basketgroupno);
+
+close a basketgroup
+
+=cut
+
+sub CloseBasketgroup {
+ my ($basketgroupno) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare("
+ UPDATE aqbasketgroups
+ SET closed=1
+ WHERE id=?
+ ");
+ $sth->execute($basketgroupno);
+}
+
+#------------------------------------------------------------#
+
+=head3 ReOpenBaskergroup($basketgroupno)
+
+ &ReOpenBaskergroup($basketgroupno);
+
+reopen a basketgroup
+
+=cut
+
+sub ReOpenBasketgroup {
+ my ($basketgroupno) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare("
+ UPDATE aqbasketgroups
+ SET closed=0
+ WHERE id=?
+ ");
+ $sth->execute($basketgroupno);
+}
+
+#------------------------------------------------------------#
+
+
+=head3 DelBasket
+
+ &DelBasket($basketno);
+
+Deletes the basket that has basketno field $basketno in the aqbasket table.
+
+=over
+
+=item C<$basketno> is the primary key of the basket in the aqbasket table.
+
+=back
+
+=cut
+
+sub DelBasket {
+ my ( $basketno ) = @_;
+ my $query = "DELETE FROM aqbasket WHERE basketno=?";
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($basketno);
+ $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+=head3 ModBasket
+
+ &ModBasket($basketinfo);
+
+Modifies a basket, using a hashref $basketinfo for the relevant information, only $basketinfo->{'basketno'} is required.
+
+=over
+
+=item C<$basketno> is the primary key of the basket in the aqbasket table.
+
+=back
+
+=cut
+
+sub ModBasket {
+ my $basketinfo = shift;
+ my $query = "UPDATE aqbasket SET ";
+ my @params;
+ foreach my $key (keys %$basketinfo){
+ if ($key ne 'basketno'){
+ $query .= "$key=?, ";
+ push(@params, $basketinfo->{$key} || undef );
+ }
+ }
+# get rid of the "," at the end of $query
+ if (substr($query, length($query)-2) eq ', '){
+ chop($query);
+ chop($query);
+ $query .= ' ';
+ }
+ $query .= "WHERE basketno=?";
+ push(@params, $basketinfo->{'basketno'});
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare($query);
+ $sth->execute(@params);
+ $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+=head3 ModBasketHeader
+
+ &ModBasketHeader($basketno, $basketname, $note, $booksellernote, $contractnumber);
+
+Modifies a basket's header.
+
+=over
+
+=item C<$basketno> is the "basketno" field in the "aqbasket" table;
+
+=item C<$basketname> is the "basketname" field in the "aqbasket" table;
+
+=item C<$note> is the "note" field in the "aqbasket" table;
+
+=item C<$booksellernote> is the "booksellernote" field in the "aqbasket" table;
+
+=item C<$contractnumber> is the "contractnumber" (foreign) key in the "aqbasket" table.
+
+=back
+
+=cut
+
+sub ModBasketHeader {
+ my ($basketno, $basketname, $note, $booksellernote, $contractnumber) = @_;
+ my $query = "UPDATE aqbasket SET basketname=?, note=?, booksellernote=? WHERE basketno=?";
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($basketname,$note,$booksellernote,$basketno);
+ if ( $contractnumber ) {
+ my $query2 ="UPDATE aqbasket SET contractnumber=? WHERE basketno=?";
+ my $sth2 = $dbh->prepare($query2);
+ $sth2->execute($contractnumber,$basketno);
+ $sth2->finish;
+ }
+ $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+=head3 GetBasketsByBookseller
+
+ @results = &GetBasketsByBookseller($booksellerid, $extra);
+
+Returns a list of hashes of all the baskets that belong to bookseller 'booksellerid'.
+
+=over
+
+=item C<$booksellerid> is the 'id' field of the bookseller in the aqbooksellers table
+
+=item C<$extra> is the extra sql parameters, can be
+
+ $extra->{groupby}: group baskets by column
+ ex. $extra->{groupby} = aqbasket.basketgroupid
+ $extra->{orderby}: order baskets by column
+ $extra->{limit}: limit number of results (can be helpful for pagination)
+
+=back
+
+=cut
+
+sub GetBasketsByBookseller {
+ my ($booksellerid, $extra) = @_;
+ my $query = "SELECT * FROM aqbasket WHERE booksellerid=?";
+ if ($extra){
+ if ($extra->{groupby}) {
+ $query .= " GROUP by $extra->{groupby}";
+ }
+ if ($extra->{orderby}){
+ $query .= " ORDER by $extra->{orderby}";
+ }
+ if ($extra->{limit}){
+ $query .= " LIMIT $extra->{limit}";
+ }
+ }
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($booksellerid);
+ my $results = $sth->fetchall_arrayref({});
+ $sth->finish;
+ return $results
+}
+
+#------------------------------------------------------------#
+
+=head3 GetBasketsByBasketgroup
+
+ $baskets = &GetBasketsByBasketgroup($basketgroupid);
+
+Returns a reference to all baskets that belong to basketgroup $basketgroupid.
+
+=cut
+
+sub GetBasketsByBasketgroup {
+ my $basketgroupid = shift;
+ my $query = "SELECT * FROM aqbasket
+ LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?";
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($basketgroupid);
+ my $results = $sth->fetchall_arrayref({});
+ $sth->finish;
+ return $results
+}
+
+#------------------------------------------------------------#
+
+=head3 NewBasketgroup
+
+ $basketgroupid = NewBasketgroup(\%hashref);
+
+Adds a basketgroup to the aqbasketgroups table, and add the initial baskets to it.
+
+$hashref->{'booksellerid'} is the 'id' field of the bookseller in the aqbooksellers table,
+
+$hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
+
+$hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
+
+=cut
+
+sub NewBasketgroup {
+ my $basketgroupinfo = shift;
+ die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
+ my $query = "INSERT INTO aqbasketgroups (";
+ my @params;
+ foreach my $field ('name', 'deliveryplace', 'deliverycomment', 'closed') {
+ if ( $basketgroupinfo->{$field} ) {
+ $query .= "$field, ";
+ push(@params, $basketgroupinfo->{$field});
+ }
+ }
+ $query .= "booksellerid) VALUES (";
+ foreach (@params) {
+ $query .= "?, ";
+ }
+ $query .= "?)";
+ push(@params, $basketgroupinfo->{'booksellerid'});
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare($query);
+ $sth->execute(@params);
+ my $basketgroupid = $dbh->{'mysql_insertid'};
+ if( $basketgroupinfo->{'basketlist'} ) {
+ foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
+ my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
+ my $sth2 = $dbh->prepare($query2);
+ $sth2->execute($basketgroupid, $basketno);
+ }
+ }
+ return $basketgroupid;
+}
+
+#------------------------------------------------------------#
+
+=head3 ModBasketgroup
+
+ ModBasketgroup(\%hashref);
+
+Modifies a basketgroup in the aqbasketgroups table, and add the baskets to it.
+
+$hashref->{'id'} is the 'id' field of the basketgroup in the aqbasketgroup table, this parameter is mandatory,
+
+$hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
+
+$hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
+
+=cut
+
+sub ModBasketgroup {
+ my $basketgroupinfo = shift;
+ die "basketgroup id is required to edit a basketgroup" unless $basketgroupinfo->{'id'};
+ my $dbh = C4::Context->dbh;
+ my $query = "UPDATE aqbasketgroups SET ";
+ my @params;
+ foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
+ if ( defined $basketgroupinfo->{$field} ) {
+ $query .= "$field=?, ";
+ push(@params, $basketgroupinfo->{$field});
+ }
+ }
+ chop($query);
+ chop($query);
+ $query .= " WHERE id=?";
+ push(@params, $basketgroupinfo->{'id'});
+ my $sth = $dbh->prepare($query);
+ $sth->execute(@params);
+
+ $sth = $dbh->prepare('UPDATE aqbasket SET basketgroupid = NULL WHERE basketgroupid = ?');
+ $sth->execute($basketgroupinfo->{'id'});
+
+ if($basketgroupinfo->{'basketlist'} && @{$basketgroupinfo->{'basketlist'}}){
+ $sth = $dbh->prepare("UPDATE aqbasket SET basketgroupid=? WHERE basketno=?");
+ foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
+ $sth->execute($basketgroupinfo->{'id'}, $basketno);
+ $sth->finish;
+ }
+ }
+ $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+=head3 DelBasketgroup
+
+ DelBasketgroup($basketgroupid);
+
+Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
+
+=over
+
+=item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
+
+=back
+
+=cut
+
+sub DelBasketgroup {
+ my $basketgroupid = shift;
+ die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
+ my $query = "DELETE FROM aqbasketgroups WHERE id=?";
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($basketgroupid);
+ $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+