StatSaleClass.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatStockInClass;
  4. use biz\stat\classes\StatStockOutClass;
  5. use bizGhs\cg\classes\CgRefundClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\custom\classes\DistClass;
  8. use bizGhs\expend\classes\ExpendClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\order\classes\OrderItemClass;
  11. use bizGhs\order\classes\PurchaseOrderClass;
  12. use bizGhs\order\classes\PurchaseOrderItemClass;
  13. use bizGhs\order\classes\RefundOrderClass;
  14. use bizGhs\order\services\NextDayRefundService;
  15. use bizGhs\order\classes\StockWastageOrderClass;
  16. use bizGhs\product\classes\ProductClass;
  17. use bizGhs\staff\classes\SalaryClass;
  18. use bizHd\goods\classes\PlantCgClass;
  19. use bizHd\order\classes\OrderClass as HdOrderClass;
  20. use bizGhs\order\classes\OrderClass;
  21. use bizHd\purchase\classes\PurchaseClass;
  22. use bizHd\refund\classes\HdRefundClass;
  23. use common\components\arrayUtil;
  24. use common\components\dateUtil;
  25. use common\components\dict;
  26. use common\components\util;
  27. use Yii;
  28. use bizGhs\base\classes\BaseClass;
  29. class StatSaleClass extends BaseClass
  30. {
  31. public static $baseFile = '\bizGhs\stat\models\StatSale';
  32. public static function wastage($mainId)
  33. {
  34. $get = Yii::$app->request->get();
  35. $where = [];
  36. $where['mainId'] = $mainId;
  37. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  38. $startTime = $get['startTime'] ?? '';
  39. $endTime = $get['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  41. $start = $period['startTime'];
  42. $end = $period['endTime'];
  43. $currentStartDate = date('Y-m-d', strtotime($start));
  44. $currentEndDate = date('Y-m-d', strtotime($end));
  45. $currentStartTime = $currentStartDate . ' 00:00:00';
  46. $currentEndTime = $currentEndDate . ' 23:59:59';
  47. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  48. $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
  49. $totalPrice = 0;
  50. $totalCost = 0;
  51. if (!empty($list)) {
  52. foreach ($list as $order) {
  53. $price = $order['price'] ?? 0;
  54. $totalPrice = bcadd($totalPrice, $price, 2);
  55. $cost = $order['cost'] ?? 0;
  56. $totalCost = bcadd($totalCost, $cost, 2);
  57. }
  58. }
  59. $totalPrice = floatval($totalPrice);
  60. return ['totalPrice' => $totalPrice, 'totalCost' => $totalCost];
  61. }
  62. //结账跟踪 ssh 20230503
  63. public static function OrderClear($mainId)
  64. {
  65. $get = Yii::$app->request->get();
  66. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  67. $timeStyle = $get['timeStyle'] ?? 0;
  68. if (isset($get['searchTime']) && !empty($get['searchTime'])) {
  69. $searchTime = $get['searchTime'];
  70. $startTime = $get['startTime'] ?? '';
  71. $endTime = $get['endTime'] ?? '';
  72. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  73. $start = $period['startTime'];
  74. $end = $period['endTime'];
  75. $currentStartDate = date('Y-m-d', strtotime($start));
  76. $currentEndDate = date('Y-m-d', strtotime($end));
  77. $currentStartTime = $currentStartDate . ' 00:00:00';
  78. $currentEndTime = $currentEndDate . ' 23:59:59';
  79. if ($timeStyle == 0) {
  80. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  81. } else {
  82. $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
  83. }
  84. } else {
  85. $payTime = 'today';
  86. $startTime = $get['startTime'] ?? '';
  87. $endTime = $get['endTime'] ?? '';
  88. $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
  89. $start = $period['startTime'];
  90. $end = $period['endTime'];
  91. $currentStartDate = date('Y-m-d', strtotime($start));
  92. $currentEndDate = date('Y-m-d', strtotime($end));
  93. $currentStartTime = $currentStartDate . ' 00:00:00';
  94. $currentEndTime = $currentEndDate . ' 23:59:59';
  95. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  96. }
  97. $customId = $get['customId'] ?? 0;
  98. if (!empty($customId)) {
  99. $where['customId'] = $customId;
  100. }
  101. $unClear = $get['unClear'] ?? 0;
  102. if ($unClear == 1) {
  103. $where['clearId'] = 0;
  104. $where['remainDebtPrice>'] = 0;
  105. }
  106. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  107. return ['list' => $ghsOrderList];
  108. }
  109. //客户下单榜 ssh 20230502
  110. public static function statCustomCg($mainId)
  111. {
  112. $get = Yii::$app->request->get();
  113. $where = [];
  114. $where['mainId'] = $mainId;
  115. $searchTime = !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  116. $startTime = $get['startTime'] ?? '';
  117. $endTime = $get['endTime'] ?? '';
  118. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  119. $start = $period['startTime'];
  120. $end = $period['endTime'];
  121. $where['time'] = ['between', [$start, $end]];
  122. $currentStartDate = date('Y-m-d', strtotime($start));
  123. $currentEndDate = date('Y-m-d', strtotime($end));
  124. $currentStartTime = $currentStartDate . ' 00:00:00';
  125. $currentEndTime = $currentEndDate . ' 23:59:59';
  126. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  127. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  128. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
  129. $arr = [];
  130. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  131. $totalAmount = 0;
  132. $totalNum = 0;
  133. $totalProfit = 0;
  134. $totalCost = 0;
  135. if (!empty($ghsOrderList)) {
  136. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  137. $actPrice = $ghsOrder['actPrice'] ?? 0;
  138. if ($actPrice <= 0) {
  139. continue;
  140. }
  141. $cost = $ghsOrder['remainCost'] ?? 0;
  142. $profit = bcsub($actPrice, $cost, 2);
  143. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  144. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  145. $customId = $ghsOrder['customId'] ?? 0;
  146. $status = $ghsOrder['status'] ?? 0;
  147. $book = $ghsOrder['book'] ?? 0;
  148. //扎数
  149. $count = $ghsOrder['itemNum'] ?? 0;
  150. if ($status == 1 || $status == 5) {
  151. continue;
  152. }
  153. if ($book == 1 && $status == 2) {
  154. continue;
  155. }
  156. $customName = $customList[$customId]['name'] ?? '';
  157. $py = $customList[$customId]['py'] ?? '';
  158. $totalNum++;
  159. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  160. $totalProfit = bcadd($totalProfit, $profit, 2);
  161. $totalCost = bcadd($totalCost, $cost, 2);
  162. if (isset($arr[$customId])) {
  163. $arr[$customId]['num']++;
  164. $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
  165. $arr[$customId]['count'] = bcadd($arr[$customId]['count'], $count, 2);
  166. $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
  167. $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
  168. $arr[$customId]['cost'] = bcadd($arr[$customId]['cost'], $cost, 2);
  169. $arr[$customId]['profit'] = bcadd($arr[$customId]['profit'], $profit, 2);
  170. } else {
  171. $arr[$customId] = [
  172. 'customId' => $customId,
  173. 'customName' => $customName,
  174. 'py' => $py,
  175. 'num' => 1,
  176. 'count' => $count,
  177. 'amount' => $actPrice,
  178. 'levelName' => '',
  179. 'orderPrice' => $orderPrice,
  180. 'tkPrice' => $tkPrice,
  181. 'cost' => $cost,
  182. 'profit' => $profit,
  183. ];
  184. }
  185. }
  186. // 隔天冲销扣客户采购金额(笔数不减)
  187. $fwdMap = NextDayRefundService::sumAmountGroupByCustom($mainId, $currentStartTime, $currentEndTime);
  188. foreach ($fwdMap as $cid => $amt) {
  189. if (!isset($arr[$cid])) {
  190. continue;
  191. }
  192. $arr[$cid]['amount'] = bcsub((string)$arr[$cid]['amount'], (string)$amt, 2);
  193. $arr[$cid]['profit'] = bcsub((string)$arr[$cid]['profit'], (string)$amt, 2);
  194. $totalAmount = bcsub((string)$totalAmount, (string)$amt, 2);
  195. $totalProfit = bcsub((string)$totalProfit, (string)$amt, 2);
  196. }
  197. $arr = arrayUtil::arraySort($arr, 'profit');
  198. }
  199. $totalAmount = floatval($totalAmount);
  200. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum, 'totalCost' => $totalCost, 'totalProfit' => $totalProfit];
  201. }
  202. public static function profile($mainId)
  203. {
  204. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  205. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  206. $get = Yii::$app->request->get();
  207. $where = [];
  208. $where['mainId'] = $mainId;
  209. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  210. $startTime = $get['startTime'] ?? '';
  211. $endTime = $get['endTime'] ?? '';
  212. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  213. $start = $period['startTime'];
  214. $end = $period['endTime'];
  215. $where['time'] = ['between', [$start, $end]];
  216. $currentStartDate = date('Y-m-d', strtotime($start));
  217. $currentEndDate = date('Y-m-d', strtotime($end));
  218. $currentStartTime = $currentStartDate . ' 00:00:00';
  219. $currentEndTime = $currentEndDate . ' 23:59:59';
  220. //出库
  221. $stockOut = StatStockOutClass::getSum($where);
  222. //采购入库
  223. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  224. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  225. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  226. $cg = 0;
  227. if (!empty($cgList)) {
  228. foreach ($cgList as $cgKey => $cgItem) {
  229. $actPrice = $cgItem['actPrice'] ?? 0;
  230. $paidPrice = $cgItem['paidPrice'] ?? 0;
  231. if ($actPrice <= 0) {
  232. // 如果$actPrice=0,表示这个采购单已经全部退款,不需要再统计成本,本来$paidPrice要同步清零,但没有,这个问题由徐州邂逅花坊马总,提出的需求发现
  233. $cg = bcadd($cg, 0, 2);
  234. } else {
  235. $total = bcadd($actPrice, $paidPrice, 2);
  236. //物流费用不算成本。意思是说,长短途运费+提货费+本地运费,这四项和打包费一起,参与到平摊花材运费成本的计算,但那四项不参与总的采购成本登记,因为,有另外地方会独立登记运费成本。
  237. //$paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  238. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  239. if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
  240. $shortCharge = $cgItem['shortCharge'] ?? 0;
  241. $longCharge = $cgItem['longCharge'] ?? 0;
  242. $pickCharge = $cgItem['pickCharge'] ?? 0;
  243. $localCharge = $cgItem['localCharge'] ?? 0;
  244. $a1 = bcadd($shortCharge, $longCharge, 2);
  245. $a2 = bcadd($pickCharge, $localCharge, 2);
  246. $a = bcadd($a1, $a2, 2);
  247. //注意下面是减号 bcsub
  248. $total = bcsub($total, $a, 2);
  249. }
  250. $cg = bcadd($cg, $total, 2);
  251. }
  252. }
  253. }
  254. $cg = floatval($cg);
  255. $lsCgWhere = ['mainId' => $mainId, 'status' => 4, 'belongCost' => 1];
  256. $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  257. $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
  258. $lsCgAmount = 0;
  259. if (!empty($lsCgList)) {
  260. foreach ($lsCgList as $lsCgItem) {
  261. $actPrice = $lsCgItem['actPrice'] ?? 0;
  262. $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
  263. }
  264. }
  265. $lsCgAmount = floatval($lsCgAmount);
  266. $lzWhere = ['mainId' => $mainId, 'status' => 3];
  267. $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  268. $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
  269. $lzCgAmount = 0;
  270. if (!empty($lzCgList)) {
  271. foreach ($lzCgList as $lzInfo) {
  272. $actPrice = $lzInfo['actPrice'] ?? 0;
  273. $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
  274. }
  275. }
  276. $lzCgAmount = floatval($lzCgAmount);
  277. //调拨入库
  278. $stockIn = StatStockInClass::getSum($where);
  279. $totalOrderNum = 0;
  280. //批发开单
  281. $pfIncome = 0;
  282. $pfSendCost = 0;
  283. $pfPack = 0;
  284. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  285. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  286. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  287. if (!empty($ghsOrderList)) {
  288. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  289. $actPrice = $ghsOrder['actPrice'] ?? 0;
  290. $sendCost = $ghsOrder['sendCost'] ?? 0;
  291. $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
  292. $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
  293. $packCost = $ghsOrder['packCost'] ?? 0;
  294. $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
  295. $remainPackCost = bcsub($packCost, $refundPackCost, 2);
  296. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  297. $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
  298. $pfPack = bcadd($pfPack, $remainPackCost, 2);
  299. $totalOrderNum++;
  300. }
  301. }
  302. // 隔天冲销扣减批发收入(笔数不变)
  303. $pfIncome = bcsub(
  304. (string)$pfIncome,
  305. NextDayRefundService::sumAmountByMainAndTime($mainId, $currentStartTime, $currentEndTime),
  306. 2
  307. );
  308. //零售开单
  309. $payCodeIncome = 0;
  310. $lsAfterSale = 0;
  311. $lsGiveBalance = 0;
  312. $lsIncome = 0;
  313. $lsSendCost = 0;
  314. $lsServiceFee = 0;
  315. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  316. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  317. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  318. foreach ($hdOrderList as $hdKey => $hdOrder) {
  319. $actPrice = $hdOrder['mainPay'] ?? 0;
  320. $onlinePay = $hdOrder['onlinePay'] ?? 1;
  321. $repeat = $hdOrder['repeat'] ?? 0;
  322. $forward = $hdOrder['forward'] ?? 0;
  323. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  324. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  325. }
  326. if ($forward == 1) {
  327. //零售售后付款
  328. $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
  329. } else {
  330. $cash = $hdOrder['cash'] ?? 0;
  331. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  332. $sendCost = $hdOrder['sendCost'] ?? 0;
  333. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  334. $lsIncome = bcadd($lsIncome, $cash, 2);
  335. //如果$actPrice<=0,则退款取消先不计算成本
  336. if ($actPrice > 0) {
  337. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  338. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  339. }
  340. $totalOrderNum++;
  341. if ($onlinePay == 2 && floatval($cash) <= 0 && $repeat == 1) {
  342. $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
  343. $payCodeIncome = floatval($payCodeIncome);
  344. }
  345. $giveBalance = $hdOrder['giveBalance'] ?? 0;
  346. $giveBalanceTk = $hdOrder['giveBalanceTk'] ?? 0;
  347. $remainGiveBalance = bcsub($giveBalance, $giveBalanceTk, 2);
  348. $lsGiveBalance = bcadd($lsGiveBalance, $remainGiveBalance, 2);
  349. }
  350. }
  351. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  352. $salary = 0;
  353. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  354. if (!empty($salaryList)) {
  355. foreach ($salaryList as $item) {
  356. $salary = bcadd($salary, $item->amount, 2);
  357. }
  358. }
  359. $condition = [
  360. 'mainId' => $mainId,
  361. 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
  362. 'delStatus' => 0,
  363. ];
  364. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  365. $expendTypeMap = dict::getDict('expendTypeMap');
  366. $expendAmount = [];
  367. if (!empty($expendList)) {
  368. foreach ($expendList as $item) {
  369. $type = $item->type ?? 0;
  370. $amount = $item->amount ?? 0;
  371. $name = $expendTypeMap[$type] ?? '暂无';
  372. if (!isset($expendAmount[$type]['amount'])) {
  373. $expendAmount[$type]['amount'] = 0;
  374. }
  375. $expendAmount[$type]['name'] = $name;
  376. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  377. }
  378. }
  379. $income = [
  380. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  381. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  382. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  383. ];
  384. $expend = [
  385. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  386. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  387. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  388. ['name' => '零售售后付款', 'id' => 'lsSendCost', 'amount' => floatval($lsAfterSale)],
  389. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  390. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  391. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  392. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  393. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  394. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  395. ['name' => '收款码收入', 'id' => 'salary', 'amount' => floatval($payCodeIncome)],
  396. ['name' => '零售充值赠送', 'id' => 'salary', 'amount' => floatval($lsGiveBalance)],
  397. ];
  398. if (!empty($expendAmount)) {
  399. foreach ($expendAmount as $it) {
  400. $expend[] = $it;
  401. }
  402. }
  403. $balance = 0;
  404. foreach ($income as $it) {
  405. $amount = $it['amount'] ?? 0;
  406. $balance = bcadd($amount, $balance, 2);
  407. }
  408. foreach ($expend as $it) {
  409. $amount = $it['amount'] ?? 0;
  410. $balance = bcsub($balance, $amount, 2);
  411. }
  412. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
  413. }
  414. //按花材统计收入 ssh 20231111
  415. public static function incomeOutItem($mainId)
  416. {
  417. $get = Yii::$app->request->get();
  418. $export = $get['export'] ?? 0;
  419. $where = [];
  420. $where['mainId'] = $mainId;
  421. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  422. $startTime = $get['startTime'] ?? '';
  423. $endTime = $get['endTime'] ?? '';
  424. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  425. $start = $period['startTime'];
  426. $end = $period['endTime'];
  427. $where['time'] = ['between', [$start, $end]];
  428. $currentStartDate = date('Y-m-d', strtotime($start));
  429. $currentEndDate = date('Y-m-d', strtotime($end));
  430. $currentStartTime = $currentStartDate . ' 00:00:00';
  431. $currentEndTime = $currentEndDate . ' 23:59:59';
  432. $s = strtotime($currentStartTime);
  433. $e = strtotime($currentEndTime);
  434. $p = bcsub($e, $s);
  435. $xx = bcmul(86400, 90);
  436. if ($p > $xx) {
  437. util::fail('查询时间不能超过三个月,编号666');
  438. }
  439. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  440. if (empty($productList)) {
  441. util::fail('没有找到花材');
  442. }
  443. $arr = [];
  444. //打包费和运费
  445. $packFreight = 0;
  446. $onlyRefundCg = 0;
  447. $onlyRefund = 0;
  448. $totalDiscountAmount = 0;
  449. $totalLabourCost = 0;
  450. //采购入库
  451. $tbCgIncome = [];
  452. $tbCgIncomeStaff = [];
  453. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  454. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  455. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  456. if (!empty($cgList)) {
  457. foreach ($cgList as $cgKey => $cgItem) {
  458. $packingCharge = $cgItem['packingCharge'] ?? 0;
  459. $shortCharge = $cgItem['shortCharge'] ?? 0;
  460. $longCharge = $cgItem['longCharge'] ?? 0;
  461. $pickCharge = $cgItem['pickCharge'] ?? 0;
  462. $localCharge = $cgItem['localCharge'] ?? 0;
  463. $packFreight = bcadd($packFreight, $packingCharge, 2);
  464. $packFreight = bcadd($packFreight, $shortCharge, 2);
  465. $packFreight = bcadd($packFreight, $longCharge, 2);
  466. $packFreight = bcadd($packFreight, $pickCharge, 2);
  467. $packFreight = bcadd($packFreight, $localCharge, 2);
  468. $tkPrice = $cgItem['tkPrice'] ?? 0;
  469. $orderSn = $cgItem['orderSn'] ?? '';
  470. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  471. if (!empty($cgItemInfoList)) {
  472. foreach ($cgItemInfoList as $cgData) {
  473. $itemNum = $cgData->itemNum ?? 0;
  474. $productId = $cgData->productId ?? 0;
  475. $refundNum = $cgData->refundNum ?? 0;
  476. $remainNum = bcsub($itemNum, $refundNum);
  477. $bigPrice = $cgData->bigPrice ?? 0;
  478. $amount = bcmul($remainNum, $bigPrice, 2);
  479. $itemName = $productList[$productId]['name'] ?? '已删除';
  480. $arr[$productId]['name'] = $itemName;
  481. $arr[$productId]['id'] = $productId;
  482. if (isset($arr[$productId]['expend'])) {
  483. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  484. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  485. } else {
  486. $arr[$productId]['expend'] = $amount;
  487. $arr[$productId]['outNum'] = $remainNum;
  488. $arr[$productId]['inNum'] = 0;
  489. $arr[$productId]['income'] = 0;
  490. }
  491. }
  492. if ($tkPrice > 0) {
  493. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  494. if (!empty($cgRefundOrderList)) {
  495. foreach ($cgRefundOrderList as $cgRefundOrder) {
  496. if ($cgRefundOrder->refundType == 2) {
  497. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  498. }
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505. //批发开单
  506. $pfIncome = 0;
  507. $tbSaleIncome = [];
  508. $tbSaleIncomeStaff = [];
  509. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  510. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  511. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  512. if (!empty($ghsOrderList)) {
  513. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  514. $book = $ghsOrder['book'] ?? 0;
  515. $status = $ghsOrder['status'] ?? 0;
  516. if ($book == 1 && $status == 2) {
  517. //预订单待发货的不算收入
  518. continue;
  519. }
  520. if ($status == 0 || $status == 5) {
  521. continue;
  522. }
  523. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  524. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  525. $orderSn = $ghsOrder['orderSn'] ?? '';
  526. $actPrice = $ghsOrder['actPrice'] ?? 0;
  527. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  528. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  529. $labourCost = $ghsOrder['labourCost'] ?? 0;
  530. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  531. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  532. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  533. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  534. if (!empty($orderItemList)) {
  535. foreach ($orderItemList as $orderItem) {
  536. $refundNum = $orderItem->refundNum ?? 0;
  537. $name = $orderItem->name ?? '';
  538. $productId = $orderItem->productId ?? 0;
  539. $num = $orderItem->xhNum ?? 0;
  540. $remainNum = bcsub($num, $refundNum);
  541. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  542. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  543. $itemName = $productList[$productId]['name'] ?? '已删除';
  544. $arr[$productId]['name'] = $itemName;
  545. $arr[$productId]['id'] = $productId;
  546. if (isset($arr[$productId]['income'])) {
  547. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  548. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  549. } else {
  550. $arr[$productId]['expend'] = 0;
  551. $arr[$productId]['outNum'] = 0;
  552. $arr[$productId]['inNum'] = $remainNum;
  553. $arr[$productId]['income'] = $amount;
  554. }
  555. //单独统计
  556. $belongCost = $orderItem->belongCost ?? 0;
  557. if ($belongCost == 1) {
  558. if (isset($tbSaleIncome[$productId])) {
  559. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  560. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  561. } else {
  562. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  563. }
  564. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  565. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  566. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  567. } else {
  568. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  569. }
  570. }
  571. }
  572. if ($tkPrice > 0) {
  573. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  574. if (!empty($refundList)) {
  575. foreach ($refundList as $refund) {
  576. if ($refund->refundType == 1) {
  577. //退货退款前面已经考虑,这里不再处理 OK
  578. } else {
  579. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  580. }
  581. }
  582. }
  583. }
  584. }
  585. }
  586. }
  587. //零售开单
  588. $lsIncome = 0;
  589. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  590. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  591. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  592. foreach ($hdOrderList as $hdKey => $hdOrder) {
  593. $actPrice = $hdOrder['mainPay'] ?? 0;
  594. $cash = $hdOrder['cash'] ?? 0;
  595. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  596. $lsIncome = bcadd($lsIncome, $cash, 2);
  597. $orderSn = $hdOrder['orderSn'] ?? '';
  598. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  599. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  600. if (!empty($orderItemList)) {
  601. foreach ($orderItemList as $orderItem) {
  602. $unitPrice = $orderItem->unitPrice ?? 0;
  603. $refundNum = $orderItem->refundNum ?? 0;
  604. $num = $orderItem->num ?? 0;
  605. $productId = $orderItem->itemId ?? 0;
  606. $remainNum = bcsub($num, $refundNum);
  607. $amount = bcmul($remainNum, $unitPrice, 2);
  608. $itemName = $productList[$productId]['name'] ?? '已删除';
  609. $arr[$productId]['name'] = $itemName;
  610. $arr[$productId]['id'] = $productId;
  611. if (isset($arr[$productId]['income'])) {
  612. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  613. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  614. } else {
  615. $arr[$productId]['expend'] = 0;
  616. $arr[$productId]['outNum'] = 0;
  617. $arr[$productId]['inNum'] = $remainNum;
  618. $arr[$productId]['income'] = $amount;
  619. }
  620. }
  621. if ($tkPrice > 0) {
  622. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  623. if (!empty($refundList)) {
  624. foreach ($refundList as $refund) {
  625. if ($refund->refundType == 2) {
  626. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  627. }
  628. }
  629. }
  630. }
  631. }
  632. }
  633. $totalIncome = 0;
  634. $totalExpend = 0;
  635. if (!empty($arr)) {
  636. foreach ($arr as $key => $val) {
  637. $income = $val['income'] ?? 0;
  638. $income = floatval($income);
  639. $totalIncome = bcadd($totalIncome, $income, 2);
  640. $expend = $val['expend'] ?? 0;
  641. $expend = floatval($expend);
  642. $totalExpend = bcadd($totalExpend, $expend, 2);
  643. $profit = bcsub($income, $expend, 2);
  644. $arr[$key]['profit'] = floatval($profit);
  645. $arr[$key]['income'] = floatval($income);
  646. $arr[$key]['expend'] = floatval($expend);
  647. }
  648. }
  649. //总成本算上物流打包等费用
  650. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  651. //总收入算上采购仅退款
  652. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  653. //总收入加上人工费
  654. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  655. //总收入减掉优惠金额
  656. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  657. //总成本算上销售仅退款
  658. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  659. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  660. $totalIncome = floatval($totalIncome);
  661. $totalExpend = floatval($totalExpend);
  662. $totalProfit = floatval($totalProfit);
  663. $arr = array_values($arr);
  664. $arr = arrayUtil::arraySort($arr, 'profit');
  665. if ($export == 1) {
  666. $statData = [
  667. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  668. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  669. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  670. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  671. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  672. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  673. ];
  674. $list = array_merge($arr, $statData);
  675. self::exportIncomeOutItem($list, $mainId);
  676. }
  677. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  678. 'totalIncome' => $totalIncome,
  679. 'totalExpend' => $totalExpend,
  680. 'totalProfit' => $totalProfit,
  681. 'tbSaleIncome' => $tbSaleIncome,
  682. 'tbCgIncome' => $tbCgIncome,
  683. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  684. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  685. 'totalLabourCost' => $totalLabourCost,
  686. 'totalDiscountAmount' => $totalDiscountAmount,
  687. ]);
  688. }
  689. //按分类统计收入 ssh 20231111
  690. public static function classProfile($mainId)
  691. {
  692. $get = Yii::$app->request->get();
  693. $export = $get['export'] ?? 0;
  694. $where = [];
  695. $where['mainId'] = $mainId;
  696. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  697. $startTime = $get['startTime'] ?? '';
  698. $endTime = $get['endTime'] ?? '';
  699. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  700. $start = $period['startTime'];
  701. $end = $period['endTime'];
  702. $where['time'] = ['between', [$start, $end]];
  703. $currentStartDate = date('Y-m-d', strtotime($start));
  704. $currentEndDate = date('Y-m-d', strtotime($end));
  705. $currentStartTime = $currentStartDate . ' 00:00:00';
  706. $currentEndTime = $currentEndDate . ' 23:59:59';
  707. $s = strtotime($currentStartTime);
  708. $e = strtotime($currentEndTime);
  709. $p = bcsub($e, $s);
  710. $xx = bcmul(86400, 90);
  711. if ($p > $xx) {
  712. util::fail('查询时间不能超过三个月,编号780');
  713. }
  714. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  715. if (empty($classList)) {
  716. util::fail('没有找到花材');
  717. }
  718. $arr = [];
  719. //打包费和运费
  720. $packFreight = 0;
  721. $onlyRefundCg = 0;
  722. $onlyRefund = 0;
  723. $totalDiscountAmount = 0;
  724. $totalLabourCost = 0;
  725. //采购入库
  726. $tbCgIncome = [];
  727. $tbCgIncomeStaff = [];
  728. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  729. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  730. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  731. if (!empty($cgList)) {
  732. foreach ($cgList as $cgKey => $cgItem) {
  733. $packingCharge = $cgItem['packingCharge'] ?? 0;
  734. $shortCharge = $cgItem['shortCharge'] ?? 0;
  735. $longCharge = $cgItem['longCharge'] ?? 0;
  736. $pickCharge = $cgItem['pickCharge'] ?? 0;
  737. $localCharge = $cgItem['localCharge'] ?? 0;
  738. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  739. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  740. $actPrice = $cgItem['actPrice'] ?? 0;
  741. if ($actPrice <= 0) {
  742. continue;
  743. }
  744. $packFreight = bcadd($packFreight, $packingCharge, 2);
  745. $packFreight = bcadd($packFreight, $shortCharge, 2);
  746. $packFreight = bcadd($packFreight, $longCharge, 2);
  747. $packFreight = bcadd($packFreight, $pickCharge, 2);
  748. $packFreight = bcadd($packFreight, $localCharge, 2);
  749. $tkPrice = $cgItem['tkPrice'] ?? 0;
  750. $orderSn = $cgItem['orderSn'] ?? '';
  751. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  752. if (!empty($cgItemInfoList)) {
  753. foreach ($cgItemInfoList as $cgData) {
  754. $itemNum = $cgData->itemNum ?? 0;
  755. $productId = $cgData->productId ?? 0;
  756. $name = $cgData->name ?? '';
  757. $refundNum = $cgData->refundNum ?? 0;
  758. $remainNum = bcsub($itemNum, $refundNum);
  759. $bigPrice = $cgData->bigPrice ?? 0;
  760. $amount = bcmul($remainNum, $bigPrice, 2);
  761. $classId = $cgData->classId;
  762. $className = $classList[$classId]['name'] ?? '已删除';
  763. if ($className == '运费') {
  764. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  765. }
  766. $arr[$classId]['className'] = $className;
  767. $arr[$classId]['classId'] = $classId;
  768. if (isset($arr[$classId]['expend'])) {
  769. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  770. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  771. } else {
  772. $arr[$classId]['expend'] = $amount;
  773. $arr[$classId]['income'] = 0;
  774. $arr[$classId]['cgNum'] = $remainNum;
  775. $arr[$classId]['saleNum'] = 0;
  776. }
  777. //单独统计
  778. $belongCost = $cgData->belongCost ?? 0;
  779. if ($belongCost == 1) {
  780. if (isset($tbCgIncome[$productId])) {
  781. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  782. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  783. } else {
  784. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  785. }
  786. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  787. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  788. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  789. } else {
  790. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  791. }
  792. }
  793. }
  794. if ($tkPrice > 0) {
  795. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  796. if (!empty($cgRefundOrderList)) {
  797. foreach ($cgRefundOrderList as $cgRefundOrder) {
  798. if ($cgRefundOrder->refundType == 2) {
  799. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  800. }
  801. }
  802. }
  803. }
  804. }
  805. }
  806. }
  807. //批发开单
  808. $pfIncome = 0;
  809. $tbSaleIncome = [];
  810. $tbSaleIncomeStaff = [];
  811. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  812. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  813. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  814. if (!empty($ghsOrderList)) {
  815. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  816. $book = $ghsOrder['book'] ?? 0;
  817. $status = $ghsOrder['status'] ?? 0;
  818. if ($book == 1 && $status == 2) {
  819. //预订单待发货的不算收入
  820. continue;
  821. }
  822. if ($status == 0 || $status == 5) {
  823. continue;
  824. }
  825. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  826. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  827. $orderSn = $ghsOrder['orderSn'] ?? '';
  828. $actPrice = $ghsOrder['actPrice'] ?? 0;
  829. if ($actPrice <= 0) {
  830. continue;
  831. }
  832. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  833. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  834. $labourCost = $ghsOrder['labourCost'] ?? 0;
  835. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  836. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  837. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  838. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  839. if (!empty($orderItemList)) {
  840. foreach ($orderItemList as $orderItem) {
  841. $refundNum = $orderItem->refundNum ?? 0;
  842. $name = $orderItem->name ?? '';
  843. $productId = $orderItem->productId ?? 0;
  844. $num = $orderItem->xhNum ?? 0;
  845. $remainNum = bcsub($num, $refundNum);
  846. $classId = $orderItem->classId ?? 0;
  847. $className = $classList[$classId]['name'] ?? '已删除';
  848. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  849. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  850. $arr[$classId]['className'] = $className;
  851. $arr[$classId]['classId'] = $classId;
  852. if (isset($arr[$classId]['income'])) {
  853. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  854. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  855. } else {
  856. $arr[$classId]['expend'] = 0;
  857. $arr[$classId]['income'] = $amount;
  858. $arr[$classId]['saleNum'] = $remainNum;
  859. $arr[$classId]['cgNum'] = 0;
  860. }
  861. //单独统计
  862. $belongCost = $orderItem->belongCost ?? 0;
  863. if ($belongCost == 1) {
  864. if (isset($tbSaleIncome[$productId])) {
  865. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  866. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  867. } else {
  868. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  869. }
  870. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  871. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  872. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  873. } else {
  874. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  875. }
  876. }
  877. }
  878. if ($tkPrice > 0) {
  879. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  880. if (!empty($refundList)) {
  881. foreach ($refundList as $refund) {
  882. if ($refund->refundType == 1) {
  883. //退货退款前面已经考虑,这里不再处理 OK
  884. } else {
  885. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  886. }
  887. }
  888. }
  889. }
  890. }
  891. }
  892. }
  893. //零售开单
  894. $lsIncome = 0;
  895. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  896. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  897. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  898. foreach ($hdOrderList as $hdKey => $hdOrder) {
  899. $actPrice = $hdOrder['mainPay'] ?? 0;
  900. $cash = $hdOrder['cash'] ?? 0;
  901. if ($actPrice <= 0 && $cash <= 0) {
  902. continue;
  903. }
  904. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  905. $lsIncome = bcadd($lsIncome, $cash, 2);
  906. $orderSn = $hdOrder['orderSn'] ?? '';
  907. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  908. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  909. if (!empty($orderItemList)) {
  910. foreach ($orderItemList as $orderItem) {
  911. $classId = $orderItem->classId ?? 0;
  912. $unitPrice = $orderItem->unitPrice ?? 0;
  913. $refundNum = $orderItem->refundNum ?? 0;
  914. $num = $orderItem->num ?? 0;
  915. $productId = $orderItem->itemId ?? 0;
  916. $name = $orderItem->name ?? '';
  917. $remainNum = bcsub($num, $refundNum);
  918. $amount = bcmul($remainNum, $unitPrice, 2);
  919. $className = $classList[$classId]['name'] ?? '已删除';
  920. $arr[$classId]['className'] = $className;
  921. $arr[$classId]['classId'] = $classId;
  922. if (isset($arr[$classId]['income'])) {
  923. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  924. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  925. } else {
  926. $arr[$classId]['expend'] = 0;
  927. $arr[$classId]['income'] = $amount;
  928. $arr[$classId]['saleNum'] = $remainNum;
  929. $arr[$classId]['cgNum'] = 0;
  930. }
  931. $belongCost = $orderItem->belongCost ?? 0;
  932. if ($belongCost == 1) {
  933. if (isset($tbSaleIncome[$productId])) {
  934. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  935. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  936. } else {
  937. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  938. }
  939. }
  940. }
  941. if ($tkPrice > 0) {
  942. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  943. if (!empty($refundList)) {
  944. foreach ($refundList as $refund) {
  945. if ($refund->refundType == 2) {
  946. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  947. }
  948. }
  949. }
  950. }
  951. }
  952. }
  953. $totalIncome = 0;
  954. $totalExpend = 0;
  955. if (!empty($arr)) {
  956. foreach ($arr as $key => $val) {
  957. $income = $val['income'] ?? 0;
  958. $income = floatval($income);
  959. $totalIncome = bcadd($totalIncome, $income, 2);
  960. $expend = $val['expend'] ?? 0;
  961. $expend = floatval($expend);
  962. $totalExpend = bcadd($totalExpend, $expend, 2);
  963. $profit = bcsub($income, $expend, 2);
  964. $arr[$key]['profit'] = floatval($profit);
  965. $arr[$key]['income'] = floatval($income);
  966. $arr[$key]['expend'] = floatval($expend);
  967. }
  968. }
  969. //总成本算上物流打包等费用
  970. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  971. //总收入算上采购仅退款
  972. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  973. //总收入加上人工费
  974. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  975. //总收入减掉优惠金额
  976. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  977. //总成本算上销售仅退款
  978. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  979. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  980. $totalIncome = floatval($totalIncome);
  981. $totalExpend = floatval($totalExpend);
  982. $totalProfit = floatval($totalProfit);
  983. $arr = array_values($arr);
  984. $arr = arrayUtil::arraySort($arr, 'profit');
  985. if ($export == 1) {
  986. $statData = [
  987. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  988. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  989. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  990. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  991. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  992. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  993. ];
  994. $list = array_merge($arr, $statData);
  995. self::exportIncomeClass($list, $mainId);
  996. }
  997. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  998. 'totalIncome' => $totalIncome,
  999. 'totalExpend' => $totalExpend,
  1000. 'totalProfit' => $totalProfit,
  1001. 'tbSaleIncome' => $tbSaleIncome,
  1002. 'tbCgIncome' => $tbCgIncome,
  1003. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  1004. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  1005. 'totalLabourCost' => $totalLabourCost,
  1006. 'totalDiscountAmount' => $totalDiscountAmount,
  1007. ]);
  1008. }
  1009. public static function exportIncomeOutItem($list, $mainId)
  1010. {
  1011. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1012. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1013. $objPHPExcel = new \PHPExcel();
  1014. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1015. ->setLastModifiedBy("Maarten Balliauw")
  1016. ->setTitle("Office 2007 XLSX Document")
  1017. ->setSubject("Office 2007 XLSX Document")
  1018. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1019. ->setKeywords("office 2007 openxml php")
  1020. ->setCategory("file");
  1021. $ghsTitle = '供货商';
  1022. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1023. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1024. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1025. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1026. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1027. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1028. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1029. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1030. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  1031. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  1032. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  1033. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  1034. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1035. //设置宽度
  1036. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1037. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1038. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1039. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1040. //加粗
  1041. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1042. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1043. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1044. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1045. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1046. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1047. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1048. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1049. $baseRow = 2;
  1050. foreach ($list as $key => $custom) {
  1051. $i = $baseRow + $key;
  1052. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1053. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  1054. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  1055. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  1056. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  1057. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1058. //居左
  1059. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1060. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1061. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1062. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1063. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1064. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1065. }
  1066. $fileName = '收支花材-' . date("m-d");
  1067. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1068. $objPHPExcel->setActiveSheetIndex(0);
  1069. $dir = './priceTable/' . $mainId;
  1070. if (file_exists($dir) == false) {
  1071. mkdir($dir, 0777, true);
  1072. }
  1073. $date = $fileName;
  1074. $file = $date . '.xls';
  1075. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1076. if (file_exists($dir . '/' . $file)) {
  1077. unlink($dir . '/' . $file);
  1078. }
  1079. $objWriter->save($dir . '/' . $file);
  1080. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1081. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1082. }
  1083. public static function exportIncomeClass($list, $mainId)
  1084. {
  1085. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1086. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1087. $objPHPExcel = new \PHPExcel();
  1088. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1089. ->setLastModifiedBy("Maarten Balliauw")
  1090. ->setTitle("Office 2007 XLSX Document")
  1091. ->setSubject("Office 2007 XLSX Document")
  1092. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1093. ->setKeywords("office 2007 openxml php")
  1094. ->setCategory("file");
  1095. $ghsTitle = '供货商';
  1096. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1097. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1098. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1099. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1100. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1101. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1102. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1103. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1104. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1105. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1106. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1107. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1108. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1109. //设置宽度
  1110. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1111. //加粗
  1112. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1113. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1114. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1115. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1116. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1117. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1118. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1119. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1120. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1121. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1122. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1123. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1124. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1125. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1126. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1127. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1128. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1129. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1130. $baseRow = 2;
  1131. foreach ($list as $key => $custom) {
  1132. $i = $baseRow + $key;
  1133. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1134. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1135. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1136. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1137. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1138. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1139. //居左
  1140. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1141. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1142. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1143. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1144. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1145. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1146. }
  1147. $fileName = '收支分类-' . time();
  1148. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1149. $objPHPExcel->setActiveSheetIndex(0);
  1150. $dir = './priceTable/' . $mainId;
  1151. if (file_exists($dir) == false) {
  1152. mkdir($dir, 0777, true);
  1153. }
  1154. $date = $fileName;
  1155. $file = $date . '.xls';
  1156. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1157. if (file_exists($dir . '/' . $file)) {
  1158. unlink($dir . '/' . $file);
  1159. }
  1160. $objWriter->save($dir . '/' . $file);
  1161. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1162. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1163. }
  1164. public static function exportItemSale($list, $mainId)
  1165. {
  1166. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1167. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1168. $objPHPExcel = new \PHPExcel();
  1169. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1170. ->setLastModifiedBy("Maarten Balliauw")
  1171. ->setTitle("Office 2007 XLSX Document")
  1172. ->setSubject("Office 2007 XLSX Document")
  1173. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1174. ->setKeywords("office 2007 openxml php")
  1175. ->setCategory("file");
  1176. $ghsTitle = '供货商';
  1177. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1178. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1179. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1180. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1181. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1182. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1183. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1184. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1185. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1186. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1187. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1188. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1189. //设置宽度
  1190. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1191. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1192. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1193. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1194. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1195. //加粗
  1196. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1197. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1198. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1199. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1200. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1201. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1202. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1203. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1204. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1205. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1206. $baseRow = 2;
  1207. foreach ($list as $key => $custom) {
  1208. $i = $baseRow + $key;
  1209. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1210. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1211. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1212. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1213. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1214. //居左
  1215. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1216. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1217. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1218. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1219. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1220. }
  1221. $fileName = '花材销量-' . date("m-d");
  1222. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1223. $objPHPExcel->setActiveSheetIndex(0);
  1224. $dir = './priceTable/' . $mainId;
  1225. if (file_exists($dir) == false) {
  1226. mkdir($dir, 0777, true);
  1227. }
  1228. $date = $fileName;
  1229. $file = $date . '.xls';
  1230. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1231. if (file_exists($dir . '/' . $file)) {
  1232. unlink($dir . '/' . $file);
  1233. }
  1234. $objWriter->save($dir . '/' . $file);
  1235. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1236. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1237. }
  1238. public static function send($mainId)
  1239. {
  1240. $get = Yii::$app->request->get();
  1241. $where = [];
  1242. $where['mainId'] = $mainId;
  1243. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1244. $startTime = $get['startTime'] ?? '';
  1245. $endTime = $get['endTime'] ?? '';
  1246. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1247. $start = $period['startTime'];
  1248. $end = $period['endTime'];
  1249. $where['time'] = ['between', [$start, $end]];
  1250. $currentStartDate = date('Y-m-d', strtotime($start));
  1251. $currentEndDate = date('Y-m-d', strtotime($end));
  1252. $currentStartTime = $currentStartDate . ' 00:00:00';
  1253. $currentEndTime = $currentEndDate . ' 23:59:59';
  1254. $s = strtotime($currentStartTime);
  1255. $e = strtotime($currentEndTime);
  1256. $p = bcsub($e, $s);
  1257. $xx = bcmul(86400, 90);
  1258. if ($p > $xx) {
  1259. util::fail('查询时间不能超过三个月,编号699');
  1260. }
  1261. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1262. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1263. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1264. $arr = [];
  1265. if (!empty($ghsOrderList)) {
  1266. foreach ($ghsOrderList as $ghsOrder) {
  1267. $book = $ghsOrder['book'] ?? 0;
  1268. $status = $ghsOrder['status'] ?? 0;
  1269. if ($book == 1 && $status == 2) {
  1270. //预订单待发货的不算收入
  1271. continue;
  1272. }
  1273. if ($status == 0 || $status == 5) {
  1274. continue;
  1275. }
  1276. $orderSn = $ghsOrder['orderSn'] ?? '';
  1277. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1278. if ($actPrice <= 0) {
  1279. continue;
  1280. }
  1281. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1282. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1283. if (isset($arr[$sendStaffId])) {
  1284. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1285. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1286. } else {
  1287. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1288. }
  1289. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1290. if (!empty($orderItemList)) {
  1291. foreach ($orderItemList as $orderItem) {
  1292. $refundNum = $orderItem->refundNum ?? 0;
  1293. $num = $orderItem->xhNum ?? 0;
  1294. $remainNum = bcsub($num, $refundNum);
  1295. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1296. }
  1297. }
  1298. }
  1299. }
  1300. if (!empty($arr)) {
  1301. $arr = array_values($arr);
  1302. }
  1303. return $arr;
  1304. }
  1305. public static function customDistStat($mainId)
  1306. {
  1307. $get = Yii::$app->request->get();
  1308. $where = [];
  1309. $where['mainId'] = $mainId;
  1310. $searchTime = !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1311. $startTime = $get['startTime'] ?? '';
  1312. $endTime = $get['endTime'] ?? '';
  1313. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1314. $start = $period['startTime'];
  1315. $end = $period['endTime'];
  1316. $where['time'] = ['between', [$start, $end]];
  1317. $currentStartDate = date('Y-m-d', strtotime($start));
  1318. $currentEndDate = date('Y-m-d', strtotime($end));
  1319. $currentStartTime = $currentStartDate . ' 00:00:00';
  1320. $currentEndTime = $currentEndDate . ' 23:59:59';
  1321. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1322. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1323. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,distId', 'id');
  1324. $distMap = DistClass::getAllByCondition(['mainId' => $mainId], null, '*', 'id');
  1325. $arr = [];
  1326. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1327. $totalAmount = 0;
  1328. $totalNum = 0;
  1329. $totalProfit = 0;
  1330. $totalCost = 0;
  1331. if (!empty($ghsOrderList)) {
  1332. foreach ($ghsOrderList as $ghsOrder) {
  1333. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1334. if ($actPrice <= 0) {
  1335. continue;
  1336. }
  1337. $cost = $ghsOrder['remainCost'] ?? 0;
  1338. $profit = bcsub($actPrice, $cost, 2);
  1339. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  1340. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  1341. $customId = $ghsOrder['customId'] ?? 0;
  1342. $status = $ghsOrder['status'] ?? 0;
  1343. $book = $ghsOrder['book'] ?? 0;
  1344. //扎数
  1345. $count = $ghsOrder['itemNum'] ?? 0;
  1346. if ($status == 1 || $status == 5) {
  1347. continue;
  1348. }
  1349. if ($book == 1 && $status == 2) {
  1350. continue;
  1351. }
  1352. $distId = $customList[$customId]['distId'] ?? 0;
  1353. $distName = isset($distMap[$distId]) ? $distMap[$distId]['name'] : '未划片区';
  1354. $totalNum++;
  1355. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  1356. $totalProfit = bcadd($totalProfit, $profit, 2);
  1357. $totalCost = bcadd($totalCost, $cost, 2);
  1358. if (isset($arr[$distId])) {
  1359. $arr[$distId]['num']++;
  1360. $arr[$distId]['amount'] = bcadd($arr[$distId]['amount'], $actPrice, 2);
  1361. $arr[$distId]['orderPrice'] = bcadd($arr[$distId]['orderPrice'], $orderPrice, 2);
  1362. $arr[$distId]['count'] = bcadd($arr[$distId]['count'], $count, 2);
  1363. $arr[$distId]['tkPrice'] = bcadd($arr[$distId]['tkPrice'], $tkPrice, 2);
  1364. $arr[$distId]['cost'] = bcadd($arr[$distId]['cost'], $cost, 2);
  1365. $arr[$distId]['profit'] = bcadd($arr[$distId]['profit'], $profit, 2);
  1366. } else {
  1367. $arr[$distId] = [
  1368. 'distId' => $distId,
  1369. 'distName' => $distName,
  1370. 'num' => 1,
  1371. 'count' => $count,
  1372. 'amount' => $actPrice,
  1373. 'levelName' => '',
  1374. 'orderPrice' => $orderPrice,
  1375. 'tkPrice' => $tkPrice,
  1376. 'cost' => $cost,
  1377. 'profit' => $profit,
  1378. ];
  1379. }
  1380. }
  1381. $arr = arrayUtil::arraySort($arr, 'profit');
  1382. }
  1383. $totalAmount = floatval($totalAmount);
  1384. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum, 'totalCost' => $totalCost, 'totalProfit' => $totalProfit];
  1385. }
  1386. }