StatSaleClass.php 76 KB

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