1<?php
2/* Copyright (C) 2018-2019  Thibault FOUCART        <support@ptibogxiv.net>
3 * Copyright (C) 2019       Frédéric France         <frederic.france@netlogic.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19// Put here all includes required by your class file
20
21require '../main.inc.php';
22require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
23require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
24require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
25//require_once DOL_DOCUMENT_ROOT.'/core/lib/stripe.lib.php';
26require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
27require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
28require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
29if (!empty($conf->accounting->enabled)) {
30	require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
31}
32
33// Load translation files required by the page
34$langs->loadLangs(array('compta', 'salaries', 'bills', 'hrm', 'stripe'));
35
36// Security check
37$socid = GETPOST("socid", "int");
38if ($user->socid) {
39	$socid = $user->socid;
40}
41//$result = restrictedArea($user, 'salaries', '', '', '');
42
43$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
44$rowid = GETPOST("rowid", 'alpha');
45$sortfield = GETPOST("sortfield", 'alpha');
46$sortorder = GETPOST("sortorder", 'alpha');
47$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
48if (empty($page) || $page == -1) {
49	$page = 0;
50}     // If $page is not defined, or '' or -1
51$offset = $limit * $page;
52$pageprev = $page - 1;
53$pagenext = $page + 1;
54
55$result = restrictedArea($user, 'banque');
56
57
58/*
59 * View
60 */
61
62$form = new Form($db);
63$societestatic = new Societe($db);
64$memberstatic = new Adherent($db);
65$acc = new Account($db);
66$stripe = new Stripe($db);
67
68llxHeader('', $langs->trans("StripePayoutList"));
69
70if (!empty($conf->stripe->enabled) && (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox', 'alpha'))) {
71	$service = 'StripeTest';
72	$servicestatus = '0';
73	dol_htmloutput_mesg($langs->trans('YouAreCurrentlyInSandboxMode', 'Stripe'), '', 'warning');
74} else {
75	$service = 'StripeLive';
76	$servicestatus = '1';
77}
78
79$stripeacc = $stripe->getStripeAccount($service);
80/*if (empty($stripeaccount))
81{
82	print $langs->trans('ErrorStripeAccountNotDefined');
83}*/
84
85if (!$rowid) {
86	print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
87	if ($optioncss != '') {
88		print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
89	}
90	print '<input type="hidden" name="token" value="'.newToken().'">';
91	print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
92	print '<input type="hidden" name="action" value="list">';
93	print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
94	print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
95	print '<input type="hidden" name="page" value="'.$page.'">';
96
97	$title = $langs->trans("StripePayoutList");
98	$title .= ($stripeaccount ? ' (Stripe connection with Stripe OAuth Connect account '.$stripeacc.')' : ' (Stripe connection with keys from Stripe module setup)');
99
100	print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $totalnboflines, 'title_accountancy.png', 0, '', '', $limit);
101
102	print '<div class="div-table-responsive">';
103	print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
104
105	print '<tr class="liste_titre">';
106	print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
107	//print_liste_field_titre("StripeCustomerId",$_SERVER["PHP_SELF"],"","","","",$sortfield,$sortorder);
108	//print_liste_field_titre("CustomerId", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
109	//print_liste_field_titre("Origin", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
110	print_liste_field_titre("DatePayment", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'center ');
111	print_liste_field_titre("DateOperation", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'center ');
112	print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'left ');
113	print_liste_field_titre("Paid", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'right ');
114	print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "", "", "", '', '', '', 'right ');
115	print "</tr>\n";
116
117	print "</tr>\n";
118
119	try {
120		if ($stripeacc) {
121			$payout = \Stripe\Payout::all(array("limit" => $limit), array("stripe_account" => $stripeacc));
122		} else {
123			$payout = \Stripe\Payout::all(array("limit" => $limit));
124		}
125
126		foreach ($payout->data as $payout) {
127			//$charge = $payout;
128			//var_dump($payout);
129
130			// The metadata FULLTAG is defined by the online payment page
131			/*$FULLTAG=$charge->metadata->FULLTAG;
132
133			// Save into $tmparray all metadata
134			$tmparray = dolExplodeIntoArray($FULLTAG,'.','=');
135			// Load origin object according to metadata
136			if (! empty($tmparray['CUS']))
137			{
138				$societestatic->fetch($tmparray['CUS']);
139			}
140			else
141			{
142				$societestatic->id = 0;
143			}
144			if (! empty($tmparray['MEM']))
145			{
146				$memberstatic->fetch($tmparray['MEM']);
147			}
148			else
149			{
150				$memberstatic->id = 0;
151			}*/
152
153			$societestatic->fetch($charge->metadata->idcustomer);
154			$societestatic->id = $charge->metadata->idcustomer;
155			$societestatic->lastname = $obj->lastname;
156			$societestatic->firstname = $obj->firstname;
157			$societestatic->admin = $obj->admin;
158			$societestatic->login = $obj->login;
159			$societestatic->email = $obj->email;
160			$societestatic->socid = $obj->fk_soc;
161
162			print '<tr class="oddeven">';
163
164			// Ref
165			if (!empty($stripeacc)) {
166				$connect = $stripeacc.'/';
167			}
168
169			$url = 'https://dashboard.stripe.com/'.$connect.'test/payouts/'.$payout->id;
170			if ($servicestatus) {
171				$url = 'https://dashboard.stripe.com/'.$connect.'payouts/'.$payout->id;
172			}
173
174			print "<td><a href='".$url."' target='_stripe'>".img_picto($langs->trans('ShowInStripe'), 'globe')." ".$payout->id."</a></td>\n";
175
176
177			// Stripe customer
178			//print "<td>".$charge->customer."</td>\n";
179			// Link
180			/*print "<td>";
181			if ($societestatic->id > 0)
182			{
183				print $societestatic->getNomUrl(1);
184			}
185			if ($memberstatic->id > 0)
186			{
187				print $memberstatic->getNomUrl(1);
188			}
189			print "</td>\n";*/
190			// Origine
191			//print "<td>";
192			////if ($charge->metadata->dol_type=="order"){
193			//	$object = new Commande($db);
194			//	$object->fetch($charge->metadata->dol_id);
195			//	print "<a href='".DOL_URL_ROOT."/commande/card.php?id=".$charge->metadata->dol_id."'>".img_picto('', 'object_order')." ".$object->ref."</a>";
196			//} elseif ($charge->metadata->dol_type=="invoice"){
197			//	$object = new Facture($db);
198			//	$object->fetch($charge->metadata->dol_id);
199			//	print "<a href='".DOL_URL_ROOT."/compta/facture/card.php?facid=".$charge->metadata->dol_id."'>".img_picto('', 'object_invoice')." ".$object->ref."</a>";
200			//}
201			//print "</td>\n";
202			// Date payment
203			print '<td class="center">'.dol_print_date($payout->created, '%d/%m/%Y %H:%M')."</td>\n";
204			// Date payment
205			print '<td class="center">'.dol_print_date($payout->arrival_date, '%d/%m/%Y %H:%M')."</td>\n";
206			// Type
207			print '<td>'.$payout->description.'</td>';
208			// Amount
209			print '<td class="right">'.price(($payout->amount) / 100, 0, '', 1, -1, -1, strtoupper($payout->currency))."</td>";
210			// Status
211			print "<td class='right'>";
212			if ($payout->status == 'paid') {
213				print img_picto($langs->trans("".$payout->status.""), 'statut4');
214			} elseif ($payout->status == 'pending') {
215				print img_picto($langs->trans("".$payout->status.""), 'statut7');
216			} elseif ($payout->status == 'in_transit') {
217				print img_picto($langs->trans("".$payout->status.""), 'statut7');
218			} elseif ($payout->status == 'failed') {
219				print img_picto($langs->trans("".$payout->status.""), 'statut7');
220			} elseif ($payout->status == 'canceled') {
221				print img_picto($langs->trans("".$payout->status.""), 'statut8');
222			}
223			print '</td>';
224			print "</tr>\n";
225		}
226	} catch (Exception $e) {
227		print '<tr><td colspan="6">'.$e->getMessage().'</td></td>';
228	}
229	print "</table>";
230	print '</div>';
231	print '</form>';
232}
233
234// End of page
235llxFooter();
236$db->close();
237