Bladeren bron

daily income counter fixes, printed / unprinted factures fixes

szollosil 1 jaar geleden
bovenliggende
commit
6d24bc4a1c

+ 134 - 0
custom/bbus/class/api_bbus.class.php

@@ -2353,6 +2353,140 @@ class BBus extends DolibarrApi
 		return $array;
 	}
 
+	/**
+	 * Create SQL queries for products clone	 
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url POST /giveMeAllProducts
+	 *
+	 * @throws RestException 401
+	 * @throws RestException 403
+	 * @throws RestException 404
+	 */
+	public function giveMeAllProducts()
+	{
+		$productsArray = [];
+		$sql = "SELECT * FROM llx_product";
+		$result = $this->db->query($sql);
+		while ($row = $this->db->fetch_object($result)) {
+			$productsArray[$row->rowid]['product'] = $row;
+			$sqlef = "SELECT * FROM llx_product_extrafields WHERE fk_object = {$row->rowid}";
+			$resultef = $this->db->query($sqlef);
+			while ($rowef = $this->db->fetch_object($resultef)) {
+				$contentef = "-- new Extrafields row {$rowef->rowid} 
+				DELETE FROM llx_product_extrafields WHERE fk_object = {$rowef->rowid};
+				INSERT INTO llx_product_extrafields (";
+				$valueContentef = "";
+				$keyNumberef = count((array)$rowef);
+				$keyNumberef - 2;
+				$counteref = 1;
+				foreach ($rowef as $efkey => $efvalue) {
+					if ($efkey == 'oneoccasion' || $efkey == 'online') {
+						$counteref++;
+					} else {
+						$contentef .= "{$efkey}";
+						if ($efkey == 'fk_user_author') {
+							$valueContentef .= intval(1);
+						} elseif ($efkey == 'fk_user_modif') {
+							$valueContentef .= intval(1);
+						} else {
+							$valueContentef .= "{$this->getvvalue($efvalue)}";
+						}
+						if ($counteref <= $keyNumberef - 1) {
+							$contentef .= ", ";
+							$valueContentef .= ", ";
+						} else {
+							$contentef .= ") VALUES (";
+							$valueContentef .= ") ON CONFLICT (rowid) DO NOTHING;";
+						}
+						$counteref++;
+					}
+				}
+				$asdef = $contentef . $valueContentef;
+				$productsArray[$row->rowid]['extrafields'] = $asdef;
+			}
+			$sqlprice = "SELECT * FROM llx_product_price WHERE fk_product = {$row->rowid}";
+			$resultprice = $this->db->query($sqlprice);
+			while ($rowprice = $this->db->fetch_object($resultprice)) {
+
+				$contentprice = "-- product price rowid: {$rowprice->rowid}
+				DELETE FROM llx_product_price WHERE rowid = {$rowprice->rowid};
+				INSERT INTO llx_product_price (";
+				$valueContentprice = "";
+				$keyNumber = count((array)$rowprice);
+				$counter = 1;
+				foreach ($rowprice as $pricekey => $pricevalue) {
+					//print $pricekey . "\r\n";
+					$contentprice .= "{$pricekey}";
+					if ($pricekey == 'fk_user_author') {
+						$valueContentprice .= intval(1);
+					} elseif ($pricekey == 'fk_user_modif') {
+						$valueContentprice .= intval(1);
+					} else {
+						$valueContentprice .= "{$this->getvvalue($pricevalue)}";
+					}
+					if ($counter <= $keyNumber - 1) {
+						$contentprice .= ", ";
+						$valueContentprice .= ", ";
+					} else {
+						$contentprice .= ") VALUES (";
+						$valueContentprice .= ") ON CONFLICT (rowid) DO NOTHING;";
+					}
+					$counter++;
+				}
+				$asdprice = $contentprice . $valueContentprice;
+				$productsArray[$row->rowid]['prices'] = $asdprice;
+			}
+			//exit;
+		}
+		foreach ($productsArray as $key => $value) {
+			$content = "-- product rowid: {$key}
+				INSERT INTO llx_product (";
+			$valueContent = "";
+			$keyNumber = count((array)$value['product']);
+			$counter = 1;
+			foreach ($value['product'] as $vkey => $vvalue) {
+				$content .= "{$vkey}";
+				if ($vkey == 'fk_user_author') {
+					$valueContent .= intval(1);
+				} elseif ($vkey == 'fk_user_modif') {
+					$valueContent .= intval(1);
+				} else {
+					$valueContent .= "{$this->getvvalue($vvalue)}";
+				}
+				if ($counter <= $keyNumber - 1) {
+					$content .= ", ";
+					$valueContent .= ", ";
+				} else {
+					$content .= ") VALUES (";
+					$valueContent .= ") ON CONFLICT (rowid) DO NOTHING;";
+				}
+				$counter++;
+			}
+			$asd = $content . $valueContent;
+			print $asd . "\r\n";
+			print $value['extrafields'] . "\r\n";
+			print $value['prices'] . "\r\n";
+		}
+		exit;
+	}
+
+	function getvvalue($value)
+	{
+		//$value = str_replace("'", '"', $value);
+		if (is_numeric($value)) {
+			$value = intval($value);
+		} else {
+			if (is_null($value)) {
+				$value = 	'NULL';
+			} else {
+				$value = "'" . $value . "'";
+			}
+		}
+		return $value;
+	}
+
 	/**
 	 * Get events ids
 	 *  

+ 173 - 10
custom/bbus/class/api_bbus.class.php.bak

@@ -326,7 +326,7 @@ class BBus extends DolibarrApi
 		$selectedTicket = $ticketChecker->getDataOfTheSelectedTicket();
 
 		$ticketChecker->setBbTicketRowId($selectedTicket->id);
-//----
+
 		$ticketChecker->checkExceptionHandlers($selectedTicket, 'ticketvalidationByPhone', $logId);
 
 		$ticketChecker->setMergedTickets($selectedTicket, 'ticketvalidationByPhone', $logId);
@@ -520,8 +520,10 @@ class BBus extends DolibarrApi
 		if ($printedCopies == 0) {
 			ApiBbusLog::appLog("printedCopies: no copies");
 			if (empty($facture_id)) {
+				ApiBbusLog::appLog("No facture_id");
 				$bbTicketsByFacture = $this->getTicketsByInvoiceNumber($ref);
 			} else {
+				ApiBbusLog::appLog("Facture_id: " . $facture_id);
 				$bbTicketsByFacture = $this->getTicketsByFactureId($facture_id);
 			}
 			foreach ($bbTicketsByFacture as $ticket) {
@@ -1864,7 +1866,7 @@ class BBus extends DolibarrApi
 	 */
 	public function invoice(array $invoice, array $lines, array $payment, string $cardPaymentLog = '', string $sendId = '', $server_host_curl = false, $company_invoice = '')
 	{
-		global $user;
+		global $user, $conf;
 		$lockLabel = 'CREATE_INVOICE';
 		if (empty($sendId)) {
 			$sendId = substr(md5(rand()), 0, 8);
@@ -1895,6 +1897,7 @@ class BBus extends DolibarrApi
 
 
 		$apiInvoiceHelper = new ApiInvoiceHelper;
+		$BookingApi = new BookingApi;
 		/**
 		 * handle invoice basic data
 		 */
@@ -1953,14 +1956,30 @@ class BBus extends DolibarrApi
 		dol_syslog("{$sendId}####################################################################", LOG_INFO, 0);
 
 		#-------------- UPDATE BTICKET --------------
-		ApiBbusLog::appLog("{$sendId}: Update bbticket with curl");
-		$invoiceForCurl['id'] = $invoiceObj->id;
-		$invoiceForCurl['ref'] = $invoiceObj->ref;
-		//$array['fk_booking_history'] = null;
-		$array['invoice'] = $invoiceForCurl;
-		$updateBBticketPostFields = json_encode($array);
-		ApiBbusLog::appLog("{$updateBBticketPostFields}");
-		$this->curlRunner('bookingapi/curlUpdateBbticket', $updateBBticketPostFields, 'POST', true);
+		foreach ($products as $product) {
+			$server_host = '';
+			$proSQL = "SELECT bbs.server_host FROM llx_product as pr 
+			INNER JOIN llx_product_extrafields as pre ON pre.fk_object = pr.rowid
+			INNER JOIN llx_bbus_basicservices as bbs ON bbs.rowid = CAST(pre.basic_service AS integer)
+			WHERE pr.rowid = {$product['id']}";
+			$proResult = $this->db->query($proSQL);
+			if ($this->db->num_rows($proResult) > 0) {
+				while ($proRow = $this->db->fetch_object($proResult)) {
+					$server_host = $proRow->server_host;
+				}
+			}
+			if ($server_host == $conf->global->LOCAL_SERVER_HOST) {
+				$BookingApi->updateBbticket(get_object_vars($invoiceObj), null);
+			} else {
+				ApiBbusLog::appLog("{$sendId}: Update bbticket with curl");
+				$invoiceForCurl['id'] = $invoiceObj->id;
+				$invoiceForCurl['ref'] = $invoiceObj->ref;
+				$array['invoice'] = $invoiceForCurl;
+				$updateBBticketPostFields = json_encode($array);
+				ApiBbusLog::appLog("{$updateBBticketPostFields}");
+				$this->curlRunner('bookingapi/curlUpdateBbticket', $updateBBticketPostFields, 'POST', true);
+			}
+		}
 
 		return [
 			'sendId' => $sendId,
@@ -2333,4 +2352,148 @@ class BBus extends DolibarrApi
 		}
 		return $array;
 	}
+
+	public function giveMeAllProducts()
+	{
+		$productsArray = [];
+		$sql = "SELECT * FROM llx_product";
+		$result = $this->db->query($sql);
+		while ($row = $this->db->fetch_object($result)) {
+			$productsArray[$row->rowid]['product'] = $row;
+			$sqlef = "SELECT * FROM llx_product_extrafields WHERE fk_object = {$row->rowid}";
+			$resultef = $this->db->query($sqlef);
+			while ($rowef = $this->db->fetch_object($resultef)) {
+				$contentef = "-- new Extrafields row {$rowef->rowid} 
+				DELETE FROM llx_product_extrafields WHERE fk_object = {$rowef->rowid};
+				INSERT INTO llx_product_extrafields (";
+				$valueContentef = "";
+				$keyNumberef = count((array)$rowef);
+				$keyNumberef - 2;
+				$counteref = 1;
+				foreach ($rowef as $efkey => $efvalue) {
+					if ($efkey == 'oneoccasion' || $efkey == 'online') {
+						$counteref++;
+					} else {
+						$contentef .= "{$efkey}";
+						if ($efkey == 'fk_user_author') {
+							$valueContentef .= intval(1);
+						} elseif ($efkey == 'fk_user_modif') {
+							$valueContentef .= intval(1);
+						} else {
+							$valueContentef .= "{$this->getvvalue($efvalue)}";
+						}
+						if ($counteref <= $keyNumberef - 1) {
+							$contentef .= ", ";
+							$valueContentef .= ", ";
+						} else {
+							$contentef .= ") VALUES (";
+							$valueContentef .= ") ON CONFLICT (rowid) DO NOTHING;";
+						}
+						$counteref++;
+					}
+				}
+				$asdef = $contentef . $valueContentef;
+				$productsArray[$row->rowid]['extrafields'] = $asdef;
+			}
+			$sqlprice = "SELECT * FROM llx_product_price WHERE fk_product = {$row->rowid}";
+			$resultprice = $this->db->query($sqlprice);
+			while ($rowprice = $this->db->fetch_object($resultprice)) {
+
+				$contentprice = "-- product price rowid: {$rowprice->rowid}
+				DELETE FROM llx_product_price WHERE rowid = {$rowprice->rowid};
+				INSERT INTO llx_product_price (";
+				$valueContentprice = "";
+				$keyNumber = count((array)$rowprice);
+				$counter = 1;
+				foreach ($rowprice as $pricekey => $pricevalue) {
+					//print $pricekey . "\r\n";
+					$contentprice .= "{$pricekey}";
+					if ($pricekey == 'fk_user_author') {
+						$valueContentprice .= intval(1);
+					} elseif ($pricekey == 'fk_user_modif') {
+						$valueContentprice .= intval(1);
+					} else {
+						$valueContentprice .= "{$this->getvvalue($pricevalue)}";
+					}
+					if ($counter <= $keyNumber - 1) {
+						$contentprice .= ", ";
+						$valueContentprice .= ", ";
+					} else {
+						$contentprice .= ") VALUES (";
+						$valueContentprice .= ") ON CONFLICT (rowid) DO NOTHING;";
+					}
+					$counter++;
+				}
+				$asdprice = $contentprice . $valueContentprice;
+				$productsArray[$row->rowid]['prices'] = $asdprice;
+			}
+			//exit;
+		}
+		foreach ($productsArray as $key => $value) {
+			$content = "-- product rowid: {$key}
+				INSERT INTO llx_product (";
+			$valueContent = "";
+			$keyNumber = count((array)$value['product']);
+			$counter = 1;
+			foreach ($value['product'] as $vkey => $vvalue) {
+				$content .= "{$vkey}";
+				if ($vkey == 'fk_user_author') {
+					$valueContent .= intval(1);
+				} elseif ($vkey == 'fk_user_modif') {
+					$valueContent .= intval(1);
+				} else {
+					$valueContent .= "{$this->getvvalue($vvalue)}";
+				}
+				if ($counter <= $keyNumber - 1) {
+					$content .= ", ";
+					$valueContent .= ", ";
+				} else {
+					$content .= ") VALUES (";
+					$valueContent .= ") ON CONFLICT (rowid) DO NOTHING;";
+				}
+				$counter++;
+			}
+			$asd = $content . $valueContent;
+			print $asd . "\r\n";
+			print $value['extrafields'] . "\r\n";
+			print $value['prices'] . "\r\n";
+		}
+		exit;
+	}
+
+	function getvvalue($value)
+	{
+		//$value = str_replace("'", '"', $value);
+		if (is_numeric($value)) {
+			$value = intval($value);
+		} else {
+			if (is_null($value)) {
+				$value = 	'NULL';
+			} else {
+				$value = "'" . $value . "'";
+			}
+		}
+		return $value;
+	}
+
+	/**
+	 * Get events ids
+	 *  
+	 * @return array|mixed Data without useless information
+	 * 
+	 * @url POST /getAllEventids
+	 */
+	public function getAllEventids()
+	{
+		global $db;
+		$array = [];
+		$sql = "SELECT rowid FROM llx_bbus_basicservices WHERE is_event = '1'";
+		$result = $db->query($sql);
+		if($db->num_rows($result) > 0){
+			while($row = $db->fetch_object($result)){
+				$array[] = $row->rowid;
+			}
+		}
+		return $array;
+	}
 }

+ 4 - 0
custom/bbus/class/commissionhandler.class.php

@@ -197,9 +197,11 @@ class CommissionHandler
         }
         $sqlForCrossShoppingFactures .= " (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing = {$user_id}) Is NULL AND
         fk_user_closing = {$user_id} 
+        
         AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
         AND multicurrency_code = '{$currency}' 
         AND date_closing BETWEEN '{$from}' AND '{$to}'";
+        //print $sqlForCrossShoppingFactures . '<br>';
         $resultForCrossShoppingFactures = $this->db->query($sqlForCrossShoppingFactures);
         if ($this->db->num_rows($resultForCrossShoppingFactures) > 0) {
             while ($factureCSRow = pg_fetch_assoc($resultForCrossShoppingFactures)) {
@@ -214,6 +216,7 @@ class CommissionHandler
         $params = ["from" => $from, "to" => $to, "factures" => $crossShoppingFacturesString];
         $postFields = json_encode($params);
         $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefs', $postFields, 'POST', true);
+        //print $crossShoppingFactures;
         $sql = $this->createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures);
         $commissions = $this->getMulticurrencyTotalTTC($sql);
         $commissionsCroSho = 0;
@@ -246,6 +249,7 @@ class CommissionHandler
         if ($crossShopping && $crossShoppingFactures != "") {
             $sql .= " f.ref in ({$crossShoppingFactures}) AND";
         } else {
+            $sql .= " EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
             if ($crossShoppingFactures != "") {
                 $sql .= " f.ref not in ({$crossShoppingFactures}) AND";
             }

+ 46 - 12
custom/settlements/class/daily_closing.class.php

@@ -263,8 +263,21 @@ class DailyClosing
         //print $sql;
         $data = $db->query($sql);
         $result = pg_fetch_all($data);
+        $refs = "";
+        while($row = $db->fetch_object($result)){
+            if ($refs == "") {
+                $refs .= "'" . $row->ref . "'";
+            } else {
+                $refs .= ",'" . $row->ref . "'";
+            }
+        }
+        $params = ["from" => $from, "to" => $to, "factures" => $refs];
+        $postFields = json_encode($params);
+        $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefsArray', $postFields, 'POST', true);
+        //print_r($crossShoppingFactures);
         print '<table id="unprintedfactureslist" style="display:none; width:100%;">';
         print '<tr class="center unprintedfacturetr">
+        <td style="width:15%;">' . $langs->trans('CS') . '</td>
         <td style="width:15%;">' . $langs->trans('Invoice') . '</td>
         <td style="width:20%;">' . $langs->trans('Company') . '</td>
         <td style="width:20%;">' . $langs->trans('Date creation') . '</td>
@@ -274,9 +287,10 @@ class DailyClosing
         </tr>';
         if (!empty($result)) {
             foreach ($result as $record) {
-                if (!is_null($record['bbt_rowid']) && is_null($record['bbtip_rowid'])) {
-                    print '<br>';
-                print '<tr class="center">
+                $isCS = $this->checkCrossShopping($record['ref']) ? '<span class="fa fa-check"></span>' : '';
+                if (((is_null($record['bbt_rowid']) && is_null($record['bbtip_rowid'])) || (!is_null($record['bbt_rowid']) && is_null($record['bbtip_rowid']))) && !in_array($record['ref'], $crossShoppingFactures)) {
+                    print '<tr class="center">
+                <td>' . $isCS . '</td>
                 <td><a href="/compta/facture/card.php?facid=' . $record['rowid'] . '&save_lastsearch_values=1">' . $record['ref'] . '</a></td>
                 <td>' . $record['label'] . '</td>
                 <td>' . $record['datec'] . '</td>
@@ -284,7 +298,7 @@ class DailyClosing
                 <td>' . $record['multicurrency_code'] . '</td>
                 <td>' . $this->setStatut($record['fk_statut']) . '</td>
                 </tr>';
-            }
+                }
             }
         } else {
             print '<tr><td style="color:red; text-align:center;" colspan="5">' . $langs->trans('Empty') . '</td></tr>';
@@ -292,6 +306,25 @@ class DailyClosing
         print '</table>';
     }
 
+    private function checkCrossShopping($facture_id){
+        global $db, $conf;
+        $sql = "SELECT bbs.server_host FROM llx_facture as f 
+            INNER JOIN llx_facturedet as fdet ON fdet.fk_facture = f.rowid
+            INNER JOIN llx_product_extrafields as pre ON pre.fk_object = fdet.fk_product
+            INNER JOIN llx_bbus_basicservices as bbs ON bbs.rowid = cast(pre.basic_service as integer)
+            WHERE f.ref = '{$facture_id}'
+            GROUP BY bbs.server_host";
+        $result = $db->query($sql);
+        if($db->num_rows($result) > 0){
+            while($row = $db->fetch_object($result)){
+                //print $facture_id . ' -> ' . $row->server_host . ' - ' . $conf->global->CURL_SERVER_HOST . '<br>';
+                return $row->server_host == $conf->global->CURL_SERVER_HOST;
+            }
+        }
+        return false;
+
+    }
+
     function printedFactures($user_id, $date_creation)
     {
         global $db, $langs;
@@ -308,11 +341,11 @@ class DailyClosing
             f.fk_user_author = {$user_id} AND 
             f.datec BETWEEN '{$startdate}' AND '{$now}'
         ORDER BY f.rowid DESC;";
-
+        //print $sql;
         $data = $db->query($sql);
         while ($row = pg_fetch_assoc($data)) {
             if (!is_null($row['bbt_rowid']) && !is_null($row['bbtip_rowid'])) {
-            $array[] = $row['rowid'];
+                $array[] = $row['rowid'];
             } else {
                 if ($crossShoppingFacturesString == "") {
                     $crossShoppingFacturesString .= "'" . $row['ref'] . "'";
@@ -322,6 +355,7 @@ class DailyClosing
             }
         }
         $params = ["from" => $startdate, "to" => $now, "factures" => $crossShoppingFacturesString];
+        //print $crossShoppingFacturesString;
         $postFields = json_encode($params);
         $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefsArray', $postFields, 'POST', true);
         $data = $db->query($sql);
@@ -360,12 +394,12 @@ class DailyClosing
         GROUP BY tip.fk_facture, tip.product_id, invoice_number";
         $data = $db->query($sql);
         if ($db->num_rows($data) > 0) {
-        while ($row = $db->fetch_object($data)) {
-            $array[$row->fk_facture][$row->product_id] = $row->count;
-        }
-        $firstKey = array_key_first($array);
-        $firstValue = $array[$firstKey];
-        $number = reset($firstValue);
+            while ($row = $db->fetch_object($data)) {
+                $array[$row->fk_facture][$row->product_id] = $row->count;
+            }
+            $firstKey = array_key_first($array);
+            $firstValue = $array[$firstKey];
+            $number = reset($firstValue);
         } else {
             $factureObj = new Facture($db);
             $result = $factureObj->fetch($facture_id);